forked from Ch1ngg/ShellcodeWrapperModify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellcode_encoder.py
More file actions
271 lines (236 loc) · 10.1 KB
/
shellcode_encoder.py
File metadata and controls
271 lines (236 loc) · 10.1 KB
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
#!/usr/bin/python
# -*- coding: utf8 -*-
#
# Author: Arno0x0x, Twitter: @Arno0x0x
# Modify: Ch1ng
#
import os
import struct
import random
import string
import argparse
from os import urandom
from string import Template
templates = {
'cpp': './templates/encryptedShellcodeWrapper.cpp',
'csharp': './templates/encryptedShellcodeWrapper.cs',
'python': './templates/encryptedShellcodeWrapper.py',
'golang': './templates/encryptedShellcodeWrapper.go',
}
resultFiles = {
'cpp': './result/encryptedShellcodeWrapper.cpp',
'csharp': './result/encryptedShellcodeWrapper.cs',
'python': './result/encryptedShellcodeWrapper.py',
'golang': './result/encryptedShellcodeWrapper.go'
}
resultBinFiles = {
'bin' :'./result/encryptpayload.bin'
}
#======================================================================================================
# CRYPTO FUNCTIONS
#======================================================================================================
#------------------------------------------------------------------------
# data as a bytearray
# key as a string
def xor(data, key):
l = len(key)
keyAsInt = list(map(ord, key))
return bytes(bytearray((
(data[i] ^ keyAsInt[i % l]) for i in range(0,len(data))
)))
def rc4(PlainBytes:bytes, KeyBytes:bytes):
#keystreamList = []
cipherList = []
keyLen = len(KeyBytes)
plainLen = len(PlainBytes)
S = list(range(256))
j = 0
for i in range(256):
j = (j + S[i] + KeyBytes[i % keyLen]) % 256
S[i], S[j] = S[j], S[i]
i = 0
j = 0
for m in range(plainLen):
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
k = S[(S[i] + S[j]) % 256]
cipherList.append(k ^ PlainBytes[m])
#result_hexstr = ','.join(['%02x' % i for i in cipherList])
return bytes(bytearray(cipherList))
#======================================================================================================
# OUTPUT FORMAT FUNCTIONS
#======================================================================================================
def convertFromTemplate(parameters, templateFile):
try:
with open(templateFile) as f:
src = Template(f.read())
result = src.substitute(parameters)
f.close()
return result
except IOError:
print(color("[!] Could not open or read template file [{}]".format(templateFile)))
return None
def formatGolang(data, key, cipherType):
shellcode = "\\x"
shellcode += "\\x".join(format(b,'02x') for b in data)
#shellcode += "\\x".join((format(b,'02x')+"\\x01\\x02\\x03") for b in data)
#print(shellcode)
key2 = "\\x"
key2 += "\\x".join(format(b,'02x') for b in bytes(key.encode('utf-8')))
result = convertFromTemplate({'shellcode': shellcode, 'key': key2, 'cipherType': cipherType}, templates['golang'])
if result != None:
try:
fileName = os.path.splitext(resultFiles['golang'])[0] + "_" + cipherType + os.path.splitext(resultFiles['golang'])[1]
with open(fileName,"w+") as f:
f.write(result)
f.close()
print(color("[+] Golang code file saved in [{}]".format(fileName)))
except IOError:
print(color("[!] Could not write Golang code [{}]".format(fileName)))
#------------------------------------------------------------------------
# data as a bytearray
def formatCPP(data, key, cipherType):
shellcode = "\\x"
shellcode += "\\x".join(format(b,'02x') for b in data)
#shellcode += "\\x".join((format(b,'02x')+"\\x01\\x02\\x03") for b in data)
#print(shellcode)
result = convertFromTemplate({'shellcode': shellcode, 'key': str(key), 'cipherType': cipherType}, templates['cpp'])
if result != None:
try:
fileName = os.path.splitext(resultFiles['cpp'])[0] + "_" + cipherType + os.path.splitext(resultFiles['cpp'])[1]
with open(fileName,"w+") as f:
f.write(result)
f.close()
print(color("[+] C++ code file saved in [{}]".format(fileName)))
except IOError:
print(color("[!] Could not write C++ code [{}]".format(fileName)))
#------------------------------------------------------------------------
# data as a bytearray
def formatCSharp(data, key, cipherType):
shellcode = '0x'
shellcode += ',0x'.join(format(b,'02x') for b in data)
result = convertFromTemplate({'shellcode': shellcode, 'key': str(key), 'cipherType': cipherType}, templates['csharp'])
if result != None:
try:
fileName = os.path.splitext(resultFiles['csharp'])[0] + "_" + cipherType + os.path.splitext(resultFiles['csharp'])[1]
with open(fileName,"w+") as f:
f.write(result)
f.close()
print(color("[+] C# code file saved in [{}]".format(fileName)))
except IOError:
print(color("[!] Could not write C# code [{}]".format(fileName)))
def formatPy(data, key, cipherType):
shellcode = '\\x'
shellcode += '\\x'.join(format(b,'02x') for b in data)
result = convertFromTemplate({'shellcode': shellcode, 'key': str(key), 'cipherType': cipherType}, templates['python'])
if result != None:
try:
fileName = os.path.splitext(resultFiles['python'])[0] + "_" + cipherType + os.path.splitext(resultFiles['python'])[1]
with open(fileName,"w+") as f:
f.write(result)
f.close()
print(color("[+] Python code file saved in [{}]".format(fileName)))
except IOError:
print(color("[!] Could not write Python code [{}]".format(fileName)))
def formatCPPBinfile(data):
fileName = os.path.splitext(resultBinFiles['bin'])[0] + "_" + cipherType + os.path.splitext(resultBinFiles['bin'])[1]
with open(fileName,"wb") as fo:
for x in data:
a = struct.pack('B', x)
fo.write(a)
print(color("[+] Bin file saved in [{}]".format(fileName)))
def color(string, color=None):
"""
Author: HarmJ0y, borrowed from Empire
Change text color for the Linux terminal.
"""
attr = []
# bold
attr.append('1')
if color:
if color.lower() == "red":
attr.append('31')
elif color.lower() == "green":
attr.append('32')
elif color.lower() == "blue":
attr.append('34')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
else:
if string.strip().startswith("[!]"):
attr.append('31')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
elif string.strip().startswith("[+]"):
attr.append('32')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
elif string.strip().startswith("[?]"):
attr.append('33')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
elif string.strip().startswith("[*]"):
attr.append('34')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
else:
return string
#======================================================================================================
# MAIN FUNCTION
#======================================================================================================
if __name__ == '__main__':
#------------------------------------------------------------------------
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("shellcodeFile", help="File name containing the raw shellcode to be encoded/encrypted")
parser.add_argument("encryptionType", help="Encryption algorithm to apply to the shellcode", choices=['xor','rc4'])
parser.add_argument("-bin", "--binary", help="Generates encrypt binary file", action="store_true")
parser.add_argument("-cpp", "--cplusplus", help="Generates C++ file code", action="store_true")
parser.add_argument("-cs", "--csharp", help="Generates C# file code", action="store_true")
parser.add_argument("-go", "--golang", help="Generates Golang file code", action="store_true")
parser.add_argument("-py", "--python", help="Generates Python file code", action="store_true")
args = parser.parse_args()
#------------------------------------------------------------------------------
# Check that required directories and path are available, if not create them
if not os.path.isdir("./result"):
os.makedirs("./result")
print(color("[+] Creating [./result] directory for resulting code files"))
#------------------------------------------------------------------------
# Open shellcode file and read all bytes from it
try:
with open(args.shellcodeFile,"rb") as shellcodeFileHandle:
shellcodeBytes = bytearray(shellcodeFileHandle.read())
shellcodeFileHandle.close()
print(color("[*] Shellcode file [{}] successfully loaded".format(args.shellcodeFile)))
except IOError:
print(color("[!] Could not open or read file [{}]".format(args.shellcodeFile)))
quit()
print(color("[*] Shellcode size: [{}] bytes".format(len(shellcodeBytes))))
#------------------------------------------------------------------------
# Perform XOR transformation
if args.encryptionType == 'xor':
masterKey = ''.join(random.sample(string.ascii_letters + string.digits, 8))
print(color("[*] XOR encoding the shellcode with key [{}]".format(masterKey)))
transformedShellcode = xor(shellcodeBytes, masterKey)
cipherType = 'xor'
elif args.encryptionType == 'rc4':
masterKey = ''.join(random.sample(string.ascii_letters + string.digits, 8))
print(color("[*] RC4 encoding the shellcode with key [{}]".format(masterKey)))
transformedShellcode = rc4(shellcodeBytes, bytes(masterKey,encoding="utf-8"))
cipherType = 'rc4'
#------------------------------------------------------------------------
# Display interim results
print("\n==================================== RESULT ====================================\n")
print(color("[*] Encrypted shellcode size: [{}] bytes".format(len(transformedShellcode))))
#------------------------------------------------------------------------
if args.cplusplus:
print(color("[*] Generating C++ code file"))
formatCPP(transformedShellcode, masterKey, cipherType)
if args.binary:
print(color("[*] Generating encrypt binary file"))
formatCPPBinfile(transformedShellcode)
if args.csharp:
print(color("[*] Generating C# code file"))
formatCSharp(transformedShellcode, masterKey, cipherType)
if args.python:
print(color("[*] Generating Python code file"))
formatPy(transformedShellcode, masterKey, cipherType)
if args.golang:
print(color("[*] Generating Python code file"))
formatGolang(transformedShellcode, masterKey, cipherType)