forked from david-sackmary/whack-a-mole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn.py
208 lines (191 loc) · 6.78 KB
/
fn.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
#!/usr/bin/python
# General functions
import server
import re
import sys
import os
import getopt
import api
import json
import dumper
def enrich_cve_list(cves):
retlist = {}
urlbase = 'https://web.nvd.nist.gov/view/vuln/detail?vulnId='
for cve in cves:
name = str(cve)
url = urlbase + name
retlist[name] = url
return(retlist)
def enrich_cve(cve):
urlbase = 'https://web.nvd.nist.gov/view/vuln/detail?vulnId='
name = str(cve)
url = urlbase + name
return(url)
def cve_e_to_html(cve_enriched):
html = ''
for c, l in cve_enriched.iteritems():
html = html + str('<a href="' + str(l) + '">' + str(c) + '</a> ')
return(html)
def build_server_list(host, authtoken, srch, field, prox):
queryurl = '/v1/servers'
jsondata = api.apihit(host, 'GET', authtoken, queryurl, '', prox)
server_list = []
if srch == 'ALL':
prefix = '^.*'
else:
prefix = '^'+str(srch)+'.*'
relevant_servers = distil_server_list(jsondata, prefix, field)
for s in relevant_servers:
# Needed by Mr. Kozak
platform = s["platform"]
platform_version = s["platform_version"]
os = s["os_version"]
kernel = s["kernel_name"]
machine = s["kernel_machine"]
name = s["hostname"]
label = s["server_label"]
ident = s["id"]
gname = s["group_name"]
print (s["connecting_ip_address"] + ", " + name + ", " + platform_version + ", " + platform + ", " + os + ", " + kernel + ", " + machine)
server_list.append(server.Server(name, ident, label, gname, platform, platform_version, os, kernel, machine))
if server_list == []:
print "Did not find any matching servers to report on!"
sys.exit(2)
else:
return(server_list)
def enrich_server_data(host, authtoken, slist, prox):
returned_dataz = []
for s in slist:
s.issues = get_server_issues(host, authtoken, s.id, prox)
returned_dataz.append(s)
return(returned_dataz)
def distil_server_list(jdata, prefix, field):
rexall = re.compile(prefix)
relevant_list = []
for s in jdata['servers']:
svr_dataz = {}
if s[field] == None:
#We set the label to ' ' instead of None, so we don't have a data type issue later on...
s[field] = ' '
if rexall.match(s[field]):
relevant_list.append(s)
return(relevant_list)
def get_server_issues(host,authtoken,node_id,prox):
queryurl = '/v1/servers/'+str(node_id)+'/issues'
results = api.apihit(host, 'GET', authtoken, queryurl, '', prox)
return(results)
def set_config_items(config,argv):
try:
opts, args = getopt.getopt(argv, "hs:c:a",["search_string=","configfile="])
except getopt.GetoptError:
print config['usagetext']
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print config['usagetext']
sys.exit()
elif opt == '-a':
config['search_string'] = 'ALL'
elif opt in ("-s","--search_string"):
config['search_string'] = arg
elif opt in ("-c","--configfile"):
config['configfile'] = arg
execfile(config['configfile'], config)
if not sanity_check(config):
print "Config is not sane!"
sys.exit(2)
config['prox'] = {'host': config['prox_host'], 'port': config['prox_port'] }
config['authtoken'] = api.get_auth_token(config['host'], config['clientid'], config['clientsecret'], config['prox'])
return(config)
def sanity_check(c):
search_fields = ["server_label", "hostname", "reported_fqdn", "group_name"]
output_formats = ["text", "pdf", "html",""]
insane = False
if c['host'] == '':
print "No hostname defined!"
insane = True
if c['clientid'] == '':
print "No clientid defined!"
insane = True
if c['clientsecret'] == '':
print "No clientsecret defined!"
insane = True
try:
if not c['search_string']:
print "No search string defined!"
insane = True
elif c['search_string'] == '':
print "Scope of report not defined!"
print "Need to state the search prefix!"
print c['usagetext']
insane = True
except:
print "Bad search string! Check your syntax!"
insane = True
try:
if not c['output']:
print "No output defined!"
insane = True
elif c['output'] not in output_formats:
print "Report output not correctly defined!"
insane = True
except:
print "Bad search string! Check your syntax!"
if c['search_field'] not in search_fields:
print "Bad search field value!"
insane = True
if not whut_am_i(c['prox_host']) in ["valid_host", "valid_ip", "empty"]:
print "Bad proxy host value."
insane = True
if not whut_am_i(c['prox_port']) in ["valid_port", "empty"]:
print "Bad value for proxy port."
insane = True
if where_is_img(c['logo_url'])[0] == 'web' and c['prox_host'] != '' and c['output'] == 'pdf':
print "If you're using a logo in your PDF report, and you require the use of a proxy, you must reference the logo from the local filesystem."
insane = True
if insane == True:
return False
else:
return True
def whut_am_i(val):
regex_bundle = {
'ip_address': re.compile(r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'),
'hostname': re.compile(r'^[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,3})?$'),
}
if val == '':
return('empty')
try:
val = int(val)
except:
pass
if type(val) == int and 0 < val < 65536:
return('valid_port')
elif regex_bundle['ip_address'].match(val):
return('valid_ip')
elif regex_bundle['hostname'].match(val):
return('valid_hostname')
else:
return('who_knows')
def where_is_img(path):
regex_bundle = {
'web_url': re.compile(r'^(http|https)://.*$'),
'local_path': re.compile(r'^(\./|\../|/)\S+$'),
}
if path == '':
return('', '')
elif regex_bundle['web_url'].match(path):
return('web', '')
elif regex_bundle['local_path'].match(path):
abspath = os.path.abspath(path)
return('local', abspath)
else:
print "Unable to interpret image path."
return('', '')
def handle_output(config, serverolist):
if config['output'] != None:
if config['output'] in ('html', 'pdf'):
dumper.html(serverolist, config['search_string'], config['output'], config['logo_url'])
if config['output'] == 'text':
dumper.print_server_stuff_plain(serverolist)
if config['output'] == None:
dumper.print_server_stuff_plain(serverolist)