-
Notifications
You must be signed in to change notification settings - Fork 0
/
hijack.c
175 lines (140 loc) · 4.53 KB
/
hijack.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
/*
* https://github.com/mncoppola/suterusu
*
* License: MIT
*
*/
//////////////////////////////////////////////////////////////////////////
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <asm/cacheflush.h>
#include "hijack.h"
//////////////////////////////////////////////////////////////////////////
#define HIJACK_SIZE 12
#define DEBUG_HOOK(...)
//////////////////////////////////////////////////////////////////////////
static LIST_HEAD(hooked_syms);
//////////////////////////////////////////////////////////////////////////
struct sym_hook {
void *addr;
unsigned char o_code[HIJACK_SIZE];
unsigned char n_code[HIJACK_SIZE];
struct list_head list;
};
//////////////////////////////////////////////////////////////////////////
static void cacheflush ( void *begin, unsigned long size )
{
flush_icache_range((unsigned long)begin, (unsigned long)begin + size);
}
//////////////////////////////////////////////////////////////////////////
void hijack_start ( void *target, void *new )
{
struct sym_hook *sa;
unsigned char o_code[HIJACK_SIZE], n_code[HIJACK_SIZE];
#if defined(_CONFIG_X86_)
unsigned long o_cr0;
// push $addr; ret
memcpy(n_code, "\x68\x00\x00\x00\x00\xc3", HIJACK_SIZE);
*(unsigned long *)&n_code[1] = (unsigned long)new;
#elif defined(_CONFIG_X86_64_)
unsigned long o_cr0;
// mov rax, $addr; jmp rax
memcpy(n_code, "\x48\xb8\x00\x00\x00\x00\x00\x00\x00\x00\xff\xe0", HIJACK_SIZE);
*(unsigned long *)&n_code[2] = (unsigned long)new;
#else // ARM
if ( (unsigned long)target % 4 == 0 )
{
// ldr pc, [pc, #0]; .long addr; .long addr
memcpy(n_code, "\x00\xf0\x9f\xe5\x00\x00\x00\x00\x00\x00\x00\x00", HIJACK_SIZE);
*(unsigned long *)&n_code[4] = (unsigned long)new;
*(unsigned long *)&n_code[8] = (unsigned long)new;
}
else // Thumb
{
// add r0, pc, #4; ldr r0, [r0, #0]; mov pc, r0; mov pc, r0; .long addr
memcpy(n_code, "\x01\xa0\x00\x68\x87\x46\x87\x46\x00\x00\x00\x00", HIJACK_SIZE);
*(unsigned long *)&n_code[8] = (unsigned long)new;
target--;
}
#endif
DEBUG_HOOK("Hooking function 0x%p with 0x%p\n", target, new);
memcpy(o_code, target, HIJACK_SIZE);
#if defined(_CONFIG_X86_) || defined(_CONFIG_X86_64_)
o_cr0 = disable_wp();
memcpy(target, n_code, HIJACK_SIZE);
restore_wp(o_cr0);
#else // ARM
memcpy(target, n_code, HIJACK_SIZE);
cacheflush(target, HIJACK_SIZE);
#endif
sa = kmalloc(sizeof(*sa), GFP_KERNEL);
if ( ! sa )
return;
sa->addr = target;
memcpy(sa->o_code, o_code, HIJACK_SIZE);
memcpy(sa->n_code, n_code, HIJACK_SIZE);
list_add(&sa->list, &hooked_syms);
}
void hijack_pause ( void *target )
{
struct sym_hook *sa;
DEBUG_HOOK("Pausing function hook 0x%p\n", target);
list_for_each_entry ( sa, &hooked_syms, list )
if ( target == sa->addr )
{
#if defined(_CONFIG_X86_) || defined(_CONFIG_X86_64_)
unsigned long o_cr0 = disable_wp();
memcpy(target, sa->o_code, HIJACK_SIZE);
restore_wp(o_cr0);
#else // ARM
memcpy(target, sa->o_code, HIJACK_SIZE);
cacheflush(target, HIJACK_SIZE);
#endif
}
}
void hijack_resume ( void *target )
{
struct sym_hook *sa;
DEBUG_HOOK("Resuming function hook 0x%p\n", target);
list_for_each_entry ( sa, &hooked_syms, list )
if ( target == sa->addr )
{
#if defined(_CONFIG_X86_) || defined(_CONFIG_X86_64_)
unsigned long o_cr0 = disable_wp();
memcpy(target, sa->n_code, HIJACK_SIZE);
restore_wp(o_cr0);
#else // ARM
memcpy(target, sa->n_code, HIJACK_SIZE);
cacheflush(target, HIJACK_SIZE);
#endif
}
}
void hijack_stop ( void *target )
{
struct sym_hook *sa;
DEBUG_HOOK("Unhooking function 0x%p\n", target);
list_for_each_entry ( sa, &hooked_syms, list )
if ( target == sa->addr )
{
#if defined(_CONFIG_X86_) || defined(_CONFIG_X86_64_)
unsigned long o_cr0 = disable_wp();
memcpy(target, sa->o_code, HIJACK_SIZE);
restore_wp(o_cr0);
#else // ARM
memcpy(target, sa->o_code, HIJACK_SIZE);
cacheflush(target, HIJACK_SIZE);
#endif
list_del(&sa->list);
kfree(sa);
break;
}
}
bool hijack_is_hijacked ( void *target )
{
struct sym_hook *sa;
list_for_each_entry ( sa, &hooked_syms, list )
if ( target == sa->addr )
return true;
return false;
}