-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction.php
157 lines (137 loc) · 4.16 KB
/
function.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
function version() {
return "1.0";
}
function easyCheck($range, $value) {
/* Perform simple comparison if you don't require a postscreen regex */
if ( strpos($range,'[') === FALSE ) {
if ($range == $value) return TRUE;
else return FALSE;
}
return NULL;
}
function ipInRange($iprange, $ip) {
/* Check Postscreen range */
/* Shortcut */
if ( !is_null( $check = easyCheck($iprange, $ip) ) )
return $check;
/* Hard work */
$re = '/(?:(?P<part>\d{1,3})|(?P<part2>\[\d{1,3}(?:\.{2}|\;)\d{1,3}\]))/';
if ( preg_match_all($re, $iprange, $matches) === FALSE )
return FALSE;
$rangevet = $matches[0];
$ipvet=explode('.',$ip);
if ( count($rangevet) != 4 ) return FALSE;
if ( count($ipvet) != 4 ) return FALSE;
$inrange = FALSE;
for ($i=0;$i<4;$i++) {
if ( easyCheck($rangevet[$i], $ipvet[$i]) )
$inrange = TRUE;
else {
if ( preg_match('/\[(?P<start>\d{1,3})(?:\.\.|\;)(?P<stop>\d{1,3})\]/',$rangevet[$i],$opt) === 1 ) {
if ( strpos($rangevet[$i], '..') !== FALSE ) {
if ( ($ipvet[$i] <= $opt['stop']) AND ($ipvet[$i] >= $opt['start']) )
$inrange = TRUE;
else
$inrange = FALSE;
}
else
if ( strpos($rangevet[$i], ';') ) {
if (( $ipvet[$i] == $opt['start'] ) OR ( $ipvet[$i] == $opt['stop'] ))
$inrange = TRUE;
else
$inrange = FALSE;
}
}
}
if (!$inrange)
return FALSE;
}
return TRUE;
}
function readList($list) {
/* Return ordered list postscreen DNSBL array, FALSE on parse error */
$regexp= '/(?P<name>[\.\w]+)(?:\=(?P<value>[\d+\.\[\;\]]+)){0,1}(?:\*(?P<score>(\-){0,1}\d+)){0,1}/';
$dnsbl= array();
foreach ( $list as $item ) {
if ( preg_match($regexp,$item,$found)=== FALSE )
return FALSE;
$err= preg_last_error();
if ($err != PREG_NO_ERROR)
syslog(LOG_ERR,"Error reading line <$item>: $err");
if ( empty($found['score']) )
$found['score'] = 1;
$dnsbl[] = array('name' => $found['name'], 'value' => $found['value'], 'score' => $found['score']);
}
return $dnsbl;
}
function update(&$array,$value, $score) {
/* Used only in checkList */
$array['values'][] = $value;
$array['scores'][] = $score;
return TRUE;
}
function checkList($ip, $list, &$alarm=FALSE) {
$return=array();
if ( !filter_var($ip, FILTER_VALIDATE_IP, array('flags'=> FILTER_FLAG_IPV4)) )
return $return;
require_once 'vendor/autoload.php';
$dnsbl = new \DNSBL\DNSBL(array(
'blacklists' => array_column($list, 'name')
));
$alarm = FALSE; /* Will be TRUE if ANY is blocklisted somewhere */
if ( $dnsbl->isListed($ip, TRUE) ) {
$return['score'] = 0;
$result = $dnsbl->getDetails($ip, TRUE);
foreach ( $result as $blname => $detail ) {
$keys=array_keys(array_column($list,'name'),$blname);
foreach ($keys as $key) {
$update = FALSE;
foreach ( $detail as $thisd ) {
if ( isset($thisd['ip']) ) { /* Is listed with value $thisd['ip']! */
if ( empty($list[$key]['value']) )
$update = update($return["$blname"],$thisd['ip'],$list[$key]['score']);
else {
if (ipInRange($list[$key]['value'],$thisd['ip']))
$update = update($return["$blname"],$thisd['ip'],$list[$key]['score']);
}
}
if ( $update ) { /* The reason and the name */
$return["$blname"]['name'] = $list[$key]['name'];
if ( (!$alarm) AND ($list[$key]['score']>0) )
$alarm = TRUE;
if ( isset($thisd['txt']) )
$return["$blname"]['reason'] = $thisd['txt'];
}
}
if ( $update ) {
if ( !isset($return["$blname"]['score']) )
$return["$blname"]['score'] = $list[$key]['score'];
else
$return["$blname"]['score'] += $list[$key]['score'];
$return['score'] += $list[$key]['score'];
}
}
}
}
return $return;
}
/* WEB */
function printTableHeader($title,$content,$footer=FALSE,$fcontent) {
print <<<END
<caption>$title</caption>
<thead>
<tr>
END;
$cols = count($content);
for ($i=0; $i<$cols; $i++)
print '<th>'.$content[$i].'</th>';
print '</tr></thead>';
if ($footer) {
print '<tfoot><tr>';
print "<th colspan=\"$cols\">".$fcontent.'</th>';
print '</tr></tfoot>';
}
return TRUE;
}
?>