-
Notifications
You must be signed in to change notification settings - Fork 4
/
usb.py
executable file
·176 lines (130 loc) · 4.34 KB
/
usb.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
import struct
from Peach.publisher import Publisher
from Peach.Engine.common import PeachException
try:
import usb.core
import usb.util
except ImportError:
print("Please install the following dependencies:")
if sys.platform == "darwin":
sys.exit("sudo port install libusb py27-pyusb-devel")
if sys.platform == "linux2":
sys.exit("sudo apt-get install libusb-1.0-0 python-usb")
if sys.platform == "nt":
sys.exit("PyUSB: http://sourceforge.net/apps/trac/pyusb/\r\n"
"LibUSB: http://sourceforge.net/apps/trac/libusb-win32/")
def getDevice(idVendor, idProduct):
busses = usb.busses()
for i, dev in enumerate(busses[0].devices):
if dev.idProduct == idProduct and dev.idVendor == idVendor:
return busses[0].devices[i]
def getDeviceName(handle):
name = []
for index in range(1, 3):
name.append(handle.getString(index, 32))
return " ".join(name)
def getConfiguration(dev):
return dev.configurations[0]
def getInterface(dev):
conf = getConfiguration(dev)
return conf.interfaces[0][0]
def getEndpoints(dev):
endpoints = []
for endpoint in getInterface(dev):
endpoints.append(endpoint)
return endpoints
def send_controlMsg(handle, command):
try:
#handle.controlMsg(0xA1, 1, 0)
handle.controlMsg(usb.TYPE_CLASS, 0x9, [0x01, command, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 0x200, 2000)
except usb.USBError as e:
print(e)
def send_interruptWrite(handle):
try:
handle.interruptWrite(0x81, [0] * 64, 1000)
except:
pass
def send_ctrl_transfer():
dev = usb.core.find(idVendor=0x05ac, idProduct=0x12a0)
dev.set_configuration()
dev.ctrl_transfer(0x80, 6, 0x100, 1, [0x40, 1, 3], 1000)
class USBCtrlTransfer(Publisher):
def __init__(self, idVendor, idProduct):
Publisher.__init__(self)
self.idVendor = int(idVendor, 16)
self.idProduct = int(idProduct, 16)
self.dev = None
def start(self):
#if self.dev:
# return
self.dev = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
if not self.dev:
raise PeachException("Device not found.")
try:
self.dev.set_configuration()
except usb.core.USBError as e:
raise PeachException(str(e))
def send(self, data):
try:
bmRequestType, bRequest, wValue, wIndex, wLength = struct.unpack(">BBHHH", data)
except:
pass
else:
try:
self.dev.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, wLength, timeout=1000)
except:
pass
def connect(self):
pass
def close(self):
pass
if __name__ == "__main__":
if sys.platform == "darwin":
os.system("system_profiler SPUSBDataType")
dev = getDevice(0x05ac, 0x12a0)
usbConfiguration = getConfiguration(dev)
usbInterface = getInterface(dev)
handle = dev.open()
print("\nDevice Name: {}\n".format(getDeviceName(handle)))
try:
prin("Unload current driver from interface ...")
handle.detachKernelDriver(usbInterface.interfaceNumber)
print("Done.")
except usb.USBError as e:
print("Failed!")
try:
print("Loading USB config ...")
handle.setConfiguration(usbConfiguration.value)
print("Done.")
except usb.USBError as e:
print("Failed!")
try:
print("Claiming interface ...")
handle.claimInterface(usbInterface.interfaceNumber)
print("Done.")
except usb.USBError as e:
print("Failed!")
try:
print("Set alternative interface ...")
handle.setAltInterface(usbInterface.interfaceNumber)
print("Done.")
except usb.USBError as e:
print("Failed!")
try:
print("Resetting device ...")
handle.reset()
print("Done.")
except usb.USBError as e:
print("Failed!")
# ...
try:
print("Releasing interface ...")
handle.releaseInterface()
print("done.")
except usb.USBError as e:
print("failed!")