blib/lib/IPTables/Parse.pm

filter: Statementstmt 4.4% 19 / 425 Branchbran 0.0% 0 / 266 Conditioncond 0.0% 0 / 231 MC/DCmcdc 0.0% 0 / 127 Subroutinesub 30.0% 6 / 20 Podpod 41.6% 5 / 12 totaltotal 2.7% 30 / 1081 CC 236 SCAR 72.0
CC
236
Cov
2%
new
2256.0
exec_iptables
1980.0
chain_rules
1806.0
CRAP 1345.6
↑ Summary
1 #
2 ##################################################################
3 #
4 # File: IPTables::Parse.pm
5 #
6 # Purpose: Perl interface to parse iptables and ip6tables rulesets.
7 #
8 # Author: Michael Rash (mbr@cipherdyne.org)
9 #
10 # Version: 1.5
11 #
12 ##################################################################
13 #
14  
15 package IPTables::Parse;
16  
17 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 $VERSION = '1.5';
25  
26 sub new() {
27 0 ▶ my $class = shift;
Subroutine new: not called CC=47 SCAR=77.2
Pod undocumented
28 0 my %args = @_;
29  
30 0 my $ipt_bin = '/sbin/iptables';
31 0 my $ipt6_bin = '/sbin/ip6tables';
32 0 my $fwc_bin = '/usr/bin/firewall-cmd';
33  
34 my $self = {
35 _iptables => $args{'iptables'} || $args{'ip6tables'} || '',
36 _firewall_cmd => $args{'firewall-cmd'} || '',
37 _fwd_args => $args{'fwd_args'} || '--direct --passthrough ipv4',
38 _ipv6 => $args{'use_ipv6'} || 0,
39 _iptout => $args{'iptout'} || '/tmp/ipt.out' . $$,
40 _ipterr => $args{'ipterr'} || '/tmp/ipt.err' . $$,
41 _ipt_alarm => $args{'ipt_alarm'} || 30,
42 _debug => $args{'debug'} || 0,
43 _verbose => $args{'verbose'} || 0,
44 _ipt_rules_file => $args{'ipt_rules_file'} || '',
45 _ipt_exec_style => $args{'ipt_exec_style'} || 'waitpid',
46 _ipt_exec_sleep => $args{'ipt_exec_sleep'} || 0,
47 _sigchld_handler => $args{'sigchld_handler'} || \&REAPER,
48 0 ▶ _skip_ipt_exec_check => $args{'skip_ipt_exec_check'} || 0
Condition0%0/31
$args{'iptables'} || $args{'ip6tables'} || ''
l!l
00
$args{"iptables"} or $args{"ip6tables"}
l!l&&r!l&&!r
000
$args{'firewall-cmd'} || ''
l!l
00
$args{'fwd_args'} || '--direct --passthrough ipv4'
l!l
00
$args{'use_ipv6'} || 0
l!l
00
$args{'iptout'} || '/tmp/ipt.out' . $$
l!l
00
$args{'ipterr'} || '/tmp/ipt.err' . $$
l!l
00
$args{'ipt_alarm'} || 30
l!l
00
$args{'debug'} || 0
l!l
00
$args{'verbose'} || 0
l!l
00
$args{'ipt_rules_file'} || ''
l!l
00
$args{'ipt_exec_style'} || 'waitpid'
l!l
00
$args{'ipt_exec_sleep'} || 0
l!l
00
$args{'sigchld_handler'} || \&IPTables::Parse::REAPER
l!l
00
$args{'skip_ipt_exec_check'} || 0
l!l
00
MC/DC0%0/2
$args{'iptables'} || $args{'ip6tables'} || ''
$args{"iptables"} $args{"ip6tables"}
MC/DC0%0/1
$args{'firewall-cmd'} || ''
$args{'firewall-cmd'}
MC/DC0%0/1
$args{'fwd_args'} || '--direct --passthrough ipv4'
$args{'fwd_args'}
MC/DC0%0/1
$args{'use_ipv6'} || 0
$args{'use_ipv6'}
MC/DC0%0/1
$args{'iptout'} || '/tmp/ipt.out' . $$
$args{'iptout'}
MC/DC0%0/1
$args{'ipterr'} || '/tmp/ipt.err' . $$
$args{'ipterr'}
MC/DC0%0/1
$args{'ipt_alarm'} || 30
$args{'ipt_alarm'}
MC/DC0%0/1
$args{'debug'} || 0
$args{'debug'}
MC/DC0%0/1
$args{'verbose'} || 0
$args{'verbose'}
MC/DC0%0/1
$args{'ipt_rules_file'} || ''
$args{'ipt_rules_file'}
MC/DC0%0/1
$args{'ipt_exec_style'} || 'waitpid'
$args{'ipt_exec_style'}
MC/DC0%0/1
$args{'ipt_exec_sleep'} || 0
$args{'ipt_exec_sleep'}
MC/DC0%0/1
$args{'sigchld_handler'} || \&IPTables::Parse::REAPER
$args{'sigchld_handler'}
MC/DC0%0/1
$args{'skip_ipt_exec_check'} || 0
$args{'skip_ipt_exec_check'}
Truth table0%0/3
A or B
A= $args{"iptables"}B= $args{"ip6tables"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{'firewall-cmd'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'fwd_args'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'use_ipv6'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'iptout'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'ipterr'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'ipt_alarm'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'debug'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'verbose'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'ipt_rules_file'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'ipt_exec_style'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'ipt_exec_sleep'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'sigchld_handler'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'skip_ipt_exec_check'}
Aresult
11
00
49 };
50  
51 0 ▶ if ($self->{'_skip_ipt_exec_check'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_skip_ipt_exec_check'}) { } 0 0 0
52 0 ▶ unless ($self->{'_firewall_cmd'} or $self->{'_iptables'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_firewall_cmd"} or $self->{"_iptables"}) 0 0 0
Condition0%0/3
$self->{"_firewall_cmd"} or $self->{"_iptables"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_firewall_cmd"} or $self->{"_iptables"}
$self->{"_firewall_cmd"} $self->{"_iptables"}
Truth table0%0/3
A or B
A= $self->{"_firewall_cmd"}B= $self->{"_iptables"}
ABresult
1X1
011
000
53 ### default
54 0 $self->{'_iptables'} = $ipt_bin;
55 }
56 } else {
57 0 ▶ if ($self->{'_firewall_cmd'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_firewall_cmd'}) { } 0 0 0
elsif ($self->{'_iptables'}) { } 0 0 0
58 croak "[*] $self->{'_firewall_cmd'} incorrect path.\n"
59 0 ▶ unless -e $self->{'_firewall_cmd'};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->{"_firewall_cmd"} 0 0 0
60 croak "[*] $self->{'_firewall_cmd'} not executable.\n"
61 0 ▶ unless -x $self->{'_firewall_cmd'};
Branch: 1 branch
BranchTrueFalseTotal
unless -x $self->{"_firewall_cmd"} 0 0 0
62 } elsif ($self->{'_iptables'}) {
63 croak "[*] $self->{'_iptables'} incorrect path.\n"
64 0 ▶ unless -e $self->{'_iptables'};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->{"_iptables"} 0 0 0
65 croak "[*] $self->{'_iptables'} not executable.\n"
66 0 ▶ unless -x $self->{'_iptables'};
Branch: 1 branch
BranchTrueFalseTotal
unless -x $self->{"_iptables"} 0 0 0
67 } else {
68 ### check for firewall-cmd first since systems with it
69 ### will have iptables installed as well (but firewall-cmd
70 ### should be used instead if it exists)
71 0 ▶ if (-e $fwc_bin and -x $fwc_bin) {
Branch: 3 branches
BranchTrueFalseTotal
if (-e $fwc_bin and -x $fwc_bin) { } 0 0 0
elsif (-e $ipt_bin and -x $ipt_bin) { } 0 0 0
elsif (-e $ipt6_bin and -x $ipt6_bin) { } 0 0 0
Condition0%0/3
-e $fwc_bin and -x $fwc_bin
!ll&&!rl&&r
000
MC/DC0%0/2
-e $fwc_bin and -x $fwc_bin
-e $fwc_bin -x $fwc_bin
Truth table0%0/3
A and B
A= -e $fwc_binB= -x $fwc_bin
ABresult
0X0
100
111
72 0 $self->{'_firewall_cmd'} = $fwc_bin;
73 ▶ } elsif (-e $ipt_bin and -x $ipt_bin) {
Condition0%0/3
-e $ipt_bin and -x $ipt_bin
!ll&&!rl&&r
000
MC/DC0%0/2
-e $ipt_bin and -x $ipt_bin
-e $ipt_bin -x $ipt_bin
Truth table0%0/3
A and B
A= -e $ipt_binB= -x $ipt_bin
ABresult
0X0
100
111
74 0 $self->{'_iptables'} = $ipt_bin;
75 ▶ } elsif (-e $ipt6_bin and -x $ipt6_bin) {
Condition0%0/3
-e $ipt6_bin and -x $ipt6_bin
!ll&&!rl&&r
000
MC/DC0%0/2
-e $ipt6_bin and -x $ipt6_bin
-e $ipt6_bin -x $ipt6_bin
Truth table0%0/3
A and B
A= -e $ipt6_binB= -x $ipt6_bin
ABresult
0X0
100
111
76 0 $self->{'_iptables'} = $ipt6_bin;
77 } else {
78 0 croak "[*] Could not find/execute iptables, " .
79 "specify path via _iptables\n";
80 }
81 }
82 }
83  
84 0 ▶ if ($self->{'_ipv6'} and $self->{'_iptables'} eq $ipt_bin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipv6"} and $self->{"_iptables"} eq $ipt_bin) 0 0 0
Condition0%0/3
$self->{"_ipv6"} and $self->{"_iptables"} eq $ipt_bin
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_ipv6"} and $self->{"_iptables"} eq $ipt_bin
$self->{"_ipv6"} $self->{"_iptables"} eq $ipt_bin
Truth table0%0/3
A and B
A= $self->{"_ipv6"}B= $self->{"_iptables"} eq $ipt_bin
ABresult
0X0
100
111
85 0 ▶ if (-e $ipt6_bin and -x $ipt6_bin) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $ipt6_bin and -x $ipt6_bin) { } 0 0 0
Condition0%0/3
-e $ipt6_bin and -x $ipt6_bin
!ll&&!rl&&r
000
MC/DC0%0/2
-e $ipt6_bin and -x $ipt6_bin
-e $ipt6_bin -x $ipt6_bin
Truth table0%0/3
A and B
A= -e $ipt6_binB= -x $ipt6_bin
ABresult
0X0
100
111
86 0 $self->{'_iptables'} = $ipt6_bin;
87 } else {
88 0 croak "[*] Could not find/execute ip6tables, " .
89 "specify path via _iptables\n";
90 }
91 }
92  
93 ### set the firewall binary name
94 0 $self->{'_ipt_bin_name'} = 'iptables';
95 0 ▶ if ($self->{'_firewall_cmd'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_firewall_cmd'}) { } 0 0 0
96 0 ▶ $self->{'_ipt_bin_name'} = $1 if $self->{'_firewall_cmd'} =~ m|.*/(\S+)|;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_firewall_cmd"} =~ m[.*/(\S+)] 0 0 0
97 } else {
98 0 ▶ $self->{'_ipt_bin_name'} = $1 if $self->{'_iptables'} =~ m|.*/(\S+)|;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_iptables"} =~ m[.*/(\S+)] 0 0 0
99 }
100  
101 ### handle ipv6
102 0 ▶ if ($self->{'_ipv6'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipv6"}) 0 0 0
103 0 ▶ if ($self->{'_firewall_cmd'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_firewall_cmd'}) { } 0 0 0
104 0 ▶ if ($self->{'_fwd_args'} =~ /ipv4/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_fwd_args"} =~ /ipv4/i) 0 0 0
105 0 $self->{'_fwd_args'} = '--direct --passthrough ipv6';
106 }
107 } else {
108 0 ▶ if ($self->{'_ipt_bin_name'} eq 'iptables') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_bin_name"} eq "iptables") 0 0 0
109 0 ▶ unless ($self->{'_skip_ipt_exec_check'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_skip_ipt_exec_check"}) 0 0 0
110 0 croak "[*] use_ipv6 is true, " .
111 "but $self->{'_iptables'} not ip6tables.\n";
112 }
113 }
114 }
115 }
116  
117 0 ▶ $self->{'_ipv6'} = 1 if $self->{'_ipt_bin_name'} eq 'ip6tables';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_ipt_bin_name"} eq "ip6tables" 0 0 0
118 0 ▶ if ($self->{'_firewall_cmd'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_firewall_cmd"}) 0 0 0
119 0 ▶ $self->{'_ipv6'} = 1 if $self->{'_fwd_args'} =~ /ipv6/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_fwd_args"} =~ /ipv6/ 0 0 0
120 }
121  
122 ### set the main command string to allow for iptables execution
123 ### via firewall-cmd if necessary
124 0 $self->{'_cmd'} = $self->{'_iptables'};
125 0 ▶ if ($self->{'_firewall_cmd'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_firewall_cmd"}) 0 0 0
126 0 $self->{'_cmd'} = "$self->{'_firewall_cmd'} $self->{'_fwd_args'}";
127 }
128  
129 0 $self->{'parse_keys'} = &parse_keys();
130  
131 0 bless $self, $class;
132 }
133  
134 sub DESTROY {
135 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=2 SCAR=17.9
136  
137 ### clean up tmp files
138 0 ▶ unless ($self->{'_debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_debug"}) 0 0 0
139 0 unlink $self->{'_iptout'};
140 0 unlink $self->{'_ipterr'};
141 }
142  
143 0 return;
144 }
145  
146 sub parse_keys() {
147 0 ▶ my $self = shift;
Subroutine parse_keys: not called CC=1 SCAR=6.9
Pod undocumented
148  
149 ### only used for IPv4 + NAT
150 0 my $ipv4_re = qr|(?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2}|;
151  
152 0 my %keys = (
153 'regular' => {
154 'packets' => {
155 'regex' => '',
156 'ipt_match' => ''
157 },
158 'bytes' => {
159 'regex' => '',
160 'ipt_match' => ''
161 },
162 'target' => {
163 'regex' => '',
164 'ipt_match' => ''
165 },
166 'protocol' => {
167 'regex' => '',
168 'ipt_match' => '-p'
169 },
170 'proto' => {
171 'regex' => '',
172 'ipt_match' => '-p'
173 },
174 'intf_in' => {
175 'regex' => '',
176 'ipt_match' => '-i'
177 },
178 'intf_out' => {
179 'regex' => '',
180 'ipt_match' => '-o'
181 },
182 'src' => {
183 'regex' => '',
184 'ipt_match' => '-s'
185 },
186 'dst' => {
187 'regex' => '',
188 'ipt_match' => '-d'
189 }
190 },
191 'extended' => {
192 's_port' => {
193 'regex' => qr/\bspts?:(\S+)/,
194 'ipt_match' => '--sport'
195 },
196 'sport' => {
197 'regex' => qr/\bspts?:(\S+)/,
198 'ipt_match' => '--sport'
199 },
200 'd_port' => {
201 'regex' => qr/\bdpts?:(\S+)/,
202 'ipt_match' => '--dport'
203 },
204 'dport' => {
205 'regex' => qr/\bdpts?:(\S+)/,
206 'ipt_match' => '--dport'
207 },
208 'to_ip' => {
209 'regex' => qr/\bto:($ipv4_re):\d+/,
210 'ipt_match' => ''
211 },
212 'to_port' => {
213 'regex' => qr/\bto:$ipv4_re:(\d+)/,
214 'ipt_match' => ''
215 },
216 'mac_source' => {
217 'regex' => qr/\bMAC\s+(\S+)/,
218 'ipt_match' => '-m mac --mac-source'
219 },
220 'state' => {
221 'regex' => qr/\bstate\s+(\S+)/,
222 'ipt_match' => '-m state --state'
223 },
224 'ctstate' => {
225 'regex' => qr/\bctstate\s+(\S+)/,
226 'ipt_match' => '-m conntrack --ctstate'
227 },
228 'comment' => {
229 'regex' => qr|\/\*\s(.*?)\s\*\/|,
230 'ipt_match' => '-m comment --comment',
231 'use_quotes' => 1
232 },
233 'string' => {
234 'regex' => qr|STRING\s+match\s+\"(.*?)\"|,
235 'ipt_match' => '-m string --algo bm --string',
236 'use_quotes' => 1
237 },
238 'length' => {
239 'regex' => qr|\blength\s(\S+)|,
240 'ipt_match' => '-m length --length',
241 },
242 },
243 'rule_num' => '',
244 'raw' => ''
245 );
246  
247 0 return \%keys;
248 }
249  
250 sub list_table_chains() {
251 0 ▶ my $self = shift;
Subroutine list_table_chains: not called CC=9 SCAR=45.0
Pod documented
252 0 ▶ my $table = shift || croak '[*] Specify a table, e.g. "nat"';
Condition0%0/3
shift() || croak('[*] Specify a table, e.g. "nat"')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Specify a table, e.g. "nat"')
shift() croak('[*] Specify a table, e.g. "nat"')
Truth table0%0/3
A || B
A= shift()B= croak('[*] Specify a table, e.g. "nat"')
ABresult
1X1
011
000
253 0 ▶ my $file = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
254  
255 0 my @ipt_lines = ();
256 0 my @chains = ();
257  
258 0 ▶ if ($self->{'_ipt_rules_file'} and not $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_rules_file"} and not $file) 0 0 0
Condition0%0/3
$self->{"_ipt_rules_file"} and not $file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_ipt_rules_file"} and not $file
$self->{"_ipt_rules_file"} not $file
Truth table0%0/3
A and not(B)
A= $self->{"_ipt_rules_file"}B= not $file
ABresult
0X0
100
111
259 0 $file = $self->{'_ipt_rules_file'};
260 }
261  
262 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
263 ### read the iptables rules out of $file instead of executing
264 ### the iptables command.
265 0 ▶ open F, "< $file" or croak "[*] Could not open file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "< $file" or croak("[*] Could not open file ${file}: $!") 0 0 0
266 0 @ipt_lines = <F>;
267 0 close F;
268 } else {
269 0 my ($rv, $out_ar, $err_ar) = $self->exec_iptables(
270 "$self->{'_cmd'} -t $table -v -n -L");
271 0 @ipt_lines = @$out_ar;
272 }
273  
274 0 for (@ipt_lines) {
275 0 ▶ if (/^\s*Chain\s(.*?)\s\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*Chain\s(.*?)\s\(/) 0 0 0
276 0 push @chains, $1;
277 }
278 }
279 0 return \@chains;
280 }
281  
282 sub chain_policy() {
283 0 ▶ my $self = shift;
Subroutine chain_policy: not called CC=10 SCAR=47.0
Pod documented
284 0 ▶ my $table = shift || croak '[*] Specify a table, e.g. "nat"';
Condition0%0/3
shift() || croak('[*] Specify a table, e.g. "nat"')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Specify a table, e.g. "nat"')
shift() croak('[*] Specify a table, e.g. "nat"')
Truth table0%0/3
A || B
A= shift()B= croak('[*] Specify a table, e.g. "nat"')
ABresult
1X1
011
000
285 0 ▶ my $chain = shift || croak '[*] Specify a chain, e.g. "OUTPUT"';
Condition0%0/3
shift() || croak('[*] Specify a chain, e.g. "OUTPUT"')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Specify a chain, e.g. "OUTPUT"')
shift() croak('[*] Specify a chain, e.g. "OUTPUT"')
Truth table0%0/3
A || B
A= shift()B= croak('[*] Specify a chain, e.g. "OUTPUT"')
ABresult
1X1
011
000
286 0 ▶ my $file = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
287  
288 0 my @ipt_lines = ();
289  
290 0 ▶ if ($self->{'_ipt_rules_file'} and not $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_rules_file"} and not $file) 0 0 0
Condition0%0/3
$self->{"_ipt_rules_file"} and not $file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_ipt_rules_file"} and not $file
$self->{"_ipt_rules_file"} not $file
Truth table0%0/3
A and not(B)
A= $self->{"_ipt_rules_file"}B= not $file
ABresult
0X0
100
111
291 0 $file = $self->{'_ipt_rules_file'};
292 }
293  
294 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
295 ### read the iptables rules out of $file instead of executing
296 ### the iptables command.
297 0 ▶ open F, "< $file" or croak "[*] Could not open file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "< $file" or croak("[*] Could not open file ${file}: $!") 0 0 0
298 0 @ipt_lines = <F>;
299 0 close F;
300 } else {
301 0 my ($rv, $out_ar, $err_ar) = $self->exec_iptables(
302 "$self->{'_cmd'} -t $table -v -n -L $chain");
303 0 @ipt_lines = @$out_ar;
304 }
305  
306 0 my $policy = '';
307  
308 0 for my $line (@ipt_lines) {
309 ### Chain INPUT (policy ACCEPT 16 packets, 800 bytes)
310 0 ▶ if ($line =~ /^\s*Chain\s+$chain\s+\(policy\s+(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*Chain\s+$chain\s+\(policy\s+(\w+)/) 0 0 0
311 0 $policy = $1;
312 0 last;
313 }
314 }
315  
316 0 return $policy;
317 }
318  
319 sub chain_action_rules() {
320 0 ▶ return &chain_rules();
Subroutine chain_action_rules: not called CC=1 SCAR=6.9
Pod undocumented
321 }
322  
323 sub chain_rules() {
324 0 ▶ my $self = shift;
Subroutine chain_rules: not called CC=42 SCAR=75.0
Pod documented
325 0 ▶ my $table = shift || croak '[*] Specify a table, e.g. "nat"';
Condition0%0/3
shift() || croak('[*] Specify a table, e.g. "nat"')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Specify a table, e.g. "nat"')
shift() croak('[*] Specify a table, e.g. "nat"')
Truth table0%0/3
A || B
A= shift()B= croak('[*] Specify a table, e.g. "nat"')
ABresult
1X1
011
000
326 0 ▶ my $chain = shift || croak '[*] Specify a chain, e.g. "OUTPUT"';
Condition0%0/3
shift() || croak('[*] Specify a chain, e.g. "OUTPUT"')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Specify a chain, e.g. "OUTPUT"')
shift() croak('[*] Specify a chain, e.g. "OUTPUT"')
Truth table0%0/3
A || B
A= shift()B= croak('[*] Specify a chain, e.g. "OUTPUT"')
ABresult
1X1
011
000
327 0 ▶ my $file = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
328  
329 0 my $found_chain = 0;
330 0 my @ipt_lines = ();
331  
332 ### only used for IPv4 + NAT
333 0 my $ip_re = qr|(?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2}|;
334  
335 ### array of hash refs
336 0 my @chain = ();
337 0 my @global_accept_state = ();
338  
339 0 ▶ if ($self->{'_ipt_rules_file'} and not $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_rules_file"} and not $file) 0 0 0
Condition0%0/3
$self->{"_ipt_rules_file"} and not $file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_ipt_rules_file"} and not $file
$self->{"_ipt_rules_file"} not $file
Truth table0%0/3
A and not(B)
A= $self->{"_ipt_rules_file"}B= not $file
ABresult
0X0
100
111
340 0 $file = $self->{'_ipt_rules_file'};
341 }
342  
343 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
344 ### read the iptables rules out of $file instead of executing
345 ### the iptables command.
346 0 ▶ open F, "< $file" or croak "[*] Could not open file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "< $file" or croak("[*] Could not open file ${file}: $!") 0 0 0
347 0 @ipt_lines = <F>;
348 0 close F;
349 } else {
350 0 my ($rv, $out_ar, $err_ar) = $self->exec_iptables(
351 "$self->{'_cmd'} -t $table -v -n -L $chain --line-numbers");
352 0 @ipt_lines = @$out_ar;
353 }
354  
355 ### determine the output style (e.g. "-nL -v" or just plain "-nL"; if the
356 ### policy data came from a file then -v might not have been used)
357 0 my $ipt_verbose = 0;
358 0 for my $line (@ipt_lines) {
359 0 ▶ if ($line =~ /\spkts\s+bytes\s+target/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\spkts\s+bytes\s+target/) 0 0 0
360 0 $ipt_verbose = 1;
361 0 last;
362 }
363 }
364 0 my $has_line_numbers = 0;
365 0 for my $line (@ipt_lines) {
366 0 ▶ if ($line =~ /^num\s+pkts\s+bytes\s+target/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^num\s+pkts\s+bytes\s+target/) 0 0 0
367 0 $has_line_numbers = 1;
368 0 last;
369 }
370 }
371  
372 0 my $rule_num = 0;
373  
374 0 LINE: for my $line (@ipt_lines) {
375 0 chomp $line;
376  
377 0 ▶ last LINE if ($found_chain and $line =~ /^\s*Chain\s+/);
Branch: 1 branch
BranchTrueFalseTotal
if $found_chain and $line =~ /^\s*Chain\s+/ 0 0 0
Condition0%0/3
$found_chain and $line =~ /^\s*Chain\s+/
!ll&&!rl&&r
000
MC/DC0%0/2
$found_chain and $line =~ /^\s*Chain\s+/
$found_chain $line =~ /^\s*Chain\s+/
Truth table0%0/3
A and B
A= $found_chainB= $line =~ /^\s*Chain\s+/
ABresult
0X0
100
111
378  
379 0 ▶ if ($line =~ /^\s*Chain\s\Q$chain\E\s\(/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*Chain\s\Q$chain\E\s\(/i) 0 0 0
380 0 $found_chain = 1;
381 0 next LINE;
382 }
383 0 ▶ next LINE if $line =~ /\starget\s{2,}prot/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\starget\s{2,}prot/i 0 0 0
384 0 ▶ next LINE unless $found_chain;
Branch: 1 branch
BranchTrueFalseTotal
unless $found_chain 0 0 0
385 0 ▶ next LINE unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
386  
387 ### track the rule number independently of --line-numbers,
388 ### but the values should always match
389 0 $rule_num++;
390  
391 ### initialize hash
392 0 my %rule = (
393 'extended' => '',
394 'raw' => $line,
395 'rule_num' => $rule_num
396 );
397 0 for my $key (keys %{$self->{'parse_keys'}->{'regular'}}) {
398 0 $rule{$key} = '';
399 }
400 0 for my $key (keys %{$self->{'parse_keys'}->{'extended'}}) {
401 0 $rule{$key} = '';
402 }
403  
404 0 my $rule_body = '';
405 0 my $packets = '';
406 0 my $bytes = '';
407 0 my $rnum = '';
408  
409 0 ▶ if ($ipt_verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipt_verbose) { } 0 0 0
410 0 ▶ if ($has_line_numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_line_numbers) { } 0 0 0
411 0 ▶ if ($line =~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(.*)/) 0 0 0
412 0 $rnum = $1;
413 0 $packets = $2;
414 0 $bytes = $3;
415 0 $rule_body = $4;
416 }
417 } else {
418 0 ▶ if ($line =~ /^\s*(\d+)\s+(\d+)\s+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\d+)\s+(\d+)\s+(.*)/) 0 0 0
419 0 $packets = $1;
420 0 $bytes = $2;
421 0 $rule_body = $3;
422 }
423 }
424 } else {
425 0 ▶ if ($has_line_numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_line_numbers) 0 0 0
426 0 ▶ if ($line =~ /^\s*(\d+)\s+(\d+)\s+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\d+)\s+(\d+)\s+(.*)/) 0 0 0
427 0 $rnum = $1;
428 0 $rule_body = $2;
429 }
430 }
431 }
432  
433 0 ▶ if ($rnum and $rnum ne $rule_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rnum and $rnum ne $rule_num) 0 0 0
Condition0%0/3
$rnum and $rnum ne $rule_num
!ll&&!rl&&r
000
MC/DC0%0/2
$rnum and $rnum ne $rule_num
$rnum $rnum ne $rule_num
Truth table0%0/3
A and B
A= $rnumB= $rnum ne $rule_num
ABresult
0X0
100
111
434 0 croak "[*] Rule number mis-match.";
435 }
436  
437 0 ▶ if ($ipt_verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipt_verbose) { } 0 0 0
438  
439 ### iptables:
440 ### 0 0 ACCEPT tcp -- eth1 * 192.168.10.3 0.0.0.0/0 tcp dpt:80
441 ### 0 0 ACCEPT tcp -- eth1 * 192.168.10.15 0.0.0.0/0 tcp dpt:22
442 ### 33 2348 ACCEPT tcp -- eth1 * 192.168.10.2 0.0.0.0/0 tcp dpt:22
443 ### 0 0 ACCEPT tcp -- eth1 * 192.168.10.2 0.0.0.0/0 tcp dpt:80
444 ### 0 0 DNAT tcp -- * * 123.123.123.123 0.0.0.0/0 tcp dpt:55000 to:192.168.12.12:80
445  
446 ### ip6tables:
447 ### 0 0 ACCEPT tcp * * ::/0 fe80::aa:0:1/128 tcp dpt:12345
448 ### 0 0 LOG all * * ::/0 ::/0 LOG flags 0 level 4
449  
450 0 my $match_re = qr/^(\S+)\s+(\S+)\s+\-\-\s+
451 (\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*(.*)/x;
452  
453 0 ▶ if ($self->{'_ipt_bin_name'} eq 'ip6tables'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/) 0 0 0
Condition0%0/6
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
l!l&&r!l&&!r
000
$self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
$self->{"_ipt_bin_name"} eq "ip6tables" $self->{"_ipt_bin_name"} eq "firewall-cmd" $self->{"_fwd_args"} =~ /\sipv6/
Truth table0%0/4
A or B and C
A= $self->{"_ipt_bin_name"} eq "ip6tables"B= $self->{"_ipt_bin_name"} eq "firewall-cmd"C= $self->{"_fwd_args"} =~ /\sipv6/
ABCresult
1XX1
0111
00X0
0100
454 or ($self->{'_ipt_bin_name'} eq 'firewall-cmd'
455 and $self->{'_fwd_args'} =~ /\sipv6/)) {
456 0 $match_re = qr/^(\S+)\s+(\S+)\s+
457 (\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*(.*)/x;
458 }
459  
460 0 ▶ if ($rule_body =~ $match_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_body =~ /$match_re/) 0 0 0
461 0 $rule{'packets'} = $packets;
462 0 $rule{'bytes'} = $bytes;
463 0 $rule{'target'} = $1;
464 0 my $proto = $2;
465 0 ▶ $proto = 'all' if $proto eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq 0 0 0 0
466 0 $rule{'protocol'} = $rule{'proto'} = lc($proto);
467 0 $rule{'intf_in'} = $3;
468 0 $rule{'intf_out'} = $4;
469 0 $rule{'src'} = $5;
470 0 $rule{'dst'} = $6;
471 0 ▶ $rule{'extended'} = $7 || '';
Condition0%0/2
$7 || ''
l!l
00
MC/DC0%0/1
$7 || ''
$7
Truth table0%0/2
A
A= $7
Aresult
11
00
472  
473 0 &parse_rule_extended(\%rule, $self->{'parse_keys'}->{'extended'});
474 }
475 } else {
476  
477 ### iptables:
478 ### ACCEPT tcp -- 164.109.8.0/24 0.0.0.0/0 tcp dpt:22 flags:0x16/0x02
479 ### ACCEPT tcp -- 216.109.125.67 0.0.0.0/0 tcp dpts:7000:7500
480 ### ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpts:7000:7500
481 ### ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:!7000
482 ### ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
483 ### ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:35000 dpt:5000
484 ### ACCEPT tcp -- 10.1.1.1 0.0.0.0/0
485  
486 ### LOG all -- 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 prefix `DROP '
487 ### LOG all -- 127.0.0.2 0.0.0.0/0 LOG flags 0 level 4
488 ### DNAT tcp -- 123.123.123.123 0.0.0.0/0 tcp dpt:55000 to:192.168.12.12:80
489  
490 ### ip6tables:
491 ### ACCEPT tcp ::/0 fe80::aa:0:1/128 tcp dpt:12345
492 ### LOG all ::/0 ::/0 LOG flags 0 level 4
493  
494 0 my $match_re = qr/^(\S+)\s+(\S+)\s+\-\-\s+(\S+)\s+(\S+)\s*(.*)/;
495  
496 0 ▶ if ($self->{'_ipt_bin_name'} eq 'ip6tables'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/) 0 0 0
Condition0%0/6
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
l!l&&r!l&&!r
000
$self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
$self->{"_ipt_bin_name"} eq "ip6tables" $self->{"_ipt_bin_name"} eq "firewall-cmd" $self->{"_fwd_args"} =~ /\sipv6/
Truth table0%0/4
A or B and C
A= $self->{"_ipt_bin_name"} eq "ip6tables"B= $self->{"_ipt_bin_name"} eq "firewall-cmd"C= $self->{"_fwd_args"} =~ /\sipv6/
ABCresult
1XX1
0111
00X0
0100
497 or ($self->{'_ipt_bin_name'} eq 'firewall-cmd'
498 and $self->{'_fwd_args'} =~ /\sipv6/)) {
499 0 $match_re = qr/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*(.*)/;
500 }
501  
502 0 ▶ if ($rule_body =~ $match_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_body =~ /$match_re/) 0 0 0
503 0 $rule{'target'} = $1;
504 0 my $proto = $2;
505 0 ▶ $proto = 'all' if $proto eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq 0 0 0 0
506 0 $rule{'protocol'} = $rule{'proto'} = lc($proto);
507 0 $rule{'src'} = $3;
508 0 $rule{'dst'} = $4;
509 0 ▶ $rule{'extended'} = $5 || '';
Condition0%0/2
$5 || ''
l!l
00
MC/DC0%0/1
$5 || ''
$5
Truth table0%0/2
A
A= $5
Aresult
11
00
510  
511 0 &parse_rule_extended(\%rule, $self->{'parse_keys'}->{'extended'});
512 }
513 }
514 0 push @chain, \%rule;
515 }
516 0 return \@chain;
517 }
518  
519 sub parse_rule_extended() {
520 0 ▶ my ($rule_hr, $ext_keys_hr) = @_;
Subroutine parse_rule_extended: not called CC=8 SCAR=42.8
Pod undocumented
521  
522 0 for my $key (keys %$ext_keys_hr) {
523 0 ▶ if ($rule_hr->{'extended'}
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_hr->{"extended"} =~ /$ext_keys_hr->{$key}{"regex"}/) 0 0 0
524 =~ /$ext_keys_hr->{$key}->{'regex'}/) {
525 0 $rule_hr->{$key} = $1;
526 }
527 }
528  
529 0 ▶ if ($rule_hr->{'protocol'} eq '0') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule_hr->{'protocol'} eq '0') { } 0 0 0
elsif ($rule_hr->{'protocol'} eq 'tcp' or $rule_hr->{'protocol'} eq 'udp') { } 0 0 0
530 0 $rule_hr->{'s_port'} = $rule_hr->{'sport'} = 0;
531 0 $rule_hr->{'d_port'} = $rule_hr->{'dport'} = 0;
532 ▶ } elsif ($rule_hr->{'protocol'} eq 'tcp'
Condition0%0/3
$rule_hr->{"protocol"} eq "tcp" or $rule_hr->{"protocol"} eq "udp"
l!l&&r!l&&!r
000
MC/DC0%0/2
$rule_hr->{"protocol"} eq "tcp" or $rule_hr->{"protocol"} eq "udp"
$rule_hr->{"protocol"} eq "tcp" $rule_hr->{"protocol"} eq "udp"
Truth table0%0/3
A or B
A= $rule_hr->{"protocol"} eq "tcp"B= $rule_hr->{"protocol"} eq "udp"
ABresult
1X1
011
000
533 or $rule_hr->{'protocol'} eq 'udp') {
534 $rule_hr->{'s_port'} = $rule_hr->{'sport'} = 0
535 0 ▶ if $rule_hr->{'s_port'} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rule_hr->{"s_port"} eq "" 0 0 0
536 $rule_hr->{'d_port'} = $rule_hr->{'dport'} = 0
537 0 ▶ if $rule_hr->{'d_port'} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rule_hr->{"d_port"} eq "" 0 0 0
538 }
539  
540 0 return;
541 }
542  
543 sub default_drop() {
544 0 ▶ my $self = shift;
Subroutine default_drop: not called CC=28 SCAR=67.0
Pod documented
545 0 ▶ my $table = shift || croak "[*] Specify a table, e.g. \"nat\"";
Condition0%0/3
shift() || croak('[*] Specify a table, e.g. "nat"')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Specify a table, e.g. "nat"')
shift() croak('[*] Specify a table, e.g. "nat"')
Truth table0%0/3
A || B
A= shift()B= croak('[*] Specify a table, e.g. "nat"')
ABresult
1X1
011
000
546 0 ▶ my $chain = shift || croak "[*] Specify a chain, e.g. \"OUTPUT\"";
Condition0%0/3
shift() || croak('[*] Specify a chain, e.g. "OUTPUT"')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Specify a chain, e.g. "OUTPUT"')
shift() croak('[*] Specify a chain, e.g. "OUTPUT"')
Truth table0%0/3
A || B
A= shift()B= croak('[*] Specify a chain, e.g. "OUTPUT"')
ABresult
1X1
011
000
547 0 ▶ my $file = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
548  
549 0 my @ipt_lines = ();
550  
551 0 ▶ if ($self->{'_ipt_rules_file'} and not $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_rules_file"} and not $file) 0 0 0
Condition0%0/3
$self->{"_ipt_rules_file"} and not $file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_ipt_rules_file"} and not $file
$self->{"_ipt_rules_file"} not $file
Truth table0%0/3
A and not(B)
A= $self->{"_ipt_rules_file"}B= not $file
ABresult
0X0
100
111
552 0 $file = $self->{'_ipt_rules_file'};
553 }
554  
555 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
556 ### read the iptables rules out of $file instead of executing
557 ### the iptables command.
558 0 ▶ open F, "< $file" or croak "[*] Could not open file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "< $file" or croak("[*] Could not open file ${file}: $!") 0 0 0
559 0 @ipt_lines = <F>;
560 0 close F;
561 } else {
562 ### FIXME -v for interfaces?
563 0 my ($rv, $out_ar, $err_ar) = $self->exec_iptables(
564 "$self->{'_cmd'} -t $table -n -L $chain");
565 0 @ipt_lines = @$out_ar;
566 }
567  
568 0 ▶ return "[-] Could not get $self->{'_ipt_bin_name'} output!", 0
Branch: 1 branch
BranchTrueFalseTotal
unless @ipt_lines 0 0 0
569 unless @ipt_lines;
570  
571 0 my %protocols = ();
572 0 my $found_chain = 0;
573 0 my $found_default_drop = 0;
574 0 my $rule_ctr = 1;
575 0 my $prefix;
576 0 my $policy = 'ACCEPT';
577 0 my $any_ip_re = qr/(?:0\.){3}0\x2f0|\x3a{2}\x2f0/;
578  
579 0 LINE: for my $line (@ipt_lines) {
580 0 chomp $line;
581  
582 0 ▶ last if ($found_chain and $line =~ /^\s*Chain\s+/);
Branch: 1 branch
BranchTrueFalseTotal
if $found_chain and $line =~ /^\s*Chain\s+/ 0 0 0
Condition0%0/3
$found_chain and $line =~ /^\s*Chain\s+/
!ll&&!rl&&r
000
MC/DC0%0/2
$found_chain and $line =~ /^\s*Chain\s+/
$found_chain $line =~ /^\s*Chain\s+/
Truth table0%0/3
A and B
A= $found_chainB= $line =~ /^\s*Chain\s+/
ABresult
0X0
100
111
583  
584 ### Chain INPUT (policy DROP)
585 ### Chain FORWARD (policy ACCEPT)
586 0 ▶ if ($line =~ /^\s*Chain\s+$chain\s+\(policy\s+(\w+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*Chain\s+$chain\s+\(policy\s+(\w+)\)/) 0 0 0
587 0 $policy = $1;
588 0 $found_chain = 1;
589 }
590 0 ▶ next LINE if $line =~ /^\s*target\s/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*target\s/i 0 0 0
591 0 ▶ next LINE unless $found_chain;
Branch: 1 branch
BranchTrueFalseTotal
unless $found_chain 0 0 0
592  
593 ### include ULOG target as well
594 0 my $log_re = qr/^\s*U?LOG\s+(\w+)\s+\-\-\s+.*
595 $any_ip_re\s+$any_ip_re\s+(.*)/x;
596 0 my $drop_re = qr/^DROP\s+(\w+)\s+\-\-\s+.*
597 $any_ip_re\s+$any_ip_re\s*$/x;
598  
599 0 ▶ if ($self->{'_ipt_bin_name'} eq 'ip6tables'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /ipv6/) 0 0 0
Condition0%0/6
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /ipv6/
l!l&&r!l&&!r
000
$self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /ipv6/
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /ipv6/
$self->{"_ipt_bin_name"} eq "ip6tables" $self->{"_ipt_bin_name"} eq "firewall-cmd" $self->{"_fwd_args"} =~ /ipv6/
Truth table0%0/4
A or B and C
A= $self->{"_ipt_bin_name"} eq "ip6tables"B= $self->{"_ipt_bin_name"} eq "firewall-cmd"C= $self->{"_fwd_args"} =~ /ipv6/
ABCresult
1XX1
0111
00X0
0100
600 or ($self->{'_ipt_bin_name'} eq 'firewall-cmd'
601 and $self->{'_fwd_args'} =~ /ipv6/)) {
602 0 $log_re = qr/^\s*U?LOG\s+(\w+)\s+
603 $any_ip_re\s+$any_ip_re\s+(.*)/x;
604 0 $drop_re = qr/^DROP\s+(\w+)\s+
605 $any_ip_re\s+$any_ip_re\s*$/x;
606 }
607  
608 ### might as well pick up any default logging rules as well
609 0 ▶ if ($line =~ $log_re) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /$log_re/) { } 0 0 0
elsif ($policy eq 'ACCEPT' and $line =~ /$drop_re/) { } 0 0 0
610 0 my $proto = $1;
611 0 my $p_tmp = $2;
612 0 my $prefix = 'NONE';
613  
614 ### some recent iptables versions return "0" instead of "all"
615 ### for the protocol number
616 0 ▶ $proto = 'all' if $proto eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq 0 0 0 0
617 ### LOG flags 0 level 4 prefix `DROP '
618 0 ▶ if ($p_tmp && $p_tmp =~ m|LOG.*\s+prefix\s+
Branch: 1 branch
BranchTrueFalseTotal
if ($p_tmp and $p_tmp =~ /LOG.*\s+prefix\s+ \`\s*(.+?)\s*\'/x) 0 0 0
Condition0%0/3
$p_tmp and $p_tmp =~ /LOG.*\s+prefix\s+ \`\s*(.+?)\s*\'/x
!ll&&!rl&&r
000
MC/DC0%0/2
$p_tmp and $p_tmp =~ /LOG.*\s+prefix\s+ \`\s*(.+?)\s*\'/x
$p_tmp $p_tmp =~ /LOG.*\s+prefix\s+ \`\s*(.+?)\s*\'/x
Truth table0%0/3
A and B
A= $p_tmpB= $p_tmp =~ /LOG.*\s+prefix\s+ \`\s*(.+?)\s*\'/x
ABresult
0X0
100
111
619 \`\s*(.+?)\s*\'|x) {
620 0 $prefix = $1;
621 }
622 ### $proto may equal "all" here
623 0 $protocols{$proto}{'LOG'}{'prefix'} = $prefix;
624 0 $protocols{$proto}{'LOG'}{'rulenum'} = $rule_ctr;
625 ▶ } elsif ($policy eq 'ACCEPT' and $line =~ $drop_re) {
Condition0%0/3
$policy eq "ACCEPT" and $line =~ /$drop_re/
!ll&&!rl&&r
000
MC/DC0%0/2
$policy eq "ACCEPT" and $line =~ /$drop_re/
$policy eq "ACCEPT" $line =~ /$drop_re/
Truth table0%0/3
A and B
A= $policy eq "ACCEPT"B= $line =~ /$drop_re/
ABresult
0X0
100
111
626 0 my $proto = $1;
627 0 ▶ $proto = 'all' if $proto eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq 0 0 0 0
628 ### DROP all -- 0.0.0.0/0 0.0.0.0/0
629 0 $protocols{$1}{'DROP'} = $rule_ctr;
630 0 $found_default_drop = 1;
631 }
632 0 $rule_ctr++;
633 }
634  
635 ### if the policy in the chain is DROP, then we don't
636 ### necessarily need to find a default DROP rule.
637 0 ▶ if ($policy eq 'DROP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy eq "DROP") 0 0 0
638 0 $protocols{'all'}{'DROP'} = 0;
639 0 $found_default_drop = 1;
640 }
641  
642 0 ▶ return "[-] There are no default drop rules in the " .
Branch: 1 branch
BranchTrueFalseTotal
unless %protocols and $found_default_drop 0 0 0
Condition0%0/3
%protocols and $found_default_drop
!ll&&!rl&&r
000
MC/DC0%0/2
%protocols and $found_default_drop
%protocols $found_default_drop
Truth table0%0/3
A and B
A= %protocolsB= $found_default_drop
ABresult
0X0
100
111
643 "$self->{'_ipt_bin_name'} policy!", 0
644 unless %protocols and $found_default_drop;
645  
646 0 return \%protocols, 1;
647 }
648  
649 sub default_log() {
650 0 ▶ my $self = shift;
Subroutine default_log: not called CC=34 SCAR=70.8
Pod documented
651 0 ▶ my $table = shift || croak "[*] Specify a table, e.g. \"nat\"";
Condition0%0/3
shift() || croak('[*] Specify a table, e.g. "nat"')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Specify a table, e.g. "nat"')
shift() croak('[*] Specify a table, e.g. "nat"')
Truth table0%0/3
A || B
A= shift()B= croak('[*] Specify a table, e.g. "nat"')
ABresult
1X1
011
000
652 0 ▶ my $chain = shift || croak "[*] Specify a chain, e.g. \"OUTPUT\"";
Condition0%0/3
shift() || croak('[*] Specify a chain, e.g. "OUTPUT"')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Specify a chain, e.g. "OUTPUT"')
shift() croak('[*] Specify a chain, e.g. "OUTPUT"')
Truth table0%0/3
A || B
A= shift()B= croak('[*] Specify a chain, e.g. "OUTPUT"')
ABresult
1X1
011
000
653 0 ▶ my $file = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
654  
655 0 my $any_ip_re = qr/(?:0\.){3}0\x2f0|\x3a{2}\x2f0/;
656 0 my @ipt_lines = ();
657 0 my %log_chains = ();
658 0 my %log_rules = ();
659  
660 0 ▶ if ($self->{'_ipt_rules_file'} and not $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_rules_file"} and not $file) 0 0 0
Condition0%0/3
$self->{"_ipt_rules_file"} and not $file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_ipt_rules_file"} and not $file
$self->{"_ipt_rules_file"} not $file
Truth table0%0/3
A and not(B)
A= $self->{"_ipt_rules_file"}B= not $file
ABresult
0X0
100
111
661 0 $file = $self->{'_ipt_rules_file'};
662 }
663  
664 ### note that we are not restricting the view to the current chain
665 ### with the iptables -nL output; we are going to parse the given
666 ### chain and all chains to which packets are jumped from the given
667 ### chain.
668 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
669 ### read the iptables rules out of $file instead of executing
670 ### the iptables command.
671 0 ▶ open F, "< $file" or croak "[*] Could not open file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "< $file" or croak("[*] Could not open file ${file}: $!") 0 0 0
672 0 @ipt_lines = <F>;
673 0 close F;
674 } else {
675 0 my ($rv, $out_ar, $err_ar) = $self->exec_iptables(
676 "$self->{'_cmd'} -t $table -n -L $chain");
677 0 @ipt_lines = @$out_ar;
678 }
679  
680 ### determine the output style (e.g. "-nL -v" or just plain "-nL"; if the
681 ### policy data came from a file then -v might not have been used)
682 0 my $ipt_verbose = 0;
683 0 for my $line (@ipt_lines) {
684 0 ▶ if ($line =~ /^\s*pkts\s+bytes\s+target/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*pkts\s+bytes\s+target/) 0 0 0
685 0 $ipt_verbose = 1;
686 0 last;
687 }
688 }
689  
690 0 ▶ return "[-] Could not get $self->{'_ipt_bin_name'} output!", 0
Branch: 1 branch
BranchTrueFalseTotal
unless @ipt_lines 0 0 0
691 unless @ipt_lines;
692  
693 ### first get all logging rules and associated chains
694 0 my $log_chain;
695  
696 0 for my $line (@ipt_lines) {
697 0 chomp $line;
698  
699 ### Chain INPUT (policy DROP)
700 ### Chain fwsnort_INPUT_eth1 (1 references)
701 0 ▶ if ($line =~ /^\s*Chain\s+(.*?)\s+\(/ and
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*Chain\s+(.*?)\s+\(/ and not $line =~ /0\s+references/) 0 0 0
Condition0%0/3
$line =~ /^\s*Chain\s+(.*?)\s+\(/ and not $line =~ /0\s+references/
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /^\s*Chain\s+(.*?)\s+\(/ and not $line =~ /0\s+references/
$line =~ /^\s*Chain\s+(.*?)\s+\(/ not $line =~ /0\s+references/
Truth table0%0/3
A and not(B)
A= $line =~ /^\s*Chain\s+(.*?)\s+\(/B= not $line =~ /0\s+references/
ABresult
0X0
100
111
702 $line !~ /0\s+references/) {
703 0 $log_chain = $1;
704 }
705 0 ▶ $log_chain = '' unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 0 0
706 0 ▶ next unless $log_chain;
Branch: 1 branch
BranchTrueFalseTotal
unless $log_chain 0 0 0
707  
708 0 my $proto = '';
709 0 my $found = 0;
710 0 ▶ if ($ipt_verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipt_verbose) { } 0 0 0
711 0 ▶ if ($self->{'_ipt_bin_name'} eq 'ip6tables'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_ipt_bin_name'} eq 'ip6tables' or $self->{'_ipt_bin_name'} eq 'firewall-cmd' and $self->{'_fwd_args'} =~ /\sipv6/) { } 0 0 0
Condition0%0/6
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
l!l&&r!l&&!r
000
$self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
$self->{"_ipt_bin_name"} eq "ip6tables" $self->{"_ipt_bin_name"} eq "firewall-cmd" $self->{"_fwd_args"} =~ /\sipv6/
Truth table0%0/4
A or B and C
A= $self->{"_ipt_bin_name"} eq "ip6tables"B= $self->{"_ipt_bin_name"} eq "firewall-cmd"C= $self->{"_fwd_args"} =~ /\sipv6/
ABCresult
1XX1
0111
00X0
0100
712 or ($self->{'_ipt_bin_name'} eq 'firewall-cmd'
713 and $self->{'_fwd_args'} =~ /\sipv6/)) {
714 0 ▶ if ($line =~ m|^\s*\d+\s+\d+\s*U?LOG\s+(\w+)\s+
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\d+\s+\d+\s*U?LOG\s+(\w+)\s+ \S+\s+\S+\s+$any_ip_re \s+$any_ip_re\s+.*U?LOG/x) 0 0 0
715 \S+\s+\S+\s+$any_ip_re
716 \s+$any_ip_re\s+.*U?LOG|x) {
717 0 $proto = $1;
718 0 $found = 1;
719 }
720 } else {
721 0 ▶ if ($line =~ m|^\s*\d+\s+\d+\s*U?LOG\s+(\w+)\s+\-\-\s+
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\d+\s+\d+\s*U?LOG\s+(\w+)\s+\-\-\s+ \S+\s+\S+\s+$any_ip_re \s+$any_ip_re\s+.*U?LOG/x) 0 0 0
722 \S+\s+\S+\s+$any_ip_re
723 \s+$any_ip_re\s+.*U?LOG|x) {
724 0 $proto = $1;
725 0 $found = 1;
726 }
727 }
728 } else {
729 0 ▶ if ($self->{'_ipt_bin_name'} eq 'ip6tables'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_ipt_bin_name'} eq 'ip6tables' or $self->{'_ipt_bin_name'} eq 'firewall-cmd' and $self->{'_fwd_args'} =~ /\sipv6/) { } 0 0 0
Condition0%0/6
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
l!l&&r!l&&!r
000
$self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"_ipt_bin_name"} eq "ip6tables" or $self->{"_ipt_bin_name"} eq "firewall-cmd" and $self->{"_fwd_args"} =~ /\sipv6/
$self->{"_ipt_bin_name"} eq "ip6tables" $self->{"_ipt_bin_name"} eq "firewall-cmd" $self->{"_fwd_args"} =~ /\sipv6/
Truth table0%0/4
A or B and C
A= $self->{"_ipt_bin_name"} eq "ip6tables"B= $self->{"_ipt_bin_name"} eq "firewall-cmd"C= $self->{"_fwd_args"} =~ /\sipv6/
ABCresult
1XX1
0111
00X0
0100
730 or ($self->{'_ipt_bin_name'} eq 'firewall-cmd'
731 and $self->{'_fwd_args'} =~ /\sipv6/)) {
732 0 ▶ if ($line =~ m|^\s*U?LOG\s+(\w+)\s+$any_ip_re
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*U?LOG\s+(\w+)\s+$any_ip_re \s+$any_ip_re\s+.*U?LOG/x) 0 0 0
733 \s+$any_ip_re\s+.*U?LOG|x) {
734 0 $proto = $1;
735 0 $found = 1;
736 }
737 } else {
738 0 ▶ if ($line =~ m|^\s*U?LOG\s+(\w+)\s+\-\-\s+$any_ip_re
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*U?LOG\s+(\w+)\s+\-\-\s+$any_ip_re \s+$any_ip_re\s+.*U?LOG/x) 0 0 0
739 \s+$any_ip_re\s+.*U?LOG|x) {
740 0 $proto = $1;
741 0 $found = 1;
742 }
743 }
744 }
745  
746 0 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 0 0 0
747 0 ▶ $proto = 'all' if $proto eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq 0 0 0 0
748 ### the above regex allows the limit target to be used
749 0 $log_chains{$log_chain}{$proto} = ''; ### protocol
750 0 ▶ $log_rules{$proto} = '' if $log_chain eq $chain;
Branch: 1 branch
BranchTrueFalseTotal
if $log_chain eq $chain 0 0 0
751 }
752 }
753  
754 0 ▶ return "[-] There are no default logging rules " .
Branch: 1 branch
BranchTrueFalseTotal
unless %log_chains 0 0 0
755 "in the $self->{'_ipt_bin_name'} policy!", 0 unless %log_chains;
756  
757 0 my %sub_chains = ();
758  
759 ### get all sub-chains of the main chain we passed into default_log()
760 0 &sub_chains($chain, \%sub_chains, \@ipt_lines);
761  
762 ### see which (if any) logging rules can be mapped back to the
763 ### main chain we passed in.
764 0 for my $log_chain (keys %log_chains) {
765 0 ▶ if (defined $sub_chains{$log_chain}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub_chains{$log_chain}) 0 0 0
766 ### the logging rule is in the main chain (e.g. INPUT)
767 0 for my $proto (keys %{$log_chains{$log_chain}}) {
768 0 $log_rules{$proto} = '';
769 }
770 }
771 }
772  
773 0 return \%log_rules, 1;
774 }
775  
776 sub sub_chains() {
777 0 ▶ my ($start_chain, $chains_hr, $ipt_lines_ar) = @_;
Subroutine sub_chains: not called CC=20 SCAR=60.4
Pod undocumented
778 0 my $found = 0;
779 0 for my $line (@$ipt_lines_ar) {
780 0 chomp $line;
781 ### Chain INPUT (policy DROP)
782 ### Chain fwsnort_INPUT_eth1 (1 references)
783 0 ▶ if ($line =~ /^\s*Chain\s+\Q$start_chain\E\s+\(/ and
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*Chain\s+\Q$start_chain\E\s+\(/ and not $line =~ /0\s+references/) 0 0 0
Condition0%0/3
$line =~ /^\s*Chain\s+\Q$start_chain\E\s+\(/ and not $line =~ /0\s+references/
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /^\s*Chain\s+\Q$start_chain\E\s+\(/ and not $line =~ /0\s+references/
$line =~ /^\s*Chain\s+\Q$start_chain\E\s+\(/ not $line =~ /0\s+references/
Truth table0%0/3
A and not(B)
A= $line =~ /^\s*Chain\s+\Q$start_chain\E\s+\(/B= not $line =~ /0\s+references/
ABresult
0X0
100
111
784 $line !~ /0\s+references/) {
785 0 $found = 1;
786 0 next;
787 }
788 0 ▶ next unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 0 0
789 0 ▶ if ($found and $line =~ /^\s*Chain\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found and $line =~ /^\s*Chain\s/) 0 0 0
Condition0%0/3
$found and $line =~ /^\s*Chain\s/
!ll&&!rl&&r
000
MC/DC0%0/2
$found and $line =~ /^\s*Chain\s/
$found $line =~ /^\s*Chain\s/
Truth table0%0/3
A and B
A= $foundB= $line =~ /^\s*Chain\s/
ABresult
0X0
100
111
790 0 last;
791 }
792 0 ▶ if ($line =~ m|^\s*(\S+)\s+\S+\s+|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\S+)\s+\S+\s+/) 0 0 0
793 0 my $new_chain = $1;
794 0 ▶ if ($new_chain ne 'LOG'
Branch: 1 branch
BranchTrueFalseTotal
if ($new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN" and $new_chain ne "QUEUE" and $new_chain ne "SNAT" and $new_chain ne "DNAT" and $new_chain ne "MASQUERADE" and $new_chain ne "pkts" and $new_chain ne "Chain" and $new_chain ne "target") 0 0 0
Condition0%0/33
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN" and $new_chain ne "QUEUE" and $new_chain ne "SNAT" and $new_chain ne "DNAT" and $new_chain ne "MASQUERADE" and $new_chain ne "pkts" and $new_chain ne "Chain" and $new_chain ne "target"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN" and $new_chain ne "QUEUE" and $new_chain ne "SNAT" and $new_chain ne "DNAT" and $new_chain ne "MASQUERADE" and $new_chain ne "pkts" and $new_chain ne "Chain"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN" and $new_chain ne "QUEUE" and $new_chain ne "SNAT" and $new_chain ne "DNAT" and $new_chain ne "MASQUERADE" and $new_chain ne "pkts"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN" and $new_chain ne "QUEUE" and $new_chain ne "SNAT" and $new_chain ne "DNAT" and $new_chain ne "MASQUERADE"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN" and $new_chain ne "QUEUE" and $new_chain ne "SNAT" and $new_chain ne "DNAT"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN" and $new_chain ne "QUEUE" and $new_chain ne "SNAT"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN" and $new_chain ne "QUEUE"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT"
!ll&&!rl&&r
000
$new_chain ne "LOG" and $new_chain ne "DROP"
!ll&&!rl&&r
000
MC/DC0%0/12
$new_chain ne "LOG" and $new_chain ne "DROP" and $new_chain ne "REJECT" and $new_chain ne "ACCEPT" and $new_chain ne "RETURN" and $new_chain ne "QUEUE" and $new_chain ne "SNAT" and $new_chain ne "DNAT" and $new_chain ne "MASQUERADE" and $new_chain ne "pkts" and $new_chain ne "Chain" and $new_chain ne "target"
$new_chain ne "LOG" $new_chain ne "DROP" $new_chain ne "REJECT" $new_chain ne "ACCEPT" $new_chain ne "RETURN" $new_chain ne "QUEUE" $new_chain ne "SNAT" $new_chain ne "DNAT" $new_chain ne "MASQUERADE" $new_chain ne "pkts" $new_chain ne "Chain" $new_chain ne "target"
Truth table0%0/13
A and B and C and D and E and F and G and H and I and J and K and L
A= $new_chain ne "LOG"B= $new_chain ne "DROP"C= $new_chain ne "REJECT"D= $new_chain ne "ACCEPT"E= $new_chain ne "RETURN"F= $new_chain ne "QUEUE"G= $new_chain ne "SNAT"H= $new_chain ne "DNAT"I= $new_chain ne "MASQUERADE"J= $new_chain ne "pkts"K= $new_chain ne "Chain"L= $new_chain ne "target"
ABCDEFGHIJKLresult
0XXXXXXXXXXX0
10XXXXXXXXXX0
110XXXXXXXXX0
1110XXXXXXXX0
11110XXXXXXX0
111110XXXXXX0
1111110XXXXX0
11111110XXXX0
111111110XXX0
1111111110XX0
11111111110X0
1111111111100
1111111111111
795 and $new_chain ne 'DROP'
796 and $new_chain ne 'REJECT'
797 and $new_chain ne 'ACCEPT'
798 and $new_chain ne 'RETURN'
799 and $new_chain ne 'QUEUE'
800 and $new_chain ne 'SNAT'
801 and $new_chain ne 'DNAT'
802 and $new_chain ne 'MASQUERADE'
803 and $new_chain ne 'pkts'
804 and $new_chain ne 'Chain'
805 and $new_chain ne 'target') {
806 0 $chains_hr->{$new_chain} = '';
807 0 &sub_chains($new_chain, $chains_hr, $ipt_lines_ar);
808 }
809 }
810 }
811 0 return;
812 }
813  
814 sub exec_iptables() {
815 0 ▶ my $self = shift;
Subroutine exec_iptables: not called CC=44 SCAR=75.9
Pod undocumented
816 0 ▶ my $cmd = shift || croak "[*] Must specify an " .
Condition0%0/3
shift() || croak('[*] Must specify an ' . "$self->{'_ipt_bin_name'} command to run.")
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('[*] Must specify an ' . "$self->{'_ipt_bin_name'} command to run.")
shift() croak('[*] Must specify an ' . "$self->{'_ipt_bin_name'} command to run.")
Truth table0%0/3
A || B
A= shift()B= croak('[*] Must specify an ' . "$self->{'_ipt_bin_name'} command to run.")
ABresult
1X1
011
000
817 "$self->{'_ipt_bin_name'} command to run.";
818 0 my $iptout = $self->{'_iptout'};
819 0 my $ipterr = $self->{'_ipterr'};
820 0 my $debug = $self->{'_debug'};
821 0 my $ipt_alarm = $self->{'_ipt_alarm'};
822 0 my $verbose = $self->{'_verbose'};
823 0 my $ipt_exec_style = $self->{'_ipt_exec_style'};
824 0 my $ipt_exec_sleep = $self->{'_ipt_exec_sleep'};
825 0 my $sigchld_handler = $self->{'_sigchld_handler'};
826  
827 0 ▶ croak "[*] $cmd does not look like an $self->{'_ipt_bin_name'} command."
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd =~ /^\s*iptables/ or $cmd =~ m[^\S+/iptables] or $cmd =~ /^\s*ip6tables/ or $cmd =~ m[^\S+/ip6tables] or $cmd =~ /^\s*firewall-cmd/ or $cmd =~ m[^\S+/firewall-cmd] 0 0 0
Condition0%0/15
$cmd =~ /^\s*iptables/ or $cmd =~ m[^\S+/iptables] or $cmd =~ /^\s*ip6tables/ or $cmd =~ m[^\S+/ip6tables] or $cmd =~ /^\s*firewall-cmd/ or $cmd =~ m[^\S+/firewall-cmd]
l!l&&r!l&&!r
000
$cmd =~ /^\s*iptables/ or $cmd =~ m[^\S+/iptables] or $cmd =~ /^\s*ip6tables/ or $cmd =~ m[^\S+/ip6tables] or $cmd =~ /^\s*firewall-cmd/
l!l&&r!l&&!r
000
$cmd =~ /^\s*iptables/ or $cmd =~ m[^\S+/iptables] or $cmd =~ /^\s*ip6tables/ or $cmd =~ m[^\S+/ip6tables]
l!l&&r!l&&!r
000
$cmd =~ /^\s*iptables/ or $cmd =~ m[^\S+/iptables] or $cmd =~ /^\s*ip6tables/
l!l&&r!l&&!r
000
$cmd =~ /^\s*iptables/ or $cmd =~ m[^\S+/iptables]
l!l&&r!l&&!r
000
MC/DC0%0/6
$cmd =~ /^\s*iptables/ or $cmd =~ m[^\S+/iptables] or $cmd =~ /^\s*ip6tables/ or $cmd =~ m[^\S+/ip6tables] or $cmd =~ /^\s*firewall-cmd/ or $cmd =~ m[^\S+/firewall-cmd]
$cmd =~ /^\s*iptables/ $cmd =~ m[^\S+/iptables] $cmd =~ /^\s*ip6tables/ $cmd =~ m[^\S+/ip6tables] $cmd =~ /^\s*firewall-cmd/ $cmd =~ m[^\S+/firewall-cmd]
Truth table0%0/7
A or B or C or D or E or F
A= $cmd =~ /^\s*iptables/B= $cmd =~ m[^\S+/iptables]C= $cmd =~ /^\s*ip6tables/D= $cmd =~ m[^\S+/ip6tables]E= $cmd =~ /^\s*firewall-cmd/F= $cmd =~ m[^\S+/firewall-cmd]
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
828 unless $cmd =~ m|^\s*iptables| or $cmd =~ m|^\S+/iptables|
829 or $cmd =~ m|^\s*ip6tables| or $cmd =~ m|^\S+/ip6tables|
830 or $cmd =~ m|^\s*firewall-cmd| or $cmd =~ m|^\S+/firewall-cmd|;
831  
832 ### sanitize $cmd - this is not bullet proof, but better than
833 ### nothing (especially for strange iptables chain names). Further,
834 ### quotemeta() is too aggressive since things like IPv6 addresses
835 ### contain ":" chars, etc.
836 0 $cmd =~ s/([;<>\$\|`\@&\(\)\[\]\{\}])/\\$1/g;
837  
838 0 my $rv = 1;
839 0 my @stdout = ();
840 0 my @stderr = ();
841  
842 0 my $fh = *STDERR;
843 0 ▶ $fh = *STDOUT if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
844  
845 0 ▶ if ($debug or $verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug or $verbose) 0 0 0
Condition0%0/3
$debug or $verbose
l!l&&r!l&&!r
000
MC/DC0%0/2
$debug or $verbose
$debug $verbose
Truth table0%0/3
A or B
A= $debugB= $verbose
ABresult
1X1
011
000
846 0 print $fh localtime() . " [+] IPTables::Parse::",
847 "exec_iptables(${ipt_exec_style}()) $cmd\n";
848 0 ▶ if ($ipt_exec_sleep > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipt_exec_sleep > 0) 0 0 0
849 0 print $fh localtime() . " [+] IPTables::Parse::",
850 "exec_iptables() sleep seconds: $ipt_exec_sleep\n";
851 }
852 }
853  
854 0 ▶ if ($ipt_exec_sleep > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipt_exec_sleep > 0) 0 0 0
855 0 ▶ if ($debug or $verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug or $verbose) 0 0 0
Condition0%0/3
$debug or $verbose
l!l&&r!l&&!r
000
MC/DC0%0/2
$debug or $verbose
$debug $verbose
Truth table0%0/3
A or B
A= $debugB= $verbose
ABresult
1X1
011
000
856 0 print $fh localtime() . " [+] IPTables::Parse: ",
857 "sleeping for $ipt_exec_sleep seconds before ",
858 "executing $self->{'_ipt_bin_name'} command.\n";
859 }
860 0 sleep $ipt_exec_sleep;
861 }
862  
863 0 ▶ if ($ipt_exec_style eq 'system') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ipt_exec_style eq 'system') { } 0 0 0
elsif ($ipt_exec_style eq 'popen') { } 0 0 0
864 0 system qq{$cmd > $iptout 2> $ipterr};
865 } elsif ($ipt_exec_style eq 'popen') {
866 0 ▶ open CMD, "$cmd 2> $ipterr |" or croak "[*] Could not execute $cmd: $!";
Branch: 1 branch
BranchTrueFalseTotal
open CMD, "$cmd 2> $ipterr |" or croak("[*] Could not execute ${cmd}: $!") 0 0 0
867 0 @stdout = <CMD>;
868 0 close CMD;
869 0 ▶ open F, "> $iptout" or croak "[*] Could not open $iptout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "> $iptout" or croak("[*] Could not open ${iptout}: $!") 0 0 0
870 0 print F for @stdout;
871 0 close F;
872 } else {
873 0 my $ipt_pid;
874  
875 0 ▶ if ($debug or $verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug or $verbose) 0 0 0
Condition0%0/3
$debug or $verbose
l!l&&r!l&&!r
000
MC/DC0%0/2
$debug or $verbose
$debug $verbose
Truth table0%0/3
A or B
A= $debugB= $verbose
ABresult
1X1
011
000
876 0 print $fh localtime() . " [+] IPTables::Parse: " .
877 "Setting SIGCHLD handler to: " . $sigchld_handler . "\n";
878 }
879  
880 0 local $SIG{'CHLD'} = $sigchld_handler;
881 0 ▶ if ($ipt_pid = fork()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipt_pid = fork) { } 0 0 0
882 0 eval {
883 ### iptables should never take longer than 30 seconds to execute,
884 ### unless there is some absolutely enormous policy or the kernel
885 ### is exceedingly busy
886 0 ▶ local $SIG{'ALRM'} = sub {die "[*] $self->{'_ipt_bin_name'} " .
Subroutine __ANON__: not called CC=1 SCAR=6.9
887 0 "command timeout.\n"};
888 0 alarm $ipt_alarm;
889 0 waitpid($ipt_pid, 0);
890 0 alarm 0;
891 };
892 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
893 0 ▶ kill 9, $ipt_pid unless kill 15, $ipt_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless kill 15, $ipt_pid 0 0 0
894 }
895 } else {
896 0 ▶ croak "[*] Could not fork $self->{'_ipt_bin_name'}: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ipt_pid 0 0 0
897 unless defined $ipt_pid;
898  
899 ### exec the iptables command and preserve stdout and stderr
900 0 exec qq{$cmd > $iptout 2> $ipterr};
901 }
902 }
903  
904 0 ▶ if (-e $iptout) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $iptout) 0 0 0
905 0 ▶ open F, "< $iptout" or croak "[*] Could not open $iptout";
Branch: 1 branch
BranchTrueFalseTotal
open F, "< $iptout" or croak("[*] Could not open $iptout") 0 0 0
906 0 @stdout = <F>;
907 0 close F;
908 }
909 0 ▶ if (-e $ipterr) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $ipterr) 0 0 0
910 0 ▶ open F, "< $ipterr" or croak "[*] Could not open $ipterr";
Branch: 1 branch
BranchTrueFalseTotal
open F, "< $ipterr" or croak("[*] Could not open $ipterr") 0 0 0
911 0 @stderr = <F>;
912 0 close F;
913  
914 0 ▶ $rv = 0 if @stderr;
Branch: 1 branch
BranchTrueFalseTotal
if @stderr 0 0 0
915 }
916  
917 0 ▶ if (@stdout) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stdout) 0 0 0
918 0 ▶ if ($stdout[$#stdout] =~ /^success/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout[$#stdout] =~ /^success/) 0 0 0
919 0 pop @stdout;
920 }
921 0 ▶ if ($self->{'_ipt_bin_name'} eq 'firewall-cmd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ipt_bin_name"} eq "firewall-cmd") 0 0 0
922 0 for (@stdout) {
923 0 ▶ if (/COMMAND_FAILED/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/COMMAND_FAILED/) 0 0 0
924 0 $rv = 0;
925 0 last;
926 }
927 }
928 }
929 }
930  
931 0 ▶ if ($debug or $verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug or $verbose) 0 0 0
Condition0%0/3
$debug or $verbose
l!l&&r!l&&!r
000
MC/DC0%0/2
$debug or $verbose
$debug $verbose
Truth table0%0/3
A or B
A= $debugB= $verbose
ABresult
1X1
011
000
932 0 print $fh localtime() . " $self->{'_ipt_bin_name'} " .
933 "command stdout:\n";
934 0 for my $line (@stdout) {
935 0 ▶ if ($line =~ /\n$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\n$/) { } 0 0 0
936 0 print $fh $line;
937 } else {
938 0 print $fh $line, "\n";
939 }
940 }
941 0 print $fh localtime() . " $self->{'_ipt_bin_name'} " .
942 "command stderr:\n";
943 0 for my $line (@stderr) {
944 0 ▶ if ($line =~ /\n$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\n$/) { } 0 0 0
945 0 print $fh $line;
946 } else {
947 0 print $fh $line, "\n";
948 }
949 }
950 }
951  
952 0 ▶ if ($debug or $verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug or $verbose) 0 0 0
Condition0%0/3
$debug or $verbose
l!l&&r!l&&!r
000
MC/DC0%0/2
$debug or $verbose
$debug $verbose
Truth table0%0/3
A or B
A= $debugB= $verbose
ABresult
1X1
011
000
953 0 print $fh localtime() . " Return value: $rv\n";
954 }
955  
956 0 return $rv, \@stdout, \@stderr;
957 }
958  
959 sub REAPER {
960 0 ▶ my $stiff;
Subroutine REAPER: not called CC=2 SCAR=17.9
Pod undocumented
961 0 ▶ while(($stiff = waitpid(-1,WNOHANG))>0){
Branch: 1 branch
BranchTrueFalseTotal
if (($stiff = waitpid(-1, 1)) > 0) 0 0 0
962 # do something with $stiff if you want
963 }
964 0 local $SIG{'CHLD'} = \&REAPER;
965 0 return;
966 }
967  
968 1 1;
969 __END__
↑ Summary