2021-12-27 13:52:26 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# written by sqall
|
|
|
|
# twitter: https://twitter.com/sqall01
|
|
|
|
# blog: https://h4des.org
|
|
|
|
# github: https://github.com/sqall01
|
|
|
|
#
|
2021-12-30 20:48:17 +01:00
|
|
|
# Licensed under the MIT License.
|
2021-12-27 13:52:26 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
from typing import List
|
|
|
|
|
2022-01-05 20:16:57 +01:00
|
|
|
from lib.util import output_finding
|
2021-12-27 13:52:26 +01:00
|
|
|
|
2022-01-05 20:16:57 +01:00
|
|
|
# Read configuration.
|
2021-12-27 13:52:26 +01:00
|
|
|
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" % 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:
|
2022-01-05 20:16:57 +01:00
|
|
|
message = "Changed deb package files found.\n\n"
|
2021-12-27 13:52:26 +01:00
|
|
|
message += "\n".join(["File: %s" % x for x in changed_files])
|
|
|
|
|
2022-01-05 20:16:57 +01:00
|
|
|
output_finding(__file__, message)
|
2021-12-27 13:52:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
verify_deb_packages()
|