-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjection.c
145 lines (121 loc) · 3.62 KB
/
jection.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
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/user.h>
#include <unistd.h>
#include "colors.h"
#include "inject.h"
#include "process/trace.h"
#include "proxy/proxy.h"
static void print_help() {
printf("USAGE: jection <PID> [FLAGS]\n\n");
printf("FLAGS:\n");
printf(
"-l <libpath> ─ injects a library from an absolute path into the target PID\n"
);
printf("-r <address> ─ reads memory from the specified address\n");
printf("-p <address> <data> ─ writes data to specified address\n");
printf("-h ─ displays this dialogue\n");
printf(
"-i ─ intercept send and send_to syscalls and print the transmitted data\n"
);
printf(
" └─ -c ─ compare each data buffer with the previous one to highlight matching bytes\n"
);
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s <PID> [FLAGS]", argv[0]);
return 1;
}
char* endptr;
pid_t pid = (pid_t)strtol(argv[1], &endptr, 10);
if (endptr == argv[1]) {
fprintf(stderr, "Error: Invalid PID\n");
return 1;
}
// check if pid is currently running
if (kill(pid, 0) != 0) {
printf("Process with PID %ld is not running\n", (long)pid);
exit(1);
}
attach(pid);
static struct option long_options[] = {
{"library", required_argument, NULL, 'l'},
{"poke", required_argument, NULL, 'p'},
{"read", required_argument, NULL, 'r'},
{"intercept", no_argument, NULL, 'i'},
{"compare", no_argument, NULL, 'c'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
int compare = 0;
int intercept = 0;
int status = 0;
char* libpath;
uint64_t addr;
uint64_t data;
int c;
while ((c = getopt_long(argc, argv, "hcir:p:l:", long_options, NULL)) != -1
) {
switch (c) {
case 'i':
intercept = 1;
break;
case 'c':
compare = 1;
break;
case 'l':
status = inject_so(pid, optarg);
if (status != 0) {
return -1;
}
return 0;
case 'p':
if (optind + 1 != argc) {
fprintf(
stderr,
RED "Option 'poke' requires two arguments\n\n" RESET
);
print_help();
return -1;
}
addr = strtoull(optarg, NULL, 16);
optarg = argv[optind++];
data = strtoull(optarg, NULL, 16);
pokemem_ul(pid, addr, &data, 1);
printf(
"[%s] Writing data 0x%lx to address %lx\n",
GREEN "+" RESET,
data,
addr
);
return 0;
case 'r':
addr = strtoull(optarg, NULL, 16);
readmem_ul(pid, addr, &data, 1);
printf(
"[%s] Data at address %lx: 0x%lx\n",
GREEN "+" RESET,
addr,
data
);
return 0;
case 'h':
print_help();
return 0;
default:
return 1;
}
}
if (intercept == 1) {
proxy_loop(pid, compare);
} else {
fprintf(stderr, RED "No valid arguments provided\n\n" RESET);
print_help();
}
detach(pid);
return 0;
}