|
1 | 1 | #!/usr/bin/python |
2 | | -import sys, getopt, argparse |
3 | | - |
| 2 | +import sys, getopt, argparse, re |
| 3 | +#TODO: Implement "-o" flag to output results of cleaned shellcode to a text file. |
4 | 4 |
|
5 | 5 | parser = argparse.ArgumentParser(prog='shellcode-cleaner', description='Takes pasted shellcode and cleans it however you like.', epilog="Default is to strip ALL of these characters. It'll strip spaces as well.") |
6 | 6 | parser.add_argument('-n', action='store_true', help="strip newline (\\n) characters") |
7 | 7 | parser.add_argument('-r', action='store_true', help="strip (\\r) characters") |
8 | 8 | parser.add_argument('-q', action='store_true', help="strip quotation marks (\"\") AND single quotes") |
9 | 9 | parser.add_argument('-x', action='store_true', help="strip (\\x) characters") |
10 | 10 | parser.add_argument('-s', action='store_true', help="strip semicolons (;)") |
| 11 | +parser.add_argument('--addhex', action='store_true', help="prepend \"\\x\" to each of the hex bytes. EXPERIMENTAL.") |
11 | 12 | args = parser.parse_args() |
12 | 13 | print "Shellcode (Press Enter then Control+D to submit):" |
13 | 14 | dirty_shellcode = sys.stdin.read() |
|
16 | 17 | "r": args.r, |
17 | 18 | "q": args.q, |
18 | 19 | "x": args.x, |
19 | | - "s": args.s |
| 20 | + "s": args.s, |
| 21 | + "addhex": args.addhex |
20 | 22 | } |
21 | 23 | #Clean up spacing first |
22 | 24 | clean_shellcode = dirty_shellcode.replace(' ', '') |
| 25 | +#chomp and clean shellcode |
23 | 26 | #This is for the "default option" to actually work. If any flag is set it will be set to false and the operation under the flags will proceed. If none are set, then the default operaiton to strip ALL the bad chars will occur. |
24 | 27 | default_flag=True |
25 | 28 | for key, value in args_dict.iteritems(): |
|
35 | 38 | clean_shellcode = clean_shellcode.replace('\\x', '') |
36 | 39 | elif key == 's' : |
37 | 40 | clean_shellcode = clean_shellcode.replace(';', '') |
| 41 | + elif key == 'addhex' : |
| 42 | + #Adding the '\x' |
| 43 | + clean_shellcode = clean_shellcode.replace('\n', '') |
| 44 | + clean_shellcode = '\\x'.join([clean_shellcode[i:i+2] for i in range(0, len(clean_shellcode), 2)]) |
| 45 | + #For some reason, first hex byte isn't given a "\x" with this method. This will fix that. |
| 46 | + clean_shellcode = '\\x' + clean_shellcode |
| 47 | + #Finally, we remove a trailing "\x" that gets added to our stirng. |
| 48 | + #re.sub(r'\\x$', '', repr(clean_shellcode)) |
| 49 | + |
38 | 50 | if default_flag == True: |
39 | 51 | clean_shellcode = dirty_shellcode.replace('\n', '').replace('\r', '').replace('"', '').replace('\\x', '').replace(';', '').replace(' ', '') |
40 | 52 | print clean_shellcode |
0 commit comments