-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
256 lines (237 loc) · 7.46 KB
/
main.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
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ptrace.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
extern long psyscall(pid_t pid, long number, ...);
struct entry { const char *name; long number; };
static struct entry syscalls[] = {
#include "syscalls.inc"
};
static struct entry constants[] = {
#include "constants.inc"
};
static int entry_cmp(const void *a, const void *b) {
const struct entry *A = a, *B = b;
return strcmp(A->name, B->name);
}
static long find_syscall(char *name)
{
char *end;
long nr = strtol(name, &end, 0);
if (*end != '\0') {
struct entry key, *hit;
key.name = name;
key.number = 0;
hit = bsearch(&key, syscalls, sizeof(syscalls)/sizeof(struct entry),
sizeof(struct entry), entry_cmp);
return hit ? hit->number : -1;
}
return nr;
}
/*
* Parses C-style constant (e.g., '42', 'PROT_READ|PROT_WRITE', ...)
*/
long parse_constant(char *arg, int *err)
{
char *end;
long value;
if (*arg == '\0' || *arg == '|' || *arg == '+') {
*err |= !!*arg;
return 0;
}
if ((end = arg + strcspn(arg, "+|")) && *end) {
char op = *end;
*end = '\0';
value = parse_constant(arg, err);
*end++ = op;
switch (op) {
case '+': value += parse_constant(end, err); break;
case '|': value |= parse_constant(end, err); break;
default: *err |= 1; break;
}
return value;
}
value = strtol(arg, &end, 0);
if (*end) {
if (end == arg) {
struct entry key, *hit;
key.name = arg;
key.number = 0;
hit = bsearch(&key, constants,
sizeof(constants)/sizeof(struct entry),
sizeof(struct entry), entry_cmp);
if (hit) {
value = hit->number;
} else {
*err |= 1;
}
} else {
*err |= 1;
}
}
return value;
}
/*
* There are more efficient and easier methods to write remote process memory.
* However, PTRACE_POKEDATA is the most portable, so we use it.
*/
static size_t ptrace_write(pid_t pid, void *addr, void *buf, size_t len)
{
size_t n = len;
int errnold = errno;
errno = 0;
while (!errno && len > 0) {
size_t i, j;
if ((i = ((size_t)addr % sizeof(long))) || len < sizeof(long)) {
union {
long value;
char buf[sizeof(long)];
} data;
data.value = ptrace(PTRACE_PEEKDATA, pid, (char *)addr-i, 0);
if (!errno) {
for (j = i; j < sizeof(long) && j-i < len; j++)
data.buf[j] = ((char *)buf)[j-i];
if (!ptrace(PTRACE_POKEDATA, pid, (char *)addr-i, data.value)) {
addr = (char *)addr + (j-i);
buf = (char *)buf + (j-i);
len -= j-i;
}
}
} else {
for (i = 0, j = len/sizeof(long); i < j; i++) {
if (ptrace(PTRACE_POKEDATA, pid, addr, *(long *)buf) == -1)
break;
addr = (char *)addr + sizeof(long);
buf = (char *)buf + sizeof(long);
len -= sizeof(long);
}
}
}
if (!errno)
errno = errnold;
return n - len;
}
int main(int argc, char *argv[])
{
int i;
pid_t pid;
unsigned long addr, len;
long ret, number, arg[6] = {0, 0, 0, 0, 0, 0};
errno = 0;
if (argc < 3 || argc > 9) {
fprintf(stderr, "syscall() inject0r\n");
fprintf(stderr, "usage: %s pid syscall [arg0] ... [arg5]\n", argv[0]);
return argc > 1;
} else if (!(pid = atoi(argv[1])) || pid == getpid() || kill(pid, 0)) {
if (errno)
fprintf(stderr, "bad pid: %s (%s)\n", argv[1], strerror(errno));
else fprintf(stderr, "bad pid: %s\n", argv[1]);
return 2;
} else if ((number = find_syscall(argv[2])) < 0) {
fprintf(stderr, "bad syscall: %s\n", argv[2]);
return 3;
}
/*
* Parse arguments and count the total length of input strings.
*/
len = 0;
for (i = 3; i < argc; i++) {
const char *p;
int err = 0;
if (argv[i][0] == '"' && (p = strchr(argv[i]+1, '"')) && !p[1]) {
len += p-argv[i];
continue;
}
arg[i-3] = parse_constant(argv[i], &err);
if (err) {
fprintf(stderr, "bad arg%d: %s\n", i-3, argv[i]);
return 10+i;
}
}
/*
* Allocate a block of memory for string arguments.
*/
addr = 0;
if (len) {
int status;
long pagesz, sc;
unsigned long j;
if ((sc = find_syscall("mmap2")) == -1) {
if ((sc = find_syscall("mmap")) == -1) {
fprintf(stderr, "__NR_mmap missing\n");
return 4;
}
}
if ((pagesz = sysconf(_SC_PAGE_SIZE)) == -1) {
fprintf(stderr, "error determining page size errno=%d (%s)\n",
errno, strerror(errno));
return 5;
}
ret = psyscall(pid, sc, NULL, (len = pagesz * (1 + (len-1)/pagesz)),
PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (ret == (long)MAP_FAILED) {
fprintf(stderr, "broken psyscall (or mmap failed) errno=%d (%s)\n",
errno, strerror(errno));
return 6;
}
addr = (unsigned long)ret;
if (ptrace(PTRACE_ATTACH, pid, NULL, 0) == -1) {
fprintf(stderr, "ptrace() attach failed: %s\n", strerror(errno));
psyscall(pid, SYS_munmap, addr, len);
return 7;
}
if (waitpid(pid, &status, 0) == -1 || !WIFSTOPPED(status)) {
fprintf(stderr, "stopping target process failed\n");
ptrace(PTRACE_DETACH, pid, NULL, 0);
psyscall(pid, SYS_munmap, addr, len);
return 8;
}
for (i = 3, j = 0; i < argc; i++) {
char *p;
if (argv[i][0] == '"' && (p = strchr(argv[i]+1, '"')) && !p[1]) {
*p = '\0';
ret = ptrace_write(pid, (char *)addr+j, argv[i]+1, p-argv[i]);
*p = '"';
if (ret == p-argv[i]) {
arg[i-3] = addr+j;
j += ret;
} else {
fprintf(stderr, "ptrace_write() failed\n");
ptrace(PTRACE_DETACH, pid, NULL, 0);
psyscall(pid, SYS_munmap, addr, len);
return 9;
}
}
}
ptrace(PTRACE_DETACH, pid, NULL, 0);
}
/*
* Finally, inject the syscall.
*/
errno = 0;
ret = psyscall(pid, number, arg[0], arg[1], arg[2], arg[3], arg[4], arg[5]);
if (len) psyscall(pid, SYS_munmap, addr, len);
if (ret == -1) {
fprintf(stderr, "[%d] psyscall() errno=%d (%s)\n",
pid, errno, strerror(errno));
return 10;
}
if (isatty(STDOUT_FILENO)) {
fprintf(stdout, "[%d] syscall(%s", pid, argv[2]);
for (i = 3; i < argc; i++) {
fprintf(stdout, ", %s", arg[i-3] ? argv[i] : "0");
}
fprintf(stdout, ") = ");
}
fprintf(stdout, ((ret+!ret) & 0xFFF) ? "%ld\n" : "0x%08lx\n", ret);
return 0;
}