-
Notifications
You must be signed in to change notification settings - Fork 17
/
patcher.c
221 lines (178 loc) · 5.23 KB
/
patcher.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
220
#include "patcher.h"
#include "config.h"
#if DEBUG_LOG
#define dprintf printf
#else
#define dprintf(...)
#endif
#if defined(_MAC64) || defined(__LP64__)
#define __address_t mach_vm_address_t
#define __size_t mach_vm_size_t
#else
#define __address_t vm_address_t
#define __size_t vm_size_t
#endif
uint64_t findBasicAddress(int pid){
__size_t region_size = 0;
__address_t region = 0;
mach_port_t task = 0;
int ret = 0;
ret = task_for_pid(mach_task_self(),pid,&task);
if (ret != 0)
{
printf("task_for_pid() message %s!\n",mach_error_string(ret));
return 0;
}
/* Get region boundaries */
#if defined(_MAC64) || defined(__LP64__)
vm_region_basic_info_data_64_t info;
mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
vm_region_flavor_t flavor = VM_REGION_BASIC_INFO_64;
if ((ret = mach_vm_region(mach_task_self(), ®ion, ®ion_size, flavor, (vm_region_info_t)&info,
(mach_msg_type_number_t*)&info_count, (mach_port_t*)&task)) != KERN_SUCCESS)
{
printf("mach_vm_region() message %s!\n",mach_error_string(ret));
return 0;
}
#else
vm_region_basic_info_data_t info;
mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT;
vm_region_flavor_t flavor = VM_REGION_BASIC_INFO;
if ((ret = vm_region(mach_task_self(), ®ion, ®ion_size, flavor, (vm_region_info_t)&info,
(mach_msg_type_number_t*)&info_count, (mach_port_t*)&task)) != KERN_SUCCESS)
{
printf("vm_region() message %s!\n",mach_error_string(ret));
return 0;
}
#endif
return region;
}
vm_size_t readMemory(char *buf,vm_size_t len,int pid,vm_address_t address)
{
vm_size_t outSize = 0;
mach_port_t task = 0;
int ret = task_for_pid(mach_task_self(),pid,&task);
if (ret != 0)
{
printf("task_for_pid() message %s!\n",mach_error_string(ret));
return 0;
}
ret = vm_read_overwrite(task,address,len,(vm_address_t)buf,&outSize);
if (ret != 0)
{
printf("vm_read_overwrite() message %s!\n",mach_error_string(ret));
return 0;
}
return outSize;
}
int writeMemory(void *dst_ptr, void *data_ptr, uint8_t data_len)
{
mach_port_t task;
__size_t region_size = 0;
__address_t region = (vm_address_t)dst_ptr;
/* Get region boundaries */
#if defined(_MAC64) || defined(__LP64__)
vm_region_basic_info_data_64_t info;
mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
vm_region_flavor_t flavor = VM_REGION_BASIC_INFO_64;
if (mach_vm_region(mach_task_self(), ®ion, ®ion_size, flavor, (vm_region_info_t)&info, (mach_msg_type_number_t*)&info_count, (mach_port_t*)&task) != 0)
{
return 0;
}
#else
vm_region_basic_info_data_t info;
mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT;
vm_region_flavor_t flavor = VM_REGION_BASIC_INFO;
if (vm_region(mach_task_self(), ®ion, ®ion_size, flavor, (vm_region_info_t)&info, (mach_msg_type_number_t*)&info_count, (mach_port_t*)&task) != 0)
{
return 0;
}
#endif
if ((uint64_t)(dst_ptr + data_len) > region + region_size)
{
return 0;
}
/* Change memory protections to rw- */
if (vm_protect(mach_task_self(), region, region_size, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY) != KERN_SUCCESS)
{
//_LineLog();
return 0;
}
/* Actually perform the write */
memcpy(dst_ptr, data_ptr, data_len);
/* Flush CPU data cache to save write to RAM */
sys_dcache_flush(dst_ptr, sizeof(data_len));
/* Invalidate instruction cache to make the CPU read patched instructions from RAM */
sys_icache_invalidate(dst_ptr, sizeof(data_len));
/* Change memory protections back to r-x */
vm_protect(mach_task_self(), region, region_size, 0, VM_PROT_EXECUTE | VM_PROT_READ);
return 1;
}
int writeOneByte(void *dst_ptr, uint8_t data) {
return writeMemory(dst_ptr, &data, 1);
}
void print_memory(void *ptr, unsigned long len) {
unsigned char *mem_ptr = (unsigned char*)ptr;
for (int i = 0; i < len; ++i)
{
printf("%.2hhx ", mem_ptr[i]);
}
putchar('\n');
fflush(stdout);
}
int dump_memory(void *ptr, size_t len, const char* filename) {
FILE *fp;
fp = fopen(filename, "wb");
if (fp != NULL) {
fwrite(ptr, len, 1, fp);
return 1;
}
return 0;
}
unsigned char inline_hook_code[] = {
0x48, 0xB8, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, //movabsq 0x1122334455667788, %rax
0xff, 0xe0, //jmp *%rax
0xc3 // ret
};
int inline_hook(void *dst_fun, void *new_fun) {
*((uint64_t*)(inline_hook_code+2)) = (uint64_t)new_fun;
return writeMemory(dst_fun, inline_hook_code, 13);
}
extern void yourcode(int pid, uint64_t basicaddress);
static void startup() {
int pid = getpid();
mach_vm_address_t address = 0;
address = findBasicAddress(pid);
dprintf("Target pid : %d\n",pid);
dprintf("Base address : %llx\n", address);
if (address == 0)
{
printf("findBasicAddress() faild!\n");
}
yourcode(pid, (uint64_t)address);
}
static void* patcher_thread(void *p)
{
startup();
return NULL;
}
void __attribute__((constructor)) patcher_enter()
{
int err;
pthread_t ntid;
#if STARTUP_MODE
dprintf("Thread : main thread\n");
startup();
#else
dprintf("Thread : standalone thread\n");
err = pthread_create(&ntid, NULL, patcher_thread, NULL);
if (err != 0)
{
printf("Can't create patcher thread: %s\n", strerror(err));
return ;
}
#endif
#if PROCESS_DELAY
sleep(PROCESS_DELAY);
#endif
}