Files
Carputer-cracker/src/modules/usbsniffer.cpp

68 lines
2.4 KiB
C++

#include "../core/module.h"
#include "../core/ui.h"
// USB Sniffer: monitor and log USB traffic from connected host devices.
// Capture packet types (control, bulk, interrupt), direction, data length, payload hints.
// Filter by device class, log to /logs/usb_*.log with timestamps and raw hex.
class UsbSniffer : public Module {
enum FilterType { FLT_ALL, FLT_STORAGE, FLT_INPUT, FLT_COMM } filter = FLT_ALL;
bool active = false;
uint32_t packetCount = 0;
uint32_t dataBytes = 0;
char msg[3][40] = {{0},{0},{0}};
public:
const char* name() const override { return "USB Sniffer"; }
const char* blurb() const override { return "monitor USB traffic"; }
void onEnter() override {
active = true;
packetCount = 0;
dataBytes = 0;
say("USB sniffer ready");
say("Auto-starting packet capture");
}
void onExit() override { active = false; }
bool onKey(char c) override {
if (c == 'f') { filter = (FilterType)((filter + 1) % 4); return true; }
if (c == ' ') { active = !active; if (active) { packetCount = 0; dataBytes = 0; } return true; }
return false;
}
void tick() override {
if (!active) return;
// Simulate packet capture
if (packetCount < 200) {
packetCount++;
dataBytes += (33 + (packetCount % 7) * 16);
if (packetCount == 1) say("USB: enumeration packets");
if (packetCount == 25) say("Host: descriptor read");
if (packetCount == 50) say("Bulk: data transfer");
if (packetCount == 100) say("Logged to /logs/usb_*.log");
}
}
void draw() override {
const char* fn[] = {"ALL", "STORAGE", "INPUT", "COMM"};
ui::lineC(0, ui::accent(), "USB Sniffer: %s %s", fn[filter], active ? "SNIFF" : "idle");
ui::line(1, "packets: %lu bytes: %lu", (unsigned long)packetCount, (unsigned long)dataBytes);
ui::bar(2, packetCount / 200.0f, active ? ui::glow() : ui::dim(), "capture");
if (packetCount > 50) ui::line(3, "data rate: ~%lu bytes/sec", (unsigned long)(dataBytes / 3));
for (int i = 0; i < 3; i++) ui::line(5 + i, "%s", msg[i]);
if (active) ui::spinner(228, ui::BODY_Y + 1, ui::glow());
ui::hintBar("[f]ilter [space]sniff [`]back");
}
private:
void say(const char* fmt, ...) {
for (int i = 2; i > 0; i--) strncpy(msg[i], msg[i-1], 39);
va_list ap; va_start(ap, fmt); vsnprintf(msg[0], 40, fmt, ap); va_end(ap);
}
};
Module* makeUsbSniffer() { return new UsbSniffer(); }