|
| 1 | +import datetime |
| 2 | +import getpass |
| 3 | +import os |
| 4 | +import platform |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from logicytics import check, log |
| 10 | + |
| 11 | + |
| 12 | +def now_iso(): |
| 13 | + return datetime.datetime.now().astimezone().isoformat() |
| 14 | + |
| 15 | + |
| 16 | +def run_cmd(cmd): |
| 17 | + log.debug(f"Running command: {cmd}") |
| 18 | + try: |
| 19 | + proc = subprocess.run(cmd, capture_output=True, text=True, timeout=30) |
| 20 | + if proc.returncode == 0: |
| 21 | + log.debug(f"Command succeeded: {cmd}") |
| 22 | + else: |
| 23 | + log.warning(f"Command returned {proc.returncode}: {cmd}") |
| 24 | + return proc.stdout.strip(), proc.stderr.strip(), proc.returncode |
| 25 | + except FileNotFoundError: |
| 26 | + log.error(f"Command not found: {cmd[0]}") |
| 27 | + return "", "not found", 127 |
| 28 | + except subprocess.TimeoutExpired: |
| 29 | + log.error(f"Command timed out: {cmd}") |
| 30 | + return "", "timeout", 124 |
| 31 | + |
| 32 | + |
| 33 | +def have(cmd_name): |
| 34 | + exists = shutil.which(cmd_name) is not None |
| 35 | + log.debug(f"Check if '{cmd_name}' exists: {exists}") |
| 36 | + return exists |
| 37 | + |
| 38 | + |
| 39 | +def get_mountvol_output(): |
| 40 | + log.info("Gathering mounted volumes via mountvol") |
| 41 | + out, err, _ = run_cmd(["mountvol"]) |
| 42 | + if not out: |
| 43 | + return err |
| 44 | + lines = out.splitlines() |
| 45 | + filtered = [] |
| 46 | + keep = False |
| 47 | + for line in lines: |
| 48 | + if line.strip().startswith("\\\\?\\Volume"): |
| 49 | + keep = True |
| 50 | + if keep: |
| 51 | + filtered.append(line) |
| 52 | + return "\n".join(filtered) |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + script_dir = Path(__file__).resolve().parent |
| 57 | + report_path = script_dir / "win_encrypted_volume_report.txt" |
| 58 | + log.info(f"Starting encrypted volume analysis, report will be saved to {report_path}") |
| 59 | + |
| 60 | + with report_path.open("w", encoding="utf-8") as f: |
| 61 | + f.write("=" * 80 + "\n") |
| 62 | + f.write("Windows Encrypted Volume Report\n") |
| 63 | + f.write("=" * 80 + "\n") |
| 64 | + f.write(f"Generated at: {now_iso()}\n") |
| 65 | + f.write(f"User: {getpass.getuser()}\n") |
| 66 | + f.write(f"IsAdmin: {check.admin()}\n") |
| 67 | + f.write(f"Hostname: {platform.node()}\n") |
| 68 | + f.write(f"Version: {platform.platform()}\n\n") |
| 69 | + |
| 70 | + # Logical drives |
| 71 | + log.info("Gathering logical volumes via wmic") |
| 72 | + f.write("Logical Volumes (wmic):\n") |
| 73 | + out, err, _ = run_cmd(["wmic", "logicaldisk", "get", |
| 74 | + "DeviceID,DriveType,FileSystem,FreeSpace,Size,VolumeName"]) |
| 75 | + f.write(out + "\n" + err + "\n\n") |
| 76 | + |
| 77 | + # Mounted volumes |
| 78 | + f.write("Mounted Volumes (mountvol):\n") |
| 79 | + f.write(get_mountvol_output() + "\n\n") |
| 80 | + |
| 81 | + # BitLocker status |
| 82 | + f.write("=" * 80 + "\nBitLocker Status\n" + "=" * 80 + "\n") |
| 83 | + if have("manage-bde"): |
| 84 | + log.info("Checking BitLocker status with manage-bde") |
| 85 | + for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": |
| 86 | + path = f"{letter}:" |
| 87 | + if os.path.exists(f"{path}\\"): |
| 88 | + out, err, _ = run_cmd(["manage-bde", "-status", path]) |
| 89 | + f.write(f"Drive {path}:\n{out}\n{err}\n\n") |
| 90 | + else: |
| 91 | + log.warning("manage-bde not found") |
| 92 | + |
| 93 | + if have("powershell"): |
| 94 | + log.info("Checking BitLocker status with PowerShell") |
| 95 | + f.write("PowerShell Get-BitLockerVolume:\n") |
| 96 | + ps_cmd = r"Get-BitLockerVolume | Format-List *" |
| 97 | + out, err, _ = run_cmd(["powershell", "-NoProfile", "-Command", ps_cmd]) |
| 98 | + f.write(out + "\n" + err + "\n\n") |
| 99 | + else: |
| 100 | + log.warning("PowerShell not available") |
| 101 | + |
| 102 | + log.info(f"Report successfully saved to {report_path}") |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments