-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipls
executable file
·144 lines (128 loc) · 4.4 KB
/
ipls
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
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw/any/;
use IPTables::Parse;
use Data::Dumper;
#use first 'Text::ANSITable', 'Text::ASCIITable', 'Text::Table::Tiny';
use first 'Text::ASCIITable', 'Text::Table::Tiny';
our $tableclass = $first::module;
our @column_names = (qw(Int Out Prot Src Dest State Target Extended Comment));
unshift @column_names, '#';
sub process_rule {
my $rule = shift;
my $int = ($rule->{intf_in} eq '*') ? '' : $rule->{intf_in};
my $ext = ($rule->{intf_out} eq '*') ? '' : $rule->{intf_out};
my $source = ($rule->{src} eq '0.0.0.0/0') ? '' : $rule->{src};
$source .= ":".$rule->{s_port} if $rule->{s_port};
my $dest = ($rule->{dst} eq '0.0.0.0/0') ? '' : $rule->{dst};
$dest .= ":".$rule->{d_port} if $rule->{d_port};
my $prot = ($rule->{protocol} eq 'all') ? '' : $rule->{protocol};
my $state = $rule->{state};
my $extended = $rule->{extended};
my @res = (qr/^\s+/, qr/\s+$/);
unshift @res, qr/\bstate $state\b/ if $state;
unshift @res, qr/\breject-with icmp-port-unreachable\b/ if ($rule->{target} eq 'REJECT') && $prot && ($prot ne 'icmp');
if ($rule->{d_port}) {
my $dstr = join '', 'dpt', (($rule->{d_port} =~ /:/) ? 's' : ''), ':', $rule->{d_port};
my $re = qr/\b$rule->{protocol}\s+$dstr\b/;
unshift @res, $re;
}
$extended =~ s{$_}{} for @res;
$extended =~ s{\s+}{ }g;
return [$rule->{rule_num}, $int, $ext, $prot, $source, $dest, $state, $rule->{target},$extended,$rule->{comment}];
}
sub assign {
my ($columns,$listref) = @_;
for (my $i=0; $i < @$listref; $i++) {
push @{$columns->{$column_names[$i]}}, $listref->[$i];
}
}
sub process_chain {
my ($chain, $policy, $rules) = @_;
my %columns;
$columns{$_} = [] for @column_names;
my $chaintext = join ' ', $chain, "(policy: $policy)" if $policy;
for my $rule (@$rules) {
assign(\%columns,process_rule($rule));
}
my $matrix = [];
for my $column (@column_names) {
push @$matrix, [ $column, @{$columns{$column}} ] if (any { defined $_ and $_ =~ /\S/ } @{$columns{$column}});
}
return ($chaintext, []) unless scalar @$matrix;
my @transposed;
my $lc = $#{$matrix->[0]};
for my $col (0..$lc) {
push @transposed, [map $_->[$col], @$matrix];
}
return ($chaintext,\@transposed);
}
sub generate_output {
my ($ipt, $table) = @_;
my $chains = $ipt->list_table_chains($table);
my @output;
for my $chain (@$chains) {
my $rules = $ipt->chain_rules($table, $chain);
my $policy = $ipt->chain_policy($table, $chain);
my ($chaintext, $rows) = process_chain($chain, $policy, $rules);
if ($tableclass eq 'Text::Table::Tiny') {
{
no warnings 'once';
$Text::Table::Tiny::HEADER_CORNER_MARKER = '+';
$Text::Table::Tiny::HEADER_ROW_SEPARATOR = '-';
}
push @output, $chaintext.((defined $rows) ? Text::Table::Tiny::generate_table(rows => $rows, header_row => 1, separate_rows => 1) : "No rules"), '';
next;
}
binmode(STDOUT, ":utf8");
my $t;
if ($tableclass eq 'Text::ASCIITable') {
if (@$rows) {
my $columns = shift @$rows;
$t = Text::ASCIITable->new({headingText => $chaintext, headingAlign => 'left'});
$t->setCols($columns);
$t->setColWidth('Extended',25);
$t->addRow($rows);
$t->setOptions({drawRowLine => 1});
} else {
$t =Text::ASCIITable->new;
$t->setCols([$chaintext]);
$t->addRow(["No rules"]);
}
push @output, $t->draw; # , '';
} else { #Text::ANSITable
if (@$rows) {
push @output, $chaintext;
$t = Text::ANSITable->new;
$t->use_utf8(1);
$t->use_box_chars(1);
$t->border_style('Default::singleh_boxchar');
$t->apply_style_set(AltRow => {even_bgcolor => '333333'});
$t->set_column_style($_, align => 'right', wrap => { ansi => 1}) for (0..(@$rows-1));
# $t->set_column_style(0, width => 2);
$t->rows($rows);
push @output, $t->draw; # , '';
} else {
push @output, $chaintext, 'No rules', '';
}
}
}
return \@output;
}
my ($table, $input);
if ((defined $ARGV[0]) && ($ARGV[0] eq '-')) {
$input = do {local $/; <>};
} else {
$table = shift @ARGV;
}
$table ||= 'filter';
my %opt;
if ($input) {
$opt{ipt_rules} = $input;
} elsif ($<) {
die "Must be run as root\n";
}
my $ipt = IPTables::Parse->new(%opt);
my $output = generate_output($ipt, $table);
print join "\n", @$output;