-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRachel.py
172 lines (152 loc) · 5.55 KB
/
Rachel.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
# pip install fuzzingbook
# pip install rospy
import sys
import getopt
import rospy
from std_msgs.msg import String
from fuzzingbook.Fuzzer import RandomFuzzer
from fuzzingbook.Grammars import is_valid_grammar, simple_grammar_fuzzer
from fuzzingbook.MutationFuzzer import MutationFuzzer
from grammar import get_grammar
def get_topics():
rospy.init_node('fuzzer', anonymous=True)
topiclist_raw = rospy.get_published_topics()
if len(topiclist_raw) != 0:
topiclist=[]
for i in topiclist_raw:
topiclist.append(i[0][1:])
choice = get_choice(topiclist, "Please choose a topic in the list:\n")
return topiclist[choice]
else:
return "notopic"
def get_choice(messages, probe):
ok = False
print("Choose your destiny:")
for i in range(len(messages)):
print(str(i + 1)+" : "+str(messages[i]))
while not ok:
try:
choice = int(input(probe))
except Exception as e:
print(e)
if (choice < 1 or choice > 10):
print("Please use a suitable input and stop fuzzing arround")
else:
ok = True
return choice - 1
def get_seed(msg):
global topic
global rate
global mutations
global seed_candidates
global subscribed
global asked
if not asked:
if len(seed_candidates) < 10:
seed_candidates.append(msg.data)
else:
choice = get_choice(seed_candidates, "Please choose a suitable input to use as fuzzing base:\n")
asked = True
seed = seed_candidates[choice]
fuzz = mutate_fuzz([seed], mutations)
fuzzer(topic, rate, fuzz, "Mutation (with files)")
subscribed.unregister()
def random_fuzz():
random_fuzzer = RandomFuzzer()
return random_fuzzer.fuzz()
def mutate_fuzz(seed_input, mutations):
mutation_list = []
#coverage
for i in range(mutations):
mutation_fuzzer = MutationFuzzer(seed=seed_input)
mutation_list.append([mutation_fuzzer.fuzz() for j in range(2)][1])
return mutation_list
def generate_fuzz(population):
GRAMMAR = get_grammar()
assert is_valid_grammar(GRAMMAR)
return [simple_grammar_fuzzer(GRAMMAR) for i in range(population)]
def fuzzer(topic, rate, input, type):
print({"Starting to fuzz " + topic + " using " + type})
pub = rospy.Publisher(topic, String, queue_size=1)
rospy.init_node('fuzzer', anonymous=True)
rosrate = rospy.Rate(rate)
while not rospy.is_shutdown():
try:
if type == "random":
pub.publish(input)
print("sending:" + input)
rosrate.sleep()
else:
for i in range(len(input)-1):
pub.publish(input[i])
print("sending:" + input[i])
rosrate.sleep()
except rospy.ROSInterruptException:
pass
return
def main(argv):
global topic
global rate
global mutations
global subscribed
help = (
'Usage: rachel.py -t <topic> -r <rate> [options...]\n'
'-a, --random Random fuzzer\n'
'-m, --mutation <seed> Mutation fuzzer\n'
'-f, --mutation_file Mutation fuzzer using a ros subscription to topic\n'
'-s, --size <population_size> For mutation fuzzer, the number of mutations\n'
'-g, --grammar <population_size> Grammar based fuzzer imported from grammar.py\n'
''
'Hint: if you want to list the available topics just mention Rachel as topic'
)
try:
opts, args = getopt.getopt(argv, "ht:r:am:fs:g:",
["help", "random", "mutation=", "mutation_file", "size=", "grammar="])
except getopt.GetoptError:
print(help)
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', "--help"):
print(help)
sys.exit()
elif opt == '-t':
topic = arg
if topic == "Rachel":
topic = get_topics()
elif opt == '-r':
rate = float(arg)
elif opt in ("-a", "--random"):
type = "random"
fuzz = random_fuzz()
elif opt in ("-m", "--mutation"):
seed_input = str(arg)
for optm, argm in opts:
if optm in ("-s", "--size"):
mutations = int(argm)
type = "mutation"
fuzz = mutate_fuzz([seed_input], mutations)
elif opt in ("-f", "--mutation_file"):
for optm, argm in opts:
if optm in ("-s", "--size"):
mutations = int(argm)
type = "mutation (with file)"
elif opt in ("-g", "--grammar"):
population_size = int(arg)
type = "grammar"
fuzz = generate_fuzz(population_size)
if type != "mutation (with file)":
fuzzer(topic, rate, fuzz, type)
else:
print("================Collecting messages from "+topic+"================")
rospy.init_node('fuzzer', anonymous=True)
subscribed = rospy.Subscriber(topic, String, get_seed, queue_size = 1)
rosrate = rospy.Rate(rate)
while not rospy.is_shutdown():
rosrate.sleep
if __name__ == '__main__':
topic = ""
rate = ""
mutations = ""
seed_candidates = []
asked = False
main(sys.argv[1:])