/[scripts]/trunk/mitm-ssl.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/mitm-ssl.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 130 - (show annotations)
Wed Jan 6 23:19:29 2010 UTC (14 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 4816 byte(s)
always use dump dir

1 #!/usr/bin/perl
2 # SSL Man-In-The-Middle v0.1. Copyright (C) Vlatko Kosturjak, Kost
3 # Distributed under GPL v2+.
4
5 use strict;
6 use POSIX;
7 use IO::Socket::SSL;
8 use Getopt::Long;
9
10 my $localport;
11 my $localaddr;
12 my $help;
13 my $host;
14 my $port;
15 my $daemon;
16 my $buffersize = 2048;
17 my $logtype;
18 my $logdir;
19 my $daemon;
20 my $serverkey;
21 my $servercert;
22 my $serverdh;
23
24 $| = 1;
25
26 my $goresult = GetOptions(
27 "lport=i" => \$localport,
28 "laddr=s" => \$localaddr,
29 "rport=i" => \$port,
30 "raddr=s" => \$host,
31 "logtype=i" => \$logtype,
32 "logdir=s" => \$logdir,
33 "daemon" => \$daemon,
34 "serverkey=s" => \$serverkey,
35 "servercert=s" => \$servercert,
36 "serverdh=s" => \$serverdh,
37 "help" => \$help
38 );
39
40 if ($help) {
41 print <<"END";
42 SSL Man-In-The-Middle v0.1. Copyright (C) Vlatko Kosturjak, Kost
43 Distributed under GPL v2+.
44
45 Usage: $0 [OPTIONS]
46
47 --lport <port> Listening port (default 80)
48 --laddr <address> Listening address (default localhost)
49 --rport <port> Remote port to connect to (default 8080)
50 --raddr <address> Remote address to connect to (default localhost)
51 --serverkey <file> Certificate key file for local SSL server
52 --servercert <file> Certificate file for local SSL server
53 --serverdh <file> Diffie-Helman file for key exchange
54 --log <type> Type of log where 0 is no log (default 0)
55 --logdir Directory to log to (default .)
56 --daemon Daemonize (work in background)
57 --help Display this help message
58 END
59 exit;
60 }
61
62 # set default values
63 $localport = 8080 unless ($localport);
64 $localaddr = "127.0.0.1" unless ($localaddr);
65 $port = 80 unless ($port);
66 $host = "127.0.0.1" unless ($host);
67 $logdir = "dump" unless ($logdir);
68
69 mkdir $logdir;
70
71 my %o = (
72 'dir' => $logdir,
73 'port' => $localport,
74 'toport' => $port,
75 'tohost' => $host
76 );
77
78 if ($daemon) {
79 my $pid = fork;
80 exit if $pid;
81 die "$!" unless defined($pid);
82 POSIX::setsid() or die "$!";
83 }
84
85 my $ah = IO::Socket::SSL->new(
86 'LocalPort' => $localport,
87 'LocalAddr' => $localaddr,
88 'Reuse' => 1,
89 'Proto' => 'tcp',
90 'SSL_verify_mode' => '0',
91 'SSLdhfile' => $serverdh,
92 'SSL_cert_file' => $servercert,
93 'SSL_key_file' => $serverkey,
94 'Listen' => 10
95 ) || die "$!";
96
97 $SIG{'CHLD'} = 'IGNORE';
98 my $num = 0;
99
100 while (1) {
101 my $ch = $ah->accept();
102 if ( !$ch ) {
103 print STDERR "cannot accept: $! ", IO::Socket::SSL::errstr(),
104 "\n";
105 next;
106 }
107 if ( !$ch ) { print STDERR "cannot accept: $!\n"; next; }
108 ++$num;
109 my $pid = fork();
110 if ( !defined($pid) ) { print STDERR "cannot fork while(1) $!\n"; }
111 elsif ( $pid == 0 ) {
112 $ah->close( SSL_no_shutdown => 1 );
113 Run( \%o, $ch, $num );
114 } else {
115 $ch->close( SSL_no_shutdown => 1 );
116 }
117 }
118
119 sub Run {
120 my ( $o, $ch, $num ) = @_;
121 my $th = IO::Socket::SSL->new(
122 'PeerAddr' => $o->{'tohost'},
123 'PeerPort' => $o->{'toport'},
124 'SSL_use_cert' => '0',
125 'SSL_verify_mode' => '0',
126
127 # 'SSL_cipher_list' => 'NUL:LOW:EXP:ADH',
128 'SSL_version' => 'SSLv3', # SSLv3, SSLv2, TLSv1
129 'Proto' => 'tcp'
130 );
131 if ( !$th ) { print "cannot connect th: $!"; exit 0; }
132 else { print "connected!"; }
133 my $fh;
134 if ( $o->{'dir'} ) {
135 $fh = Symbol::gensym();
136 open( $fh, ">$o->{'dir'}/tunnel$num.log" ) or die "$!";
137 }
138 $ch->autoflush();
139 $th->autoflush();
140 my $httpheader = "";
141 my $httpbuf = "";
142 while ( $ch || $th ) {
143 my $rin = "";
144 vec( $rin, fileno($ch), 1 ) = 1 if $ch;
145 vec( $rin, fileno($th), 1 ) = 1 if $th;
146 my ( $rout, $eout );
147 select( $rout = $rin, undef, $eout = $rin, 120 );
148 if ( !$rout && !$eout ) { }
149 my $cbuffer = "";
150 my $tbuffer = "";
151
152 if ($ch
153 && ( vec( $eout, fileno($ch), 1 )
154 || vec( $rout, fileno($ch), 1 ) )
155 )
156 {
157 my $result = sysread( $ch, $tbuffer, $buffersize );
158 if ( !defined($result) ) {
159 print STDERR "$!\n";
160 exit 0;
161 }
162 if ( $result == 0 ) { exit 0; }
163 }
164 if ($th
165 && ( vec( $eout, fileno($th), 1 )
166 || vec( $rout, fileno($th), 1 ) )
167 )
168 {
169 my $result = sysread( $th, $cbuffer, $buffersize );
170 if ( !defined($result) ) { print STDERR "$!\n"; exit 0; }
171 if ( $result == 0 ) { exit 0; }
172 }
173 if ( $fh && $tbuffer ) {
174 ( print $fh "[c]" . $tbuffer . "[/c]" );
175 }
176 while ( my $len = length($tbuffer) ) {
177 my $res = syswrite( $th, $tbuffer, $len );
178 if ( $res > 0 ) { $tbuffer = substr( $tbuffer, $res ); }
179 else { print STDERR "$!\n"; }
180 }
181 if ( $fh && $cbuffer ) {
182 ( print $fh "[s]" . $cbuffer . "[/s]" );
183 }
184 while ( my $len = length($cbuffer) ) {
185 my $res = syswrite( $ch, $cbuffer, $len );
186 if ( $res > 0 ) { $cbuffer = substr( $cbuffer, $res ); }
187 else { print STDERR "$!\n"; }
188 }
189 }
190 }
191

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26