-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocker
executable file
·294 lines (272 loc) · 7.56 KB
/
blocker
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
#!/usr/bin/php -q
<?php
$attempts = 3;
// Path to logpipe
// Can be created with
// mkfifo <destination>
// Remember to have this in /etc/rsyslog.d/bruteblocker.conf:
// auth.* |/opt/bruteblocker/logpipe
$file="/opt/bruteblocker/logpipe";
// Path to database that bruteblocker uses:
$db = "/opt/bruteblocker/block.db";
$ipt = new IPTables("BLOCKS","BLOCKS");
$r = new Read($file);
// Block's callback is IPTables
$b = new Block($db, $attempts, $ipt);
$data4 = $ipt->listflush4();
$b->startup($data4);
while(1){
$r->open();
while (($line=$r->getline())!==false) {
echo "<<: $line\n";
$b->insert($line);
}
$r->close();
$ipt->logger("Lost log pipe, reopening..");
sleep(1);
}
class Read {
var $sock;
var $file;
function __construct($file){
$this->file=$file;
}
function open(){
$this->sock = fopen($this->file,"r") or die("D'aww :(\n");
}
function close(){
if($this->sock) fclose($this->sock);
$this->sock = false;
}
function getline(){
$data = fgets($this->sock, 65536);
if($data===false) return false;
return trim($data, "\r\n");
}
}
class IPTables{
var $chain4;
var $chain6;
var $block4;
function __construct($chain4, $chain6){
$this->chain4 = escapeshellarg($chain4);
$this->chain6 = escapeshellarg($chain6);
}
function listflush4($syslog=true){
if($syslog) $this->logger("Taking control of iptables (v4 chain: ".$this->chain4.")");
// get IPs from iptables, add them to our list and rewrite them
$data = `iptables -L {$this->chain4} -n | grep DROP | sed 's/ */ /g' | cut -d ' ' -f 4`;
$data = trim($data);
if(empty($data)) return;
$data = explode("\n",$data);
$ips = array();
foreach($data as $d) $ips[$d]=true;
ksort($ips);
`iptables -F {$this->chain4}`;
return $ips;
}
/*function list4(){
// get IPs from iptables, add them to our list and rewrite them
// beware of the sed..
$data = `iptables -L {$this->chain4} -n | sed 's/ *'/' /g' | cut -d ' ' -f 4 | tail -n+3`;
if(empty(trim($data))) return;
$data = explode("\n",trim($data));
$ips = array();
foreach($data as $d) $ips[$d]=true;
ksort($ips);
`iptables -F {$this->chain4}`;
foreach($ips as $ip=>$v) $this->blacklist4($ip, false);
}*/
function logger($mesg){
echo "$mesg\n";
$m = escapeshellarg($mesg);
`logger -t bruteblocker -- $m`;
}
function whitelist4($ip){
if(isset($this->block4[$ip])){
$ip = escapeshellarg($ip);
$this->logger("Unblocking $ip");
$cmd="iptables -D ".$this->chain4." -s $ip -j DROP\n";
`$cmd 2>/dev/null`;
unset($this->block4[$ip]);
}
}
function whitelist6($ip){
$ip = escapeshellarg($ip);
$this->logger("Unblocking $ip");
$cmd="ip6tables -D ".$this->chain6." -s $ip -j DROP\n";
`$cmd 2>/dev/null`;
}
function blacklist4($ip, $syslog=true){
if(!isset($this->block4[$ip])){
$this->block4[$ip]=true;
$ip = escapeshellarg($ip);
if($syslog) $this->logger("Blocking $ip");
$cmd="iptables -A ".$this->chain4." -s $ip -j DROP\n";
echo `$cmd`;
}
}
function blacklist6($ip){
$ip = escapeshellarg($ip);
$this->logger("Blocking $ip");
$cmd="ip6tables -A ".$this->chain6." -s $ip -j DROP\n";
`$cmd`;
}
}
class Block{
var $file;
var $db;
var $state4;
var $state6;
var $thr;
// cb stands for callback
var $cb;
function __construct($db,$threshold,$callback){
$this->file =$db;
$this->thr = $threshold;
$this->cb = $callback;
$this->read();
}
function read(){
$this->db = unserialize(file_get_contents($this->file));
}
function write(){
file_put_contents($this->file,serialize($this->db));
}
function startup($data4){ // get in sync
$this->cb->logger("bruteblocker starting up");
foreach($this->db['blacklist4'] as $ip=>$banned){
if($banned){
$this->cb->blacklist4($ip, false);
$this->db['blacklist4'][$ip]=true;
} else{
unset($this->db['blacklist4'][$ip]);
}
}
foreach($data4 as $ip=>$banned){
if($banned){
$this->cb->blacklist4($ip, false);
$this->db['blacklist4'][$ip]=true;
}
}
$this->write();
}
function whitelist4($ip){
if(!isset($this->db['whitelist4'][$ip])){
$this->db['whitelist4'][$ip]=true;
$this->cb->whitelist4($ip);
}
$this->write();
}
function blacklist4($ip, $log=true){
if(isset($this->db['whitelist4'][$ip]) && $this->db['whitelist4'][$ip]){
return; // Cannot blacklist anything whitelisted
}
if(!isset($this->db['blacklist4'][$ip])){
$this->db['blacklist4'][$ip]=true;
$this->cb->blacklist4($ip, $log);
}
$this->write();
}
function whitelist6($ip){
if(isset($this->db['whitelist6'][$ip]) && $this->db['whitelist6'][$ip]){
return; // Cannot blacklist anything whitelisted
}
if(!isset($this->db['whitelist6'][$ip])){
$this->db['whitelist6'][$ip]=true;
$this->cb->whitelist6($ip);
}
$this->write();
}
function blacklist6($ip){
if(!isset($this->db['blacklist6'][$ip])){
$this->db['blacklist6'][$ip]=true;
$this->cb->blacklist6($ip);
}
$this->write();
}
function success4($ip){
if(isset($this->db['blacklist4'][$ip])){
echo "Warning, whitelisting a previously blacklisted ip: $ip\n";
unset($this->db['blacklist4'][$ip]);
}
if(isset($this->state4[$ip])){
unset($this->state4[$ip]);
}
$this->whitelist4($ip);
}
function success6($ip){
if(isset($this->db['blacklist6'][$ip])){
echo "Warning, whitelisting a previously blacklisted ip: $ip\n";
unset($this->db['blacklist6'][$ip]);
}
if($this->state6[$ip]>0){
unset($this->state6[$ip]);
}
$this->whitelist6($ip);
}
function failure4($ip){
if(isset($this->db['whitelist4'][$ip])) return;
if(!isset($this->state4[$ip])) $this->state4[$ip]=0;
$this->state4[$ip]++;
if($this->state4[$ip]>=$this->thr){
$this->blacklist4($ip);
}
}
function failure6($ip){
if(isset($this->db['whitelist6'][$ip])) return;
if(!isset($this->state6[$ip])) $this->state6[$ip]=0;
$this->state6[$ip]++;
if($this->state6[$ip]>=$this->thr){
$this->blacklist6($ip);
}
}
function insert($l){
$l = trim($l);
if(preg_match('/^([a-zA-Z]+\s+[0-9]+\s+[0-9:]+) ([^ ]*) sshd\[[0-9]+\]: (.*)$/', $l, $m)){
$l = $m[3];
} else if(preg_match('/^([0-9-T:\.\+]+) ([^ ]+) sshd\[[0-9]+\]: (.*)$/', $l, $m)) {
$l = $m[3];
} else {
echo "Not relevant afaik\n";
return;
}
$l = $m[3];
if(preg_match('/^Failed password for( invalid user)? ([^ ]+) from ([0-9\.]+) port ([0-9]+) ssh[12]$/', $l, $m)){
array_shift($m);
array_shift($m);
echo "failure4\n";
$this->failure4($m[1]);
$this->lastip = $m[1];
return;
} else if(preg_match('/^Failed password for( invalid user)? ([^ ]+) from ([0-9a-f:]+) port ([0-9]+) ssh[12]$/', $l, $m)){
array_shift($m);
array_shift($m);
echo "failure6\n";
$this->failure6($m[1]);
$this->lastip = $m[1];
return;
} else if(preg_match('/^Accepted password for ([^ ]+) from ([0-9a-f\.]+) port ([0-9]+) ssh[12]$/', $l, $m)){
array_shift($m);
echo "accept4\n";
$this->success4($m[1]);
return;
} else if(preg_match('/^Accepted password for ([^ ]+) from ([0-9a-f:]+) port ([0-9]+) ssh[12]$/', $l, $m)){
array_shift($m);
echo "accept6\n";
$this->success6($m[1]);
return;
} else if(preg_match('/^last message repeated (\d+) times$/', $l, $m)){
echo "failure\n";
for ( $i = 0; $i < $m[1]; $i++ ) {
$this->failure4($this->lastip);
}
return;
}
if(preg_match('/^(pam|PAM)/', $l)) return;
if(preg_match('/^Received/', $l)) return;
if(preg_match('/^Disconnecting/', $l)) return;
if(preg_match('/^Connection closed by/', $l)) return;
echo $l."\n"; // log unknown lines
}
}