forked from GreySyntax/irecovery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
irecovery.c
653 lines (423 loc) · 13.2 KB
/
irecovery.c
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/**
* iRecovery - Utility for DFU 2.0, WTF and Recovery Mode
* Copyright (C) 2008 - 2009 westbaer
*
* 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/>.
**/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <libusb-1.0/libusb.h>
#define VERSION "2.0.4"
#define LIBUSB_VERSION "1.0"
#define LIBUSB_DEBUG 0
#define CMD_LOG ".irecovery_history"
#define VENDOR_ID (int)0x05AC
#define NORM_MODE (int)0x1290
#define RECV_MODE (int)0x1281
#define WTF_MODE (int)0x1227
#define DFU_MODE (int)0x1222
#define BUF_SIZE (int)0x10000
static struct libusb_device_handle *device = NULL;
void device_connect() {
if ((device = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, RECV_MODE)) == NULL) {
if ((device = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, WTF_MODE)) == NULL) {
if ((device = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, DFU_MODE)) == NULL) {
}
}
}
}
void device_close() {
if (device != NULL) {
printf("[Device] Closing Connection.\n");
libusb_close(device);
}
}
void device_reset() {
if (device != NULL) {
printf("[Device] Reseting Connection.\n");
libusb_reset_device(device);
}
}
int device_sendcmd(char* argv[]) {
char* command = argv[0];
size_t length = strlen(command);
if (length >= 0x200) {
printf("[Device] Failed to send command (to long).");
return -1;
}
if (! libusb_control_transfer(device, 0x40, 0, 0, 0, command, (length + 1), 1000)) {
printf("[Device] Failed to send command.\r\n");
return -1;
}
return 1;
}
int device_autoboot() {
printf("[Device] Enabling auto-boot.\r\n");
char* command[3];
command[0] = "setenv auto-boot true";
command[1] = "saveenv";
command[2] = "reboot";
device_sendcmd(&command[0]);
device_sendcmd(&command[1]);
device_sendcmd(&command[2]);
return 1;
}
int device_upload(char* filename) {
FILE* file = fopen(filename, "rb");
if(file == NULL) {
printf("[Program] Unable to find file. (%s)\r\n",filename);
return 1;
}
fseek(file, 0, SEEK_END);
unsigned int len = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = malloc(len);
if (buffer == NULL) {
printf("[Program] Error allocating memory.\r\n");
fclose(file);
return 1;
}
fread(buffer, 1, len, file);
fclose(file);
int packets = len / 0x800;
if(len % 0x800)
packets++;
int last = len % 0x800;
if(!last)
last = 0x800;
int i = 0;
unsigned int sizesent=0;
char response[6];
for(i = 0; i < packets; i++) {
int size = i + 1 < packets ? 0x800 : last;
sizesent+=size;
printf("[Device] Sending packet %d of %d (0x%08x of 0x%08x bytes)/", i+1, packets, sizesent, len);
if(! libusb_control_transfer(device, 0x21, 1, i, 0, &buffer[i * 0x800], size, 1000)) {
printf("[Device] Error sending packet.\r\n");
return -1;
}
if( libusb_control_transfer(device, 0xA1, 3, 0, 0, response, 6, 1000) != 6) {
printf("[Device] Error receiving status while uploading file.\r\n");
return -1;
}
if(response[4] != 5) {
printf("[Device] Invalid status error during file upload.\r\n");
return -1;
}
printf("[Device] Upload successfull.\r\n");
}
printf("[Device] Executing file.\r\n");
libusb_control_transfer(device, 0x21, 1, i, 0, buffer, 0, 1000);
for(i = 6; i <= 8; i++) {
if(libusb_control_transfer(device, 0xA1, 3, 0, 0, response, 6, 1000) != 6) {
printf("[Device] Error receiving execution status.\r\n");
return -1;
}
if(response[4] != i) {
printf("[Device] Invalid execution status.\r\n");
return -1;
}
}
printf("[Device] Successfully executed file.\r\n");
free(buffer);
return 0;
}
int device_buffer(char* data, int len) {
int packets = len / 0x800;
if(len % 0x800) {
packets++;
}
int last = len % 0x800;
if(!last) {
last = 0x800;
}
int i = 0;
char response[6];
for(i = 0; i < packets; i++) {
int size = i + 1 < packets ? 0x800 : last;
if(! libusb_control_transfer(device, 0x21, 1, i, 0, &data[i * 0x800], size, 1000)) {
printf("[Device] Error sending packet from buffer.\r\n");
return -1;
}
if( libusb_control_transfer(device, 0xA1, 3, 0, 0, response, 6, 1000) != 6) {
printf("[Device] Error receiving status from buffer.\r\n");
return -1;
}
if(response[4] != 5) {
printf("[Device] Invalid status error from buffer.\r\n");
return -1;
}
}
libusb_control_transfer(device, 0x21, 1, i, 0, data, 0, 1000);
for(i = 6; i <= 8; i++) {
if( libusb_control_transfer(device, 0xA1, 3, 0, 0, response, 6, 1000) != 6) {
printf("[Device] Error receiving execution status from buffer.\r\n");
return -1;
}
if(response[4] != i) {
printf("[Device] Invalid execution status from buffer.\r\n");
return -1;
}
}
return 0;
}
int device_exploit(char* payload) {
if(payload != NULL) {
if(device_upload(payload) < 0) {
printf("[Device] Error uploading payload.\r\n");
return -1;
}
}
if(!libusb_control_transfer(device, 0x21, 2, 0, 0, 0, 0, 1000)) {
printf("[Device] Error sending exploit.\r\n");
return -1;
}
return 0;
}
int device_sendrawusb0xA1(char *command) {
printf("[Device] Sending raw command to 0xA1, x, 0, 0, 0, 0, 1000.\r\n", libusb_control_transfer(device, 0xA1, atoi(command), 0, 0, 0, 0, 1000));
}
int device_sendrawusb0x40(char *command) {
printf("[Device] Sending raw command to 0x40, x, 0, 0, 0, 0, 1000.\r\n", libusb_control_transfer(device, 0x40, atoi(command), 0, 0, 0, 0, 1000));
}
int device_sendrawusb0x21(char *command) {
printf("[Device] Sending raw command to 0x21, x, 0, 0, 0, 0, 1000.\r\n", libusb_control_transfer(device, 0x21, atoi(command), 0, 0, 0, 0, 1000));
}
void prog_usage() {
printf("./irecovery [args]\n");
printf("\t-a\t\tenables auto-boot and reboots the device (exit recovery loop).\r\n");
printf("\t-s\t\tstarts a shell.\r\n");
printf("\t-r\t\tusb reset.\r\n");
printf("\t-u <file>\tuploads a file.\r\n");
printf("\t-c \"command\"\tsend a single command.\r\n");
printf("\t-b <file>\truns batch commands from a file(one per line).\r\n");
printf("\t-x <file>\tuploads a file then resets the usb connection.\r\n");
printf("\t-e <file>\tupload a file then run usb exploit.\r\n");
printf("\t-x21 <command>\tsend a raw command to 0x21.\r\n");
printf("\t-x40 <command>\tsend a raw command to 0x41.\r\n");
printf("\t-xA1 <command>\tsend a raw command to 0xA1.\r\n");
printf("\r\n");
printf("== Console / Batch Commands ==\r\n");
printf("\r\n");
printf("\t/auto-boot\tenables auto-boot and reboots the device (exit recovery loop).\r\n");
printf("\t/exit\t\texit the recovery console.\r\n");
printf("\t/upload <file>\tupload a file to the device.\r\n");
printf("\t/exploit <file>\tupload a file then execute a usb exploit.\r\n");
printf("\t/batch <file>\texecute commands from a batch file.\r\n");
}
void prog_init() {
libusb_init(NULL);
device_connect();
}
void prog_exit() {
printf("\r\n");
device_close();
libusb_exit(NULL);
exit(0);
}
int prog_parse(char *command) {
char* action = strtok(strdup(command), " ");
if(! strcmp(action, "help")) {
printf("Commands:\n");
printf("\t/exit\t\t\texit from recovery console.\r\n");
printf("\t/upload <file>\t\tupload file to device.\r\n");
printf("\t/exploit [payload]\tsend usb exploit packet.\r\n");
printf("\t/batch <file>\t\texecute commands from a batch file.\r\n");
printf("\t/auto-boot\t\t\tenable auto-boot (exit recovery loop).\r\n");
} else if(! strcmp(action, "exit")) {
free(action);
return -1;
} else if(strcmp(action, "upload") == 0) {
char* filename = strtok(NULL, " ");
if(filename != NULL)
device_upload(filename);
} else if(strcmp(action, "exploit") == 0) {
char* payload = strtok(NULL, " ");
if (payload != NULL)
device_exploit(payload);
else
device_exploit(NULL);
} else if (! strcmp(action, "batch")) {
char* filename = strtok(NULL, " ");
if (filename != NULL)
prog_batch(filename);
} else if (! strcmp(action, "auto-boot")) {
device_autoboot();
}
free(action);
return 0;
}
int prog_batch(char *filename) {
//max command length
char line[0x200];
FILE* script = fopen(filename, "rb");
if (script == NULL) {
printf("[Program] Unable to find batch file.\r\n");
return -1;
}
printf("\n");
while (fgets(line, 0x200, script) != NULL) {
if(!((line[0]=='/') && (line[1]=='/'))) {
if(line[0] == '/') {
printf("[Program] Running command: %s", line);
char byte;
int offset = (strlen(line) - 1);
while(offset > 0) {
if (line[offset] == 0x0D || line[offset] == 0x0A) line[offset--] = 0x00;
else break;
};
prog_parse(&line[1]);
} else {
char *command[1];
command[0] = line;
device_sendcmd(command);
}
}
}
fclose(script);
return 0;
}
int prog_console(char* logfile) {
if(libusb_set_configuration(device, 1) < 0) {
printf("[Program] Error setting configuration.\r\n");
return -1;
}
if(libusb_claim_interface(device, 1) < 0) {
printf("[Program] Error claiming interface.\r\n");
return -1;
}
if(libusb_set_interface_alt_setting(device, 1, 1) < 0) {
printf("[Program] Error claiming alt interface.\r\n");
return -1;
}
char* buffer = malloc(BUF_SIZE);
if(buffer == NULL) {
printf("[Program] Error allocating memory.\r\n");
return -1;
}
FILE* fd = NULL;
if(logfile != NULL) {
fd = fopen(logfile, "w");
if(fd == NULL) {
printf("[Program] Unable to open log file.\r\n");
free(buffer);
return -1;
}
}
printf("[Program] Attached to Recovery Console.\r\n");
if (logfile)
printf("[Program] Output being logged to: %s.\r\n", logfile);
read_history(CMD_LOG);
while(1) {
int bytes = 0;
memset(buffer, 0, BUF_SIZE);
libusb_bulk_transfer(device, 0x81, buffer, BUF_SIZE, &bytes, 500);
if (bytes>0) {
int i;
for(i = 0; i < bytes; ++i) {
fprintf(stdout, "%c", buffer[i]);
if(fd) fprintf(fd, "%c", buffer[i]);
}
}
char *command = readline("iRecovery> ");
if (command != NULL && *command) {
add_history(command);
write_history(CMD_LOG);
if(fd) fprintf(fd, ">%s\n", command);
if (command[0] == '/' && strlen(command) > 1 && command[1] != ' ' && prog_parse(&command[1]) < 0) {
free(command);
continue;
}
device_sendcmd(&command);
char* action = strtok(strdup(command), " ");
if (! strcmp(action, "getenv")) {
char response[0x200];
libusb_control_transfer(device, 0xC0, 0, 0, 0, response, 0x200, 1000);
printf("Env: %s\r\n", response);
}
if (! strcmp(action, "reboot"))
return -1;
}
free(command);
}
free(buffer);
if(fd) fclose(fd);
libusb_release_interface(device, 1);
}
void prog_handle(int argc, char *argv[]) {
if (! strcmp(argv[1], "-a")) {
device_autoboot();
} else if (! strcmp(argv[1], "-c")) {
if (argc >= 3) {
if (argc > 3) {
char command[0x200];
int i = 2;
for (i; i < argc; i++) {
if (i > 2) strcat(command, " ");
strcat(command, argv[i]);
}
argv[2] = command;
}
device_sendcmd(&argv[2]);
}
} else if (! strcmp(argv[1], "-r")) {
device_reset();
} else if (! strcmp(argv[1], "-b")) {
prog_batch(argv[2]);
} else if (! strcmp(argv[1], "-u") || ! strcmp(argv[1], "-x")) {
if (argc == 3) {
device_upload(argv[2]);
if (! strcmp(argv[1], "-x")) {
device_reset();
}
}
} else if(! strcmp(argv[1], "-e")) {
if(argc >= 3)
device_exploit(argv[2]);
else
device_exploit(NULL);
} else if (! strcmp(argv[1], "-s")) {
if (argc >= 3) //Logfile specified
prog_console(argv[2]);
else
prog_console(NULL);
} else {
printf("[Program] Invalid program argument specified.\r\n");
prog_usage();
}
}
int main(int argc, char *argv[]) {
printf("iRecovery - Version: %s - For LIBUSB: %s\n", VERSION, LIBUSB_VERSION);
printf("by westbaer. Thanks to pod2g, tom3q, planetbeing, geohot and posixninja.\r\nRewrite by GreySyntax.\n\n");
if(argc < 2) {
prog_usage();
exit(1);
}
prog_init();
(void) signal(SIGTERM, prog_exit);
(void) signal(SIGQUIT, prog_exit);
(void) signal(SIGINT, prog_exit);
if (device == NULL) {
printf("[Device] Failed to connect, check the device is in DFU or WTF (Recovery) Mode.\r\n");
return -1;
}
printf("[Device] Connected.\n");
prog_handle(argc, argv); //Handle arguments
prog_exit();
exit(0);
}