1
2 '''
3 Process control agent. This agent is able to start, stop, and monitor
4 if a process is running. If the process exits early a fault will be
5 issued to the fuzzer.
6
7 @author: Michael Eddington
8 @version: $Id: Peach.Agent.process-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $
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 <!-- Start, stop, and monitor a process -->
40 <Monitor class="process.Process">
41
42 <!-- [Optional] Should we restart the process for each test case? -->
43 <!-- Default: False -->
44 <Param name="RestartOnEachTest" value="False" />
45
46 <!-- [Optional] Should we report an early exit of the process as a fault? -->
47 <!-- Default: True -->
48 <Param name="FaultOnEarlyExit" value="True" />
49
50 <!-- Command to start process -->
51 <Param name="Command" value="" />
52
53 </Monitor>
54 '''
55
56 import sys
57 sys.path.append("..")
58 sys.path.append("../..")
59 import os
60
61 try:
62 import win32con
63 from win32process import *
64 except:
65 print "Warning: win32 extensions not found, disabing variouse process monitors."
66
67 from Peach.agent import Monitor
68
69 -class PageHeap(Monitor):
70 '''
71 A monitor that will enable/disable pageheap on an executable.
72 '''
73
74 - def __init__(self, args):
75 '''
76 Params: Path, Executable
77 '''
78
79 try:
80 self._path = args['Path'] + "\\gflags.exe"
81
82 except:
83 self._path = 'c:\\Program Files\\Debugging Tools for Windows\\gflags.exe'
84
85 self._exe = args['Executable']
86
87 self._onParams = [ 'gflags.exe', '/p', '/full', '/enable', self._exe ]
88 self._offParams = [ 'gflags.exe', '/p', '/disable', self._exe ]
89
90 os.spawnv(os.P_WAIT, self._path, self._onParams )
91
92 - def OnShutdown(self):
93 os.spawnv(os.P_WAIT, self._path, self._offParams )
94
95
97 '''
98 Process control agent. This agent is able to start, stop, and monitor
99 if a process is running. If the process exits early a fault will be
100 issued to the fuzzer.
101 '''
102
104 if args.has_key('RestartOnEachTest'):
105 if args['RestartOnEachTest'].lower() == 'true':
106 self.restartOnTest = True
107 else:
108 self.restartOnTest = False
109 else:
110 self.restartOnTest = False
111
112 if args.has_key('FaultOnEarlyExit'):
113 if args['FaultOnEarlyExit'].lower() == 'true':
114 self.faultOnEarlyExit = True
115 else:
116 self.faultOnEarlyExit = False
117 else:
118 self.faultOnEarlyExit = True
119
120 self.strangeExit = False
121 self.command = args["Command"]
122 self.args = None
123 self.pid = None
124 self.hProcess = None
125 self.hThread = None
126 self.dwProcessId = None
127 self.dwThreadId = None
128
130
131 if self.hProcess == None:
132 return
133
134 if self._IsProcessRunning():
135 TerminateProcess(self.hProcess, 0)
136
137 self.hProcess = None
138 self.hThread = None
139 self.dwProcessId = None
140 self.dwThreadId = None
141
143 if self.hProcess != None:
144 self._StopProcess()
145
146 (hProcess, hThread, dwProcessId, dwThreadId) = CreateProcess(None, self.command,
147 None, None, 0, 0, None, None, STARTUPINFO())
148
149 self.hProcess = hProcess
150 self.hThread = hThread
151 self.dwProcessId = dwProcessId
152 self.dwThreadId = dwThreadId
153
155 if self.hProcess == None:
156 return False
157
158 ret = GetExitCodeProcess(self.hProcess)
159 if ret != win32con.STILL_ACTIVE:
160 return False
161
162 ret = GetExitCodeThread(self.hThread)
163 if ret != win32con.STILL_ACTIVE:
164 return False
165
166 return True
167
176
178 '''
179 Called right after a test.
180 '''
181 if not self._IsProcessRunning():
182 self.strangeExit = True
183
184 if self.restartOnTest:
185 self._StopProcess()
186
188 '''
189 Get any monitored data.
190 '''
191 if self.strangeExit:
192 return "Process exited early"
193
194 return None
195
197 '''
198 Check if a fault was detected. If the process exits
199 with out our help we will report it as a fault.
200 '''
201 if self.faultOnEarlyExit:
202 return not self._IsProcessRunning()
203
204 else:
205 return False
206
208 '''
209 Called when a fault was detected.
210 '''
211 self._StopProcess()
212
214 '''
215 Called when Agent is shutting down.
216 '''
217 self._StopProcess()
218
219
220