Skip to content

Commit d43ac22

Browse files
author
Sylvain
committed
Update
1 parent 27824c5 commit d43ac22

5 files changed

Lines changed: 324 additions & 286 deletions

File tree

config.inc.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
#####################
3+
# CONSTANTS #
4+
#####################
5+
define("SUDO", "/run/wrappers/bin/sudo");
6+
define("F2BC", "/run/current-system/sw/bin/fail2ban-client");
7+
define("GREP", "/run/current-system/sw/bin/grep");
8+
define("AWK", "/run/current-system/sw/bin/awk");
9+
?>

engine.inc.php

Lines changed: 93 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,93 @@
1-
<?php
2-
3-
#####################
4-
# LANGUAGE #
5-
#####################
6-
$lang=substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
7-
if(stream_resolve_include_path("language/$lang.php")) {
8-
include ("language/$lang.php");
9-
} else {
10-
include ("language/fr.php");
11-
}
12-
13-
#####################
14-
# FUNCTIONS #
15-
#####################
16-
17-
function list_jails() {
18-
global $f2b;
19-
$jails=array();
20-
$erg=@exec('sudo /usr/bin/fail2ban-client status | grep "Jail list:" | awk -F ":" \'{print $2}\' | awk \'{$1=$1;print}\'');
21-
$erg=explode(",",$erg);
22-
foreach($erg as $jail) {
23-
$jails[trim($jail)]=false;
24-
}
25-
ksort($jails);
26-
return $jails;
27-
}
28-
29-
function jail_info($jail) {
30-
global $f2b;
31-
$info=array();
32-
$erg=@exec('sudo /usr/bin/fail2ban-client get '.escapeshellarg($jail).' findtime ');
33-
if(is_numeric($erg)) {
34-
$info['findtime']='findtime: '.$erg;
35-
}
36-
$erg=@exec('sudo /usr/bin/fail2ban-client get '.escapeshellarg($jail).' bantime ');
37-
if(is_numeric($erg)) {
38-
$info['bantime']='bantime: '.$erg;
39-
}
40-
$erg=@exec('sudo /usr/bin/fail2ban-client get '.escapeshellarg($jail).' maxretry ');
41-
if(is_numeric($erg)) {
42-
$info['maxretry']='maxretry: '.$erg;
43-
}
44-
return $info;
45-
}
46-
47-
function list_clients_banned($jail,$usedns) {
48-
global $f2b;
49-
$clients_banned=array();
50-
$erg=@exec('sudo /usr/bin/fail2ban-client status '.$jail.' | grep "IP list:" | awk -F "list:" \'{print$2}\' | awk \'{$1=$1;print}\'');
51-
if($erg!='') {
52-
$clients_banned=explode(" ",$erg);
53-
if($usedns==1) {
54-
foreach($clients_banned as $client_banned=>$client) {
55-
$client_dns=gethostbyaddr($client);
56-
if($client_dns==$client) {
57-
$client_dns=' ('.$GLOBALS['unknown'].')';
58-
} else {
59-
$client_dns=' ('.$client_dns.')';
60-
}
61-
$clients_banned[$client_banned].=$client_dns;
62-
}
63-
}
64-
return $clients_banned;
65-
}
66-
return false;
67-
}
68-
69-
function ban_unban_ip($action,$jail,$ip) {
70-
if($jail=='') {
71-
return 'nojailselected';
72-
} elseif(!filter_var($ip,FILTER_VALIDATE_IP)) {
73-
return 'ipnotvalid';
74-
}
75-
$erg=@exec('sudo /usr/bin/fail2ban-client set '.escapeshellarg($jail).' '.escapeshellarg($action).' '.escapeshellarg($ip));
76-
if($erg!=1) {
77-
return 'couldnot';
78-
}
79-
return 'OK';
80-
}
81-
82-
?>
1+
<?php
2+
3+
require_once('config.inc.php');
4+
5+
#####################
6+
# LANGUAGE #
7+
#####################
8+
$lang=substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
9+
if(stream_resolve_include_path("language/$lang.php")) {
10+
include ("language/$lang.php");
11+
} else {
12+
include ("language/fr.php");
13+
}
14+
15+
#####################
16+
# FUNCTIONS #
17+
#####################
18+
19+
function available() {
20+
$erg=@exec(SUDO.' '.F2BC.' status');
21+
if($erg==''){
22+
return false;
23+
} else {
24+
return true;
25+
}
26+
}
27+
28+
function list_jails() {
29+
global $f2b;
30+
$jails=array();
31+
$erg=@exec(SUDO.' '.F2BC.' status | '.GREP.' "Jail list:" | '.AWK.' -F ":" \'{print $2}\' | '.AWK.' \'{$1=$1;print}\'');
32+
$erg=explode(",",$erg);
33+
foreach($erg as $jail) {
34+
$jails[trim($jail)]=false;
35+
}
36+
ksort($jails);
37+
return $jails;
38+
}
39+
40+
function jail_info($jail) {
41+
global $f2b;
42+
$info=array();
43+
$erg=@exec(SUDO.' '.F2BC.' get '.escapeshellarg($jail).' findtime ');
44+
if(is_numeric($erg)) {
45+
$info['findtime']='findtime: '.$erg;
46+
}
47+
$erg=@exec(SUDO.' '.F2BC.' get '.escapeshellarg($jail).' bantime ');
48+
if(is_numeric($erg)) {
49+
$info['bantime']='bantime: '.$erg;
50+
}
51+
$erg=@exec(SUDO.' '.F2BC.' get '.escapeshellarg($jail).' maxretry ');
52+
if(is_numeric($erg)) {
53+
$info['maxretry']='maxretry: '.$erg;
54+
}
55+
return $info;
56+
}
57+
58+
function list_clients_banned($jail,$usedns) {
59+
global $f2b;
60+
$clients_banned=array();
61+
$erg=@exec(SUDO.' '.F2BC.' status '.$jail.' | '.GREP.' "IP list:" | '.AWK.' -F "list:" \'{print$2}\' | '.AWK.' \'{$1=$1;print}\'');
62+
if($erg!='') {
63+
$clients_banned=explode(" ",$erg);
64+
if($usedns==1) {
65+
foreach($clients_banned as $client_banned=>$client) {
66+
$client_dns=gethostbyaddr($client);
67+
if($client_dns==$client) {
68+
$client_dns=' ('.$GLOBALS['unknown'].')';
69+
} else {
70+
$client_dns=' ('.$client_dns.')';
71+
}
72+
$clients_banned[$client_banned].=$client_dns;
73+
}
74+
}
75+
return $clients_banned;
76+
}
77+
return false;
78+
}
79+
80+
function ban_unban_ip($action,$jail,$ip) {
81+
if($jail=='') {
82+
return 'nojailselected';
83+
} elseif(!filter_var($ip,FILTER_VALIDATE_IP)) {
84+
return 'ipnotvalid';
85+
}
86+
$erg=@exec(SUDO.' '.F2BC.' set '.escapeshellarg($jail).' '.escapeshellarg($action).' '.escapeshellarg($ip));
87+
if($erg!=1) {
88+
return 'couldnot';
89+
}
90+
return 'OK';
91+
}
92+
93+
?>

examples/fail2ban-web-start.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
# Poll until the socket file exists
4+
while ! [ -S "__FAIL2BAN_SOCK__" ]
5+
do
6+
sleep 1
7+
done
8+
9+
# Poll until netcat notices someone's listening on the socket
10+
while ! /bin/nc -zU "__FAIL2BAN_SOCK__"
11+
do
12+
sleep 1
13+
done
14+
15+
/bin/chmod u=rw,g=rw,o= "__FAIL2BAN_SOCK__"

examples/systemd.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Type=oneshot
88
# Only root can fully execute chmod
99
User=root
1010
Group=root
11-
ExecStart=/bin/chmod 660 /var/run/fail2ban/fail2ban.sock
11+
ExecStart=__INSTALL_DIR__/fail2ban-web-start.sh
1212

1313
[Install]
1414
WantedBy=multi-user.target

0 commit comments

Comments
 (0)