Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Contador de Regras para Pfsense (Hit Count)

    Scheduled Pinned Locked Moved Portuguese
    73 Posts 17 Posters 24.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • marcellocM
      marcelloc
      last edited by

      Passo a passo para pfSense 2.2.4  (atualizado 24/11/2015) v0.4.1

      Passo 1:
      Instale os pacotes filer e System Patches

      Passo 2:
      Crie no pacote filer, o arquivo /etc/inc/rule_count.inc com permissão 0644 e o seguinte conteúdo:

      $rules_count_array=array();
      exec("/sbin/pfctl -vvsr | /usr/bin/grep -A1 ' label '",$cnt_pfctl);
      if (!empty($cnt_pfctl)) {
      	foreach($cnt_pfctl as $line) {
      		if (preg_match('/@(\d+)\W\d+.*label "(USER_RULE: |)(.*)"/',$line,$m1)){
      			$cr=$m1[3];
      			$rl=$m1[2].$m1[3];
      			$rid=$m1[1];
      			continue;
      		}
      		if ($cr != "" && preg_match("/Evaluations:\s+(\d+)\s+Packets:\s+(\d+)\s+Bytes:\s+(\d+)\s+States:\s+(\d+)\s+/",$line,$m2)){
      			if (isset($rules_count_array[$cr][Packets])){
      				$rules_count_array[$cr]['Packets'] += $m2[2];
      				$rules_count_array[$cr]['Evaluations'] += $m2[1];
      				$rules_count_array[$cr]['Bytes'] += $m2[3];
      				$rules_count_array[$cr]['States'] += $m2[4];
      				$rules_count_array[$cr]['RuleId'] .= "|$rid";
      			}else {
      				$rules_count_array[$cr]=array('Evaluations'=>$m2[1],'Packets'=>$m2[2],'Bytes'=>$m2[3],'States'=>$m2[4],'Label'=>$rl,'RuleId'=>$rid);
      			}
      		}
      	}
      }
      ?>
      
      

      Passo 3:
      Crie, salve, teste e aplique os seguintes patches em system->patches.

      Description: filter_inc_patch
      Patch Contents:

      
      --- filter.orig.inc	2015-08-11 13:17:11.000000000 +0000
      +++ filter.inc	2015-08-17 15:05:54.000000000 +0000
      @@ -2168,10 +2168,13 @@
       	$line = filter_generate_user_rule($rule);
       	$ret['rule'] = $line;
       	$ret['interface'] = $rule['interface'];
      -	if($rule['descr'] != "" and $line != "")
      +       if ($rule['tracker'] != "" and $line != "") {
      +               $ret['descr'] = "label \"" . fix_rule_label("USER_RULE: {$rule['tracker']}") . "\"";
      +       } elseif ($rule['descr'] != "" and $line != "") {
       		$ret['descr'] = "label \"" . fix_rule_label("USER_RULE: {$rule['descr']}") . "\"";
      -	else
      +       } else {
       		$ret['descr'] = "label \"USER_RULE\"";
      +       }
      
       	return $ret;
       }
      
      

      Base Directory:/etc/inc/

      Description:firewall_rules_patch
      Patch Contents:

      --- firewall_rules.orig.php	2015-08-11 13:27:15.000000000 +0000
      +++ firewall_rules.php	2015-08-21 17:13:30.000000000 +0000
      @@ -46,10 +46,72 @@
       require_once("functions.inc");
       require_once("filter.inc");
       require_once("shaper.inc");
      +require_once("rule_count.inc");
      
       $pgtitle = array(gettext("Firewall"),gettext("Rules"));
       $shortcut_section = "firewall";
      
      +//Get rule Hit count
      +function get_rule_ht($tracker,$sum_ht=array()){
      +	global $g,$rules_count_array;
      +	//check if there is previous values
      +	$packets=(isset($sum_ht['packets']) ? $sum_ht['packets'] : 0);
      +	$states=(isset($sum_ht['bytes']) ? $sum_ht['bytes'] : 0);
      +	$rules_title=(isset($sum_ht['title']) ? $sum_ht['title'] : "");
      +	$rules_id=array();
      +	if (preg_match("/\s+/",$tracker)){
      +		$rules_title.="$tracker\n";
      +	}
      +	if (is_array($rules_count_array) && array_key_exists($tracker,$rules_count_array)) {
      +		$packets +=$rules_count_array[$tracker]['Packets'];
      +		$states +=$rules_count_array[$tracker]['States'];
      +		foreach ($rules_count_array[$tracker] as $rck => $rcv) {
      +			switch($rck){
      +				case "Label":
      +					$label=$rcv;
      +					break;
      +				case "RuleId":
      +					$ruleid=$rcv;
      +					break;
      +				default:
      +				$rules_title.= "$rck: ".bd_nice_number($rcv)."\n";
      +			}
      +		}
      +	}
      +	$hitcount=bd_nice_number($packets)."/".bd_nice_number($states);
      +	if ($states > 0){
      +		//icon_log.gif
      +		///themes/pfsense_ng/images/icons/icon_log_d.gif
      +		//$html=
      +		$html="{$hitcount}
      ";
      +		$html.="![](\"./themes/{$g['theme']}/images/icons/icon_block.gif\")";
      +		//$resp_info="Kill Rule States {$rulelabel}" . $resp;
      +	} else {
      +		$html="{$hitcount}";
      +	}
      +	return (array(	'title'=> $rules_title, 
      +					'packets' =>$packets,
      +					'states' =>$states,
      +					'html' =>$html
      +	));
      +}
      +
      +function bd_nice_number($n) {
      +	// first strip any formatting;
      +	$n = (0+str_replace(",","",$n));
      +
      +	// is this a number?
      +	if(!is_numeric($n)) return false;
      +
      +	// now filter it;
      +	if($n>1000000000000) return round(($n/1000000000000),1).'t';
      +	else if($n>1000000000) return round(($n/1000000000),1).'g';
      +	else if($n>1000000) return round(($n/1000000),1).'m';
      +	else if($n>1000) return round(($n/1000),1).'k';
      +
      +	return number_format($n);
      +}
      +
       function delete_nat_association($id) {
       	global $config;
      
      @@ -135,6 +197,70 @@
      
       		$savemsg = sprintf(gettext("The settings have been applied. The firewall rules are now reloading in the background.
      You can also %s monitor %s the reload progress"),"[","](status_filter_reload.php)");
       	}
      +	/* handle AJAX operations */
      +	if($_POST['action'] == "KillRuleStates") {
      +		if (isset($_POST['label'])){
      +			$rulelabel=html_entity_decode($_POST['label']);
      +			$cnt_pfctlk=array();
      +			exec("/sbin/pfctl -k label -k \"{$rulelabel}\" 2>&1",$cnt_pfctlk);
      +		} else {
      +			echo gettext("invalid input");
      +		}
      +		if (!empty($cnt_pfctlk)) {
      +			foreach($cnt_pfctlk as $line) {
      +				print "$line\n";
      +			}
      +		}
      +		return;
      +	}
      +	if($_POST['action'] == "ShowRuleStates") {
      +		$td1="";
      +		$td2="";
      +		//$td2="";
      +   		$resp  = "";
      +        $resp .= "";
      +        $resp .= $td1 . gettext("Proto") . "";
      +        $resp .= $td1 . gettext("Source -> Router -> Destination") . "";
      +        $resp .= $td1 . gettext("State") . "";
      +        $resp .= $td1 . gettext("Packets") . "";
      +        $resp .= $td1 . gettext("bytes") . "";
      +		$resp .= "";
      +		$state_count=0;
      +		/*if (isset($_POST['label'])){
      +			$rulelabel=html_entity_decode($_POST['label']);
      +		}
      +		if (isset($_POST['packets'])){
      +			$packets=html_entity_decode($_POST['packets']);
      +		}
      +		*/
      +		if (isset($_POST['hitcount'])){
      +			$hitcount=html_entity_decode($_POST['hitcount']);
      +		}
      +		if (isset($_POST['ruleid']) && preg_match("/\d+/",$_POST['ruleid'])){
      +			$ruleid=html_entity_decode($_POST['ruleid']);
      +			$cnt_pfctls=array();
      +			exec("/sbin/pfctl -vvss | /usr/bin/grep -EB2 \"rule ({$ruleid})\"",$cnt_pfctls);
      +		} else {
      +			echo gettext("invalid input");
      +		}
      +		if (!empty($cnt_pfctls)) {
      +			foreach($cnt_pfctls as $line) {
      +				if (preg_match("/^\w+\s+(\w+)\s+(.*)(.-.)(.*)\s+(\w+:\w+)/",$line,$mcon)) {
      +					$state_count++;
      +					$resp .= "{$td2}{$mcon[1]}{$td2}{$mcon[2]}{$mcon[3]}{$mcon[4]}{$td2}{$mcon[5]}";
      +				}
      +				elseif (preg_match("/age.*, (\S+) pkts, (\S+) bytes, rule (\d+)/",$line,$mrule)) {
      +					list($pkt1,$pkt2)=split(":",$mrule[1],2);
      +					list($bt1,$bt2)=split(":",$mrule[2],2);
      +					$resp .= "{$td2}".bd_nice_number($pkt1)." / ".bd_nice_number($pkt2)."{$td2}".bd_nice_number($bt1)." / ".bd_nice_number($bt2)."";
      +				}
      +			}
      +		}
      +		$resp .= "
      
      ";
      +		$html.="<u>{$hitcount}</u>";
      +		print ($html);
      +		return;
      +	}
       }
      
       if ($_GET['act'] == "del") {
      @@ -233,7 +359,66 @@
      
      <form action="firewall_rules.php" method="post">
      +
      
      @@ -272,7 +457,7 @@
      
      -			
      +                       ">
       			 				pfSense_handle_custom_code("/usr/local/pkg/firewall_rules/pre_id_tablehead");
       			?>
      @@ -319,11 +504,12 @@
       					|| ((count($config['interfaces']) == 1) && ($if == 'wan')))):
      
       					$alports = implode('
      ', filter_get_antilockout_ports(true));
      +				$rule_hit_count=get_rule_ht("anti-lockout rule");
       			?>
      
       			![pass](./themes/<?= $g['theme']; ?>/images/icons/icon_pass.gif)
      -			 
      +			
       			 				pfSense_handle_custom_code("/usr/local/pkg/firewall_rules/pre_id_tr_antilockout");
       			?>
      @@ -351,11 +537,15 @@
      
      -
      ++			$rule_hit_count=get_rule_ht("Block private networks from " . strtoupper($if) . " block 192.168/16");
      +			$rule_hit_count=get_rule_ht("Block private networks from " . strtoupper($if) . " block 127/8",$rule_hit_count);
      +			$rule_hit_count=get_rule_ht("Block private networks from " . strtoupper($if) . " block 172.16/12",$rule_hit_count);
      +			$rule_hit_count=get_rule_ht("Block private networks from " . strtoupper($if) . " block 10/8",$rule_hit_count);?>
      
       			![block](./themes/<?= $g['theme']; ?>/images/icons/icon_block.gif)
      -			 
      +			
       			*
      
       			*
      @@ -379,11 +569,14 @@
      
      -
      ++			$rule_hit_count=get_rule_ht("block bogon IPv4 networks from ".strtoupper($if));
      +			$rule_hit_count=get_rule_ht("block bogon IPv6 networks from ".strtoupper($if),$rule_hit_count);
      +			?>
      
       			![block](./themes/<?= $g['theme']; ?>/images/icons/icon_block.gif)
      -			 
      +			
       			*
      
       			*
      @@ -606,9 +799,10 @@
       						$printicon = true;
       					}
       				}
      +				 $rule_hit_count=get_rule_ht($filterent['tracker']);
       			?>
      -			
      -				
      +                       
      +                               
      
       			 				pfSense_handle_custom_code("/usr/local/pkg/firewall_rules/pre_id_tr");
      @@ -816,6 +1010,7 @@
      
      </form>
      
      + 
      
      

      Base Directory:/usr/local/www/

      Passo 4:
      Edit e salve qualquer regra para atualizar o pfctl e começar a ver os hit counts na interface web.

      Treinamentos de Elite: http://sys-squad.com

      Help a community developer! ;D

      1 Reply Last reply Reply Quote 0
      • JackLJ
        JackL
        last edited by

        Este é o Marcello Coutinho…

        Seguramente um dos caras que mais contribui no mundo todo com patches e pacotes para o pfSense!

        Parabéns novamente pelo trabalho marcelloc!

        Abraços!
        Jack

        Treinamentos de Elite: http://sys-squad.com
        Soluções: https://conexti.com.br

        1 Reply Last reply Reply Quote 0
        • T
          tomaswaldow
          last edited by

          Fantástico!

          Tomas @ 2W Consultoria

          1 Reply Last reply Reply Quote 0
          • V
            victorfmaraujo
            last edited by

            Sempre senti falta desta feature.

            Marcelo está de parabéns!

            [Off]
            Uma coisa que eu mesmo estou pesquisando sobre como fazer é um categorizador, onde o mesmo faria um busca por expressões regulares nas listas do Squidguard.

            1 Reply Last reply Reply Quote 0
            • V
              victorfmaraujo
              last edited by

              Este patch poderia já ser incorporado ao código em uma próxima versão heim Marcelo!?  :D

              1 Reply Last reply Reply Quote 0
              • marcellocM
                marcelloc
                last edited by

                @victorfmaraujo:

                Este patch poderia já ser incorporado ao código em uma próxima versão heim Marcelo!?  :D

                Essa parte não depende de mim. É possível que eu faça o pull request e ele caia no esquecimento…

                Treinamentos de Elite: http://sys-squad.com

                Help a community developer! ;D

                1 Reply Last reply Reply Quote 0
                • G
                  guitarcleiton
                  last edited by

                  Parabéns Marcelo.
                  Testado e aprovado em meu ambiente

                  Analista de Sistemas
                  Bacharel em Sistemas de Informação

                  https://cleiton.tech.blog/

                  1 Reply Last reply Reply Quote 0
                  • W
                    wesleycorrea
                    last edited by

                    Parabéns

                    Vou testar!

                    1 Reply Last reply Reply Quote 0
                    • V
                      victorfmaraujo
                      last edited by

                      @marcelloc:

                      @victorfmaraujo:

                      Este patch poderia já ser incorporado ao código em uma próxima versão heim Marcelo!?  :D

                      Essa parte não depende de mim. É possível que eu faça o pull request e ele caia no esquecimento…

                      Trágico.

                      Marcelo, largue essa vida e vá trabalhar no Core Team rsrs.

                      Forte abraço

                      1 Reply Last reply Reply Quote 0
                      • marcellocM
                        marcelloc
                        last edited by

                        @guitarcleiton:

                        Parabéns Marcelo.
                        Testado e aprovado em meu ambiente

                        Já coloquei em uns 5  :) e já não sei como era pra debugar quantidade de conexões antes disso. :D

                        Treinamentos de Elite: http://sys-squad.com

                        Help a community developer! ;D

                        1 Reply Last reply Reply Quote 0
                        • T
                          tomaswaldow
                          last edited by

                          Marcello, pelo que vi só vai fazer a contagem das regras que o log está ativo?

                          Tomas @ 2W Consultoria

                          1 Reply Last reply Reply Quote 0
                          • marcellocM
                            marcelloc
                            last edited by

                            @Tomas:

                            Marcello, pelo que vi só vai fazer a contagem das regras que o log está ativo?

                            Não, todas as regras criadas pelo sysadmin.
                            Depois de rodar o php que cria os hashes,```
                            cp /conf/config.xml /root/config.bkp.xml && php /root/create_rule_hashes.php

                            Regras criadas pelo nat precisam de um edit/save pra criar o hash.

                            Treinamentos de Elite: http://sys-squad.com

                            Help a community developer! ;D

                            1 Reply Last reply Reply Quote 0
                            • A
                              atsuma
                              last edited by

                              E mais uma vez senhor Marcelo mandando ver!. :D
                              Fico me perguntando….se esse cara morrer, o que vai ser desse fórum?!

                              PS.
                              Já revisou seu seguro de vida Marcelo?!...kkkkkkkkkkkk.

                              1 Reply Last reply Reply Quote 0
                              • J
                                jonataaraujo
                                last edited by

                                Show de bola! Muito bom esse recurso.

                                Parabéns pela contribuição.

                                Abraço!

                                1 Reply Last reply Reply Quote 0
                                • T
                                  tomaswaldow
                                  last edited by

                                  Marcello sei que já falou sobre isso, mas envia novamente essa contribuição, quem sabe dessa vez eles aceitam!

                                  Tomas @ 2W Consultoria

                                  1 Reply Last reply Reply Quote 0
                                  • marcellocM
                                    marcelloc
                                    last edited by

                                    @Tomas:

                                    Marcello sei que já falou sobre isso, mas envia novamente essa contribuição, quem sabe dessa vez eles aceitam!

                                    vou tentar sim, o Jimp não rejeitou a idéia, o que já é metade do caminho.

                                    Estou reescrevendo ele todo para 2.2, ele testando, vejo se o processo burocratico começa novamente ou se ele mesmo pode fazer o merge na 2.3

                                    https://forum.pfsense.org/index.php?topic=97925.0

                                    Treinamentos de Elite: http://sys-squad.com

                                    Help a community developer! ;D

                                    1 Reply Last reply Reply Quote 0
                                    • T
                                      tomaswaldow
                                      last edited by

                                      Legal, vai ficar bom.

                                      Tomas @ 2W Consultoria

                                      1 Reply Last reply Reply Quote 0
                                      • marcellocM
                                        marcelloc
                                        last edited by

                                        Publiquei no fórum internacional uma nova vesão que mostra o hit de todas as regras, até as "automaticas" para o pfsense 2.2.4.

                                        https://forum.pfsense.org/index.php?topic=97925.0

                                        Treinamentos de Elite: http://sys-squad.com

                                        Help a community developer! ;D

                                        1 Reply Last reply Reply Quote 0
                                        • T
                                          tomaswaldow
                                          last edited by

                                          Qual o procedimento para atualizar os que já está rodando? Obrigado.

                                          Tomas @ 2W Consultoria

                                          1 Reply Last reply Reply Quote 0
                                          • marcellocM
                                            marcelloc
                                            last edited by

                                            @Tomas:

                                            Qual o procedimento para atualizar os que já está rodando? Obrigado.

                                            Basicamente, só precisa reverter o patch anterior e aplicar.

                                            Depois que estiver original novamente, exclui o patch antigo e aplica o novo.

                                            Treinamentos de Elite: http://sys-squad.com

                                            Help a community developer! ;D

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.