#!/usr/bin/perl -w use strict; use warnings; use Net::Proxy; use LWP::UserAgent; use Getopt::Long; our $VERSION = '0.06'; # default configuration our %CONF = ( 'proxy-authentication' => '', 'proxy' => $ENV{HTTP_PROXY}, 'user-agent' => "connect-tunnel/$VERSION", 'verbose' => 0, ); # # get and check the options # GetOptions( \%CONF, "verbose|v+", "tunnel|T=s@", "proxy|P=s", "proxy-authentication|A=s", "local-only|L", "user-agent|U=s" ); # set up the verbosity level Net::Proxy->set_verbosity( $CONF{verbose} ); # check for a proxy if ( $CONF{proxy} ) { $CONF{proxy} .= ":8080" if not $CONF{proxy} =~ /:/; } die "--proxy option required$/" if !$CONF{proxy}; # create the tunnels entrances die "--tunnel option required$/" unless exists $CONF{tunnel}; # split proxy-authentication { my ( $user, $pass ) = split ':', $CONF{'proxy-authentication'}, 2; $CONF{'proxy-authentication'} = $CONF{'proxy-authentication'} ? [ proxy_user => $user, proxy_pass => $pass ] : []; } for my $tunnel ( @{ $CONF{tunnel} } ) { die "--tunnel format required$/" if $tunnel !~ /^\d+:[\w.]+:\d+$/; my ( $port, $host, $hostport ) = split ':', $tunnel; my ( $proxy_host, $proxy_port) = split ':', $CONF{proxy}; my $proxy = Net::Proxy->new( { in => { type => 'tcp', port => $port, host => $CONF{'local-only'} ? 'localhost' : '0.0.0.0', }, out => { type => 'connect', host => $host, port => $hostport, proxy_host => $proxy_host, proxy_port => $proxy_port, proxy_agent => $CONF{'user-agent'}, @{ $CONF{'proxy-authentication'} }, }, } ); $proxy->register(); } # the main loop Net::Proxy->mainloop(); __END__ =head1 NAME connect-tunnel - Create CONNECT tunnels through HTTP proxies =head1 SYNOPSIS B S<[ B<-Lv> ]> S<[ B<-A> I ]> S<[ B<-P> I ]> S<[ B<-C> I ]> S<[ B<-T> I ]> =head1 DESCRIPTION B sets up tunneled connections to external hosts by redirecting connections to local ports towards thoses hosts/ports through a HTTP proxy. B makes use of the HTTP C method to ask the proxy to create a tunnel to an outside server. Be aware that some proxies are set up to deny outside tunnels (either to ports other than 443 or outside a specified set of outside hosts). =head1 OPTIONS The program follows the usual GNU command line syntax, with long options starting with two dashes. =over 4 =item B<-A>, B<--proxy-authentication> I Proxy authentication information. Please note that all the authentication schemes supported by C are supported (we use an C internally to contact the proxy). =item B<-C>, B<--control-port> I The port to which one can connect to issue control commands to B. See L for more details about the available commands. =item B<-L>, B<--local-only> Create the tunnels so that they will only listen on C. Thus, only connections originating from the machine that runs B will be accepted. That was the default behaviour in B version 0.02. =item B<-P>, B<--proxy> I[I<:port>] The proxy is required to connect the tunnels. If no port is given, 8080 is used by default. See also L. =item B<-T>, B<--tunnel> I Specifies that the given I on the local host is to be forwarded to the given I and I on the remote side. This works by allocating a socket to listen to I on the local side, and whenever a connection is made to this I, B forwards it to the proxy (with the credentials, if required), which in turn forwards it to the final destination. Note that this does not imply the use of any cryptographic system (SSL or any other). This is a simple TCP redirection. The security if any, is the one provided by the protocol used to connect to the destination through B. On Unix systems, only root can forward privileged ports. Note that you can setup tunnels to multiple destinations, by using the B<--tunnel> option several times. =item B<-U>, B<--user-agent> I Specify User-Agent value to send in HTTP requests. The default is to send C>. =item B<-v>, B<--verbose> Verbose output. This option can be used several times for more verbose output. =back =head1 EXAMPLES To connect to a SSH server running on C, on port 443, through the proxy C, running on port 8080, use the following command: connect-tunnel -P proxy.company.com:8080 -T 22:ssh.example.com:443 And now point your favorite ssh client to the machine running B. You can also emulate a "standard" user-agent: connect-tunnel -U "Mozilla/4.03 [en] (X11; I; Linux 2.1.89 i586)" -P proxy.company.com:8080 -T 22:ssh.example.com:443 B can easily use your proxy credentials to connect outside: connect-tunnel -U "Mozilla/4.03 [en] (X11; I; Linux 2.1.89 i586)" -P proxy.company.com:8080 -T 22:ssh.example.com:443 -A book:s3kr3t But if you don't want anybody else to connect to your tunnels and through the proxy with I credentials, use the B<--local-only> option: connect-tunnel -U "Mozilla/4.03 [en] (X11; I; Linux 2.1.89 i586)" -P proxy.company.com:8080 -T 22:ssh.example.com:443 -A book:s3kr3t -L If you have several destinations, there is no need to run several instances of B: connect-tunnel -U "Mozilla/4.03 [en] (X11; I; Linux 2.1.89 i586)" -P proxy.company.com:8080 -A book:s3kr3t -L -T 22:ssh.example.com:443 -T 222:ssh2.example.com:443 But naturally, you will need to correctly set up the ports in your clients. Mmm, such a long command line would perfectly fit in an alias or a F<.BAT> file. C<;-)> =head1 ENVIRONMENT VARIABLES The environment variable C can be used to provide a proxy definition. The environment variable is overriden by the B<--proxy> option, if passed to B. =head1 AUTHOR Philippe "BooK" Bruhat, C<< >>. I seem to have re-invented a well-known wheel with that script, but at least, I hope I have added a few interesting options to it. =head1 SCRIPT HISTORY The first version of the script was a quick hack that let me go through a corporate proxy. Version 0.02 and version 0.03 were released on CPAN in 2003. Version 0.04 sits half-finished in a CVS repository at home: I couldn't decypher the spaghetti of my data structures any more. C<:-(> Version 0.05 (and higher) are based on C, and included with the C distribution. =head1 Even though it's not rocket science, B has been cited in at least one academic works: =over 4 =item * I, Daniel Alman Available at SANS InfoSec Reading Room: Covert Channels L Direct link: L =back =head1 COPYRIGHT Copyright 2003-2006, Philippe Bruhat. All rights reserved. =head1 LICENSE This module is free software; you can redistribute it or modify it under the same terms as Perl itself. =cut