11# -*- coding: utf-8 -*-
22import pytest
33
4- import os
5- import sys
6- import time
7- import struct
8- import textwrap
4+ import weakref
95import shutil
6+ import time
107
118import windows
129import windows .generated_def as gdef
1310
14- from .pfwtest import *
11+ from .conftest import pop_proc_32 , pop_proc_64
12+ from .pfwtest import DEFAULT_CREATION_FLAGS
13+
14+ @pytest .fixture (params =
15+ [(pop_proc_32 , DEFAULT_CREATION_FLAGS ),
16+ (pop_proc_32 , gdef .CREATE_SUSPENDED ),
17+ (pop_proc_64 , DEFAULT_CREATION_FLAGS ),
18+ (pop_proc_64 , gdef .CREATE_SUSPENDED )],
19+ ids = ["proc32" , "proc32susp" , "proc64" , "proc64susp" ])
20+ def proc_3264_runsus (request ):
21+ """Fixture for process 32/64 both running & suspended"""
22+ proc_poper , dwCreationFlags = request .param
23+ proc = proc_poper (dwCreationFlags = dwCreationFlags )
24+ time .sleep (0.2 ) # Give time to the process to load :)
25+ print ("Created {0} ({1}bits) for test" .format (proc , proc .bitness ))
26+ yield weakref .proxy (proc ) # provide the fixture value
27+ try :
28+ proc .exit (0 )
29+ except WindowsError as e :
30+ if not proc .is_exit :
31+ raise
32+ # print("DEL PROC")
33+ del proc
1534
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 ]
35+ # Its really the same test as test_process.test_load_library but with suspended process as well
36+ def test_dll_injection (proc_3264_runsus ):
37+ 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 ]
2140
22- def test_dll_injection_error_reporting (proc32_64 ):
41+ def test_dll_injection_error_reporting (proc_3264_runsus ):
2342 with pytest .raises (windows .injection .InjectionFailedError ) as excinfo :
24- windows .injection .load_dll_in_remote_process (proc32_64 , "NO_A_DLL.dll" )
43+ windows .injection .load_dll_in_remote_process (proc_3264_runsus , "NO_A_DLL.dll" )
2544 assert excinfo .value .__cause__ .winerror == gdef .ERROR_MOD_NOT_FOUND
2645
27- def test_dll_injection_access_denied (proc32_64 , tmpdir ):
46+ def test_dll_injection_access_denied (proc_3264_runsus , tmpdir ):
2847 """Emulate injection of MsStore python, were its DLL are not executable by any other append
2948 See: https://github.com/hakril/PythonForWindows/issues/72
3049 """
3150 mybitness = windows .current_process .bitness
32- if proc32_64 .bitness == mybitness :
51+ if proc_3264_runsus .bitness == mybitness :
3352 DLLPATH = r"c:\windows\system32\wintrust.dll"
3453 elif mybitness == 64 : # target is 32
3554 DLLPATH = r"c:\windows\syswow64\wintrust.dll"
@@ -45,10 +64,10 @@ def test_dll_injection_access_denied(proc32_64, tmpdir):
4564
4665 try :
4766 with pytest .raises (windows .injection .InjectionFailedError ) as excinfo :
48- windows .injection .load_dll_in_remote_process (proc32_64 , targetname )
67+ windows .injection .load_dll_in_remote_process (proc_3264_runsus , targetname )
4968 assert excinfo .value .__cause__ .winerror == gdef .ERROR_ACCESS_DENIED
5069 finally :
51- proc32_64 .exit ()
52- proc32_64 .wait ()
70+ proc_3264_runsus .exit ()
71+ proc_3264_runsus .wait ()
5372 time .sleep (0.5 ) # Fail on Azure CI of no sleep
5473 os .unlink (targetname )
0 commit comments