-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
verify_deb_packages.py
executable file
·89 lines (66 loc) · 2.11 KB
/
verify_deb_packages.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
#!/usr/bin/env python3
# written by sqall
# twitter: https://twitter.com/sqall01
# blog: https://h4des.org
# github: https://github.com/sqall01
#
# Licensed under the MIT License.
"""
Short summary:
Use `debsums` to verify the integrity of installed deb packages using /var/lib/dpkg/info/*.md5sums.
Requirements:
`debsums` installed on system
Reference:
https://www.sandflysecurity.com/blog/detecting-linux-binary-file-poisoning/
"""
import os
import sys
from typing import List
from lib.util import output_finding
# Read configuration.
try:
from config.config import ALERTR_FIFO, FROM_ADDR, TO_ADDR
from config.verify_deb_packages import ACTIVATED, DEBSUMS_EXE, FILE_WHITELIST
except:
ALERTR_FIFO = None
FROM_ADDR = None
TO_ADDR = None
DEBSUMS_EXE = "debsums"
FILE_WHITELIST = []
ACTIVATED = True
def _process_whitelist(changed_files: List[str]) -> List[str]:
if not FILE_WHITELIST:
return changed_files
new_changed_files = []
for changed_file in changed_files:
if changed_file in FILE_WHITELIST:
continue
new_changed_files.append(changed_file)
return new_changed_files
def verify_deb_packages():
# Decide where to output results.
print_output = False
if ALERTR_FIFO is None and FROM_ADDR is None and TO_ADDR is None:
print_output = True
if not ACTIVATED:
if print_output:
print("Module deactivated.")
return
fd = os.popen("%s -c 2> /dev/null" % DEBSUMS_EXE)
output_raw = fd.read().strip()
fd.close()
if output_raw != "":
changed_files = output_raw.split("\n")
changed_files = _process_whitelist(changed_files)
if changed_files:
message = "Changed deb package files found.\n\n"
message += "\n".join(["File: %s" % x for x in changed_files])
output_finding(__file__, message)
if __name__ == '__main__':
is_init_run = False
if len(sys.argv) == 2:
if sys.argv[1] == "--init":
is_init_run = True
# Script does not need to establish a state.
if not is_init_run:
verify_deb_packages()