-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhw_check.py
More file actions
481 lines (401 loc) · 17.2 KB
/
hw_check.py
File metadata and controls
481 lines (401 loc) · 17.2 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/usr/bin/env python3
"""
Cthaeh - Hardware presence check
Queries actual PnP hardware on the Windows machine and cross-references
against drivers to determine if the hardware backing a driver is present.
Helps avoid wasting time on drivers for hardware that isn't installed
(e.g., athw8x.sys recommended as CRITICAL but no Atheros hardware exists).
Usage:
# Check hardware presence for all drivers in results
python hw_check.py --results triage_results.json
# Check a specific driver
python hw_check.py --driver athw8x.sys
# Just enumerate present hardware
python hw_check.py --list-hardware
Requires: Windows (uses PowerShell for PnP device enumeration)
Works from WSL via powershell.exe
"""
import argparse
import json
import os
import re
import subprocess
import sys
from pathlib import Path
# DriverStore FileRepository default path
DRIVERSTORE_PATH = r"C:\Windows\System32\DriverStore\FileRepository"
def _get_powershell():
"""Find the right PowerShell executable (works from WSL and native Windows)."""
if sys.platform == "win32":
return "powershell"
# WSL: powershell.exe is on PATH if interop is enabled
for ps in ["powershell.exe", "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"]:
try:
result = subprocess.run(
[ps, "-NoProfile", "-Command", "echo ok"],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0 and "ok" in result.stdout:
return ps
except (FileNotFoundError, subprocess.TimeoutExpired):
continue
return None
def enumerate_hardware(powershell_cmd=None):
"""Query PnP devices present on the system.
Returns:
dict with:
- hardware_ids: set of all hardware IDs (e.g., 'PCI\\VEN_8086&DEV_2723')
- devices: list of dicts with friendly_name, instance_id, hardware_ids, status
"""
if powershell_cmd is None:
powershell_cmd = _get_powershell()
if powershell_cmd is None:
print("WARNING: PowerShell not available. Hardware check requires Windows.")
return None
# Get PnP devices with their hardware IDs
ps_script = (
"Get-PnpDevice -Status OK -ErrorAction SilentlyContinue | "
"Select-Object FriendlyName, InstanceId, Class | "
"ConvertTo-Json -Depth 3"
)
try:
result = subprocess.run(
[powershell_cmd, "-NoProfile", "-Command", ps_script],
capture_output=True, text=True, timeout=30
)
if result.returncode != 0:
print(f"WARNING: PnP enumeration failed: {result.stderr.strip()}")
return None
devices_raw = json.loads(result.stdout)
if isinstance(devices_raw, dict):
devices_raw = [devices_raw]
except subprocess.TimeoutExpired:
print("WARNING: PnP enumeration timed out")
return None
except json.JSONDecodeError as e:
print(f"WARNING: Failed to parse PnP output: {e}")
return None
# Now get hardware IDs for each device (separate query since Select doesn't include HardwareID)
ps_hwid_script = (
"Get-PnpDevice -Status OK -ErrorAction SilentlyContinue | "
"ForEach-Object { "
" $hwids = (Get-PnpDeviceProperty -InstanceId $_.InstanceId "
" -KeyName 'DEVPKEY_Device_HardwareIds' -ErrorAction SilentlyContinue).Data; "
" [PSCustomObject]@{ "
" InstanceId = $_.InstanceId; "
" HardwareIds = if ($hwids) { $hwids -join '|' } else { '' } "
" } "
"} | ConvertTo-Json -Depth 3"
)
hwid_map = {}
try:
result2 = subprocess.run(
[powershell_cmd, "-NoProfile", "-Command", ps_hwid_script],
capture_output=True, text=True, timeout=60
)
if result2.returncode == 0 and result2.stdout.strip():
hwid_data = json.loads(result2.stdout)
if isinstance(hwid_data, dict):
hwid_data = [hwid_data]
for item in hwid_data:
iid = item.get("InstanceId", "")
hwids = item.get("HardwareIds", "")
if iid and hwids:
hwid_map[iid.upper()] = [h.strip() for h in hwids.split("|") if h.strip()]
except (subprocess.TimeoutExpired, json.JSONDecodeError):
pass # Hardware IDs are optional enrichment
devices = []
all_hardware_ids = set()
all_instance_ids = set()
for dev in devices_raw:
instance_id = dev.get("InstanceId", "")
friendly_name = dev.get("FriendlyName", "")
dev_class = dev.get("Class", "")
hw_ids = hwid_map.get(instance_id.upper(), [])
devices.append({
"friendly_name": friendly_name,
"instance_id": instance_id,
"class": dev_class,
"hardware_ids": hw_ids,
})
all_instance_ids.add(instance_id.upper())
for hwid in hw_ids:
all_hardware_ids.add(hwid.upper())
return {
"hardware_ids": all_hardware_ids,
"instance_ids": all_instance_ids,
"devices": devices,
"device_count": len(devices),
}
def parse_inf_hardware_ids(inf_path):
"""Extract hardware IDs from a .inf file.
Looks for lines like:
%DeviceName% = Install, PCI\\VEN_14C3&DEV_0616
HKR,,HardwareID,,"USB\\VID_0B05&PID_1234"
"""
hardware_ids = set()
try:
# INF files can be UTF-16 or UTF-8
for encoding in ["utf-16", "utf-8", "latin-1"]:
try:
with open(inf_path, "r", encoding=encoding) as f:
content = f.read()
break
except (UnicodeDecodeError, UnicodeError):
continue
else:
return hardware_ids
# Match hardware ID patterns: PCI\VEN_XXXX&DEV_XXXX, USB\VID_XXXX&PID_XXXX, etc.
patterns = [
r'(?:PCI|USB|ACPI|HID|SWD|HDAUDIO|ROOT)\\[A-Z0-9_&]+',
]
for pattern in patterns:
for match in re.finditer(pattern, content, re.IGNORECASE):
hardware_ids.add(match.group(0).upper())
except Exception:
pass
return hardware_ids
def build_driver_to_inf_map(driverstore_path=DRIVERSTORE_PATH, powershell_cmd=None):
"""Map driver .sys filenames to their INF hardware IDs.
Scans DriverStore FileRepository directories. Each subdirectory typically
contains an INF + associated .sys files.
"""
driver_hw_map = {} # driver_name.lower() -> set of hardware IDs
# Convert path for WSL if needed
scan_path = driverstore_path
if sys.platform != "win32":
# WSL: convert Windows path to /mnt/c/... path
if driverstore_path.startswith("C:"):
scan_path = "/mnt/c" + driverstore_path[2:].replace("\\", "/")
elif driverstore_path.startswith("\\"):
scan_path = driverstore_path # already a UNC or similar
if not os.path.isdir(scan_path):
print(f"WARNING: DriverStore not accessible at {scan_path}")
return driver_hw_map
for direntry in os.scandir(scan_path):
if not direntry.is_dir():
continue
# Find INF files in this directory
inf_files = []
sys_files = []
try:
for f in os.scandir(direntry.path):
name_lower = f.name.lower()
if name_lower.endswith(".inf"):
inf_files.append(f.path)
elif name_lower.endswith(".sys"):
sys_files.append(f.name.lower())
except PermissionError:
continue
if not inf_files or not sys_files:
continue
# Parse hardware IDs from all INFs in this directory
all_hw_ids = set()
for inf in inf_files:
all_hw_ids |= parse_inf_hardware_ids(inf)
# Map each .sys file to these hardware IDs
for sys_name in sys_files:
if sys_name in driver_hw_map:
driver_hw_map[sys_name] |= all_hw_ids
else:
driver_hw_map[sys_name] = set(all_hw_ids)
return driver_hw_map
def check_hardware_presence(driver_names, hw_info=None, driver_hw_map=None, powershell_cmd=None):
"""Check if hardware is present for a list of drivers.
Args:
driver_names: list of driver filenames (e.g., ['athw8x.sys'])
hw_info: pre-computed hardware info from enumerate_hardware()
driver_hw_map: pre-computed driver->INF map from build_driver_to_inf_map()
Returns:
dict mapping driver_name -> {
'status': 'HARDWARE_PRESENT' | 'HARDWARE_ABSENT' | 'UNKNOWN',
'matched_device': friendly name if present,
'inf_hardware_ids': list of HW IDs from INF,
'score_adjustment': int
}
"""
if powershell_cmd is None:
powershell_cmd = _get_powershell()
if hw_info is None:
hw_info = enumerate_hardware(powershell_cmd)
if hw_info is None:
return {name: {"status": "UNKNOWN", "reason": "hardware enumeration unavailable"}
for name in driver_names}
if driver_hw_map is None:
driver_hw_map = build_driver_to_inf_map(powershell_cmd=powershell_cmd)
present_hw_ids = hw_info["hardware_ids"]
results = {}
for driver_name in driver_names:
name_lower = driver_name.lower()
inf_hw_ids = driver_hw_map.get(name_lower, set())
if not inf_hw_ids:
results[driver_name] = {
"status": "UNKNOWN",
"reason": "no INF hardware IDs found for this driver",
"inf_hardware_ids": [],
"score_adjustment": 0,
}
continue
# Check if any of the driver's hardware IDs match present hardware
matched_ids = inf_hw_ids & present_hw_ids
if matched_ids:
# Find the friendly name of the matched device
matched_device = None
for dev in hw_info["devices"]:
dev_hw_ids = {h.upper() for h in dev.get("hardware_ids", [])}
if dev_hw_ids & matched_ids:
matched_device = dev["friendly_name"]
break
results[driver_name] = {
"status": "HARDWARE_PRESENT",
"matched_device": matched_device,
"matched_hardware_ids": list(matched_ids)[:3], # top 3 for brevity
"inf_hardware_ids": list(inf_hw_ids)[:5],
"score_adjustment": 0, # no penalty when hardware is present
}
else:
results[driver_name] = {
"status": "HARDWARE_ABSENT",
"reason": "no matching PnP hardware found on this system",
"inf_hardware_ids": list(inf_hw_ids)[:5],
"score_adjustment": -20, # penalty in audit mode
}
return results
def augment_triage_results(results_path, research_mode=False, output_path=None, driverstore_path=None):
"""Augment existing triage results with hardware presence info.
Args:
results_path: path to triage_results.json
research_mode: if True, hardware_absent is informational only (no score penalty)
output_path: where to write augmented results (default: overwrite input)
driverstore_path: path to DriverStore FileRepository (default: standard Windows path)
"""
with open(results_path, "r") as f:
results = json.load(f)
driver_names = []
for r in results:
d = r.get("driver", {})
name = d.get("name", "")
if name:
driver_names.append(name)
if not driver_names:
print("No drivers found in results.")
return results
print(f"Checking hardware presence for {len(driver_names)} drivers...")
# Get hardware info and driver map (one-time cost)
powershell_cmd = _get_powershell()
hw_info = enumerate_hardware(powershell_cmd)
if hw_info is None:
print("Cannot check hardware presence (not on Windows).")
return results
print(f" Found {hw_info['device_count']} PnP devices")
store_path = driverstore_path or DRIVERSTORE_PATH
driver_hw_map = build_driver_to_inf_map(driverstore_path=store_path, powershell_cmd=powershell_cmd)
print(f" Mapped {len(driver_hw_map)} drivers to INF hardware IDs")
hw_results = check_hardware_presence(driver_names, hw_info, driver_hw_map, powershell_cmd)
# Augment results
present_count = 0
absent_count = 0
unknown_count = 0
for r in results:
d = r.get("driver", {})
name = d.get("name", "")
if name not in hw_results:
continue
hw = hw_results[name]
r["hardware_check"] = hw
if hw["status"] == "HARDWARE_PRESENT":
present_count += 1
elif hw["status"] == "HARDWARE_ABSENT":
absent_count += 1
# Apply score adjustment in audit mode
if not research_mode:
adjustment = hw.get("score_adjustment", 0)
if adjustment != 0:
r["score"] = r.get("score", 0) + adjustment
r.setdefault("findings", []).append({
"check": "hardware_presence",
"score": adjustment,
"detail": f"Hardware absent on this system ({hw.get('reason', '')})",
})
# Recalculate priority after adjustment
score = r["score"]
if score >= 250:
r["priority"] = "CRITICAL"
elif score >= 150:
r["priority"] = "HIGH"
elif score >= 75:
r["priority"] = "MEDIUM"
elif score >= 30:
r["priority"] = "LOW"
else:
r["priority"] = "SKIP"
else:
# Research mode: informational only
r.setdefault("findings", []).append({
"check": "hardware_presence",
"score": 0,
"detail": f"Hardware absent on this system (informational - research mode)",
})
else:
unknown_count += 1
# Re-sort by score
results.sort(key=lambda x: x.get("score", 0), reverse=True)
# Write output
out = output_path or results_path
with open(out, "w") as f:
json.dump(results, f, indent=2)
print(f"\nHardware presence results:")
print(f" PRESENT: {present_count} drivers (hardware found)")
print(f" ABSENT: {absent_count} drivers (no hardware)")
print(f" UNKNOWN: {unknown_count} drivers (no INF mapping)")
if not research_mode and absent_count:
print(f" Score adjustment: -{20} applied to {absent_count} absent drivers")
elif research_mode and absent_count:
print(f" Research mode: no score adjustment applied")
print(f" Results written to: {out}")
return results
def main():
parser = argparse.ArgumentParser(
description="Cthaeh - Hardware presence check for driver triage"
)
parser.add_argument("--results", help="Path to triage_results.json to augment")
parser.add_argument("--driver", help="Check a specific driver filename")
parser.add_argument("--list-hardware", action="store_true",
help="Just list present PnP hardware")
parser.add_argument("--research", action="store_true",
help="Research mode: hardware_absent is informational only (no score penalty)")
parser.add_argument("--output", help="Output path (default: overwrite input)")
parser.add_argument("--driverstore",
default=DRIVERSTORE_PATH,
help="DriverStore FileRepository path")
args = parser.parse_args()
if args.list_hardware:
hw = enumerate_hardware()
if hw is None:
print("Hardware enumeration not available (requires Windows).")
sys.exit(1)
print(f"PnP devices ({hw['device_count']}):\n")
for dev in sorted(hw["devices"], key=lambda d: d.get("class") or ""):
hw_ids = dev.get("hardware_ids", [])
hw_str = f" [{hw_ids[0]}]" if hw_ids else ""
cls = dev.get("class") or "Unknown"
name = dev.get("friendly_name") or "Unknown"
print(f" [{cls:>20s}] {name}{hw_str}")
return
if args.driver:
hw_results = check_hardware_presence([args.driver])
for name, info in hw_results.items():
print(f"\n{name}: {info['status']}")
for k, v in info.items():
if k != "status":
print(f" {k}: {v}")
return
if args.results:
augment_triage_results(
args.results, research_mode=args.research,
output_path=args.output, driverstore_path=args.driverstore,
)
return
parser.print_help()
if __name__ == "__main__":
main()