18
18
if IS_WINDOWS :
19
19
import queue
20
20
import threading
21
- import windows
22
- from windows .generated_def .winstructs import CREATE_SUSPENDED
23
- from windows .generated_def import ntstatus
24
- ntstatus_names = {int (v ):k for k ,v in ntstatus .__dict__ .items () if k .startswith ('STATUS' ) and v }
25
21
else :
26
22
import fcntl
27
23
import pty
38
34
from pwnlib .util .misc import which
39
35
from pwnlib .util .misc import normalize_argv_env
40
36
from pwnlib .util .packing import _need_bytes
37
+ from pwnlib .util .proc import cwd
38
+ from pwnlib .util .proc import memory_maps
41
39
42
40
log = getLogger (__name__ )
43
41
@@ -380,25 +378,13 @@ def __init__(self, argv = None,
380
378
p .success ('pid %i' % self .pid )
381
379
382
380
if IS_WINDOWS :
383
- class winprocess (windows .winobject .process .WinProcess ):
384
- def __del__ (self ):
385
- # sys.path is not None -> check if python shutdown
386
- # workaround crash on shutdown trying to delete invalid WinProcess object
387
- if hasattr (sys , "path" ) and sys .path is not None :
388
- super (winprocess , self ).__del__ ()
389
-
390
- #: :class:`windows.winobject.process.WinProcess` object that provides insight into the process
391
- self .win_process = winprocess (pid = self .pid )
392
381
self ._read_thread = None
393
382
self ._read_queue = queue .Queue ()
394
383
if self .proc .stdout :
395
384
# Read from stdout in a thread
396
385
self ._read_thread = threading .Thread (target = _read_in_thread , args = (self ._read_queue , self .proc .stdout ))
397
386
self ._read_thread .daemon = True
398
387
self ._read_thread .start ()
399
-
400
- if (creationflags & CREATE_SUSPENDED ) == 0 :
401
- self ._wait_initialized ()
402
388
return
403
389
404
390
if self .pty is not None :
@@ -522,18 +508,6 @@ def __on_enoexec(self, exception):
522
508
# we don't have a qemu which can run it.
523
509
self .exception (exception )
524
510
525
- def _check_initialized (self ):
526
- # Accessing PEB until WinProcess is done initializing.
527
- try :
528
- self .win_process .peb .modules [1 ]
529
- return True
530
- except :
531
- return False
532
-
533
- def _wait_initialized (self ):
534
- while not self ._check_initialized () and not self .win_process .is_exit :
535
- time .sleep (0.05 )
536
-
537
511
@property
538
512
def program (self ):
539
513
"""Alias for ``executable``, for backward compatibility.
@@ -566,10 +540,7 @@ def cwd(self):
566
540
'/proc'
567
541
"""
568
542
try :
569
- if IS_WINDOWS :
570
- self ._cwd = self .win_process .peb .ProcessParameters .contents .CurrentDirectory .DosPath .str
571
- else :
572
- self ._cwd = os .readlink ('/proc/%i/cwd' % self .pid )
543
+ self ._cwd = cwd (self .pid )
573
544
except Exception :
574
545
pass
575
546
@@ -713,12 +684,8 @@ def poll(self, block = False):
713
684
if returncode is not None and not self ._stop_noticed :
714
685
self ._stop_noticed = time .time ()
715
686
signame = ''
716
- if IS_WINDOWS :
717
- if returncode in ntstatus_names :
718
- signame = ' (%s)' % (ntstatus_names [returncode ])
719
- else :
720
- if returncode < 0 :
721
- signame = ' (%s)' % (signal_names .get (returncode , 'SIG???' ))
687
+ if returncode < 0 :
688
+ signame = ' (%s)' % (signal_names .get (returncode , 'SIG???' ))
722
689
723
690
self .info ("Process %r stopped with exit code %d%s (pid %i)" % (self .display ,
724
691
returncode ,
@@ -920,15 +887,7 @@ def libs(self):
920
887
by the process to the address it is loaded at in the process' address
921
888
space.
922
889
"""
923
- if IS_WINDOWS :
924
- if not self ._check_initialized ():
925
- raise Exception ("PEB not initialized while getting the loaded modules" )
926
- return {module .name .lower (): module .baseaddr for module in self .win_process .peb .modules if module .name }
927
-
928
- try :
929
- maps_raw = open ('/proc/%d/maps' % self .pid ).read ()
930
- except IOError :
931
- maps_raw = None
890
+ maps_raw = memory_maps (self .pid )
932
891
933
892
if not maps_raw :
934
893
import pwnlib .elf .elf
@@ -938,18 +897,18 @@ def libs(self):
938
897
939
898
# Enumerate all of the libraries actually loaded right now.
940
899
maps = {}
941
- for line in maps_raw . splitlines () :
942
- if '/' not in line : continue
943
- path = line [ line . index ( '/' ):]
900
+ for mapping in maps_raw :
901
+ path = mapping . path
902
+ if os . sep not in path : continue
944
903
path = os .path .realpath (path )
945
904
if path not in maps :
946
905
maps [path ]= 0
947
906
948
907
for lib in maps :
949
908
path = os .path .realpath (lib )
950
- for line in maps_raw . splitlines () :
951
- if line . endswith ( path ) :
952
- address = line .split ('-' )[0 ]
909
+ for mapping in maps_raw :
910
+ if mapping . path == path :
911
+ address = mapping . addr .split ('-' )[0 ]
953
912
maps [lib ] = int (address , 16 )
954
913
break
955
914
@@ -1058,11 +1017,6 @@ def leak(self, address, count=1):
1058
1017
>>> p.leak(e.address, 4)
1059
1018
b'\x7fELF'
1060
1019
"""
1061
- if IS_WINDOWS :
1062
- if not self ._check_initialized ():
1063
- self .error ("PEB not initialized while reading memory" )
1064
- return self .win_process .read_memory (address , count )
1065
-
1066
1020
# If it's running under qemu-user, don't leak anything.
1067
1021
if 'qemu-' in os .path .realpath ('/proc/%i/exe' % self .pid ):
1068
1022
self .error ("Cannot use leaker on binaries under QEMU." )
@@ -1108,11 +1062,6 @@ def writemem(self, address, data):
1108
1062
>>> io.recvall()
1109
1063
b'aaaabaaacaaadaaaeaaafaaagaaahaaa'
1110
1064
"""
1111
- if IS_WINDOWS :
1112
- if not self ._check_initialized ():
1113
- self .error ("PEB not initialized while writing memory" )
1114
- return self .win_process .write_memory (address , data )
1115
-
1116
1065
if 'qemu-' in os .path .realpath ('/proc/%i/exe' % self .pid ):
1117
1066
self .error ("Cannot use leaker on binaries under QEMU." )
1118
1067
0 commit comments