0

我是 Python 的新手,使用 *scapy** 库时遇到以下问题。在这里您可以找到整个代码(但我认为它并不那么重要,因为错误位于特定行:https ://github.com/AndreaNobili/replace_download/blob/master/replace_download.py )

Python 2项目中,我有以下两行:

modified_packet = set_load(scapy_packet, "HTTP/1.1 301 Moved Permanently\nLocation: https://www.rarlab.com/rar/wrar590.exe\n\n")

# Replace the original packet payload with the packet forget by scapy:
packet.set_payload(str(modified_packet))

这是我的set_load()函数的代码:

def set_load(packet, load):
    #pdb.set_trace()
    print("set_load() START")

    # When the victim try to download a ".exe" file he\she is redirected to this other ".exe" link:
    packet[scapy.Raw].load = load
    # The value of the following fields are changed because the file is changed, they will be removed and
    # scapy automatically recalculate the values of these fields inserting the correct values:
    del packet[scapy.IP].len
    del packet[scapy.IP].chksum
    del packet[scapy.TCP].chksum
    return packet

所以基本上我正在使用scapy伪造一个数据包,最后我将原始数据包变量的有效负载设置为Scapy伪造的有效负载:

packet.set_payload(str(modified_packet))

注意:数据包变量不是**scapy数据包,而是使用netfilterqueue获得的数据包

使用Python 2运行我的脚本可以正常工作,但使用Python 3最后一行给我以下错误:

TypeError: Argument 'payload' has incorrect type (expected bytes, got str)
> /root/Documents/PycharmWS/replace_download/replace_download.py(61)process_packet()
-> packet.set_payload(str(modified_packet))  

因此,我将scapy数据包转换为字符串,然后设置原始netfilterqueue数据包的有效负载,但它似乎需要一个字节

我该如何解决这个问题?我错过了什么?

另一个疑问是:为什么 Python 2 运行良好?我怀疑 Python 2 使用的netfilterqueue依赖版本与 Python 3 使用的版本略有不同,并且在旧版本中需要一个字符串而不是字节参数。这个推理是正确的还是我错过了什么?

4

1 回答 1

1

Python 3 在网络上使用“字节”。而不是使用str(),使用bytes()。看看http://python-future.org/compatible_idioms.html#strings-and-bytes可以很好地比较你应该在 Py3 和 Py2 上做什么。

在你的情况下,只是做

packet.set_payload(bytes(modified_packet))
于 2020-04-08T11:49:44.943 回答