Skip to content

Commit 8bcf82a

Browse files
improve security
1 parent 3e880f0 commit 8bcf82a

File tree

4 files changed

+77
-32
lines changed

4 files changed

+77
-32
lines changed

build/MagnusBilling-current.tar.gz

-99.4 MB
Binary file not shown.

protected/commands/FailtwobanipCommand.php

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,19 @@ public function run($args)
5353
add the ~/.ssh/id_rsa.pub on /root/.ssh/authorized_keys of the proxy and slaves.
5454
*/
5555

56+
$action = [
57+
['0', 'Temp ban'],
58+
['1', 'Permanent ban'],
59+
['3', 'Unban'],
60+
['5', 'Add to IgnoreIP']
61+
];
5662

5763

5864
if (isset($args[0])) {
5965
$this->ssh_port = $args[0];
6066
}
6167

62-
68+
//only execute this script if the admin is logged on magnusbilling
6369
$sql = "SELECT count(id) as logged FROM pkg_log where id_log_actions = 1 AND date >= NOW() - INTERVAL (SELECT config_value FROM pkg_configuration WHERE config_key = 'session_timeout') SECOND AND id_user IN (SELECT id FROM pkg_user WHERE id_group IN (SELECT id FROM pkg_group_user WHERE id_user_type = 1))";
6470
$command = Yii::app()->db->createCommand($sql);
6571
$resultAdmins = $command->queryAll();
@@ -68,21 +74,24 @@ public function run($args)
6874
return;
6975
}
7076

71-
77+
//get all ips that action is 3 (to unban)
7278
$sql = 'SELECT ip FROM pkg_firewall WHERE action = 3';
7379
$command = Yii::app()->db->createCommand($sql);
7480
$this->resultUnBanIps = $command->queryAll();
7581

82+
83+
//delete all that already added on $this->resultUnBanIps
7684
foreach ($this->resultUnBanIps as $unbanIP) {
7785
$sql = 'DELETE FROM pkg_firewall WHERE ip = "' . $unbanIP['ip'] . '"';
7886
Yii::app()->db->createCommand($sql)->execute();
7987
}
8088

89+
//get all ips that action is 1 (Permanent ban)
8190
$sql = 'SELECT ip FROM pkg_firewall WHERE action = 1';
8291
$command = Yii::app()->db->createCommand($sql);
8392
$this->resultBanIps = $command->queryAll();
8493

85-
94+
//get all ips that action is 5 (Add to IgnoreIP) and add it to $this->ignogeips
8695
$sql = 'SELECT ip FROM pkg_firewall WHERE action = 5';
8796
$command = Yii::app()->db->createCommand($sql);
8897
$modelServersIgnoreIPs = $command->queryAll();
@@ -91,18 +100,19 @@ public function run($args)
91100
$this->ignogeips .= $server['ip'] . " ";
92101
}
93102

94-
103+
//get all ips of the clients and add it to $this->ignogeips
95104
$sql = 'SELECT host FROM pkg_sip JOIN pkg_user ON pkg_sip.id_user = pkg_user.id WHERE pkg_user.active = 1 AND host != "dynamic"';
96105
$command = Yii::app()->db->createCommand($sql);
97-
$modelServersIgnoreIPs = $command->queryAll();
106+
$modelServersIgnoreIPsSips = $command->queryAll();
98107

99-
foreach ($modelServersIgnoreIPs as $key => $server) {
108+
foreach ($modelServersIgnoreIPsSips as $key => $server) {
100109
$this->ignogeips .= $server['host'] . " ";
101110
}
102111

112+
//get all ips of the trunks and add it to $this->ignogeips
103113
$sql = 'SELECT host FROM pkg_trunk WHERE status = 1 AND host != "dynamic"';
104114
$command = Yii::app()->db->createCommand($sql);
105-
$modelServersIgnoreIPs = $command->queryAll();
115+
$modelServersIgnoreIPstrunks = $command->queryAll();
106116

107117
foreach ($modelServersIgnoreIPs as $key => $server) {
108118
$this->ignogeips .= $server['host'] . " ";
@@ -115,10 +125,12 @@ public function run($args)
115125
echo "\nresultBanIps";
116126
print_r($this->resultBanIps);
117127

128+
//truncate the table
118129
$sql = 'TRUNCATE TABLE pkg_firewall';
119130
Yii::app()->db->createCommand($sql)->execute();
120131

121132

133+
//insert all the ips with status 5 (IgnoreIP) again to the table
122134
foreach ($modelServersIgnoreIPs as $key => $server) {
123135

124136
if (strlen($server['ip']) > 5) {
@@ -134,6 +146,7 @@ public function run($args)
134146

135147
$modelServers = Servers::model()->findAll('status IN (1,3,4)');
136148

149+
//if there no server, add the localhost
137150
if (! isset($modelServers[0])) {
138151

139152
$modelServers = new Servers;
@@ -150,21 +163,16 @@ public function run($args)
150163
$modelServers->save();
151164
$modelServers = Servers::model()->findAll('status IN (1,3,4)');
152165
}
153-
166+
//include all the servers on $this->ignogeips
154167
foreach ($modelServers as $key => $server) {
155168
if ($server['host'] != 'localhost') {
156169
$this->ignogeips .= $server['host'] . " ";
157170
}
158171
}
159172

160-
161-
162-
$modelServers = Servers::model()->findAll('status IN (1,3,4)');
163-
173+
//loop for all the servers to process the data
164174
foreach ($modelServers as $key => $server) {
165175

166-
echo "\n" . $server['host'] . "\n\n";
167-
168176
if ($server['type'] == 'sipproxy') {
169177

170178
$this->getLinesCommand('ip-blacklist', 1, $server);
@@ -183,27 +191,36 @@ public function getLinesCommand($command, $action = 0, $server)
183191

184192
echo "sed -i 's/^ignoreip = .*/" . $this->ignogeips . "/' /etc/fail2ban/jail.local\n";
185193

194+
195+
//if is master server
186196
if ($server['type'] == 'mbilling') {
187197

198+
//add the ignore ips to jail.local and reload fail2ban
188199
shell_exec("sed -i 's/^ignoreip = .*/" . $this->ignogeips . "/' /etc/fail2ban/jail.local");
189200
shell_exec("systemctl reload fail2ban");
190201

191-
202+
//unban all the ips of $this->resultUnBanIps
192203
foreach ($this->resultUnBanIps as $unbanIP) {
193204
echo "unbanip IP " . $unbanIP['ip'] . " on MASTER\n";
194205
@shell_exec("sudo fail2ban-client unban " . $unbanIP['ip']);
195206
}
207+
208+
//if command is ip-blacklist
196209
if ($command == 'ip-blacklist') {
197210

198211
foreach ($this->resultBanIps as $blokedIP) {
212+
//ban the ip on ip-blacklist jail
199213
$status = shell_exec("fail2ban-client set ip-blacklist banip " . $blokedIP['ip']);
200214

215+
216+
//check if exist on the table pkg_firewall
201217
$sqlCheck = "SELECT COUNT(*) FROM pkg_firewall WHERE ip = '" . $blokedIP['ip'] . "' AND id_server = '" . $server['id'] . "'";
202218
$exists = Yii::app()->db->createCommand($sqlCheck)->queryScalar();
203219
if ($exists > 0) {
204220
continue;
205221
}
206222

223+
//if not exist, add it
207224
$sql = "INSERT INTO pkg_firewall (ip,action, date, description, jail, id_server) VALUES ('" . $blokedIP['ip'] . "',1, NOW(), '" . $server['name'] . "','$command','" . $server['id'] . "')";
208225
echo $sql . "\n";
209226
try {
@@ -212,28 +229,41 @@ public function getLinesCommand($command, $action = 0, $server)
212229
}
213230
}
214231
}
232+
//get all ips banned on the jail
215233
$status = shell_exec("fail2ban-client status " . $command);
216234
} else {
217235

236+
//if is a Slave or proxy execute the commands via SSH
237+
238+
239+
//add the ignore ips to jail.local and reload fail2ban
218240
@shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p ' . $this->ssh_port . ' "sed -i \'s/^ignoreip = .*/' . $this->ignogeips . '/\' /etc/fail2ban/jail.local" ');
219241
@shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p ' . $this->ssh_port . ' "systemctl reload fail2ban"');
220242

221243
foreach ($this->resultUnBanIps as $unbanIP) {
222-
244+
//unban all the ips of $this->resultUnBanIps
223245
echo "unbanip IP " . $unbanIP['ip'] . " on " . $server['host'] . "\n";
224-
225246
@shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p ' . $this->ssh_port . ' "fail2ban-client unban ' . $unbanIP['ip'] . '" ');
226247
}
227248

249+
250+
//if command is ip-blacklist
251+
228252
if ($command == 'ip-blacklist') {
253+
254+
229255
foreach ($this->resultBanIps as $blokedIP) {
256+
257+
//ban the ip on ip-blacklist jail
230258
$status = shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p ' . $this->ssh_port . ' "fail2ban-client set ip-blacklist banip ' . $blokedIP['ip'] . '" ');
231259

260+
//check if exist on the table pkg_firewall
232261
$sqlCheck = "SELECT COUNT(*) FROM pkg_firewall WHERE ip = '" . $blokedIP['ip'] . "' AND id_server = '" . $server['id'] . "'";
233262
$exists = Yii::app()->db->createCommand($sqlCheck)->queryScalar();
234263
if ($exists > 0) {
235264
continue;
236265
}
266+
//if not exist, add it
237267
$sql = "INSERT INTO pkg_firewall (ip,action, date, description, jail, id_server) VALUES ('" . $blokedIP['ip'] . "',1, NOW(), '" . $server['name'] . "','$command','" . $server['id'] . "')";
238268
echo $sql . "\n";
239269
try {
@@ -242,12 +272,14 @@ public function getLinesCommand($command, $action = 0, $server)
242272
}
243273
}
244274
}
245-
275+
//get all ips banned on the jail
246276
$status = shell_exec('ssh -o StrictHostKeyChecking=no root@' . $server['host'] . ' -p ' . $this->ssh_port . ' "fail2ban-client status ' . $command . '" ');
247277
}
248278

279+
//get all the ips banned
249280
preg_match('/Banned IP list:\s*(.*)/', $status, $ipMatches);
250281

282+
//if there no ips, return
251283
if (!isset($ipMatches[1]) || empty($ipMatches[1])) {
252284
return;
253285
}
@@ -256,6 +288,7 @@ public function getLinesCommand($command, $action = 0, $server)
256288

257289

258290
foreach ($ips as $ip) {
291+
//insert the ips on the table.
259292
$sql = "INSERT INTO pkg_firewall (ip,action, date, description, jail, id_server) VALUES ('$ip',$action, NOW(), '" . $server['name'] . "','$command','" . $server['id'] . "')";
260293

261294
echo $sql . "\n";

protected/components/LinuxAccess.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Classe de com funcionalidades globais
45
*
@@ -13,20 +14,21 @@ public static function exec($command)
1314
{
1415

1516
Yii::log('LinuxAccess::exec -> ' . $command, 'error');
16-
exec($command, $output);
17+
$sanitized = escapeshellcmd($command);
18+
exec($sanitized, $output);
1719
return $output;
1820
}
1921

2022
public static function getDirectoryDiskSpaceUsed($filter = '*', $directory = '/var/spool/asterisk/monitor/')
2123
{
22-
$command = 'ls -lR ' . $directory . $filter . ' | grep -v \'^d\' | awk \'{total += $5} END {print total}\'';
23-
return @LinuxAccess::exec($command);
24+
25+
$command = 'ls -lR ' . escapeshellarg($directory) . escapeshellarg($filter) . ' | grep -v \'^d\' | awk \'{total += $5} END {print total}\'';
26+
return @self::exec($command);
2427
}
2528

2629
public static function getLastFileInDirectory($filter = '*', $directory = '/var/spool/asterisk/monitor/')
2730
{
28-
$command = 'ls -tr ' . $directory . $filter . ' | head -n 1';
29-
return @LinuxAccess::exec($command);
31+
$command = 'ls -tr ' . escapeshellarg($directory) . escapeshellarg($filter) . ' | head -n 1';
32+
return @self::exec($command);
3033
}
31-
3234
}

protected/components/SLUserSave.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* =======================================
45
* ###################################
@@ -23,7 +24,8 @@ class SLUserSave
2324
public static function saveUserSLCurl($modelUser, $SLAppToken, $SLAccessToken, $showError = true)
2425
{
2526
$url = "http://api.superlogica.net:80/v2/financeiro/clientes";
26-
$params = ["ST_NOME_SAC" => $modelUser->firstname . ' ' . $modelUser->lastname,
27+
$params = [
28+
"ST_NOME_SAC" => $modelUser->firstname . ' ' . $modelUser->lastname,
2729
"ST_NOMEREF_SAC" => $modelUser->username,
2830
"ST_DIAVENCIMENTO_SAC" => date('d'),
2931
"ST_CGC_SAC " => $modelUser->doc,
@@ -46,12 +48,13 @@ public static function saveUserSLCurl($modelUser, $SLAppToken, $SLAccessToken, $
4648
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
4749
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $modelUser->getIsNewRecord() ? "POST" : "PUT");
4850

49-
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded",
51+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
52+
"Content-Type: application/x-www-form-urlencoded",
5053
"app_token: " . $SLAppToken,
5154
"access_token:" . $SLAccessToken,
5255
]);
5356

54-
if ( ! $modelUser->getIsNewRecord()) {
57+
if (! $modelUser->getIsNewRecord()) {
5558
$params['ID_SACADO_SAC'] = $modelUser->id_sacado_sac;
5659
}
5760

@@ -76,6 +79,12 @@ public static function saveUserSLCurl($modelUser, $SLAppToken, $SLAccessToken, $
7679

7780
public static function criarBoleto($methodPay, $modelUser)
7881
{
82+
83+
$amount = isset($_GET['amount']) ? floatval($_GET['amount']) : 0.0;
84+
if ($amount <= 0) {
85+
throw new Exception('Valor inválido');
86+
}
87+
7988
$url = "http://api.superlogica.net:80/v2/financeiro/cobranca";
8089
$ch = curl_init();
8190
curl_setopt($ch, CURLOPT_URL, $url);
@@ -84,26 +93,27 @@ public static function criarBoleto($methodPay, $modelUser)
8493
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
8594
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
8695
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
87-
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded",
96+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
97+
"Content-Type: application/x-www-form-urlencoded",
8898
"app_token: " . $methodPay->SLAppToken,
8999
"access_token:" . $methodPay->SLAccessToken,
90100
]);
91101

92-
$SLparams = ["ID_SACADO_SAC" => $modelUser->id_sacado_sac,
102+
$SLparams = [
103+
"ID_SACADO_SAC" => $modelUser->id_sacado_sac,
93104
"ST_NOMEREF_SAC" => $modelUser->username,
94105
"COMPO_RECEBIMENTO" => [[
95106
'ID_PRODUTO_PRD' => $methodPay->SLIdProduto,
96-
"VL_UNITARIO_PRD" => $_GET['amount'],
107+
"VL_UNITARIO_PRD" => $amount,
97108
"NM_QUANTIDADE_COMP" => 1,
98109
]],
99-
"VL_EMITIDO_RECB" => $_GET['amount'],
110+
"VL_EMITIDO_RECB" => $amount,
100111
"DT_VENCIMENTO_RECB" => date("m/d/Y", mktime(0, 0, 0, date("m"), date("d") + 7, date("Y"))),
101112

102113
];
103114

104115
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($SLparams));
105116
$response = (array) json_decode(curl_exec($ch));
106117
curl_close($ch);
107-
108118
}
109119
}

0 commit comments

Comments
 (0)