-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsc-make.pl
599 lines (444 loc) · 12.3 KB
/
sc-make.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Cwd 'abs_path';
use File::Basename;
our @TMP_FILES;
umask ~0700;
$SIG{INT}=\&clean_tmp_files;
main();
sub main {
my ($sc, $bin);
my ($dir, $path);
my %OPT;
GetOptions(
'out=s' => \$OPT{o},
'bad=s' => \$OPT{b},
'disassemble' => \$OPT{d},
'trace' => \$OPT{t},
'arch=s' => \$OPT{a},
'version' => \$OPT{v},
'help' => \$OPT{h},
'info' => \$OPT{i});
pod2usage(1) if $OPT{h};
pod2usage(-verbose => 99, -sections => 'VERSION') if($OPT{v});
pod2usage(-exitval => 0, -verbose => 2) if $OPT{i};
pod2usage(-msg => 'No file selected !', -exitval => 1) if(@ARGV != 1);
$OPT{o} = 'raw' unless($OPT{o});
$OPT{a} = 'x86' unless($OPT{a});
$path = abs_path($ARGV[0]);
$dir = dirname($path);
die "Can't chdir($dir) : $!\n" unless(chdir($dir));
die "Bad arch ($OPT{a}) specified !\n" unless(grep { $OPT{a} eq $_ } ('x86', 'arm', 'x86-64'));
die "Assembly failed !\n" unless($bin = sc_assemble($path, $OPT{a}));
die "Shellcode extraction failed !\n" unless($sc = sc_extract($bin, $OPT{a}));
if($OPT{d}){
die "Disassembly failed !\n" unless(sc_disasm($bin, $OPT{o}, $OPT{a}));
}
die "Bad chars in shellcode !\n" unless(check_bad_ch($sc, $OPT{b}));
die "Can't pring shellcode !\n" unless(sc_print($sc, $OPT{o}));
if($OPT{t}) {
die "Can't test shellcode !\n" unless(sc_test($sc, $OPT{a}));
}
}
# Genere a random ascii string of $len chars.
# Charset : a-zA-Z0-9
# @RETURN random string.
sub gen_random_name {
my $len = shift;
my @chars = (('a'..'z'), ('A'..'Z'), ('0'..'9'));
my $name;
while($len > 0) {
$name .= $chars[int(rand(scalar @chars))];
$len--;
}
return $name;
}
# Test shellcode with strace.
# @RETURN undef if error.
sub sc_test {
my ($sc, $arch) = @_;
my $c_source = '/tmp/' . gen_random_name(30) . '.c';
my $bin = '/tmp/' . gen_random_name(30);
push @TMP_FILES, $c_source;
push @TMP_FILES, $bin;
unless(open F, '>', $c_source) {
warn "Failed to open $c_source : $!\n";
return undef;
}
print F "#include <sys/mman.h>\n";
print F "#include <string.h>\n";
print F "#include <stdio.h>\n";
print F "int main(void){char sc[]=\"";
while(length($sc) > 0) {
printf F "\\x%02x", ord $sc;
$sc = substr($sc, 1);
}
print F '";void (*f)(void);void *p=mmap(0,0x1000,PROT_EXEC|PROT_WRITE|PROT_READ,MAP_ANON|MAP_PRIVATE,-1,0);';
print F 'if(p==MAP_FAILED){perror("mmap:");return -1;}';
print F 'memcpy(p,sc,sizeof(sc));f=p;f();return 0;}';
close F;
if($arch eq 'x86') {
`gcc -m32 -Wall $c_source -g -o $bin`;
} elsif($arch eq 'x86-64') {
`gcc -m64 -Wall $c_source -g -o $bin`;
} elsif($arch eq 'arm') {
`arm-none-eabi-gcc $c_source -g -o $bin`;
} else {
die "[-] Arch not supported for testing : $arch\n";
}
if($?) {
warn "Failed to compile $c_source\n";
return undef;
}
unless(open F, "strace -f $bin|") {
warn "Failed to open strace pipe : $!\n";
return undef;
}
while(defined(my $l = <F>)) {
print "$l";
}
close F;
return 1;
}
# Disassemble shellcode with objdump
# @RETURN undef if error.
sub sc_disasm {
my ($bin, $out, $arch) = @_;
my $cmd;
$cmd = "objdump -d -Mintel $bin|" if($arch eq 'x86');
$cmd = "objdump -d -Mintel $bin|" if($arch eq 'x86-64');
$cmd = "arm-none-eabi-objdump -d $bin|" if($arch eq 'arm');
unless(open F, $cmd) {
warn "Failed to objdump $bin : $!\n";
return undef;
}
while(defined(my $l = <F>)) {
return undef unless(print_comment($l, $out));
}
close F;
}
# Assemble shellcode with nasm
# @RETURN undef if error.
sub sc_assemble {
my ($asm, $arch) = @_;
my $bin = '/tmp/' . gen_random_name(30);
push @TMP_FILES, $bin;
`nasm $asm -f elf -o $bin` if($arch eq 'x86');
`nasm $asm -f elf64 -o $bin` if($arch eq 'x86-64');
`arm-none-eabi-as $asm -o $bin` if($arch eq 'arm');
return undef if($?);
return $bin;
}
sub sc_extract_x86 {
my ($bin) = @_;
my $sc;
my $cmd;
$cmd = "objdump -d -Mintel $bin|";
unless(open(F, $cmd)) {
warn "Failed to open objdump pipe $bin : $!\n";
return undef;
}
while(defined(my $l = <F>)) {
$l =~ s/^\s*[0-9a-f]+:\s+//;
while($l =~ m/^([a-f0-9]{2})\s/) {
$sc .= chr(hex("0x$1"));
$l = substr($l, 3);
}
}
return $sc;
}
sub sc_extract_x86_64 {
my ($bin) = @_;
my $sc;
my $cmd;
$cmd = "objdump -d -Mintel $bin|";
unless(open(F, $cmd)) {
warn "Failed to open objdump pipe $bin : $!\n";
return undef;
}
while(defined(my $l = <F>)) {
$l =~ s/^\s*[0-9a-f]+:\s+//;
while($l =~ m/^([a-f0-9]{2})\s/) {
$sc .= chr(hex("0x$1"));
$l = substr($l, 3);
}
}
return $sc;
}
sub sc_extract_arm {
my ($bin) = @_;
my $sc;
my $cmd;
$cmd = "arm-none-eabi-objdump -d $bin|";
unless(open(F, $cmd)) {
warn "Failed to open objdump pipe $bin : $!\n";
return undef;
}
while(defined(my $l = <F>)) {
$l =~ s/.+:\s+//;
if($l =~ m/^([a-f0-9]{2,8})\s/) {
$sc .= pack('L', hex("0x$1")) if(length $1 == 8);
$sc .= pack('S', hex("0x$1")) if(length $1 == 4);
$sc .= pack('B', hex("0x$1")) if(length $1 == 2);
}
}
return $sc;
}
# Extract shellcode with objdump
# @RETURN undef if error.
sub sc_extract {
my ($bin, $arch) = @_;
my $sc;
$sc = sc_extract_x86($bin) if($arch eq 'x86');
$sc = sc_extract_x86_64($bin) if($arch eq 'x86-64');
$sc = sc_extract_arm($bin) if($arch eq 'arm');
return $sc;
}
# Print shellcode (perl output)
sub sc_print_perl {
my $sc = shift;
my $n = 0;
print '# SHELLCODE LENGTH: ' . length($sc) . "\n\n\n";
print 'my $shellcode = "';
while(length($sc) > 0) {
if($n >= 12) {
print "\" . \n \"";
$n = 0;
}
printf "\\x%02x", ord $sc;
$sc = substr($sc, 1);
$n++;
}
print "\";\n";
}
# Print shellcode (C output)
sub sc_print_c {
my $sc = shift;
my $n = 0;
print '// SHELLCODE LENGTH: ' . length($sc) . "\n\n\n";
print 'char shellcode[] = "';
while(length($sc) > 0) {
if($n >= 12) {
print "\" \n \"";
$n = 0;
}
printf "\\x%02x", ord $sc;
$sc = substr($sc, 1);
$n++;
}
print "\";\n";
}
# Print shellcode (asm output)
sub sc_print_asm {
my $sc = shift;
my $n = 0;
print ';; SHELLCODE LENGTH: ' . length($sc) . "\n\n\n";
print "shellcode: \n db ";
while(length($sc) > 0) {
if($n >= 12) {
print "\n db ";
$n = 0;
}
printf "0x%02x", ord $sc;
print "," if($n < 11 && length($sc) > 1);
$sc = substr($sc, 1);
$n++;
}
print "\n";
}
# Print shellcode (bash output)
sub sc_print_bash {
my $sc = shift;
my $n = 0;
print '# SHELLCODE LENGTH: ' . length($sc) . "\n\n\n";
print "export shellcode=\$\'";
while(length($sc) > 0) {
if($n >= 12) {
print "\'\\ \n \$\'";
$n = 0;
}
printf "\\x%02x", ord $sc;
$sc = substr($sc, 1);
$n++;
}
print "\'\n";
}
# Print shellcode (python output)
sub sc_print_python {
my $sc = shift;
my $n = 0;
print '# SHELLCODE LENGTH: ' . length($sc) . "\n\n\n";
print 'shellcode = "';
while(length($sc) > 0) {
if($n >= 12) {
print "\" + \\\n \"";
$n = 0;
}
printf "\\x%02x", ord $sc;
$sc = substr($sc, 1);
$n++;
}
print "\"\n";
}
# Print shellcode (raw output)
sub sc_print_raw {
my $sc = shift;
print 'SHELLCODE LENGTH: ' . length($sc) . "\n\n";
print "\"";
while(length($sc) > 0) {
printf "\\x%02x", ord $sc;
$sc = substr($sc, 1);
}
print "\"\n";
}
# Print shellcode
# @RETURN undef if error.
sub sc_print {
my ($sc, $out) = @_;
if($out eq 'perl') {
sc_print_perl($sc);
} elsif($out eq 'c') {
sc_print_c($sc);
} elsif($out eq 'asm') {
sc_print_asm($sc);
} elsif($out eq 'bash') {
sc_print_bash($sc);
} elsif($out eq 'python') {
sc_print_python($sc);
} elsif($out eq 'raw') {
sc_print_raw($sc);
} else {
warn "Unvailable output format <$out>\n";
return undef;
}
return 1;
}
# Print commented string.
# @RETURN undef if error.
sub print_comment {
my ($string, $out) = @_;
if($out eq 'perl') {
print "# $string";
} elsif($out eq 'c') {
print "// $string";
} elsif($out eq 'asm') {
print ";; $string";
} elsif($out eq 'bash') {
print "# $string";
} elsif($out eq 'python') {
print "# $string";
} elsif($out eq 'raw') {
print "# $string";
} else {
warn "Unvailable output format <$out>\n";
return undef;
}
return 1;
}
# Check if $sc contain bad chars.
# @RETURN true if $sc contain bad chars, false overwise.
sub check_bad_ch {
my ($sc, $chars) = @_;
return 1 unless(length $chars);
while(length($sc) > 0) {
my $tmp = $chars;
while(length($tmp) > 0) {
my $c;
if($tmp =~ m/^\\x([0-9a-fA-F]{2})/) {
$c = hex("0x$1");
$tmp = substr($tmp, 4);
} else {
$c = ord(substr($tmp, 0, 1));
$tmp = substr($tmp, 1);
}
return 0 if(ord(substr($sc, 0, 1)) == $c);
}
$sc = substr($sc, 1);
}
return 1;
}
# Clean temporary files.
sub clean_tmp_files {
foreach(@TMP_FILES) {
unlink $_ if(-f $_);
}
}
sub END {
clean_tmp_files();
}
=pod
=head1 NAME
sc-make - Shellcode Maker -
Tool for automating shellcodes creation
=head1 SYNOPSIS
sc-make [OPTIONS] file
=head1 OPTIONS
=over 4
=item B<-t -test>
Test shellcode with strace.
=item B<-d -disassemble>
Disassemble the shellcode.
=item B<-o -out> FORMAT
Change the output format.
Available format : c,perl,bash,asm,python,raw. (default: raw)
=item B<-a -arch> ARCH
Specify the architecture (default: x86)
Available arch: x86, x86-64, arm
=item B<-b -bad> STRING
Specify the bad chars you don't want in shellcode.
Example : -bad "\x00\x0a"
=item B<-h -help>
Print short help.
=item B<-i -info>
Print long help.
=item B<-v -version>
Print program version.
=back
=head1 DESCRIPTION
B<This program> is a simply tool for assembling, disassembling,
and testing shellcodes.
Shellcodes must be wrote in B<ASM> with B<NASM> syntax.
=head1 VERSION
V1.0
=head1 AUTHOR
Written by B<Tosh>
(duretsimon73 -at- gmail -dot- com)
=head1 LICENCE
This program is a free software.
It is distrubued with the terms of the B<GPLv3 licence>.
=head1 DEPENDS
These programs are needed to run correctly sc-make :
=over 4
=item B<objdump>
=item B<nasm>
=item B<as>
=item B<strace>
=item B<gcc>
=item B<perl>
=back
=head1 EXAMPLES
=over 4
=item B<sc-make -o perl shellcode.asm> # Print shellcode in Perl format
# SHELLCODE LENGTH: 25
my $shellcode = "\x31\xc0\x50\x6a\x68\x68\x2f\x62\x61\x73\x68\x2f" .
"\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd" .
"\x80";
=item B<sc-make -o python shellcode.asm> # Print shellcode in Python format
# SHELLCODE LENGTH: 25
shellcode = ("\x31\xc0\x50\x6a\x68\x68\x2f\x62\x61\x73\x68\x2f"
"\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd"
"\x80");
=item B<sc-make -o asm shellcode.asm> # Print shellcode in ASM format
;; SHELLCODE LENGTH: 25
shellcode:
db 0x31,0xc0,0x50,0x6a,0x68,0x68,0x2f,0x62,0x61,0x73,0x68,0x2f
db 0x62,0x69,0x6e,0x89,0xe3,0x89,0xc1,0x89,0xc2,0xb0,0x0b,0xcd
db 0x80
=item B<sc-make -t shellcode.asm> # Test shellcode with strace
=item B<sc-make -d shellcode.asm> # Disassemble shellcode with objdump
=item B<sc-make -b "\x00\x0a"> # Print shellcode if it don't contain NUL and 0x0a byte
=back
=cut