-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathministack.py
executable file
·82 lines (67 loc) · 1.7 KB
/
ministack.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
#!/usr/bin/env python
from scapy.all import *
import threading
import time
import signal
iface_name='eth5'
iface_mac='00:02:a5:4e:53:dc'
conf.verb=0
def icmp_handler(pkt):
if ICMP in pkt and pkt[ICMP].type == 8:
reply=Ether()/IP()/ICMP()
reply.dst =pkt.src
reply.src =iface_mac
reply[IP].src =pkt[IP].dst
reply[IP].dst =pkt[IP].src
reply[IP].id =pkt[IP].id
reply[ICMP].id =pkt[ICMP].id
reply[ICMP].seq =pkt[ICMP].seq
reply[ICMP] =pkt[ICMP] #copy original ICMP header to reply
reply[ICMP].type =0 #but convert type to echo-reply
sendp(reply,iface=iface_name)
def arp_handler(pkt):
if ARP in pkt and pkt[ARP].op == 1: #if ARP and who-has
reply=Ether()/ARP()
reply.dst =pkt.src
reply.src =iface_mac
reply.type =0x806
reply.hwtype =0x1
reply.ptype =0x800
reply.hwlen =6
reply.plen =4
reply.op =2 #is-at
reply.hwsrc =iface_mac
reply.hwdst =pkt.hwsrc
reply.psrc =pkt.pdst
reply.pdst =pkt.psrc
sendp(reply,iface=iface_name)
def main():
threads=[]
t = threading.Thread(
target=sniff,
kwargs={'prn':arp_handler, 'filter':'arp', 'iface': iface_name, 'store': 0},
name='ARP'
)
threads.append(t)
t = threading.Thread(
target=sniff,
kwargs={'prn':icmp_handler, 'filter':'icmp', 'iface': iface_name, 'store': 0},
name='ICMP'
)
threads.append(t)
for worker in threads:
worker.daemon=True
print "%s worker starting" % (worker.name)
worker.start()
try:
while (42):
time.sleep(1)
signal.pause()
except KeyboardInterrupt:
print "CTRL+C caught"
for worker in threads:
print "%s worker joining main thread" % (worker.name)
worker.join(1)
sys.exit(0)
if __name__ == '__main__':
main()