|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * ======================================= |
| 5 | + * ################################### |
| 6 | + * MagnusBilling |
| 7 | + * |
| 8 | + * @package MagnusBilling |
| 9 | + * @author Adilson Leffa Magnus. |
| 10 | + * @copyright Copyright (C) 2005 - 2023 MagnusSolution. All rights reserved. |
| 11 | + * ################################### |
| 12 | + * |
| 13 | + * This software is released under the terms of the GNU Lesser General Public License v2.1 |
| 14 | + * A copy of which is available from http://www.gnu.org/copyleft/lesser.html |
| 15 | + * |
| 16 | + * Please submit bug reports, patches, etc to https://github.com/magnusbilling/mbilling/issues |
| 17 | + * ======================================= |
| 18 | + * Magnusbilling.com <[email protected]> |
| 19 | + * |
| 20 | + */ |
| 21 | +class FailtwobanipCommand extends ConsoleCommand |
| 22 | +{ |
| 23 | + protected $resultBanIps = []; |
| 24 | + protected $resultUnBanIps = []; |
| 25 | + public function run($args) |
| 26 | + { |
| 27 | + |
| 28 | + /* |
| 29 | + sed -i 's/ssh-iptables/sshd/g' /etc/fail2ban/jail.local |
| 30 | +
|
| 31 | +
|
| 32 | + echo "[ip-blacklist] |
| 33 | +enabled = true |
| 34 | +maxretry = 0 |
| 35 | +findtime = 15552000 |
| 36 | +bantime = -1" >> /etc/fail2ban/jail.local |
| 37 | +
|
| 38 | +echo "[Definition] |
| 39 | +failregex = ^<HOST> \[.*\]$ |
| 40 | +ignoreregex = |
| 41 | +" > /etc/fail2ban/filter.d/ip-blacklist.conf |
| 42 | +
|
| 43 | + systemctl restart fail2ban |
| 44 | + fail2ban-client status |
| 45 | + |
| 46 | +
|
| 47 | + cd /root/.ssh |
| 48 | + ssh-keygen -t rsa -N "" -f id_rsa |
| 49 | + cat ~/.ssh/id_rsa.pub |
| 50 | +
|
| 51 | + add the ~/.ssh/id_rsa.pub on /root/.ssh/authorized_keys of the proxy and slaves. |
| 52 | + */ |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + $sql = 'SELECT ip FROM pkg_firewall WHERE action = 3'; |
| 57 | + $command = Yii::app()->db->createCommand($sql); |
| 58 | + $this->resultUnBanIps = $command->queryAll(); |
| 59 | + |
| 60 | + $sql = 'DELETE FROM pkg_firewall WHERE action = 3'; |
| 61 | + $command = Yii::app()->db->createCommand($sql); |
| 62 | + |
| 63 | + |
| 64 | + $sql = 'SELECT ip FROM pkg_firewall WHERE action = 1'; |
| 65 | + $command = Yii::app()->db->createCommand($sql); |
| 66 | + $this->resultBanIps = $command->queryAll(); |
| 67 | + |
| 68 | + |
| 69 | + $sql = 'TRUNCATE TABLE pkg_firewall'; |
| 70 | + Yii::app()->db->createCommand($sql)->execute(); |
| 71 | + |
| 72 | + $modelServers = Servers::model()->findAll('status IN (1,3,4)'); |
| 73 | + |
| 74 | + if (! isset($modelServers[0])) { |
| 75 | + |
| 76 | + $modelServers = new Servers; |
| 77 | + |
| 78 | + |
| 79 | + $modelServers->name = 'Master'; |
| 80 | + $modelServers->host = 'localhost'; |
| 81 | + $modelServers->type = 'mbilling'; |
| 82 | + $modelServers->port = '5038'; |
| 83 | + $modelServers->username = 'magnus'; |
| 84 | + $modelServers->password = 'magnussolution'; |
| 85 | + $modelServers->status = '1'; |
| 86 | + $modelServers->description = '1'; |
| 87 | + $modelServers->save(); |
| 88 | + $modelServers = Servers::model()->findAll('status IN (1,3,4)'); |
| 89 | + } |
| 90 | + |
| 91 | + foreach ($modelServers as $key => $server) { |
| 92 | + |
| 93 | + echo $server['host'] . "\n"; |
| 94 | + |
| 95 | + if ($server['type'] == 'sipproxy') { |
| 96 | + |
| 97 | + $this->getLinesCommand('ip-blacklist', 1, $server); |
| 98 | + $this->getLinesCommand('opensips-iptables', 0, $server); |
| 99 | + } else { |
| 100 | + |
| 101 | + $this->getLinesCommand('ip-blacklist', 1, $server); |
| 102 | + $this->getLinesCommand('asterisk-iptables', 0, $server); |
| 103 | + $this->getLinesCommand('sshd', 0, $server); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public function getLinesCommand($command, $action = 0, $server) |
| 109 | + { |
| 110 | + if ($server['type'] == 'mbilling') { |
| 111 | + |
| 112 | + foreach ($this->resultUnBanIps as $unbanIP) { |
| 113 | + |
| 114 | + echo "unbanip IP " . $unbanIP['ip'] . "\n"; |
| 115 | + |
| 116 | + @shell_exec("sudo fail2ban-client set asterisk-iptables unbanip " . $unbanIP['ip']); |
| 117 | + @shell_exec("sudo fail2ban-client set ip-blacklist unbanip " . $unbanIP['ip']); |
| 118 | + @shell_exec("sudo fail2ban-client set sshd unbanip " . $unbanIP['ip']); |
| 119 | + } |
| 120 | + if ($command == 'ip-blacklist') { |
| 121 | + |
| 122 | + foreach ($this->resultBanIps as $blokedIP) { |
| 123 | + $status = shell_exec("fail2ban-client set ip-blacklist banip " . $blokedIP['ip']); |
| 124 | + |
| 125 | + $sqlCheck = "SELECT COUNT(*) FROM pkg_firewall WHERE ip = '" . $blokedIP['ip'] . "' AND id_server = '" . $server['id'] . "'"; |
| 126 | + $exists = Yii::app()->db->createCommand($sqlCheck)->queryScalar(); |
| 127 | + if ($exists > 0) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + $sql = "INSERT INTO pkg_firewall (ip,action, date, description, jail, id_server) VALUES ('" . $blokedIP['ip'] . "',1, NOW(), '" . $server['name'] . "','$command','" . $server['id'] . "')"; |
| 132 | + try { |
| 133 | + Yii::app()->db->createCommand($sql)->execute(); |
| 134 | + } catch (Exception $e) { |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + $status = shell_exec("fail2ban-client status " . $command); |
| 139 | + } else { |
| 140 | + |
| 141 | + foreach ($this->resultUnBanIps as $unbanIP) { |
| 142 | + |
| 143 | + echo "unbanip IP " . $unbanIP['ip'] . "\n"; |
| 144 | + |
| 145 | + @shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p 22 "fail2ban-client set asterisk-iptables unbanip ' . $unbanIP['ip'] . '" '); |
| 146 | + @shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p 22 "fail2ban-client set ip-blacklist unbanip ' . $unbanIP['ip'] . '" '); |
| 147 | + @shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p 22 "fail2ban-client set sshd unbanip ' . $unbanIP['ip'] . '" '); |
| 148 | + @shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p 22 "fail2ban-client set opensips-iptables unbanip ' . $unbanIP['ip'] . '" '); |
| 149 | + } |
| 150 | + |
| 151 | + if ($command == 'ip-blacklist') { |
| 152 | + foreach ($this->resultBanIps as $blokedIP) { |
| 153 | + $status = shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p 22 "fail2ban-client set ip-blacklist banip ' . $blokedIP['ip'] . '" '); |
| 154 | + |
| 155 | + $sqlCheck = "SELECT COUNT(*) FROM pkg_firewall WHERE ip = '" . $blokedIP['ip'] . "' AND id_server = '" . $server['id'] . "'"; |
| 156 | + $exists = Yii::app()->db->createCommand($sqlCheck)->queryScalar(); |
| 157 | + if ($exists > 0) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + $sql = "INSERT INTO pkg_firewall (ip,action, date, description, jail, id_server) VALUES ('" . $blokedIP['ip'] . "',1, NOW(), '" . $server['name'] . "','$command','" . $server['id'] . "')"; |
| 161 | + try { |
| 162 | + Yii::app()->db->createCommand($sql)->execute(); |
| 163 | + } catch (Exception $e) { |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + $status = shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p 22 "fail2ban-client status ' . $command . '" '); |
| 170 | + } |
| 171 | + |
| 172 | + preg_match('/Banned IP list:\s*(.*)/', $status, $ipMatches); |
| 173 | + |
| 174 | + if (!isset($ipMatches[1]) || empty($ipMatches[1])) { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + $ips = array_filter(array_map('trim', explode(' ', $ipMatches[1]))); |
| 179 | + |
| 180 | + |
| 181 | + foreach ($ips as $ip) { |
| 182 | + $sql = "INSERT INTO pkg_firewall (ip,action, date, description, jail, id_server) VALUES ('$ip',$action, NOW(), '" . $server['name'] . "','$command','" . $server['id'] . "')"; |
| 183 | + |
| 184 | + echo $sql; |
| 185 | + try { |
| 186 | + Yii::app()->db->createCommand($sql)->execute(); |
| 187 | + } catch (Exception $e) { |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments