-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptables2fmc_v2.py
187 lines (146 loc) · 6.92 KB
/
iptables2fmc_v2.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
import requests
import base64
import json
import xmltodict
# FMC settings (Test LAB)
host = "10.48.26.176:40443"
username = "apiscript"
password = "Cisco!123"
autodeploy = False
timeout=300
def encodeBasicAuth(username, password):
'''Encoded basic authentication into username:password format in base64.'''
return base64.b64encode(f'{username}:{password}'.encode('ascii')).decode('utf-8')
def getTokens():
'''Returns auth tokens (access and refresh) as well as DomainUUID.'''
url = f"https://{host}/api/fmc_platform/v1/auth/generatetoken"
payload={}
headers = {
'Authorization': f'Basic {encodeBasicAuth(username, password)}'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=False)
return {'X-auth-access-token': response.headers['X-auth-access-token'], 'X-auth-refresh-token': response.headers['X-auth-refresh-token'], 'DOMAIN_UUID': response.headers['DOMAIN_UUID']}
def getDeviceRecords(authAccessToken, authRefreshToken, domainUUID):
url = f"https://{host}/api/fmc_config/v1/domain/{domainUUID}/devices/devicerecords"
payload={}
headers = {
'X-auth-access-token': authAccessToken,
'X-auth-refresh-token': authRefreshToken,
'Authorization': f'Basic {encodeBasicAuth(username, password)}'
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
print(response.text)
def createACP(authAccessToken, authRefreshToken, domainUUID, type, name, description, defaultAction):
url = f"https://{host}/api/fmc_config/v1/domain/{domainUUID}/policy/accesspolicies"
payload = {
"type": type,
"name": name,
"description": description,
"defaultAction": {
"action": defaultAction
}
}
headers = {
'X-auth-access-token': authAccessToken,
'X-auth-refresh-token': authRefreshToken,
'Authorization': f'Basic {encodeBasicAuth(username, password)}'
}
response = requests.request("POST", url, headers=headers, json=payload, verify=False)
return json.loads(response.text)['id']
def xml2json():
'''Converts iptables xml into a dictionary and saves it as a json file.'''
with open("iptables_xml.txt") as f:
input = xmltodict.parse(f.read())
rules_as_dict = json.loads(json.dumps(input))
# save json to a file -> this is a little bit inefficient as it could've been done above
with open("iptables_json.txt", "w") as out:
json.dump(rules_as_dict, out, indent=2)
return rules_as_dict
def parseRule(rule):
if 'conditions' in rule.keys():
# -- MATCH --
if 'match' in rule['conditions'].keys():
# Source
if 's' in rule['conditions']['match'].keys():
print('Source: ', rule['conditions']['match']['s'])
# Destination
if 'd' in rule['conditions']['match'].keys():
print('Destination: ', rule['conditions']['match']['d'])
# Protocol
if 'p' in rule['conditions']['match'].keys():
print('Protocol: ', rule['conditions']['match']['p'])
# Src. Port
if 'sport' in rule['conditions']['match'].keys():
print('Src. Port: ', rule['conditions']['match']['sport'])
# Dest. Port
if 'dport' in rule['conditions']['match'].keys():
print('Dest. Port: ', rule['conditions']['match']['dport'])
# SET
if 'set' in rule['conditions'].keys():
# if there's multiple sets
if isinstance(rule['conditions']['set'], list):
for set in rule['conditions']['set']:
if 'match-set' in set.keys():
# some are bugged so check if there's a #text tag under match-set
if isinstance(set['match-set'], dict):
print('Set: ', set['match-set']['#text'])
else:
print('Set: ', set['match-set'])
# Source
if 's' in set.keys():
print('Source: ', set['s'])
# Destination
if 'd' in set.keys():
print('Destination: ', set['s'])
# Protocol
if 'p' in set.keys():
print('Protocol: ', set['p'])
else:
if 'match-set' in rule['conditions']['set'].keys():
# some are bugged so check if there's a #text tag under match-set
if isinstance(rule['conditions']['set']['match-set'], dict):
print('Set: ', rule['conditions']['set']['match-set']['#text'])
else:
print('Set: ', rule['conditions']['set']['match-set'])
# Source
if 's' in rule['conditions']['set'].keys():
print('Source: ', rule['conditions']['set']['s'])
# Destination
if 'd' in rule['conditions']['set'].keys():
print('Destination: ', rule['conditions']['set']['d'])
# Protocol
if 'p' in rule['conditions']['set'].keys():
print('Protocol: ', rule['conditions']['set']['p'])
# MULTIPORT
if 'multiport' in rule['conditions']:
# Protocol
if 'p' in rule['conditions']['multiport']:
print('Protocol: ', rule['conditions']['multiport']['p'])
# Dest. Ports
if 'dports' in rule['conditions']['multiport']:
print('Dest. ports: ', rule['conditions']['multiport']['dports'])
# COMMENT
if 'comment' in rule['conditions']:
print('Comment: ', rule['conditions']['comment']['comment'])
# -- ACTIONS --
if rule['actions']:
# accept / allow
if 'ACCEPT' in rule['actions']:
print('Action: Allow')
# drop / block
if 'DROP' in rule['actions']:
print('Action: Block')
if __name__ == "__main__":
#tokens = getTokens()
#getDeviceRecords(tokens['X-auth-access-token'], tokens['X-auth-refresh-token'], tokens['DOMAIN_UUID'])
#containerUUID = createACP(tokens['X-auth-access-token'], tokens['X-auth-refresh-token'], tokens['DOMAIN_UUID'], "AccessPolicy", f"ACP_{str(int(time.time()))}", "Sample API-created ACP", "BLOCK")
rules = xml2json()
forwardRules = rules['iptables-rules']['table'][2]['chain'][1]['rule'] # tip: use https://jsonformatter.org/json-viewer to easily explore the json file
forwardRules_list = []
for rule in forwardRules: # last index: 2475 (DROP)
forwardRules_list.append(rule)
print('Parsing ', rule)
parseRule(rule)
print('----------- \n')
# to-do list:
# - try to replicate the rule json by getting it from the FMC