-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathhid.py
132 lines (109 loc) · 4.87 KB
/
hid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import random
import datetime
from USBIP import BaseStucture, USBDevice, InterfaceDescriptor, DeviceConfigurations, EndPoint, USBContainer
# Emulating USB mouse
# HID Configuration
class HIDClass(BaseStucture):
_fields_ = [
('bLength', 'B', 9),
('bDescriptorType', 'B', 0x21), # HID
('bcdHID', 'H'),
('bCountryCode', 'B'),
('bNumDescriptors', 'B'),
('bDescriptprType2', 'B'),
('wDescriptionLength', 'H'),
]
hid_class = HIDClass(bcdHID=0x0100, # Mouse
bCountryCode=0x0,
bNumDescriptors=0x1,
bDescriptprType2=0x22, # Report
wDescriptionLength=0x3400) # Little endian
interface_d = InterfaceDescriptor(bAlternateSetting=0,
bNumEndpoints=1,
bInterfaceClass=3, # class HID
bInterfaceSubClass=1,
bInterfaceProtocol=2,
iInterface=0)
end_point = EndPoint(bEndpointAddress=0x81,
bmAttributes=0x3,
wMaxPacketSize=8000, # Little endian
bInterval=0xFF) # interval to report
configuration = DeviceConfigurations(wTotalLength=0x2200,
bNumInterfaces=0x1,
bConfigurationValue=0x1,
iConfiguration=0x0, # No string
bmAttributes=0x80, # valid self powered
bMaxPower=50) # 100 mah current
interface_d.descriptions = [hid_class] # Supports only one description
interface_d.endpoints = [end_point] # Supports only one endpoint
configuration.interfaces = [interface_d] # Supports only one interface
class USBHID(USBDevice):
vendorID = 0x0627
productID = 0x0
bcdDevice = 0x0
bcdUSB = 0x0
bNumConfigurations = 0x1
bNumInterfaces = 0x1
bConfigurationValue = 0x1
configurations = []
bDeviceClass = 0x0
bDeviceSubClass = 0x0
bDeviceProtocol = 0x0
configurations = [configuration] # Supports only one configuration
def __init__(self):
USBDevice.__init__(self)
self.start_time = datetime.datetime.now()
def generate_mouse_report(self):
arr = [0x05, 0x01, # Usage Page (Generic Desktop)
0x09, 0x02, # Usage (Mouse)
0xa1, 0x01, # Collection (Application)
0x09, 0x01, # Usage (Pointer)
0xa1, 0x00, # Collection (Physical)
0x05, 0x09, # Usage Page (Button)
0x19, 0x01, # Usage Minimum (1)
0x29, 0x03, # Usage Maximum (3)
0x15, 0x00, # Logical Minimum (0)
0x25, 0x01, # Logical Maximum (1)
0x95, 0x03, # Report Count (3)
0x75, 0x01, # Report Size (1)
0x81, 0x02, # Input (Data, Variable, Absolute)
0x95, 0x01, # Report Count (1)
0x75, 0x05, # Report Size (5)
0x81, 0x01, # Input (Constant)
0x05, 0x01, # Usage Page (Generic Desktop)
0x09, 0x30, # Usage (X)
0x09, 0x31, # Usage (Y)
0x09, 0x38, # Usage (Wheel)
0x15, 0x81, # Logical Minimum (-0x7f)
0x25, 0x7f, # Logical Maximum (0x7f)
0x75, 0x08, # Report Size (8)
0x95, 0x03, # Report Count (3)
0x81, 0x06, # Input (Data, Variable, Relative)
0xc0, # End Collection
0xc0] # End Collection
return_val = ''
for val in arr:
return_val+=chr(val)
return return_val
def handle_data(self, usb_req):
# Sending random mouse data
# Send data only for 5 seconds
if (datetime.datetime.now() - self.start_time).seconds < 5:
return_val = chr(0x0) + chr(random.randint(1, 10)) + chr(random.randint(1, 10)) + chr(random.randint(1, 10))
self.send_usb_req(usb_req, return_val)
def handle_unknown_control(self, control_req, usb_req):
if control_req.bmRequestType == 0x81:
if control_req.bRequest == 0x6: # Get Descriptor
if control_req.wValue == 0x22: # send initial report
print 'send initial report'
self.send_usb_req(usb_req, self.generate_mouse_report())
if control_req.bmRequestType == 0x21: # Host Request
if control_req.bRequest == 0x0a: # set idle
print 'Idle'
# Idle
pass
usb_Dev = USBHID()
usb_container = USBContainer()
usb_container.add_usb_device(usb_Dev) # Supports only one device!
usb_container.run()
# Run in cmd: usbip.exe -a 127.0.0.1 "1-1"