This file is indexed.

/usr/share/logwatch/scripts/services/iptables is in logwatch 7.4.1-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/perl
##########################################################################
# $Id: iptables 175 2013-11-08 17:05:21Z opoplawski $
##########################################################################
# $Log: iptables,v $
# Revision 1.9  2009/06/05 14:06:07  mike
# Added IPF patch from Don Wilder/Mark Dalton -mgt
#
# Revision 1.8  2008/03/24 23:31:26  kirk
# added copyright/license notice to each script
#
# Revision 1.7  2007/02/16 03:27:05  bjorn
# Remove explicit invocation of perl executable, by Ivana Varekova.
#
# Revision 1.6  2007/01/29 18:40:29  bjorn
# Handle timestamp generated in Ubuntu, suggested by MrC.
#
# Revision 1.5  2006/08/23 22:47:29  bjorn
# Corrected icmp type reporting, by Allen Kistler.
#
# Revision 1.4  2006/07/11 15:59:56  bjorn
# Allow sorting by either source or target, by Michel Messerschmidt.
#
# Revision 1.3  2006/01/16 18:40:31  kirk
# fixed name to Logwatch (how I like it now)
#
# Revision 1.2  2005/12/06 02:35:43  bjorn
# Report icmp type properly, by Allen Kistler.
#
# Revision 1.1  2005/07/25 22:17:31  bjorn
# Moved iptables (and ipchains, ipfwadm) code to its own service (iptables).
#
##########################################################################
# iptables, ipchains, and ipfwadm script for Logwatch.
# Ipfwadm and ipchains are deprecated, but is included
# here for backwards compatibility.
#
# This script was extracted from the kernel script,
# which processed netfilter (iptables, ipchains, and
# ipfwadm) statements until kernel script Revision 1.29.
#
# Visit the Logwatch website at
#   http://www.logwatch.org
##########################################################################

#####################################################
## Copyright (c) 2008 Kirk Bauer
## Covered under the included MIT/X-Consortium License:
##    http://www.opensource.org/licenses/mit-license.php
## All modifications and contributions by other persons to
## this script are assumed to have been donated to the
## Logwatch project and thus assume the above copyright
## and licensing terms.  If you want to make contributions
## under your own copyright or a different license this
## must be explicitly stated in the contribution an the
## Logwatch project reserves the right to not accept such
## contributions.  If you have made significant
## contributions to this script and want to claim
## copyright please contact logwatch-devel@lists.sourceforge.net.
#########################################################

use Logwatch ':ip';

$Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;
$MinFilter = $ENV{'iptables_host_min_count'} || 0;
$DoLookup = $ENV{'iptables_ip_lookup'}; $DoLookup = $DoLookup; # keep -w happy
$ListByHost = $ENV{'iptables_list_by_host'} || 0;
$ListByService = $ENV{'iptables_list_by_service'} || 0;
# Keep old behaviour if nothing is configured
$ListByHost = 1 unless ($ListByService);
$MaxFlood = 10;
$MaxNum =0;

sub lookupService {
   my ($port, $proto, $service);
   ($port, $proto) = ($_[0], $_[1]);
   if ($service = getservbyport ($port, $proto)) {
      return($service);
   } else {
      return($port);
   }
}

sub lookupProtocol {
   my ($proto, $name);
   $proto = $_[0];
   if ($name = getprotobynumber ($proto)) {
      return($name);
   } else {
      return($proto);
   }
}

sub lookupAction {
   my ($chain, $actionType);
   $chain = $_[0];

   # choose an action type
   if ( $chain =~ /reject/i ) {
      $actionType = "Rejected";
   } elsif ( $chain =~ /drop/i ) {
      $actionType = "Dropped";
   } elsif ( $chain =~ /deny/i ) {
      $actionType = "Denied";
   } elsif ( $chain =~ /denied/i ) {
      $actionType = "Denied";
   } elsif ( $chain =~ /accept/i ) {
      $actionType = "Accepted";
   } else {
      $actionType = "Logged";
   }

   return $actionType;
}

# SORT COMPARISONS
sub compStr {
   return $a cmp $b;
}

sub compNum {
   return $a <=> $b;
}

while (defined($ThisLine = <STDIN>)) {
   chomp($ThisLine);
   next if ($ThisLine eq '');

   # the format for ulogd/ulogd.syslogmenu and messages differ in that
   # the earlier has no service name after the date.  So RemoveHeaders
   # doesn't work.  Therefore, we extract it here:
   $ThisLine =~ s/^... .. ..:..:.. ([^ ]*) (kernel: )?(\[\s*\d+\.\d+\] )?//;

   # IPCHAINS
   if( ($TU,$from,$port,$on) = ( $ThisLine =~ /IP fw-in deny \w+ (\w+) ([^:]+):\d+ ([^:]+):(\d+) / ) ){
      if($MaxNum < ++$TCPscan{$TU}{$from}) {
         $MaxNum = $TCPscan{$TU}{$from}
      }
      $port=0;
   } elsif ( ($chain,$action,$if,$proto,$fromip,$toip,$toport) = ( $ThisLine =~ /^Packet log: ([^ ]+) (\w+) (\w+) PROTO=(\d+) ([\d|\.]+):\d+ ([\d|\.]+):(\d+)/ ) ) {
      $actionType = lookupAction($action);
      $ipt{$actionType}{$if}{$fromip}{$toip}{$toport}{$proto}{"$chain,$if"}++;
      $ipt2{$actionType}{$if}{$toport}{$proto}{$fromip}{$toip}{"$chain,$if"}++;
   }
   # IPTABLES
   elsif (($chain,$ifin,$ifout,$fromip,$toip,$proto,$rest) = ($ThisLine =~ /^(.*?)\s*IN=([\w\.\-]*).*?OUT=([\w\.\-]*).*?SRC=([\w\.:]+).*?DST=([\w\.:]+).*?PROTO=(\w+)(.*)/ )) {

      # get a destination port number  (or icmp type) if there is one
      if (! ( ($toport) = ( $rest =~ /TYPE=(\w+)/ ) ) ) {
         if (! ( ($toport) = ( $rest =~ /DPT=(\w+)/ ) ) ) {
            $toport = 0;
         }
      }

      # get the action type
      $actionType = lookupAction($chain);

      # determine the dominant interface
      if ($ifin  =~ /\w+/ && $ifout  =~ /\w+/) {
         $interface = $ifin;
      } elsif ($ifin =~ /\w+/) {
         $interface = $ifin;
         $ifout = "none";
      } else {
         $interface = $ifout;
         $ifin = "none";
      }

      if ($chain eq "") {
         $chain_info = "";
      } else {
         $chain_info = "(" . $chain . ") ";
      }

      # add the packet
#      $ipt{$actionType}{$interface}{$fromip}{$toip}{$toport}{$proto}{"$chain,$ifin,$ifout"}++;
      $ipt{$actionType}{$interface}{$fromip}{$toip}{$toport}{$proto}{$chain_info}++;
      $ipt2{$actionType}{$interface}{$toport}{$proto}{$fromip}{$toip}{$chain_info}++;
   }
   # IPF
   elsif (($repcnt,$if,$chain,$fromip,$fromport,$toip,$toport,$proto,$rest,$inout) = ($ThisLine =~ /^.*\d{2,2}:\d{2,2}:\d{2,2}\.\d{6,6}\s*(?:(\d{1,})x)*\s*(\w*)\s*@[-]*\d{1,}:[-]*\d{1,}\s*(\w*)\s*([\w\.:]+\w),*(\d*)\s*->\s*([\w\.:]+\w),*(\d*)\s*PR\s*(\w*)\s*(.*((IN|OUT)).*)/)) {
      if ($chain eq 'b') {
          $actionType = "drop";
      } elsif ($chain eq 'p') {
          $actionType = "accept";
      }
      if ($repcnt eq '') {
         $repcnt = 1;
      }
      while ($repcnt >= 1) {
        $ipt{$actionType}{$if}{$fromip}{$toip}{$toport}{$proto}{"$actionType,$if"}++;
        $ipt2{$actionType}{$if}{$toport}{$proto}{$fromip}{$toip}{"$actionType,$if"}++;
        $repcnt = $repcnt - 1;
      }
   }
}

# IPCHAINS
if (keys %TCPscan and $MaxNum>$MaxFlood) {
   print "\nWarning: ipfwadm scan detected on:\n";
   foreach $ThisOne (sort compStr keys %TCPscan) {
      print "   " . $ThisOne . " from:\n";
      foreach $Next (sort compStr keys %{$TCPscan{$ThisOne}}) {
         $TCPscan{$ThisOne}{$Next}>$MaxFlood &&
            print "      " . LookupIP($Next). ": $TCPscan{$ThisOne}{$Next} Time(s)\n";
      }
   }
}


if ((keys %ipt2) and $ListByService) {
   foreach my $actionType (sort compStr keys %ipt2) {
      foreach my $interface (sort compStr keys %{$ipt2{$actionType}}) {
         my $outputMain = '';
         my $interfaceCount = 0;
         foreach my $toport (sort compNum keys %{$ipt2{$actionType}{$interface}}) {
            foreach my $proto (sort compStr keys %{$ipt2{$actionType}{$interface}{$toport}}) {
               my $outputSection = '';
               my $portCount = 0;
               my $hostCount = 0;
               undef %hostList;
               my %host_list = ();
               my $protocol;
               # determine the protocol
               if ( $proto =~ /^\d+$/ ) {
                  $protocol = lookupProtocol($proto);
               } else {
                  $protocol = lc($proto);
               }

               # determine the name of the service
               my $service = lookupService($toport,$protocol);

               foreach my $fromip (sort SortIP keys %{$ipt2{$actionType}{$interface}{$toport}{$proto}}) {
                  my $fromHostCount = 0;
                  my $from = LookupIP($fromip);
                  my $outputDetails = "";
                  foreach my $toip (sort SortIP keys %{$ipt2{$actionType}{$interface}{$toport}{$proto}{$fromip}}) {
                     my $toHostCount = 0;
                     my $to = LookupIP($toip);

                     foreach my $details (sort keys %{$ipt2{$actionType}{$interface}{$toport}{$proto}{$fromip}{$toip}}) {
                        my $packetCount = $ipt2{$actionType}{$interface}{$toport}{$proto}{$fromip}{$toip}{$details};
                        $toHostCount += $packetCount;
                        if ( $Detail > 9 and ( $outputDetails !~ /\Q$details\E/ ) ) {
                           $outputDetails .= $details . ", ";
                        }
                     }
                     $fromHostCount += $toHostCount;
                  }
                     if ( $Detail > 9 ) {
                        chop $outputDetails;
                        chop $outputDetails;
                        push @{$hostList{"$fromHostCount"}}, $from .
                             " " . $outputDetails;
                     } else {
                        push @{$hostList{"$fromHostCount"}}, $from;
                     }
                  $portCount += $fromHostCount;
                  $hostCount++;
               }

               $interfaceCount += $portCount;
               if ($Detail > 5 ) {
                  $outputMain .= sprintf("   To port %d/%s (%s) - ".
                                         "%d packet%s from %d host%s\n",
                                   $toport, $protocol,
                                   ( $service =~ /^\d+$/ ) ? "?" : $service,
                                   $portCount, ( $portCount > 1 ) ? "s" : " ",
                                   $hostCount, ( $hostCount > 1 ) ? "s" : " "
                                 );
                  foreach my $hc (sort { $b <=> $a } keys %hostList) {
                     foreach my $h (@{$hostList{"$hc"}}) {
                        $outputMain .= sprintf("    %6d packet%s from %s\n",
                                       $hc, ( $hc > 1 ) ? "s" : " ", $h);
                     }
                  }
               } elsif ($Detail > 3 ) {
                  my $topHostCount;
                  ($topHostCount, undef) = sort { $b <=> $a } keys %hostList;
                  my $topHost = ${$hostList{"$topHostCount"}}[0];
                  $outputMain .= sprintf( "   To port %5d/%s - %5d packet%s ".
                                         "from %4d host%s (%d from %s)\n",
                                   $toport, $protocol, $portCount,
                                   ( $portCount > 1 ) ? "s" : " ", $hostCount,
                                   ( $hostCount > 1 ) ? "s" : " ",
                                   $topHostCount, $topHost
                                 );
               } else {
                  $outputMain .= sprintf("   To port %5d/%s - %5d packet%s ".
                                         "from %4d host%s\n",
                                   $toport, $protocol, $portCount,
                                   ( $portCount > 1 ) ? "s" : " ", $hostCount,
                                   ( $hostCount > 1 ) ? "s" : " "
                                 );
               }
            }
         }
         print "Listed by target ports:";
         print "\n$actionType $interfaceCount " .
               ( ( $interfaceCount > 1 ) ? "packets" : "packet" ) .
               " on interface $interface\n";
         print $outputMain;
      }
   }
}


# IPCHAINS / IPTABLES
if ((keys %ipt) and $ListByHost) {
   foreach $actionType (sort compStr keys %ipt) {
      foreach $interface (sort compStr keys %{$ipt{$actionType}}) {
         $outputMain = '';
         $interfaceCount = 0;
         foreach $fromip (sort SortIP keys %{$ipt{$actionType}{$interface}}) {
            $outputSection = '';
            $fromHostCount = 0;
            $from = LookupIP($fromip);
            undef %port_list;
            foreach $toip (sort SortIP keys %{$ipt{$actionType}{$interface}{$fromip}}) {
               $toHostCount = 0;
               $to = LookupIP($toip);
               $outputServices = '';
               foreach $toport (sort compNum keys %{$ipt{$actionType}{$interface}{$fromip}{$toip}}) {
                  foreach $proto (sort compStr keys %{$ipt{$actionType}{$interface}{$fromip}{$toip}{$toport}}) {
                     # determine the protocol
                     if ( $proto =~ /^\d+$/ ) {
                        $protocol = lookupProtocol($proto);
                     } else {
                        $protocol = lc($proto);
                     }

                     # determine the name of the service
                     $service = lookupService($toport,$protocol);

                     foreach $details (sort keys %{$ipt{$actionType}{$interface}{$fromip}{$toip}{$toport}{$proto}}) {
                        $packetCount = $ipt{$actionType}{$interface}{$fromip}{$toip}{$toport}{$proto}{$details};
                        $toHostCount += $packetCount;
                        if ( $Detail > 0 ) {
                           $outputServices .= "         Service: $service ($protocol/$toport) $details- $packetCount " . ( ( $packetCount > 1 ) ? "packets\n" : "packet\n" );
                        } else {
                           ${ $port_list{ $protocol } }{$toport}++;
                        }
                     }
                  }
               }
               $fromHostCount += $toHostCount;
               if ( $Detail > 0 ) { $outputSection .= "      To $to - $toHostCount " . ( ( $toHostCount > 1 ) ? "packets\n" : "packet\n" ); }
               $outputSection .= $outputServices;
            }
            $interfaceCount += $fromHostCount;
            if ($fromHostCount >= $MinFilter) {
               if ($Detail > 0 ) {
                  $outputMain .= "   From $from - $fromHostCount " . ( ( $fromHostCount > 1 ) ? "packets\n" : "packet\n" );
               } else {
                  $outputMain .= "  From $from - $fromHostCount " .  ( ($fromHostCount > 1) ? "packets" : "packet" ) .  " to " ;
                  foreach $protocol ( keys %port_list ) {
                     if ( $#{ keys %{$port_list { $protocol } } } > 10 ) {
                        $outputMain .= $#{ $port_list{ $protocol } } ." $protocol ports";
                     } else {
                        $outputMain .= "$protocol(" . join(",", sort compNum keys %{ $port_list{ $protocol } } ) . ") " ;
                     }
                  }
                  $outputMain .="\n";
               }
            }
            $outputMain .= $outputSection;
         }
         print "\nListed by source hosts:";
         print "\n$actionType $interfaceCount " . ( ( $interfaceCount > 1 ) ? "packets" : "packet" ) . " on interface $interface\n";
         print $outputMain;
      }
   }
}


exit(0);

# vi: shiftwidth=3 tabstop=3 syntax=perl et
# Local Variables:
# mode: perl
# perl-indent-level: 3
# indent-tabs-mode: nil
# End: