使用 scapy 修改接收到的数据包 src/dst

Modifying received packet src/dst using scapy

我正在尝试嗅探我的 wifi 以获取接收到的数据包。当我收到一个 ftp 数据包时,我将源和目标(Mac 地址、IP 地址和端口)颠倒过来,然后再次发回。

然而,当我检查wireshark时,我发现发送的修改后的数据包有一个错误“[Malformed Packet] [ETHERNET FRAME CHECK SEQUENCE INCORRECT]”,尽管我在发回数据包之前删除了校验和。下面是我定义的修改接收数据包的函数 src/dst:

def modify_packet(pkt):
    source_mac_address = pkt[Ether].src
    destination_mac_address = pkt[Ether].dst
    destination_address = pkt[IP].dst
    source_address = pkt[IP].src
    destination_port = pkt[TCP].dport
    source_port = pkt[TCP].sport


    pkt[Ether].dst = source_mac_address
    pkt[Ether].src = destination_mac_address
    pkt[IP].dst = source_address
    pkt[IP].src = destination_address
    pkt[TCP].dport = source_port
    pkt[TCP].sport =  destination_port

    del pkt[IP].chksum
    del pkt[TCP].chksum

    send(pkt)
return

Scapy 尚未实现以太网校验和,因为它们不可预测:如 wireshark's doc

所述

Most Ethernet interfaces also either don't supply the FCS to Wireshark or other applications, or aren't configured by their driver to do so; therefore, Wireshark will typically only be given the green fields, although on some platforms, with some interfaces, the FCS will be supplied on incoming packets.

如果你知道一个数据包有一个校验和,它可能作为一个 Padding 层存在于你正在处理的 Scapy 数据包的末尾。如果是这种情况,您可能可以毫无问题地删除它。

你可以试试

if Padding in pkt:
    pkt[Padding].underlayer.remove_payload()

删除它。

另请注意:当您处理第 2 层数据包(在本例中为以太网帧)时,您需要使用 sendp() 而不是 send()