forked from yiwiz-sai/pyInspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_op.py
200 lines (175 loc) · 7.44 KB
/
process_op.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# -*- coding: utf-8 -*
# author: SAI
import os,sys,time
import traceback
import pykd
from common import *
mmhighestuseraddress=pykd.ptrPtr(pykd.getOffset('nt!MmHighestUserAddress'))
class ProcessInfo(object):
def init(self, eprocessobj):
try:
if eprocessobj.ObjectTable<mmhighestuseraddress or eprocessobj.VadRoot<mmhighestuseraddress or eprocessobj.QuotaBlock<mmhighestuseraddress:
return False
self.eprocessaddr=int(eprocessobj)
self.pid=int(eprocessobj.UniqueProcessId)
self.parentpid=int(eprocessobj.InheritedFromUniqueProcessId)
self.peb=int(eprocessobj.Peb)
filepath=pykd.loadUnicodeString(eprocessobj.SeAuditProcessCreationInfo.ImageFileName.Name)
filepath=revise_filepath(filepath)
name=pykd.loadChars(eprocessobj.ImageFileName, 16)
if name.startswith('\x00'):
name=''
name=name.strip('\x00')
self.filepath, self.name=guess_filepath(filepath, name)
return True
except Exception, err:
print traceback.format_exc()
return False
def listProcessByPspcidTable():
processlist=[]
try:
cmdline='!process 0 0'
r=pykd.dbgCommand(cmdline)
r=r.splitlines()
for i in r:
if i.startswith('PROCESS '):
startpos=len('PROCESS ')
endpos=i.find(' ', startpos)
eprocessaddr=int(i[startpos:endpos], 16)
eprocessobj=pykd.typedVar('nt!_EPROCESS', eprocessaddr)
info=ProcessInfo()
if info.init(eprocessobj):
processlist.append(info)
except Exception, err:
print traceback.format_exc()
return processlist
def listProcessBySessionProcessLinks(sourceprocesslist=[]):
processlist={}
try:
if not sourceprocesslist:
sourceprocesslist=listProcessByPsActiveProcessHead()
SessionProcessLinks_table=[]
for eproc in sourceprocesslist:
eprocessaddr=eproc.eprocessaddr
if eprocessaddr not in processlist:
#print hex(eprocessaddr)
eprocessobj=pykd.typedVar('nt!_EPROCESS', eprocessaddr)
SessionProcessLinks=eprocessobj.SessionProcessLinks
SessionProcessLinks=int(SessionProcessLinks)
if SessionProcessLinks and SessionProcessLinks not in SessionProcessLinks_table:
SessionProcessLinks_table.append(SessionProcessLinks)
l=pykd.typedVarList(SessionProcessLinks, 'nt!_EPROCESS', 'SessionProcessLinks')
for i in l:
if int(i) not in processlist:
info=ProcessInfo()
if info.init(i):
processlist[int(i)]=info
except Exception, err:
print traceback.format_exc()
return processlist.values()
def listProcessByWorkingSetExpansionLinks(sourceprocesslist=[]):
processlist={}
try:
if not sourceprocesslist:
sourceprocesslist=listProcessByPsActiveProcessHead()
WorkingSetExpansionLinks_list=[]
for eproc in sourceprocesslist:
eprocessaddr=eproc.eprocessaddr
if eprocessaddr not in processlist:
#print hex(eprocessaddr)
eprocessobj=pykd.typedVar('nt!_EPROCESS', eprocessaddr)
WorkingSetExpansionLinks=eprocessobj.Vm.WorkingSetExpansionLinks
WorkingSetExpansionLinks=int(WorkingSetExpansionLinks)
if WorkingSetExpansionLinks and WorkingSetExpansionLinks not in WorkingSetExpansionLinks_list:
WorkingSetExpansionLinks_list.append(WorkingSetExpansionLinks)
l=pykd.typedVarList(WorkingSetExpansionLinks, 'nt!_EPROCESS', 'Vm.WorkingSetExpansionLinks')
for i in l:
if int(i) not in processlist:
info=ProcessInfo()
if info.init(i):
processlist[int(i)]=info
except Exception, err:
print traceback.format_exc()
return processlist.values()
def listProcessByPsActiveProcessHead():
processlist=[]
try:
PsActiveProcessHead=pykd.getOffset('nt!PsActiveProcessHead')
l=pykd.typedVarList(PsActiveProcessHead, 'nt!_EPROCESS', 'ActiveProcessLinks')
for i in l:
info=ProcessInfo()
if info.init(i):
processlist.append(info)
except Exception, err:
print traceback.format_exc()
return processlist
def findProcessObject(name):
name=name.lower()
l=listProcessByPsActiveProcessHead()
for i in l:
if i.name==name:
return i
return None
import win32process
import copy
def inspectHiddenProcess():
try:
pidlist=list(win32process.EnumProcesses())
funclist=\
[
listProcessByPsActiveProcessHead,
listProcessBySessionProcessLinks,
listProcessByWorkingSetExpansionLinks,
listProcessByPspcidTable,
]
print 'eprocess pid ppid peb name filepath'
for func in funclist:
print '-'*10,'find hidden process by %s' % func.func_name,'-'*10
processlist=func()
#print len(processlist)
pidlist2=copy.deepcopy(pidlist)
for i in processlist:
if i.pid not in pidlist2:
print '%x %5d %5d %x %s %s' % (i.eprocessaddr, i.pid, i.parentpid, i.peb, i.name, i.filepath)
else:
pidlist2.remove(i.pid)
for i in pidlist2:
print "pid %d can't be found by %s" % (i, func.func_name)
print
print 'inspect completely'
except Exception, err:
print traceback.format_exc()
def help():
print '-inspect'
print '-list0 #listProcessByPsActiveProcessHead'
print '-list1 #listProcessBySessionProcessLinks'
print '-list2 #listProcessByWorkingSetExpansionLinks'
print '-list3 #listProcessByPspcidTable'
if __name__=='__main__':
try:
if len(sys.argv)<2:
help()
sys.exit(0)
if sys.argv[1]=='-inspect':
inspectHiddenProcess()
elif sys.argv[1]=='-list0':
processlist=listProcessByPsActiveProcessHead()
for i in processlist:
print '%x %5d %5d %x %s %s' % (i.eprocessaddr, i.pid, i.parentpid, i.peb, i.name, i.filepath)
elif sys.argv[1]=='-list1':
processlist=listProcessBySessionProcessLinks()
for i in processlist:
print '%x %5d %5d %x %s %s' % (i.eprocessaddr, i.pid, i.parentpid, i.peb, i.name, i.filepath)
elif sys.argv[1]=='-list2':
processlist=listProcessByWorkingSetExpansionLinks()
for i in processlist:
print '%x %5d %5d %x %s %s' % (i.eprocessaddr, i.pid, i.parentpid, i.peb, i.name, i.filepath)
elif sys.argv[1]=='-list3':
processlist=listProcessByPspcidTable()
for i in processlist:
print '%x %5d %5d %x %s %s' % (i.eprocessaddr, i.pid, i.parentpid, i.peb, i.name, i.filepath)
else:
help()
except Exception, err:
print traceback.format_exc()