-
Notifications
You must be signed in to change notification settings - Fork 6
/
patch.c
55 lines (47 loc) · 1.16 KB
/
patch.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
#include "patch.h"
#include "ptrace.h"
#include <stdio.h>
int patch(unsigned int pid,
unsigned long addr, unsigned long data, unsigned long mask)
{
int status = -1;
status = ptrace_attach_and_wait(pid);
if (status < 0)
{
printf("attach and wait failed\n");
return -1;
}
unsigned long data_before = 0;
status = ptrace_peek(pid, addr, &data_before);
if (status < 0)
{
printf("peek failed\n");
goto cleanup;
}
printf("[%d][0x%llx] => 0x%llx\n", pid, addr, data_before);
if (mask == 0)
{
status = 0;
goto cleanup;
}
const unsigned long dataq = (data_before & ~mask) | data;
printf("[%d][0x%llx] <= 0x%llx\n", pid, addr, dataq);
status = ptrace_poke(pid, addr, dataq);
if (status < 0)
{
printf("poke failed\n");
goto cleanup;
}
unsigned long data_after = 0;
status = ptrace_peek(pid, addr, &data_after);
if (status < 0)
{
printf("peek failed\n");
goto cleanup;
}
printf("[%d][0x%llx] => 0x%llx\n", pid, addr, data_after);
status = 0;
cleanup:
ptrace_detach(pid);
return status;
}