Changes between Version 12 and Version 13 of doc/TorDNSExitList


Ignore:
Timestamp:
Apr 23, 2010, 10:49:37 AM (9 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • doc/TorDNSExitList

    v12 v13  
    9191You will need the [http://www.net-dns.org/ Net::DNS module] and its dependencies for this to work properly.
    9292{{{
    93 #!/usr/local/bin/perl -w
    94 $|=1;
    95 
    96 # torel_check ($ip, $port, $destip) queries the Tor DNS Exit List server.
     93#!/usr/local/bin/perl
     94
     95use strict;
     96use warnings;
     97
     98# query_exitlist($srcip, $dstip, $dstport) queries the Tor DNS Exit List server.
    9799#   The result of the query is one of the following:
    98 #   -1 : DNS lookup failed to get a response, or other error occurred.
    99 #    0 : $ip does not appear to be a Tor exit.
    100 #    1 : $ip is a known Tor exit for the provided destination IP / port.
    101 
     100#     undef : DNS lookup failed or an unexpected response was received.
     101#         0 : $srcip does not appear to be a Tor exit.
     102#         1 : $srcip is a known Tor exit for the provided destination IP / port.
     103
     104use Getopt::Long;
    102105use Net::DNS::Resolver;
    103 use Getopt::Long;
    104 
    105 # some helper methods
    106 sub revaddr {
    107   my ($addr) = @_;
    108   my ($a, $b, $c, $d) = split('[.]', $addr);
    109   return("${d}.${c}.${b}.${a}");
    110 }
    111 
    112 sub torel_qh {
    113   my ($ip, $port, $dest) = @_;
    114   my $rsrcip = revaddr($ip);
    115   my $rdstip = revaddr($dest);
    116   return("${rsrcip}.${port}.${rdstip}.ip-port.exitlist.torproject.org");
    117 }
    118 
    119 sub torel_check {
    120   my ($ip, $port, $dest) = @_;
     106
     107# Construct a DNSEL query from a source address and destination address/port.
     108# IP addresses should be in dotted-decimal notation.
     109sub build_query {
     110  my ($srcip, $dstip, $dstport) = @_;
     111
     112  # reverse address octets
     113  ($srcip, $dstip) = map { join '.', reverse split /\./ } $srcip, $dstip;
     114
     115  "$srcip.$dstport.$dstip.ip-port.exitlist.torproject.org.";
     116}
     117
     118sub query_exitlist {
     119  my ($srcip, $dstip, $dstport) = @_;
     120
    121121  my $res = Net::DNS::Resolver->new;
    122   my $qh = torel_qh($ip, $port, $dest);
    123   my $pkt;
    124122
    125123  # uncomment this line to query the server directly...
     
    132130
    133131  # perform DNS query
    134   if (! ($pkt = $res->search($qh)) ) {
    135     if ($res->errorstring eq "NXDOMAIN") {
    136       # response but no answer.  does not appear to be Tor exit.
    137       return(0);
     132  if (defined(my $pkt = $res->send(build_query $srcip, $dstip, $dstport))) {
     133    if (grep $_->type eq 'A', $pkt->answer) {
     134      # an A record was returned: this is a Tor exit node
     135      return 1;
     136    } elsif ($pkt->header->rcode eq 'NXDOMAIN') {
     137      # NXDOMAIN: this is not a Tor exit node
     138      return 0;
    138139    }
    139     # search failed: no response or other problem...
    140     return(-1);
    141140  }
    142   if (! defined (($pkt->answer)[0])) {
    143     # response but no answer section.  does not appear to be Tor exit.
    144     # (this should only happen when authority sections are provided without answer)
    145     return(0);
    146   }
    147   # is Tor exit
    148   return(1);
     141
     142  # the DNS query failed or something unexpected was returned
     143  return undef;
    149144}
    150145
    151146# defaults, get options...
    152 my $ip = "82.227.101.236";
    153 my $port = 80;
    154 my $dest = "4.3.2.1";
    155 my $pstatus = GetOptions (
    156  "ip=s" => \$ip,
    157  "dest=s" => \$dest,
    158  "port=i" => \$port);
    159 
     147my $srcip = "82.227.101.236";
     148my $dstip = "1.2.3.4";
     149my $dstport = 80;
     150my $pstatus = GetOptions(
     151 "srcip=s"   => \$srcip,
     152 "dstip=s"   => \$dstip,
     153 "dstport=i" => \$dstport
     154);
     155
     156$| = 1;
    160157# perform the lookup...
    161 print "Querying Tor DNS Exit List for IP $ip to destination $dest at port $port ... ";
    162 my $result = torel_check($ip, $port, $dest);
     158print "Querying Tor DNS Exit List for IP $srcip to destination $dstip at port $dstport ... ";
     159my $result = query_exitlist $srcip, $dstip, $dstport;
    163160print "Done.\n";
    164161
    165 if ($result < 0) {
    166   print "DNS query failed due to timeout or other problem.\n";
    167 }
    168 elsif ($result) {
    169   print "$ip is a known Tor exit to $dest at port $port.\n";
    170   exit(1);
    171 }
    172 else {
    173   print "$ip does not appear to be a Tor exit.\n";
    174 }
    175 exit (0);
     162if ($result) {
     163  print "$srcip is a known Tor exit to $dstip at port $dstport.\n";
     164} elsif (defined $result) {
     165  print "$srcip does not appear to be a Tor exit.\n";
     166} else {
     167  print "DNS query failed or an unexpected DNS response was received.\n";
     168  exit 1;
     169}
    176170}}}
    177171
     
    180174# using defaults
    181175torelcheck.pl
    182 Querying Tor DNS Exit List for IP 82.227.101.236 to destination 4.3.2.1 at port 80 ... Done.
    183 82.227.101.236 is a known Tor exit to 4.3.2.1 at port 80.
     176Querying Tor DNS Exit List for IP 82.227.101.236 to destination 1.2.3.4 at port 80 ... Done.
     17782.227.101.236 is a known Tor exit to 1.2.3.4 at port 80.
    184178
    185179# using explicit check
    186 torelcheck.pl --ip 71.111.92.174 --port 80 --dest 66.135.40.74
     180torelcheck.pl --srcip 71.111.92.174 --dstip 66.135.40.74 --dstport 80
    187181Querying Tor DNS Exit List for IP 71.111.92.174 to destination 66.135.40.74 at port 80 ... Done.
    18818271.111.92.174 does not appear to be a Tor exit.