Skip to content

Commit 13c2842

Browse files
committed
process improv : bitness based on PVOID size + CurrentProcess.name based on GetProcessImageFileNameW + peb addr based on RtlGetCurrentPeb
1 parent 3818bb2 commit 13c2842

1 file changed

Lines changed: 33 additions & 63 deletions

File tree

windows/winobject/process.py

Lines changed: 33 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def exit_code(self):
6464

6565

6666
class Process(utils.AutoHandle):
67-
@utils.fixedpropety
67+
@utils.fixedproperty
6868
def is_wow_64(self):
6969
"""``True`` if the process is a SysWow64 process (32bit process on 64bits system).
7070
@@ -73,7 +73,7 @@ def is_wow_64(self):
7373
# return utils.is_wow_64(self.handle)
7474
return utils.is_wow_64(self.limited_handle)
7575

76-
@utils.fixedpropety
76+
@utils.fixedproperty
7777
def bitness(self):
7878
"""The bitness of the process
7979
@@ -85,15 +85,25 @@ def bitness(self):
8585
return 32
8686
return 64
8787

88-
@utils.fixedpropety
88+
@utils.fixedproperty
8989
def limited_handle(self):
9090
if windows.system.version[0] <= 5:
9191
# Windows XP | Serveur 2003
9292
return winproxy.OpenProcess(PROCESS_QUERY_INFORMATION, dwProcessId=self.pid)
9393
return winproxy.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, dwProcessId=self.pid)
9494

95+
@utils.fixedproperty
96+
def name(self):
97+
"""Name of the process
98+
99+
:type: :class:`str`
100+
"""
101+
buffer = ctypes.create_unicode_buffer(0x1024)
102+
rsize = winproxy.GetProcessImageFileNameW(self.limited_handle, buffer)
103+
# GetProcessImageFileNameW returns the fullpath
104+
return buffer[:rsize].split("\\")[-1]
95105

96-
@utils.fixedpropety
106+
@utils.fixedproperty
97107
def ppid(self):
98108
"""Parent Process ID
99109
@@ -559,14 +569,17 @@ def set_token(self, token):
559569

560570
class CurrentThread(Thread):
561571
"""The current thread"""
562-
@property #It's not a fixedpropety because executing thread might change
572+
573+
@property #It's not a fixedproperty because executing thread might change
563574
def tid(self):
564575
"""Thread ID
565576
566577
:type: :class:`int`
567578
"""
568579
return winproxy.GetCurrentThreadId()
569580

581+
582+
570583
@property
571584
def owner(self):
572585
"""The current process
@@ -585,46 +598,19 @@ def exit(self, code=0):
585598
"""Exit the thread"""
586599
return winproxy.ExitThread(code)
587600

588-
589-
590601
def wait(self, timeout=INFINITE):
591602
"""Raise :class:`ValueError` to prevent deadlock :D"""
592603
raise ValueError("wait() on current thread")
593604

594605

595606
class CurrentProcess(Process):
596607
"""The current process"""
597-
get_peb = None
598-
599-
get_peb_32_code = x86.MultipleInstr()
600-
get_peb_32_code += x86.Mov('EAX', x86.mem('fs:[0x30]'))
601-
get_peb_32_code += x86.Ret()
602-
get_peb_32_code = get_peb_32_code.get_code()
603-
604-
get_peb_64_code = x64.MultipleInstr()
605-
get_peb_64_code += x64.Mov('RAX', x64.mem('gs:[0x60]'))
606-
get_peb_64_code += x64.Ret()
607-
get_peb_64_code = get_peb_64_code.get_code()
608-
609608
allocator = native_exec.native_function.allocator
610609

611-
name = "CurrentProcess" # Used by Winthread for __repr__
612-
613-
# Use RtlGetCurrentPeb ?
614-
def get_peb_builtin(self):
615-
if self.get_peb is not None:
616-
return self.get_peb
617-
if self.bitness == 32:
618-
get_peb = native_exec.create_function(self.get_peb_32_code, [PVOID])
619-
else:
620-
get_peb = native_exec.create_function(self.get_peb_64_code, [PVOID])
621-
self.get_peb = get_peb
622-
return get_peb
623-
624610
def _get_handle(self):
625611
return winproxy.GetCurrentProcess()
626612

627-
@utils.fixedpropety
613+
@utils.fixedproperty
628614
def limited_handle(self):
629615
return winproxy.GetCurrentProcess()
630616

@@ -640,23 +626,21 @@ def pid(self):
640626
"""
641627
return os.getpid()
642628

643-
@utils.fixedpropety # leave it has fixed property as we don't care if CurrentProcess is never collected
629+
@utils.fixedproperty # leave it has fixed property as we don't care if CurrentProcess is never collected
644630
def peb(self):
645631
"""The Process Environment Block of the current process
646632
647633
:type: :class:`PEB`
648634
"""
649-
return PEB.from_address(self.get_peb_builtin()())
635+
return PEB.from_address(windows.winproxy.RtlGetCurrentPeb())
650636

651-
@utils.fixedpropety
637+
@utils.fixedproperty
652638
def bitness(self):
653639
"""The bitness of the process
654640
655641
:type: :class:`int` -- 32 or 64
656642
"""
657-
import platform
658-
bits = platform.architecture()[0]
659-
return int(bits[:2])
643+
return ctypes.sizeof(gdef.PVOID) * 8 # byte to bits
660644

661645
def virtual_alloc(self, size, prot=PAGE_EXECUTE_READWRITE):
662646
"""Allocate memory in the process
@@ -718,7 +702,7 @@ def wait(self, timeout=INFINITE):
718702
"""Raise :class:`ValueError` to prevent deadlock :D"""
719703
raise ValueError("wait() on current thread")
720704

721-
@utils.fixedpropety
705+
@utils.fixedproperty
722706
def peb_syswow(self):
723707
"""The 64bits PEB of a SysWow64 process
724708
@@ -761,21 +745,21 @@ def _from_handle(cls, handle):
761745
# Create a DeadThread if thread is already dead ?
762746
return WinThread(handle=handle)
763747

764-
@utils.fixedpropety
748+
@utils.fixedproperty
765749
def tid(self):
766750
"""Thread ID
767751
768752
:type: :class:`int`"""
769753
return self._get_thread_id(self.handle)
770754

771-
@utils.fixedpropety
755+
@utils.fixedproperty
772756
def owner_pid(self):
773757
res = THREAD_BASIC_INFORMATION()
774758
windows.winproxy.NtQueryInformationThread(self.handle, ThreadBasicInformation, byref(res), ctypes.sizeof(res))
775759
owner_id = res.ClientId.UniqueProcess
776760
return owner_id
777761

778-
@utils.fixedpropety
762+
@utils.fixedproperty
779763
def owner(self):
780764
"""The Process owning the thread
781765
@@ -900,8 +884,6 @@ def teb_base(self):
900884
# TebBase->NtTib.ExceptionList = (PVOID)Teb32Base;
901885
return self.owner.read_dword(main_teb_addr)
902886

903-
904-
905887
@property
906888
def teb_syswow_base(self):
907889
"""The address of the thread's TEB64 for a SysWow64 process
@@ -914,6 +896,7 @@ def teb_syswow_base(self):
914896
return self._get_principal_teb_addr()
915897

916898

899+
917900
def exit(self, code=0):
918901
"""Exit the thread"""
919902
return winproxy.TerminateThread(self.handle, code)
@@ -1006,18 +989,7 @@ def _from_PROCESSENTRY32W(cls, entry):
1006989
return cls(pid=pid, name=name, ppid=ppid)
1007990

1008991

1009-
@utils.fixedpropety
1010-
def name(self):
1011-
"""Name of the process
1012-
1013-
:type: :class:`str`
1014-
"""
1015-
buffer = ctypes.create_unicode_buffer(0x1024)
1016-
rsize = winproxy.GetProcessImageFileNameW(self.limited_handle, buffer)
1017-
# GetProcessImageFileNameW returns the fullpath
1018-
return buffer[:rsize].split("\\")[-1]
1019-
1020-
@utils.fixedpropety
992+
@utils.fixedproperty
1021993
def pid(self):
1022994
"""Process ID
1023995
@@ -1150,9 +1122,7 @@ def execute_python_unsafe(self, pycode):
11501122
return injection.execute_python_code(self, pycode)
11511123

11521124

1153-
1154-
1155-
@utils.fixedpropety
1125+
@utils.fixedproperty
11561126
def peb_addr(self):
11571127
"""The address of the PEB
11581128
@@ -1179,7 +1149,7 @@ def peb_addr(self):
11791149
raise ValueError("Could not get peb addr of process {0}".format(self.name))
11801150
return peb_addr
11811151

1182-
# Not a fixedpropety to prevent ref-cycle and uncollectable WinProcess
1152+
# Not a fixedproperty to prevent ref-cycle and uncollectable WinProcess
11831153
# Try with a weakref ?
11841154
@property
11851155
def peb(self):
@@ -1193,7 +1163,7 @@ def peb(self):
11931163
return RemotePEB32(self.peb_addr, self)
11941164
return RemotePEB(self.peb_addr, self)
11951165

1196-
@utils.fixedpropety
1166+
@utils.fixedproperty
11971167
def peb_syswow_addr(self):
11981168
if not self.is_wow_64:
11991169
raise ValueError("Not a syswow process")
@@ -1212,7 +1182,7 @@ def peb_syswow_addr(self):
12121182
peb_addr = struct.unpack("<Q", data[x.PebBaseAddress.offset: x.PebBaseAddress.offset+8])[0]
12131183
return peb_addr
12141184

1215-
# Not a fixedpropety to prevent ref-cycle and uncollectable WinProcess
1185+
# Not a fixedproperty to prevent ref-cycle and uncollectable WinProcess
12161186
# Try with a weakref ?
12171187
@property
12181188
def peb_syswow(self):

0 commit comments

Comments
 (0)