-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfuzz_route_refresh.py
224 lines (199 loc) · 12.1 KB
/
fuzz_route_refresh.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
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
import argparse
from boofuzz import *
from boofuzz import helpers
from boofuzz import constants
from base.bgp_route_refresh import BaseRouteRefreshFuzzer
'''
BGP ROUTE REFRESH #1
- The BGP header length is correct
- The "AFI" field is fuzzable
- The "Subtype" field is set to supported values, as per RFC 7313 (plus, one "unknown" value 0x03)
- The "SAFI' field is fuzzable
'''
class BgpRouteRefreshFuzzer_1(BaseRouteRefreshFuzzer):
def __init__(self, bgp_id, asn_id, rhost, rpc_port=1234, rport='179', hold_time=240):
super().__init__(bgp_id, asn_id, rhost, rpc_port, rport, hold_time)
self.poc_name = 'BgpRouteRefreshFuzzer_1_testcase_%s.py'
def do_fuzz(self):
PARAM_ASN_ID = self.asn_id
PARAM_BGP_ID = self.bgp_id
PARAM_HOLD_TIME = self.hold_time
s_initialize("bgp_open")
if s_block_start("BGP"):
if s_block_start("Header"):
s_bytes(value=b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", padding=b"\xFF", size=16, name="Marker", fuzzable=False)
s_size(block_name="Open", length=2, math=lambda x: x + 19, name="Length", endian=BIG_ENDIAN, fuzzable=False) # keep right length
s_byte(value=0x01, endian=BIG_ENDIAN, name="Type", fuzzable=False)
s_block_end()
if s_block_start("Open"):
s_byte(value=0x04, endian=BIG_ENDIAN, name="version", fuzzable=False)
s_word(value=PARAM_ASN_ID, endian=BIG_ENDIAN, name="ASN", fuzzable=False)
s_word(value=PARAM_HOLD_TIME, endian=BIG_ENDIAN, name="Hold Time", fuzzable=False)
s_dword(value=helpers.ip_str_to_bytes(PARAM_BGP_ID), endian=BIG_ENDIAN, name="BGP Identifier", fuzzable=False)
s_bytes(value=b"\x42\x02\x06\x01\x04\x00\x01\x00\x01\x02\x02\x80\x00\x02\x02\x02\x00\x02\x02\x46\x00\x02\x06\x41\x04\x00\x00" + PARAM_ASN_ID.to_bytes(2, 'big') + b"\x02\x02\x06\x00\x02\x06\x45\x04\x00\x01\x01\x01\x02\x07\x49\x05\x03\x62\x6f\x78\x00\x02\x04\x40\x02\x40\x78\x02\x09\x47\x07\x00\x01\x01\x80\x00\x00\x00", fuzzable=False)
s_block_end()
s_block_end()
s_initialize("bgp_keepalive")
if s_block_start("BGP"):
if s_block_start("Header"):
s_bytes(value=b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", padding=b"\xFF", size=16, name="Marker", fuzzable=False)
s_size(block_name="Keepalive", length=2, math=lambda x: x + 19, name="Length", endian=BIG_ENDIAN, fuzzable=False)
s_byte(value=0x04, endian=BIG_ENDIAN, name="Type", fuzzable=False)
s_block_end()
if s_block_start("Keepalive"):
pass
s_block_end()
s_block_end()
s_initialize("bgp_refresh")
if s_block_start("BGP"):
if s_block_start("Header"):
s_bytes(value=b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", padding=b"\xFF", size=16, name="Marker", fuzzable=False)
s_size(block_name="Refresh", length=2, math=lambda x: x + 19, name="Length", endian=BIG_ENDIAN, fuzzable=False) # keep right length
s_byte(value=0x05, endian=BIG_ENDIAN, name="Type", fuzzable=False)
s_block_end()
if s_block_start("Refresh"):
s_word(value=0x0, endian=BIG_ENDIAN, name="afi", fuzzable=True)
s_group(values=[b'\x00', b'\x01', b'\x02', b'\x03', b'\xff'], name='subtype')
s_byte(value=0x0, endian=BIG_ENDIAN, name="safi", fuzzable=True)
s_block_end()
s_block_end()
self.session_handle.connect(s_get("bgp_open"))
self.session_handle.connect(s_get("bgp_open"),s_get("bgp_keepalive"))
self.session_handle.connect(s_get("bgp_keepalive"), s_get("bgp_refresh"))
self.session_handle.fuzz()
'''
BGP ROUTE REFRESH #2
- The BGP header length is correct
- The message body is a random fuzzload
'''
class BgpRouteRefreshFuzzer_2(BaseRouteRefreshFuzzer):
def __init__(self, bgp_id, asn_id, rhost, rpc_port=1234, rport='179', hold_time=240):
super().__init__(bgp_id, asn_id, rhost, rpc_port, rport, hold_time)
self.poc_name = 'BgpRouteRefreshFuzzer_2_testcase_%s.py'
def do_fuzz(self):
PARAM_ASN_ID = self.asn_id
PARAM_BGP_ID = self.bgp_id
PARAM_HOLD_TIME = self.hold_time
s_initialize("bgp_open")
if s_block_start("BGP"):
if s_block_start("Header"):
s_bytes(value=b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", padding=b"\xFF", size=16, name="Marker", fuzzable=False)
s_size(block_name="Open", length=2, math=lambda x: x + 19, name="Length", endian=BIG_ENDIAN, fuzzable=False) # keep right length
s_byte(value=0x01, endian=BIG_ENDIAN, name="Type", fuzzable=False)
s_block_end()
if s_block_start("Open"):
s_byte(value=0x04, endian=BIG_ENDIAN, name="version", fuzzable=False)
s_word(value=PARAM_ASN_ID, endian=BIG_ENDIAN, name="ASN", fuzzable=False)
s_word(value=PARAM_HOLD_TIME, endian=BIG_ENDIAN, name="Hold Time", fuzzable=False)
s_dword(value=helpers.ip_str_to_bytes(PARAM_BGP_ID), endian=BIG_ENDIAN, name="BGP Identifier", fuzzable=False)
s_bytes(value=b"\x42\x02\x06\x01\x04\x00\x01\x00\x01\x02\x02\x80\x00\x02\x02\x02\x00\x02\x02\x46\x00\x02\x06\x41\x04\x00\x00" + PARAM_ASN_ID.to_bytes(2, 'big') + b"\x02\x02\x06\x00\x02\x06\x45\x04\x00\x01\x01\x01\x02\x07\x49\x05\x03\x62\x6f\x78\x00\x02\x04\x40\x02\x40\x78\x02\x09\x47\x07\x00\x01\x01\x80\x00\x00\x00", fuzzable=False)
s_block_end()
s_block_end()
s_initialize("bgp_keepalive")
if s_block_start("BGP"):
if s_block_start("Header"):
s_bytes(value=b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", padding=b"\xFF", size=16, name="Marker", fuzzable=False)
s_size(block_name="Keepalive", length=2, math=lambda x: x + 19, name="Length", endian=BIG_ENDIAN, fuzzable=False)
s_byte(value=0x04, endian=BIG_ENDIAN, name="Type", fuzzable=False)
s_block_end()
if s_block_start("Keepalive"):
pass
s_block_end()
s_block_end()
s_initialize("bgp_refresh")
if s_block_start("BGP"):
if s_block_start("Header"):
s_bytes(value=b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", padding=b"\xFF", size=16, name="Marker", fuzzable=False)
s_size(block_name="Refresh", length=2, math=lambda x: x + 19, name="Length", endian=BIG_ENDIAN, fuzzable=False) # keep right length
s_byte(value=0x05, endian=BIG_ENDIAN, name="Type", fuzzable=False)
s_block_end()
if s_block_start("Refresh"):
s_random(num_mutations=1024, min_length=0, max_length=1024, fuzzable=True)
s_block_end()
s_block_end()
self.session_handle.connect(s_get("bgp_open"))
self.session_handle.connect(s_get("bgp_open"),s_get("bgp_keepalive"))
self.session_handle.connect(s_get("bgp_keepalive"), s_get("bgp_refresh"))
self.session_handle.fuzz()
'''
BGP ROUTE REFRESH #3
- The BGP header length is correct
- This one is to test specific stuff in the FRRouting project
'''
class BgpRouteRefreshFuzzer_3(BaseRouteRefreshFuzzer):
def __init__(self, bgp_id, asn_id, rhost, rpc_port=1234, rport='179', hold_time=240):
super().__init__(bgp_id, asn_id, rhost, rpc_port, rport, hold_time)
self.poc_name = 'BgpRouteRefreshFuzzer_3_testcase_%s.py'
def do_fuzz(self):
PARAM_ASN_ID = self.asn_id
PARAM_BGP_ID = self.bgp_id
PARAM_HOLD_TIME = self.hold_time
s_initialize("bgp_open")
if s_block_start("BGP"):
if s_block_start("Header"):
s_bytes(value=b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", padding=b"\xFF", size=16, name="Marker", fuzzable=False)
s_size(block_name="Open", length=2, math=lambda x: x + 19, name="Length", endian=BIG_ENDIAN, fuzzable=False) # keep right length
s_byte(value=0x01, endian=BIG_ENDIAN, name="Type", fuzzable=False)
s_block_end()
if s_block_start("Open"):
s_byte(value=0x04, endian=BIG_ENDIAN, name="version", fuzzable=False)
s_word(value=PARAM_ASN_ID, endian=BIG_ENDIAN, name="ASN", fuzzable=False)
s_word(value=PARAM_HOLD_TIME, endian=BIG_ENDIAN, name="Hold Time", fuzzable=False)
s_dword(value=helpers.ip_str_to_bytes(PARAM_BGP_ID), endian=BIG_ENDIAN, name="BGP Identifier", fuzzable=False)
s_bytes(value=b"\x42\x02\x06\x01\x04\x00\x01\x00\x01\x02\x02\x80\x00\x02\x02\x02\x00\x02\x02\x46\x00\x02\x06\x41\x04\x00\x00" + PARAM_ASN_ID.to_bytes(2, 'big') + b"\x02\x02\x06\x00\x02\x06\x45\x04\x00\x01\x01\x01\x02\x07\x49\x05\x03\x62\x6f\x78\x00\x02\x04\x40\x02\x40\x78\x02\x09\x47\x07\x00\x01\x01\x80\x00\x00\x00", fuzzable=False)
s_block_end()
s_block_end()
s_initialize("bgp_keepalive")
if s_block_start("BGP"):
if s_block_start("Header"):
s_bytes(value=b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", padding=b"\xFF", size=16, name="Marker", fuzzable=False)
s_size(block_name="Keepalive", length=2, math=lambda x: x + 19, name="Length", endian=BIG_ENDIAN, fuzzable=False)
s_byte(value=0x04, endian=BIG_ENDIAN, name="Type", fuzzable=False)
s_block_end()
if s_block_start("Keepalive"):
pass
s_block_end()
s_block_end()
s_initialize("bgp_refresh")
with s_block("BGP"):
with s_block("Header"):
s_bytes(value=b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", padding=b"\xFF", size=16, name="Marker", fuzzable=False)
s_size(block_name="Refresh", length=2, math=lambda x: x + 19, name="Length", endian=BIG_ENDIAN, fuzzable=False) # keep right length
s_byte(value=0x05, endian=BIG_ENDIAN, name="Type", fuzzable=False)
with s_block("Refresh"):
s_word(value=0x1, endian=BIG_ENDIAN, name="afi", fuzzable=False)
s_byte(value=0x0, endian=BIG_ENDIAN, name="type", fuzzable=False)
s_byte(value=0x1, endian=BIG_ENDIAN, name="safi", fuzzable=False)
with s_block('FUZZLOAD'):
s_byte(name='when_to_refresh', value=0, fuzzable=False)
s_byte(name='orf_type', value=64, fuzzable=False)
s_word(name='orf_len', endian=BIG_ENDIAN, value=7, fuzzable=False)
s_random(num_mutations=1024, min_length=13, max_length=13, fuzzable=True)
self.session_handle.connect(s_get("bgp_open"))
self.session_handle.connect(s_get("bgp_open"),s_get("bgp_keepalive"))
self.session_handle.connect(s_get("bgp_keepalive"), s_get("bgp_refresh"))
self.session_handle.fuzz()
'''
Modify this code to choose different test suites and parameters.
'''
if __name__ == '__main__':
'''
Set the parameters
'''
argparser = argparse.ArgumentParser()
argparser.add_argument('--fbgp_id', dest='fbgp_id', type=str, required=True, help='Fuzzer BGP ID.')
argparser.add_argument('--fasn', dest='fasn', type=int, required=True, default=2, help='Fuzzer ASN number.')
argparser.add_argument('--tip', dest='tip', type=str, required=True, help='Target IP address.')
argparser.add_argument('--trpc_port', dest='trpc_port', type=int, required=True, default=1234, help='Target RPC port.')
args = argparser.parse_args()
FBGP_ID = args.fbgp_id
FASN = args.fasn
TIP = args.tip
TRPC_PORT = args.trpc_port
'''
Instantiate and run a test suite
'''
fuzzer = BgpRouteRefreshFuzzer_1(bgp_id=FBGP_ID, asn_id=FASN, rhost=TIP, rpc_port=TRPC_PORT)
#fuzzer = BgpRouteRefreshFuzzer_2(bgp_id=FBGP_ID, asn_id=FASN, rhost=TIP, rpc_port=TRPC_PORT)
#fuzzer = BgpRouteRefreshFuzzer_3(bgp_id=FBGP_ID, asn_id=FASN, rhost=TIP, rpc_port=TRPC_PORT)
fuzzer.do_fuzz()