网络数据包sniffer:使用Python处理以太网框架(Mac Src& dest地址)
我正在尝试简单地将我的以太网框架在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()
但是应该是我的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:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在Linux上,如果按照root运行,则完全基于您的代码对我有用:
这给了我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:
This gives me the correct MAC address of my WiFi card. The EtherType (proto) in my sample
eth_packet
should have beenETH_P_IP
(IPv4 packet) which is defined as0x0800
, but I got8
instead. So it seems you can remove thesocket.htons()
call to get the correct value.