Skip to content

Commit

Permalink
Acquire device list (DIS-897)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecinestpasunepipe committed Oct 22, 2024
1 parent 1fa3bde commit f864602
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions acquire/acquire.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import argparse
import ctypes
import enum
import functools
import io
Expand Down Expand Up @@ -384,6 +385,82 @@ class Netstat(Module):
EXEC_ORDER = ExecutionOrder.BOTTOM


@register_module("--devices")
@local_module
class Devices(Module):
DESC = "devices output"
EXEC_ORDER = ExecutionOrder.BOTTOM

@classmethod
def _run(cls, target: Target, cli_args: argparse.Namespace, collector: Collector) -> None:
def _get_message_for_errno(errno: int) -> str:
kernel32.FormatMessageW.argtypes = [

Check warning on line 397 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L396-L397

Added lines #L396 - L397 were not covered by tests
ctypes.wintypes.DWORD,
ctypes.wintypes.LPVOID,
ctypes.wintypes.DWORD,
ctypes.wintypes.DWORD,
ctypes.wintypes.LPVOID,
ctypes.wintypes.DWORD,
]

kernel32.FormatMessageW.restype = ctypes.wintypes.DWORD

Check warning on line 406 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L406

Added line #L406 was not covered by tests

FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200

Check warning on line 410 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L408-L410

Added lines #L408 - L410 were not covered by tests

lpMsgBuf = ctypes.c_wchar_p()
kernel32.FormatMessageW(

Check warning on line 413 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L412-L413

Added lines #L412 - L413 were not covered by tests
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
None,
errno,
0, # Language
ctypes.byref(lpMsgBuf),
0,
)
try:
message = lpMsgBuf.value
except Exception as e:
message = f"(unable to retrieve error message: {e})"

Check warning on line 424 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L421-L424

Added lines #L421 - L424 were not covered by tests
finally:
kernel32.LocalFree.argtypes = [ctypes.wintypes.HLOCAL]
kernel32.LocalFree.restype = ctypes.wintypes.HLOCAL
kernel32.LocalFree(lpMsgBuf)

Check warning on line 428 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L426-L428

Added lines #L426 - L428 were not covered by tests

return message.strip()

Check warning on line 430 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L430

Added line #L430 was not covered by tests

try:
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)

Check warning on line 433 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L432-L433

Added lines #L432 - L433 were not covered by tests

buf_size = 65536
buf = ctypes.create_string_buffer(buf_size)

Check warning on line 436 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L435-L436

Added lines #L435 - L436 were not covered by tests

kernel32.GetLastError.argtypes = []
kernel32.GetLastError.restype = ctypes.wintypes.DWORD

Check warning on line 439 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L438-L439

Added lines #L438 - L439 were not covered by tests

kernel32.QueryDosDeviceA(None, buf, buf_size)

Check warning on line 441 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L441

Added line #L441 was not covered by tests

lines = []
for line in buf.raw.split(b"\x00"):
if line.strip(b" ") == b"":
break
lines.append(line.decode("utf-8"))

Check warning on line 447 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L443-L447

Added lines #L443 - L447 were not covered by tests

if len(lines) < 1:
error_code = kernel32.GetLastError()
raise Exception(f"Last Error = {error_code} ({_get_message_for_errno(error_code)})")

Check warning on line 451 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L449-L451

Added lines #L449 - L451 were not covered by tests

collector.output.write_bytes("QueryDosDeviceA.txt", "\n".join(lines).encode("utf-8"))
collector.report.add_command_collected(cls.__name__, ["QueryDosDeviceA"])
except Exception:
collector.report.add_command_failed(cls.__name__, ["QueryDosDeviceA"])
log.error(

Check warning on line 457 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L453-L457

Added lines #L453 - L457 were not covered by tests
"- Failed to collect output from command `QueryDosDeviceA`",
exc_info=True,
)
return

Check warning on line 461 in acquire/acquire.py

View check run for this annotation

Codecov / codecov/patch

acquire/acquire.py#L461

Added line #L461 was not covered by tests


@register_module("--win-processes")
@local_module
class WinProcesses(Module):
Expand Down Expand Up @@ -2017,6 +2094,7 @@ class OSXProfile:

class VolatileProfile:
DEFAULT = [
Devices,
Netstat,
WinProcesses,
WinProcEnv,
Expand Down

0 comments on commit f864602

Please sign in to comment.