-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathr2k_u.c
314 lines (246 loc) · 7.53 KB
/
r2k_u.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
Just tiny program to test the module. Sanity checks not done
*/
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#define CHAR_FMT "%c "
#define HEX_FMT "0x%02x "
#define HEX 0x0
#define CHAR 0x1
struct r2k_data {
int pid;
unsigned long addr;
unsigned long len;
unsigned char *buff;
};
#define MAX_PHYS_ADDR 128
struct kernel_map_info {
unsigned long start_addr;
unsigned long end_addr;
unsigned long phys_addr[MAX_PHYS_ADDR];
int n_pages;
int n_phys_addr;
};
struct kernel_maps {
int n_entries;
int size;
};
#define R2_TYPE 0x69
#define IOCTL_READ_KERNEL_MEMORY _IOR (R2_TYPE, 0x1, sizeof (struct r2k_data))
#define IOCTL_WRITE_KERNEL_MEMORY _IOR (R2_TYPE, 0x2, sizeof (struct r2k_data))
#define IOCTL_READ_PROCESS_ADDR _IOR (R2_TYPE, 0x3, sizeof (struct r2k_data))
#define IOCTL_WRITE_PROCESS_ADDR _IOR (R2_TYPE, 0x4, sizeof (struct r2k_data))
#define IOCTL_READ_PHYSICAL_ADDR _IOR (R2_TYPE, 0x5, sizeof (struct r2k_data))
#define IOCTL_WRITE_PHYSICAL_ADDR _IOR (R2_TYPE, 0x6, sizeof (struct r2k_data))
#define IOCTL_GET_KERNEL_MAP _IOR (R2_TYPE, 0x7, sizeof (struct r2k_data))
#define READ_KERNEL_MEMORY 0x1
#define WRITE_KERNEL_MEMORY 0x2
#define READ_PROCESS_ADDR 0x3
#define WRITE_PROCESS_ADDR 0x4
#define READ_PHYSICAL_ADDR 0x5
#define WRITE_PHYSICAL_ADDR 0x6
#define GET_KERNEL_MAP 0x7
const char *ioctl_str[] = {
"IOCTL_READ_KERNEL_MEMORY",
"IOCTL_WRITE_KERNEL_MEMORY",
"IOCTL_READ_PROCESS_ADDR",
"IOCTL_WRITE_PROCESS_ADDR",
"IOCTL_READ_PHYSICAL_ADDR",
"IOCTL_WRITE_PHYSICAL_ADDR",
"IOCTL_GET_PROC_MAPS",
"IOCTL_GET_KERNEL_MAP"
};
const char *devicename = "/dev/r2k";
const char *prog_name;
void print_help(void)
{
printf ("%s -a [addr] -i [ioctl] -b [n_bytes] -w [w_bytes] -p [pid]\n", prog_name);
exit (-1);
}
int main(int argc, char **argv)
{
int fd;
int ret;
int i;
int j;
int opt;
int n_bytes;
int n_ioctl;
unsigned int ioctl_n;
struct r2k_data data;
struct kernel_maps map_data;
struct kernel_map_info *info;
unsigned char c = 98;
char *p;
char *str;
int output;
data.addr = data.pid = data.len = 0;
prog_name = argv[0];
output = HEX;
ioctl_n = 0;
/* if (argc < 4)
print_help(); */
while ((opt = getopt (argc, argv, "a:i:b:w:p:o:")) != -1) {
switch (opt) {
case 'a':
data.addr = strtoul (optarg, &p, 16);
break;
case 'i':
n_ioctl = atoi (optarg);
if (n_ioctl < 1 || n_ioctl > 8) {
printf ("ioctl from 1 to 8\n");
exit (-1);
}
break;
case 'b':
n_bytes = atoi (optarg);
break;
case 'w':
printf ("str = optarg\n");
str = optarg;
printf ("str = optarg\n");
n_bytes = strlen (str);
printf ("str = optarg\n");
case 'p':
data.pid = atoi (optarg);
break;
case 'o':
output = *optarg == 'h' ? HEX : CHAR;
break;
default:
printf ("%s: arg %s not valid\n", prog_name, optarg);
break;
}
}
if (!n_ioctl)
print_help();
printf ("ioctl: %s\n", ioctl_str[n_ioctl - 1]);
printf ("ioctl: addr 0x%lx\n", data.addr);
printf ("ioctl: %d bytes\n", n_bytes);
if (data.pid)
printf ("ioctl: pid (%d)\n", data.pid);
fd = open (devicename, O_RDONLY);
if( fd == -1 ) {
perror ("open error");
return -1;
}
switch (n_ioctl) {
case READ_KERNEL_MEMORY:
data.buff = (unsigned char *)calloc (n_bytes, 1);
data.len = n_bytes;
ioctl_n = IOCTL_READ_KERNEL_MEMORY;
ret = ioctl (fd, ioctl_n, &data);
printf ("ret: %d\n", ret);
fprintf (stderr, "ioctl err: %s\n", strerror (errno));
if (!ret) {
printf ("Got the state: addr: 0x%lx - value: ", data.addr);
for (i = 0; i < n_bytes; i++) {
printf (output == HEX ? HEX_FMT : CHAR_FMT , data.buff[i]);
}
}
break;
case WRITE_KERNEL_MEMORY:
data.buff = (unsigned char *)calloc (n_bytes, 1);
data.len = n_bytes;
strncpy (data.buff, str, data.len);
printf ("data.buff: %x\n", *data.buff);
ioctl_n = IOCTL_WRITE_KERNEL_MEMORY;
ret = ioctl (fd, ioctl_n, &data);
fprintf (stderr, "ioctl err: %s\n", strerror (errno));
break;
case READ_PROCESS_ADDR:
data.buff = (unsigned char *)calloc (n_bytes, 1);
data.len = n_bytes;
printf ("Reading %d bytes at 0x%lx from pid (%d)\n", data.len, data.addr, data.pid);
ioctl_n = IOCTL_READ_PROCESS_ADDR;
ret = ioctl (fd, ioctl_n, &data);
printf ("ret: %d\n", ret);
fprintf (stderr, "ioctl err: %s\n", strerror (errno));
if (!ret) {
printf ("Got the state: addr: 0x%lx - value: ", data.addr);
for (i = 0; i < n_bytes; i++) {
printf (output == HEX ? HEX_FMT : CHAR_FMT, data.buff[i]);
}
}
break;
case WRITE_PROCESS_ADDR:
data.buff = (unsigned char *)calloc (n_bytes, 1);
data.len = n_bytes;
strncpy (data.buff, str, data.len);
printf ("Writing %d bytes at 0x%lx from pid (%d)\n", data.len, data.addr, data.pid);
printf ("Str: %s\n", data.buff);
ioctl_n = IOCTL_WRITE_PROCESS_ADDR;
ret = ioctl (fd, ioctl_n, &data);
printf ("ret: %d\n", ret);
fprintf (stderr, "ioctl err: %s\n", strerror (errno));
break;
case READ_PHYSICAL_ADDR:
data.buff = (unsigned char *)calloc (n_bytes, 1);
data.len = n_bytes;
printf ("Reading %d bytes at 0x%lx from pid (%d)\n", data.len, data.addr, data.pid);
ioctl_n = IOCTL_READ_PHYSICAL_ADDR;
ret = ioctl (fd, ioctl_n, &data);
printf ("ret: %d\n", ret);
fprintf (stderr, "ioctl err: %s\n", strerror (errno));
if (!ret) {
printf ("Got the state: addr: 0x%lx - value: ", data.addr);
for (i = 0; i < n_bytes; i++) {
printf (output == HEX ? HEX_FMT : CHAR_FMT, data.buff[i]);
}
}
break;
case WRITE_PHYSICAL_ADDR:
data.buff = (unsigned char *)calloc (n_bytes, 1);
data.len = n_bytes;
strncpy (data.buff, str, data.len);
printf ("Writing %d bytes at 0x%lx from pid (%d)\n", data.len, data.addr, data.pid);
printf ("Str: %s\n", data.buff);
ioctl_n = IOCTL_WRITE_PHYSICAL_ADDR;
ret = ioctl (fd, ioctl_n, &data);
printf ("ret: %d\n", ret);
fprintf (stderr, "ioctl err: %s\n", strerror (errno));
break;
case GET_KERNEL_MAP:
ioctl_n = IOCTL_GET_KERNEL_MAP;
ret = ioctl (fd, ioctl_n, &map_data);
if (ret < 0) {
fprintf (stderr, "ioctl err: %s\n", strerror (errno));
break;
}
printf ("map_data.size: %d, map_data.n_entries: %d\n", map_data.size, map_data.n_entries);
info = mmap (0, map_data.size, PROT_READ, MAP_SHARED, fd, 0);
if (info == MAP_FAILED) {
fprintf (stderr, "mmap err: %s\n", strerror (errno));
break;
}
printf ("ret: %d\n", ret);
fprintf (stderr, "ioctl err: %s\n", strerror (errno));
int i, j;
long page_size = sysconf (_SC_PAGESIZE);
for (i = 0; i < map_data.n_entries ; i++) {
struct kernel_map_info *in = &info[i];
printf ("start_addr: 0x%lx\n", in->start_addr);
printf ("end_addr: 0x%lx\n", in->end_addr);
printf ("n_pages: %d (%ld Kbytes)\n", in->n_pages, (in->n_pages * page_size) / 1024);
printf ("n_phys_addr: %d\n", in->n_phys_addr);
for(j = 0; j < in->n_phys_addr; j++)
printf("\tphys_addr: 0x%lx\n", in->phys_addr[j]);
printf ("\n\n");
}
if (munmap (info, map_data.size) == -1)
perror("munmap failed\n");
break;
default:
printf ("ioctl not implemented\n");
break;
}
printf ("\n");
close (fd);
return 0;
}