forked from TheAlanNix/cisco-security-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkObjectDelete.py
executable file
·295 lines (219 loc) · 9.86 KB
/
NetworkObjectDelete.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#####################
# ABOUT THIS SCRIPT #
#####################
#
# NetworkObjectDelete.py
# ----------------
# Author: Alan Nix
# Property of: Cisco Systems
# Version: 1.0
# Release Date: 09/11/2017
#
# Summary
# -------
#
# This script will delete all FMC objects that start with a specified pre-fix.
#
# Requirements
# ------------
#
# 1) Must have Python installed.
# 2) Must have 'requests' Python module installed. Easiest way to do that:
# - wget https://bootstrap.pypa.io/get-pip.py
# - python get-pip.py (may need to use 'sudo')
# - pip install requests (may need to use 'sudo')
#
# How To Run
# ----------
#
# 1) Configure Firepower Management Console IP and Credentials
# 2) Specify the Object Prefix
#
############################################################
import getpass
import logging
import json
import os
import requests
from requests.auth import HTTPBasicAuth
from pprint import pprint
# If receiving SSL Certificate Errors, un-comment the line below
#requests.packages.urllib3.disable_warnings()
####################
# CONFIGURATION #
####################
#
#----------------------------------------------------#
#
# Logging Parameters
logging.basicConfig(filename='NetworkObjectDelete.log', filemode='w', level=logging.INFO)
# Network Object Prefix
OBJECT_PREFIX = "BOGUS_PREFIX"
# Firepower Management Console Variables
FMC_IP = ""
FMC_USERNAME = None
FMC_PASSWORD = None
FMC_AUTH_TOKEN = None
FMC_DOMAIN_UUID = None
#
#----------------------------------------------------#
####################
# !!! DO WORK !!! #
####################
#----------------------------------------------------#
# Afunction to get the authentication token from the FMC
def getAuthTokenFMC():
global FMC_AUTH_TOKEN, FMC_DOMAIN_UUID
logging.info('Fetching Authentication Token from FMC...')
# Build HTTP Authentication Instance
auth = HTTPBasicAuth(FMC_USERNAME, FMC_PASSWORD)
# Build HTTP Headers
auth_headers = {'Content-Type': 'application/json'}
# Build URL for Authentication
auth_url = "https://{}/api/fmc_platform/v1/auth/generatetoken".format(FMC_IP)
try:
http_req = requests.post(url=auth_url, auth=auth, headers=auth_headers, verify=False)
logging.debug('FMC Auth Response: ' + str(http_req.headers))
FMC_AUTH_TOKEN = http_req.headers.get('X-auth-access-token', default=None)
FMC_DOMAIN_UUID = http_req.headers.get('DOMAIN_UUID', default=None)
if FMC_AUTH_TOKEN == None:
print('Authentication Token Not Found...')
logging.error('Authentication Token Not Found. Exiting...')
exit()
logging.info('Authentication Token Successfully Fetched.')
except Exception as err:
print('Error fetching auth token from FMC: ' + str(err))
logging.error('Error fetching auth token from FMC: ' + str(err))
exit()
#----------------------------------------------------#
#----------------------------------------------------#
# A function to modify an Object in the FMC
def ObjectCallFMC(method, endpoint, json_data=None):
print('Submitting ' + str(endpoint) + ' Object to the FMC via ' + str(method) + ' request. Data: ' + json.dumps(json_data))
logging.info('Submitting ' + str(endpoint) + ' Object to the FMC via ' + str(method) + ' request. Data: ' + json.dumps(json_data))
# Build URL for Authentication
endpoint_url = "https://{}/api/fmc_config/v1/domain/{}/object/{}".format(FMC_IP, FMC_DOMAIN_UUID, endpoint)
# Build new headers with the access token
headers = {'Content-Type': 'application/json', 'X-auth-access-token': FMC_AUTH_TOKEN}
try:
if method is 'POST':
http_req = requests.post(url=endpoint_url, headers=headers, json=json_data, verify=False)
elif method is 'PUT':
http_req = requests.put(url=endpoint_url, headers=headers, json=json_data, verify=False)
elif method is 'DELETE':
http_req = requests.delete(url=endpoint_url, headers=headers, json=json_data, verify=False)
else:
http_req = requests.get(url=endpoint_url, headers=headers, json=json_data, verify=False)
# Check to make sure the POST was successful
if http_req.status_code >= 200 and http_req.status_code < 300:
#print('Request succesfully sent to FMC.')
logging.info('Request succesfully sent to FMC.')
logging.debug('HTTP Response: ' + str(http_req.text))
return http_req.json()
else:
print("FMC Connection Failure - HTTP Return Code: {}\nResponse: {}".format(http_req.status_code, http_req.json()))
logging.error("FMC Connection Failure - HTTP Return Code: {}\nResponse: {}".format(http_req.status_code, http_req.json()))
exit()
except Exception as err:
print('Error posting request to FMC: ' + str(err))
logging.error('Error posting request to FMC: ' + str(err))
exit()
#----------------------------------------------------#
#----------------------------------------------------#
# A function to modify an Object in the FMC
def getAllObjectsFMC(endpoint):
try:
OBJECT_LIST = []
# Query Loop Parameters
query_limit = 50
query_offset = 0
object_count = query_limit
# Loop through all Host objects
while (object_count == query_limit):
# Build a URL with the appropriate parameters
url_endpoint = endpoint + "?offset={}&limit={}".format(query_offset, query_limit)
# Get the objects from the FMC
object_response = ObjectCallFMC('GET', url_endpoint)
# Update the number of returned Host objects
object_count = object_response['paging']['limit']
if 'items' not in object_response.keys():
break
# Go through all hosts and see if they have the prefix
for current_object in object_response['items']:
# If the host begins with our prefix, then add it to our list
if current_object['name'].startswith(OBJECT_PREFIX):
OBJECT_LIST.append(current_object['id'])
# Increment the query offset
query_offset += query_limit
return OBJECT_LIST
except Exception as err:
logging.error(err)
#----------------------------------------------------#
#----------------------------------------------------#
# A function to get all objects, and delete the ones that match the specified prefix
if __name__ == "__main__":
logging.info("Starting Network Object Delete...")
# If not hard coded, get the FMC Username and Password
if FMC_USERNAME is None:
FMC_USERNAME = input("Firepower Username:")
if FMC_PASSWORD is None:
FMC_PASSWORD = getpass.getpass("Firepower Password:")
# If there's no Authentication Token, then fetch one
if FMC_AUTH_TOKEN == None:
getAuthTokenFMC()
# Get all of the objects that have the OBJECT_PREFIX
network_groups = getAllObjectsFMC('networkgroups')
print("Deleting {} Network Group Items...".format(len(network_groups)))
# Go through the delete process for each entity
for network_group in network_groups:
# Build a URL with the appropriate parameters
network_groups_endpoint = "networkgroups/{}".format(network_group)
# Delete the Host object from the FMC
network_groups_response = ObjectCallFMC('DELETE', network_groups_endpoint)
# Get all of the objects that have the OBJECT_PREFIX
hosts = getAllObjectsFMC('hosts')
print("Deleting {} Host Items...".format(len(hosts)))
# Go through the delete process for each entity
for host in hosts:
# Build a URL with the appropriate parameters
hosts_endpoint = "hosts/{}".format(host)
# Delete the Host object from the FMC
hosts_response = ObjectCallFMC('DELETE', hosts_endpoint)
# Get all of the objects that have the OBJECT_PREFIX
networks = getAllObjectsFMC('networks')
print("Deleting {} Network Items...".format(len(networks)))
# Go through the delete process for each entity
for network in networks:
# Build a URL with the appropriate parameters
networks_endpoint = "networks/{}".format(network)
# Delete the Networks object from the FMC
networks_response = ObjectCallFMC('DELETE', networks_endpoint)
# Get all of the objects that have the OBJECT_PREFIX
address_ranges = getAllObjectsFMC('ranges')
print("Deleting {} Address Range Items...".format(len(address_ranges)))
# Go through the delete process for each entity
for address_range in address_ranges:
# Build a URL with the appropriate parameters
address_ranges_endpoint = "ranges/{}".format(address_range)
# Delete the Address Range object from the FMC
address_ranges_response = ObjectCallFMC('DELETE', address_ranges_endpoint)
# Get all of the objects that have the OBJECT_PREFIX
port_groups = getAllObjectsFMC('portobjectgroups')
print("Deleting {} Port Group Items...".format(len(port_groups)))
# Go through the delete process for each entity
for port_group in port_groups:
# Build a URL with the appropriate parameters
port_groups_endpoint = "portobjectgroups/{}".format(port_group)
# Delete the Port Group object from the FMC
port_groups_response = ObjectCallFMC('DELETE', port_groups_endpoint)
# Get all of the objects that have the OBJECT_PREFIX
ports = getAllObjectsFMC('protocolportobjects')
print("Deleting {} Port Items...".format(len(ports)))
# Go through the delete process for each entity
for port in ports:
# Build a URL with the appropriate parameters
ports_endpoint = "protocolportobjects/{}".format(port)
# Delete the Port object from the FMC
ports_response = ObjectCallFMC('DELETE', ports_endpoint)