| 1 | # | ||
| 2 | # $Id: Connect.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $ | ||
| 3 | # | ||
| 4 | package Net::SinFP3::Input::Connect; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use base qw(Net::SinFP3::Input); | ||
| 9 | our @AS = qw( | ||
| 10 | data | ||
| 11 | _dump | ||
| 12 | ); | ||
| 13 | __PACKAGE__->cgBuildIndices; | ||
| 14 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 15 | |||
| 16 | use Net::SinFP3::Next::MultiFrame; | ||
| 17 | |||
| 18 | sub give { | ||
| 19 | return [ | ||
| 20 | 'Net::SinFP3::Next::Frame', | ||
| 21 | ]; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my $self = shift->SUPER::new( | ||
| 26 | data => "GET / HTTP/1.0\r\n\r\n", | ||
| 27 | @_, | ||
| 28 | ); | ||
| 29 | |||
| 30 | my $global = $self->global; | ||
| 31 | my $log = $global->log; | ||
| 32 | |||
| 33 | if (! defined($global->target)) { | ||
| 34 | $log->fatal("You must provide `target' attribute in Global object"); | ||
| 35 | } | ||
| 36 | |||
| 37 | if (! defined($global->port)) { | ||
| 38 | $log->fatal("You must provide `port' attribute in Global object"); | ||
| 39 | } | ||
| 40 | |||
| 41 | my $port = $global->port; | ||
| 42 | if ($port !~ /^[-,\d]+$/) { | ||
| 43 | $log->fatal("Invalid port provided: [$port]"); | ||
| 44 | } | ||
| 45 | |||
| 46 | if (! $global->targetIp) { | ||
| 47 | $log->fatal("Invalid target IP provided: [".$global->targetIp."]"); | ||
| 48 | } | ||
| 49 | |||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub init { | ||
| 54 | my $self = shift->SUPER::init(@_) or return; | ||
| 55 | |||
| 56 | my $global = $self->global; | ||
| 57 | my $log = $global->log; | ||
| 58 | |||
| 59 | my $me = $global->ip; | ||
| 60 | my $ip = $global->targetIp; | ||
| 61 | my $port = $global->port; | ||
| 62 | |||
| 63 | # Capture TCP SYN and SYN|ACK between source and target | ||
| 64 | my $filter = ''; | ||
| 65 | if ($global->ipv6) { | ||
| 66 | $filter = "(tcp and host $ip and port $port)"; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | $filter = "(tcp and src host $ip and src port $port)". | ||
| 70 | " or ". | ||
| 71 | "(tcp and dst host $ip and dst port $port)". | ||
| 72 | " and (tcp[tcpflags] == (tcp-syn|tcp-ack) or ". | ||
| 73 | " tcp[tcpflags] == (tcp-syn))"; | ||
| 74 | } | ||
| 75 | |||
| 76 | my $oDump = $global->getDumpOnline( | ||
| 77 | filter => $filter, | ||
| 78 | timeoutOnNext => 0, | ||
| 79 | ); | ||
| 80 | $oDump->start; | ||
| 81 | |||
| 82 | $self->_dump($oDump); | ||
| 83 | |||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub run { | ||
| 88 | my $self = shift->SUPER::run(@_) or return; | ||
| 89 | |||
| 90 | my $global = $self->global; | ||
| 91 | my $log = $global->log; | ||
| 92 | |||
| 93 | my $ip = $global->targetIp; | ||
| 94 | my $port = $global->port; | ||
| 95 | |||
| 96 | $log->info("Connecting to [$ip]:$port"); | ||
| 97 | |||
| 98 | my $s = $global->tcpConnect(ip => $ip, port => $port); | ||
| 99 | print $s $self->data; | ||
| 100 | close($s); | ||
| 101 | |||
| 102 | $log->info("Success sending [".$self->data."]"); | ||
| 103 | |||
| 104 | my $oDump = $self->_dump; | ||
| 105 | |||
| 106 | my @frames = (); | ||
| 107 | while (my $h = $oDump->next) { | ||
| 108 | my $frame = Net::Frame::Simple->newFromDump($h); | ||
| 109 | |||
| 110 | # Due to some buggy pcap installs that miss ip6 filter | ||
| 111 | if ($global->ipv6 && !$frame->ref->{IPv6}) { | ||
| 112 | next; | ||
| 113 | } | ||
| 114 | |||
| 115 | push @frames, $frame; | ||
| 116 | |||
| 117 | $self->last(1); | ||
| 118 | } | ||
| 119 | |||
| 120 | my $next = Net::SinFP3::Next::MultiFrame->new( | ||
| 121 | global => $global, | ||
| 122 | frameList => \@frames, | ||
| 123 | ); | ||
| 124 | |||
| 125 | return $next; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub post { | ||
| 129 | my $self = shift; | ||
| 130 | $self->_dump->stop; | ||
| 131 | $self->_dump(undef); | ||
| 132 | return 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 | |||
| 137 | __END__ |