| 1 | ############################################################################ | ||
| 2 | # Net::Inspect::L5::Socks | ||
| 3 | # handle Socks encapsulation (only Socks4 connect currently) | ||
| 4 | # TODO: bind, Socks5 | ||
| 5 | ############################################################################ | ||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | package Net::Inspect::L5::Socks; | ||
| 9 | use base 'Net::Inspect::Connection'; | ||
| 10 | use fields qw(replay sockshdr meta fwd error); | ||
| 11 | use Net::Inspect::Debug; | ||
| 12 | use Socket 'inet_ntoa'; | ||
| 13 | |||
| 14 | sub guess_protocol { | ||
| 15 | my ($self,$guess,$dir,$data,$eof,$time,$meta) = @_; | ||
| 16 | |||
| 17 | if ($data ne '') { | ||
| 18 | # keep all calls for replaying later | ||
| 19 | my $replay = $self->{replay} ||=[]; | ||
| 20 | push @$replay,[$dir,$data,$eof,$time]; | ||
| 21 | |||
| 22 | if ( $dir == 1 ) { # socks reply? | ||
| 23 | my $buf0 = my $buf1 = ''; | ||
| 24 | for(@$replay) { | ||
| 25 | if ($_->[0]) { | ||
| 26 | $buf1 .= $_->[1] | ||
| 27 | } else { | ||
| 28 | $buf0 .= $_->[1] | ||
| 29 | } | ||
| 30 | } | ||
| 31 | goto not_me if length($buf0)<9; # too small for socks4 header | ||
| 32 | my ($ver,$conn,$port,$ip) = unpack('CCna4',$buf0); | ||
| 33 | goto not_me if $ver != 4; # not socks4 | ||
| 34 | goto not_me if ! $conn; # do only connect not bind for now | ||
| 35 | |||
| 36 | return if length($buf1)<8; # not enough bytes for response | ||
| 37 | ($ver,my $status,$port,$ip) = unpack('CCna4',$buf1); | ||
| 38 | goto not_me if $ver != 0; # no socks4 reply | ||
| 39 | |||
| 40 | # FIXME - what should we do if status not success? | ||
| 41 | goto not_me if $status != 90; | ||
| 42 | |||
| 43 | |||
| 44 | # looks like socks4 | ||
| 45 | my $obj = $self->new_connection($meta) or goto not_me; | ||
| 46 | $obj->in(@$_) for(@$replay); | ||
| 47 | return ($obj,length($data)); | ||
| 48 | |||
| 49 | not_me: | ||
| 50 | $guess->detach($self); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | return; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub in { | ||
| 59 | my ($self,$dir,$data,$eof,$time) = @_; | ||
| 60 | return length($data) if $self->{error}; | ||
| 61 | |||
| 62 | my $bytes = 0; | ||
| 63 | if ( ! $self->{fwd} ) { | ||
| 64 | # strip socks header | ||
| 65 | if ( $dir == 0 ) { | ||
| 66 | if ( ! $self->{sockshdr} ) { | ||
| 67 | goto need_more if length($data)<9; # incomplete socks4 header | ||
| 68 | my ($ver,$conn,$port,$ip) = | ||
| 69 | unpack('CCna4',substr($data,0,8,'')); | ||
| 70 | return $self->fatal("only version 4 supported, version=$ver") | ||
| 71 | if $ver != 4; | ||
| 72 | return $self->fatal("only connect supported") if ! $conn; | ||
| 73 | |||
| 74 | # strip username\0 | ||
| 75 | my $null = index($data,"\0"); | ||
| 76 | if ( $null<0 ) { | ||
| 77 | # username not finished | ||
| 78 | return $self->fatal("username too long") if length($data)>512; | ||
| 79 | goto need_more; | ||
| 80 | } | ||
| 81 | my $user = substr($data,0,$null+1,''); | ||
| 82 | $bytes += 8 + $null + 1; | ||
| 83 | |||
| 84 | $self->{sockshdr} = { | ||
| 85 | daddr => inet_ntoa($ip), | ||
| 86 | dport => $port, | ||
| 87 | socks_user => $user, | ||
| 88 | replay0 => [], | ||
| 89 | }; | ||
| 90 | |||
| 91 | return $bytes if $data eq ''; # done | ||
| 92 | } | ||
| 93 | |||
| 94 | # no object yet, e.g. no socks response: buffer data | ||
| 95 | push @{ $self->{sockshdr}{replay0}}, [ $data,$eof,$time ]; | ||
| 96 | $bytes += length($data); | ||
| 97 | return $bytes; | ||
| 98 | |||
| 99 | } elsif ( $self->{sockshdr} ) { | ||
| 100 | # socks reply | ||
| 101 | goto need_more if length($data)<8; # incomplete | ||
| 102 | my ($ver,$status,$port,$ip) = unpack('CCna4',substr($data,0,8,'')); | ||
| 103 | $ver == 0 or return $self->fatal("invalid version $ver in socks4 reply"); | ||
| 104 | my $r0 = delete $self->{sockshdr}{replay0}; | ||
| 105 | if ( $status == 90 ) { | ||
| 106 | # successful connect | ||
| 107 | $self->{fwd} = $self->{upper_flow}->new_connection({ | ||
| 108 | %{$self->{meta}}, | ||
| 109 | %{$self->{sockshdr}}, | ||
| 110 | }); | ||
| 111 | $self->{fwd} ||= Net::Inspect::L5::Socks::IgnoreConnection->new; | ||
| 112 | } else { | ||
| 113 | $self->{fwd} = Net::Inspect::L5::Socks::NoData->new; | ||
| 114 | } | ||
| 115 | $self->{fwd}->in(0,@$_) for(@$r0); | ||
| 116 | $bytes += 8; | ||
| 117 | return $bytes if $data eq ''; | ||
| 118 | |||
| 119 | } else { | ||
| 120 | $self->fatal("data from server w/o sockshdr from client"); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | # got socks header from both sides, just forward to upper layer | ||
| 125 | return $bytes + $self->{fwd}->in($dir,$data,$eof,$time); | ||
| 126 | |||
| 127 | need_more: | ||
| 128 | $self->fatal("eof inside socks hdr($dir)") if $eof; | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | sub new_connection { | ||
| 134 | my ($self,$meta) = @_; | ||
| 135 | my $obj = $self->new; | ||
| 136 | $obj->{meta} = $meta; | ||
| 137 | return $obj; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub fatal { | ||
| 141 | my ($self,$reason,$dir,$time) = @_; | ||
| 142 | $self->{error} = 1; | ||
| 143 | my $obj = $self->{fwd}; | ||
| 144 | return $obj->fatal($reason,$dir,$time) if $obj; | ||
| 145 | trace($reason); | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub expire { | ||
| 150 | my ($self,$time) = @_; | ||
| 151 | if ( my $obj = $self->{fwd} ) { | ||
| 152 | return $obj->expire($time) | ||
| 153 | } | ||
| 154 | return $self->SUPER::expire($time); | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | package Net::Inspect::L5::Socks::IgnoreConnection; | ||
| 159 | { | ||
| 160 | my $singleton; | ||
| 161 | sub new { return $singleton ||= bless {},shift } | ||
| 162 | sub in { | ||
| 163 | my ($self,$dir,$data) = @_; | ||
| 164 | return length($data); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | package Net::Inspect::L5::Socks::NoDataConnection; | ||
| 170 | use base 'Net::Inspect::L5::Socks'; | ||
| 171 | { | ||
| 172 | my $singleton; | ||
| 173 | sub new { return $singleton ||= bless {},shift } | ||
| 174 | sub in { | ||
| 175 | my ($self,$dir,$data) = @_; | ||
| 176 | return $self->fatal("unexpected data in connection with socks error") | ||
| 177 | if $data ne ''; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | 1; | ||
| 183 | |||
| 184 | __END__ |