-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_tuesday.py
More file actions
executable file
·374 lines (291 loc) · 11.7 KB
/
patch_tuesday.py
File metadata and controls
executable file
·374 lines (291 loc) · 11.7 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys, argparse
from argparse import RawTextHelpFormatter
from timeit import default_timer as timer
import json
from datetime import date
from datetime import datetime, timedelta
from collections import Counter
from icecream import ic
import httpx
import pyfiglet
from rich import print as rprint
from rich_argparse import RawTextRichHelpFormatter
from rich.console import Console
from rich.table import Table
data = {}
base = 'https://api.msrc.microsoft.com/cvrf/v3.0/cvrf/'
cisa = 'https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json'
hdrs = {'Accept': 'application/json'}
desc = f'To get [yellow]summary of security updates and product families[/yellow] from [blue]MSRC[/blue].'
note = f' [i][#8080FF]Get security updates from MSRC, formatted according to CVRF.[/#8080FF][/i]'
banner = f"""
Zzzzz |\ _,,,---,,_
/,`.-'`' -. ;-;;,_ __author__ : [ [#FFBF00][i]zd[/i][/#FFBF00] ]
|,4- ) )-,_..;\ ( `'-' __year__ : [ [magenta][i]2024.08[/i][/magenta] ]
'---''(_/--' `-'\_) __file__ : [ [i]{__file__}[/i] ]
[ {desc} ]
"""
shCh = False
verb = False
def timeit(func):
def timed(*args, **kwargs):
stime = timer()
result = func(*args, **kwargs)
etime = timer()
rprint(f'\n [*] [{date.today()}] {func.__name__}(): Completed within [{etime-stime:.4f} sec].')
return result
return timed
def count_vulns(all_vulns, mode):
base_score = 8.5
counter = 0
cves = []
for vuln in all_vulns:
cvss_tscore = 0.0
cvss_score = 0.0
cvss_sets = vuln.get('CVSSScoreSets', [])
if len(cvss_sets) > 0 :
cvss_tscore = cvss_sets[0].get('TemporalScore', 0.0)
cvss_score = cvss_sets[0].get('BaseScore', 0.0)
if cvss_score >= base_score and mode == 'critical':
counter += 1
tv = vuln.get('Title').get('Value', '<>')
cves.append(f'{vuln["CVE"]} - {cvss_score} - {cvss_tscore} - {tv}')
for threat in vuln['Threats']:
if threat['Type'] == 1:
description = threat['Description']['Value']
#if description == 'DOS:N/A':
# print(vuln.get('Title').get('Value'))
if mode == 'exploited' and 'Exploited:Yes' in description:
counter += 1
tv = vuln.get('Title').get('Value', '<>')
cves.append(f'{vuln["CVE"]} - {cvss_score} - {cvss_tscore} - {tv}')
break
if mode == 'high_likely' and 'Exploitation More Likely'.lower() in description.lower():
counter += 1
tv = vuln.get('Title').get('Value', '<>')
cves.append(f'{vuln["CVE"]} - {cvss_score} - {cvss_tscore} - {tv}')
break
return {'counter': counter, 'cves': cves}
def count_action(all_vulns):
action = 0
cves = []
for vuln in all_vulns:
for note in vuln['Notes']:
if note['Title'] == 'Customer Action Required' and note['Value'] == "Yes":
action += 1
return action
def find_patch_tuesday(month, year):
""" To get the patch Tuesday date in the format of YYYY-mm-dd """
basedate = datetime.strptime('{} 12 {} 12:00AM'.format(month, year), '%m %d %Y %I:%M%p')
dayoftheweek = basedate.weekday() + 1
if dayoftheweek > 6:
dayoftheweek = 0
return (basedate - timedelta(days=dayoftheweek) + timedelta(days=2)).date()
def fetching(year=None, month=None):
""" Fetching data from MSRC """
global verb
global base
global hdrs
global data
param = f'{year}-{month}'
url = base + param
client = httpx.Client(http2=True, headers=hdrs)
try:
#resp = httpx.get(url, headers=hdrs)
resp = client.get(url)
resp.raise_for_status()
except httpx.RequestError as exc:
rprint(f" [!] Error occurred while requesting {exc.request.url!r}.")
except httpx.HTTPStatusError as exc:
rprint(f" [!] Error response {exc.response.status_code} while requesting {exc.request.url!r}.")
if verb:
if resp.status_code == 400:
rprint(f' [!] FAIL: Bad Request on {url}')
elif resp.status_code == 401:
rprint(f' [!] FAIL: Unauthorized access to {url}')
elif resp.status_code == 404:
rprint(f' [!] FAIL: Not Found at {url}')
elif resp.status_code == 500:
rprint(f' [!] FAIL: Internal Service Error at {url}')
else:
rprint(f' [!] FAIL: Unknown error "{resp.status_code}" at {url}')
return False
else:
if resp.status_code == 200:
data = resp.json()
total = len(resp.content)
if verb:
rprint(f'\n [*] Finish fetching [{total:,} bytes] from {url}\n')
return True
finally:
client.close()
def saveJSON(filename):
""" Save the JSON file as YYYY_MM.json """
global data
ic(filename)
with open (filename, 'w') as fh:
json.dump(data, fh, indent = 4)
def showChartSummary(tuesday):
""" Display Chart and Summary """
global data
global verb
global shCh
global cisa
all_products = {}
all_microsoft = data.get('ProductTree').get('Branch', [])
all_items = all_microsoft[0].get('Items', [])
all_cisa = {}
jcisa = httpx.get(cisa)
if jcisa.status_code == httpx.codes.OK:
all_cisa = jcisa.json()
else:
rprint(f' [*] FAIL to access the JSON file at {cisa}\n')
if verb:
rprint(f'\n [*] [red]{all_cisa.get("title")}[/red] [ {all_cisa.get("catalogVersion")}/{all_cisa.get("count")} ]\n')
print(f'')
for p in all_items:
pname = p['Name']
icount = len(p['Items'])#ic(len(cve_cisa))
all_products[pname] = icount
title = data.get('DocumentTitle', 'Release not found').get('Value')
#all_vulns = data.get('Vulnerability', [])
all_vuln = data.get('Vulnerability', [])
all_vulns = [ v for v in all_vuln if v.get('Title').get('Value') ]
cve_cisa = []
for vuln in all_cisa['vulnerabilities']:
cve_cisa.append(vuln['cveID'])
#ic(len(cve_cisa))
cve_msrc = []
for vuln in all_vulns:
cve_msrc.append(vuln["CVE"])
#ic(len(cve_msrc))
mark0 = "▏"
mark1 = "▇"
max_length = 50
Header1 = f' Microsoft Patch Tuesday - By [blue]MSRC[/blue]'
Header2 = f' << [yellow]{title}[/yellow] [ [green]{tuesday}[/green] ] >>'
rprint(Header1)
rprint('='*len(Header1))
rprint(Header2)
print(f'')
print(f'')
rprint(f' [+] Vulnerabilities : [ {len(all_vulns):>3} ]')
critical = count_vulns(all_vulns, 'critical')
rprint(f'\t[-] High_Severity : [ [yellow]{critical["counter"]:>3}[/yellow] ]')
high_likely = count_vulns(all_vulns, 'high_likely')
rprint(f'\t[-] High_likelihood : [ [yellow]{high_likely["counter"]:>3}[/yellow] ]')
exploited = count_vulns(all_vulns, 'exploited')
rprint(f'\t[-] Exploited in_wild : [ [yellow]{exploited["counter"]:>3}[/yellow] ]')
actions = count_action(all_vulns)
rprint(f'\t[-] Action_required : [ [yellow]{actions:>3}[/yellow] ]')
cisa_kev = sum(x in cve_msrc for x in cve_cisa)
rprint(f'\t[-] Found in CISA_KEV : [ [red]{cisa_kev:>3}[/red] ]')
vcat = {}
vcat = {'c': critical, 'h': high_likely, 'e': exploited}
if verb:
print(f'')
for c,d in vcat.items():
if d.get('counter') == 0:
continue
table = Table(title='')
if d.get('counter') != 0 and c == 'c':
title = f'High_Severity/[yellow]{d.get("counter")}[/yellow]'
table = Table(title=title)
if d.get('counter') != 0 and c == 'h':
title = f'High_Likelihood/[yellow]{d.get("counter")}[/yellow]'
table = Table(title=title)
if d.get('counter') != 0 and c == 'e':
title = f'Exploited_in_Wild/[yellow]{d.get("counter")}[/yellow]'
table = Table(title=title)
table.add_column('CVE', style='red', no_wrap=True)
#table.add_column('CVSS_Base', justify='center', style='cyan')
#table.add_column('CVSS_Temporal', justify='center', style='magenta')
table.add_column('CVSS_Base/Temp', justify='center', style='magenta')
table.add_column('Title_Value')
for cve in d['cves']:
cv,vv,pv,tv = cve.split(' - ', 3)
if cv in cve_cisa:
cvss2 = f'B:{vv}/T:{pv} [K]'
else:
cvss2 = f'B:{vv}/T:{pv}'
#table.add_row(cv,vv,pv,tv)
table.add_row(cv,cvss2,tv)
console = Console()
console.print(table)
print(f'')
print(f'')
i = 1
max_value = sum(all_products.values())
rprint(f' [+] Product Families ({len(all_products)})')
for p in sorted(all_products, key=all_products.get, reverse=True):
if shCh:
c = round(all_products[p] / max_value * max_length)
if c >= 1:
rprint(f'{"":>4s}{p:>26s} [blue]{mark1*c}[/blue] {all_products[p]}')
else:
rprint(f'{"":>4s}{p:>26s} [blue]{mark0}[/blue] {all_products[p]}')
else:
rprint(f'\t[{i:>2}] {p:>20s} : [green]{all_products[p]}[/green]')
i += 1
print(f'')
docTrack = data.get('DocumentTracking')
doc_history = docTrack.get('RevisionHistory', [])
doc_revision = doc_history[0]['Number']
doc_value = doc_history[0]['Description']['Value']
doc_initial = docTrack['InitialReleaseDate']
doc_current = docTrack['CurrentReleaseDate']
rprint(f' [*] "[blue]{doc_value}[/blue]" (Rev {doc_revision})')
rprint(f'\t[-] Initial Release date: [yellow]{doc_initial}[/yellow]')
rprint(f'\t[-] Current Release date: [green]{doc_current}[/green]')
def usage():
""" usage() function """
parser = argparse.ArgumentParser(description=banner, formatter_class=RawTextRichHelpFormatter, epilog=note)
parser.add_argument('-c', action='store_true', help='show chart output')
parser.add_argument('-j', action='store_true', help='save the JSON file')
parser.add_argument('-k', dest='cvrf', metavar='<YYYY-mmm>', help="Date string for the report query in format YYYY-mmm: <2024-apr>")
parser.add_argument('-v', action='store_true', help='verbose output')
return parser.parse_args()
@timeit
def main():
""" main() function """
global verb
global base
global shCh
args = usage()
verb = True if args.v else False
shCh = True if args.c else False
print(f'')
word = pyfiglet.figlet_format("Patch Tuesday", font="rectangles")
rprint(f'[blue]{word}[/blue]')
tuesday = "error"
filename = ''
if args.cvrf:
cvrf = args.cvrf
yyyy, mmm = cvrf.split('-')
date_obj = datetime.strptime(cvrf, "%Y-%b").date()
m = date_obj.month
y = date_obj.year
tuesday = find_patch_tuesday(m, y)
filename = f'{y}_{m:02}.json'
else:
m = date.today().month
y = date.today().year
tuesday = find_patch_tuesday(m, y)
mmm = date.today().strftime("%h").lower()
yyyy = date.today().year
filename = f'{y}_{m:02}.json'
#ic(tuesday)
if not fetching(yyyy, mmm):
return
else:
#ic(len(data))
if args.j:
saveJSON(filename)
else:
showChartSummary(tuesday)
print(f'')
return
if __name__ == "__main__":
main()