-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathmanul_utils.py
More file actions
290 lines (262 loc) · 8.58 KB
/
manul_utils.py
File metadata and controls
290 lines (262 loc) · 8.58 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Manul - utils file
# -------------------------------------
# Maksim Shudrak <mshudrak@salesforce.com> <mxmssh@gmail.com>
#
# Copyright 2019 Salesforce.com, inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import OrderedDict, namedtuple
import os
import printing
import time
import psutil
import signal
import sys
from helper import *
import zlib
import struct
'''
manul useful functions
'''
STATS_FREQUENCY = 1
SHM_SIZE = 65535
IGNORE_ABORT = True
UPDATE = True
MAX_SEED = 1024*1024*1024
PY3 = sys.version_info[0] == 3
if not sys.platform == "win32":
#https://github.com/torvalds/linux/blob/556e2f6020bf90f63c5dd65e9a2254be6db3185b/include/linux/signal.h#L330
critical_signals_nix = {signal.SIGQUIT, signal.SIGILL, signal.SIGTRAP, signal.SIGBUS, signal.SIGFPE, signal.SIGSEGV,
signal.SIGXCPU, signal.SIGXFSZ, signal.SIGSYS} #,signal.SIGABRT
else:
critical_signals_nix = {}
class FuzzerStats:
def __init__(self):
self.stats = OrderedDict()
self.stats["executions"] = 0.0
self.stats["exceptions"] = 0.0
self.stats["new_paths"] = 0.0
self.stats["bitmap_coverage"] = 0.0
self.stats["blacklisted_paths"] = 0.0
self.stats["last_path_time"] = 0.0
self.stats['unique_crashes'] = 0.0
self.stats["crashes"] = 0.0
self.stats["last_crash_time"] = 0.0
self.stats["files_in_queue"] = 0.0
self.stats["file_running"] = 0.0
self.stats["exec_per_sec"] = 0.0
def get_len(self):
return len(self.stats)
if os.name == 'nt':
class bcolors:
HEADER = ''
OKBLUE = ''
OKGREEN = ''
WARNING = ''
GRAY = ''
FAIL = ''
ENDC = ''
BOLD = ''
UNDERLINE = ''
else:
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
GRAY = '\033[30m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def bytes_to_int(byte_str):
if PY3:
return int.from_bytes(byte_str, "little")
else:
return struct.unpack("<L", byte_str)[0]
def parse_config(file_path):
content = open(file_path, 'r').readlines()
additional_cmd = ""
args_dict = dict()
for i, line in enumerate(content):
line = line.replace("\n", "")
if line.startswith("#") or len(line) <= 1: # comments or empty lines
continue
line = line.replace(" ", "")
if line.count("=") != 1:
printing.ERROR("Wrong config format at line %d:%s, exiting" % (i, line))
line = line.split("=") # [arg_name, value]
# parse Bool|Number|String
value = line[1]
if value == 'True':
additional_cmd += "--%s " % (line[0])
elif value == 'None' or value == 'False':
continue
else:
additional_cmd += "--%s %s " % (line[0], value)
additional_cmd = additional_cmd[:-1]
return additional_cmd
def get_list_of_idle_processes(timeout):
ids = list()
pid = os.getpid()
parent = psutil.Process(pid)
children = parent.children(recursive=True)
for p in children:
# enumerating targets without name python in them
child = psutil.Process(p.pid)
if "python" in child.name() or "Python" in child.name() or "sh" == child.name(): # FYI: if target has python name we can't stop it
continue
created_at = child.create_time()
elapsed = time.time() - created_at
if elapsed > timeout:
ids.append(p)
return ids
def is_alive(pid):
if psutil.pid_exists(pid):
# alive but zombie ?
status = None
try:
proc = psutil.Process(pid)
status = proc.status()
except psutil.NoSuchProcess as exc:
return False # already dead
if status and status == psutil.STATUS_ZOMBIE:
printing.WARNING(None, "The process is alive but Zombie, killing it")
kill_all(pid) # avoid Zombies in our environment
return False
return True
else:
return False
def kill_process(p):
try:
if sys.platform == "win32":
p.kill()
else:
p.send_signal(signal.SIGKILL)
except psutil.NoSuchProcess:
pass
def kill_all(pid):
try:
parent = psutil.Process(pid)
children = parent.children(recursive=True)
except psutil.NoSuchProcess as exc:
return
for p in children:
kill_process(p)
kill_process(parent)
def watchdog(timeout): # used only in python2
'''
Watchdog runs infinitely and kills all idle processes
:param timeout:
:param target_name:
:return:
'''
procs = dict()
if sys.platform == "win32":
sig = signal.CTRL_BREAK_EVENT
else:
sig = signal.SIGTERM # FYI: sometimes it might be handled by the target. sigkill can cause FP?
while True:
# getting list of running targets
try:
proc_ids = get_list_of_idle_processes(timeout)
for pid in proc_ids:
target_id = pid.pid
p = psutil.Process(pid.pid)
printing.INFO(0, None, None, "Sending SIGKILL signal to %d %s" % (target_id, pid.name()))
p.send_signal(sig)
except psutil.NoSuchProcess as exc:
pass # already dead
time.sleep(timeout/4)
def extract_content(file_name):
fd = open(file_name, 'rb')
if not fd:
printing.ERROR("Failed to open input file, error code, stopping")
# check file size and return empty string if 0
if not os.path.getsize(file_name):
fd.close()
return b""
content = bytearray(fd.read())
if not content:
printing.ERROR("Unable to read data from the file %s" % file_name)
fd.close()
return content
fd_dict = dict()
def save_content_lin(data, output_file_path):
global fd_dict
fd = fd_dict.get(output_file_path, None)
if not fd:
fd = open(output_file_path, 'wb')
fd_dict[output_file_path] = fd
else:
fd.seek(0, 0)
if not fd:
printing.ERROR("Failed to open output file, aborting")
fd.write(data)
fd.flush()
#fd.close()
return 1
# on Windows, it is better to close the file before we can send it into the target to avoid any sync problems
def save_content_win(data, output_file_path):
fd = open(output_file_path, 'wb')
if not fd:
printing.ERROR("Failed to open output file, aborting")
fd.write(data)
fd.flush()
fd.close() # TODO: find the way to avoid closing it on Windows
return 1
def save_content(data, output_file_path):
if sys.platform == "win32":
save_content_win(data, output_file_path)
else: # #TODO: test it on MacOS
save_content_lin(data, output_file_path)
def is_bytearrays_equal(data1, data2):
if not PY3:
data1 = data1.decode()
data2 = data2.decode()
hash1 = zlib.crc32(data1)
hash2 = zlib.crc32(data2)
if hash1 != hash2:
return False
return True
def locate_diffs(data1, data2, length):
f_loc = -1
l_loc = -1
for i in range(0, length):
if data1[i] != data2[i]:
if f_loc == -1: f_loc = i
l_loc = i
return f_loc, l_loc
# source of this function
#https://stackoverflow.com/questions/18092354/python-split-string-without-splitting-escaped-character
def split_unescape(s, delim, escape='\\', unescape=True):
ret = []
current = []
itr = iter(s)
for ch in itr:
if ch == escape:
try:
# skip the next character; it has been escaped!
if not unescape:
current.append(escape)
current.append(next(itr))
except StopIteration:
if unescape:
current.append(escape)
elif ch == delim:
# split! (add current to the list and reset it)
ret.append(''.join(current))
current = []
else:
current.append(ch)
ret.append(''.join(current))
return ret