-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryToShellcode.py
105 lines (97 loc) · 4.5 KB
/
binaryToShellcode.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
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
#!/usr/bin/env python3
import argparse
import base64
'''Based on the script of FortyNorthSecurity. Thank you so much for doing the hard work.
https://github.com/FortyNorthSecurity/RandomScripts/blob/main/Cobalt%20Scripts/shellcode_formatter.py
Edited by @_Barriuso
'''
class Color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
def get_args():
parser = argparse.ArgumentParser(description='Convert binary raw to different Shellcode formats')
parser.add_argument('-f', '--file', dest='file', type=str, required=True, help='File to convert to Csharp')
parser.add_argument('-o', '--output', dest='output', type=str,default=None, help='Name of the output file')
parser.add_argument('-c', '--csharp', dest='csharp', action="store_true",default=False,
help='Convert to Csharp')
parser.add_argument('-s', '--standard', dest='standard', action="store_true", default=False,
help='Convert to standard Shellcode')
parser.add_argument('--fsharp', dest='fsharp',action="store_true", default=False,
help='Convert to fsharp Shellcode')
parser.add_argument('--base64', dest='base64',action="store_true", default=False,
help='Convert to Base64')
return parser.parse_args()
def readFile(fileRead):
try:
with open(fileRead, 'rb') as sc_handle:
return sc_handle.read()
except FileNotFoundError:
print (Color.RED+"The file does not exists.")
def writeToFile(filename,content):
f = open(filename, "a+")
f.write(content)
f.close()
def convertShellcode(file,csharp,standard,fsharp,b64,output):
# Just raw binary blog base64 encoded
if (b64):
encoded_raw = base64.b64encode(file)
print(Color.GREEN+"Your base64 raw shellcode is in file called base64_raw.txt")
writeToFile("base64_raw.txt",encoded_raw.decode('ascii'))
# Print in "standard" shellcode format \x41\x42\x43....
binary_code = ''
fs_code = ''
for byte in file:
binary_code += "\\x" + hex(byte)[2:].zfill(2)
# this is for f#
fs_code += "0x" + hex(byte)[2:].zfill(2) + "uy;"
if (standard):
if (output != None):
print(Color.GREEN + "Your standard shellcode is in file called "+output)
writeToFile(output, binary_code)
else:
print(Color.GREEN + "Your standard shellcode is in file called standard_shellcode.txt")
writeToFile("standard_shellcode.txt",binary_code)
if (fsharp):
if (output != None):
print(Color.GREEN + "Your fsharp shellcode is in file called "+output)
writeToFile(output, fs_code)
else:
print(Color.GREEN + "Your fsharp shellcode is in file called fsharp_shellcode.txt")
writeToFile("fsharp_shellcode.txt", fs_code)
if (csharp):
# Convert this into a C# style shellcode format
cs_shellcode = "0" + ",0".join(binary_code.split("\\")[1:])
if (output != None):
print(Color.GREEN + "Your chsarp shellcode is in file called "+output)
writeToFile(output, cs_shellcode)
# Base 64 encode the C# code (for use with certain payloads :))
encoded_cs = base64.b64encode(cs_shellcode.encode())
print(Color.GREEN + "Your chsarp shellcode is in base64 file called b64"+output)
writeToFile("b64"+output, encoded_cs.decode('ascii'))
else:
print(Color.GREEN + "Your chsarp shellcode is in file called fsharp_shellcode.txt")
writeToFile("csharp_shellcode.txt",cs_shellcode)
# Base 64 encode the C# code (for use with certain payloads :))
encoded_cs = base64.b64encode(cs_shellcode.encode())
print(Color.GREEN + "Your chsarp shellcode is in base64 file called b64Csharp_shellcode.txt")
writeToFile("b64Csharp_shellcode.txt", encoded_cs.decode('ascii'))
if __name__ == '__main__':
args = get_args()
file = args.file
csharp = args.csharp
fsharp = args.fsharp
standard = args.standard
b64 = args.base64
output = args.output
if (b64 == False and csharp == False and fsharp == False and standard == False):
print (Color.BOLD+Color.RED+"You need to put one shellcode format to convert the binary")
exit()
convertShellcode(readFile(file),csharp,standard,fsharp,b64,output)