lib/Net/SinFP3/Input/SynScan.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 267 CC 28 SCAR 57.5
CC
28
Cov
0%
init
552.0
_addToResult
20.0
new
12.0
CRAP 313.0
1 #
2 # $Id: SynScan.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $
3 #
4 package Net::SinFP3::Input::SynScan;
5 use strict;
6 use warnings;
7  
8 use base qw(Net::SinFP3::Input);
9 our @AS = qw(
10 fingerprint
11 );
12 __PACKAGE__->cgBuildIndices;
13 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
14  
15 use Net::SinFP3::Next::IpPort;
16  
17 use POSIX qw(:sys_wait_h ceil);
18 use Net::Frame::Dump::Online2;
19 use Net::Frame::Simple;
20 use Net::Write::Fast;
21 use Time::Interval;
22  
23 sub give {
24 return [
25 'Net::SinFP3::Next::IpPort',
26 'Net::SinFP3::Next::Frame',
27 ];
28 }
29  
30 sub new {
31 my $self = shift->SUPER::new(
32 fingerprint => 0,
33 nextList => [],
34 @_,
35 );
36  
37 my $global = $self->global;
38 my $log = $global->log;
39  
40 if (! defined($global->target)) {
41 $log->fatal("You must provide a `target' attribute in Global object");
42 }
43  
44 if (! defined($global->port)) {
45 $log->fatal("You must provide a `port' attribute in Global object");
46 }
47  
48 return $self;
49 }
50  
51 sub _addToResult {
52 my $self = shift;
53 my ($f) = @_;
54  
55 my $global = $self->global;
56  
57 if ($self->fingerprint) {
58 return Net::SinFP3::Next::Frame->new(
59 global => $global,
60 frame => $f,
61 );
62 }
63 else {
64 my $eth = $f->ref->{ETH};
65 my $ip = $f->ref->{IPv4} || $f->ref->{IPv6};
66 my $tcp = $f->ref->{TCP};
67  
68 my $res;
69 if ($ip && $tcp) {
70 $res = Net::SinFP3::Next::IpPort->new(
71 global => $global,
72 ip => $ip->src,
73 port => $tcp->src,
74 ) or return;
75 if ($eth) {
76 $res->mac($eth->src);
77 }
78 return $res;
79 }
80 }
81  
82 return;
83 }
84  
85 sub init {
86 my $self = shift->SUPER::init(@_) or return;
87  
88 my $global = $self->global;
89 my $log = $global->log;
90  
91 my $portList = $global->portList;
92 my $portCount = $global->portCount;
93 my $targetCount = $global->targetCount;
94 my $targetList = $global->targetListAsInt;
95 if ($global->ipv6) {
96 $targetList = [ $global->targetIp ];
97 }
98
99 my $mask = $global->targetSubnet || $global->targetIp;
100  
101 # Prepare Dump object
102 my $filter;
103 if ($targetCount > 1) {
104 if (!$global->ipv6) {
105 $filter = '((tcp and dst host '.$global->ip.') or ('.
106 'icmp and dst host '.$global->ip.'))';
107 }
108 else {
109 $filter = '((tcp and dst host '.$global->ip6.' and src host '.
110 $mask.') or (icmp6 and dst host '.$global->ip6.'))';
111 }
112 }
113 else {
114 if (!$global->ipv6) {
115 $filter = '((tcp and dst host '.$global->ip.' and src host '.
116 $mask.') or (icmp and dst host '.$global->ip.'))';
117 }
118 else {
119 $filter = '((tcp and dst host '.$global->ip6.' and src host '.
120 $mask.') or (icmp6 and dst host '.$global->ip6.'))';
121 }
122 }
123 if ($global->targetSubnet) {
124 $filter .= ' and src net '.$global->targetSubnet;
125 }
126  
127 my $oDump = $global->getDumpOnline(
128 filter => $filter,
129 ) or return;
130 $oDump->start or return;
131  
132 # Main SYN scan loop
133 my $retry = $global->retry;
134 my $pps = $global->pps;
135  
136 my $ipv6 = $global->ipv6;
137 my $ip = $global->ip;
138 my $ip6 = $global->ip6;
139  
140 my $seconds = ($retry * $portCount * $targetCount / $pps) + $global->timeout;
141 $seconds =~ s/\.\d+$//;
142  
143 my $estim = Time::Interval::parseInterval(seconds => $seconds);
144 $log->info(sprintf("Estimated running time: %d day(s) %d hour(s) ".
145 "%d minute(s) %d second(s) for %d host(s)",
146 $estim->{days},
147 $estim->{hours},
148 $estim->{minutes},
149 $estim->{seconds},
150 $targetCount,
151 ));
152  
153 defined(my $pid = fork()) or $log->fatal("Can't fork() [$!]\n");
154 if (! $pid) { # Son
155 $log->debug("run send(): targets[$targetCount] ports[$portCount] ".
156 "packets[".($retry * $portCount * $targetCount)."]");
157  
158 if ($ipv6) {
159 my $r = Net::Write::Fast::l4_send_tcp_syn_multi(
160 $ip6,
161 $targetList,
162 $portList,
163 $pps,
164 $retry,
165 $ipv6,
166 $log->level, # display warnings or not
167 );
168 if ($r == 0) {
169 $log->fatal(Net::Write::Fast::nwf_geterror());
170 }
171 }
172 else {
173 # We have to split by chunks of 500_000 elements, to avoid taking to
174 # much memory in one row. And there is a SIGSEGV if we don't do so ;)
175 my $nChunks = ceil($targetCount / 500_000);
176 for my $n (0..$nChunks-1) {
177 my $first = 500_000 * $n;
178 my $last = 499_999 + (500_000 * $n);
179 if ($last > ($targetCount - 1)) {
180 $last = $targetCount - 1;
181 }
182  
183 if ($nChunks > 1) {
184 $log->info("Scanning chunk @{[$n+1]}/@{[($nChunks)]} ".
185 "($first-$last)");
186 }
187  
188 my @this = @$targetList[$first..$last];
189 my $r = Net::Write::Fast::l4_send_tcp_syn_multi(
190 $ipv6 ? $ip6 : $ip,
191 \@this,
192 $portList,
193 $pps,
194 $retry,
195 $ipv6,
196 $log->level, # display warnings or not
197 );
198 if ($r == 0) {
199 $log->error(Net::Write::Fast::nwf_geterror());
200 }
201 }
202 }
203  
204 $log->debug("stop send()");
205 exit(0);
206 }
207 # Parent
208 my $start;
209 my %skip = ();
210 while (1) {
211 if (my $f = $oDump->next) {
212 my $s = Net::Frame::Simple->newFromDump($f);
213 if ($s->ref->{TCP}) {
214 my $ip = $ipv6 ? $s->ref->{IPv6} : $s->ref->{IPv4};
215 my $tcp = $s->ref->{TCP};
216 # Skip ports which already had a reply
217 if ($skip{$ip->src}{$tcp->src}) {
218 }
219 elsif ($tcp->flags == 0x12) { # SYN+ACK
220 my $res = $self->_addToResult($s) or next;
221 my @old = $self->nextList;
222 $self->nextList([ @old, $res ]);
223 $log->verbose("Found ".$res->print);
224 $skip{$ip->src}{$tcp->src}++;
225 }
226 elsif ($tcp->flags == 0x14) { # RST+ACK
227 $skip{$ip->src}{$tcp->src}++;
228 }
229 }
230 }
231 my $r = waitpid($pid, WNOHANG);
232 # First time, if process stopped, it returns the $pid
233 # Other times, if process stopped, it returns -1
234 if ($r == $pid || $r == -1) { # Process stopped
235 if (! $start) { # Start counting for the timeout
236 $start = time();
237 }
238 # Check error code
239 if (($r == $pid) && $?) {
240 $log->fatal("Child returned: ".${^CHILD_ERROR_NATIVE});
241 }
242 # If stopped since $timeout seconds, we exit
243 if (time() - $start > $global->timeout) {
244 $log->debug("Process stopped since ".$global->timeout." second(s)");
245 last;
246 }
247 }
248 }
249  
250 $log->debug("Parent finished");
251  
252 # Cleanup before end of init()
253 $oDump->stop;
254  
255 return 1;
256 }
257  
258 sub run {
259 my $self = shift->SUPER::run(@_) or return;
260  
261 my @nextList = $self->nextList;
262 my $next = shift @nextList;
263 $self->nextList(\@nextList);
264  
265 return $next;
266 }
267  
268 1;
269  
270 __END__