【问题标题】:Scapy sniff in monitor mode监控模式下的 Scapy 嗅探
【发布时间】:2015-11-09 22:20:09
【问题描述】:

我使用 scapy 编写了一个 python 脚本来嗅探我的 WIFI 网络中的 TCP 数据包,并查看两个目的地之间是否存在连接。 如果我在不处于监控模式时嗅探数据包,它会起作用,但是当我在监控模式界面上嗅探时它不起作用。

任何想法如何使它工作? 片段:

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
import time

class deferring_delete(object):
def __init__(self, d):
    self._dict = d
def __enter__(self):
    self._deletes = set()
    return self
def __exit__(self, type, value, tb):
    for key in self._deletes:
        try:
            del self._dict[key]
        except KeyError:
            pass
    del self._deletes
def __delitem__(self, key):
    if key not in self._dict:
        raise KeyError(str(key))
    self._deletes.add(key)

packet_count = 0
packets = {}
accepted = {}
YOUR_IP = '10.0.0.1'
FILTER = "tcp and host not {0}".format(YOUR_IP) 

def handshake_status(packet):
    global packets,accepted,packet_count


    flag = packet[0][1].sprintf('%TCP.flags%')
    src_ip = packet[0][1].src
    dst_ip = packet[0][1].dst

    if flag == 'S':
        packets[packet_count] = {'src_ip': src_ip, 'dst_ip': dst_ip, 'time': time.ctime() , 'flag': flag} 
        print "%s ==> %s SYN_SENT" % (src_ip, dst_ip)
        packet_count += 1

    if flag == 'SA':
        for key , packet in packets.iteritems():
            if packet['src_ip'] == dst_ip:
                accepted[key] = packet

    if len(accepted) > 0:
        with deferring_delete(packets) as p:
            for key in accepted.keys():
                print "%s ==> %s ESTABLISHED" % (packets[key]['src_ip'], packets[key]['dst_ip'])
                del p[key]

        with deferring_delete(accepted) as a:
            for key in accepted.keys():
                del a[key]


if __name__ == '__main__':
    sniff(iface="mon0", filter=FILTER ,prn=handshake_status)

【问题讨论】:

  • 你有什么错误吗?您确定设备已启动?您是如何将其设置为监控模式的?
  • @Yoel "第 1076 行,在 getfield_and_val 中引发 AttributeError(attr) AttributeError: src" 。我使用了来自其他 linux 的 PCAP 文件并开启了监控模式

标签: python network-programming wifi scapy


【解决方案1】:

问题在于以下几行:

flag = packet[0][1].sprintf('%TCP.flags%')
src_ip = packet[0][1].src
dst_ip = packet[0][1].dst

尝试如下重写它们:

flag = packet.getlayer(TCP).sprintf('%TCP.flags%')
src_ip = packet.getlayer(IP).src
dst_ip = packet.getlayer(IP).dst

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-29
    • 2015-04-09
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多