/
nex.py
176 lines (161 loc) · 8.33 KB
/
nex.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
from xml.etree.ElementTree import Element, SubElement, ElementTree
from defusedxml.ElementTree import parse, fromstring
from io import BytesIO
import requests
import util
from util import Utilities
# Have not used the diffusedXML library as the XML construction functions is only done here and they are not supported.
class Nexpose:
"""All the Nexpose API are handled here"""
# Payload type XML
# self. - for all members that are common to the object without self scope is limited to the block
# self must the default mandatory parameter in all member functions
# Caution !!: __init__ have two underscores on each side
def makeRequest(self, url, payload, headers):
response = requests.post(url, data=payload, headers=headers, verify=False)
# print(response.text)
return response.content
def __init__(self, scanner_info):
try:
self.nexpose_host = scanner_info['host']
self.reqURL = self.nexpose_host + "/api/1.1/xml"
self.headers = {'Content-Type': 'text/xml'}
self.login_try = 0
self.msg = ""
except Exception as e:
Utilities.printException(str(e))
def login_nexpose(self, scanner_info):
# API v1.1 Login and get the session here
max_login_try_limit = 2
while True:
if self.login_try == 0:
xmlReq = Element('LoginRequest', attrib={'user-id': scanner_info['uname'], 'password': scanner_info['passwd']})
elif self.login_try > 0 and self.login_try < max_login_try_limit:
usr_name = input("Please enter your username for " + " Nexpose" + ": ")
usr_passwd = input("Please enter your password for " + " Nexpose" + ": ")
xmlReq = Element('LoginRequest', attrib={'user-id': usr_name, 'password': usr_passwd})
else:
Utilities.printError("Nexpose login attemts exceded maximum limit, skipping Nexpose tasks..")
return False
xmlReq = Element('LoginRequest', attrib={'user-id': scanner_info['uname'], 'password': scanner_info['passwd']})
xmlTree = ElementTree(xmlReq)
f = BytesIO()
xmlTree.write(f, encoding='utf-8', xml_declaration=True) # required so that xml declarations will come up in generated XML
loginReqXML = f.getvalue().decode("utf-8") # converts bytes to string
# print(self.loginReqXML)
responseXML = self.makeRequest(self.reqURL, loginReqXML, self.headers)
tree = ElementTree(fromstring(responseXML))
root = tree.getroot()
loginResponse = root.get('success')
if (loginResponse == "1"):
self.session_id = root.get('session-id')
Utilities.printSuccess("Logged in to Nexpose Scanner")
return True
else:
fa = root.find('Failure')
ex = fa.find('Exception')
msg = ex.find('message').text
Utilities.printError("Login Failure: " + msg)
self.login_try += 1
# API v1.1 SiteSave- Save changes to a new or existing site.
def addSite(self, access_req):
# print("TestModule:SiteManagement")
siteSaveRequest = Element('SiteSaveRequest', attrib={'session-id': self.session_id})
# print(access_req['site_name'])
# Site element have 'S' in caps !!--lost a day on this !!
site_elem = SubElement(siteSaveRequest, 'Site', attrib={'name': access_req['site_name'], 'id': '-1'})
host_elem = SubElement(site_elem, 'Hosts')
for ip in access_req['ip'].split(','):
range_elem = SubElement(host_elem, 'range', attrib={'from': ip, 'to': ''})
scanConfig_elem = SubElement(site_elem, 'ScanConfig', attrib={'name': 'Full audit', 'templateID': 'full-audit'})
xmlTree = ElementTree(siteSaveRequest)
f = BytesIO()
xmlTree.write(f, encoding='utf-8',
xml_declaration=True) # required so that xml declarations will come up in generated XML
saveSiteReqXML = f.getvalue().decode("utf-8") # converts bytes to string
# print(saveSiteReqXML)
responseXML = self.makeRequest(self.reqURL, saveSiteReqXML, self.headers)
tree = ElementTree(fromstring(responseXML))
root = tree.getroot()
addSiteResponse = root.get('success')
if (addSiteResponse == "1"):
self.site_id = root.get('site-id')
Utilities.printSuccess("Created site with site-id: " + self.site_id)
return True
else:
fa = root.find('Failure')
ex = fa.find('Exception')
msg = ex.find('message').text
Utilities.printError("Site creation failed: " + msg)
return False
# API v1.1 UserSave- Create a new user account, or update the settings for an existing account.
def addUser(self, access_req):
# print("addUser Module")
usrLst = access_req['userList']
for user in usrLst:
usrSaveRequest = Element('UserSaveRequest', attrib={'session-id': self.session_id})
userinfo = user.split(',') # uname,name,email
pswd = Utilities.gen_code()
usrConfig_elem = SubElement(usrSaveRequest, 'UserConfig',
attrib={'id': '-1', 'role-name': 'user', 'authsrcid': '-1', 'enabled': '1',
'name': userinfo[0], 'fullname': userinfo[1], 'email': userinfo[2],
'password': pswd})
sites_elem = SubElement(usrConfig_elem, 'UserSite')
site_elem = SubElement(sites_elem, 'site', attrib={'id': self.site_id})
site_elem.text = access_req['site_name']
xmlTree = ElementTree(usrSaveRequest)
f = BytesIO()
xmlTree.write(f, encoding='utf-8',
xml_declaration=True) # required so that xml declarations will come up in generated XML
usrSaveReqXML = f.getvalue().decode("utf-8") # converts bytes to string
# print(usrSaveReqXML)
responseXML = self.makeRequest(self.reqURL, usrSaveReqXML, self.headers)
# print(responseXML)
tree = ElementTree(fromstring(responseXML))
root = tree.getroot()
addUserReq = root.get('success')
if (addUserReq == "1"):
Utilities.printSuccess("Created user: " + userinfo[0])
self.msg = "Nexpose\nUsername:"+userinfo[0]+"\nPassword:"+pswd
return True
else:
fa = root.find('Failure')
ex = fa.find('Exception')
msg = ex.find('message').text
Utilities.printError("User creation failed: " + msg)
return False
def handleAccessReq(self, access_req, scanner_info):
user_add_status = False
# print("TestMofule")
if self.login_nexpose(scanner_info):
addSiteStatus = self.addSite(access_req)
if addSiteStatus:
user_add_status = self.addUser(access_req)
else:
Utilities.printError("Site creation failed, aborting user creation..")
self.logoutOperation()
if user_add_status:
return self.msg
else:
return "Nexpose user creation failed"
# API v1.1 Logout and close the session
def logoutOperation(self):
xmlReq = Element('LogoutRequest', attrib={'session-id': self.session_id})
xmlTree = ElementTree(xmlReq)
f = BytesIO()
xmlTree.write(f, encoding='utf-8',
xml_declaration=True) # required so that xml declarations will come up in generated XML
logoutReqXML = f.getvalue().decode("utf-8") # converts bytes to string
# print(logoutReqXML)
responseXML = self.makeRequest(self.reqURL, logoutReqXML, self.headers)
tree = ElementTree(fromstring(responseXML))
root = tree.getroot()
logoutResponse = root.get('success')
if (logoutResponse == "1"):
self.session_id = root.get('session-id')
Utilities.printSuccess("Logged out of Nexpose Scanner")
else:
fa = root.find('Failure')
ex = fa.find('Exception')
msg = ex.find('message').text
Utilities.printError("Logout Failure: " + msg)