-
Notifications
You must be signed in to change notification settings - Fork 99
/
img4test.c
219 lines (199 loc) · 5.82 KB
/
img4test.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
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "libvfs/vfs.h"
#define FOURCC(tag) (unsigned char)((tag) >> 24), (unsigned char)((tag) >> 16), (unsigned char)((tag) >> 8), (unsigned char)(tag)
static int
str2hex(int buflen, unsigned char *buf, const char *str)
{
unsigned char *ptr = buf;
int seq = -1;
while (buflen > 0) {
int nibble = *str++;
if (nibble >= '0' && nibble <= '9') {
nibble -= '0';
} else {
nibble |= 0x20;
if (nibble < 'a' || nibble > 'f') {
break;
}
nibble -= 'a' - 10;
}
if (seq >= 0) {
*buf++ = (seq << 4) | nibble;
buflen--;
seq = -1;
} else {
seq = nibble;
}
}
return buf - ptr;
}
static int
write_file(const char *name, void *buf, size_t size)
{
FHANDLE out = file_open(name, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (!out) {
return -1;
}
out->write(out, buf, size);
return out->close(out);
}
int
main(int argc, char **argv)
{
FHANDLE fd;
FHANDLE in;
size_t size;
uint64_t nonce;
unsigned type;
unsigned char *buf;
size_t sz;
int rv;
unsigned char ivkey[16 + 32];
str2hex(16 + 32, ivkey, "a6ff60f2fcf3cdcaaf735e1683418ff56828540cd92ac15f3144ed4dc9d5bcb34c01cc8154bc22c3658d82b6c439340b");
#if 0
buf = calloc(1, 4);
fd = lzss_reopen(enc_reopen(sub_reopen(file_open("kc_iPhone6,1_9.0_13A344.bin", O_RDONLY), 51, -1/*11062960*/), ivkey, ivkey + 16));
if (fd) {
sz = fd->read(fd, buf, 4);
printf("%zd: %02x %02x %02x %02x\n", sz, buf[0], buf[1], buf[2], buf[3]);
fd->close(fd);
}
return 0;
#endif
//FHANDLE orig;
//fd = img4_reopen(orig = memory_open_from_file("kc_iPhone6,1_9.0_13A344.bin", O_RDWR), ivkey, 0);
fd = img4_reopen(file_open("kc_iPhone6,1_9.0_13A344.bin", O_RDWR), ivkey, 0);
if (!fd) {
fprintf(stderr, "cannot parse\n");
return -1;
}
/* print info */
rv = fd->ioctl(fd, IOCTL_IMG4_GET_TYPE, &type);
if (rv) {
fprintf(stderr, "cannot identify\n");
fd->close(fd);
return -1;
}
printf("%c%c%c%c\n", FOURCC(type));
/* get the image: we could fd->read(fd, buf, sz) but why waste memory? */
rv = fd->ioctl(fd, IOCTL_MEM_GET_DATAPTR, &buf, &sz);
if (rv) {
fprintf(stderr, "cannot get data\n");
fd->close(fd);
return -1;
}
rv = write_file("_image", buf, sz);
if (rv) {
fprintf(stderr, "cannot create image file\n");
fd->close(fd);
return -1;
}
/* get the watchtower */
rv = fd->ioctl(fd, IOCTL_LZSS_GET_WTOWER, &buf, &sz);
if (rv == 0) {
rv = write_file("_wtower", buf, sz);
if (rv) {
fprintf(stderr, "cannot create watchtower file\n");
fd->close(fd);
return -1;
}
} else {
fprintf(stderr, "no watchtower\n");
}
/* get the manifest */
rv = fd->ioctl(fd, IOCTL_IMG4_GET_MANIFEST, &buf, &sz);
if (rv == 0) {
rv = write_file("_manifest", buf, sz);
if (rv) {
fprintf(stderr, "cannot create manifest file\n");
fd->close(fd);
return -1;
}
} else {
fprintf(stderr, "no manifest\n");
}
/* get the keybag: note that the only way to get it is by ioctl() */
rv = fd->ioctl(fd, IOCTL_IMG4_GET_KEYBAG, &buf, &sz);
if (rv == 0) {
rv = write_file("_keybag", buf, sz);
if (rv) {
fprintf(stderr, "cannot create keybag file\n");
fd->close(fd);
return -1;
}
} else {
fprintf(stderr, "no keybag\n");
}
/* get the nonce: note that the only way to get it is by ioctl() */
rv = fd->ioctl(fd, IOCTL_IMG4_GET_NONCE, &nonce);
if (rv == 0) {
printf("nonce: 0x%llx\n", nonce);
} else {
fprintf(stderr, "no nonce\n");
}
/* set the manifest */
in = file_open("manifest.im4m", O_RDONLY);
if (in) {
sz = in->length(in);
buf = malloc(sz);
if (!buf) {
fprintf(stderr, "out of memory\n");
in->close(in);
fd->close(fd);
return -1;
}
size = in->read(in, buf, sz);
in->close(in);
if (size != sz) {
fprintf(stderr, "cannot read manifest\n");
fd->close(fd);
return -1;
}
rv = fd->ioctl(fd, IOCTL_IMG4_SET_MANIFEST, buf, sz);
free(buf);
if (rv) {
fprintf(stderr, "cannot set manifest\n");
fd->close(fd);
return -1;
}
/* set the nonce */
rv = fd->ioctl(fd, IOCTL_IMG4_SET_NONCE, 0x1122334455667788);
if (rv) {
fprintf(stderr, "cannot set nonce\n");
fd->close(fd);
return -1;
}
} else {
fprintf(stderr, "cannot read manifest\n");
}
/* set new type */
rv = fd->ioctl(fd, IOCTL_IMG4_SET_TYPE, 'rkrn');
if (rv) {
fprintf(stderr, "cannot set type\n");
fd->close(fd);
return -1;
}
/* leave it decrypted */
rv = fd->ioctl(fd, IOCTL_ENC_SET_NOENC);
if (rv) {
fprintf(stderr, "failed set decrypt\n");
fd->close(fd);
return -1;
}
/* patches */
unsigned char patch_AMFI[] = { 0xE0, 0x03, 0x00, 0x32, 0xC0, 0x03, 0x5F, 0xD6 };
fd->lseek(fd, 0x6AF484, SEEK_SET);
fd->write(fd, patch_AMFI, sizeof(patch_AMFI));
unsigned char patch_MAC[] = { 0x00, 0x00, 0x80, 0x52 };
fd->lseek(fd, 0x4823CC/*0xF5595C*/, SEEK_SET);
fd->write(fd, patch_MAC, sizeof(patch_MAC));
//fd->fsync(fd);
//rv = orig->ioctl(orig, IOCTL_MEM_GET_DATAPTR, &buf, &sz);
//write_file("_output", buf, sz);
rv = fd->close(fd);
printf("%d\n", rv);
return 0;
}