-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrunshellcode.py
More file actions
68 lines (55 loc) · 1.75 KB
/
runshellcode.py
File metadata and controls
68 lines (55 loc) · 1.75 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
import sys
import ctypes
from ctypes import wintypes
import argparse
def test_shellcode(shellcode_file):
"""
Test shellcode in a controlled environment.
For security research purposes only.
"""
# Read shellcode from file
try:
with open(shellcode_file, 'rb') as f:
shellcode = f.read()
except Exception as e:
print(f"Error reading shellcode file: {e}")
return False
# Print shellcode length for verification
print(f"Shellcode size: {len(shellcode)} bytes")
# Allocate memory with RWX permissions
rwx = 0x40 # PAGE_EXECUTE_READWRITE
ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_void_p
ptr = ctypes.windll.kernel32.VirtualAlloc(
None,
len(shellcode),
0x1000, # MEM_COMMIT
rwx
)
if not ptr:
print("Memory allocation failed")
return False
# Copy shellcode to allocated memory
buffer = (ctypes.c_char * len(shellcode)).from_buffer_copy(shellcode)
ctypes.memmove(ptr, buffer, len(shellcode))
# Create function pointer
function = ctypes.cast(ptr, ctypes.CFUNCTYPE(None))
print("Executing shellcode...")
# Execute
function()
# Clean up
ctypes.windll.kernel32.VirtualFree(
ctypes.c_void_p(ptr),
0,
0x8000 # MEM_RELEASE
)
return True
def main():
parser = argparse.ArgumentParser(description='Shellcode testing harness for security research')
parser.add_argument('shellcode_file', help='Path to the shellcode binary file')
args = parser.parse_args()
if test_shellcode(args.shellcode_file):
print("Shellcode execution completed")
else:
print("Shellcode execution failed")
if __name__ == '__main__':
main()