网络数据包sniffer:使用Python处理以太网框架(Mac Src& dest地址)

发布于 2025-02-08 08:37:31 字数 1986 浏览 2 评论 0原文

我正在尝试简单地将我的以太网框架在Python中进行操作,以便我在Python(由教程的帮助)中撰写此简单代码:

import socket
import struct


def ethernet_frame_fct(data):
    dest_mac, src_mac, proto = struct.unpack('! 6s 6s H', data[:14])
    return get_mac_addr_fct(dest_mac), get_mac_addr_fct(src_mac), socket.htons(proto), data[14:]


def get_mac_addr_fct(bytes_addr):
    bytes_str = map('{:02x}'.format, bytes_addr)
    mac_addr = ':'.join(bytes_str).upper()
    return mac_addr


def main_fct():

    # if platform == "linux" or platform == "linux2":
    #     conn = socket.socket(socket.AF_PACKET, socket.SOCKET_RAW, socket.ntohs(3))
    # if platform == "win32":
    HOST = socket.gethostbyname(socket.gethostname())  # the public network interface
    conn = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)  # create a raw socket and bind it to the public interface
    conn.bind((HOST, 0))
    conn.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)  # Include IP headers
    conn.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)  # receives all packets

    while True:
        raw_data, addr = conn.recvfrom(65536)

        dest_mac, src_mac, eth_proto, data = ethernet_frame_fct(raw_data)
        print("\n-Ethernet Frame:")
        print('\t' + "MAC addr Destination= {}, MAC addr Source= {}, Protocol= {}".format(dest_mac, src_mac, eth_proto))


#
main_fct()

Proble是我在运行该程序时得到了这些结果:

但是应该是我的MAC地址的源MAC地址根本不是我的MAC地址,而原核不是预期的标签之一。
出现:6 = TCP,17 = UDP ...等...但是17796根本不是我期望获得的价值。
关于这个时间的最后一个值,我在laptot上运行此程序时会获得不同的价值(因此WiFi更改),但我从来没有得到逻辑。

如通常的以太网框架应该看起来像这样:

我绝对不知道我在哪里错了。
几天来,我真的很困惑和陷入这个问题,因此,如果有人能够帮助我,我将非常感谢。

谢谢。

I am trying to simply get and process my ethernet frame in python to do it i wrotte this simple code in python (helped by a tutorial):

import socket
import struct


def ethernet_frame_fct(data):
    dest_mac, src_mac, proto = struct.unpack('! 6s 6s H', data[:14])
    return get_mac_addr_fct(dest_mac), get_mac_addr_fct(src_mac), socket.htons(proto), data[14:]


def get_mac_addr_fct(bytes_addr):
    bytes_str = map('{:02x}'.format, bytes_addr)
    mac_addr = ':'.join(bytes_str).upper()
    return mac_addr


def main_fct():

    # if platform == "linux" or platform == "linux2":
    #     conn = socket.socket(socket.AF_PACKET, socket.SOCKET_RAW, socket.ntohs(3))
    # if platform == "win32":
    HOST = socket.gethostbyname(socket.gethostname())  # the public network interface
    conn = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)  # create a raw socket and bind it to the public interface
    conn.bind((HOST, 0))
    conn.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)  # Include IP headers
    conn.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)  # receives all packets

    while True:
        raw_data, addr = conn.recvfrom(65536)

        dest_mac, src_mac, eth_proto, data = ethernet_frame_fct(raw_data)
        print("\n-Ethernet Frame:")
        print('\t' + "MAC addr Destination= {}, MAC addr Source= {}, Protocol= {}".format(dest_mac, src_mac, eth_proto))


#
main_fct()

The proble is that i get those results when i am running the program:
enter image description here

But the Source MAC address that should be MY mac address is not at all my MAC ADDRESS and the Protocole is not the one of an expected tag.
For exemple: 6=TCP, 17=UDP ...etc... but 17796 is not at all a value that i expected to get.
Concerning this last value for times to times i get different value as i run this programm on my laptot (so the wifi changes) but I NEVER got something logic.

As the usual ethernet frame should look like this:
enter image description here

I absolutely don't know where i am wrong.
For days i am really confused and stuck on this problem and so i will very appreciate if someone will be able to help me.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2025-02-15 08:37:31

我在Linux上,如果按照root运行,则完全基于您的代码对我有用:

import socket

ETH_P_ALL = 3
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind(('wlp4s0', 0))  # your WiFi interface, on Linux found using `ifconfig` or similar

eth_packet = s.recv(2048)  # get a sample packet
ethernet_frame_fct(eth_packet)

这给了我WiFi卡的正确MAC地址。我的样本eth_packet中的Ethertype(proto)应该是eth_p_ip(ipv4 packet),定义为 0x0800 code> 8 8 8 8 8 8 8 8 socket.htons()调用以获取正确的值。

I'm on Linux and the following code based entirely on your code works for me if run as root:

import socket

ETH_P_ALL = 3
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind(('wlp4s0', 0))  # your WiFi interface, on Linux found using `ifconfig` or similar

eth_packet = s.recv(2048)  # get a sample packet
ethernet_frame_fct(eth_packet)

This gives me the correct MAC address of my WiFi card. The EtherType (proto) in my sample eth_packet should have been ETH_P_IP (IPv4 packet) which is defined as 0x0800, but I got 8 instead. So it seems you can remove the socket.htons() call to get the correct value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文