-
Notifications
You must be signed in to change notification settings - Fork 120
/
find_hosts_by_cve.py
382 lines (333 loc) · 12.5 KB
/
find_hosts_by_cve.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
374
375
376
377
378
379
380
381
382
"""
______ __ _______ __ __ __
| |.----.-----.--.--.--.--| | __| |_.----.|__| |--.-----.
| ---|| _| _ | | | | _ |__ | _| _|| | <| -__|
|______||__| |_____|________|_____|_______|____|__| |__|__|__|_____|
_______ __ __ __ __ __
| _ .-----.-----| |_| |__.-----| |--| |_
| 1___| _ | _ | _| | | _ | | _|
|____ | __|_____|____|__|__|___ |__|__|____|
|: 1 |__| |_____|
|::.. . |
`-------' Find hosts by CVE
Creation date: 01.13.2021 - jshcodes@CrowdStrike
This solution requires the crowdstrike-falconpy (v0.8.6+) and tabulate packages.
python3 -m pip install crowdstrike-falconpy tabulate
Required API scopes
Hosts: READ
Spotlight: READ
"""
from argparse import ArgumentParser, RawTextHelpFormatter
import json
import sys
try:
from tabulate import tabulate
except ImportError as no_tabulate:
raise SystemExit(
"The tabulate library is not installed.\n"
"Install this package using the following command:\n"
" python3 -m pip install tabulate"
) from no_tabulate
try:
from falconpy import SpotlightVulnerabilities, Hosts, OAuth2
except ImportError as no_falconpy:
raise SystemExit(
"The crowdstrike-falconpy package is not installed.\n"
"Install this package using the following command:\n"
" python3 -m pip install crowdstrike-falconpy"
) from no_falconpy
class SpotlightCVEMatch(): # pylint: disable=R0902
"""Class to represent a returned match to a CVE vulnerability."""
def __init__(self, api_response): # pylint: disable=R0912
"""Initialize the object and set all attributes based upon the inbound API response."""
self.cve = None
self.score = 0
self.severity = None
self.cve_description = None
self.created_on = None
self.updated_on = None
self.hostname = "Unknown"
self.local_ip = "Unknown"
self.os_version = "Unknown"
self.service_provider = "Unknown"
self.remediation = "Not found"
self.status = "open"
cve_detail = api_response["cve"]
if "id" in cve_detail:
self.cve = cve_detail["id"]
if "base_score" in cve_detail:
self.score = cve_detail["base_score"]
if "severity" in cve_detail:
self.severity = cve_detail["severity"]
if "created_timestamp" in api_response:
self.created_on = api_response["created_timestamp"]
if "updated_timestamp" in api_response:
self.updated_on = api_response["updated_timestamp"]
if "status" in api_response:
self.status = api_response["status"]
host_detail = []
detail_lookup = hosts.get_device_details(ids=api_response["aid"])
if "resources" in detail_lookup["body"]:
host_detail = detail_lookup["body"]["resources"]
if host_detail:
for host in host_detail:
if "hostname" in host:
self.hostname = host["hostname"]
if "local_ip" in host:
self.local_ip = host["local_ip"]
if "os_version" in host:
self.os_version = host["os_version"]
if "service_provider" in host:
self.service_provider = host["service_provider"]
else:
# No host details are returned, show the AID for this entry
self.hostname = api_response["aid"]
try:
self.remediation = self.chunk_long_description(
spotlight.get_remediations_v2(
ids=api_response["remediation"]["ids"]
)["body"]["resources"][0]["action"],
32
)
except KeyError:
pass
if not self.remediation:
self.remediation = "Not found"
self.cve_description = self.chunk_long_description(
api_response['cve']['description'].strip(),
30
)
@staticmethod
def chunk_long_description(desc, col_width) -> str:
"""Chunks a long string by delimiting with CR based upon column length."""
desc_chunks = []
chunk = ""
for word in desc.split():
new_chunk = f"{chunk} {word.strip()}"
if len(new_chunk) >= col_width:
if new_chunk[0] == " ":
new_chunk = new_chunk[1:]
desc_chunks.append(new_chunk.strip())
chunk = ""
else:
chunk = new_chunk
delim = "\n"
desc_chunks.append(chunk.strip())
return delim.join(desc_chunks)
def _to_json(self):
"""Return the entire object in JSON format."""
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False)
def to_object(self):
"""Return the entire object."""
return json.loads(self._to_json())
def parse_command_line() -> object:
"""Parse the command line for inbound configuration parameters."""
parser = ArgumentParser(
description=__doc__,
formatter_class=RawTextHelpFormatter
)
parser.add_argument(
'-k',
'--client_id',
help='CrowdStrike Falcon API key ID',
required=True
)
parser.add_argument(
'-s',
'--client_secret',
help='CrowdStrike Falcon API key secret',
required=True
)
parser.add_argument(
'-b',
'--base_url',
help='CrowdStrike API region (us1, us2, eu1, usgov1)\n'
'NOT required unless you are using `usgov1`',
required=False
)
parser.add_argument(
'-c',
'--cve',
help='CVE IDs to search for. (ex: CVE-2022-12345,CVE-2022-54321)\n'
'Delimit with a comma (no spaces). The string CVE- is not required.\n'
'When not provided, all matches with a valid severity are returned.',
required=False
)
parser.add_argument(
'-x',
'--exclude',
help='List of columns to exclude from the display.\n'
'Delimit with a comma (no spaces).\n'
'(cve, score, severity, cve_description, created_on, updated_on,\n'
'hostname, local_ip, os_version, service_provider, remediation)',
required=False
)
parser.add_argument(
'-i',
'--include',
help='List of columns to include in the display, comma-separated.\n'
'If specified, only these columns will be displayed.\n'
'(cve, score, severity, cve_description, created_on, updated_on,\n'
'hostname, local_ip, os_version, service_provider, remediation)',
required=False
)
parser.add_argument(
'-f',
'--format',
help='Table format to use for display.\n'
'(plain, simple, github, grid, fancy_grid, pipe, orgtbl, jira, presto, \n'
'pretty, psql, rst, mediawiki, moinmoin, youtrack, html, unsafehtml, \n'
'latext, latex_raw, latex_booktabs, latex_longtable, textile, tsv)',
required=False
)
parser.add_argument(
'-o',
'--sort',
help='Sort results by display column.\n'
"(cve, score, severity, cve_description, created_on, updated_on,\n"
"hostname, local_ip, os_version, service_provider, remediation, status)",
required=False
)
parser.add_argument(
'-r',
'--reverse',
help='Reverse the sort direction.',
action="store_true",
required=False
)
parser.add_argument(
'-p',
'--hide_progress',
help='Hide progress indicator as data is retrieved.',
action="store_false",
required=False
)
parser.add_argument(
'-d',
'--deduplicate',
help='Remove duplicate entries based on hostname and local_ip.',
action="store_true",
required=False
)
return parser.parse_args()
def inform(msg: str):
"""Provide informational updates to the user as the program progresses."""
if PROGRESS:
print(f"\r{' ' * 80}\r{msg}", end='', flush=True)
def get_spotlight_matches(cves: list) -> list:
"""Retrieve a list of matches to the CVEs specified."""
# Unspecified searches return all with a severity
filter_string = "cve.severity:!'UNKNOWN'"
if cves:
filter_string = f"cve.id:{cves}"
returned = spotlight.query_vulnerabilities(filter=filter_string)
if returned["status_code"] >= 400:
raise SystemExit(returned["body"]["errors"][0]["message"])
return returned["body"]["resources"]
def remove_exclusions(resultset: dict) -> dict:
"""Remove requested columns from the table display."""
if INCLUDE:
return [{key: result[key] for key in INCLUDE} for result in resultset]
for result in resultset:
for exclusion in EXCLUDE:
del result[exclusion]
return resultset
def get_match_details(match_list: list) -> list:
"""Retrieve details for individual matches to the specified CVEs."""
returned = []
seen = set()
inform("[ Retrieve matches ]")
match_results = spotlight.get_vulnerabilities(ids=match_list)
if match_results["status_code"] >= 400:
raise SystemExit(match_results["body"]["errors"][0]["message"])
for result in match_results["body"]["resources"]:
row = SpotlightCVEMatch(result).to_object()
if args.deduplicate:
unique_id = (row['hostname'], row['local_ip'])
if unique_id not in seen:
seen.add(unique_id)
inform(f"[ {row['cve']} ] Found {row['hostname']}/{row['local_ip']}")
returned.append(row)
else:
inform(f"[ {row['cve']} ] Found {row['hostname']}/{row['local_ip']}")
returned.append(row)
reversing = False
if SORT_REVERSE:
reversing = True
inform("[ Results sort ]")
returned = sorted(returned, key=lambda item: item[SORT], reverse=reversing)
return returned
# Allow formats for our tabular output
TABLE_FORMATS = [
"plain", "simple", "github", "grid", "fancy_grid", "pipe", "orgtbl", "jira", "presto",
"pretty", "psql", "rst", "mediawiki", "moinmoin", "youtrack", "html", "unsafehtml",
"latex", "latex_raw", "latex_booktabs", "latex_longtable", "textile", "tsv"
]
# Consume inbound command line parameters
args = parse_command_line()
BASE = "us1"
if args.base_url:
BASE = args.base_url
CVE_LIST = []
if args.cve:
for cve in args.cve.upper().split(","):
if "CVE-" not in cve:
CVE_LIST.append(f"CVE-{cve}")
else:
CVE_LIST.append(cve)
EXCLUDE = []
if args.exclude:
EXCLUDE = args.exclude.split(",")
INCLUDE = []
if args.include:
INCLUDE = args.include.split(",")
TABLE_FORMAT = "fancy_grid"
if args.format:
table_format = args.format.strip().lower()
if table_format in TABLE_FORMATS:
TABLE_FORMAT = table_format
SORT = "created_on"
if args.sort:
sort_types = ["cve", "score", "severity", "cve_description", "created_on", "updated_on",
"hostname", "local_ip", "os_version", "service_provider", "remediation", "status"
]
sort_type = args.sort.strip().lower()
if sort_type in sort_types:
SORT = sort_type
SORT_REVERSE = args.reverse
PROGRESS = args.hide_progress
# Connect to the API and create instances of the SpotlightVulnerabilities and Hosts Service Classes
auth = OAuth2(client_id=args.client_id,
client_secret=args.client_secret,
base_url=BASE
)
spotlight = SpotlightVulnerabilities(auth_object=auth)
hosts = Hosts(auth_object=auth)
# Headers used for our results display
HEADERS = {
"cve": "CVE",
"score": "Score",
"severity": "Severity",
"cve_description": "Description",
"created_on": "Created",
"updated_on": "Updated",
"hostname": "Host",
"local_ip": "IP Address",
"os_version": "Operating System",
"service_provider": "Service Provider",
"remediation": "Remediation",
"status": "Status"
}
# Run the process
inform("[ Process startup ]")
details = get_match_details(get_spotlight_matches(CVE_LIST))
# Clear the progress message
print("\r" + " " * 80 + "\r", end='', flush=True)
# Display results
print(
tabulate(
tabular_data=remove_exclusions(details),
headers=HEADERS,
tablefmt=TABLE_FORMAT
)
)