-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinjector.c
159 lines (126 loc) · 4.11 KB
/
injector.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <frida-core.h>
#define INFO(...) printf(__VA_ARGS__)
#define BANNER (" \n" \
"\t ██╗███╗ ██╗ ██╗███████╗ ██████╗████████╗ ██████╗ ██████╗ \n" \
"\t ██║████╗ ██║ ██║██╔════╝██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗ \n" \
"\t ██║██╔██╗ ██║ ██║█████╗ ██║ ██║ ██║ ██║██████╔╝ \n" \
"\t ██║██║╚██╗██║██ ██║██╔══╝ ██║ ██║ ██║ ██║██╔══██╗ \n" \
"\t ██║██║ ╚████║╚█████╔╝███████╗╚██████╗ ██║ ╚██████╔╝██║ ██║ \n" \
"\t ╚═╝╚═╝ ╚═══╝ ╚════╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ \n" \
" \n")
void show_usage(char *process)
{
INFO("Usage for %s\n\n", process);
INFO(" -p pid\n");
INFO(" -l library path\n");
INFO(" -e entry point\n");
exit(EXIT_FAILURE);
}
bool is_pid_running(pid_t pid)
{
if (kill(pid, 0) == 0)
{
return true;
}
return false;
}
int main(int argc, char *argv[])
{
int optch;
pid_t pid = 0;
char *library_path = NULL;
char *entry_point = NULL;
INFO(BANNER);
while ((optch = getopt(argc, argv, "p:l:e:h")) != -1)
{
switch (optch)
{
case 'p':
pid = atoi(optarg);
break;
case 'l':
library_path = optarg;
break;
case 'e':
entry_point = optarg;
break;
case 'h':
show_usage(argv[0]);
break;
default:
show_usage(argv[0]);
break;
}
}
if (pid == 0 || library_path == NULL)
{
show_usage(argv[0]);
}
else if (entry_point == NULL)
{
entry_point = "";
INFO("[!] Entry point not provided, the target process might segfault!\n");
}
//
// Check if PID is running
//
if (!is_pid_running(pid))
{
INFO("[-] Process not found: %d\n", pid);
exit(EXIT_FAILURE);
}
//
// Check if we are able to access the library that we want to inject
//
if (access(library_path, F_OK) == -1)
{
INFO("[-] Library path invalid or inaccessible: %s\n", library_path);
exit(EXIT_FAILURE);
}
guint result;
GError *error = NULL;
FridaInjector *injector;
const char *context = "u:object_r:frida_file:s0";
//
// Initialize Frida
//
frida_init();
//
// Patch SeLinux policies
//
INFO("[+] Patching SeLinux policy\n");
frida_selinux_patch_policy();
//
// TODO: check for error
//
setxattr(library_path, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0);
//
// Get new Frida injector instance
//
injector = frida_injector_new();
INFO("[+] Injecting library: %s in pid: %d\n", library_path, pid);
//
// Perform the injection
//
result = frida_injector_inject_library_file_sync(injector, pid, library_path, entry_point, "", NULL, &error);
if (error != NULL)
{
INFO("[-] Error while injecting: %s\n", error->message);
g_clear_error(&error);
}
frida_injector_close_sync(injector, NULL, NULL);
g_object_unref(injector);
//
// Deinitialize Frida
//
frida_deinit();
INFO("[+] Injection completed\n");
return result;
}