-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot_check.py
More file actions
259 lines (215 loc) · 8.56 KB
/
Copy pathboot_check.py
File metadata and controls
259 lines (215 loc) · 8.56 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
boot_check.py - Anti Evil-Maid Attack Monitor
Original: @JusticeRage
Updated: @moscovium-mc
Detects whether your hard drive was powered on without the OS booting –
the classic signature of an evil-maid attack (drive duplication, cold-boot,
interrupted FDE prompt, etc.).
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
Installation (systemd):
1. cp boot_check.service /etc/systemd/system/
(update the ExecStart path inside it)
2. systemctl enable boot_check.service
3. apt install smartmontools dialog
4. ./boot_check.py ← run once to initialise the baseline
---------------------------------------------------------------------------
"""
import json
import os
import shutil
import subprocess
import sys
from pathlib import Path
from typing import Optional
# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------
BOOT_COUNT_FILE = Path("/root/.boot_check")
SERVICE_NAME = "boot_check.service"
# ---------------------------------------------------------------------------
# Colour helpers
# ---------------------------------------------------------------------------
RED = "\033[0;31m"
GREEN = "\033[0;32m"
CYAN = "\033[0;36m"
END = "\033[0m"
def _c(colour: str, text: str) -> str: return f"{colour}{text}{END}"
def red(t): return _c(RED, t)
def green(t): return _c(GREEN, t)
def cyan(t): return _c(CYAN, t)
def info(t): print(f"[ ] {t}")
def success(t): print(f"[{green('*')}] {green(t)}")
def error(t): print(f"[{red('!')}] {red(t)}", file=sys.stderr)
def warn(t): print(f"[{red('!')}] {red(t)}")
# ---------------------------------------------------------------------------
# Prerequisite checks
# ---------------------------------------------------------------------------
def check_prerequisites() -> None:
"""Exit with a descriptive message if the environment isn't suitable."""
if os.geteuid() != 0:
error("This script must be run as root.")
sys.exit(1)
for tool, pkg in [("smartctl", "smartmontools"), ("lsblk", "util-linux"), ("dialog", "dialog")]:
if shutil.which(tool) is None:
error(f"{tool!r} is not installed. Run: apt install {pkg}")
sys.exit(1)
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
def main() -> None:
check_prerequisites()
if not BOOT_COUNT_FILE.exists():
initialise()
else:
check_boot_count()
# ---------------------------------------------------------------------------
# Initialisation
# ---------------------------------------------------------------------------
def initialise() -> None:
"""Create the baseline power-cycle file."""
result = subprocess.run(
["systemctl", "--quiet", "is-enabled", SERVICE_NAME],
capture_output=True,
)
if result.returncode != 0:
warn(f"Boot Check is NOT enabled in systemd!\n"
f" Run: systemctl enable {SERVICE_NAME}")
return
init_data: dict[str, int] = {}
for device in get_hard_drives():
count = get_power_cycle_count(device)
if count is not None:
init_data[device] = count
else:
warn(f"Could not read power cycle count for {device} – skipping.")
BOOT_COUNT_FILE.write_text(json.dumps(init_data, indent=2))
BOOT_COUNT_FILE.chmod(0o600)
success("Boot Check initialised. Baseline saved.")
# ---------------------------------------------------------------------------
# Check
# ---------------------------------------------------------------------------
def check_boot_count() -> None:
"""Compare current power-cycle counts against the stored baseline."""
try:
state: dict[str, int] = json.loads(BOOT_COUNT_FILE.read_text())
except (json.JSONDecodeError, OSError) as exc:
error(f"Could not read state file: {exc}")
sys.exit(1)
for device in get_hard_drives():
if device not in state:
show_dialog(
f"WARNING: No baseline data for {device}.\n"
f"Delete {BOOT_COUNT_FILE} and re-run this script to reinitialise."
)
continue
current = get_power_cycle_count(device)
if current is None:
warn(f"Could not read power cycle count for {device}.")
continue
# -1 because the current boot doesn't count
suspicious_boots = current - state[device] - 1
if suspicious_boots <= 0:
state[device] = current
else:
model = get_drive_model(device) or f"/dev/{device}"
plural = "s" if suspicious_boots != 1 else ""
show_dialog(
f"⚠ EVIL MAID ALERT ⚠\n\n"
f"{model} was powered on {suspicious_boots} extra time{plural} "
f"since the last verified boot.\n\n"
f"Someone may have tampered with your drive."
)
state[device] = current
BOOT_COUNT_FILE.write_text(json.dumps(state, indent=2))
# ---------------------------------------------------------------------------
# Hardware helpers
# ---------------------------------------------------------------------------
def get_hard_drives() -> list[str]:
"""Return device names (e.g. ['sda', 'nvme0n1']) for all physical disks."""
result = subprocess.run(
["lsblk", "-d", "-J"],
capture_output=True,
text=True,
timeout=10,
)
try:
data = json.loads(result.stdout)
return [
d["name"]
for d in data.get("blockdevices", [])
if d.get("type") == "disk"
]
except (json.JSONDecodeError, KeyError):
warn("Could not parse lsblk output.")
return []
def get_drive_model(device: str) -> Optional[str]:
"""Return the human-readable model string for a device, or None."""
result = subprocess.run(
["lsblk", "-S", "-J"],
capture_output=True,
text=True,
timeout=10,
)
try:
data = json.loads(result.stdout)
for d in data.get("blockdevices", []):
if d.get("name") == device:
return d.get("model")
except (json.JSONDecodeError, KeyError):
pass
return None
def get_power_cycle_count(device: str) -> Optional[int]:
"""
Query SMART data for the Power_Cycle_Count attribute.
Returns None if it can't be determined.
"""
result = subprocess.run(
["smartctl", "-A", f"/dev/{device}"],
capture_output=True,
text=True,
timeout=15,
)
for line in result.stdout.splitlines():
if "power_cycle_count" in line.lower():
parts = line.split()
if parts:
try:
return int(parts[-1])
except ValueError:
pass
return None
# ---------------------------------------------------------------------------
# Display
# ---------------------------------------------------------------------------
def show_dialog(text: str, width: int = 60) -> None:
"""
Display a scary full-screen alert on TTY2 at boot, then restore TTY7.
Uses the dialog --msgbox trick with a red background injected via stdin.
"""
if not text:
return
# Sleep briefly so the display manager doesn't grab TTY2 back immediately
subprocess.run(["sh", "-c", "sleep 5; chvt 2"], check=False)
lines_needed = 6 + len(text) // (width - 4)
dialog_proc = subprocess.Popen(
"OLDDIALOGRC=$DIALOGRC; "
"export DIALOGRC=/dev/stdin; "
f'dialog --clear --msgbox "{text}" {lines_needed} {width}; '
"export DIALOGRC=$OLDDIALOGRC",
shell=True,
stdin=subprocess.PIPE,
)
dialog_proc.communicate(input=b"screen_color = (CYAN,RED,ON)\n")
print(f"[{red('!')}] Press [{red('CTRL+ALT+F7')}] to return to the desktop.")
subprocess.run(["chvt", "7"], check=False)
# ---------------------------------------------------------------------------
if __name__ == "__main__":
main()