forked from danwalkeruk/fortigate2csv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fortigate2csv.py
executable file
·361 lines (315 loc) · 11 KB
/
fortigate2csv.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env python
"""Export policy rules and objects from FortiGate.
author dan walker <[email protected]>
created 2020-11-12
updated 2021-06-10
url github.com/danwalkeruk/fortigate2csv
"""
import argparse
import getpass
import sys
import requests
from netaddr import IPAddress
# disable warnings for insecure connections
import urllib3
import urllib3.exceptions
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# items
item_types = [
"interface",
"policy",
"snat",
"address",
"service",
"dnat",
"pool",
"addrgrp",
]
def main():
"""Build a parser, set arguments, parse the input."""
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--firewall", help="Firewall", required=True)
parser.add_argument("-u", "--user", help="Username", required=True)
parser.add_argument("-v", "--vdom", help="VDOM", required=True)
parser.add_argument("-i", "--item", help="Item", required=True)
parser.add_argument(
"-t",
"--translate",
help="Include translation of IP objects",
action="store_true",
)
parser.add_argument("-o", "--outfile", help="Output file")
args = parser.parse_args()
base_url = f"https://{args.firewall}/"
# holds an address lookup table, if requested
addresses = {}
# check item type
if args.item not in item_types:
print(f'Please choose a valid item type: {", ".join(map(str, item_types))}')
sys.exit(1)
# use a hidden password entry
password = getpass.getpass()
# connect and authenticate
print(f"Connecting to {args.firewall} ({args.vdom}) as {args.user}")
f = f_login(args.firewall, args.user, password, args.vdom)
# build a translation table if required
if args.translate:
print("Building lookup tables")
# add addresses to the local lookup table
data = f.get(f"{base_url}api/v2/cmdb/firewall/address/?vdom={args.vdom}").json()
for address in data["results"]:
if address["type"] == "ipmask":
ip, subnet = address["subnet"].split()
addresses[address["name"]] = f"{ip}/{IPAddress(subnet).netmask_bits()}"
elif address["type"] == "iprange":
addresses[
address["name"]
] = f'{address["start-ip"]}-{address["end-ip"]}'
# add ip pools to the local lookup table
data = f.get(f"{base_url}api/v2/cmdb/firewall/ippool/?vdom={args.vdom}").json()
for address in data["results"]:
if address["startip"] == address["endip"]:
addresses[address["name"]] = address["startip"]
else:
addresses[address["name"]] = f"{address['startip']}-{address['endip']}"
# add vips to the local lookup table
data = f.get(f"{base_url}api/v2/cmdb/firewall/vip/?vdom={args.vdom}").json()
for address in data["results"]:
addresses[address["name"]] = address["extip"]
print("Fetching data...")
# DNAT and VIPs
if args.item == "dnat":
data = f.get(f"{base_url}api/v2/cmdb/firewall/vip/?vdom={args.vdom}").json()
headers = [
"name",
"extip",
"mappedip",
"extintf",
"arp-reply",
"nat-source-vip",
"portforward",
"srcintf-filter",
"comments",
]
# SNAT Mapping
elif args.item == "snat":
data = f.get(
f"{base_url}api/v2/cmdb/firewall/central-snat-map/?vdom={args.vdom}"
).json()
headers = [
"policyid",
"status",
"orig-addr",
"dst-addr",
"srcintf",
"dstintf",
"nat",
"nat-ippool",
"comments",
]
# addresses
elif args.item == "address":
data = f.get(f"{base_url}api/v2/cmdb/firewall/address/?vdom={args.vdom}").json()
headers = [
"name",
"type",
"subnet",
"fqdn",
"associated-interface",
"visibility",
"allow-routing",
"comment",
]
# address groups
elif args.item == "addrgrp":
data = f.get(f"{base_url}api/v2/cmdb/firewall/addrgrp/?vdom={args.vdom}").json()
headers = ["name", "member", "comment", "visibility", "allow-routing"]
# pools
elif args.item == "pool":
data = f.get(f"{base_url}api/v2/cmdb/firewall/ippool/?vdom={args.vdom}").json()
headers = [
"name",
"type",
"startip",
"endip",
"source-startip",
"source-endip",
"block-size",
"permit-any-host",
"arp-reply",
"comments",
]
# services
elif args.item == "service":
data = f.get(
f"{base_url}api/v2/cmdb/firewall.service/custom?vdom={args.vdom}"
).json()
headers = [
"name",
"category",
"protocol",
"tcp-portrange",
"udp-portrange",
"visibility",
"comments",
]
# interfaces
elif args.item == "interface":
data = f.get(
f"{base_url}api/v2/monitor/system/available-interfaces?vdom={args.vdom}"
).json()
headers = [
"name",
"alias",
"description",
"type",
"is_vdom_link",
"is_system_interface",
"is_vlan",
"status",
"role",
"ipv4_addresses",
"vlan_interface",
"vlan_id",
"mac_address",
"visibility",
"comments",
]
# policies
elif args.item == "policy":
data = f.get(f"{base_url}api/v2/cmdb/firewall/policy?vdom={args.vdom}").json()
headers = [
"policyid",
"name",
"srcintf",
"dstintf",
"srcaddr",
"dstaddr",
"internet-service-id",
"internet-service-src-id",
"service",
"action",
"status",
"schedule",
"visibility",
"profile-group",
"nat",
"comments",
]
# logout to prevent stale sessions
print("Logging out of firewall")
f.get(f"https://{args.firewall}/logout", verify=False, timeout=10)
# format the data
if "results" not in data:
print("Firewall returned no results")
sys.exit(1)
if args.translate:
csv_data = build_csv(headers, data["results"], addresses)
else:
csv_data = build_csv(headers, data["results"])
# display or save
if not args.outfile:
# display with some start/end padding
print(f"{'*'*20} start csv {'*'*20}\n{csv_data}\n{'*'*21} end csv {'*'*21}")
else:
# save to designated file
print(f"Saving to {args.outfile}")
with open(args.outfile, "w") as file:
file.write(csv_data)
print("Done!")
def build_csv(headers, rows, address_lookup=None):
"""
Return a formatted CSV, dynamically generated from headers/data.
:param headers: CSV header row (field names)
:param rows: list of data to parse
:return: string of csv data
"""
# create csv output variable + add headers
csv = f"{','.join(map(str, headers))}\n"
# ROWS
for row in rows:
row_data = [] # holds the data for each row before we add to csv
# COLUMNS
for header in headers:
# we only want to extract fields that match our headers
if header in row:
# some fields are lists of dicts
if type(row[header]) == list:
# display a blank instead of '[]' for empty lists
if len(row[header]) == 0:
row_data.append("")
else:
subitems = []
if (
header == "ipv4_addresses"
): # parse list, extract ip mask for each item within the field
for x in row[header]:
subitems.append(f"{x['ip']}/{x['cidr_netmask']}")
else: # parse list, extract q_origin_key for each item within the field
for x in row[header]:
if (
address_lookup
and x["q_origin_key"] in address_lookup
):
subitems.append(address_lookup[x["q_origin_key"]])
else:
subitems.append(x["q_origin_key"])
# join with a space, can't use comma due to csv
row_data.append(" ".join(map(str, subitems)))
else:
# this field is just a string/int, simply add it to the row
if type(row[header]) == str:
row_data.append(row[header].replace(",", ""))
else:
if address_lookup and row[header] in address_lookup:
subitems.append(address_lookup[row[header]])
else:
row_data.append(row[header])
else:
# display blanks where we have no info for this header
row_data.append("")
# append row to csv output
csv += f"{','.join(map(str, row_data))}\n"
return csv
def f_login(host, user, password, vdom):
"""
Return a requests session after authenticating.
:param host: IP/FQDN of firewall
:param user: FortiGate username
:param password: FortiGate user password
:param vdom: FortiGate VDOM
:return: authenticated session or failure
"""
# send the initial authentication request
session = requests.session()
p = session.post(
f"https://{host}/logincheck",
data=f"username={user}&secretkey={password}",
verify=False,
timeout=10,
)
# extract CSRF token from cookies for use in headers
for cookie in session.cookies:
if cookie.name == "ccsrftoken":
print("Received CSRF token")
session.headers.update({"X-CSRFTOKEN": cookie.value[1:-1]})
# if there is a login banner, we need to 'accept' it
if "logindisclaimer" in p.text:
print("Accepting login banner")
session.post(
f"https://{host}/logindisclaimer",
data="confirm=1&redir=/ng",
verify=False,
timeout=10,
)
# check login was successful
try:
login = session.get(f"https://{host}/api/v2/cmdb/system/vdom")
login.raise_for_status()
print(f"Successfully logged in as {user}")
except Exception as e:
print(f"Failed to login with provided credentials: {e}")
sys.exit(1)
return session
if __name__ == "__main__":
main()