-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipe-ban.php
executable file
·143 lines (126 loc) · 3.09 KB
/
pipe-ban.php
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
#!/usr/bin/php5
<?php
define('WORKING_DIR', __DIR__ . '/');
include WORKING_DIR . 'shell-common.php';
function is_ip($string)
{
return preg_match('/^\d{1,3}((\.\d{1,3}){3}|(\.\d{1,3}){0,2}\.\*)$/', $string);
}
function incorrect_arguments()
{
$file = basename(__FILE__);
print "Usage: ./$file [{block|unblock|list} default=block]".
" [valid ip-address] {tr203|tr206|tr600|...|all}\n";
exit();
}
function getBanTrackers($name)
{
if ($name == 'all')
{
return getAllTrackers();
}
return getTrackers(array($name));
}
function blockIp($ip, $tracker)
{
if ($tracker == 'all')
{
print "Blocking $ip from all trackers... ";
shell_exec("sudo iptables -t filter --append INPUT -p tcp -s $ip -j REJECT > /dev/null 2>&1");
print "[OK]\n";
}
else
{
$trackers = getBanTrackers($tracker);
foreach ($trackers as $tracker => $config)
{
print "Blocking $ip from $tracker tracker... ";
shell_exec("sudo iptables -t filter --append INPUT -p tcp -s $ip --dport $config[port] -j REJECT > /dev/null 2>&1");
print "[OK]\n";
}
}
shell_exec("sudo iptables-save > /dev/null 2>&1");
}
function unblockIp($ip, $tracker)
{
if ($tracker == 'all')
{
print "Allowing $ip to all trackers... ";
shell_exec("sudo iptables -t filter --delete INPUT -p tcp -s $ip -j REJECT > /dev/null 2>&1");
$singleTrackers = getAllTrackers();
foreach ($singleTrackers as $singleTracker => $config)
{
shell_exec("sudo iptables -t filter --delete INPUT -p tcp -s $ip --dport $config[port] -j REJECT > /dev/null 2>&1");
}
print "[OK]\n";
}
else
{
$trackers = getBanTrackers($tracker);
foreach ($trackers as $tracker => $config)
{
print "Allowing $ip to $tracker all tracker.. ";
shell_exec("sudo iptables -t filter --delete INPUT -p tcp -s $ip --dport $config[port] -j REJECT > /dev/null 2>&1");
print "[OK]\n";
}
}
shell_exec("sudo iptables-save > /dev/null 2>&1");
}
function listTrackers($tracker)
{
$trackers = getBanTrackers($tracker);
foreach ($trackers as $tracker => $config)
{
$data = shell_exec("sudo iptables -t filter --list");
print "Blocked IPs for tracker $tracker:\n";
$data = explode("\n", $data);
$output = array();
foreach ($data as $line)
{
if (preg_match('/REJECT\s+tcp[\s\-]+([\d\.]+)(.*dpt:' . $config['port'] . '|(?!.*dpt:).*$)/us', $line, $ip)) {
$output[ip2long($ip[1])] = $ip[1];
}
}
$output = array_unique($output);
ksort($output);
print implode("\n", $output) . "\n";
}
}
// read input arguments
$params = arguments($argv);
$params = $params['input'];
if (empty($params[0]))
{
incorrect_arguments();
}
if (is_ip($params[0]))
{
array_unshift($params, 'block');
}
print "Pipe-server Ban tool v1.0.0\n";
switch ($params[0])
{
case 'block':
if (empty($params[1]) || empty($params[2]))
{
incorrect_arguments();
}
blockIp($params[1], $params[2]);
break;
case 'unblock':
if (empty($params[1]) || empty($params[2]))
{
incorrect_arguments();
}
unblockIp($params[1], $params[2]);
break;
case 'list':
if (empty($params[1]))
{
incorrect_arguments();
}
listTrackers($params[1]);
break;
default:
incorrect_arguments();
}