Stoppt die Vorratsdatenspeicherung! Jetzt klicken & handeln!Willst du auch bei der Aktion teilnehmen? Hier findest du alle relevanten Infos und Materialien:

ProxyGen - Aufbau



Jeder kennt das, man ist gerade mal unterwegs und will einfach mal eben schnell nen Proxy haben. Nunja hier ist Lösung - ein kleines Perl-Script was noch ganz Alpha ist und welches ich noch weiter entwickeln möchte.

Den Output wirft man in das Firefox-Plugin ProxySwitch.

ToDo



  • Besser Code / CodeReview
  • Direkter Firefox-Import
  • Unterstützung anderer Browser
  • Parsen einer anderen Seite ermöglichen?

Quellcode



#!/usr/bin/perl

# needed modules
use WWW::Mechanize;
use HTML::TableExtract;
use IO::Socket::INET;
use Net::IP;
use Net::DNS::Resolver;

# debugging
my $debug = "true";

# mechanize handler
my $m = WWW::Mechanize->new();

# site to parse
my $url = $ARGV[0] || "http://www.publicproxyservers.com/page1.html";

# high-anonimity only?
my $ha_only = "true";

#################################################
# Don't change from here
#################################################

# we need some global arrays
my @proxies; # array for all proxies
my @gproxies; # array for good proxies

# call the sub for parsing the site
parse_site();
print "[+] site was scanned\n";
my $num_proxies = @proxies;
print "[+] found $num_proxies proxies total\n";

# call the sub for checking if proxy answers
test_proxie();
my $num_gproxies = @gproxies;
print "[+] found $num_gproxies working proxies\n";

# if debug we list
if ($debug eq "true") {
    list_proxies();
}

mm3_output();

# now we write the good proxies to FF switch-proxy conf


#################################################
# subs
#################################################

# test-proxy sub
sub test_proxie {
    my $timeout = 2; # timeout for connections - we want fast servers only!
        foreach $proxie (@proxies) {
            if (($ha_only eq "true" && $proxie->{mode} eq "high anonymity") || $ha_only eq "false") {
                my $sock = IO::Socket::INET->new(PeerAddr => $proxie->{ip}, PeerPort => $proxie->{port}, Proto => 'tcp', Timeout => $timeout) || next;
                if ($debug eq "true") {print "[++] Found: $proxie->{ip}:$proxie->{port} in mode $proxie->{mode}\n";};
                my $name = query_dns($proxie->{ip});
                push @gproxies, {ip => $proxie->{ip}, name => $name,port => $proxie->{port}, mode => $proxie->{mode}};
            }
        }
}


# sub for parsing the website
sub parse_site {
    my @tableheaders = qw (IP Port Type);

    my $agent = WWW::Mechanize->new();
    $agent->get($url) || die "[+] Website cannot be opened - exiting!\n";

# Output headers
    print join(',',@tableheaders), "\n";

# Find table in html page
    $te = new HTML::TableExtract( headers => \@tableheaders );
    $te->parse( $agent->content() ); #parse contents

# Examine all matching tables (there is only be one?)
        foreach $ts ($te->table_states) {
            foreach my $row ($ts->rows) {
                if (@$row[0] =~ /[\d]/ && @$row[1] =~ /[\d]/) {
                    if ( $debug eq "true") { print "[++] New Proxy @$row[0]:@$row[1] in @$row[2]-mode found \n"; };
# filling array
                    push @proxies, { ip => @$row[0], port => @$row[1], mode => @$row[2] };
                }
            }
        }
}

# function for dns lookups
sub query_dns {
    $ip = shift; # get IP number
        if ( $debug eq "true") { print "[++] Resolving $ip\n"; }; # DEBUG INFO

            my $res = Net::DNS::Resolver->new; # create the resolver object
                $res->tcp_timeout(3); # IMPORTANT! Set timeout
                my $query = $res->search("$ip"); # creaing the query
                if ($query) {
# checking the result
                    foreach my $rr ($query->answer) {
                        next unless $rr->type eq "PTR";
                        $name = $rr->rdatastr;
                        chop($name);
                        if ( $debug eq "true") { print "[++] $ip is known as $name\n"; }; # DEBUG INFO
                            return ($name); # return DNS name
                    }
                } else {
                    if ( $debug eq "true") { print "[++] $ip has no DNS-name\n"; }; # DEBUG INFO
                        return ($ip); # no name found - return ip
                }
}

# list the good proxies
sub list_proxies {
    print    "[++] ==== List of answering proxies ===\n";
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    print "|       IP         |  Port  |              Name              |         Mode         |\n";
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    foreach $proxie (@gproxies) {
        my $ip = $proxie->{ip};
        my $port = $proxie->{port};
        my $name = $proxie->{name};
        my $mode = $proxie->{mode};
        $ip .=  (" " x (16-length($ip))) if (16-length($ip)>0);
        $port .= ( " " x (6-length($port))) if (6-length($port)>0);
        $name .= ( " " x (30-length($name))) if (30-length($name)>0);
        $mode .= ( " " x (20-length($mode))) if (20-length($mode)>0);
        print "| $ip | $port | $name | $mode |\n";    
    }
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    print "[++] $num_gproxies total.\n";
}
# MM3 Output
sub mm3_output {
    print "\n[+] MM3-ProxySwitch\n";
    print "\[+] Just open the edit-dialog and do copy and paste\n";
    print "\n<-- snip -->\n";
    foreach $proxie (@gproxies) {
        print "[$proxie->{name}\nhttp=$proxie->{ip}:$proxie->{port}\nftp=$proxie->{ip}:$proxie->{port}\n]\n";
    }
    print "<-- snip -->\n\n";
}
#EOF