-
Notifications
You must be signed in to change notification settings - Fork 27
/
cdb.py
59 lines (46 loc) · 1.57 KB
/
cdb.py
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
import argparse
import sys
parser = argparse.ArgumentParser(description="Converts shellcode generated by pe2shc into cdb format.")
parser.add_argument("-f","--file",help="File to convert to cdb format")
args = parser.parse_args()
if len(sys.argv) < 2:
print("Please specify the shellcode file.")
exit(1)
# Try to open file
try:
f = open(args.file, "r+")
except FileNotFoundError:
print("File not found.")
exit(1)
# Delete the first line and last 2 lines
lines = f.readlines()
f.seek(0)
f.writelines(lines[1:-2])
# Read the file again
f.seek(0)
unformatted_shellcode = r'{}'.format(f.read())
# Remove commas and whitespace
temp_shellcode = unformatted_shellcode.replace(",","")
temp_shellcode = temp_shellcode.replace("\n","")
temp_shellcode = temp_shellcode.replace(" ","")
# Begin formatting
PREFIX = ";eb @$t0+"
counter = hex(0)[2:].zfill(2) # Hex counter starts at 00
linebreak_counter = 0
formatted_shellcode = ""
shellcode_list = temp_shellcode.split("0x")
for i in range(1,len(shellcode_list)):
formatted_shellcode = formatted_shellcode + (PREFIX + str(counter) + " " + shellcode_list[i].upper())
counter = hex(i)[2:].zfill(2)
linebreak_counter += 1
if(linebreak_counter % 4 == 0):
formatted_shellcode = formatted_shellcode + "\n"
memory_needed = int(counter,16)
# Output file and write content
out_file = open("out.wds", "w")
out_file.write(".foreach /pS 5 ( register { .dvalloc " + str(memory_needed) + " } ) { r @$t0 = register }\n")
out_file.write(formatted_shellcode)
out_file.write("\nr @$ip=@$t0\n")
print("[+] Data written to out.wds");
out_file.close()
f.close()