]>
Commit | Line | Data |
---|---|---|
ac1cfefa | 1 | #!/usr/bin/perl |
70df8302 MT |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
75bc929e | 5 | # Copyright (C) 2007-2012 IPFire Team <info@ipfire.org> # |
70df8302 MT |
6 | # # |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
ac1cfefa | 21 | |
75bc929e | 22 | use strict; |
ac1cfefa MT |
23 | |
24 | use Net::IPv4Addr qw( :all ); | |
75bc929e | 25 | use Switch; |
ac1cfefa MT |
26 | |
27 | # enable only the following on debugging purpose | |
1465b127 | 28 | #use warnings; |
cb5e9c6c | 29 | #use CGI::Carp 'fatalsToBrowser'; |
ac1cfefa | 30 | |
986e08d9 | 31 | require '/var/ipfire/general-functions.pl'; |
ac1cfefa MT |
32 | require "${General::swroot}/lang.pl"; |
33 | require "${General::swroot}/header.pl"; | |
ea4620fc | 34 | require "${General::swroot}/geoip-functions.pl"; |
ac1cfefa | 35 | |
c8a8778f MT |
36 | my $colour_multicast = "#A0A0A0"; |
37 | ||
9b37e91e KMK |
38 | # sort arguments for connection tracking table |
39 | # the sort field. eg. 1=src IP, 2=dst IP, 3=src port, 4=dst port | |
40 | my $SORT_FIELD = 0; | |
41 | # the sort order. (a)scending orr (d)escending | |
42 | my $SORT_ORDER = 0; | |
43 | # cgi query arguments | |
44 | my %cgiin; | |
45 | # debug mode | |
46 | my $debug = 0; | |
47 | ||
48 | # retrieve query arguments | |
49 | # note: let a-z A-Z and 0-9 pass as value only | |
50 | if (length ($ENV{'QUERY_STRING'}) > 0){ | |
51 | my $name; | |
52 | my $value; | |
53 | my $buffer = $ENV{'QUERY_STRING'}; | |
54 | my @pairs = split(/&/, $buffer); | |
55 | foreach my $pair (@pairs){ | |
56 | ($name, $value) = split(/=/, $pair); | |
57 | $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # e.g. "%20" => " " | |
58 | $value =~ s/[^a-zA-Z0-9]*//g; # a-Z 0-9 will pass | |
59 | $cgiin{$name} = $value; | |
60 | } | |
61 | } | |
62 | ||
75bc929e MT |
63 | &Header::showhttpheaders(); |
64 | ||
65 | my @network=(); | |
66 | my @masklen=(); | |
67 | my @colour=(); | |
68 | ||
69 | my %netsettings=(); | |
70 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
71 | ||
9b37e91e KMK |
72 | # output cgi query arrguments to browser on debug |
73 | if ( $debug ){ | |
74 | &Header::openbox('100%', 'center', 'DEBUG'); | |
75 | my $debugCount = 0; | |
76 | foreach my $line (sort keys %cgiin) { | |
77 | print "$line = '$cgiin{$line}'<br />\n"; | |
78 | $debugCount++; | |
79 | } | |
80 | print " Count: $debugCount\n"; | |
81 | &Header::closebox(); | |
82 | } | |
83 | ||
ac1cfefa MT |
84 | #workaround to suppress a warning when a variable is used only once |
85 | my @dummy = ( ${Header::table1colour} ); | |
86 | undef (@dummy); | |
87 | ||
9b37e91e KMK |
88 | # check sorting arguments |
89 | if ( $cgiin{'sort_field'} ~~ [ '1','2','3','4','5','6','7','8','9' ] ) { | |
90 | $SORT_FIELD = $cgiin{'sort_field'}; | |
91 | ||
92 | if ( $cgiin{'sort_order'} ~~ [ 'a','d','A','D' ] ) { | |
93 | $SORT_ORDER = lc($cgiin{'sort_order'}); | |
94 | } | |
95 | } | |
96 | ||
97 | # Read and sort the connection tracking table | |
98 | # do sorting | |
99 | if ($SORT_FIELD and $SORT_ORDER) { | |
100 | # field sorting when sorting arguments are sane | |
101 | open(CONNTRACK, "/usr/local/bin/getconntracktable | /usr/local/bin/consort.sh $SORT_FIELD $SORT_ORDER |") or die "Unable to read conntrack table"; | |
102 | } else { | |
103 | # default sorting with no query arguments | |
104 | open(CONNTRACK, "/usr/local/bin/getconntracktable | sort -k 5,5 --numeric-sort --reverse |") or die "Unable to read conntrack table"; | |
105 | } | |
75bc929e MT |
106 | my @conntrack = <CONNTRACK>; |
107 | close(CONNTRACK); | |
ac1cfefa | 108 | |
75bc929e | 109 | # Collect data for the @network array. |
ac1cfefa | 110 | |
75bc929e MT |
111 | # Add Firewall Localhost 127.0.0.1 |
112 | push(@network, '127.0.0.1'); | |
113 | push(@masklen, '255.255.255.255'); | |
114 | push(@colour, ${Header::colourfw}); | |
ac1cfefa | 115 | |
2c42fe6a | 116 | if (open(IP, "${General::swroot}/red/local-ipaddress")) { |
75bc929e MT |
117 | my $redip = <IP>; |
118 | close(IP); | |
119 | ||
120 | chomp $redip; | |
121 | push(@network, $redip); | |
122 | push(@masklen, '255.255.255.255'); | |
123 | push(@colour, ${Header::colourfw}); | |
2c42fe6a MT |
124 | } |
125 | ||
75bc929e MT |
126 | # Add STATIC RED aliases |
127 | if ($netsettings{'RED_DEV'}) { | |
128 | my $aliasfile = "${General::swroot}/ethernet/aliases"; | |
129 | open(ALIASES, $aliasfile) or die 'Unable to open aliases file.'; | |
130 | my @aliases = <ALIASES>; | |
131 | close(ALIASES); | |
132 | ||
133 | # We have a RED eth iface | |
134 | if ($netsettings{'RED_TYPE'} eq 'STATIC') { | |
135 | # We have a STATIC RED eth iface | |
136 | foreach my $line (@aliases) { | |
137 | chomp($line); | |
138 | my @temp = split(/\,/,$line); | |
139 | if ($temp[0]) { | |
140 | push(@network, $temp[0]); | |
141 | push(@masklen, $netsettings{'RED_NETMASK'} ); | |
142 | push(@colour, ${Header::colourfw} ); | |
143 | } | |
144 | } | |
145 | } | |
146 | } | |
ac1cfefa MT |
147 | |
148 | # Add Green Firewall Interface | |
149 | push(@network, $netsettings{'GREEN_ADDRESS'}); | |
150 | push(@masklen, "255.255.255.255" ); | |
151 | push(@colour, ${Header::colourfw} ); | |
152 | ||
153 | # Add Green Network to Array | |
154 | push(@network, $netsettings{'GREEN_NETADDRESS'}); | |
155 | push(@masklen, $netsettings{'GREEN_NETMASK'} ); | |
156 | push(@colour, ${Header::colourgreen} ); | |
157 | ||
158 | # Add Green Routes to Array | |
159 | my @routes = `/sbin/route -n | /bin/grep $netsettings{'GREEN_DEV'}`; | |
160 | foreach my $route (@routes) { | |
75bc929e MT |
161 | chomp($route); |
162 | my @temp = split(/[\t ]+/, $route); | |
163 | push(@network, $temp[0]); | |
164 | push(@masklen, $temp[2]); | |
165 | push(@colour, ${Header::colourgreen} ); | |
5433e2c9 MT |
166 | } |
167 | ||
f9aaffa6 MT |
168 | # Add Blue Firewall Interface |
169 | push(@network, $netsettings{'BLUE_ADDRESS'}); | |
170 | push(@masklen, "255.255.255.255" ); | |
171 | push(@colour, ${Header::colourfw} ); | |
172 | ||
5433e2c9 MT |
173 | # Add Blue Network |
174 | if ($netsettings{'BLUE_DEV'}) { | |
75bc929e MT |
175 | push(@network, $netsettings{'BLUE_NETADDRESS'}); |
176 | push(@masklen, $netsettings{'BLUE_NETMASK'} ); | |
177 | push(@colour, ${Header::colourblue} ); | |
178 | ||
179 | # Add Blue Routes to Array | |
180 | @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`; | |
181 | foreach my $route (@routes) { | |
182 | chomp($route); | |
183 | my @temp = split(/[\t ]+/, $route); | |
184 | push(@network, $temp[0]); | |
185 | push(@masklen, $temp[2]); | |
186 | push(@colour, ${Header::colourblue} ); | |
187 | } | |
188 | } | |
189 | ||
5ac2ed47 MT |
190 | # Add Orange Firewall Interface |
191 | push(@network, $netsettings{'ORANGE_ADDRESS'}); | |
192 | push(@masklen, "255.255.255.255" ); | |
193 | push(@colour, ${Header::colourfw} ); | |
194 | ||
75bc929e MT |
195 | # Add Orange Network |
196 | if ($netsettings{'ORANGE_DEV'}) { | |
197 | push(@network, $netsettings{'ORANGE_NETADDRESS'}); | |
198 | push(@masklen, $netsettings{'ORANGE_NETMASK'} ); | |
199 | push(@colour, ${Header::colourorange} ); | |
200 | # Add Orange Routes to Array | |
201 | @routes = `/sbin/route -n | /bin/grep $netsettings{'ORANGE_DEV'}`; | |
202 | foreach my $route (@routes) { | |
203 | chomp($route); | |
204 | my @temp = split(/[\t ]+/, $route); | |
205 | push(@network, $temp[0]); | |
206 | push(@masklen, $temp[2]); | |
207 | push(@colour, ${Header::colourorange} ); | |
208 | } | |
5433e2c9 MT |
209 | } |
210 | ||
c8a8778f MT |
211 | # Highlight multicast connections. |
212 | push(@network, "224.0.0.0"); | |
213 | push(@masklen, "239.0.0.0"); | |
214 | push(@colour, $colour_multicast); | |
215 | ||
6e13d0a5 MT |
216 | # Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate) |
217 | if (-e "${General::swroot}/ovpn/settings") { | |
75bc929e MT |
218 | my %ovpnsettings = (); |
219 | &General::readhash("${General::swroot}/ovpn/settings", \%ovpnsettings); | |
220 | my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'}); | |
221 | ||
222 | # add OpenVPN net | |
223 | push(@network, $tempovpnsubnet[0]); | |
224 | push(@masklen, $tempovpnsubnet[1]); | |
225 | push(@colour, ${Header::colourovpn} ); | |
226 | ||
227 | # add BLUE:port / proto | |
228 | if (($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'}) { | |
229 | push(@network, $netsettings{'BLUE_ADDRESS'} ); | |
230 | push(@masklen, '255.255.255.255' ); | |
231 | push(@colour, ${Header::colourovpn}); | |
232 | } | |
6e13d0a5 | 233 | |
75bc929e MT |
234 | # add ORANGE:port / proto |
235 | if (($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'}) { | |
236 | push(@network, $netsettings{'ORANGE_ADDRESS'} ); | |
237 | push(@masklen, '255.255.255.255' ); | |
238 | push(@colour, ${Header::colourovpn} ); | |
239 | } | |
240 | } | |
6e13d0a5 | 241 | |
c7220d6e DH |
242 | # Add OpenVPN net for custom OVPNs |
243 | if (-e "${General::swroot}/ovpn/ccd.conf") { | |
244 | open(OVPNSUB, "${General::swroot}/ovpn/ccd.conf"); | |
245 | my @ovpnsub = <OVPNSUB>; | |
246 | close(OVPNSUB); | |
247 | ||
248 | foreach (@ovpnsub) { | |
249 | my ($network, $mask) = split '/', (split ',', $_)[2]; | |
250 | ||
251 | $mask = ipv4_cidr2msk($mask) unless &General::validip($mask); | |
252 | ||
253 | push(@network, $network); | |
254 | push(@masklen, $mask); | |
255 | push(@colour, ${Header::colourovpn}); | |
256 | } | |
257 | } | |
258 | ||
03435d85 | 259 | open(IPSEC, "${General::swroot}/vpn/config"); |
75bc929e MT |
260 | my @ipsec = <IPSEC>; |
261 | close(IPSEC); | |
7dbf47dc | 262 | |
75bc929e MT |
263 | foreach my $line (@ipsec) { |
264 | my @vpn = split(',', $line); | |
6e13d0a5 | 265 | |
ffeaaef6 | 266 | my @subnets = split(/\|/, $vpn[12]); |
c6fba315 MT |
267 | for my $subnet (@subnets) { |
268 | my ($network, $mask) = split("/", $subnet); | |
269 | ||
270 | if (!&General::validip($mask)) { | |
271 | $mask = ipv4_cidr2msk($mask); | |
272 | } | |
ac1cfefa | 273 | |
c6fba315 MT |
274 | push(@network, $network); |
275 | push(@masklen, $mask); | |
276 | push(@colour, ${Header::colourvpn}); | |
277 | } | |
ac1cfefa | 278 | } |
ac1cfefa | 279 | |
d9ac41d5 MT |
280 | if (-e "${General::swroot}/ovpn/n2nconf") { |
281 | open(OVPNN2N, "${General::swroot}/ovpn/ovpnconfig"); | |
282 | my @ovpnn2n = <OVPNN2N>; | |
283 | close(OVPNN2N); | |
284 | ||
285 | foreach my $line (@ovpnn2n) { | |
286 | my @ovpn = split(',', $line); | |
287 | next if ($ovpn[4] ne 'net'); | |
288 | ||
289 | my ($network, $mask) = split("/", $ovpn[12]); | |
290 | if (!&General::validip($mask)) { | |
291 | $mask = ipv4_cidr2msk($mask); | |
292 | } | |
293 | ||
294 | push(@network, $network); | |
295 | push(@masklen, $mask); | |
296 | push(@colour, ${Header::colourovpn}); | |
297 | } | |
298 | } | |
299 | ||
75bc929e MT |
300 | # Show the page. |
301 | &Header::openpage($Lang::tr{'connections'}, 1, ''); | |
302 | &Header::openbigbox('100%', 'left'); | |
303 | &Header::openbox('100%', 'left', $Lang::tr{'connection tracking'}); | |
c2b15814 | 304 | |
75bc929e MT |
305 | # Print legend. |
306 | print <<END; | |
429c1a3f | 307 | <table style='width:100%'> |
75bc929e | 308 | <tr> |
429c1a3f AH |
309 | <td style='text-align:center;'> |
310 | <b>$Lang::tr{'legend'} :</b> | |
75bc929e | 311 | </td> |
429c1a3f AH |
312 | <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourgreen}; font-weight:bold;'> |
313 | <b>$Lang::tr{'lan'}</b> | |
75bc929e | 314 | </td> |
429c1a3f AH |
315 | <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourred};'> |
316 | <b>$Lang::tr{'internet'}</b> | |
75bc929e | 317 | </td> |
429c1a3f AH |
318 | <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourorange};'> |
319 | <b>$Lang::tr{'dmz'}</b> | |
75bc929e | 320 | </td> |
429c1a3f AH |
321 | <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourblue};'> |
322 | <b>$Lang::tr{'wireless'}</b> | |
75bc929e | 323 | </td> |
429c1a3f AH |
324 | <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourfw};'> |
325 | <b>IPFire</b> | |
75bc929e | 326 | </td> |
429c1a3f AH |
327 | <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourvpn};'> |
328 | <b>$Lang::tr{'vpn'}</b> | |
75bc929e | 329 | </td> |
429c1a3f AH |
330 | <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourovpn};'> |
331 | <b>$Lang::tr{'OpenVPN'}</b> | |
75bc929e | 332 | </td> |
429c1a3f AH |
333 | <td style='text-align:center; color:#FFFFFF; background-color:$colour_multicast;'> |
334 | <b>Multicast</b> | |
c8a8778f | 335 | </td> |
75bc929e MT |
336 | </tr> |
337 | </table> | |
338 | <br> | |
339 | END | |
c2b15814 | 340 | |
9b37e91e KMK |
341 | if ($SORT_FIELD and $SORT_ORDER) { |
342 | my @sort_field_name = ( | |
343 | $Lang::tr{'source ip'}, | |
344 | $Lang::tr{'destination ip'}, | |
345 | $Lang::tr{'source port'}, | |
346 | $Lang::tr{'destination port'}, | |
347 | $Lang::tr{'protocol'}, | |
348 | $Lang::tr{'connection'}.' '.$Lang::tr{'status'}, | |
349 | $Lang::tr{'expires'}.' ('.$Lang::tr{'seconds'}.')', | |
350 | $Lang::tr{'download'}, | |
351 | $Lang::tr{'upload'} | |
352 | ); | |
353 | my $sort_order_name; | |
354 | if (lc($SORT_ORDER) eq "a") { | |
355 | $sort_order_name = $Lang::tr{'sort ascending'}; | |
356 | } else { | |
357 | $sort_order_name = $Lang::tr{'sort descending'}; | |
358 | } | |
359 | ||
360 | print <<END | |
361 | <div style="font-weight:bold;margin:10px;font-size: 70%"> | |
362 | $sort_order_name: $sort_field_name[$SORT_FIELD-1] | |
363 | </div> | |
364 | END | |
365 | ; | |
366 | } | |
367 | ||
75bc929e MT |
368 | # Print table header. |
369 | print <<END; | |
429c1a3f AH |
370 | <table style='width:100%'> |
371 | <tr> | |
372 | <th style='text-align:center'> | |
373 | <a href="?sort_field=5&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a> | |
374 | <a href="?sort_field=5&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a> | |
9b37e91e | 375 | </th> |
429c1a3f AH |
376 | <th style='text-align:center' colspan='2'> |
377 | <a href="?sort_field=1&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a> | |
378 | <a href="?sort_field=1&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a> | |
9b37e91e | 379 | |
429c1a3f AH |
380 | <a href="?sort_field=3&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a> |
381 | <a href="?sort_field=3&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a> | |
9b37e91e | 382 | </th> |
ea4620fc | 383 | <th> </th> |
429c1a3f AH |
384 | <th style='text-align:center' colspan='2'> |
385 | <a href="?sort_field=2&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a> | |
386 | <a href="?sort_field=2&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a> | |
9b37e91e | 387 | |
429c1a3f AH |
388 | <a href="?sort_field=4&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a> |
389 | <a href="?sort_field=4&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a> | |
75bc929e | 390 | </th> |
ea4620fc | 391 | <th> </th> |
429c1a3f AH |
392 | <th style='text-align:center'> |
393 | <a href="?sort_field=8&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a> | |
394 | <a href="?sort_field=8&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a> | |
9b37e91e | 395 | |
429c1a3f AH |
396 | <a href="?sort_field=9&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a> |
397 | <a href="?sort_field=9&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a> | |
9b37e91e | 398 | </th> |
429c1a3f AH |
399 | <th style='text-align:center'> |
400 | <a href="?sort_field=6&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a> | |
401 | <a href="?sort_field=6&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a> | |
75bc929e | 402 | </th> |
429c1a3f AH |
403 | <th style='text-align:center'> |
404 | <a href="?sort_field=7&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a> | |
405 | <a href="?sort_field=7&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a> | |
9b37e91e KMK |
406 | </th> |
407 | </tr> | |
429c1a3f AH |
408 | <tr> |
409 | <th style='text-align:center'> | |
9b37e91e KMK |
410 | $Lang::tr{'protocol'} |
411 | </th> | |
429c1a3f | 412 | <th style='text-align:center' colspan='2'> |
9b37e91e KMK |
413 | $Lang::tr{'source ip and port'} |
414 | </th> | |
ea4620fc PM |
415 | <th style='text-align:center'> |
416 | $Lang::tr{'country'} | |
417 | </th> | |
429c1a3f | 418 | <th style='text-align:center' colspan='2'> |
75bc929e MT |
419 | $Lang::tr{'dest ip and port'} |
420 | </th> | |
ea4620fc PM |
421 | <th style='text-align:center'> |
422 | $Lang::tr{'country'} | |
423 | </th> | |
429c1a3f | 424 | <th style='text-align:center'> |
75bc929e MT |
425 | $Lang::tr{'download'} / |
426 | <br>$Lang::tr{'upload'} | |
427 | </th> | |
429c1a3f | 428 | <th style='text-align:center'> |
75bc929e MT |
429 | $Lang::tr{'connection'}<br>$Lang::tr{'status'} |
430 | </th> | |
429c1a3f | 431 | <th style='text-align:center'> |
75bc929e MT |
432 | $Lang::tr{'expires'}<br>($Lang::tr{'seconds'}) |
433 | </th> | |
434 | </tr> | |
435 | END | |
c2b15814 | 436 | |
75bc929e MT |
437 | foreach my $line (@conntrack) { |
438 | my @conn = split(' ', $line); | |
c2b15814 | 439 | |
75bc929e MT |
440 | # The first bit is the l3 protocol. |
441 | my $l3proto = $conn[0]; | |
c2b15814 | 442 | |
75bc929e MT |
443 | # Skip everything that is not IPv4. |
444 | if ($l3proto ne 'ipv4') { | |
445 | next; | |
446 | } | |
ac1cfefa | 447 | |
75bc929e MT |
448 | # L4 protocol (tcp, udp, ...). |
449 | my $l4proto = $conn[2]; | |
c2b15814 | 450 | |
7d55ca0d | 451 | # Translate unknown protocols. |
75bc929e | 452 | if ($l4proto eq 'unknown') { |
7d55ca0d MT |
453 | my $l4protonum = $conn[3]; |
454 | if ($l4protonum eq '2') { | |
455 | $l4proto = 'IGMP'; | |
456 | } elsif ($l4protonum eq '4') { | |
457 | $l4proto = 'IPv4 Encap'; | |
458 | } elsif ($l4protonum eq '33') { | |
459 | $l4proto = 'DCCP'; | |
460 | } elsif ($l4protonum eq '41') { | |
461 | $l4proto = 'IPv6 Encap'; | |
462 | } elsif ($l4protonum eq '50') { | |
463 | $l4proto = 'ESP'; | |
464 | } elsif ($l4protonum eq '51') { | |
465 | $l4proto = 'AH'; | |
466 | } elsif ($l4protonum eq '132') { | |
467 | $l4proto = 'SCTP'; | |
468 | } else { | |
469 | $l4proto = $l4protonum; | |
470 | } | |
471 | } else { | |
472 | $l4proto = uc($l4proto); | |
75bc929e | 473 | } |
4809e64e | 474 | |
75bc929e MT |
475 | # Source and destination. |
476 | my $sip; | |
c9e01c8c | 477 | my $sip_ret; |
75bc929e | 478 | my $dip; |
c9e01c8c | 479 | my $dip_ret; |
75bc929e | 480 | my $sport; |
c9e01c8c | 481 | my $sport_ret; |
75bc929e | 482 | my $dport; |
c9e01c8c | 483 | my $dport_ret; |
75bc929e MT |
484 | my @packets; |
485 | my @bytes; | |
486 | ||
487 | my $ttl = $conn[4]; | |
488 | my $state; | |
7d55ca0d | 489 | if ($l4proto eq 'TCP') { |
75bc929e MT |
490 | $state = $conn[5]; |
491 | } | |
c2b15814 | 492 | |
75bc929e MT |
493 | # Kick out everything that is not IPv4. |
494 | foreach my $item (@conn) { | |
495 | my ($key, $val) = split('=', $item); | |
496 | ||
497 | switch ($key) { | |
498 | case "src" { | |
c9e01c8c MT |
499 | if ($sip == "") { |
500 | $sip = $val; | |
501 | } else { | |
502 | $dip_ret = $val; | |
503 | } | |
75bc929e MT |
504 | } |
505 | case "dst" { | |
c9e01c8c MT |
506 | if ($dip == "") { |
507 | $dip = $val; | |
508 | } else { | |
509 | $sip_ret = $val; | |
510 | } | |
75bc929e MT |
511 | } |
512 | case "sport" { | |
c9e01c8c MT |
513 | if ($sport == "") { |
514 | $sport = $val; | |
515 | } else { | |
516 | $dport_ret = $val; | |
517 | } | |
75bc929e MT |
518 | } |
519 | case "dport" { | |
c9e01c8c MT |
520 | if ($dport == "") { |
521 | $dport = $val; | |
522 | } else { | |
523 | $sport_ret = $val; | |
524 | } | |
75bc929e MT |
525 | } |
526 | case "packets" { | |
527 | push(@packets, $val); | |
528 | } | |
529 | case "bytes" { | |
530 | push(@bytes, $val); | |
531 | } | |
532 | } | |
533 | } | |
1465b127 | 534 | |
75bc929e | 535 | my $sip_colour = ipcolour($sip); |
e60cd3a4 DH |
536 | # use colour of destination network for DNAT |
537 | my $dip_colour = $dip ne $dip_ret ? ipcolour($dip_ret) : ipcolour($dip); | |
1465b127 | 538 | |
7d55ca0d MT |
539 | my $sserv = ''; |
540 | if ($sport < 1024) { | |
75bc929e | 541 | $sserv = uc(getservbyport($sport, lc($l4proto))); |
7d55ca0d | 542 | } |
1465b127 | 543 | |
7d55ca0d MT |
544 | my $dserv = ''; |
545 | if ($dport < 1024) { | |
75bc929e | 546 | $dserv = uc(getservbyport($dport, lc($l4proto))); |
7d55ca0d | 547 | } |
1465b127 | 548 | |
75bc929e MT |
549 | my $bytes_in = format_bytes($bytes[0]); |
550 | my $bytes_out = format_bytes($bytes[1]); | |
551 | ||
ea4620fc PM |
552 | # enumerate GeoIP information |
553 | my $srcccode = &GeoIP::lookup($sip_ret); | |
554 | my $src_flag_icon = &GeoIP::get_flag_icon($srcccode); | |
555 | my $dstccode = &GeoIP::lookup($dip_ret); | |
556 | my $dst_flag_icon = &GeoIP::get_flag_icon($dstccode); | |
557 | ||
75bc929e MT |
558 | # Format TTL |
559 | $ttl = format_time($ttl); | |
560 | ||
c9e01c8c | 561 | my $sip_extra; |
ee299e2e | 562 | if ($sip_ret && $sip ne $sip_ret) { |
429c1a3f | 563 | $sip_extra = "<span style='color:#FFFFFF;'>></span> "; |
c9e01c8c | 564 | $sip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$sip_ret'>"; |
429c1a3f | 565 | $sip_extra .= " <span style='color:#FFFFFF;'>$sip_ret</span>"; |
c9e01c8c MT |
566 | $sip_extra .= "</a>"; |
567 | } | |
568 | ||
569 | my $dip_extra; | |
ee299e2e | 570 | if ($dip_ret && $dip ne $dip_ret) { |
429c1a3f | 571 | $dip_extra = "<span style='color:#FFFFFF;'>></span> "; |
c9e01c8c | 572 | $dip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$dip_ret'>"; |
429c1a3f | 573 | $dip_extra .= " <span style='color:#FFFFFF;'>$dip_ret</span>"; |
c9e01c8c MT |
574 | $dip_extra .= "</a>"; |
575 | } | |
576 | ||
577 | ||
578 | my $sport_extra; | |
579 | if ($sport ne $sport_ret) { | |
580 | my $sserv_ret = ''; | |
581 | if ($sport_ret < 1024) { | |
582 | $sserv_ret = uc(getservbyport($sport_ret, lc($l4proto))); | |
583 | } | |
584 | ||
429c1a3f | 585 | $sport_extra = "<span style='color:#FFFFFF;'>></span> "; |
c9e01c8c | 586 | $sport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$sport_ret' target='top' title='$sserv_ret'>"; |
429c1a3f | 587 | $sport_extra .= " <span style='color:#FFFFFF;'>$sport_ret</span>"; |
c9e01c8c MT |
588 | $sport_extra .= "</a>"; |
589 | } | |
590 | ||
591 | my $dport_extra; | |
592 | if ($dport ne $dport_ret) { | |
593 | my $dserv_ret = ''; | |
594 | if ($dport_ret < 1024) { | |
595 | $dserv_ret = uc(getservbyport($dport_ret, lc($l4proto))); | |
596 | } | |
597 | ||
429c1a3f | 598 | $dport_extra = "<span style='color:#FFFFFF;'>></span> "; |
c9e01c8c | 599 | $dport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$dport_ret' target='top' title='$dserv_ret'>"; |
429c1a3f | 600 | $dport_extra .= " <span style='color:#FFFFFF;'>$dport_ret</span>"; |
c9e01c8c MT |
601 | $dport_extra .= "</a>"; |
602 | } | |
603 | ||
75bc929e MT |
604 | print <<END; |
605 | <tr> | |
429c1a3f AH |
606 | <td style='text-align:center'>$l4proto</td> |
607 | <td style='text-align:center; background-color:$sip_colour;'> | |
75bc929e | 608 | <a href='/cgi-bin/ipinfo.cgi?ip=$sip'> |
429c1a3f | 609 | <span style='color:#FFFFFF;'>$sip</span> |
75bc929e | 610 | </a> |
c9e01c8c | 611 | $sip_extra |
75bc929e | 612 | </td> |
429c1a3f | 613 | <td style='text-align:center; background-color:$sip_colour;'> |
c9e01c8c | 614 | <a href='http://isc.sans.org/port_details.php?port=$sport' target='top' title='$sserv'> |
429c1a3f | 615 | <span style='color:#FFFFFF;'>$sport</span> |
75bc929e | 616 | </a> |
c9e01c8c | 617 | $sport_extra |
75bc929e | 618 | </td> |
ea4620fc PM |
619 | <td style='text-align:center; background-color:$sip_colour;'> |
620 | <a href='country.cgi#$srcccode'><img src='$src_flag_icon' border='0' align='absmiddle' alt='$srcccode' title='$srcccode' /></a> | |
621 | </td> | |
429c1a3f | 622 | <td style='text-align:center; background-color:$dip_colour;'> |
75bc929e | 623 | <a href='/cgi-bin/ipinfo.cgi?ip=$dip'> |
429c1a3f | 624 | <span style='color:#FFFFFF;'>$dip</span> |
75bc929e | 625 | </a> |
c9e01c8c | 626 | $dip_extra |
75bc929e | 627 | </td> |
429c1a3f | 628 | <td style='text-align:center; background-color:$dip_colour;'> |
c9e01c8c | 629 | <a href='http://isc.sans.org/port_details.php?port=$dport' target='top' title='$dserv'> |
429c1a3f | 630 | <span style='color:#FFFFFF;'>$dport</span> |
75bc929e | 631 | </a> |
c9e01c8c | 632 | $dport_extra |
75bc929e | 633 | </td> |
ea4620fc PM |
634 | <td style='text-align:center; background-color:$sip_colour;'> |
635 | <a href='country.cgi#$dstccode'><img src='$dst_flag_icon' border='0' align='absmiddle' alt='$dstccode' title='$dstccode' /></a> | |
636 | </td> | |
429c1a3f | 637 | <td style='text-align:center'> |
75bc929e MT |
638 | $bytes_in / $bytes_out |
639 | </td> | |
429c1a3f AH |
640 | <td style='text-align:center'>$state</td> |
641 | <td style='text-align:center'>$ttl</td> | |
75bc929e | 642 | </tr> |
ac1cfefa | 643 | END |
ac1cfefa | 644 | } |
c2b15814 | 645 | |
75bc929e MT |
646 | # Close the main table. |
647 | print "</table>"; | |
ac1cfefa MT |
648 | |
649 | &Header::closebox(); | |
650 | &Header::closebigbox(); | |
651 | &Header::closepage(); | |
652 | ||
75bc929e MT |
653 | sub format_bytes($) { |
654 | my $bytes = shift; | |
655 | my @units = ("B", "k", "M", "G", "T"); | |
656 | ||
657 | foreach my $unit (@units) { | |
658 | if ($bytes < 1024) { | |
659 | return sprintf("%d%s", $bytes, $unit); | |
660 | } | |
c2b15814 | 661 | |
75bc929e MT |
662 | $bytes /= 1024; |
663 | } | |
664 | ||
665 | return sprintf("%d%s", $bytes, $units[$#units]); | |
c2b15814 MT |
666 | } |
667 | ||
75bc929e MT |
668 | sub format_time($) { |
669 | my $time = shift; | |
c2b15814 | 670 | |
75bc929e MT |
671 | my $seconds = $time % 60; |
672 | my $minutes = $time / 60; | |
c2b15814 | 673 | |
75bc929e MT |
674 | my $hours = 0; |
675 | if ($minutes >= 60) { | |
676 | $hours = $minutes / 60; | |
677 | $minutes %= 60; | |
678 | } | |
679 | ||
680 | return sprintf("%3d:%02d:%02d", $hours, $minutes, $seconds); | |
c2b15814 MT |
681 | } |
682 | ||
75bc929e MT |
683 | sub ipcolour($) { |
684 | my $id = 0; | |
685 | my $colour = ${Header::colourred}; | |
686 | my ($ip) = $_[0]; | |
687 | my $found = 0; | |
688 | ||
ee299e2e MT |
689 | if ($ip) { |
690 | foreach my $line (@network) { | |
691 | if ($network[$id] eq '') { | |
692 | $id++; | |
693 | } else { | |
694 | if (!$found && ipv4_in_network($network[$id], $masklen[$id], $ip) ) { | |
695 | $found = 1; | |
696 | $colour = $colour[$id]; | |
697 | } | |
698 | $id++; | |
75bc929e | 699 | } |
75bc929e MT |
700 | } |
701 | } | |
702 | ||
703 | return $colour; | |
c2b15814 MT |
704 | } |
705 | ||
706 | 1; |