-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.c
86 lines (69 loc) · 2.11 KB
/
hook.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
#include <stddef.h>
// that's what the baddest do!
#include "libc.c"
#include "global_obj.h"
#define SRAM_BASE 0x62000000
#define MAX_OUT_LEN 0x40
enum {
kAESKeyUser = 0, // probably
kAESKeyGID = 1,
kAESKeyUID = 2
};
static void (*usb_shutdown)() = (void *)0x20008DE0;
static void (*reboot)() __attribute__((noreturn)) = (void *)0x2000834C;
static void (*aes_encrypt)(void *buf, size_t len, int key, void *user_key, void *user_iv) = (void *)0x20000C80;
static void (*aes_decrypt)(void *buf, size_t len, int key, void *user_key, void *user_iv) = (void *)0x20000BA4;
static void (*prepare_and_jump)(uintptr_t addr) __attribute__((noreturn)) = (void *)0x200077EC;
typedef enum {
kHookCommandReset = 'rest',
kHookCommandDump = 'dump',
kHookCommandAESEncrypt = 'aese',
kHookCommandAESDecrypt = 'aesd',
} hook_cmd_t;
struct cmd {
hook_cmd_t cmd;
uint32_t args[];
};
#define RESET_VECTOR 0xEA00000A
int hook(void **state) {
struct cmd *cmd = *state;
switch (cmd->cmd) {
case kHookCommandReset: {
usb_shutdown();
reboot();
}
case kHookCommandDump: {
void *addr = (void *)cmd->args[0];
size_t len = cmd->args[1];
if (len > MAX_OUT_LEN) {
return -1;
}
memcpy(*state, addr, len);
break;
}
case kHookCommandAESEncrypt:
case kHookCommandAESDecrypt: {
size_t len = cmd->args[0];
int key = cmd->args[1];
void *iv = &cmd->args[2];
void *buf = &cmd->args[6];
if (len > MAX_OUT_LEN) {
return -1;
}
if (cmd->cmd == kHookCommandAESDecrypt) {
aes_decrypt(buf, len, key, NULL, iv);
} else {
aes_encrypt(buf, len, key, NULL, iv);
}
memcpy(*state, buf, len);
break;
}
default: {
if (*(uint32_t *)SRAM_BASE == RESET_VECTOR) {
usb_shutdown();
prepare_and_jump(SRAM_BASE);
}
}
}
return 0;
}