-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatom.xml
1186 lines (890 loc) · 113 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>m00dy's place</title>
<link href="/atom.xml" rel="self"/>
<link href=""/>
<updated>2014-12-19T12:50:44+00:00</updated>
<id></id>
<author>
<name>John Doe</name>
<email>[email protected]</email>
</author>
<entry>
<title>An Analysis of ShellShock Malware</title>
<link href="/An-Analysis-of-Shell-shock-malware"/>
<updated>2014-10-03T17:00:41+00:00</updated>
<id>/An-Analysis-of-Shell-shock-malware</id>
<content type="html"><p>TL;DR
I had this malware from a gist which i dont really remember right now. I downloaded it and started to analyse what this is all about. As i analyzed so far, it&#39;s somehow broken. May be i am late ?</p>
<p>Ok lets start with file command.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">eren@lisa:~<span class="nv">$ </span>file nginx
nginx: ELF 32-bit LSB executable, Intel 80386, version <span class="m">1</span> <span class="o">(</span>SYSV<span class="o">)</span>, statically linked, <span class="k">for</span> GNU/Linux 2.6.18, stripped
eren@lisa:~<span class="err">$</span></code></pre></div>
<p>file program tells us that it&#39;s statically linked and stripped. So, Most of its dependencies are inside the code and we will have difficulties while debugging it because the debug informations have been removed.</p>
<p>We&#39;ll fire up gdb and start to debug the malware from its starting point.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">eren@lisa:~<span class="nv">$ </span>gdb nginx
GNU gdb <span class="o">(</span>GDB<span class="o">)</span> 7.4.1-debian
Copyright <span class="o">(</span>C<span class="o">)</span> <span class="m">2012</span> Free Software Foundation, Inc.
License GPLv3+: GNU GPL version <span class="m">3</span> or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type <span class="s2">&quot;show copying&quot;</span>
and <span class="s2">&quot;show warranty&quot;</span> <span class="k">for</span> details.
This GDB was configured as <span class="s2">&quot;x86_64-linux-gnu&quot;</span>.
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;...
Reading symbols from /home/eren/nginx...<span class="o">(</span>no debugging symbols found<span class="o">)</span>...done.
<span class="o">(</span>gdb<span class="o">)</span> info file
Symbols from <span class="s2">&quot;/home/eren/nginx&quot;</span>.
Local <span class="nb">exec </span>file:
<span class="sb">`</span>/home/eren/nginx<span class="err">&#39;</span>, file <span class="nb">type </span>elf32-i386.
Entry point: 0x8048160</code></pre></div>
<p>I&#39;ll put a breakpoint to 0x8048160</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">b * 0x8048160</code></pre></div>
<p>and then i&#39;ll run the executable and it&#39;ll will stop in its entry point.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> b * 0x8048160
Breakpoint <span class="m">1</span> at 0x8048160
<span class="o">(</span>gdb<span class="o">)</span> r
Starting program: /home/eren/nginx
Breakpoint 1, 0x08048160 in ?? <span class="o">()</span>
<span class="o">=</span>&gt; 0x08048160: <span class="m">31</span> ed xor ebp,ebp
<span class="o">(</span>gdb<span class="o">)</span> x/20i <span class="nv">$pc</span>
<span class="o">=</span>&gt; 0x8048160: xor ebp,ebp
0x8048162: pop esi
0x8048163: mov ecx,esp
0x8048165: and esp,0xfffffff0
0x8048168: push eax
0x8048169: push esp
0x804816a: push edx
0x804816b: push 0x804be00
0x8048170: push 0x804be40
0x8048175: push ecx
0x8048176: push esi
0x8048177: push 0x804b2cc
0x804817c: call 0x804b7c0
0x8048181: hlt</code></pre></div>
<p>From 0x8048160 to 0x8048181, the code blocks are standart way of glibc execution. Therefore, the malware programmer didnt modified the entry point. If you compile an empty helloworld program, you would see the same starting code like above.
Xoring ebp to ebp means nothing but this type of practices come from ABI (Application Binary Interface Specs). In the address of 0x804817c, the calls refers to &lt;_<em>libc</em>start_main@plt&gt; function.</p>
<p>You can look at the details <a href="http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/baselib---libc-start-main-.html">here</a>
So, first paramether to libc<em>start</em>main function is actually the last one pushed on the stack. Therefore, 0x804b2cc is our main function. Let&#39;s put a breakpoint there and go on.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">b * 0x804b2cc</code></pre></div>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">Breakpoint 2, 0x0804b2cc in ?? <span class="o">()</span>
<span class="o">=</span>&gt; 0x0804b2cc: 8d 4c <span class="m">24</span> 04 lea ecx,<span class="o">[</span>esp+0x4<span class="o">]</span>
<span class="o">(</span>gdb<span class="o">)</span> x/30i <span class="nv">$pc</span>
<span class="o">=</span>&gt; 0x804b2cc: lea ecx,<span class="o">[</span>esp+0x4<span class="o">]</span>
0x804b2d0: and esp,0xfffffff0
0x804b2d3: push DWORD PTR <span class="o">[</span>ecx-0x4<span class="o">]</span>
0x804b2d6: push ebp
0x804b2d7: mov ebp,esp
0x804b2d9: push edi
0x804b2da: push esi
0x804b2db: push ebx
0x804b2dc: push ecx
0x804b2dd: sub esp,0x1464
0x804b2e3: push 0x0
0x804b2e5: call 0x8056ce0
0x804b2ea: mov ebx,eax
0x804b2ec: call 0x8057430
0x804b2f1: xor eax,ebx
0x804b2f3: mov DWORD PTR <span class="o">[</span>esp<span class="o">]</span>,eax
0x804b2f6: call 0x804cb90
0x804b2fb: mov DWORD PTR <span class="o">[</span>esp<span class="o">]</span>,0x0
0x804b302: call 0x8056ce0
0x804b307: mov ebx,eax
0x804b309: call 0x8057430
0x804b30e: xor eax,ebx
0x804b310: mov DWORD PTR <span class="o">[</span>esp<span class="o">]</span>,eax
0x804b313: call 0x8048240
0x804b318: call 0x8048fd5
0x804b31d: movzx eax,BYTE PTR ds:0x80cc2b5
0x804b324: add esp,0xc
0x804b327: push eax
0x804b328: movzx eax,BYTE PTR ds:0x80cc2b4
0x804b32f: push eax
<span class="o">(</span>gdb<span class="o">)</span></code></pre></div>
<p>As you may see, the main function has a lot of calls to other functions. I&#39;ll not go into every call but cover most important aspects of its actions.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">0x8074fd0: mov edx,ebx
0x8074fd2: mov ebx,DWORD PTR <span class="o">[</span>esp+0x4<span class="o">]</span>
0x8074fd6: mov eax,0x7a
0x8074fdb: call DWORD PTR ds:0x80cbfd0</code></pre></div>
<p>In the forth line, i see a call to somewhere over a pointer. I need to deference it.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> p/x *0x80cbfd0
<span class="nv">$1</span> <span class="o">=</span> 0xf7ffd420
<span class="o">(</span>gdb<span class="o">)</span> x/10i 0xf7ffd420
0xf7ffd420 &lt;__kernel_vsyscall&gt;: push ecx
0xf7ffd421 &lt;__kernel_vsyscall+1&gt;: push edx
0xf7ffd422 &lt;__kernel_vsyscall+2&gt;: push ebp
0xf7ffd423 &lt;__kernel_vsyscall+3&gt;: mov ebp,esp
0xf7ffd425 &lt;__kernel_vsyscall+5&gt;: sysenter</code></pre></div>
<p>After deferencing, It becomes our gate to the kernel. I wrote the address of 0x80cbfd0 somewhere to keep in my mind. We need to analyze the registers before it goes through syscall gate.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> info reg
eax 0x7a 122
ecx 0xffffffff -1
edx 0x0 0
ebx 0xffffd5f6 -10762
esp 0xffffd5e0 0xffffd5e0
ebp 0xffffd7c8 0xffffd7c8
esi 0x0 0
edi 0x804be00 134528512
eip 0x8074fdb 0x8074fdb
eflags 0x286 <span class="o">[</span> PF SF IF <span class="o">]</span>
cs 0x23 35
ss 0x2b 43
ds 0x2b 43
es 0x2b 43
fs 0x0 0
gs 0x0 0</code></pre></div>
<p>We need to look up syscall table for 0x7a (eax value). You can find syscall table <a href="http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html">here</a></p>
<p>122 belongs to uname syscall.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> x/20c <span class="nv">$ebx</span>
0xffffd5f6: <span class="m">76</span> <span class="s1">&#39;L&#39;</span> <span class="m">105</span> <span class="s1">&#39;i&#39;</span> <span class="m">110</span> <span class="s1">&#39;n&#39;</span> <span class="m">117</span> <span class="s1">&#39;u&#39;</span> <span class="m">120</span> <span class="s1">&#39;x&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span>
0xffffd5fe: <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span>
0xffffd606: <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span> <span class="m">0</span> <span class="s1">&#39;\000&#39;</span></code></pre></div>
<p>So the malware got the information about my system.At least it knows which operating system im running :)</p>
<p>It uses several times of brk syscall to allocate some space from the heap.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">0x8056ce6: push ebx
0x8056ce7: xor ebx,ebx
<span class="o">=</span>&gt; 0x8056ce9: mov eax,0xd
0x8056cee: call DWORD PTR ds:0x80cbfd0</code></pre></div>
<p>Another syscall for getting time and later it will get also pid number.I&#39;ll skip these until socket things come up.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">0x805864f: nop
0x8058650: mov edx,ebx
0x8058652: mov eax,0x66
0x8058657: mov ebx,0x1
<span class="o">=</span>&gt; 0x805865c: lea ecx,<span class="o">[</span>esp+0x4<span class="o">]</span>
0x8058660: call DWORD PTR ds:0x80cbfd0</code></pre></div>
<p>This is very important, the malware creates a socket with socket syscall.This time, eax is 102 and it is sys_socketcall in linux syscall table (i386).</p>
<p>When we look at the parameters, </p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> info reg
eax 0x66 102
ecx 0xffffb2b0 -19792
edx 0x54302d33 1412443443
ebx 0x1 1
esp 0xffffb2ac 0xffffb2ac
ebp 0xffffc338 0xffffc338
esi 0x2e5fd200 778031616
edi 0x804be00 134528512
eip 0x8058660 0x8058660
eflags 0x282 <span class="o">[</span> SF IF <span class="o">]</span>
cs 0x23 35
ss 0x2b 43
ds 0x2b 43
es 0x2b 43
fs 0x0 0
gs 0x63 99</code></pre></div>
<p>Ebx is really important, 0x1 tells us <a href="http://lxr.free-electrons.com/source/include/uapi/linux/net.h#L26">that</a> it is a sys<em>socket and it will go down just <a href="http://lxr.free-electrons.com/source/net/socket.c#L2509">right</a> there.
Ecx is our pointer which is being passed into sys</em>socket function.</p>
<p>And then, after syscall execution, the kernel should give us a file descriptor that we can write to or read from it.</p>
<p>I go to my proc file system to see which file descriptor are mapped to my process adress space.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">eren@lisa:/proc/30480/fd<span class="nv">$ </span>ls -la
total 0
dr-x------ <span class="m">2</span> eren eren <span class="m">0</span> Oct <span class="m">4</span> 18:03 .
dr-xr-xr-x <span class="m">8</span> eren eren <span class="m">0</span> Oct <span class="m">4</span> 17:24 ..
lrwx------ <span class="m">1</span> eren eren <span class="m">64</span> Oct <span class="m">4</span> 18:04 <span class="m">0</span> -&gt; /dev/pts/0
lrwx------ <span class="m">1</span> eren eren <span class="m">64</span> Oct <span class="m">4</span> 18:04 <span class="m">1</span> -&gt; /dev/pts/0
lrwx------ <span class="m">1</span> eren eren <span class="m">64</span> Oct <span class="m">4</span> 18:03 <span class="m">2</span> -&gt; /dev/pts/0
lrwx------ <span class="m">1</span> eren eren <span class="m">64</span> Oct <span class="m">4</span> 18:04 <span class="m">3</span> -&gt; socket:<span class="o">[</span>324793<span class="o">]</span>
lrwx------ <span class="m">1</span> eren eren <span class="m">64</span> Oct <span class="m">4</span> 18:04 <span class="m">4</span> -&gt; socket:<span class="o">[</span>324794<span class="o">]</span>
lr-x------ <span class="m">1</span> eren eren <span class="m">64</span> Oct <span class="m">4</span> 18:04 <span class="m">5</span> -&gt; pipe:<span class="o">[</span>324795<span class="o">]</span>
l-wx------ <span class="m">1</span> eren eren <span class="m">64</span> Oct <span class="m">4</span> 18:04 <span class="m">6</span> -&gt; pipe:<span class="o">[</span>324795<span class="o">]</span>
lrwx------ <span class="m">1</span> eren eren <span class="m">64</span> Oct <span class="m">4</span> 18:04 <span class="m">7</span> -&gt; socket:<span class="o">[</span>327132<span class="o">]</span></code></pre></div>
<p>Yeah there it is.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">0x805844c: mov eax,0x66
0x8058451: mov ebx,0x3
0x8058456: lea ecx,<span class="o">[</span>esp+0x4<span class="o">]</span>
<span class="o">=</span>&gt; 0x805845a: call DWORD PTR ds:0x80cbfd0</code></pre></div>
<p>This time, Ebx is 0x3 that means SYS<em>CONNECT. Ecx now points to paramethers that we pushed into sys</em>connect. Let&#39;s examine the parameter.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="m">2515</span> <span class="k">case</span> SYS_CONNECT:
<span class="m">2516</span> <span class="nv">err</span> <span class="o">=</span> sys_connect<span class="o">(</span>a0, <span class="o">(</span>struct sockaddr __user *<span class="o">)</span>a1, a<span class="o">[</span>2<span class="o">])</span><span class="p">;</span>
<span class="m">2517</span> <span class="nb">break</span><span class="p">;</span></code></pre></div>
<p>The code above is from kernel source.Paramethers are a0,a pointer and another a2. These are all 12 bytes. So ecx should point to 12 bytes.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> x/3wx <span class="nv">$ecx</span>
0xffffb2b0: 0x00000007 0xffffc30c 0x00000010
<span class="o">(</span>gdb<span class="o">)</span></code></pre></div>
<p>Now, a0 =&gt; 0x07, sockaddr pointer is =&gt; 0xffffc30c and last paramether is 0x00000010.</p>
<p>We want to find where this malware connects. We know that sockaddr structure holds this information. Sockaddr structure is <a href="http://www.softwarerevolution.com/jeneral/ipc/include/linux/socket.h/source/SOURCE-socket.h.html">here</a></p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">typedef unsigned short sa_family_t<span class="p">;</span>
struct sockaddr <span class="o">{</span>
sa_family_t sa_family<span class="p">;</span> /* address family, AF_xxx */
char sa_data<span class="o">[</span>14<span class="o">]</span><span class="p">;</span> /* <span class="m">14</span> bytes of protocol address */
<span class="o">}</span><span class="p">;</span></code></pre></div>
<p>We can do our estimation that 1 short + 14 char =&gt; 16 bytes.
So i&#39;ll skip the first 2 bytes and print the others.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> x/14bu 0xffffc30e
0xffffc30e: 0 80 108 162 197 26 0 0
0xffffc316: 0 0 0 0 0 0</code></pre></div>
<p>So, it&#39;s clear that the malware wants to connect to 108.162.197.26 through 80 port.</p>
<p>Malware then tries to get the ip of host machine by executing getsockname.</p>
<p>It then reads &quot;/proc/net/route&quot; here to parse network interface and uses ioctl syscall to get its MAC address (SIOCGIFHWADDR).</p>
<p>Later, It forks itself and quit.</p>
<p>The forked process also tries to connect somewhere(89.238.150.154) but it cannot. I don&#39;t know why.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">root@lisa:~/blog# tcpdump -v -X dst 89.238.150.154
tcpdump: listening on eth0, link-type EN10MB <span class="o">(</span>Ethernet<span class="o">)</span>, capture size <span class="m">65535</span> bytes
18:52:50.373950 IP <span class="o">(</span>tos 0x0, ttl 64, id 46732, offset 0, flags <span class="o">[</span>DF<span class="o">]</span>, proto TCP <span class="o">(</span>6<span class="o">)</span>, length 60<span class="o">)</span>
95.85.48.36.51746 &gt; 89.238.150.154.5: Flags <span class="o">[</span>S<span class="o">]</span>, cksum 0x8030 <span class="o">(</span>incorrect -&gt; 0x9be6<span class="o">)</span>, seq 3399773995, win 14600, options <span class="o">[</span>mss 1460,sackOK,TS val <span class="m">113698896</span> ecr 0,nop,wscale 8<span class="o">]</span>, length 0
0x0000: <span class="m">4500</span> 003c b68c <span class="m">4000</span> <span class="m">4006</span> 042e 5f55 <span class="m">3024</span> E..&lt;..@.@..._U0<span class="err">$</span>
0x0010: 59ee 969a ca22 <span class="m">0005</span> caa4 6f2b <span class="m">0000</span> <span class="m">0000</span> Y....<span class="err">&quot;</span>....o+....
0x0020: a002 <span class="m">3908</span> <span class="m">8030</span> <span class="m">0000</span> <span class="m">0204</span> 05b4 <span class="m">0402</span> 080a ..9..0..........
0x0030: 06c6 e850 <span class="m">0000</span> <span class="m">0000</span> <span class="m">0103</span> <span class="m">0308</span> ...P........</code></pre></div>
<p>Anyway, this malware looks harmless now :)</p>
</content>
</entry>
<entry>
<title>[Part2] I was asked to crack a program in a job interview !</title>
<link href="/I-was-just-asked-to-crack-a-program-Part-2"/>
<updated>2014-09-20T17:00:41+00:00</updated>
<id>/I-was-just-asked-to-crack-a-program-Part-2</id>
<content type="html"><p>Hello guys ,
(If you don&#39;t know what part2 means , please go back and read part 1.)
First of all , i want to thank you all the people who read the <a href="http://erenyagdiran.github.io/I-was-just-asked-to-crack-a-program-Part-1/">part1</a>.I got really good feedbacks after all.</p>
<p>But I&#39;m sorry , i would like to correct some misunderstandings.</p>
<p>*My English is not that bad </p>
<p>*I wasn&#39;t expecting too much traffic from HN , Reddit and others.</p>
<p>*I&#39;m not working for that company now , I moved to Barcelona.</p>
<p>*I passed the interviews almost 1 year ago.</p>
<p>*I cracked the programs on the cloud.($5 plan , yes you know the company.)</p>
<p>So , I don&#39;t really think that root@ is problem.I can redeploy another droplet in seconds.</p>
<p>I switched to eren@ user that&#39;s because gdb didn&#39;t accept root&#39;s init file.</p>
<p>*And finally , please read the end of the post.I&#39;m sure you will like it.</p>
<p>Now , this time it&#39;s not about doors , we are going to crack the nuke.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">eren@lisa:~<span class="nv">$ </span>./CrackTheNuke
*** NUKE CONTROL SYSTEM ***
PASSWORD: giveMeNuke
*** ACCESS DENIED ***
PASSWORD: iwantanexplosion
*** ACCESS DENIED ***
PASSWORD: knockknockitsme
*** ACCESS DENIED ***
*** SYSTEM LOCKED ***
*** SHUTTING DOWN ***
eren@lisa:~<span class="err">$</span></code></pre></div>
<p>I&#39;ll dump whole binary into intel asm syntax as reference.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">eren@lisa:~<span class="nv">$ </span>objdump -M intel -D CrackTheNuke &gt; staticDis
eren@lisa:~<span class="err">$</span></code></pre></div>
<p>We&#39;ll need this file later.If you look at the staticDis file , you can see whole dump in intel syntax.</p>
<p>This time , we&#39;ll try something different.I&#39;ll execute the process first and later I&#39;ll attach the debugger to the process.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">eren@lisa:~<span class="nv">$ </span>./CrackTheNuke
*** NUKE CONTROL SYSTEM ***
PASSWORD:</code></pre></div>
<p>Therefore in this case , I&#39;ll switch to another shell for debugging.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">eren@lisa:~<span class="nv">$ </span>ps aux <span class="p">|</span> grep Crack
eren <span class="m">4741</span> 0.0 0.0 <span class="m">1724</span> <span class="m">252</span> pts/0 S+ 14:54 0:00 ./CrackTheNuke
eren <span class="m">4845</span> 0.0 0.1 <span class="m">7832</span> <span class="m">832</span> pts/1 S+ 14:56 0:00 grep Crack
eren@lisa:~<span class="nv">$ </span>gdb --pid 4741
GNU gdb <span class="o">(</span>GDB<span class="o">)</span> 7.4.1-debian
Copyright <span class="o">(</span>C<span class="o">)</span> <span class="m">2012</span> Free Software Foundation, Inc.
License GPLv3+: GNU GPL version <span class="m">3</span> or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type <span class="s2">&quot;show copying&quot;</span>
and <span class="s2">&quot;show warranty&quot;</span> <span class="k">for</span> details.
This GDB was configured as <span class="s2">&quot;x86_64-linux-gnu&quot;</span>.
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;.
Catchpoint <span class="m">1</span> <span class="o">(</span>syscall <span class="s1">&#39;ptrace&#39;</span> <span class="o">[</span>26<span class="o">])</span>
Attaching to process 4741
Reading symbols from /home/eren/CrackTheNuke...<span class="o">(</span>no debugging symbols found<span class="o">)</span>...done.
Reading symbols from /lib32/libc.so.6...<span class="o">(</span>no debugging symbols found<span class="o">)</span>...done.
Loaded symbols <span class="k">for</span> /lib32/libc.so.6
Reading symbols from /lib/ld-linux.so.2...<span class="o">(</span>no debugging symbols found<span class="o">)</span>...done.
Loaded symbols <span class="k">for</span> /lib/ld-linux.so.2
0xf7726430 in __kernel_vsyscall <span class="o">()</span>
<span class="o">=</span>&gt; 0xf7726430 &lt;__kernel_vsyscall+16&gt;: 5d pop ebp
<span class="o">(</span>gdb<span class="o">)</span></code></pre></div>
<p>Now type random 16 characters as input to the previous shell , and comeback here.You are in the scanf function which comes from glibc.
(The crackme will call scanf 16 times.We save time here)</p>
<p>In gdb , You can type &quot;si&quot; which is an abbreviation for &quot;single step&quot;.Type &quot;si&quot; once until you are here 0x80495ed</p>
<p>Or you can type &quot;b * 0x80495ed&quot; and &quot;c&quot; to stop in that address.</p>
<p>Anyway , we are now here</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">0x80495ed &lt;main+195&gt;:</code></pre></div>
<p>Ok here , we have a comparison </p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">0x80495ed &lt;main+195&gt;: cmp DWORD PTR <span class="o">[</span>esp+0x1c<span class="o">]</span>,0x0</code></pre></div>
<p>In gdb , You can type </p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">p/x <span class="nv">$esp</span></code></pre></div>
<p>to examine what&#39;s inside of $esp </p>
<p>You can also do some calculations based on registers and addresses</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">p/x <span class="nv">$esp</span>+0x1c</code></pre></div>
<p>or show the contents of the address by dereferencing</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">p/x *0xff811bac</code></pre></div>
<p>You can type &quot;si&quot; here , the crackme has consumed the 16 chars that you have typed just before and now It is looking for &quot;\n&quot; char to understand if we press the enter key or not.</p>
<p>put another break point to 0x804962d &quot;b * 0x804962d&quot; if you don&#39;t want to wait.</p>
<p>Now this part is interesting.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">=</span>&gt; 0x804962d &lt;main+259&gt;: push eax
0x804962e &lt;main+260&gt;: push ebx
0x804962f &lt;main+261&gt;: rdtsc
0x8049631 &lt;main+263&gt;: and eax,0xfffff
0x8049636 &lt;main+268&gt;: <span class="nb">test </span>eax,eax
0x8049638 &lt;main+270&gt;: je 0x8049646 &lt;g99&gt;
0x804963a &lt;main+272&gt;: xor ebx,0xe
0x804963d &lt;main+275&gt;: add ebx,0xe
0x8049640 &lt;main+278&gt;: sub ebx,0xe
0x8049643 &lt;main+281&gt;: dec eax</code></pre></div>
<p>Have you heard about <a href="http://en.wikipedia.org/wiki/Time_Stamp_Counter">rdtsc</a> instruction ? </p>
<p>It keeps cpu cycles.After calling rdtsc instruction ,The stamp counter will be stored in edx and eax.
After rdtsc , we keep rightmost 20bits of the eax into eax.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">0x8049636 &lt;main+268&gt;: <span class="nb">test </span>eax,eax
0x8049638 &lt;main+270&gt;: je 0x8049646 &lt;g99&gt;</code></pre></div>
<p>Above asm lines were written in C , codes would be like below.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="k">if</span><span class="o">(</span><span class="nv">eax</span> <span class="o">==</span> 0<span class="o">)</span>
<span class="o">{</span>
goto 0x8049646
<span class="o">}</span></code></pre></div>
<p>Since eax is not 0 , we will continue to investigate more.
As you may see , below codes are kindda trash :).We add 0xe to ebx and later subtract it.Those are put there to get us away from the code :) It&#39;s typical psychological test :)</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">xor ebx,0xe
add ebx,0xe
sub ebx,0xe
dec eax
<span class="k">until</span> eax is <span class="m">0</span> , <span class="k">continue</span> this loop :<span class="o">)</span></code></pre></div>
<p>put a breakpoint &quot;b * 0x8049646&quot; there , and press &quot;c&quot;</p>
<p>Ok not so much info here , put another step into or continue until </p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">=</span>&gt; 0x80494db &lt;nkc1qpE2L6f6AyqaendA&gt;: push ebp
0x80494dc &lt;nkc1qpE2L6f6AyqaendA+1&gt;: mov ebp,esp
0x80494de &lt;nkc1qpE2L6f6AyqaendA+3&gt;: sub esp,0x14
0x80494e1 &lt;nkc1qpE2L6f6AyqaendA+6&gt;: mov DWORD PTR <span class="o">[</span>ebp-0x4<span class="o">]</span>,0x0
0x80494e8 &lt;nkc1qpE2L6f6AyqaendA+13&gt;: mov DWORD PTR <span class="o">[</span>esp<span class="o">]</span>,0x0
0x80494ef &lt;nkc1qpE2L6f6AyqaendA+20&gt;: call 0x804944b &lt;qEWL8Jl0zdpmTbwhziDv&gt;
0x80494f4 &lt;nkc1qpE2L6f6AyqaendA+25&gt;: mov eax,DWORD PTR <span class="o">[</span>ebp+0x8<span class="o">]</span>
0x80494f7 &lt;nkc1qpE2L6f6AyqaendA+28&gt;: mov DWORD PTR <span class="o">[</span>esp<span class="o">]</span>,eax
0x80494fa &lt;nkc1qpE2L6f6AyqaendA+31&gt;: call 0x8048604 &lt;fjDKIzPtGuE8ZdfSL8vq&gt;
0x80494ff &lt;nkc1qpE2L6f6AyqaendA+36&gt;: mov DWORD PTR <span class="o">[</span>esp<span class="o">]</span>,0x2
0x8049506 &lt;nkc1qpE2L6f6AyqaendA+43&gt;: call 0x804944b &lt;qEWL8Jl0zdpmTbwhziDv&gt;
0x804950b &lt;nkc1qpE2L6f6AyqaendA+48&gt;: mov eax,DWORD PTR <span class="o">[</span>ebp+0x8<span class="o">]</span>
0x804950e &lt;nkc1qpE2L6f6AyqaendA+51&gt;: mov DWORD PTR <span class="o">[</span>esp<span class="o">]</span>,eax
0x8049511 &lt;nkc1qpE2L6f6AyqaendA+54&gt;: call 0x8048ab1 &lt;W0ElBw5Smo9TPiWOeK8c&gt;
0x8049516 &lt;nkc1qpE2L6f6AyqaendA+59&gt;: mov DWORD PTR <span class="o">[</span>ebp-0x4<span class="o">]</span>,eax
0x8049519 &lt;nkc1qpE2L6f6AyqaendA+62&gt;: mov DWORD PTR <span class="o">[</span>esp<span class="o">]</span>,0x1
0x8049520 &lt;nkc1qpE2L6f6AyqaendA+69&gt;: call 0x804944b &lt;qEWL8Jl0zdpmTbwhziDv&gt;
0x8049525 &lt;nkc1qpE2L6f6AyqaendA+74&gt;: mov eax,DWORD PTR <span class="o">[</span>ebp-0x4<span class="o">]</span>
0x8049528 &lt;nkc1qpE2L6f6AyqaendA+77&gt;: leave
0x8049529 &lt;nkc1qpE2L6f6AyqaendA+78&gt;: ret</code></pre></div>
<p>nkc1qpE2L6f6AyqaendA this function is the main workflow for the whole process.</p>
<p>Let&#39;s first investigate each function , we have 5 function call in nkc1qpE2L6f6AyqaendA.Those 3 functions are qEWL8Jl0zdpmTbwhziDv , fjDKIzPtGuE8ZdfSL8vq and W0ElBw5Smo9TPiWOeK8c.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> x/10i qEWL8Jl0zdpmTbwhziDv
0x804944b &lt;qEWL8Jl0zdpmTbwhziDv&gt;: push ebp
0x804944c &lt;qEWL8Jl0zdpmTbwhziDv+1&gt;: mov ebp,esp
0x804944e &lt;qEWL8Jl0zdpmTbwhziDv+3&gt;: mov eax,DWORD PTR <span class="o">[</span>ebp+0x8<span class="o">]</span>
0x8049451 &lt;qEWL8Jl0zdpmTbwhziDv+6&gt;: cmp eax,0x0
0x8049454 &lt;qEWL8Jl0zdpmTbwhziDv+9&gt;: je 0x80494b9 &lt;hzdhp&gt;
0x8049456 &lt;qEWL8Jl0zdpmTbwhziDv+11&gt;: cmp eax,0x1
0x8049459 &lt;qEWL8Jl0zdpmTbwhziDv+14&gt;: je 0x8049499 &lt;qEWL8Jl0zdpmTbwhziDv+78&gt;
0x804945b &lt;qEWL8Jl0zdpmTbwhziDv+16&gt;: call 0x8047b71
0x8049460 &lt;qEWL8Jl0zdpmTbwhziDv+21&gt;: add DWORD PTR <span class="o">[</span>eax+0x48604bf<span class="o">]</span>,0x5eb9008
0x804946a &lt;qEWL8Jl0zdpmTbwhziDv+31&gt;: add DWORD PTR <span class="o">[</span>eax-0x4608ea13<span class="o">]</span>,0x8048ab1</code></pre></div>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> x/10i fjDKIzPtGuE8ZdfSL8vq
0x8048604 &lt;fjDKIzPtGuE8ZdfSL8vq&gt;: call 0xb027:0xaf72c78c
0x804860b &lt;fjDKIzPtGuE8ZdfSL8vq+7&gt;: cmp esi,DWORD PTR ds:0xe4dfbbf1
0x8048611 &lt;fjDKIzPtGuE8ZdfSL8vq+13&gt;: <span class="o">(</span>bad<span class="o">)</span>
0x8048612 &lt;fjDKIzPtGuE8ZdfSL8vq+14&gt;: and al,BYTE PTR <span class="o">[</span>ebp+edi*2-0x8<span class="o">]</span>
0x8048616 &lt;fjDKIzPtGuE8ZdfSL8vq+18&gt;: push ebx
0x8048617 &lt;fjDKIzPtGuE8ZdfSL8vq+19&gt;: push esi
0x8048618 &lt;fjDKIzPtGuE8ZdfSL8vq+20&gt;: inc edx
0x8048619 &lt;fjDKIzPtGuE8ZdfSL8vq+21&gt;: mov WORD PTR <span class="o">[</span>ebp+0x76<span class="o">]</span>,ss
0x804861c &lt;fjDKIzPtGuE8ZdfSL8vq+24&gt;: xchg edx,eax
0x804861d &lt;fjDKIzPtGuE8ZdfSL8vq+25&gt;: mov al,ds:0x45fd3fbb
<span class="o">(</span>gd</code></pre></div>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">(</span>gdb<span class="o">)</span> x/10i W0ElBw5Smo9TPiWOeK8c
0x8048ab1 &lt;W0ElBw5Smo9TPiWOeK8c&gt;: call 0xb023:0x1c72c78c
0x8048ab8 &lt;W0ElBw5Smo9TPiWOeK8c+7&gt;: cmp esi,DWORD PTR ds:0xe4dfbbf1
0x8048abe &lt;W0ElBw5Smo9TPiWOeK8c+13&gt;: jmp 0xf86e358
0x8048ac3 &lt;W0ElBw5Smo9TPiWOeK8c+18&gt;: xchg ax,ax
0x8048ac5 &lt;W0ElBw5Smo9TPiWOeK8c+20&gt;: out dx,eax
0x8048ac6 &lt;W0ElBw5Smo9TPiWOeK8c+21&gt;: dec ebp
0x8048ac7 &lt;W0ElBw5Smo9TPiWOeK8c+22&gt;: xchg edi,eax
0x8048ac8 &lt;W0ElBw5Smo9TPiWOeK8c+23&gt;: popa
0x8048ac9 &lt;W0ElBw5Smo9TPiWOeK8c+24&gt;: <span class="nb">test </span>DWORD PTR <span class="o">[</span>ecx-0x7e<span class="o">]</span>,esp
0x8048acc &lt;W0ElBw5Smo9TPiWOeK8c+27&gt;: <span class="nb">test </span>DWORD PTR <span class="o">[</span>edi<span class="o">]</span>,esi</code></pre></div>
<p>So the workflow is in function nkc1qpE2L6f6AyqaendA ,</p>
<p>we call qEWL8Jl0zdpmTbwhziDv -&gt; fjDKIzPtGuE8ZdfSL8vq -&gt; qEWL8Jl0zdpmTbwhziDv -&gt; W0ElBw5Smo9TPiWOeK8c -&gt; qEWL8Jl0zdpmTbwhziDv.</p>
<p>As we examined the first 10 lines of each function.Have you felt something unusual ? </p>
<p>Especially , If you look at the first lines of each function , you may figure out that first lines of fjDKIzPtGuE8ZdfSL8vq and W0ElBw5Smo9TPiWOeK8c are quite stupid :)</p>
<p>I havent seen such a code &quot;call 0xb023:0x1c72c78c&quot; in my entire life :).It&#39;s because those 2 functions are totally encrypted and gdb tried to disassemble those encrypted bytes :)</p>
<p>So qEWL8Jl0zdpmTbwhziDv actually decrypts the functions.Therefore , it is called before them.</p>
<p>I am going to change whole workflow ,i will swap encrypted functions with plain ones and remove qEWL8Jl0zdpmTbwhziDv function from the workflow.</p>
<p>Therefore , new workflow will be fjDKIzPtGuE8ZdfSL8vq -&gt; W0ElBw5Smo9TPiWOeK8c and that&#39;s it.</p>
<h2> DeadEnd 1 Starts</h2>
<p>But before doing all these stuff , i would like tell you my another dead-end.It&#39;s quite important for me.
While i was working on this crackme , I looked for a way to disable TimeStampCounter or somehow controll it.Because crackme calls &quot;rdtsc&quot; instruction 2 times , substract from each other and calculate the time between instructions that have been executed.
If you run the program under gdb , the time between instructions take much more time than it is executed by just itself.Therefore , I first searched a way to controll tsc counter.Tsc counter gets its value through CPU.It&#39;s out of my ability to change its state with OS.But i wrote a kernel module to update it to 0.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">#include &lt;linux/module.h&gt; // included for all kernel modules</span>
<span class="c">#include &lt;linux/kernel.h&gt; // included for KERN_INFO</span>
<span class="c">#include &lt;linux/init.h&gt; // included for __init and __exit macros</span>
<span class="c">#include &lt;linux/kthread.h&gt; // for threads</span>
<span class="c">#include &lt;linux/sched.h&gt; // for task_struct</span>
<span class="c">#include &lt;linux/time.h&gt; // for using jiffies </span>
<span class="c">#include &lt;linux/timer.h&gt;</span>
MODULE_LICENSE<span class="o">(</span><span class="s2">&quot;GPL&quot;</span><span class="o">)</span><span class="p">;</span>
MODULE_AUTHOR<span class="o">(</span><span class="s2">&quot;m00dy&quot;</span><span class="o">)</span><span class="p">;</span>
MODULE_DESCRIPTION<span class="o">(</span><span class="s2">&quot;A Fake rdtsc emulation&quot;</span><span class="o">)</span><span class="p">;</span>
static struct task_struct *thread1<span class="p">;</span>
int thread_fn<span class="o">(){</span>
uint32_t hi,lo<span class="p">;</span>
unsigned long j0,j1<span class="p">;</span>
int <span class="nv">delay</span> <span class="o">=</span> HZ / 250<span class="p">;</span>
<span class="nv">hi</span><span class="o">=</span>0<span class="p">;</span> <span class="nv">lo</span><span class="o">=</span>0xb<span class="p">;</span>
printk<span class="o">(</span>KERN_INFO <span class="s2">&quot;In thread1&quot;</span><span class="o">)</span><span class="p">;</span>
<span class="nv">j0</span> <span class="o">=</span> jiffies<span class="p">;</span>
<span class="nv">j1</span> <span class="o">=</span> j0 + delay<span class="p">;</span>
asm volatile<span class="o">(</span><span class="s2">&quot;wrmsr&quot;</span>::<span class="s2">&quot;c&quot;</span><span class="o">(</span>0x10<span class="o">)</span>,<span class="s2">&quot;a&quot;</span><span class="o">(</span>lo<span class="o">)</span>,<span class="s2">&quot;d&quot;</span><span class="o">(</span>hi<span class="o">))</span><span class="p">;</span>
<span class="k">while</span><span class="o">(</span>1<span class="o">){</span>
<span class="k">if</span><span class="o">(</span>time_before<span class="o">(</span>jiffies,j1<span class="o">))</span>
schedule<span class="o">()</span><span class="p">;</span>
<span class="k">else</span>
<span class="o">{</span>
<span class="nv">j1</span> <span class="o">=</span> jiffies + delay<span class="p">;</span>
asm volatile<span class="o">(</span><span class="s2">&quot;wrmsr&quot;</span>::<span class="s2">&quot;c&quot;</span><span class="o">(</span>0x10<span class="o">)</span>,<span class="s2">&quot;a&quot;</span><span class="o">(</span>lo<span class="o">)</span>,<span class="s2">&quot;d&quot;</span><span class="o">(</span>hi<span class="o">))</span><span class="p">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
static int __init hello_init<span class="o">(</span>void<span class="o">)</span>
<span class="o">{</span>
char our_thread<span class="o">[</span>8<span class="o">]=</span><span class="s2">&quot;thread1&quot;</span><span class="p">;</span>
printk<span class="o">(</span>KERN_INFO <span class="s2">&quot;in init&quot;</span><span class="o">)</span><span class="p">;</span>
<span class="nv">thread1</span> <span class="o">=</span> kthread_create<span class="o">(</span>thread_fn,NULL,our_thread<span class="o">)</span><span class="p">;</span>
<span class="k">if</span><span class="o">((</span>thread1<span class="o">))</span>
<span class="o">{</span>
printk<span class="o">(</span>KERN_INFO <span class="s2">&quot;in if&quot;</span><span class="o">)</span><span class="p">;</span>
wake_up_process<span class="o">(</span>thread1<span class="o">)</span><span class="p">;</span>
<span class="o">}</span>
<span class="k">return</span> 0<span class="p">;</span>
<span class="o">}</span>
static void __exit hello_cleanup<span class="o">(</span>void<span class="o">)</span>
<span class="o">{</span>
printk<span class="o">(</span>KERN_INFO <span class="s2">&quot;Fake RDTSC end \n&quot;</span><span class="o">)</span><span class="p">;</span>
<span class="o">}</span>
module_init<span class="o">(</span>hello_init<span class="o">)</span><span class="p">;</span>
module_exit<span class="o">(</span>hello_cleanup<span class="o">)</span><span class="p">;</span></code></pre></div>
<p>I lost the makefile but if you want to compile it , you can use helloworld makefile template for kernel module.You can easily find it on google.</p>
<p>Anyway , that code didnt work as i expected.So i tried something else.</p>
<h2>DeadEnd 1 Ends</h2>
<p>My strategy will be to stop program where those 2 encrypted functions are in plain state.</p>
<p>For example , 0x8048ab0 is in quite good position.Because it&#39;s end of the fjDKIzPtGuE8ZdfSL8vq.</p>
<p>Now open .gdbinit file again.And put these things.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nb">set </span>disassembly-flavor intel
<span class="nb">set </span>disassemble-next-line on
handle SIGTRAP noprint pass nostop
b * 0x8048ab0</code></pre></div>
<p>Re open the crackme , attach the gdb again.Put 16chars and type &quot;c&quot;.
The program should stop </p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="o">=</span>&gt; 0xf7706430 &lt;__kernel_vsyscall+16&gt;: 5d pop ebp
<span class="o">(</span>gdb<span class="o">)</span> c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x08048ab1 in W0ElBw5Smo9TPiWOeK8c <span class="o">()</span>
<span class="o">=</span>&gt; 0x08048ab1 &lt;W0ElBw5Smo9TPiWOeK8c+0&gt;: 9a 8c c7 <span class="m">72</span> 1c <span class="m">23</span> b0 call 0xb023:0x1c72c78c
<span class="o">(</span>gdb<span class="o">)</span> x/10i fjDKIzPtGuE8ZdfSL8vq
0x8048604 &lt;fjDKIzPtGuE8ZdfSL8vq&gt;: push ebp
0x8048605 &lt;fjDKIzPtGuE8ZdfSL8vq+1&gt;: mov ebp,esp
0x8048607 &lt;fjDKIzPtGuE8ZdfSL8vq+3&gt;: call 0x8047b08
0x804860c &lt;fjDKIzPtGuE8ZdfSL8vq+8&gt;: xor eax,0x20ec8390
0x8048611 &lt;fjDKIzPtGuE8ZdfSL8vq+13&gt;: call 0x8047b08
0x8048616 &lt;fjDKIzPtGuE8ZdfSL8vq+18&gt;: xor eax,0x32ff45c6
0x804861b &lt;fjDKIzPtGuE8ZdfSL8vq+23&gt;: call 0x8047b08
0x8048620 &lt;fjDKIzPtGuE8ZdfSL8vq+28&gt;: xor eax,0xdafe45c6
0x8048625 &lt;fjDKIzPtGuE8ZdfSL8vq+33&gt;: call 0x8047b08
0x804862a &lt;fjDKIzPtGuE8ZdfSL8vq+38&gt;: xor eax,0xdbfd45c6
<span class="o">(</span>gdb<span class="o">)</span></code></pre></div>
<p>Ok we have at least plain form of the function fjDKIzPtGuE8ZdfSL8vq :) There&#39;s still a trick there.Gdb still have problems.This technique called false assembly.Please read <a href="http://www.stonedcoder.org/%7Ekd/lib/14-61-1-PB.pdf">this</a> for more info.</p>
<p>Now we need to dump the plain function to a file.Paramethers are filename startAddr and endAddr.</p>
<p>in this case , i typed </p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">dump ihex memory fjDKIzPtGuE8ZdfSL8vq_dump 0x8048604 0x8048ab0</code></pre></div>
<p>We dumped the entire function into fjDKIzPtGuE8ZdfSL8vq_dump file as plain.</p>
<p>Now , we need to do this for another encrypted function.</p>
<p>put a breakpoint 0x08048e14 here , wait until program crashes and then type </p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">dump ihex memory W0ElBw5Smo9TPiWOeK8c_dump W0ElBw5Smo9TPiWOeK8c g999+3</code></pre></div>
<h2>Let&#39;s get back to the surgery.</h2>
<p>We now have plain forms of both encrypted functions.We can now change the workflow.</p>
<p>Ok now , clean the .gdbinit file and put a breakpoint in 0x80494db</p>
<p>(If you struggle here , please look at nkc1qpE2L6f6AyqaendA function.)</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="nb">set </span>disassembly-flavor intel
<span class="nb">set </span>disassemble-next-line on
<span class="nb">break</span> * 0x80494ef
commands
<span class="nb">set</span><span class="o">(</span><span class="nv">$eip</span><span class="o">)</span> <span class="o">=</span> 0x80494f4
<span class="k">continue</span>
end
<span class="nb">break</span> * 0x80494fa
commands
restore fjDKIzPtGuE8ZdfSL8vq_dump
restore W0ElBw5Smo9TPiWOeK8c_dump
<span class="k">continue</span>
end
<span class="nb">break</span> * 0x08049506
commands
<span class="nb">set</span><span class="o">(</span><span class="nv">$eip</span><span class="o">)</span> <span class="o">=</span> 0x804950b
<span class="k">continue</span>
end
<span class="nb">break</span> * 0x8049520
commands
<span class="nb">set</span><span class="o">(</span><span class="nv">$eip</span><span class="o">)</span> <span class="o">=</span> 0x8049525
<span class="k">continue</span>
end</code></pre></div>
<p>We changed the workflow , replaced the encrypted functions with plain ones.Now this part is easy.The main algorithm is same as part 1.</p>
<p>Inputs are xored with other constants and we compare the outputs with other constants</p>
<p>Inputs ^ FirstConstants == SecondConstants</p>
<p>Therefore , Inputs = SecondConstants ^ FirstConstants</p>
<p>Here is the our key generator.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">#!/usr/bin/python</span>
<span class="nv">firstConst</span> <span class="o">=</span> <span class="o">[</span>0x32,0xda,0xdb,0x1,0xf3,0x77,0x4c,0x57,0xbe,0x49,0xec,0x5f,0xab,0x7f,0xed,0x9f<span class="o">]</span>
<span class="nv">secondConst</span> <span class="o">=</span> <span class="o">[</span>0x0d,0xef,0xf1,0x4d,0xb6,0x4c,0x69,0x20,0xf9,0x20,0xdd,0x7c,0xda,0x3b,0xc9,0xaf<span class="o">]</span>
<span class="nv">ret</span> <span class="o">=</span><span class="s2">&quot;&quot;</span>
<span class="k">for</span> x in range<span class="o">(</span>16<span class="o">)</span>:
ret+<span class="o">=</span>chr<span class="o">(</span>firstConst<span class="o">[</span>x<span class="o">]</span> ^ secondConst<span class="o">[</span>x<span class="o">])</span>
print ret</code></pre></div>
<h2>Here we go.</h2>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">eren@lisa:~<span class="nv">$ </span>./CrackTheNuke
*** NUKE CONTROL SYSTEM ***
PASSWORD: ?5*LE<span class="p">;</span>%wGi1#qD<span class="nv">$0</span>
*** ACCESS GRANTED ***
*** THE NUKE STOPPED ***
eren@lisa:~<span class="err">$</span></code></pre></div>
<h2>I&#39;ll tell you what happened after i signed the contract</h2>
<p>In my first day of the new job , They changed my department.(I still dont know why and the company still markets itself like best of bests in Turkey.)</p>
<p>I became J2ee developer.I was using eclipse , committing svn , using a operating system called &quot;Windows *&quot;.They even asked me to write some css.</p>
<p>Now , i live in Barcelona and have a great life.</p>
<p>bb </p>
<p>(If you want to have the crackme , shoot me an email.)</p>
<p>(I can also give you the plain dumps of the encrypted functions in ihex format.)</p>
<p>(Typos &amp; comments are always welcome.)</p>
</content>
</entry>
<entry>
<title>I was just asked to crack a program in a job interview !</title>
<link href="/I-was-just-asked-to-crack-a-program-Part-1"/>
<updated>2014-09-17T17:00:41+00:00</updated>
<id>/I-was-just-asked-to-crack-a-program-Part-1</id>
<content type="html"><p>TL;DR</p>
<p>I was just asked to crack a program in a job interview.
and got the job.</p>
<p>Hello everyone,</p>
<p>I am quite excited about my new blog here. I am planning to write couple of blog posts every week.</p>
<p>Since the title gives you a brief information about a general concept, i would like to tell you my story about a job interview that was held in Ankara, TR.</p>
<p>I applied a &quot;Software Security Engineer&quot; position and in the interview, they asked me really low level stuff some of which I knew, some of which I did not.</p>
<p>Then they sent me an email which includes an attachment for a protected and encrypted binary (&quot;CRACK MEEE!&quot;).</p>
<p>When i got home, I downloaded it and it only asked me a password to unlock it. They wanted me to find that password :)</p>
<p>At first, it looks pretty hard but I will try to introduce the general concept that I had followed :)</p>
<p>Here is the first thing I typed in the terminal</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">root@lisa:~# ./CrackTheDoor
*** DOOR CONTROL SYSTEM ***
PASSWORD:</code></pre></div>
<p>I typed something stupid keyword 3 times and it quited. :)</p>
<p>I have more tools to analyze. Lets get more info about the file.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">root@lisa:~# file CrackTheDoor
CrackTheDoor: ELF 32-bit LSB executable, Intel 80386, version <span class="m">1</span> <span class="o">(</span>SYSV<span class="o">)</span>, dynamically linked <span class="o">(</span>uses shared libs<span class="o">)</span>, <span class="k">for</span> GNU/Linux 2.6.15, BuildID<span class="o">[</span>sha1<span class="o">]=</span>0x9927be2fe310bea01d412164103b9c8b2d7567ea, not stripped
root@lisa:~#</code></pre></div>
<p>Ok. Now we have a little bit more info about the binary :)</p>
<p>Let&#39;s do this.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">root@lisa:~# ldd CrackTheDoor
linux-gate.so.1 <span class="o">=</span>&gt; <span class="o">(</span>0xf777b000<span class="o">)</span>
libc.so.6 <span class="o">=</span>&gt; /lib32/libc.so.6 <span class="o">(</span>0xf760c000<span class="o">)</span>
/lib/ld-linux.so.2 <span class="o">(</span>0xf777c000<span class="o">)</span>
root@lisa:~#</code></pre></div>
<p>Oh! just standard stuff. I will explain a bit.</p>
<p>linux-gate.so is something like you cant find in your filesystem. But ldd shows that it&#39;s a shared library right ? Yes, Have you heard about Virtual DSO (Virtual Dynamic Shared Object)</p>
<p>I suggest you to read about <a href="http://www.trilithium.com/johan/2005/08/linux-gate/">linux-gate.so</a></p>
<p>libc.so.6 is general C library for GNU system as you probably know.</p>
<p>ld-linux.so is linux&#39;s dynamic loader.</p>
<p>Anyway till here everything is fine. We need to run the program under the debugger and see what happens.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">root@lisa:~# gdb CrackTheDoor
GNU gdb <span class="o">(</span>GDB<span class="o">)</span> 7.4.1-debian
Copyright <span class="o">(</span>C<span class="o">)</span> <span class="m">2012</span> Free Software Foundation, Inc.
License GPLv3+: GNU GPL version <span class="m">3</span> or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type <span class="s2">&quot;show copying&quot;</span>
and <span class="s2">&quot;show warranty&quot;</span> <span class="k">for</span> details.
This GDB was configured as <span class="s2">&quot;x86_64-linux-gnu&quot;</span>.
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;...
Reading symbols from /root/CrackTheDoor...<span class="o">(</span>no debugging symbols found<span class="o">)</span>...done.
<span class="o">(</span>gdb<span class="o">)</span> r
Starting program: /root/CrackTheDoor
Program received signal SIGSEGV, Segmentation fault.
0x080484fb in __do_global_dtors_aux <span class="o">()</span>
<span class="o">(</span>gdb<span class="o">)</span></code></pre></div>
<p>So, the program crashed itself. It figured out that we run it in a debugger. Therefore, there should be some anti-debugging tricks embedded inside the program. Ok...</p>
<p>Lets reloadd the program and get the starting point of the program.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">root@lisa:~# gdb CrackTheDoor
GNU gdb <span class="o">(</span>GDB<span class="o">)</span> 7.4.1-debian
Copyright <span class="o">(</span>C<span class="o">)</span> <span class="m">2012</span> Free Software Foundation, Inc.
License GPLv3+: GNU GPL version <span class="m">3</span> or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type <span class="s2">&quot;show copying&quot;</span>
and <span class="s2">&quot;show warranty&quot;</span> <span class="k">for</span> details.
This GDB was configured as <span class="s2">&quot;x86_64-linux-gnu&quot;</span>.
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;...
Reading symbols from /root/CrackTheDoor...<span class="o">(</span>no debugging symbols found<span class="o">)</span>...done.
<span class="o">(</span>gdb<span class="o">)</span> info file
Symbols from <span class="s2">&quot;/root/CrackTheDoor&quot;</span>.
Local <span class="nb">exec </span>file:
<span class="sb">`</span>/root/CrackTheDoor<span class="err">&#39;</span>, file <span class="nb">type </span>elf32-i386.
Entry point: 0x804762c
...
...</code></pre></div>
<p>Now we got the Entry point for the program. Let&#39;s put a breakpoint there and start to debug the program through its entry point</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">b * 0x804762c</code></pre></div>
<p>Then press type &quot;r&quot; to run the program. You should be stopped at the first line of entry point</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">gdb<span class="o">)</span> x/30i <span class="nv">$pc</span>
<span class="o">=</span>&gt; 0x804762c: pusha
0x804762d: mov <span class="nv">$0xaa</span>,%dl
0x804762f: mov <span class="nv">$0x8048480</span>,%edi
0x8047634: mov <span class="nv">$0x8048cbc</span>,%ecx
0x8047639: mov %edi,0x80476f3
0x804763f: mov %ecx,0x80476f7
0x8047645: sub %edi,%ecx
0x8047647: mov <span class="nv">$0x804762f</span>,%esi
0x804764c: push <span class="nv">$0x80476c1</span>
0x8047651: pusha
0x8047652: mov <span class="nv">$0x55</span>,%al
0x8047654: xor <span class="nv">$0x99</span>,%al
0x8047656: mov <span class="nv">$0x8047656</span>,%edi
0x804765b: mov <span class="nv">$0x80476e5</span>,%ecx
0x8047660: sub <span class="nv">$0x8047656</span>,%ecx
0x8047666: repnz scas %es:<span class="o">(</span>%edi<span class="o">)</span>,%al
0x8047668: je 0x804770a
0x804766e: mov %edi,0x80476eb
0x8047674: popa
0x8047675: add 0x80476eb,%edx
0x804767b: ret</code></pre></div>
<p>It should be look like this.This syntax mode belongs to AT&amp;T and you can switch to Intel mode.In my opinion, Intel Syntax is a bit better</p>
<p>0x8047654 in this address, we first put 0x55 to al register then xor it via 0x99 which produces 0xCC</p>
<p>0xCC is very important because it means it stops your process or like peter said in comments it is break-to-debugger in x86 architecture. When your debugger wants to stop your program, it swaps the bytes to 0xCC in where it wants to stop.</p>
<p>0x8047666 , here we see repnz scas =&gt; this will search the memory region bounded by es to edi for the value inside al ( 0xCC ).</p>
<p>So, those lines will basically scan the memory, if there is a 0xCC, it will crash your program and such...</p>
<p>Ok, I dont want to spend too much time here. Let&#39;s try strace.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">root@lisa:~# strace ./CrackTheDoor
execve<span class="o">(</span><span class="s2">&quot;./CrackTheDoor&quot;</span>, <span class="o">[</span><span class="s2">&quot;./CrackTheDoor&quot;</span><span class="o">]</span>, <span class="o">[</span>/* <span class="m">17</span> vars */<span class="o">])</span> <span class="o">=</span> 0
<span class="o">[</span> Process <span class="nv">PID</span><span class="o">=</span><span class="m">31085</span> runs in <span class="m">32</span> bit mode. <span class="o">]</span>
brk<span class="o">(</span>0<span class="o">)</span> <span class="o">=</span> 0x9972000
access<span class="o">(</span><span class="s2">&quot;/etc/ld.so.nohwcap&quot;</span>, F_OK<span class="o">)</span> <span class="o">=</span> -1 ENOENT <span class="o">(</span>No such file or directory<span class="o">)</span>
mmap2<span class="o">(</span>NULL, 4096, PROT_READ<span class="p">|</span>PROT_WRITE, MAP_PRIVATE<span class="p">|</span>MAP_ANONYMOUS, -1, 0<span class="o">)</span> <span class="o">=</span> 0xfffffffff7715000
access<span class="o">(</span><span class="s2">&quot;/etc/ld.so.preload&quot;</span>, R_OK<span class="o">)</span> <span class="o">=</span> -1 ENOENT <span class="o">(</span>No such file or directory<span class="o">)</span>
open<span class="o">(</span><span class="s2">&quot;/etc/ld.so.cache&quot;</span>, O_RDONLY<span class="o">)</span> <span class="o">=</span> 3
fstat64<span class="o">(</span>3, <span class="o">{</span><span class="nv">st_mode</span><span class="o">=</span>S_IFREG<span class="p">|</span>0644, <span class="nv">st_size</span><span class="o">=</span>35597, ...<span class="o">})</span> <span class="o">=</span> 0
mmap2<span class="o">(</span>NULL, 35597, PROT_READ, MAP_PRIVATE, 3, 0<span class="o">)</span> <span class="o">=</span> 0xfffffffff770c000
close<span class="o">(</span>3<span class="o">)</span> <span class="o">=</span> 0
access<span class="o">(</span><span class="s2">&quot;/etc/ld.so.nohwcap&quot;</span>, F_OK<span class="o">)</span> <span class="o">=</span> -1 ENOENT <span class="o">(</span>No such file or directory<span class="o">)</span>
open<span class="o">(</span><span class="s2">&quot;/lib32/libc.so.6&quot;</span>, O_RDONLY<span class="o">)</span> <span class="o">=</span> 3
<span class="nb">read</span><span class="o">(</span>3, <span class="s2">&quot;\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\300o\1\0004\0\0\0&quot;</span>..., 512<span class="o">)</span> <span class="o">=</span> 512
fstat64<span class="o">(</span>3, <span class="o">{</span><span class="nv">st_mode</span><span class="o">=</span>S_IFREG<span class="p">|</span>0755, <span class="nv">st_size</span><span class="o">=</span>1441884, ...<span class="o">})</span> <span class="o">=</span> 0
mmap2<span class="o">(</span>NULL, 4096, PROT_READ<span class="p">|</span>PROT_WRITE, MAP_PRIVATE<span class="p">|</span>MAP_ANONYMOUS, -1, 0<span class="o">)</span> <span class="o">=</span> 0xfffffffff770b000
mmap2<span class="o">(</span>NULL, 1456504, PROT_READ<span class="p">|</span>PROT_EXEC, MAP_PRIVATE<span class="p">|</span>MAP_DENYWRITE, 3, 0<span class="o">)</span> <span class="o">=</span> 0xfffffffff75a7000
mprotect<span class="o">(</span>0xf7704000, 4096, PROT_NONE<span class="o">)</span> <span class="o">=</span> 0
mmap2<span class="o">(</span>0xf7705000, 12288, PROT_READ<span class="p">|</span>PROT_WRITE, MAP_PRIVATE<span class="p">|</span>MAP_FIXED<span class="p">|</span>MAP_DENYWRITE, 3, 0x15d<span class="o">)</span> <span class="o">=</span> 0xfffffffff7705000
mmap2<span class="o">(</span>0xf7708000, 10616, PROT_READ<span class="p">|</span>PROT_WRITE, MAP_PRIVATE<span class="p">|</span>MAP_FIXED<span class="p">|</span>MAP_ANONYMOUS, -1, 0<span class="o">)</span> <span class="o">=</span> 0xfffffffff7708000
close<span class="o">(</span>3<span class="o">)</span> <span class="o">=</span> 0
mmap2<span class="o">(</span>NULL, 4096, PROT_READ<span class="p">|</span>PROT_WRITE, MAP_PRIVATE<span class="p">|</span>MAP_ANONYMOUS, -1, 0<span class="o">)</span> <span class="o">=</span> 0xfffffffff75a6000
set_thread_area<span class="o">(</span>0xffe4d864<span class="o">)</span> <span class="o">=</span> 0
mprotect<span class="o">(</span>0xf7705000, 8192, PROT_READ<span class="o">)</span> <span class="o">=</span> 0
mprotect<span class="o">(</span>0x8049000, 4096, PROT_READ<span class="o">)</span> <span class="o">=</span> 0
mprotect<span class="o">(</span>0xf7733000, 4096, PROT_READ<span class="o">)</span> <span class="o">=</span> 0
munmap<span class="o">(</span>0xf770c000, 35597<span class="o">)</span> <span class="o">=</span> 0
ptrace<span class="o">(</span>PTRACE_TRACEME, 0, 0x1, 0<span class="o">)</span> <span class="o">=</span> -1 EPERM <span class="o">(</span>Operation not permitted<span class="o">)</span>
ptrace<span class="o">(</span>PTRACE_TRACEME, 0, 0x1, 0<span class="o">)</span> <span class="o">=</span> -1 EPERM <span class="o">(</span>Operation not permitted<span class="o">)</span></code></pre></div>
<p>If you look at the last lines, the program crashed itself again. That&#39;s because ptrace syscall.</p>
<p>In linux, ptrace is an abbreviation for &quot;Process Trace&quot;. With ptrace, you can control another process, changing its internal state like debuggers.</p>
<p>Debuggers use ptrace a lot :) it&#39;s their job.</p>
<p>If we imagine code, it should look like this.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">int main<span class="o">()</span>
<span class="o">{</span>
<span class="k">if</span> <span class="o">(</span>ptrace<span class="o">(</span>PTRACE_TRACEME, 0, 1, 0<span class="o">)</span> &lt; 0<span class="o">)</span> <span class="o">{</span>
<span class="nb">printf</span><span class="o">(</span><span class="s2">&quot;DEBUGGING... Bye\n&quot;</span><span class="o">)</span><span class="p">;</span>
<span class="k">return</span> 1<span class="p">;</span>
<span class="o">}</span>
<span class="nb">printf</span><span class="o">(</span><span class="s2">&quot;Hello\n&quot;</span><span class="o">)</span><span class="p">;</span>
<span class="k">return</span> 0<span class="p">;</span>
<span class="o">}</span></code></pre></div>
<p>By the way, you can only do once ptrace[PTRACE_TRACEMe], so debugger ptraced the program before, there our call will return false so we figured out there is something out there controlling our program</p>
<p>We need to bypass this ptrace protection so that program shall never understand even it is running under a debugger.</p>
<p>So, Our strategy will be changing result of the syscall.</p>
<p>Syscalls are gateways from userspace to kernelspace. We are sure that ptrace is also using some syscalls to do process controlling thing.</p>
<p>We will detect when the program uses ptrace and we will set its result to 0 :) here it is</p>
<p>In my home folder, I create a new .gdbinit file. Therefore, everytime i run gdb, those configurations will be loaded automatically.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">~/.gdbinit
<span class="nb">set </span>disassembly-flavor intel <span class="c"># Intel syntax is better</span>
<span class="nb">set </span>disassemble-next-line on
catch syscall ptrace <span class="c">#Catch the syscall.</span>
commands 1
<span class="nb">set</span> <span class="o">(</span><span class="nv">$eax</span><span class="o">)</span> <span class="o">=</span> 0
<span class="k">continue</span>
end</code></pre></div>
<p>Eax will hold the result of the syscall. And it&#39;s ia always 0 or let me say TRUE</p>
<p>this way, we bypass the ptrace protection and now we need to switch back to gdb</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">eren@lisa:~<span class="nv">$ </span>gdb ./CrackTheDoor
GNU gdb <span class="o">(</span>GDB<span class="o">)</span> 7.4.1-debian
Copyright <span class="o">(</span>C<span class="o">)</span> <span class="m">2012</span> Free Software Foundation, Inc.
License GPLv3+: GNU GPL version <span class="m">3</span> or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type <span class="s2">&quot;show copying&quot;</span>
and <span class="s2">&quot;show warranty&quot;</span> <span class="k">for</span> details.
This GDB was configured as <span class="s2">&quot;x86_64-linux-gnu&quot;</span>.
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;...
Catchpoint <span class="m">1</span> <span class="o">(</span>syscall <span class="s1">&#39;ptrace&#39;</span> <span class="o">[</span>26<span class="o">])</span>
Reading symbols from /home/eren/CrackTheDoor...<span class="o">(</span>no debugging symbols found<span class="o">)</span>...done.
<span class="o">(</span>gdb<span class="o">)</span> r
Starting program: /home/eren/CrackTheDoor
Catchpoint <span class="m">1</span> <span class="o">(</span>call to syscall ptrace<span class="o">)</span>, 0x08047698 in ?? <span class="o">()</span>
<span class="o">=</span>&gt; 0x08047698: 3d <span class="m">00</span> f0 ff ff cmp eax,0xfffff000
Catchpoint <span class="m">1</span> <span class="o">(</span>returned from syscall ptrace<span class="o">)</span>, 0x08047698 in ?? <span class="o">()</span>
<span class="o">=</span>&gt; 0x08047698: 3d <span class="m">00</span> f0 ff ff cmp eax,0xfffff000
*** DOOR CONTROL SYSTEM ***
PASSWORD:</code></pre></div>
<p>Ok, at least we can use our debugger as we want :)</p>
<p>I put another breakpoint here PJeGPC4TIVaKFmmy53DJ</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">Breakpoint 2, 0x08048534 in PJeGPC4TIVaKFmmy53DJ <span class="o">()</span>