-
Notifications
You must be signed in to change notification settings - Fork 12
/
iptablespush.c
324 lines (297 loc) · 9.86 KB
/
iptablespush.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Author: Gandalf
* email: [email protected]
*/
#include "redismodule.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int redis_waitpid(pid_t pid) {
int rc, status;
do {
if (-1 == (rc = waitpid(pid, &status, WUNTRACED))) {
goto exit;
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
exit:
return rc;
}
int execute_fork() {
fflush(stdout);
fflush(stderr);
return fork();
}
int execute_popen(pid_t *pid, const char *command) {
int fd[2];
if (-1 == pipe(fd))
return -1;
if (-1 == (*pid = execute_fork())) {
close(fd[0]);
close(fd[1]);
return -1;
}
if (0 != *pid) /* parent process */
{
close(fd[1]);
return fd[0];
}
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
dup2(fd[1], STDERR_FILENO);
close(fd[1]);
if (-1 == setpgid(0, 0)) {
exit(EXIT_SUCCESS);
}
execl("/bin/sh", "sh", "-c", command, NULL);
exit(EXIT_SUCCESS);
}
int DROP_Insert_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2)
return RedisModule_WrongArity(ctx);
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1],
REDISMODULE_READ | REDISMODULE_WRITE);
pid_t pid;
int fd;
#ifdef WITH_IPSET
static char insert_command[256];
sprintf(insert_command, "ipset add block_ip %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif BSD
static char insert_command[256];
sprintf(insert_command, " pfctl -t block_ip -T add %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif WITH_NFTABLES
static char insert_command[256];
sprintf(insert_command, "nft insert rule ip redis INPUT ip saddr %s drop",
RedisModule_StringPtrLen(argv[1], NULL));
#else
static char check_command[256], insert_command[256];
char tmp_buf[4096];
sprintf(check_command, "iptables -C INPUT -s %s -j DROP",
RedisModule_StringPtrLen(argv[1], NULL));
sprintf(insert_command, "iptables -I INPUT -s %s -j DROP",
RedisModule_StringPtrLen(argv[1], NULL));
#endif
printf("%s || %s\n", RedisModule_StringPtrLen(argv[0], NULL),
RedisModule_StringPtrLen(argv[1], NULL));
#if defined (WITH_IPSET) || defined (BSD) || defined (WITH_NFTABLES)
fd = execute_popen(&pid, insert_command);
redis_waitpid(pid);
close(fd);
#else
fd = execute_popen(&pid, check_command);
redis_waitpid(pid);
if (0 < read(fd, tmp_buf, sizeof(tmp_buf) - 1)) {
close(fd);
execute_popen(&pid, insert_command);
redis_waitpid(pid);
}
close(fd);
#endif
RedisModule_StringSet(key, argv[1]);
size_t newlen = RedisModule_ValueLength(key);
RedisModule_CloseKey(key);
RedisModule_ReplyWithLongLong(ctx, newlen);
return REDISMODULE_OK;
}
int DROP_Delete_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2)
return RedisModule_WrongArity(ctx);
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1],
REDISMODULE_READ | REDISMODULE_WRITE);
pid_t pid;
int fd;
static char insert_command[256];
#ifdef WITH_IPSET
sprintf(insert_command, "ipset del block_ip %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif BSD
sprintf(insert_command, "pfctl -t block_ip -T delete %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif WITH_NFTABLES
sprintf(insert_command, "nft delete rule redis INPUT `nft list table ip redis --handle --numeric |grep -m1 \"ip saddr %s drop\"|grep -oe \"handle [0-9]*\"`",
RedisModule_StringPtrLen(argv[1], NULL));
#else
sprintf(insert_command, "iptables -D INPUT -s %s -j DROP",
RedisModule_StringPtrLen(argv[1], NULL));
#endif
printf("%s || %s\n", RedisModule_StringPtrLen(argv[0], NULL),
RedisModule_StringPtrLen(argv[1], NULL));
fd = execute_popen(&pid, insert_command);
redis_waitpid(pid);
close(fd);
RedisModule_DeleteKey(key);
size_t newlen = RedisModule_ValueLength(key);
RedisModule_CloseKey(key);
RedisModule_ReplyWithLongLong(ctx, newlen);
return REDISMODULE_OK;
}
int ACCEPT_Insert_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2)
return RedisModule_WrongArity(ctx);
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1],
REDISMODULE_READ | REDISMODULE_WRITE);
pid_t pid;
int fd;
#ifdef WITH_IPSET
static char insert_command[256];
sprintf(insert_command, "ipset add allow_ip %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif BSD
static char insert_command[256];
sprintf(insert_command, "pfctl -t allow_ip -T add %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif WITH_NFTABLES
static char insert_command[256];
sprintf(insert_command, "nft insert rule ip redis INPUT ip saddr %s accept",
RedisModule_StringPtrLen(argv[1], NULL));
#else
char tmp_buf[4096];
static char check_command[256], insert_command[256];
sprintf(check_command, "iptables -C INPUT -s %s -j ACCEPT",
RedisModule_StringPtrLen(argv[1], NULL));
sprintf(insert_command, "iptables -I INPUT -s %s -j ACCEPT",
RedisModule_StringPtrLen(argv[1], NULL));
#endif
printf("%s || %s\n", RedisModule_StringPtrLen(argv[0], NULL),
RedisModule_StringPtrLen(argv[1], NULL));
#if defined (WITH_IPSET) || defined (BSD) || defined (WITH_NFTABLES)
fd = execute_popen(&pid, insert_command);
redis_waitpid(pid);
close(fd);
#else
fd = execute_popen(&pid, check_command);
redis_waitpid(pid);
if (0 < read(fd, tmp_buf, sizeof(tmp_buf) - 1)) {
close(fd);
execute_popen(&pid, insert_command);
redis_waitpid(pid);
}
close(fd);
#endif
RedisModule_StringSet(key, argv[1]);
size_t newlen = RedisModule_ValueLength(key);
RedisModule_CloseKey(key);
RedisModule_ReplyWithLongLong(ctx, newlen);
return REDISMODULE_OK;
}
int ACCEPT_Delete_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2)
return RedisModule_WrongArity(ctx);
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1],
REDISMODULE_READ | REDISMODULE_WRITE);
pid_t pid;
int fd;
static char insert_command[256];
#ifdef WITH_IPSET
sprintf(insert_command, "ipset del allow_ip %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif BSD
sprintf(insert_command, "pfctl -t allow_ip -T delete %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif WITH_NFTABLES
sprintf(insert_command, "nft delete rule redis INPUT `nft list table ip redis --handle --numeric |grep -m1 \"ip saddr %s accept\"|grep -oe \"handle [0-9]*\"`",
RedisModule_StringPtrLen(argv[1], NULL));
#else
sprintf(insert_command, "iptables -D INPUT -s %s -j ACCEPT",
RedisModule_StringPtrLen(argv[1], NULL));
#endif
printf("%s || %s\n", RedisModule_StringPtrLen(argv[0], NULL),
RedisModule_StringPtrLen(argv[1], NULL));
fd = execute_popen(&pid, insert_command);
redis_waitpid(pid);
close(fd);
RedisModule_DeleteKey(key);
size_t newlen = RedisModule_ValueLength(key);
RedisModule_CloseKey(key);
RedisModule_ReplyWithLongLong(ctx, newlen);
return REDISMODULE_OK;
}
int TTL_DROP_Insert_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 3)
return RedisModule_WrongArity(ctx);
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1],
REDISMODULE_READ | REDISMODULE_WRITE);
long long count;
if ((RedisModule_StringToLongLong(argv[2], &count) != REDISMODULE_OK)
|| (count < 0)) {
return RedisModule_ReplyWithError(ctx, "ERR invalid count");
}
pid_t pid;
int fd;
#ifdef WITH_IPSET
static char insert_command[256];
sprintf(insert_command, "ipset add block_ip %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif BSD
static char insert_command[256];
sprintf(insert_command, "pfctl -t block_ip -T add %s",
RedisModule_StringPtrLen(argv[1], NULL));
#elif WITH_NFTABLES
static char insert_command[256];
sprintf(insert_command, "nft insert rule ip redis INPUT ip saddr %s drop",
RedisModule_StringPtrLen(argv[1], NULL));
#else
static char check_command[256], insert_command[256];
char tmp_buf[4096];
sprintf(check_command, "iptables -C INPUT -s %s -j DROP",
RedisModule_StringPtrLen(argv[1], NULL));
sprintf(insert_command, "iptables -I INPUT -s %s -j DROP",
RedisModule_StringPtrLen(argv[1], NULL));
#endif
printf("%s || %s\n", RedisModule_StringPtrLen(argv[0], NULL),
RedisModule_StringPtrLen(argv[1], NULL));
#if defined (WITH_IPSET) || defined (BSD) || defined (WITH_NFTABLES)
fd = execute_popen(&pid, insert_command);
redis_waitpid(pid);
close(fd);
#else
fd = execute_popen(&pid, check_command);
redis_waitpid(pid);
if (0 < read(fd, tmp_buf, sizeof(tmp_buf) - 1)) {
close(fd);
execute_popen(&pid, insert_command);
redis_waitpid(pid);
}
close(fd);
#endif
RedisModule_StringSet(key, argv[1]);
RedisModule_SetExpire(key, count * 1000);
size_t newlen = RedisModule_ValueLength(key);
RedisModule_CloseKey(key);
RedisModule_ReplyWithLongLong(ctx, newlen);
return REDISMODULE_OK;
}
/* This function must be present on each Redis module. It is used in order to
* register the commands into the Redis server. */
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (RedisModule_Init(ctx, "iptables-input-filter", 1,
REDISMODULE_APIVER_1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
/* Log the list of parameters passing loading the module. */
for (int j = 0; j < argc; j++) {
const char *s = RedisModule_StringPtrLen(argv[j], NULL);
printf("Module loaded with ARGV[%d] = %s\n", j, s);
}
if (RedisModule_CreateCommand(ctx, "drop_insert", DROP_Insert_RedisCommand,
"write deny-oom", 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "drop_delete", DROP_Delete_RedisCommand,
"write deny-oom", 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "accept_insert", ACCEPT_Insert_RedisCommand,
"write deny-oom", 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "accept_delete", ACCEPT_Delete_RedisCommand,
"write deny-oom", 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "ttl_drop_insert", TTL_DROP_Insert_RedisCommand,
"write deny-oom", 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
return REDISMODULE_OK;
}