Skip to content

Commit 77b78fa

Browse files
authored
Added the ability to insert escape hex (\x) character before each set of hex bytes.
1 parent 2b28caa commit 77b78fa

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

shellcode-cleaner.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/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.
44

55
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.")
66
parser.add_argument('-n', action='store_true', help="strip newline (\\n) characters")
77
parser.add_argument('-r', action='store_true', help="strip (\\r) characters")
88
parser.add_argument('-q', action='store_true', help="strip quotation marks (\"\") AND single quotes")
99
parser.add_argument('-x', action='store_true', help="strip (\\x) characters")
1010
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.")
1112
args = parser.parse_args()
1213
print "Shellcode (Press Enter then Control+D to submit):"
1314
dirty_shellcode = sys.stdin.read()
@@ -16,10 +17,12 @@
1617
"r": args.r,
1718
"q": args.q,
1819
"x": args.x,
19-
"s": args.s
20+
"s": args.s,
21+
"addhex": args.addhex
2022
}
2123
#Clean up spacing first
2224
clean_shellcode = dirty_shellcode.replace(' ', '')
25+
#chomp and clean shellcode
2326
#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.
2427
default_flag=True
2528
for key, value in args_dict.iteritems():
@@ -35,6 +38,15 @@
3538
clean_shellcode = clean_shellcode.replace('\\x', '')
3639
elif key == 's' :
3740
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+
3850
if default_flag == True:
3951
clean_shellcode = dirty_shellcode.replace('\n', '').replace('\r', '').replace('"', '').replace('\\x', '').replace(';', '').replace(' ', '')
4052
print clean_shellcode

0 commit comments

Comments
 (0)