Skip to content

Commit ec4f240

Browse files
committed
Add tests to reproduce setup/errors of #72
1 parent 217da0d commit ec4f240

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/test_injection.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest
3+
4+
import os
5+
import sys
6+
import time
7+
import struct
8+
import textwrap
9+
import shutil
10+
11+
import windows
12+
import windows.generated_def as gdef
13+
14+
from .pfwtest import *
15+
16+
# Its really the same test as test_process.test_load_library
17+
def test_dll_injection(proc32_64):
18+
assert "wintrust.dll" not in [mod.name for mod in proc32_64.peb.modules]
19+
windows.injection.load_dll_in_remote_process(proc32_64, "wintrust.dll")
20+
assert "wintrust.dll" in [mod.name for mod in proc32_64.peb.modules]
21+
22+
def test_dll_injection_error_reporting(proc32_64):
23+
with pytest.raises(windows.injection.InjectionFailedError) as excinfo:
24+
windows.injection.load_dll_in_remote_process(proc32_64, "NO_A_DLL.dll")
25+
assert excinfo.value.__cause__.winerror == gdef.ERROR_MOD_NOT_FOUND
26+
27+
def test_dll_injection_access_denied(proc32_64, tmpdir):
28+
"""Emulate injection of MsStore python, were its DLL are not executable by any other append
29+
See: https://github.com/hakril/PythonForWindows/issues/72
30+
"""
31+
mybitness = windows.current_process.bitness
32+
if proc32_64.bitness == mybitness:
33+
DLLPATH = r"c:\windows\system32\wintrust.dll"
34+
elif mybitness == 64: # target is 32
35+
DLLPATH = r"c:\windows\syswow64\wintrust.dll"
36+
elif mybitness == 32: # target is 64
37+
DLLPATH = r"c:\windows\sysnative\wintrust.dll"
38+
else:
39+
raise Value("WTF ARE THE BITNESS ?")
40+
targetname = os.path.join(str(tmpdir), "wintrust_noexec.dll")
41+
shutil.copy(DLLPATH, targetname)
42+
# Deny Execute; allow read for everyone
43+
sd = windows.security.SecurityDescriptor.from_string("D:(D;;GXFX;;;WD)(A;;1;;;WD)")
44+
sd.to_filename(targetname)
45+
46+
try:
47+
with pytest.raises(windows.injection.InjectionFailedError) as excinfo:
48+
windows.injection.load_dll_in_remote_process(proc32_64, targetname)
49+
assert excinfo.value.__cause__.winerror == gdef.ERROR_ACCESS_DENIED
50+
finally:
51+
proc32_64.exit()
52+
proc32_64.wait()
53+
time.sleep(0.5) # Fail on Azure CI of no sleep
54+
os.unlink(targetname)

0 commit comments

Comments
 (0)