forked from victor-yacovlev/fpmi-caos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptrace_catch_string.c
64 lines (62 loc) · 1.83 KB
/
ptrace_catch_string.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
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdbool.h>
#include <asm/unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
static void
premoderate_write_syscall(pid_t pid, struct user_regs_struct state)
{
size_t orig_buf = state.rsi; // ecx for i386
size_t size = state.rdx; // rdx for i386
char *buffer = calloc(size+sizeof(long), sizeof(*buffer));
int val = 0;
for (size_t i=0; i<size; ++i) {
buffer[i] = ptrace(PTRACE_PEEKDATA, pid, orig_buf+i, NULL);
}
char *bad_word;
if ( (bad_word=strstr(buffer, "fuck")) ) {
size_t offset = bad_word - buffer + 1; // 'u' letter
buffer[offset] = '*';
size_t target_address = orig_buf + offset;
long val;
memcpy(&val, buffer+offset, sizeof(val));
ptrace(PTRACE_POKEDATA, pid, target_address, val);
}
free(buffer);
}
int main(int argc, char *argv[])
{
pid_t pid = fork();
if (-1==pid) { perror("fork"); exit(1); }
if (0==pid) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execvp(argv[1], argv+1);
perror("exec");
exit(2);
}
else {
int wstatus = 0;
struct user_regs_struct state;
bool stop = false;
while (!stop) {
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
waitpid(pid, &wstatus, 0);
stop = WIFEXITED(wstatus);
if (WIFSTOPPED(wstatus)) {
ptrace(PTRACE_GETREGS, pid, 0, &state);
if (__NR_write==state.orig_rax) { // orig_eax for i386
premoderate_write_syscall(pid, state);
}
}
}
}
}