|
From: | Marcelo Primo |
Subject: | [shell-script-pt] Parser em log pflogfile |
Date: | Wed, 10 Mar 2021 19:22:40 -0300 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 |
Boa noite a todos,
Há um script (de autoria de outra pessoa) que efetua um parser em
um arquivo de logs do PF que possui em média 800k linhas e ele faz
um ranking nas portas atingidas no firewall (endereços em
negrito).
As linhas são conforme as que seguem:
Mar 09 00:00:03.146758 rule 12/(match) pass in on em0:
198.199.88.65.61953 > 192.168.1.143.9004: S
3776023135:3776023135(0) win 1024
Mar 09 00:00:03.151840 rule 12/(match) pass in on em0:
198.199.88.65.61953 > 192.168.1.176.9004: S
4103556202:4103556202(0) win 1024
Mar 09 00:00:03.168233 rule 12/(match) pass in on em0:
203.212.200.241.58481 > 192.168.1.210.23: S
1794254071:1794254071(0) win 5808 <mss 1452,sackOK,timestamp
63610891 0,nop,wscale 2> (DF)
Mar 09 00:00:03.190210 rule 12/(match) pass in on em0:
178.175.97.53.46828 > 192.168.1.210.23: S
700145820:700145820(0) win 5808 <mss 1452,sackOK,timestamp
159224881 0,nop,wscale 1> (DF) [tos 0x4]
Mar 09 00:00:03.213339 rule 12/(match) pass in on em0:
131.159.24.205.51075 > 192.168.1.212.80: S
2924909527:2924909527(0) win 65535 <sackOK,timestamp 4294967295
16843009,wscale
1,nop,opt-34:,opt-64:,opt-30:00810c0c0c0c0c0c0c0c,eol>
Mar 09 00:00:03.263630 rule 12/(match) pass in on em0:
138.197.180.77.61953 > 192.168.1.194.10000: S
3809871894:3809871894(0) win 1024
Mar 09 00:00:03.394312 rule 12/(match) pass in on em0:
178.175.97.53.53172 > 192.168.1.210.23: S
2983985618:2983985618(0) win 33941 [tos 0x4]
Mar 09 00:00:03.500488 rule 12/(match) pass in on em0:
171.67.71.100.35957 > 192.168.1.249.8848: S
1091778644:1091778644(0) win 65535
Mar 09 00:00:03.637855 rule 12/(match) pass in on em0:
203.212.200.241.58457 > 192.168.1.210.23: S
1746544785:1746544785(0) win 5808 <mss 1452,sackOK,timestamp
63610938 0,nop,wscale 2> (DF)
Para fazer isso (o ranking), ele tem uma linha muito confusa, mas
que faz o trabalho, segue abaixo:
zcat pflogtext.0.gz | grep " pass in" | grep -v "icmp" | awk '{print $12}' | cut -d "." -f5 | sort | uniq -c | sort -rn | head -n20 | awk '{print $1,$2}' | sed 's/ /;/g' | sed 's/://g'
(Essa linha vai no IP de destino, extrai a porta, conta e
classifica de maneira inversa para encontrar as "top ports").
Apesar de, em um primeiro momento, essa linha parecer ineficiente, ela consome em média 2,9s para fazer o parser e retornar a saída abaixo:
184202;23
21960;22
18063;3389
17491;1433
17065;80
12519;5060
10853;8291
7263;7547
6144;8080
5498;443
3867;8728
3827;6379
3525;81
3284;123
2338;11211
2035;5555
1947;26
1912;8081
1892;82
1736;8182
Estou tentando deixar ela um pouco mais eficiente (culpa do Júlio... kkkk, pois no curso ministrado por ele, ele sempre diz: "Em shell há várias formas de fazer algo, basta saber se ela é a mais eficiente"), com isso em mente, comecei as mudanças, na tentativa de deixar a coisa mais clara e, se não melhor, ao menos no mesmo tempo. Minhas tentativas foram:
zgrep "pass in" pflogtext.0.gz | sed -E 's/.*\.([0-9]+):.*/\1/' | sort | uniq -c | sort -rn | head -n20 | tr -s ' ' = 13s
zgrep "pass in" pflogtext.0.gz | grep -Eo '(\.[0-9]+\:)' | sort | uniq -c | sort -rn | head -n20 | tr '[.:]' ' ' | tr -s ' ' = 12s
zgrep "pass in" pflogtext.0.gz | cut -d " " -f12 | cut -d "." -f5
| sort | uniq -c | sort -rn | head -n20 | tr '[.:]' ' ' | tr -s '
' = 5,3s
zgrep "pass in" pflogtext.0.gz | awk '{print $12}' | cut -d "." -f5 | sort | uniq -c | sort -rn | head -n20 | tr '[.:]' ' ' | tr -s ' ' = 2,6s
Apesar da última linha estar "melhor" do que as anteriores, fiquei pensando se esse awk não está meio perdido (ou se teria uma forma melhor de usá-lo, essa combinação awk + cut não está agradando :D ).
Não sei se fui claro, mas aos que puderem e quiserem, estou aberto a sugestões.
Grato desde já.
Atenciosamente,
Marcelo
[Prev in Thread] | Current Thread | [Next in Thread] |