-
Notifications
You must be signed in to change notification settings - Fork 2
/
stats.py
executable file
·92 lines (77 loc) · 2.93 KB
/
stats.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 Richard Hughes <[email protected]>
#
# SPDX-License-Identifier: GPL-2.0+
#
# pylint: disable=consider-using-f-string,missing-module-docstring,missing-class-docstring
# pylint: disable=too-few-public-methods,missing-function-docstring
import sys
import glob
import subprocess
from typing import Optional, List
class DbxEntry:
def __init__(self):
self.guid: str = None
self.type: str = None
self.checksum: str = None
def __repr__(self) -> str:
return "DbxEntry[type={}, hash={}]".format(self.type, self.checksum)
class DbxFile:
def __init__(self, filename: str):
self.filename = filename
self.entries: List[DbxEntry] = []
out = subprocess.run(
["dbxtool", "-l", "-d", filename], capture_output=True, check=True
)
for line in out.stdout.decode().split("\n"):
entry = DbxEntry()
try:
_, entry.guid, entry.type, entry.checksum = line.split(":")[1].split(
" "
)
if entry.type == "{sha256}":
entry.type = "SHA256"
if entry.type == "{x509_cert}":
entry.type = "X509CT"
except IndexError:
pass
else:
self.entries.append(entry)
def find_entry_from_hash(self, checksum: str) -> Optional[DbxEntry]:
for entry in self.entries:
if checksum == entry.checksum:
return entry
return None
def __repr__(self) -> str:
return "DbxFile[{}]".format(", ".join(str(entry) for entry in self.entries))
def print_stats() -> None:
for arch in ["ia32", "aa64", "x64"]:
dbxfiles: List[DbxFile] = []
for filename in sorted(glob.glob("DBXUpdate*.{}.bin".format(arch))):
dbxfiles.append(DbxFile(filename))
for idx, dbxfile_new in enumerate(dbxfiles):
if idx == 0:
dbxfile_old = None
dbxfile_fns = "origin -> {}".format(dbxfile_new.filename)
else:
dbxfile_old = dbxfiles[idx - 1]
dbxfile_new = dbxfiles[idx]
dbxfile_fns = "{} -> {}".format(
dbxfile_old.filename, dbxfile_new.filename
)
# look for added hashes
for entry in dbxfile_new.entries:
if not dbxfile_old or not dbxfile_old.find_entry_from_hash(
entry.checksum
):
print("{} ADD {}".format(dbxfile_fns.ljust(60), str(entry)))
# look for removed hashes
if dbxfile_old:
for entry in dbxfile_old.entries:
if not dbxfile_new.find_entry_from_hash(entry.checksum):
print("{} DEL {}".format(dbxfile_fns.ljust(60), str(entry)))
if __name__ == "__main__":
print_stats()
sys.exit(0)