Today is: 7 January, 2012
Check todays hot topics

Testing Open Proxies

This is code that was given to me while building LIB::Spider. Judging by the way it's written, it's a little old. I don't really know or care if it works. I thought it was a semi-neat addition to the proxy spider. I used it as a guideline while building a perl module for testing proxies in parallel.

This is modified by some Efnet admins to connect to a private server and announce it's presence. A bot runs here that will promptly globally g/k-line the open proxy. I think there are far better ways to do this but I guess they're pretty set in their ways.

I have not touched this script. Any comments, problems, or specific configurations were not put in by me.

#!/usr/bin/perl
#
 
#use warnings;
use strict;
 
use IO::Socket;
use IO::Handle;
use POSIX qw(:signal_h :sys_wait_h);  # fork
 
use Time::HiRes;
 
use vars qw($VERSION);
$VERSION = "1.0";
 
# EO4L Modules
use LWP::Simple;
use String::Random;
 
# {{{ globals
 
my ($g_forkcount, $g_pid) = (0, undef);
my ($g_dead_nigger_storage, $g_maxfork) = (0, 60);
my $ptype;
 
#######################
# EO4L Config Options #
#######################
 
my $cookie;
my $url = 'http://pileofcarp.com/cookie.txt';
my $g_server_host = 'irc.efnet.org';
my @g_server_ip = resolve($g_server_host);
 
###########################
# END EO4L Config Options #
###########################
 
my $randchar = new String::Random ;
 
# {{{ signal handlers
 
$SIG{INT} = sub { kill('INT', (getpgrp(0) * -1)) && exit; };
$SIG{CHLD} = sub { $g_dead_nigger_storage++ while(($g_pid = waitpid(-1, WNOHANG)) > 0); };
 
# }}}
 
 
# {{{ entry point
 
if ($ARGV[0]) {
  show_help(); exit 0;
}
 
# }}}
 
# {{{ help/usage information
 
sub show_help {
  print <<EOF;
     Please run this with no command line options
     Enjoy. -- YA WE KILLIN UR PROXIES
EOF
}
 
# }}}
 
 
# load the proxy and name list(s)
my @g_proxies = load_proxy_list();
 
 
# fork(2) off up to $g_maxfork child processes to use as
# a pool for subsequent connection attempts
notice("Initializing (forking) bots");
for ($g_forkcount = 0;                  # $g_forkcount must _not_
     $g_forkcount < $g_maxfork;         # be local to here
     $g_forkcount++) {
  sleep 1;                              # so we don't overload ourselves
 
  if (!defined(my $g_pid = fork())) {   # fork
    error("couldn't fork: $!");         # die if fork fails
  } elsif ($g_pid == 0) {
    # in child:
    while (@g_proxies) {
      # grab a random proxy off the list...
      my $proxy_slot = int rand @g_proxies;
      my $proxy = $g_proxies[$proxy_slot];
 
      # ...attempt to establish a connection through it 
      if(spawn_bot($proxy->{ip}, $proxy->{port}, $proxy->{type},
                   @g_server_ip, $g_server_host)) {
        # succeeded
      } else {
        # failed, delete proxy
        # XXX: not shared
        #delete $g_proxies[$proxy_slot];
      };
 
     # sleep 10;   # to prevent throttling by IRCd
    }
    exit 0;
 
  } else {
    # in parent:
 
  }
}
 
sleep while ($g_dead_nigger_storage < $g_maxfork);
exit 666;
 
 
# {{{ load lists
 
sub load_proxy_list {
  my (@proxies);
 
  error("$@") unless push @proxies, load_socks4_list();
  error("$@") unless push @proxies, load_http_list();
  return @proxies;
}
 
sub load_socks4_list {
  my (@proxies);
 
  open SOCKSFILE, "<", "./socks4.txt" or error("could not open SOCKS 4 proxy file socks4.txt: $!");
  while (<SOCKSFILE>) {
    chomp;
    my ($ip, $port) = /([^:]+):([0-9]+)/;
    push @proxies, ({ip => $ip, port => $port, type => '4'});
  }
  close(SOCKSFILE) or error("could not close SOCKS 4 proxy file socks4.txt: $!");
 
  notice("acquired ". scalar(@proxies) ." SOCKS 4 prox(y|ies).");
  return (@proxies);
}
 
sub load_http_list {
  my (@proxies);
 
  open HTTPFILE, "<", "./http.txt" or error("could not open HTTP proxy file http.txt: $!");
  while (<HTTPFILE>) {
    chomp;
    my ($ip, $port) = /([^:]+):([0-9]+)/;
    push @proxies, ({ip => $ip, port => $port, type => 'h'});
  }
  close(HTTPFILE) or error("could not close HTTP proxy file http.txt: $!");
 
  notice("acquired ". scalar(@proxies) ." http prox(y|ies).");
  return (@proxies);
}
 
# {{{ wrappers/tools
 
sub iptoipstr {
  my ($ip) = $_;
  my $d = $ip % 256; $ip -= $d; $ip /= 256;
  my $c = $ip % 256; $ip -= $c; $ip /= 256;
  my $b = $ip % 256; $ip -= $b; $ip /= 256;
  my $a = $ip;
  my $ipstr = "$a.$b.$c.$d";
  return $ipstr;
}
 
sub notice {
  my $notice = shift;
  print ">>>> ". $notice ."\n";
  return;
}
 
sub incoming {
  my ($nick, $line, $server) = @_;
  #printf("IRCd >>>> %-12s  ] %s\n", $nick, $line);
  return;
}
 
sub outgoing {
  my ($nick, $line, $server) = @_;
  #printf("IRCd <<<< %-12s  ] %s\n", $nick, $line);
  return;
}
 
sub warning {
  my $warning = shift;
  print "!!!! ". $warning ."\n";
  return;
}
 
sub error {
  my $error = shift;
  print "!!!! ". $error ."\n";
  exit 0;
}
 
# }}}
 
# {{{ per-drone logic
 
sub spawn_bot { # only return 0 if the proxy failed.  Otherwise, return 1;
  my ($proxy_ip, $proxy_port, $proxy_type,
      $remote_ip, $remote_host) = @_;
  # They had some retarded ass logic to feed a list of nicks/usernames, uh lets be smart and choose random strings...
  my $nick = $randchar->randregex("[a-zA-Z][a-zA-Z0-9]{2,8}");
  my $ident = $randchar->randregex("[a-zA-Z][a-zA-Z0-9]{2,8}");
  my $realname = $randchar->randregex("[a-zA-Z][a-zA-Z0-9]{2,10}");
  my ($line, $sock, $altsock);
  my ($pingtime) = -1;
 
  eval {
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
    alarm 5;
    $sock = connect_to_proxy($proxy_ip, $proxy_port, $proxy_type,
                             $remote_ip, 6667);
    alarm 0;
  };
  if ($@) {
    error("unkown error: $@") unless $@ eq "alarm\n"; # propagate unexpected errors
    #warning("$proxy_ip:$proxy_port not responding, removing from list");
    #return 0;
  }
 
  $sock = connect_to_proxy($proxy_ip, $proxy_port, $proxy_type,
                           $remote_ip, 6667);
  return 0 unless $sock;
 
  chomp($cookie = get $url);
 
  if ( $proxy_type =~ /h/i ) {
        $ptype = 'HTTP';
  } elsif ( $proxy_type == 4 ) {
        $ptype = 'SOCKS4';
  } else {
        $ptype = $proxy_type;
  }
 
  print $sock "NICK $nick\r\n";
  outgoing($nick, "NICK $nick");
  print $sock "USER $ident 0 0 :$cookie $proxy_type\r\n";
  outgoing($nick, "USER $ident 0 0 :$cookie $proxy_type");
 
  while ($line = <$sock>) {
    chomp $line;
    # MIGHT WANNA ADJUST THESE vv
    next if $line =~ /372/; # ignore motd msgs
    incoming($nick, $line);
    last if $line =~ /376|422/; # end of motd or no motd
    return 0 if $line =~ /BANNED/i;
    return 0 if $line =~ /ERROR.*G.lined/i;
    return 0 if $line =~ /ERROR.*K.lined/i;
    return 1 if $line =~ /ERROR/i;
    return 1 if $line =~ /432/;
    return 1 if $line =~ /433/;
    # MIGHT WANNA ADJUST THESE ^^
    if ($line =~ /PING (.*)$/) {
      print $sock "PONG $1\r\n";
    }
  } 
  notice("connected to $remote_host as $nick!$ident\@$proxy_ip ($proxy_ip:$proxy_port:$ptype)");
  if ( $proxy_type =~ /h/i ) { 
        $ptype = 'HTTP';
  } elsif ( $proxy_type == 4 ) {
        $ptype = 'SOCKS4';
  } else {
        $ptype = $proxy_type;
  }
}
 
# }}}
 
# {{{ proxy protocol handshakes/tunnel establishment
 
sub connect_to_proxy {
  my ($proxy_ip, $proxy_port, $proxy_type,
      $remote_ip, $remote_port) = @_;
  if($proxy_type eq '4') {
    return connect_to_socks4_proxy($proxy_ip, $proxy_port,
                                   $remote_ip, $remote_port);
  } elsif($proxy_type eq 'h') {
    return connect_to_http_proxy($proxy_ip, $proxy_port,
                                 $remote_ip, $remote_port);
  } else {
    error("unknown proxy type $proxy_type ($proxy_ip:$proxy_port)");
  }
}
 
sub connect_to_socks4_proxy {
  my ($socks_ip, $socks_port, $remote_ip, $remote_port) = @_;
  my $sock = IO::Socket::INET->new(
    PeerAddr => $socks_ip,
    PeerPort => $socks_port,
    Proto  => 'tcp',
    Timeout => '8'
  );
  return unless $sock;
  $sock->autoflush(1);
  print $sock pack('CCn', 4, 1, $remote_port) . inet_aton($remote_ip) . pack('x');
  my $received = '';
  while (read($sock, $received, 8) && (length($received) < 8)) {}
  my ($vn, $cd, $listen_port, $listen_addr) = unpack('CCnN', $received);
  return unless $cd;
  if ($cd != 90) {
    return;
  }
  return $sock;
}
 
sub connect_to_http_proxy {
  my ($http_ip, $http_port, $remote_ip, $remote_port) = @_;
  my $sock = IO::Socket::INET->new(
    PeerAddr => $http_ip,
    PeerPort => $http_port,
    Proto  => 'tcp',
    Timeout => '8'
  );
  return unless $sock;
  $sock->autoflush(1);
 
  print $sock "CONNECT $remote_ip:$remote_port HTTP/1.0\r\n\r\n";
  my $received = '';
  while (read($sock, $received, 12) && (length($received) < 12)) {}
  my (undef, $response) = split / /, $received;
  return if $received eq "";
  return if $response ne '200';
 
  while(read($sock, $received, 1)) {
    if($received eq "\n") {
      read($sock, $received, 1);
      if($received eq "\r") {
        read($sock, $received, 1);
        return $sock;
      }
    }
  }
  return;
}
 
sub resolve {
  my $host = shift;
  my (undef, undef, undef, undef, @servers) = gethostbyname($host);
  unless (@servers) {
    error("cannot resolve server $host: $?");
    return 0;
  }
  my @servers_ip;
  foreach my $server (@servers) {
    my ($a, $b, $c, $d) = unpack('C4', $server);
    my $server_ip = "$a.$b.$c.$d";
    push (@servers_ip, $server_ip);
  }
  return @servers_ip;
}

AttachmentSize
arab.pl.txt8.53 KB