-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathprep_hodor.py
71 lines (66 loc) · 3.08 KB
/
prep_hodor.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
#!/usr/bin/python
"""
This is the Hodor module that will prep the files for fuzzing.
Handles delimited text files as well as binary files with formatting info.
Delimiters can be specified by config file and are $$ by default.
"""
import re, string, time
import config_hodor, mutator_hodor, post_hodor
# Takes in text blob, pulls strings delimited by text_delimeter (or not), sends to mutator
# Sends bytearray of mutated output to post_hodor.handler() for further processing
# tlock is set to false by things that aren't utilizing threading. plock is always used
def parse_text(filetext, ignore_tokens, plock, tlock):
if ignore_tokens:
filetext = [filetext] # mutate expects a list
mutated_text = mutator_hodor.mutate(filetext)[0]
else:
delim = config_hodor.text_delimiter
regexp = "%s([\s\S]*?)%s" % (delim, delim)
tokens = re.findall(regexp, filetext)
toklocs = [m.start() for m in re.finditer(regexp, filetext)]
mutated_tokens = mutator_hodor.mutate(tokens)
# Replace original input with mutated output
for idx, val in enumerate(tokens):
filetext = filetext[:2+toklocs[idx]] + mutated_tokens[idx] + filetext[toklocs[idx]+2+len(mutated_tokens[idx]):]
mutated_text = bytearray(string.replace(filetext, delim, ""))
post_hodor.handler(mutated_text, plock, tlock)
return
# Takes in a binary blob, pulls fields specified in bin_fields (or not), sends to mutator
# Sends bytearray of mutated output to post_hodor.handler() for further processing
def parse_bin(filebytes, ignore_fields, plock, tlock):
if ignore_fields:
filebytes = [filebytes]
mutated_bytes = mutator_hodor.mutate(filebytes)[0]
else:
tokens = []
for fields in config_hodor.bin_fields:
tokens.append(filebytes[fields[0]:fields[1]])
mutated_tokens = mutator_hodor.mutate(tokens)
mutated_bytes = bytearray(filebytes)
for idx, val in enumerate(config_hodor.bin_fields):
mutated_bytes[val[0]:val[1]] = mutated_tokens[idx]
mutated_bytes = bytearray(mutated_bytes)
post_hodor.handler(mutated_bytes, plock, tlock)
return
# qpq mode requires some different stuff
def qpq_text(filetext, ignore_tokens, plock, tlock):
if ignore_tokens:
filetext = [filetext] # mutate expects a list
mutated_text = mutator_hodor.qpq(filetext)
mutated_text = bytearray(mutated_text[0][0])
post_hodor.handler(mutated_text, plock, tlock)
else:
delim = config_hodor.text_delimiter
regexp = delim + "([\s\S]*?)" + delim
tokens = re.findall(regexp, filetext)
toklocs = [m.start() for m in re.finditer(regexp, filetext)]
mutated_tokens = mutator_hodor.qpq(tokens)
# Replace original input with mutated output
# qpq returns a list of lists of new tokens for each delimmed token
for idx, val in enumerate(tokens):
for newtok in mutated_tokens[idx]:
mutated_text = filetext[:2+toklocs[idx]] + newtok + filetext[toklocs[idx]+2+len(val):]
mutated_text = bytearray(string.replace(mutated_text, delim, ""))
if config_hodor.execdelay != 0: time.sleep(config_hodor.execdelay)
post_hodor.handler(mutated_text, plock, tlock)
return