Skip to content

Commit ca48140

Browse files
add new fail2ban menu
1 parent 947cde4 commit ca48140

File tree

7 files changed

+288
-9
lines changed

7 files changed

+288
-9
lines changed

app/model/Firewall.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Ext.define('MBilling.model.Firewall', {
3939
}, {
4040
name: 'jail',
4141
type: 'string'
42-
}],
42+
}, 'idServername'],
4343
proxy: {
4444
type: 'uxproxy',
4545
module: 'firewall'

build/MagnusBilling-current.tar.gz

1.8 KB
Binary file not shown.

classic/src/view/firewall/List.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Ext.define('MBilling.view.firewall.List', {
2222
extend: 'Ext.ux.grid.Panel',
2323
alias: 'widget.firewalllist',
2424
store: 'Firewall',
25-
initComponent: function() {
25+
initComponent: function () {
2626
var me = this;
2727
me.textDelete = 'Unban';
2828
me.textNew = t('Ban new IP');
@@ -53,6 +53,16 @@ Ext.define('MBilling.view.firewall.List', {
5353
]
5454
},
5555
flex: 2
56+
}, {
57+
header: t('Server'),
58+
dataIndex: 'idServername',
59+
filter: {
60+
type: 'string',
61+
field: 'idServer.name'
62+
},
63+
flex: 3,
64+
hidden: App.user.isClient,
65+
hideable: !App.user.isClient
5666
}]
5767
me.callParent(arguments);
5868
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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+
}

protected/commands/UpdateMysqlCommand.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,32 @@ public function run($args)
20762076
$version = '7.8.5.1';
20772077
$this->update($version);
20782078
}
2079+
2080+
//2025-04-23
2081+
if ($version == '7.8.5.1') {
2082+
2083+
$sql = "INSERT INTO `pkg_module`(`id`, `text`, `module`, `icon_cls`, `id_module`, `priority`) VALUES (82,'t(\'Fail2ban\')','firewall','x-fa fa-desktop',12,82);";
2084+
$this->executeDB($sql);
2085+
2086+
$sql = "INSERT INTO `pkg_group_module` (`id_group`, `id_module`, `action`, `show_menu`, `createShortCut`, `createQuickStart`) VALUES ('1', '82', 'crud', '1', '0', '0');";
2087+
$this->executeDB($sql);
2088+
2089+
$sql = "TRUNCATE TABLE pkg_firewall";
2090+
$this->executeDB($sql);
2091+
2092+
$sql = " ALTER TABLE `pkg_firewall` ADD `id_server` INT(11) NOT NULL AFTER `jail`;";
2093+
$this->executeDB($sql);
2094+
2095+
$sql = "ALTER TABLE pkg_firewall ADD UNIQUE KEY ipperserver (ip, id_server);";
2096+
$this->executeDB($sql);
2097+
2098+
exec("echo '\n* * * * * root php /var/www/html/mbilling/cron.php failtwobanip' >> /etc/crontab");
2099+
exec("sed -i 's/ssh-iptables/sshd/g' /etc/fail2ban/jail.local");
2100+
exec("systemctl restart fail2ban");
2101+
2102+
$version = '7.8.5.2';
2103+
$this->update($version);
2104+
}
20792105
}
20802106

20812107
public function executeDB($sql)
Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Actions of module "Firewall".
45
*
@@ -11,16 +12,44 @@ class FirewallController extends Controller
1112
{
1213

1314
public $attributeOrder = 'date DESC';
14-
15+
public $extraValues = ['idServer' => 'name'];
1516
public function init()
1617
{
1718

18-
echo json_encode([
19-
$this->nameSuccess => $this->success,
20-
$this->nameRoot => $this->attributes,
21-
$this->nameMsg => $this->msg . 'This option has been discontinued.',
22-
]);
19+
$this->instanceModel = new Firewall;
20+
$this->abstractModel = Firewall::model();
21+
$this->titleReport = Yii::t('zii', 'Firewall');
2322

23+
parent::init();
2424
}
2525

26+
public function actionDestroy()
27+
{
28+
$values = $this->getAttributesRequest();
29+
$namePk = 'id';
30+
$ids = array();
31+
32+
# Se existe a chave 0, indica que existe um array interno (mais de 1 registro selecionado)
33+
if (array_key_exists(0, $values)) {
34+
# percorre o array para excluir o(s) registro(s)
35+
foreach ($values as $value) {
36+
array_push($ids, $value[$namePk]);
37+
}
38+
} else {
39+
array_push($ids, $values[$namePk]);
40+
}
41+
42+
foreach ($ids as $value) {
43+
44+
45+
$model = Firewall::model()->findByPk($value);
46+
$model->action = 3;
47+
$model->save();
48+
}
49+
50+
echo json_encode(array(
51+
$this->nameSuccess => true,
52+
$this->nameMsg => 'The IP wil unban in 1 minute',
53+
));
54+
}
2655
}

protected/models/Firewall.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Modelo para a tabela "Firewall".
45
* =======================================
@@ -54,10 +55,32 @@ public function rules()
5455
{
5556
$rules = [
5657
['ip, action', 'required'],
57-
['action', 'numerical', 'integerOnly' => true],
58+
['action, id_server', 'numerical', 'integerOnly' => true],
5859
['description,jail', 'length', 'max' => 200],
60+
['ip', 'checkip'],
5961

6062
];
6163
return $this->getExtraField($rules);
6264
}
65+
66+
public function checkip($attribute, $params)
67+
{
68+
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $this->ip)) {
69+
$this->addError($attribute, Yii::t('zii', 'The IP is not valid'));
70+
}
71+
}
72+
73+
public function relations()
74+
{
75+
return [
76+
'idServer' => [self::BELONGS_TO, 'Servers', 'id_server'],
77+
];
78+
}
79+
public function beforeSave()
80+
{
81+
if ($this->getIsNewRecord()) {
82+
$this->id_server = 1;
83+
}
84+
return parent::beforeSave();
85+
}
6386
}

0 commit comments

Comments
 (0)