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