-
Notifications
You must be signed in to change notification settings - Fork 0
/
FMCACPMain.py
373 lines (313 loc) · 16.3 KB
/
FMCACPMain.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
362
363
364
365
366
367
368
369
370
371
372
373
# Version 201909.NEXT
# Release Candidate
import json
import os
import sys
import time
import datetime
import requests
import tkinter as tk
# Disable SSL verifications warning
import urllib3
urllib3.disable_warnings()
# Begin Staging The Script by Collecting Access Information and generating proper tokens
def getAuthToken(fmcIP, fmcUser, fmcPass):
global domain_uuid
url = 'https://' + fmcIP + '/api/fmc_platform/v1/auth/generatetoken'
headers = {'Content-Type': 'application/json'}
try:
reqResp = requests.post(url, headers=headers, auth=requests.auth.HTTPBasicAuth(fmcUser, fmcPass), verify=False)
except Exception as e:
print('Function getAuthToken failed...', e)
sys.exit('Authentication Failure: Failed to retrieve token... Error Result: GameStopper')
else:
if reqResp.status_code == 401:
sys.exit('FMC responded with Unauthorized Access HTTP 401... Function: getAuthToken... Result: GameStopper...')
domain_uuid = reqResp.headers.get('DOMAIN_UUID', default=None)
return reqResp.headers.get('X-auth-access-token', default=None)
sys.exit('How Did I get here???... Function: getAuthToken... Error Result: GameStopper')
def inputWindow():
configData = {}
master = tk.Tk()
master.title('FMC ACP Review by Sam Jbori')
tk.Label(master, text='FMC IP :').grid(row=0)
tk.Label(master, text='User Name:').grid(row=1)
tk.Label(master, text='Password :').grid(row=2)
tk.Label(master, text='Org Name :').grid(row=3)
fmcIP = tk.Entry(master)
fmcIP.grid(row=0, column=1)
uName = tk.Entry(master)
uName.grid(row=1, column=1)
pWord = tk.Entry(master)
pWord.grid(row=2, column=1)
pWord.config(show='W')
orgID = tk.Entry(master)
orgID.grid(row=3, column=1)
tk.Button(master, text='Okay', command=master.quit).grid(row=10, column=10, sticky=tk.W, pady=4)
master.mainloop()
configData.update(FMC_IP= fmcIP.get())
configData.update(FMC_USER= uName.get())
configData.update(FMC_PASS= pWord.get())
configData.update(ORG_ID= orgID.get())
return configData # dict
def setScriptVariables():
global configFile
rebuildConfigData = False
print('Checking for config file...')
if os.path.isfile(configFile):
print('Config file found, processing saved information...')
with open(configFile, 'r') as config_file:
configData = json.loads(config_file.read())
if 'FMC_IP' in configData and 'FMC_USER' in configData and 'FMC_PASS' in configData and 'ORG_ID' in configData:
print('Config file loaded... DOESN\'T GRANTEES CORRECT DATA, DELETE config.json TO START FRESH...')
else:
print('Corrupted config file...\nRebuilding Config File, press CTRL + C to cancel...')
time.sleep(5)
print('Rebuilding')
rebuildConfigData = True
else:
print('No config file found, using user input...')
rebuildConfigData = True
if rebuildConfigData:
# configData = {
# 'FMC_IP': '',
# 'FMC_USER': '',
# 'FMC_PASS': '',
# 'ORG_ID':'',
# }
configData = inputWindow()
with open(configFile, '+w') as output_file:
json.dump(configData, output_file, indent=4)
configData.update(FMC_AUTHTOKEN=getAuthToken(configData['FMC_IP'], configData['FMC_USER'], configData['FMC_PASS']))
del configData['FMC_USER'] # remove user name
del configData['FMC_PASS'] # and password from the program
return configData
# End Staging The Script by Collecting Access Information and generating proper tokens
# The result should be 2 variables, accessVar and domain_UUID
#Begin Policy and Entry Data Collection Section
def getAccessPolicy(fmcIP, auth_token, domain_uuid, api_function, container_uuid, api_subfunction, tagName, tagID, plcName, plcID):
entryBuilder = []
global timeStamp
api_path = "/api/fmc_config/v1/domain/" + domain_uuid + api_function + container_uuid + api_subfunction # param
url = 'https://' + fmcIP + api_path + '?offset=0&limit=1000'
headers = {'Content-Type': 'application/json', 'X-auth-access-token': auth_token}
try:
apiResp = requests.get(url, headers=headers, verify=False, )
except Exception as e:
print(e)
sys.exit('Error unknown to the function logic... Function: getAccessPolicy... Result: GameStopper...\n')
if apiResp.status_code is 200:
print('API Call pulled successfully...')
elif apiResp.status_code is 404:
print('Object Not Found... No worry it\'s most likely bug id CSCvq67271... Result: Ignore...')
return None
elif apiResp.status_code is 429:
print('FMC Sent HTTP 429, pausing for', int(65 - ((time.time() - timeStamp)%60)), '... Function: getAccessPolicy... Result: Delay, sit tight...')
time.sleep(65 - ((time.time() - timeStamp)%60))
print('Proceeding with changes...')
timeStamp = time.time()
apiResp = getAccessPolicy(fmcIP, domain_uuid, auth_token, api_function, container_uuid, api_subfunction, tagName, tagID, plcName, plcID)
elif apiResp.status_code is 401:
sys.exit('FMC responded with Unauthorized Access HTTP 401... Function: getAccessPolicy... Result: GameStopper...')
else:
print('Getting HTTP', apiResp.status_code)
sys.exit('Error unknown to the function logic... Function: getAccessPolicy... Result: GameStopper...\n')
json_resp = json.loads(apiResp.text)
if 'items' in json_resp:
for element in json_resp['items']:
if not plcID:
print('Policy found, adding', element['id'], element['type'], element['name'])
else:
print('ACE found, adding', element['id'], element['type'], plcName+ '/' + element['name'])
policyPair = {'TAG_NAME' : tagName, 'TAG_ID' : tagID, 'PLC_NAME' : plcName, 'PLC_ID' : plcID, 'DOMAIN_UUID' : domain_uuid, 'OBJECT_TYPE': element['type'], 'OBJECT_ID' : element['id'], 'OBJECT_NAME' : element['name']}
entryBuilder.append(policyPair)
if len(entryBuilder) is 0:
print('Dummy policy', plcName,'- no entries - Skipping')
else:
print('Data Extracted...')
return entryBuilder
def getACLDetails(fmcIP, auth_token, domain_uuid, api_function, container_uuid, api_subfunction, object_uuid, tagName, plcName):
global timeStamp
global e500
headers = {'Content-Type': 'application/json', 'X-auth-access-token': auth_token}
api_path = "/api/fmc_config/v1/domain/" + domain_uuid + api_function + container_uuid + api_subfunction + object_uuid
url = 'https://' + fmcIP + api_path
try:
apiResp = requests.get(url, headers=headers, verify=False, )
except Exception as e:
print(e)
sys.exit('Error unknown to the function logic... Function: getACLDetails... Result: GameStopper...\n')
if apiResp.status_code == 200:
print('API Call pulled successfully...')
elif apiResp.status_code == 404 or apiResp.status_code == 500:
print('Object Not Found... No worry it\'s most likely bug id CSCvq67271... Result: Ignore...', apiResp.status_code)
return None
elif apiResp.status_code == 429:
print('FMC Sent HTTP 429, pausing for', int(65 - ((time.time() - timeStamp)%60)), ' seconds... Function: getACLDetails... Result: Delay, sit tight...')
time.sleep(65 - ((time.time() - timeStamp)%60))
print('Proceeding with changes...')
timeStamp = time.time()
apiResp = getACLDetails(fmcIP, auth_token, domain_uuid, api_function, container_uuid, api_subfunction, object_uuid, tagName, plcName)
return apiResp
elif apiResp.status_code == 401:
sys.exit('FMC responded with Unauthorized Access HTTP 401... Function: getACLDetails... Result: GameStopper...')
# elif apiResp.status_code == 500:
# e500 += 1
# if e500 > 49:
# sys.exit('Error HTTP 500 occur 50 times, time to shut this babe down')
# else:
# print('Getting HTTP 500', 50 - e500, 'remaining to shut this process down...')
# time.sleep(10)
# getACLDetails(fmcIP, auth_token, domain_uuid, api_function, container_uuid, api_subfunction, object_uuid,
# tagName, plcName)
else:
print('Response code HTTP', apiResp.status_code)
sys.exit('Error unknown to the function logic... Function: getACLDetails... Result: GameStopper...\n')
json_resp = json.loads(apiResp.text)
userList = []
if 'users' in json_resp:
userElement = json_resp['users']
for element in userElement['objects']:
pairingList = [element['type'], element['name']]
userList.append(pairingList)
urlList = []
if 'urls' in json_resp:
urlElement = json_resp['urls']
if 'literals' in urlElement:
for element in urlElement['literals']:
pairingList = [element['type'], element['url']]
urlList.append(pairingList)
if 'urlCategoriesWithReputation' in urlElement:
for catagories in urlElement['urlCategoriesWithReputation']:
if 'catagory' in catagories:
for element in catagories:
pairingList = [element['type'], element['name']]
urlList.append(pairingList)
srcZoneList = []
if 'sourceZones' in json_resp:
srcZoneElement = json_resp['sourceZones']
for element in srcZoneElement['objects']:
pairingList = [element['type'], element['name']]
srcZoneList.append(pairingList)
dstZoneList = []
if 'destinationZones' in json_resp:
dstZoneElement = json_resp['destinationZones']
for element in dstZoneElement['objects']:
pairingList = [element['type'], element['name']]
dstZoneList.append(pairingList)
srcNetList = []
if 'sourceNetworks' in json_resp:
srcNetElement = json_resp['sourceNetworks']
if 'literals' in srcNetElement:
for element in srcNetElement['literals']:
pairingList = [element['type'], element['value']]
srcNetList.append(pairingList)
if 'objects' in srcNetElement:
for element in srcNetElement['objects']:
pairingList = [element['type'], element['name']]
srcNetList.append(pairingList)
dstNetList = []
if 'destinationNetworks' in json_resp:
dstNetElement = json_resp['destinationNetworks']
if 'literals' in dstNetElement:
for element in dstNetElement['literals']:
pairingList = [element['type'], element['value']]
dstNetList.append(pairingList)
if 'objects' in dstNetElement:
for element in dstNetElement['objects']:
pairingList = [element['type'], element['name']]
dstNetList.append(pairingList)
srcPrtList = []
if 'sourcePorts' in json_resp:
srcPrtElement = json_resp['sourcePorts']
if 'literals' in srcPrtElement:
for element in srcPrtElement['literals']:
pairingList = [element['protocol'], element['port']]
srcPrtList.append(pairingList)
if 'objects' in srcPrtElement:
for element in srcPrtElement['objects']:
pairingList = [element['type'], element['name']]
srcPrtList.append(pairingList)
dstPrtList = []
if 'destinationPorts' in json_resp:
dstPrtElement = json_resp['destinationPorts']
if 'literals' in dstPrtElement:
for element in dstPrtElement['literals']:
pairingList = [element['protocol'], element['port']]
dstPrtList.append(pairingList)
if 'objects' in dstPrtElement:
for element in dstPrtElement['objects']:
pairingList = [element['type'], element['name']]
dstPrtList.append(pairingList)
appsList = []
if 'applications' in json_resp:
appsElement = json_resp['applications']
if 'catagories' in appsElement:
for element in appsElement['categories']:
pairingList = [element['type'], element['name']]
appsList.append(pairingList)
if 'applications' in appsElement:
for element in appsElement['applications']:
pairingList = [element['type'], element['name']]
appsList.append(pairingList)
print('Data Extraction completed successfully... Next')
return {'DOMAIN_ID' :domain_uuid, 'TAG_ID' : tagName, 'PLC_ID': container_uuid, 'PLC_NAME': plcName,
'OBJECT_ID': object_uuid, 'OBJECT_NAME' : json_resp['name'],'OBJECT_ENABLED' : json_resp['enabled'],
'ZN_SRC' : srcZoneList, 'ZN_DST' : dstZoneList, 'SRC_NET' : srcNetList, 'DST_NET' : dstNetList,
'PORT_SRC' : srcPrtList, 'PORT_DST' : dstPrtList, 'USERS' : userList, 'URLS' : urlList, 'APPS' : appsList}
def processedACLEntries(aceEntries):
processedText = 'Orginization\tDomain ID\tPolicy Name\tPolicy ID\tRule\'s Name\tRule\'s ID\tEnabled\tSource Zone\t' \
'Destination Zone\tSource Network\tDestination Network\tSource Port\tDestination Port\tUsers\tURL\t' \
'Applications\n'
for entry in aceEntries:
processedText += str(entry['TAG_ID']) + '\t' + \
str(entry['DOMAIN_ID']) + '\t' + \
str(entry['PLC_NAME']) + '\t' + \
str(entry['PLC_ID']) + '\t' + \
str(entry['OBJECT_NAME']) + '\t' + \
str(entry['OBJECT_ID']) + '\t' + \
str(entry['OBJECT_ENABLED']) + '\t' + \
str(entry['ZN_SRC']) + '\t' + \
str(entry['ZN_DST']) + '\t' + \
str(entry['SRC_NET']) + '\t' + \
str(entry['DST_NET']) + '\t' + \
str(entry['PORT_SRC']) + '\t' + \
str(entry['PORT_DST']) + '\t' + \
str(entry['USERS']) + '\t' + \
str(entry['URLS']) + '\t' + \
str(entry['APPS']) + '\n'
return processedText
def writeReviewFile(aceEntries):
fileName = 'FirewallReviews.txt' #+ str(datetime.datetime.today()) + '.txt'
reportFile = open(fileName, "w")
reportFile.write(processedACLEntries(aceEntries))
# Begin main section
timeStamp = time.time() # Recover mechanism against HTTP 429 by the FMC
# System Access Global Variables!
domain_uuid = None # FMC Global Domain UUID
configFile = 'config.json' # Configfile name
accessPolicy = [] # Access Policy list of names, UUID,
aclEntries = [] # ACL Entries: Parent Policy, Parent UUID, Policy Name, UUID
aclDetails = [] # ACL Entries Detailed: Parent Policy, Parent UUID, Self Name, Self UUID,
# Zone SRC/DST, IP SRC/DST, Port SRC/DST, App, URL/Car, User/Group
accessVar = setScriptVariables() # Contain Sever IP 'FMC_IP' and AuthToken 'FMC_AUTHTOKEN'
print('Authentication Token retrieved: ********-****-****-****-****'+ str(accessVar['FMC_AUTHTOKEN'])[-8:])
print('Generating Access Policy list...')
accessPolicy = getAccessPolicy(accessVar['FMC_IP'], accessVar['FMC_AUTHTOKEN'], domain_uuid, "/policy/accesspolicies", '', '', accessVar['ORG_ID'], domain_uuid,'','')
for element in accessPolicy:
print('Importing Policy', element['OBJECT_ID'], element["OBJECT_NAME"])
aclEntry = getAccessPolicy(accessVar['FMC_IP'], accessVar['FMC_AUTHTOKEN'], domain_uuid, "/policy/accesspolicies/", element['OBJECT_ID'], '/accessrules', element['TAG_NAME'], element['TAG_ID'],element['OBJECT_NAME'], element['OBJECT_ID'])
if aclEntry:
for element in aclEntry:
aclEntries.append(element)
del aclEntry
del element
for element in aclEntries:
# getACLDetails(auth_token, api_function, container_uuid, api_subfunction, object_uuid, tagName, plcName):
print('Retrieving ACE', element['OBJECT_ID'], element['PLC_NAME'] + '/' + element['OBJECT_NAME'])
aclEntry = getACLDetails(accessVar['FMC_IP'], accessVar['FMC_AUTHTOKEN'], domain_uuid, "/policy/accesspolicies/",
element['PLC_ID'], '/accessrules/', element['OBJECT_ID'], element['TAG_NAME'], element['PLC_NAME'])
if aclEntry:
aclDetails.append(aclEntry)
writeReviewFile(aclDetails)
print('TADA...')