在Python3中读取与特定端口之间的TCP数据包

2024-03-28 15:21:40 发布

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

我正在尝试使用此筛选器tcp.port == 25565复制我在Wireshark中看到的数据。我已经尝试过使用socket和pyshark,但是,我似乎找不到一个简单的教程来解释如何做到这一点

正如你可能通过端口知道的,我正在尝试解码Minecraft数据包。关于如何获取有效负载并开始解析该数据的建议将非常有用

到目前为止,我有以下代码:

from scapy.all import *

def test(pkt):
    print(pkt)

if __name__ == '__main__':
    single = sniff(filter="tcp.port == 25565", prn=test)

非常感谢您的帮助


Tags: 数据端口代码testport教程socket解码
1条回答
网友
1楼 · 发布于 2024-03-28 15:21:40

您需要sniff(filter="tcp port 25565", prn=test)

看看scapy documentation

We can add filtering to capture only packets that are interesting to us. Use standard tcpdump/libpcap syntax:

该语法在pcap-filterman page中指定

qualifiers restrict the match to a particular protocol.
Possible protos are: ether, fddi, tr, wlan, ip, ip6, arp, rarp, decnet, tcp and udp. E.g., 'ether src foo', 'arp net 128.3', 'tcp port 21',

我不认为语法在其中得到了很好的解释(或者我没有阅读正确的部分),但是正如您所看到的,tcp port 21是一个有效的过滤器,您正在寻找它。对于使用and的另一种语法,您将进一步看到:

Primitives may be combined using: A parenthesized group of primitives and operators (parentheses are special to the Shell and must be escaped).
Negation ('!' or 'not').
Concatenation ('&&' or 'and').
Alternation ('||' or 'or').

如您所见,过滤器选项(或基本体)应使用运算符分组。在本例中,您希望两者都为true,因此希望tcp and port 25565,或者tcp && port 25565

相关问题 更多 >