= 8) { $hostname = htmlspecialchars(gethostbyaddr($getpfhostname), ENT_QUOTES); } else { $hostname = $getpfhostname; } if ($hostname == $getpfhostname) { $hostname = 'unknown'; } echo $hostname; die; } require_once("util.inc"); require_once("guiconfig.inc"); require_once("/usr/local/pkg/pfblockerng/pfblockerng.inc"); global $rule_list; pfb_global(); // Application Paths $pathgeoip = "/usr/pbi/pfblockerng-" . php_uname("m") . "/bin/geoiplookup"; $pathgeoip6 = "/usr/pbi/pfblockerng-" . php_uname("m") . "/bin/geoiplookup6"; // Define File Locations $filter_logfile = "{$g['varlog_path']}/filter.log"; $pathgeoipdat = "/usr/pbi/pfblockerng-" . php_uname("m") . "/share/GeoIP/GeoIP.dat"; $pathgeoipdat6 = "/usr/pbi/pfblockerng-" . php_uname("m") . "/share/GeoIP/GeoIPv6.dat"; // Define Alerts Log filter Rollup window variable. (Alert Filtering Code adapted from B.Meeks - Snort Package) $pfb['filterlogentries'] = FALSE; // Emerging Threats IQRisk Header Name Reference $pfb['et_header'] = TRUE; $et_header = $config['installedpackages']['pfblockerngreputation']['config'][0]['et_header']; if (empty($et_header)) $pfb['et_header'] = FALSE; // Collect pfBlockerNGSuppress Alias and Create pfbsuppression.txt if ($pfb['supp'] == "on") pfb_create_suppression_file(); // Collect Number of Suppressed Hosts if (file_exists("{$pfb['supptxt']}")) { $pfbsupp_cnt = exec ("/usr/bin/grep -c ^ {$pfb['supptxt']}"); } else { $pfbsupp_cnt = 0; } // Collect pfBlockerNG Rule Names and Number $rule_list = array(); $results = array(); $data = exec ("/sbin/pfctl -vv -sr | grep 'pfB_'", $results); if (!isset($config['installedpackages']['pfblockerngglobal']['pfbdenycnt'])) $config['installedpackages']['pfblockerngglobal']['pfbdenycnt'] = '25'; if (!isset($config['installedpackages']['pfblockerngglobal']['pfbpermitcnt'])) $config['installedpackages']['pfblockerngglobal']['pfbpermitcnt'] = '5'; if (!isset($config['installedpackages']['pfblockerngglobal']['pfbmatchcnt'])) $config['installedpackages']['pfblockerngglobal']['pfbmatchcnt'] = '5'; if (empty($config['installedpackages']['pfblockerngglobal']['alertrefresh'])) $config['installedpackages']['pfblockerngglobal']['alertrefresh'] = 'off'; if (empty($config['installedpackages']['pfblockerngglobal']['hostlookup'])) $config['installedpackages']['pfblockerngglobal']['hostlookup'] = 'off'; if (isset($_POST['save'])) { if (!is_array($config['installedpackages']['pfblockerngglobal'])) $config['installedpackages']['pfblockerngglobal'] = array(); $config['installedpackages']['pfblockerngglobal']['alertrefresh'] = $_POST['alertrefresh'] ? 'on' : 'off'; $config['installedpackages']['pfblockerngglobal']['hostlookup'] = $_POST['hostlookup'] ? 'on' : 'off'; if (is_numeric($_POST['pfbdenycnt'])) $config['installedpackages']['pfblockerngglobal']['pfbdenycnt'] = $_POST['pfbdenycnt']; if (is_numeric($_POST['pfbpermitcnt'])) $config['installedpackages']['pfblockerngglobal']['pfbpermitcnt'] = $_POST['pfbpermitcnt']; if (is_numeric($_POST['pfbmatchcnt'])) $config['installedpackages']['pfblockerngglobal']['pfbmatchcnt'] = $_POST['pfbmatchcnt']; write_config("pfBlockerNG pkg: updated ALERTS tab settings."); header("Location: " . $_SERVER['PHP_SELF']); exit; } if (is_array($config['installedpackages']['pfblockerngglobal'])) { $alertrefresh = $config['installedpackages']['pfblockerngglobal']['alertrefresh']; $hostlookup = $config['installedpackages']['pfblockerngglobal']['hostlookup']; $pfbdenycnt = $config['installedpackages']['pfblockerngglobal']['pfbdenycnt']; $pfbpermitcnt = $config['installedpackages']['pfblockerngglobal']['pfbpermitcnt']; $pfbmatchcnt = $config['installedpackages']['pfblockerngglobal']['pfbmatchcnt']; } function pfb_match_filter_field($flent, $fields) { foreach ($fields as $key => $field) { if ($field == null) continue; if ((strpos($field, '!') === 0)) { $field = substr($field, 1); $field_regex = str_replace('/', '\/', str_replace('\/', '/', $field)); if (@preg_match("/{$field_regex}/i", $flent[$key])) return false; } else { $field_regex = str_replace('/', '\/', str_replace('\/', '/', $field)); if (!@preg_match("/{$field_regex}/i", $flent[$key])) return false; } } return true; } if ($_POST['filterlogentries_submit']) { // Set flag for filtering alert entries $pfb['filterlogentries'] = TRUE; // Note the order of these fields must match the order decoded from the alerts log $filterfieldsarray = array(); $filterfieldsarray[0] = $_POST['filterlogentries_rule'] ? $_POST['filterlogentries_rule'] : null; $filterfieldsarray[2] = $_POST['filterlogentries_int'] ? $_POST['filterlogentries_int'] : null; $filterfieldsarray[6] = strtolower($_POST['filterlogentries_proto']) ? $_POST['filterlogentries_proto'] : null; // Remove any zero-length spaces added to the IP address that could creep in from a copy-paste operation $filterfieldsarray[7] = $_POST['filterlogentries_srcip'] ? str_replace("\xE2\x80\x8B", "", $_POST['filterlogentries_srcip']) : null; $filterfieldsarray[8] = $_POST['filterlogentries_dstip'] ? str_replace("\xE2\x80\x8B", "", $_POST['filterlogentries_dstip']) : null; $filterfieldsarray[9] = $_POST['filterlogentries_srcport'] ? $_POST['filterlogentries_srcport'] : null; $filterfieldsarray[10] = $_POST['filterlogentries_dstport'] ? $_POST['filterlogentries_dstport'] : null; $filterfieldsarray[99] = $_POST['filterlogentries_date'] ? $_POST['filterlogentries_date'] : null; } if ($_POST['filterlogentries_clear']) { $pfb['filterlogentries'] = TRUE; $filterfieldsarray = array(); } // Collect pfBlockerNG Firewall Rules if (!empty($results)) { foreach ($results as $result) { # Find Rule Descriptions $descr = ""; if (preg_match("/USER_RULE: (\w+)/",$result,$desc)) $descr = $desc[1]; if ($pfb['pfsenseversion'] >= '2.2') { preg_match ("/@(\d+)\(/",$result, $rule); } else { preg_match ("/@(\d+)\s/",$result, $rule); } $id = $rule[1]; # Create array of Rule Description and pfctl Rule Number $rule_list['id'][] = $id; $rule_list[$id]['name'] = $descr; } } // Add IP to the Suppression Alias if (isset($_POST['addsuppress'])) { $ip = ""; if (isset($_POST['ip'])) { $ip = $_POST['ip']; $table = $_POST['table']; $descr = $_POST['descr']; $cidr = $_POST['cidr']; // If Description or CIDR field is empty, exit. if (empty($descr) || empty($cidr)) { header("Location: " . $_SERVER['PHP_SELF']); exit; } if (is_ipaddr($ip)) { $savemsg1 = "Host IP address {$ip}"; if (is_ipaddrv4($ip)) { $iptrim1 = preg_replace("/(\d{1,3})\.(\d{1,3}).(\d{1,3}).(\d{1,3})/", '$1.$2.$3.0/24', $ip); $iptrim2 = preg_replace("/(\d{1,3})\.(\d{1,3}).(\d{1,3}).(\d{1,3})/", '$1.$2.$3.', $ip); $iptrim3 = preg_replace("/(\d{1,3})\.(\d{1,3}).(\d{1,3}).(\d{1,3})/", '$4', $ip); if ($cidr == "32") { $pfb_pfctl = exec ("/sbin/pfctl -t {$table} -T show | grep {$iptrim1} 2>&1"); if ($pfb_pfctl == "") { $savemsg2 = " : Removed /32 entry"; exec ("/sbin/pfctl -t {$table} -T delete {$ip}"); } else { $savemsg2 = " : Removed /24 entry, added 254 addr"; exec ("/sbin/pfctl -t {$table} -T delete {$iptrim1}"); for ($add_ip=0; $add_ip <= 255; $add_ip++){ if ($add_ip != $iptrim3) { exec ("/sbin/pfctl -t {$table} -T add {$iptrim2}{$add_ip}"); } } } } else { $cidr = 24; $savemsg2 = " : Removed /24 entry"; exec ("/sbin/pfctl -t {$table} -T delete {$iptrim1} 2>&1", $pfb_pfctl); if (!preg_grep("/1\/1 addresses deleted/", $pfb_pfctl)) { $savemsg2 = " : Removed all entries"; // Remove 0-255 IP Address from Alias Table for ($del_ip=0; $del_ip <= 255; $del_ip++){ exec ("/sbin/pfctl -t {$table} -T delete {$iptrim2}{$del_ip}"); } } } } // Collect pfBlockerNGSuppress Alias Contents $pfb_sup_list = array(); $pfb_sup_array = array(); $pfb['found'] = FALSE; $pfb['update'] = FALSE; if (is_array($config['aliases']['alias'])) { foreach ($config['aliases']['alias'] as $alias) { if ($alias['name'] == "pfBlockerNGSuppress") { $data = $alias['address']; $data2 = $alias['detail']; $arr1 = explode(" ",$data); $arr2 = explode("||",$data2); if (!empty($data)) { $row = 0; foreach ($arr1 as $host) { $pfb_sup_list[] = $host; $pfb_sup_array[$row]['host'] = $host; $row++; } $row = 0; foreach ($arr2 as $detail) { $pfb_sup_array[$row]['detail'] = $detail; $row++; } } $pfb['found'] = TRUE; } } } // Call Function to Create Suppression Alias if not found. if (!$pfb['found']) pfb_create_suppression_alias(); // Save New Suppress IP to pfBlockerNGSuppress Alias if (in_array($ip . '/' . $cidr, $pfb_sup_list)) { $savemsg = gettext("Host IP address {$ip} already exists in the pfBlockerNG Suppress Table."); } else { if (!$pfb['found'] && empty($pfb_sup_list)) { $next_id = 0; } else { $next_id = count($pfb_sup_list); } $pfb_sup_array[$next_id]['host'] = $ip . '/' . $cidr; $pfb_sup_array[$next_id]['detail'] = $descr; $address = ""; $detail = ""; foreach ($pfb_sup_array as $pfb_sup) { $address .= $pfb_sup['host'] . " "; $detail .= $pfb_sup['detail'] . "||"; } // Find pfBlockerNGSuppress Array ID Number if (is_array($config['aliases']['alias'])) { $pfb_id = 0; foreach ($config['aliases']['alias'] as $alias) { if ($alias['name'] == "pfBlockerNGSuppress") { break; } $pfb_id++; } } $config['aliases']['alias'][$pfb_id]['address'] = rtrim($address, " "); $config['aliases']['alias'][$pfb_id]['detail'] = rtrim($detail, "||"); $savemsg = gettext($savemsg1) . gettext($savemsg2) . gettext(" and added Host to the pfBlockerNG Suppress Table."); $pfb['update'] = TRUE; } if ($pfb['found'] || $pfb['update']) { // Save all Changes to pfsense config file write_config(); } } } } // Host Resolve Function lookup function getpfbhostname($type = 'src', $hostip, $countme = 0) { $hostnames['src'] = ''; $hostnames['dst'] = ''; $hostnames[$type] = '
'; return $hostnames; } // Determine if Alert Host 'Dest' is within the Local Lan IP Range. function check_lan_dest($lan_ip,$lan_mask,$dest_ip,$dest_mask="32") { $result = check_subnets_overlap($lan_ip, $lan_mask, $dest_ip, $dest_mask); return $result; } // Parse Filter log for pfBlockerNG Alerts function conv_log_filter_lite($logfile, $nentries, $tail, $pfbdenycnt, $pfbpermitcnt, $pfbmatchcnt) { global $pfb, $rule_list, $filterfieldsarray; $fields_array = array(); $logarr = ""; $denycnt = 0; $permitcnt = 0; $matchcnt = 0; if (file_exists($logfile)) { exec("/usr/local/sbin/clog " . escapeshellarg($logfile) . " | grep -v \"CLOG\" | grep -v \"\033\" | /usr/bin/grep 'filterlog:' | /usr/bin/tail -r -n {$tail}", $logarr); } else return; if (!empty($logarr) && !empty($rule_list['id'])) { foreach ($logarr as $logent) { $pfbalert = array(); $log_split = ""; if (!preg_match("/(.*)\s(.*)\sfilterlog:\s(.*)$/", $logent, $log_split)) continue; list($all, $pfbalert[99], $host, $rule) = $log_split; $rule_data = explode(",", $rule); $pfbalert[0] = $rule_data[0]; // Rulenum // Skip Alert if Rule is not a pfBNG Alert if (!in_array($pfbalert[0], $rule_list['id'])) continue; $pfbalert[1] = $rule_data[4]; // Realint $pfbalert[3] = $rule_data[6]; // Act $pfbalert[4] = $rule_data[8]; // Version if ($pfbalert[4] == "4") { $pfbalert[5] = $rule_data[15]; // Protocol ID $pfbalert[6] = $rule_data[16]; // Protocol $pfbalert[7] = $rule_data[18]; // SRC IP $pfbalert[8] = $rule_data[19]; // DST IP $pfbalert[9] = $rule_data[20]; // SRC Port $pfbalert[10] = $rule_data[21]; // DST Port $pfbalert[11] = $rule_data[23]; // TCP Flags } else { $pfbalert[5] = $rule_data[13]; // Protocol ID $pfbalert[6] = $rule_data[12]; // Protocol $pfbalert[7] = $rule_data[15]; // SRC IP $pfbalert[8] = $rule_data[16]; // DST IP $pfbalert[9] = $rule_data[17]; // SRC Port $pfbalert[10] = $rule_data[18]; // DST Port $pfbalert[11] = $rule_data[20]; // TCP Flags } if ($pfbalert[5] == "6" || $pfbalert[5] == "17") { // skip } else { $pfbalert[9] = ""; $pfbalert[10] = ""; $pfbalert[11] = ""; } // Skip Repeated Alerts if (($pfbalert[3] . $pfbalert[8] . $pfbalert[10]) == $previous_dstip || ($pfbalert[3] . $pfbalert[7] . $pfbalert[9]) == $previous_srcip) continue; $pfbalert[2] = convert_real_interface_to_friendly_descr($rule_data[4]); // Friendly Interface Name $pfbalert[6] = str_replace("TCP", "TCP-", strtoupper($pfbalert[6]), $pfbalert[6]) . $pfbalert[11]; // Protocol Flags // If Alerts Filtering is selected, process Filters as required. if ($pfb['filterlogentries'] && !pfb_match_filter_field($pfbalert, $filterfieldsarray)) { continue; } if ($pfbalert[3] == "block") { if ($denycnt < $pfbdenycnt) { $fields_array['Deny'][] = $pfbalert; $denycnt++; } } elseif ($pfbalert[3] == "pass") { if ($permitcnt < $pfbpermitcnt) { $fields_array['Permit'][] = $pfbalert; $permitcnt++; } } elseif ($pfbalert[3] == "unkn(%u)" || $pfbalert[3] == "unkn(11)") { if ($matchcnt < $pfbmatchcnt) { $fields_array['Match'][] = $pfbalert; $matchcnt++; } } // Exit function if Sufficinet Matches found. if ($denycnt >= $pfbdenycnt && $permitcnt >= $pfbpermitcnt && $matchcnt >= $pfbmatchcnt) { unset ($pfbalert, $logarr); return $fields_array; } // Collect Details for Repeated Alert Comparison $previous_srcip = $pfbalert[3] . $pfbalert[7] . $pfbalert[9]; $previous_dstip = $pfbalert[3] . $pfbalert[8] . $pfbalert[10]; } unset ($pfbalert, $logarr); return $fields_array; } } $pgtitle = gettext("pfBlockerNG: Alerts"); include_once("head.inc"); ?>