-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodule.c
158 lines (117 loc) · 3.94 KB
/
module.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
/* module.c -- Functions that handle payload modules.
*
* This file is part of fsnoop.
*
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and
* you think this stuff is worth it, you can buy me a beer in return.
*/
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <signal.h>
#include <string.h>
#include "event.h"
#include "global.h"
#include "module.h"
#include "util.h"
/* Outputs information about current module.
*/
#define mprintf(format, ...) printf("[+] %s: "format, g_paymod->dso, \
##__VA_ARGS__)
int signal_caught = 0;
static void *handle;
/* When ctrl-c is typed, this function is called. It exits properly by
* first unloading module.
*/
static void ctrlc_handler() {
signal_caught = 1;
printf("\n[+] Signal caught.\n");
unload_module();
exit(0);
}
/* Unloads the module.
*/
void unload_module(void) {
if (!signal_caught)
mprintf("Exploitation done.\n");
mprintf("Unloading module.\n");
if (dlclose(handle) != 0)
mprintf("Error: dlclose() failed.\n");
}
/* Loads the DSO, retrieve module parameters and feed the g_paymod
* structure.
*/
void load_module(void) {
if (!(handle = dlopen(g_paymod->dso, RTLD_LAZY))) {
mprintf("Error: dlopen() failed.\n");
exit(1);
}
if (!(g_paymod->title = dlsym(handle, "title"))) {
g_paymod->title = strdup("** Untitled paymod **");
}
if (!(g_paymod->file = dlsym(handle, "file"))) {
g_paymod->file = strdup("/dev/null");
}
g_paymod->proc_name = dlsym(handle, "proc_name");
if (!(g_paymod->count = (int *) dlsym(handle, "count"))) {
/* count symbol not found, we set a default one (0). */
g_paymod->count = malloc(sizeof(int));
if (!g_paymod->count) {
fprintf(stderr, "load_module(): malloc failed.\n");
exit(1);
}
*g_paymod->count = 0;
}
if (!(g_paymod->mask = (uint32_t *) dlsym(handle, "mask"))) {
/* mask symbol not found, we set a default one (DFT_INOTIFY_BITS) */
g_paymod->mask = malloc(sizeof(uint32_t));
if (!g_paymod->mask) {
fprintf(stderr, "load_module(): malloc failed.\n");
exit(1);
}
*g_paymod->mask = DFT_INOTIFY_BITS;
}
if (!(g_paymod->payload = dlsym(handle, "payload"))) {
mprintf("Error: payload symbol not found.\n");
exit(1);
}
mprintf("%s\n", g_paymod->title);
if (!g_paymod->proc_name) {
/* imask_str is never be NULL as imask_to_str() exit(1) on malloc
* failure. */
char *imask_str = imask_to_str(*g_paymod->mask);
mprintf("payload=[%p] file=[%s] mask=[%s] count=[%d]\n",
g_paymod->payload, g_paymod->file,
imask_str, *g_paymod->count);
free(imask_str);
} else {
if (strstr(g_paymod->file, PIDSTR)) {
mprintf("payload=[%p] file=[%s]\n",
g_paymod->payload, g_paymod->file);
mprintf("waiting for command: \"%s\"\n", g_paymod->proc_name);
} else if (strstr(g_paymod->file, "/dev/null")) {
mprintf("payload=[%p]\n", g_paymod->payload);
} else {
mprintf("payload=[%p] file=[%s]\n", g_paymod->payload,
g_paymod->file);
}
}
/* now the module is correctly loaded, destructor routines should be
* executed on ctrl-c. */
signal(SIGINT, ctrlc_handler);
}
/* Waits for the process name g_paymod->proc_name and replaces the PIDSTR
* string by the process ID in the g_paymod->file.
*/
void substitute_process_id(void) {
char *pid;
int rc;
pid = loop_pgrep(g_paymod->proc_name);
rc = sub_str(PIDSTR, pid, g_paymod->file);
if (rc < 0) {
fprintf(stderr, "substitute_process_id() failed");
exit(1);
}
}