1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 """
32 Wrapper for psapi.dll in ctypes.
33 """
34
35 __revision__ = "$Id: psapi.py 1299 2013-12-20 09:30:55Z qvasimodo $"
36
37 from defines import *
38
39
40
41 _all = None
42 _all = set(vars().keys())
43
44
45
46
47 LIST_MODULES_DEFAULT = 0x00
48 LIST_MODULES_32BIT = 0x01
49 LIST_MODULES_64BIT = 0x02
50 LIST_MODULES_ALL = 0x03
51
52
53
54
55
56
58 _fields_ = [
59 ("lpBaseOfDll", LPVOID),
60 ("SizeOfImage", DWORD),
61 ("EntryPoint", LPVOID),
62 ]
63 LPMODULEINFO = POINTER(MODULEINFO)
64
65
66
67
68
69
70
71
73 _EnumDeviceDrivers = windll.psapi.EnumDeviceDrivers
74 _EnumDeviceDrivers.argtypes = [LPVOID, DWORD, LPDWORD]
75 _EnumDeviceDrivers.restype = bool
76 _EnumDeviceDrivers.errcheck = RaiseIfZero
77
78 size = 0x1000
79 lpcbNeeded = DWORD(size)
80 unit = sizeof(LPVOID)
81 while 1:
82 lpImageBase = (LPVOID * (size // unit))()
83 _EnumDeviceDrivers(byref(lpImageBase), lpcbNeeded, byref(lpcbNeeded))
84 needed = lpcbNeeded.value
85 if needed <= size:
86 break
87 size = needed
88 return [ lpImageBase[index] for index in xrange(0, (needed // unit)) ]
89
90
91
92
93
94
96 _EnumProcesses = windll.psapi.EnumProcesses
97 _EnumProcesses.argtypes = [LPVOID, DWORD, LPDWORD]
98 _EnumProcesses.restype = bool
99 _EnumProcesses.errcheck = RaiseIfZero
100
101 size = 0x1000
102 cbBytesReturned = DWORD()
103 unit = sizeof(DWORD)
104 while 1:
105 ProcessIds = (DWORD * (size // unit))()
106 cbBytesReturned.value = size
107 _EnumProcesses(byref(ProcessIds), cbBytesReturned, byref(cbBytesReturned))
108 returned = cbBytesReturned.value
109 if returned < size:
110 break
111 size = size + 0x1000
112 ProcessIdList = list()
113 for ProcessId in ProcessIds:
114 if ProcessId is None:
115 break
116 ProcessIdList.append(ProcessId)
117 return ProcessIdList
118
119
120
121
122
123
124
126 _EnumProcessModules = windll.psapi.EnumProcessModules
127 _EnumProcessModules.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD]
128 _EnumProcessModules.restype = bool
129 _EnumProcessModules.errcheck = RaiseIfZero
130
131 size = 0x1000
132 lpcbNeeded = DWORD(size)
133 unit = sizeof(HMODULE)
134 while 1:
135 lphModule = (HMODULE * (size // unit))()
136 _EnumProcessModules(hProcess, byref(lphModule), lpcbNeeded, byref(lpcbNeeded))
137 needed = lpcbNeeded.value
138 if needed <= size:
139 break
140 size = needed
141 return [ lphModule[index] for index in xrange(0, int(needed // unit)) ]
142
143
144
145
146
147
148
149
151 _EnumProcessModulesEx = windll.psapi.EnumProcessModulesEx
152 _EnumProcessModulesEx.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, DWORD]
153 _EnumProcessModulesEx.restype = bool
154 _EnumProcessModulesEx.errcheck = RaiseIfZero
155
156 size = 0x1000
157 lpcbNeeded = DWORD(size)
158 unit = sizeof(HMODULE)
159 while 1:
160 lphModule = (HMODULE * (size // unit))()
161 _EnumProcessModulesEx(hProcess, byref(lphModule), lpcbNeeded, byref(lpcbNeeded), dwFilterFlag)
162 needed = lpcbNeeded.value
163 if needed <= size:
164 break
165 size = needed
166 return [ lphModule[index] for index in xrange(0, (needed // unit)) ]
167
168
169
170
171
172
174 _GetDeviceDriverBaseNameA = windll.psapi.GetDeviceDriverBaseNameA
175 _GetDeviceDriverBaseNameA.argtypes = [LPVOID, LPSTR, DWORD]
176 _GetDeviceDriverBaseNameA.restype = DWORD
177
178 nSize = MAX_PATH
179 while 1:
180 lpBaseName = ctypes.create_string_buffer("", nSize)
181 nCopied = _GetDeviceDriverBaseNameA(ImageBase, lpBaseName, nSize)
182 if nCopied == 0:
183 raise ctypes.WinError()
184 if nCopied < (nSize - 1):
185 break
186 nSize = nSize + MAX_PATH
187 return lpBaseName.value
188
190 _GetDeviceDriverBaseNameW = windll.psapi.GetDeviceDriverBaseNameW
191 _GetDeviceDriverBaseNameW.argtypes = [LPVOID, LPWSTR, DWORD]
192 _GetDeviceDriverBaseNameW.restype = DWORD
193
194 nSize = MAX_PATH
195 while 1:
196 lpBaseName = ctypes.create_unicode_buffer(u"", nSize)
197 nCopied = _GetDeviceDriverBaseNameW(ImageBase, lpBaseName, nSize)
198 if nCopied == 0:
199 raise ctypes.WinError()
200 if nCopied < (nSize - 1):
201 break
202 nSize = nSize + MAX_PATH
203 return lpBaseName.value
204
205 GetDeviceDriverBaseName = GuessStringType(GetDeviceDriverBaseNameA, GetDeviceDriverBaseNameW)
206
207
208
209
210
211
213 _GetDeviceDriverFileNameA = windll.psapi.GetDeviceDriverFileNameA
214 _GetDeviceDriverFileNameA.argtypes = [LPVOID, LPSTR, DWORD]
215 _GetDeviceDriverFileNameA.restype = DWORD
216
217 nSize = MAX_PATH
218 while 1:
219 lpFilename = ctypes.create_string_buffer("", nSize)
220 nCopied = ctypes.windll.psapi.GetDeviceDriverFileNameA(ImageBase, lpFilename, nSize)
221 if nCopied == 0:
222 raise ctypes.WinError()
223 if nCopied < (nSize - 1):
224 break
225 nSize = nSize + MAX_PATH
226 return lpFilename.value
227
229 _GetDeviceDriverFileNameW = windll.psapi.GetDeviceDriverFileNameW
230 _GetDeviceDriverFileNameW.argtypes = [LPVOID, LPWSTR, DWORD]
231 _GetDeviceDriverFileNameW.restype = DWORD
232
233 nSize = MAX_PATH
234 while 1:
235 lpFilename = ctypes.create_unicode_buffer(u"", nSize)
236 nCopied = ctypes.windll.psapi.GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize)
237 if nCopied == 0:
238 raise ctypes.WinError()
239 if nCopied < (nSize - 1):
240 break
241 nSize = nSize + MAX_PATH
242 return lpFilename.value
243
244 GetDeviceDriverFileName = GuessStringType(GetDeviceDriverFileNameA, GetDeviceDriverFileNameW)
245
246
247
248
249
250
251
253 _GetMappedFileNameA = ctypes.windll.psapi.GetMappedFileNameA
254 _GetMappedFileNameA.argtypes = [HANDLE, LPVOID, LPSTR, DWORD]
255 _GetMappedFileNameA.restype = DWORD
256
257 nSize = MAX_PATH
258 while 1:
259 lpFilename = ctypes.create_string_buffer("", nSize)
260 nCopied = _GetMappedFileNameA(hProcess, lpv, lpFilename, nSize)
261 if nCopied == 0:
262 raise ctypes.WinError()
263 if nCopied < (nSize - 1):
264 break
265 nSize = nSize + MAX_PATH
266 return lpFilename.value
267
269 _GetMappedFileNameW = ctypes.windll.psapi.GetMappedFileNameW
270 _GetMappedFileNameW.argtypes = [HANDLE, LPVOID, LPWSTR, DWORD]
271 _GetMappedFileNameW.restype = DWORD
272
273 nSize = MAX_PATH
274 while 1:
275 lpFilename = ctypes.create_unicode_buffer(u"", nSize)
276 nCopied = _GetMappedFileNameW(hProcess, lpv, lpFilename, nSize)
277 if nCopied == 0:
278 raise ctypes.WinError()
279 if nCopied < (nSize - 1):
280 break
281 nSize = nSize + MAX_PATH
282 return lpFilename.value
283
284 GetMappedFileName = GuessStringType(GetMappedFileNameA, GetMappedFileNameW)
285
286
287
288
289
290
291
293 _GetModuleFileNameExA = ctypes.windll.psapi.GetModuleFileNameExA
294 _GetModuleFileNameExA.argtypes = [HANDLE, HMODULE, LPSTR, DWORD]
295 _GetModuleFileNameExA.restype = DWORD
296
297 nSize = MAX_PATH
298 while 1:
299 lpFilename = ctypes.create_string_buffer("", nSize)
300 nCopied = _GetModuleFileNameExA(hProcess, hModule, lpFilename, nSize)
301 if nCopied == 0:
302 raise ctypes.WinError()
303 if nCopied < (nSize - 1):
304 break
305 nSize = nSize + MAX_PATH
306 return lpFilename.value
307
309 _GetModuleFileNameExW = ctypes.windll.psapi.GetModuleFileNameExW
310 _GetModuleFileNameExW.argtypes = [HANDLE, HMODULE, LPWSTR, DWORD]
311 _GetModuleFileNameExW.restype = DWORD
312
313 nSize = MAX_PATH
314 while 1:
315 lpFilename = ctypes.create_unicode_buffer(u"", nSize)
316 nCopied = _GetModuleFileNameExW(hProcess, hModule, lpFilename, nSize)
317 if nCopied == 0:
318 raise ctypes.WinError()
319 if nCopied < (nSize - 1):
320 break
321 nSize = nSize + MAX_PATH
322 return lpFilename.value
323
324 GetModuleFileNameEx = GuessStringType(GetModuleFileNameExA, GetModuleFileNameExW)
325
326
327
328
329
330
331
342
343
344
345
346
347
349 _GetProcessImageFileNameA = windll.psapi.GetProcessImageFileNameA
350 _GetProcessImageFileNameA.argtypes = [HANDLE, LPSTR, DWORD]
351 _GetProcessImageFileNameA.restype = DWORD
352
353 nSize = MAX_PATH
354 while 1:
355 lpFilename = ctypes.create_string_buffer("", nSize)
356 nCopied = _GetProcessImageFileNameA(hProcess, lpFilename, nSize)
357 if nCopied == 0:
358 raise ctypes.WinError()
359 if nCopied < (nSize - 1):
360 break
361 nSize = nSize + MAX_PATH
362 return lpFilename.value
363
365 _GetProcessImageFileNameW = windll.psapi.GetProcessImageFileNameW
366 _GetProcessImageFileNameW.argtypes = [HANDLE, LPWSTR, DWORD]
367 _GetProcessImageFileNameW.restype = DWORD
368
369 nSize = MAX_PATH
370 while 1:
371 lpFilename = ctypes.create_unicode_buffer(u"", nSize)
372 nCopied = _GetProcessImageFileNameW(hProcess, lpFilename, nSize)
373 if nCopied == 0:
374 raise ctypes.WinError()
375 if nCopied < (nSize - 1):
376 break
377 nSize = nSize + MAX_PATH
378 return lpFilename.value
379
380 GetProcessImageFileName = GuessStringType(GetProcessImageFileNameA, GetProcessImageFileNameW)
381
382
383
384 _all = set(vars().keys()).difference(_all)
385 __all__ = [_x for _x in _all if not _x.startswith('_')]
386 __all__.sort()
387
388