A Process on the system
| 981 | |
| 982 | |
| 983 | class WinProcess(Process): |
| 984 | """A Process on the system""" |
| 985 | def __init__(self, pid=None, handle=None, name=None, ppid=None): |
| 986 | if pid is None and handle is None: |
| 987 | raise ValueError("Need at least <pid> or <handle> to create a {0}".format(type(self).__name__)) |
| 988 | |
| 989 | if pid is not None: self._pid = pid |
| 990 | if handle is not None: self._handle = handle |
| 991 | if name is not None: self._name = name |
| 992 | if ppid is not None: self._ppid = ppid |
| 993 | |
| 994 | |
| 995 | @staticmethod |
| 996 | def _from_handle(handle): |
| 997 | #pid = winproxy.GetProcessId(handle) |
| 998 | #proc = [p for p in windows.system.processes if p.pid == pid][0] |
| 999 | #proc._handle = handle |
| 1000 | #dbgprint("Process {0} from handle {1}".format(proc, hex(handle)), "HANDLE") |
| 1001 | return WinProcess(handle=handle) |
| 1002 | |
| 1003 | @classmethod |
| 1004 | def _from_PROCESSENTRY32(cls, entry): |
| 1005 | # Temporary encoded name |
| 1006 | name = entry.szExeFile.encode(errors="backslashreplace") |
| 1007 | pid = entry.th32ProcessID |
| 1008 | ppid = entry.th32ParentProcessID |
| 1009 | return cls(pid=pid, name=name, ppid=ppid) |
| 1010 | |
| 1011 | |
| 1012 | @utils.fixedpropety |
| 1013 | def name(self): |
| 1014 | """Name of the process |
| 1015 | |
| 1016 | :type: :class:`str` |
| 1017 | """ |
| 1018 | buffer = ctypes.c_buffer(0x1024) |
| 1019 | rsize = winproxy.GetProcessImageFileNameA(self.limited_handle, buffer) # Use a syscall and not some remote process reading |
| 1020 | # GetProcessImageFileNameA returns the fullpath |
| 1021 | return buffer[:rsize].decode().split("\\")[-1] |
| 1022 | |
| 1023 | @utils.fixedpropety |
| 1024 | def pid(self): |
| 1025 | """Process ID |
| 1026 | |
| 1027 | :type: :class:`int` |
| 1028 | """ |
| 1029 | return winproxy.GetProcessId(self.handle) |
| 1030 | |
| 1031 | def _get_handle(self): |
| 1032 | return winproxy.OpenProcess(dwProcessId=self.pid) |
| 1033 | |
| 1034 | def __repr__(self): |
| 1035 | try: |
| 1036 | exe_name = self.name |
| 1037 | except WindowsError as e: |
| 1038 | exe_name = "!cannot-retrieve-name" |
| 1039 | try: |
| 1040 | if self.is_exit: |
no outgoing calls
no test coverage detected