Skip to content

Commit d0a5dc3

Browse files
committed
more testing
1 parent 2644b53 commit d0a5dc3

3 files changed

Lines changed: 19 additions & 11 deletions

File tree

tests/test_injection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import weakref
55
import shutil
66
import time
7+
import os
78

89
import windows
910
import windows.generated_def as gdef
@@ -35,8 +36,9 @@ def proc_3264_runsus(request):
3536
# Its really the same test as test_process.test_load_library but with suspended process as well
3637
def test_dll_injection(proc_3264_runsus):
3738
assert (not proc_3264_runsus.peb.Ldr) or ("wintrust.dll" not in [mod.name for mod in proc_3264_runsus.peb.modules])
38-
windows.injection.load_dll_in_remote_process(proc_3264_runsus, "wintrust.dll")
39-
assert "wintrust.dll" in [mod.name for mod in proc_3264_runsus.peb.modules]
39+
modaddr = windows.injection.load_dll_in_remote_process(proc_3264_runsus, "wintrust.dll")
40+
wintrustmod = [mod for mod in proc_3264_runsus.peb.modules if mod.name == "wintrust.dll"][0]
41+
assert wintrustmod.baseaddr == modaddr
4042

4143
def test_dll_injection_error_reporting(proc_3264_runsus):
4244
with pytest.raises(windows.injection.InjectionFailedError) as excinfo:

tests/test_process.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,11 @@ def test_load_library(self, proc32_64):
370370
proc32_64.load_library(DLL)
371371
assert DLL in [m.name for m in proc32_64.peb.modules]
372372

373+
def test_load_library_suspended(self, proc32_64_suspended):
374+
DLL = "wintrust.dll"
375+
proc32_64_suspended.load_library(DLL)
376+
assert DLL in [m.name for m in proc32_64_suspended.peb.modules]
377+
373378
def test_load_library_unicode_name(self, proc32_64, tmpdir):
374379
mybitness = windows.current_process.bitness
375380
UNICODE_FILENAME = u'\u4e2d\u56fd\u94f6\u884c\u7f51\u94f6\u52a9\u624b.dll'

windows/injection.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,20 @@ def generate_simple_LoadLibraryW_32_with_error(k32):
128128
code += x86.Ret()
129129
return code.get_code()
130130

131-
def generate_simple_LoadLibraryW_64(load_libraryW, GetLastError, remote_store):
131+
def generate_simple_LoadLibraryW_64_with_error(k32, remote_store):
132+
"""A shellcode that execute LoadLibraryW(param) and store the value at a fixed address.
133+
This allow a 32b process to inject and retrieve a 64bit module address
134+
135+
Thread return value is the result of GetLastError()
136+
"""
137+
load_libraryW = k32.pe.exports["LoadLibraryW"]
138+
GetLastError = k32.pe.exports["GetLastError"]
132139
code = RemoteLoadLibrayStub = x64.MultipleInstr()
133140
code += x64.Mov("RAX", load_libraryW)
134141
code += (x64.Push("RDI") * 5) # Prepare stack
135142
code += x64.Call("RAX")
136143
code += x64.Mov(x64.deref(remote_store), "RAX")
137-
code += x64.Mov("RAX", GetLastError)
144+
code += x64.Mov("RAX", GetLastError) # Add a jump ?
138145
code += x64.Call("RAX")
139146
code += (x64.Pop("RDI") * 5) # Clean stack
140147
code += x64.Ret()
@@ -161,12 +168,6 @@ def load_dll_in_remote_process(target, dll_path):
161168
if k32:
162169
# We have kernel32 \o/
163170
k32 = k32[0]
164-
try:
165-
load_libraryW = k32.pe.exports["LoadLibraryW"]
166-
GetLastError = k32.pe.exports["GetLastError"]
167-
except KeyError:
168-
raise ValueError("Kernel32 have no export <LoadLibraryA> (wtf)")
169-
170171
with target.allocated_memory(0x1000) as addr:
171172
if target.bitness == 32:
172173
shellcode32 = generate_simple_LoadLibraryW_32_with_error(k32)
@@ -193,7 +194,7 @@ def load_dll_in_remote_process(target, dll_path):
193194
param_addr = addr
194195
addr += len(full_dll_name)
195196
shellcode_addr = addr
196-
shellcode = generate_simple_LoadLibraryW_64(load_libraryW, GetLastError, retval_addr)
197+
shellcode = generate_simple_LoadLibraryW_64_with_error(k32, retval_addr)
197198
target.write_memory(shellcode_addr, shellcode)
198199
t = target.create_thread(shellcode_addr, param_addr)
199200
t.wait()

0 commit comments

Comments
 (0)