This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocker.c
165 lines (135 loc) · 4.59 KB
/
blocker.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#define iptables /sbin/iptables
#define ipset /sbin/ipset
#define username "simpleguardian"
#define indexCommand 1
#define indexIp 2
/**
____ _ _
| __ )| | ___ ___| | _____ _ __
| _ \| |/ _ \ / __| |/ / _ \ '__|
| |_) | | (_) | (__| < __/ |
|____/|_|\___/ \___|_|\_\___|_|
======================================
Program that uses iptables to block or unblock IP
Usage: first init with ./blocker init
then proceed with blocker block/unblock IP
Is supposed to be executed with root privileges, so as protection from executing by unauthorized user
the program checks if you are user simpleguardian or root.
Before blocking IP the program checks if it is not blocked yet to prevent duplicated entries.
After compiling, it is recommended to run following commands:
chown root:root blocker
chmod 755 blocker
chmod u+s blocker
*/
void help(int exitStatus){
printf("usage: init with: blocker init\n");
printf("then usage: blocker block/unblock ip\n");
exit(exitStatus);
}
void check_requirements() {
if (system("iptables -h > /dev/null 2>&1") != 0) {
printf("ERROR: iptables command is missing\n");
exit(1);
}
if (system("ipset -h > /dev/null 2>&1") != 0) {
printf("ERROR: ipset command is missing\n");
exit(1);
}
}
int check_ip_valid(const char *ip) {
const size_t len = strlen(ip);
for (size_t i = 0; i < len; i++) {
const char ch = ip[i];
if (ch == '.' || ch == ':' || ch == '[' || ch == ']') {
continue;
}
if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9')) {
continue;
}
return 0;
}
return 1;
}
int check_user_valid(){
uid_t uid;
struct passwd *udetails;
uid = getuid();
udetails = getpwuid(uid);
return !strcmp(username, udetails->pw_name) || !strcmp("root", udetails->pw_name);
}
int is_already_blocked(char **argv) {
/*
Checks if passed IP is already blocked
Returns 0 if not blocked, 1 if already blocked
*/
char command_check_if_blocked[] = "ipset test simpleguardian %s > /dev/null 2>&1";
char *command_check_if_blocked_formatted = (char*)malloc(50 + sizeof(command_check_if_blocked) + sizeof(argv[indexIp]));
sprintf(command_check_if_blocked_formatted, command_check_if_blocked, argv[indexIp]);
return system(command_check_if_blocked_formatted) == 0;
}
int main(int argc, char **argv){
check_requirements();
if (!check_user_valid()){
printf("this program is exclusive for user %s or root\n", username);
exit(1);
}
if (argc < indexCommand + 1){
help(0);
}
setuid(0);
if (!strcmp("init", argv[indexCommand])){
if (system("ipset list simpleguardian > /dev/null 2>&1") == 0){
printf("already inited before, just flushing\n");
system("ipset flush simpleguardian > /dev/null 2>&1");
exit(0);
} else {
system("ipset create simpleguardian iphash > /dev/null 2>&1");
printf("init complete\n");
system("iptables -I INPUT 2 -m set --match-set simpleguardian src -j DROP");
exit(0);
}
}
if (argc < indexIp + 1){
help(0);
}
if (!strcmp("block", argv[indexCommand])){
const char *ip = argv[indexIp];
if (!check_ip_valid(ip)) {
printf("this IP is invalid (%s)\n", ip);
exit(1);
}
if (is_already_blocked(argv)) {
printf("this IP is already blocked\n");
exit(0);
}
printf("blocking %s\n", argv[indexIp]);
char command[] = "ipset add simpleguardian '%s' > /dev/null 2>&1";
char *command_formatted = (char*)malloc(50 + strlen(command) + strlen(ip));
sprintf(command_formatted, command, ip);
system(command_formatted);
exit(0);
} else if (!strcmp("unblock", argv[indexCommand])){
const char *ip = argv[indexIp];
if (!check_ip_valid(ip)) {
printf("this IP is invalid (%s)\n", ip);
exit(1);
}
if (!is_already_blocked(argv)) {
printf("this IP is not blocked\n");
exit(0);
}
printf("unblocking %s\n", argv[indexIp]);
char command[] = "ipset del simpleguardian '%s' > /dev/null 2>&1";
char *command_formatted = (char*)malloc(50 + strlen(command) + strlen(ip));
sprintf(command_formatted, command, ip);
system(command_formatted);
exit(0);
}
help(1);
}