-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathauto_sc_config.py
358 lines (302 loc) · 12.4 KB
/
auto_sc_config.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
############################################################
# Name: autoSCConfig.py #
# Description: Uses supplied information to configure #
# SecurityCenter 5.x. #
# #
# **DISCLAIMER** #
# This script was designed for SecurityCenter 5.x using #
# Python 3.x. Please make sure all variables at the #
# bottom of this script are filled in prior to running. #
# #
# #
# INSTRUCTIONS FOR USE: #
# This script uses 'requests' and 'urllib3', please make #
# sure those are installed on your system. You can install #
# these using pip. #
# #
# 1. Fill in all variables listed below (currently the #
# variables are set with dummy values). #
# 2. Install the SecurityCenter 5 RPM. Once installation #
# is complete, start the service by running: #
# service SecurityCenter start #
# 3. Next, run this python script from the command line. #
# The script will use the given license key path and #
# other defined variables to register the product. #
# 4. Once the script is finished you can access your admin #
# account using your specified password or the 'secman' #
# Security Manager account using 'password' for the #
# password. SecurityCenter will prompt you to change this.#
# #
# NOTE: This only registers a single Nessus scanner at #
# this time. Additional Nessus scanners, as well as LCE #
# and PVS scanners must be added manually once setup is #
# complete. #
# #
# Version 1.1 #
############################################################
import json
import re
import sys
import urllib3
import requests
requests.packages.urllib3.disable_warnings()
class SecurityCenter:
def __init__(self, server, verify_ssl=False):
self.server = server
self.verify = verify_ssl
self.token = ''
self.cookie = ''
def login(self, username, password):
# Our login function. This will store and return our token
# value used later in the script.
data = {'username': username, 'password': password}
resp = self.connect('POST', 'token', data)
print(resp)
if resp is not None:
self.token = str(resp['token'])
def logout(self):
# Destroys token forcing a logout.
self.connect('DELETE', 'token')
self.token = ''
self.cookie = ''
def connect(self, method, resource, data=None):
headers = {
'Content-Type': 'application/json',
}
if self.token != '':
headers['X-SecurityCenter'] = self.token
if self.cookie != '':
headers['Cookie'] = self.cookie
# Only convert the data to JSON if there is data.
if data is not None:
data = json.dumps(data)
url = "https://{0}/rest/{1}".format(self.server, resource)
# Our API calls (POST, PUT, DELETE, PATCH, GET)
try:
if method == 'POST':
r = requests.post(url, data=data, headers=headers, verify=self.verify)
elif method == 'PUT':
r = requests.put(url, data=data, headers=headers, verify=self.verify)
elif method == 'DELETE':
r = requests.delete(url, data=data, headers=headers, verify=self.verify)
elif method == 'PATCH':
r = requests.patch(url, data=data, headers=headers, verify=self.verify)
else:
r = requests.get(url, params=data, headers=headers, verify=self.verify)
# Checks for connection error and prints the error.
except requests.ConnectionError as e:
print(e)
return None
if r.headers.get('set-cookie') is not None:
match = re.findall("TNS_SESSIONID=[^,]*", r.headers.get('set-cookie'))
self.cookie = match[1]
print(self.cookie)
# Checks the data for a JSON response. Returns none if no data exists.
try:
jsondata = r.json()
except ValueError as e:
print(e)
return None
# Throws an error message if the error code is not 0.
if jsondata['error_code'] != 0:
sys.exit(jsondata['error_msg'])
# Returns the 'response' section of the JSON data.
return jsondata['response']
def upload(self, licensekeypath):
headers = {
'X-SecurityCenter': self.token,
'Cookie': self.cookie
}
print(self.token)
params = {}
# Opens the file path and stores it as f
f = open(licensekeypath, 'r')
# Sets files to include JSON data for the uploaded file.
files = {'Filedata': f}
url = "https://{0}/rest/file/upload".format(self.server)
print('Uploading file {0}.'.format(licensekeypath))
r = requests.post(url, headers=headers, params=params, files=files, verify=self.verify)
# Prints JSON request data and response data.
# These can be commented out if there is too much information displayed.
print('Request Data: {0}'.format(r.request.body))
print('Response Data: {0}'.format(r.content))
# Make sure we have a JSON response. If not then return None.
try:
jsonData = r.json()
except ValueError as e:
print(e)
return None
# If the response error code is not 0, there is an error.
if jsonData['error_code'] != 0:
sys.exit(jsonData['error_msg'])
# Return the filename given by SecurityCenter
print("License has been uploaded successfully as {0}.".format(jsonData['response']['filename']))
return jsonData['response']['filename']
def apply_license(self, filename):
# Applies the uploaded license file and registers SecurityCenter
data = {"filename": filename}
print("Applying the license file.")
r = self.connect('POST', 'config/license/register', data)
print("License was successfully applied.")
def register_nessus(self, nessus_activation_code):
# Registers the provided Nessus activation code.
print("Registering Nessus activation code...")
data = {"activationCode": nessus_activation_code,
"updateSite": "downloads.nessus.org",
"type": "active"}
r = self.connect('POST', '/config/plugins/register', data)
# Checks if the activation code returned as 'invalid'.
if 'Invalid' in r.values():
print("Invalid activation code. This may be due to the code being in use or entered incorrectly.")
else:
print("Nessus activation was successful.")
def set_admin_passwd(self, passwd):
# Changes the admin password from 'admin' to the
# provided value.
input = {"password": passwd}
print("Changing administrator password...")
r = self.connect('PATCH', '/user/1', input)
print("Administrator password changed successfully.")
def add_scan_zone(self, name, range):
# Adds scan zone using specified variables.
data = {
"id": "1",
"name": name,
"description": "",
"ipList": range}
print("Adding scan zone...")
self.connect('POST', '/zone', data)
print("Scan zone added successfully...")
def add_nessus_scanner(self, scan_zone_name, name, ip, nessus_username, nessus_password, port):
# Adds Nessus scanner using the provided variables.
# Left more settings than needed in case other options
# are needed (such as proxy).
if port is "":
port = 8834
data = {"id": "1",
"name": name,
"description": "",
"ip": ip,
"port": port,
"useProxy": "false",
"enabled": "true",
"verifyHost": "false",
"managePlugins": "true",
"authType": "password",
"username": nessus_username,
"password": nessus_password,
"admin": "false",
"zones":
[{
"id": "1",
"name": scan_zone_name,
"description": ""
}]
}
print("Adding Nessus scanner...")
self.connect('POST', '/scanner', data)
print("Nessus scanner added successfully.")
def add_organization(self, org_name):
# Adds organization using the provided variables.
data = {
"id": "1",
"name": org_name,
"description": "",
"zoneSelection": "auto_only"
}
print("Adding organization...")
self.connect('POST', '/organization', data)
print("Organization successfully added.")
def add_repository(self, org_name, repo_name, repo_ip_range):
# Adds Repository using the provided variables.
data = {
"name": repo_name,
"description": "",
"dataFormat": "IPv4",
"type": "Local",
"trendingDays": "30",
"trendWithRaw": "true",
"ipRange": repo_ip_range,
"organizations":
[{
"id": 1,
"name": org_name,
}]
}
print("Adding repository...")
self.connect('POST', '/repository', data)
print("Repository added successfully.")
def add_security_manager(self, username="secman", password="password"):
# Creates Security Manager and assigns them to the freshly
# created Organization. Sets password to 'password' which
# must be changed upon logging into the Security Manager.
data = {
"name": "",
"description": "",
"firstname": "Security",
"lastname": "Manager",
"username": username,
"password": password,
"title": "Security Manager",
"address": "",
"city": "",
"state": "",
"country": "",
"phone": "",
"email": "",
"fax": "",
"authType": "tns",
"orgID": 1,
"roleID": 2,
"mustChangePassword": "true"}
print("Creating Security Manager ...")
r = self.connect('POST', '/user', data)
print("Security Manager created successfully.")
if __name__ == '__main__':
"""
MODIFY THE BELOW VARIABLES TO MATCH YOUR NEEDS.
"""
# SecurityCenter IP
sc = SecurityCenter('172.26.18.153')
# Path to your license key file.
licenseKeyPath = '/tmp/tenable-SecurityCenter-4.2-5.x-DEMO.key'
# Nessus activation code
nessusActivationCode = ""
# New administrator password
newAdminPassword = ""
# Scan zone name.
scanZoneName = ""
# Scan Zone IP Range
scanZoneRange = "0.0.0.0/0"
# Nessus scanner name
nessusName = "Nessus Scanner"
# Nessus IP Address
nessusIP = ""
# Nessus user name
nessusUN = ""
# Nessus password
nessusPW = ""
# Nessus port, this will default to 8834 if nothing is supplied.
port = ""
# Organization Name
orgName = "Test Org"
# Repository Name
repoName = "Test Repo"
# Repository IP Range
repoIPRange = "0.0.0.0/0"
# Security Manager user name
securityManagerUN = "secman"
# DO NOT MODIFY THE BELOW DATA #
sc.login('admin', 'admin')
fileName = sc.upload(licenseKeyPath)
sc.apply_license(fileName)
sc.register_nessus(nessusActivationCode)
sc.set_admin_passwd(newAdminPassword)
sc.add_scan_zone(scanZoneName, scanZoneRange)
sc.add_nessus_scanner(scanZoneName, nessusName, nessusIP, nessusUN, nessusPW, port)
sc.add_organization(orgName)
sc.add_repository(orgName, repoName, repoIPRange)
sc.add_security_manager(securityManagerUN)
print("SecurityCenter is now configured.")
print("Logging out.")
sc.logout()