summaryrefslogtreecommitdiffstats
path: root/src/libusb-sniff.c
blob: 91f78723271bab90902263e05ad5abc59698fd95 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
    libusb-sniff.c - Library for usb sniffing nokia's flasher-3.5
    Copyright (C) 2011-2012  Pali Rohár <pali.rohar@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/* compile: gcc libusb-sniff.c -o libusb-sniff.so -W -Wall -O2 -fPIC -ldl -shared -m32 */
/* usage: sudo USBSNIFF_WAIT=1 LD_PRELOAD=./libusb-sniff.so flasher-3.5 ... */
/* usage: sudo USBSNIFF_SKIP_READ=1 USBSNIFF_SKIP_WRITE=1 LD_PRELOAD=./libusb-sniff.so flasher-3.5 ... */

/* Enable RTLD_NEXT for glibc */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <dlfcn.h>

struct usb_dev_handle;
struct libusb_device_handle;
typedef struct usb_dev_handle usb_dev_handle;
typedef struct libusb_device_handle libusb_device_handle;

static char to_ascii(char c) {

	if ( c >= 32 && c <= 126 )
		return c;
	return '.';

}

static void dump_bytes(const char * buf, size_t size) {

	size_t i, ascii_cnt = 0;
	char ascii[17] = { 0, };

	for ( i = 0; i < size; i++ ) {
		if ( i % 16 == 0 ) {
			if ( i != 0 ) {
				printf("  |%s|\n", ascii);
				ascii[0] = 0;
				ascii_cnt = 0;
			}
			printf("%04X:", (unsigned int)i);
		}
		printf(" %02X", buf[i] & 0xFF);
		ascii[ascii_cnt] = to_ascii(buf[i]);
		ascii[ascii_cnt + 1] = 0;
		ascii_cnt++;
	}

	if ( ascii[0] ) {
		if ( size % 16 )
			for ( i = 0; i < 16 - (size % 16); i++ )
				printf("   ");
		printf("  |%s|\n", ascii);
	}

}

int usb_bulk_write(usb_dev_handle * dev, int ep, const char * bytes, int size, int timeout) {

	static int (*real_usb_bulk_write)(usb_dev_handle * dev, int ep, const char * bytes, int size, int timeout) = NULL;

	if ( ! real_usb_bulk_write )
		*(void **)(&real_usb_bulk_write) = dlsym(RTLD_NEXT, "usb_bulk_write");

	if ( ! getenv("USBSNIFF_SKIP_WRITE") ) {

		printf("\n==== usb_bulk_write (ep=%d size=%d timeout=%d) ====\n", ep, size, timeout);
		dump_bytes(bytes, size);
		printf("====\n");

		if ( getenv("USBSNIFF_WAIT") ) {
			printf("Press ENTER"); fflush(stdout); getchar();
		}

	}

	return real_usb_bulk_write(dev, ep, bytes, size, timeout);

}

int usb_bulk_read(usb_dev_handle * dev, int ep, char * bytes, int size, int timeout) {

	static int (*real_usb_bulk_read)(usb_dev_handle * dev, int ep, char * bytes, int size, int timeout) = NULL;
	int ret;

	if ( ! real_usb_bulk_read )
		*(void **)(&real_usb_bulk_read) = dlsym(RTLD_NEXT, "usb_bulk_read");

	ret = real_usb_bulk_read(dev, ep, bytes, size, timeout);

	if ( ! getenv("USBSNIFF_SKIP_READ") ) {

		printf("\n==== usb_bulk_read (ep=%d size=%d timeout=%d) ret = %d ====\n", ep, size, timeout, ret);
		if ( ret > 0 ) {
			dump_bytes(bytes, ret);
			printf("====\n");
		}

		if ( getenv("USBSNIFF_WAIT") ) {
			printf("Press ENTER"); fflush(stdout); getchar();
		}

	}

	return ret;

}

int libusb_bulk_transfer(libusb_device_handle *dev, unsigned char ep, unsigned char *bytes, int size, int *actual_length, unsigned int timeout) {

	static int (*real_libusb_bulk_transfer)(libusb_device_handle *dev, unsigned char ep, unsigned char *bytes, int size, int *actual_length, unsigned int timeout) = NULL;
	int ret;

	if ( ! real_libusb_bulk_transfer )
		*(void **)(&real_libusb_bulk_transfer) = dlsym(RTLD_NEXT, "libusb_bulk_transfer");

	if ( ep == 0x81 ) {

		ret = real_libusb_bulk_transfer(dev, ep, bytes, size, actual_length, timeout);

		if ( ! getenv("USBSNIFF_SKIP_READ") ) {

			printf("\n==== usb_bulk_read (ep=%d size=%d timeout=%d) ret = %d ====\n", ep, size, timeout, (ret < 0) ? ret : *actual_length);
			if ( ret == 0 ) {
				dump_bytes((char*) bytes, *actual_length);
				printf("====\n");
			}

			if ( getenv("USBSNIFF_WAIT") ) {
				printf("Press ENTER"); fflush(stdout); getchar();
			}

		}

		return ret;

	} else {

		if ( ! getenv("USBSNIFF_SKIP_WRITE") ) {

			printf("\n==== usb_bulk_write (ep=%d size=%d timeout=%d) ====\n", ep, size, timeout);
			dump_bytes((char*) bytes, size);
			printf("====\n");

			if ( getenv("USBSNIFF_WAIT") ) {
				printf("Press ENTER"); fflush(stdout); getchar();
			}

		}

		return real_libusb_bulk_transfer(dev, ep, bytes, size, actual_length, timeout);

	}

}

int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout) {

	static int (*real_usb_control_msg)(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout) = NULL;
	int ret;

	if ( ! real_usb_control_msg )
		*(void **)(&real_usb_control_msg) = dlsym(RTLD_NEXT, "usb_control_msg");

	if ( requesttype == 64 && ! getenv("USBSNIFF_SKIP_CONTROL") ) {

		printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ====\n", requesttype, request, value, index, size, timeout);
		dump_bytes(bytes, size);
		printf("====\n");

		if ( getenv("USBSNIFF_WAIT") ) {
			printf("Press ENTER"); fflush(stdout); getchar();
		}

	}

	ret = real_usb_control_msg(dev, requesttype, request, value, index, bytes, size, timeout);

	if ( requesttype != 64 && ! getenv("USBSNIFF_SKIP_CONTROL") ) {

		printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ret = %d ====\n", requesttype, request, value, index, size, timeout, ret);
		if ( ret > 0 ) {
			dump_bytes(bytes, ret);
			printf("====\n");
		}

		if ( getenv("USBSNIFF_WAIT") ) {
			printf("Press ENTER"); fflush(stdout); getchar();
		}

	}

	return ret;

}

int libusb_control_transfer(libusb_device_handle *dev, uint8_t requesttype, uint8_t request, uint16_t value, uint16_t index, unsigned char *bytes, uint16_t size, unsigned int timeout) {

	static int (*real_libusb_control_transfer)(libusb_device_handle *dev, uint8_t requesttype, uint8_t request, uint16_t value, uint16_t index, unsigned char *bytes, uint16_t size, unsigned int timeout) = NULL;
	int ret;

	if ( ! real_libusb_control_transfer )
		*(void **)(&real_libusb_control_transfer) = dlsym(RTLD_NEXT, "libusb_control_transfer");

	if ( requesttype == 64 && ! getenv("USBSNIFF_SKIP_CONTROL") ) {

		printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ====\n", (int)requesttype, (int)request, (int)value, (int)index, (int)size, (int)timeout);
		dump_bytes((char*) bytes, size);
		printf("====\n");

		if ( getenv("USBSNIFF_WAIT") ) {
			printf("Press ENTER"); fflush(stdout); getchar();
		}

	}

	ret = real_libusb_control_transfer(dev, requesttype, request, value, index, bytes, size, timeout);

	if ( requesttype != 64 && ! getenv("USBSNIFF_SKIP_CONTROL") ) {

		printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ret = %d ====\n", (int)requesttype, (int)request, (int)value, (int)index, (int)size, (int)timeout, ret);
		if ( ret > 0 ) {
			dump_bytes((char*) bytes, ret);
			printf("====\n");
		}

		if ( getenv("USBSNIFF_WAIT") ) {
			printf("Press ENTER"); fflush(stdout); getchar();
		}

	}

	return ret;

}

int usb_set_configuration(usb_dev_handle *dev, int configuration) {

	static int (*real_usb_set_configuration)(usb_dev_handle *dev, int configuration) = NULL;

	if ( ! real_usb_set_configuration )
		*(void **)(&real_usb_set_configuration) = dlsym(RTLD_NEXT, "usb_set_configuration");

	printf("\n==== usb_set_configuration (configuration=%d) ====\n", configuration);

	return real_usb_set_configuration(dev, configuration);

}

int libusb_set_configuration(libusb_device_handle *dev, int configuration) {

	static int (*real_usb_set_configuration)(libusb_device_handle *dev, int configuration) = NULL;

	if ( ! real_usb_set_configuration )
		*(void **)(&real_usb_set_configuration) = dlsym(RTLD_NEXT, "libusb_set_configuration");

	printf("\n==== usb_set_configuration (configuration=%d) ====\n", configuration);

	return real_usb_set_configuration(dev, configuration);

}

int usb_claim_interface(usb_dev_handle *dev, int interface) {

	static int (*real_usb_claim_interface)(usb_dev_handle *dev, int interface) = NULL;

	if ( ! real_usb_claim_interface )
		*(void **)(&real_usb_claim_interface) = dlsym(RTLD_NEXT, "usb_claim_interface");

	printf("\n==== usb_claim_interface (interface=%d) ====\n", interface);

	return real_usb_claim_interface(dev, interface);

}

int libusb_claim_interface(libusb_device_handle *dev, int interface) {

	static int (*real_usb_claim_interface)(libusb_device_handle *dev, int interface) = NULL;

	if ( ! real_usb_claim_interface )
		*(void **)(&real_usb_claim_interface) = dlsym(RTLD_NEXT, "libusb_claim_interface");

	printf("\n==== usb_claim_interface (interface=%d) ====\n", interface);

	return real_usb_claim_interface(dev, interface);

}

int usb_set_altinterface(usb_dev_handle *dev, int alternate) {

	static int (*real_usb_set_altinterface)(usb_dev_handle *dev, int alternate) = NULL;

	if ( ! real_usb_set_altinterface )
		*(void **)(&real_usb_set_altinterface) = dlsym(RTLD_NEXT, "usb_set_altinterface");

	printf("\n==== usb_set_altinterface (alternate=%d) ====\n", alternate);

	return real_usb_set_altinterface(dev, alternate);

}

int libusb_set_interface_alt_setting(libusb_device_handle *dev, int interface, int alternate) {

	static int (*real_usb_set_altinterface)(libusb_device_handle *dev, int interface, int alternate) = NULL;

	if ( ! real_usb_set_altinterface )
		*(void **)(&real_usb_set_altinterface) = dlsym(RTLD_NEXT, "libusb_set_interface_alt_setting");

	printf("\n==== usb_set_altinterface (alternate=%d) ====\n", alternate);

	return real_usb_set_altinterface(dev, interface, alternate);

}