Skip to content

Commit

Permalink
Add OS fingerprinting
Browse files Browse the repository at this point in the history
- New feature to detect the host OS
- Uses the size of the string descriptor requests for fingerprinting the OS
  • Loading branch information
RockyZeroFour committed Feb 28, 2025
1 parent 73e0056 commit c4305a8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/common/tusb_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ enum
CONTROL_STAGE_ACK
};

typedef enum
{
UNKNOWN,
WINDOWS,
LINUX,
MACOS
} host_os_t;

//--------------------------------------------------------------------+
// USB Descriptors
//--------------------------------------------------------------------+
Expand Down
54 changes: 54 additions & 0 deletions src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ typedef struct
uint8_t remote_wakeup_en : 1; // enable/disable by host
uint8_t remote_wakeup_support : 1; // configuration descriptor's attribute
uint8_t self_powered : 1; // configuration descriptor's attribute

union
{
uint8_t string_marker : 4;
struct
{
uint8_t string_lenght_255 : 1; // Saw a string descriptor request with a lenght of 255 bytes
uint8_t string_lenght_2 : 1; // Saw a string descriptor request with a lenght of 2 bytes
uint8_t string_lenght_x : 1; // Saw a string descriptor request with a different lenght
};
};
};

volatile uint8_t cfg_num; // current active configuration (0x00 is not configured)
Expand Down Expand Up @@ -1095,6 +1106,26 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
{
TU_LOG2(" String[%u]\r\n", desc_index);

// Handle OS fingerprinting but not for the language ID descriptor
if (desc_index)
{
// Log the seen characteristic for the fingerprinting
switch (p_request->wLength)
{
case 2:
_usbd_dev.string_lenght_2 = true;
break;

case 255:
_usbd_dev.string_lenght_255 = true;
break;

default:
_usbd_dev.string_lenght_x = true;
break;
}
}

// String Descriptor always uses the desc set from user
uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index, tu_le16toh(p_request->wIndex));
TU_VERIFY(desc_str);
Expand Down Expand Up @@ -1451,4 +1482,27 @@ void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr)
return;
}

host_os_t tud_guess_operation_system(void)
{
switch (_usbd_dev.string_marker)
{
case 1:
return LINUX;
break;

case 5:
return WINDOWS;
break;

case 6:
return MACOS;
break;

default:
break;
}

return UNKNOWN;
}

#endif
3 changes: 3 additions & 0 deletions src/device/usbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, vo
// Send STATUS (zero length) packet
bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request);

// Guesses the host OS due to fingerprinting
host_os_t tud_guess_operation_system(void);

//--------------------------------------------------------------------+
// Application Callbacks (WEAK is optional)
//--------------------------------------------------------------------+
Expand Down

0 comments on commit c4305a8

Please sign in to comment.