-
Notifications
You must be signed in to change notification settings - Fork 0
/
instream.c
87 lines (75 loc) · 2.11 KB
/
instream.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
#include <stdio.h>
#include <signal.h>
#include <sys/ptrace.h> // for ptrace
#include <sys/wait.h> // for waitpid
#include <sys/user.h> // for user_regs_struct
#include <sys/types.h>
#include "spawn.h"
#include "inject.h"
#include "dump.h"
static void loop(pid_t pid);
/*void handle_interrupt(int sig) {
if(sig == SIGINT) {
ptrace(PTRACE_DETACH, pid, 0, 0);
}
}*/
int main(int argc, char *argv[]) {
if(argc <= 1) {
printf("Usage: %s program args...\n", argv[0]);
return 1;
}
pid_t pid = spawn_program(argv + 1);
loop(pid);
return 0;
}
static unsigned long current_rip(pid_t pid) {
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
return (unsigned long)regs.rip;
}
static void loop(pid_t pid) {
int trap_count = 0;
for(;;) {
int status;
waitpid(pid, &status, 0);
if(WIFEXITED(status)) {
printf("Target exited with status %d\n", WEXITSTATUS(status));
break;
}
else if(WIFSIGNALED(status)) {
printf("Target killed by signal %d\n", WTERMSIG(status));
break;
}
else if(WIFSTOPPED(status)) {
int sig = WSTOPSIG(status);
if(sig == SIGTRAP) {
if(trap_count == 0) {
inject_at_entry_point(pid);
}
else if(trap_count == 1) {
undo_code_injection(pid);
}
if(trap_count > 0) {
#if 0
printf("[%12lx]\n", current_rip(pid));
#else
disassemble_code_ptrace(current_rip(pid), pid);
#endif
}
trap_count ++;
ptrace(PTRACE_CONT, pid, 0, 0);
}
else {
printf("Target received signal %d\n", sig);
ptrace(PTRACE_CONT, pid, 0, sig);
}
}
else if(WIFCONTINUED(status)) {
printf("Target continued\n");
}
else {
printf("Target stopped for unknown reason\n");
break; // for debugging
}
}
}