简体   繁体   English

如何将 mac os x en1 接口置于监视模式以与 python3 scapy 一起使用?

[英]How can I put mac os x en1 interface into monitor mode to use with python3 scapy?

On my mac the wireless interface is the en1 interface.在我的 mac 上,无线接口是 en1 接口。 I can put the interface into monitor mode using mac's airport application but then it doesn't work with the scapy module when i use python 3. How can i make this work?我可以使用 mac 的机场应用程序将界面置于监控模式,但是当我使用 python 3 时它不能与 scapy 模块一起工作。我怎样才能使它工作?

Thanks in advance提前致谢

ifconfig output ifconfig 输出

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    options=3<RXCSUM,TXCSUM>
    inet6 ::1 prefixlen 128 
    inet 127.0.0.1 netmask 0xff000000 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
    nd6 options=1<PERFORMNUD>
    media: autoselect (none)
    status: inactive
fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
    lladdr 00:3e:e1:ff:fe:0f:0a:4a 
    nd6 options=1<PERFORMNUD>
    media: autoselect <full-duplex>
    status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    inet6 fe80::7ed1:c3ff:fe6e:eeda%en1 prefixlen 64 scopeid 0x6 
    inet 192.168.1.2 netmask 0xffffff00 broadcast 192.168.1.255
    nd6 options=1<PERFORMNUD>
    media: autoselect
    status: active
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    options=60<TSO4,TSO6>
    media: autoselect <full-duplex>
    status: inactive
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
    media: autoselect
    status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1452
    inet6 fe80::18b8:64ff:fec8:85%awdl0 prefixlen 64 scopeid 0x9 
    nd6 options=1<PERFORMNUD>
    media: autoselect
    status: active
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=63<RXCSUM,TXCSUM,TSO4,TSO6>
    Configuration:
        id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
        maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
        root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
        ipfilter disabled flags 0x2
    member: en2 flags=3<LEARNING,DISCOVER>
            ifmaxaddr 0 port 7 priority 0 path cost 0
    nd6 options=1<PERFORMNUD>
    media: <unknown type>
    status: inactive

Python Script To Detected Packets (After putting en1 into mon mode using airport)检测数据包的 Python 脚本(使用机场将 en1 置于 mon 模式后)

from scapy.all import *

def pktIdentifier(pkt):
    if pkt.haslayer(Dot11Beacon):
        print ("[+] Detected 802.11 Beacon Frame")
    elif pkt.haslayer(Dot11ProbeReq):
        print ("[+] Detected 802.11 Probe Frame")
    elif pkt.haslayer(TCP):
        print ("[+] Detected TCP Packet")
    elif pky.haslayer(UDP):
        print ("[+] Detected UDP Packet")

conf.iface = 'en1'
sniff(prn=pktIdentifier)

Output of conf.route conf.route 的输出

Network         Netmask         Gateway         Iface           Output IP
0.0.0.0         0.0.0.0         192.168.0.1     en1             192.168.0.7    
127.0.0.0       255.0.0.0       0.0.0.0         lo0             127.0.0.1      
127.0.0.1       255.255.255.255 0.0.0.0         lo0             127.0.0.1      
169.254.0.0     255.255.0.0     0.0.0.0         en1             192.168.0.7    
192.168.0.0     255.255.255.0   0.0.0.0         en1             192.168.0.7    
192.168.0.1     255.255.255.255 0.0.0.0         en1             192.168.0.7    
192.168.0.1     255.255.255.255 0.0.0.0         en1             192.168.0.7    
192.168.0.7     255.255.255.255 0.0.0.0         en1             192.168.0.7    
192.168.0.255   255.255.255.255 0.0.0.0         en1             192.168.0.7 

Short Answer: You could MonkeyPatch the _PcapWrapper_pypcap class.简短回答:您可以 MonkeyPatch _PcapWrapper_pypcap 类。 An example Code is provided below.下面提供了一个示例代码。

Slightly Longer Answer: On Mac OS X scapy sniffs on interfaces through libpcap.稍微长一点的答案:在 Mac OS X 上,scapy 通过 libpcap 嗅探接口。 Instead of calling pcap_open_live we call pcap_create , pcap_set_rfmon and pcap_activate (in this order).我们不调用pcap_open_live ,而是调用pcap_createpcap_set_rfmonpcap_activate (按此顺序)。 This will set the interface in monitor mode and start capturing.这会将接口设置为监控模式并开始捕获。 I tested the following MonkeyPatch under scapy-python3 (0.21) and macOS Sierra 10.12.6.我在 scapy-python3 (0.21) 和 macOS Sierra 10.12.6 下测试了以下 MonkeyPatch。 Make sure you run this Code with admin rights.确保您以管理员权限运行此代码。

from scapy.all import *

import scapy.arch.pcapdnet
from ctypes import POINTER, byref, create_string_buffer
from ctypes.util import find_library

class _PcapWrapper_pypcap_monkeypatched(scapy.arch.pcapdnet._PcapWrapper_pypcap):
    def __init__(self, device, snaplen, promisc, to_ms):
        self.errbuf = create_string_buffer(PCAP_ERRBUF_SIZE)
        self.iface = create_string_buffer(device.encode('ascii'))

        #self.pcap = pcap_open_live(self.iface, snaplen, promisc, to_ms, self.errbuf)

        STRING = c_char_p

        _lib_name = find_library("pcap")
        if not _lib_name:
            raise OSError("Cannot fine libpcap.so library")
        _lib=CDLL(_lib_name)


        pcap_create = _lib.pcap_create
        pcap_create.restype = POINTER(pcap_t)
        pcap_create.argtypes = [STRING, STRING]

        pcap_set_rfmon = _lib.pcap_set_rfmon
        pcap_set_rfmon.restype = c_int
        pcap_set_rfmon.argtypes = [POINTER(pcap_t), c_int]

        pcap_activate = _lib.pcap_activate
        pcap_activate.restype = c_int
        pcap_activate.argtypes = [POINTER(pcap_t)]


        self.pcap = pcap_create(self.iface, self.errbuf)
        pcap_set_rfmon(self.pcap, 1)
        pcap_activate(self.pcap)
        self.header = POINTER(pcap_pkthdr)()
        self.pkt_data = POINTER(c_ubyte)()
        self.bpf_program = bpf_program()

scapy.arch.pcapdnet._PcapWrapper_pypcap = _PcapWrapper_pypcap_monkeypatched

def pktIdentifier(pkt):
    if pkt.haslayer(Dot11Beacon):
        print("[+] Detected 802.11 Beacon Frame")
    elif pkt.haslayer(Dot11ProbeReq):
        print("[+] Detected 802.11 Probe Frame")

sniff(iface="en0", prn=pktIdentifier)

When using the sniff function setting monitor=True on Mac OS Catalina always works for me.在 Mac OS Catalina 上使用嗅探功能设置monitor=True ,总是对我有用。 Example: scapy.all.sniff(iface='en0, monitor=True) then obviously what ever other functions you want.示例: scapy.all.sniff(iface='en0, monitor=True)那么显然你想要什么其他功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM