-
Notifications
You must be signed in to change notification settings - Fork 13
/
process_mem.c
112 lines (104 loc) · 2.38 KB
/
process_mem.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
#include "include/process_mem.h"
#include "include/process_mem_genl.h"
struct nl_sock *sock;
int f_id;
#ifdef DEBUG
#define PRINTF(fmt, arg...) printf(fmt, ##arg);
#else
#define PRINTF(fmt, arg...) do {} while(0);
#endif
struct mm_info *get_mm(int pid)
{
struct nl_msg *msg = NULL;
struct mm_info *mm = NULL;
int err;
/* request for mm */
err = request_pid_mm(sock, f_id, pid);
if (err < 0)
{
fatal(err, "Unable to send message: %s", nl_geterror(err));
mm = NULL;
goto out;
}
/* receive message */
err = recv_nl_msg(sock, &msg);
if (err < 0)
{
fatal(err, "Unable to receive message: %s", nl_geterror(err));
mm = NULL;
goto out;
}
mm = get_info(msg);
out:
nlmsg_free(msg);
return mm;
}
struct vma_info *get_vma(int pid, unsigned long address)
{
struct nl_msg *msg = NULL;
struct vma_info *vm = NULL;
int err;
/* request for vma */
err = request_pid_next_vma(sock, f_id, pid, address);
if (err < 0)
{
fatal(err, "Unable to send message: %s", nl_geterror(err));
vm = NULL;
goto out;
}
/* receive message */
err = recv_nl_msg(sock, &msg);
if (err < 0)
{
fatal(err, "Unable to receive message: %s", nl_geterror(err));
vm = NULL;
goto out;
}
vm = get_info(msg);
out:
nlmsg_free(msg);
return vm;
}
size_t read_memory(int pid, void * address, size_t size,
void * l_buff)
{
size_t read_bytes = 0;
int err;
/* request to get bytes */
err = request_pid_rw_mem(sock, f_id, pid, (unsigned long)address,
size, (unsigned long)l_buff, (unsigned long)&read_bytes, 0);
if (err < 0)
{
return err;
}
return read_bytes;
}
size_t write_memory(int pid, void * address, size_t size,
void * l_buff)
{
size_t write_bytes = 0;
int err;
/* request to set bytes */
err = request_pid_rw_mem(sock, f_id, pid, (unsigned long)address,
size, (unsigned long)l_buff, (unsigned long)&write_bytes, 1);
if (err < 0)
{
return err;
}
return write_bytes;
}
void __attribute__ ((constructor)) initLibrary(void)
{
f_id = prep_nl_sock(&sock);
if (f_id < 0)
{
exit(-1);
}
PRINTF("process_mem_lib initialized!\n");
}
void __attribute__ ((destructor)) cleanUpLibrary(void)
{
nl_close(sock);
nl_socket_free(sock);
PRINTF("process_mem_lib exited\n");
}