-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.py
executable file
·108 lines (93 loc) · 3.45 KB
/
util.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import shutil
from Peach.agent import Monitor
class CleanupFolder(Monitor):
"""
This monitor will remove any files created in a folder during a fuzzing
iteration. Create for removing stale temp files, etc.
"""
def __init__(self, args):
"""
Constructor. Arguments are supplied via the Peach XML file.
@type args: Dictionary
@param args: Dictionary of parameters
"""
self._name = None
self._folder = args['Folder'].replace("'''", "")
self._folderListing = os.listdir(self._folder)
def OnTestStarting(self):
"""
Called right after a test case or variation.
"""
listing = os.listdir(self._folder)
for item in listing:
if item not in self._folderListing:
realName = os.path.join(self._folder, item)
print("CleanupFolder: Removing '{}'".format(realName))
try:
os.unlink(realName)
except:
pass
try:
shutil.rmtree(realName)
except:
pass
try:
import win32api, win32con
except:
pass
class CleanupRegistry(Monitor):
"""
This monitor will remove any sub-keys for a specified registry key before
each run. This is useful for removing document recovery keys for fuzzing
Office.
"""
def __init__(self, args):
"""
Constructor. Arguments are supplied via the Peach XML file.
@type args: Dictionary
@param args: Dictionary of parameters
"""
self._name = None
self._key = args['Key'].replace("'''", "")
if self._key.startswith("HKCU\\"):
self._root = win32con.HKEY_CURRENT_USER
elif self._key.startswith("HKCC\\"):
self._root = win32con.HKEY_CURRENT_CONFIG
elif self._key.startswith("HKLM\\"):
self._root = win32con.HKEY_LOCAL_MACHINE
elif self._key.startswith("HKPD\\"):
self._root = win32con.HKEY_PERFORMANCE_DATA
elif self._key.startswith("HKU\\"):
self._root = win32con.HKEY_USERS
else:
print("CleanupRegistry: Error, key must be prefixed with: "
"HKCU, HKCC, HKLM, HKPD, or HKU.")
raise Exception("CleanupRegistry: Error, key must be prefixed "
"with: HKCU, HKCC, HKLM, HKPD, or HKU.")
self._key = self._key[self._key.find("\\") + 1:]
def OnTestStarting(self):
self.deleteKey(self._root, self._key)
def deleteKey(self, hKey, subKey):
"""
Recursively remove registry keys.
"""
try:
hKey = win32api.RegOpenKeyEx(hKey, subKey, 0,
win32con.KEY_ALL_ACCESS)
try:
while True:
s = win32api.RegEnumKey(hKey, 0)
self.deleteKey(hKey, s)
print("CleanupRegistry: Removing sub-key '{}'".format(s))
win32api.RegDeleteKey(hKey, s)
except win32api.error:
pass
finally:
win32api.RegCloseKey(hKey)
except:
print("Warning: Unable to open registry key!")
pass