Raw socket Paket-sniffer in Python 3.6 für Windows
Ich versuche zu schnuppern-Pakete, aber ich bin immer eine merkwürdige Ausgabe, und ich verstehe nicht den Grund..
So das ist mein code bitte mir helfen
(Ich bin mit Python 3.6 auf Windows 8.1)
Code:
import socket
import struct
import binascii
import textwrap
def main():
# Get host
host = socket.gethostbyname(socket.gethostname())
print('IP: {}'.format(host))
# Create a raw socket and bind it
conn = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
conn.bind((host, 0))
# Include IP headers
conn.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# Enable promiscuous mode
conn.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
while True:
# Recive data
raw_data, addr = conn.recvfrom(65536)
# Unpack data
dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
print('\nEthernet Frame:')
print("Destination MAC: {}".format(dest_mac))
print("Source MAC: {}".format(src_mac))
print("Protocol: {}".format(eth_proto))
# Unpack ethernet frame
def ethernet_frame(data):
dest_mac, src_mac, proto = struct.unpack('!6s6s2s', data[:14])
return get_mac_addr(dest_mac), get_mac_addr(src_mac), get_protocol(proto), data[14:]
# Return formatted MAC address AA:BB:CC:DD:EE:FF
def get_mac_addr(bytes_addr):
bytes_str = map('{:02x}'.format, bytes_addr)
mac_address = ':'.join(bytes_str).upper()
return mac_address
# Return formatted protocol ABCD
def get_protocol(bytes_proto):
bytes_str = map('{:02x}'.format, bytes_proto)
protocol = ''.join(bytes_str).upper()
return protocol
main()
Aus diesem code bekomme ich diese Ausgabe:
IP: 192.168.1.12
Ethernet-Frame:
Ziel-MAC -: 45:00:00:43:00:00
Quell-MAC: 40:00:2C:11:48:D3
Protokoll: 4266
Ethernet-Frame:
Ziel-MAC -: 45:00:00:42:11: E7
Quell-MAC: 00:00:80:11:00:00
Protokoll: C0A8
Ethernet-Frame:
Ziel-MAC -: 45:00:00:33:04: D6
Quell-MAC: 00:00:80:11:00:00
Protokoll: C0A8
.
.
.
Laut EtherType-Liste diese Protokolle nicht existieren, und mein traffic Analyse mit Wireshark bin ich mir sicher, dass diese MACs existieren nicht in meinem LAN
So, ich bin definitiv etwas falsch, aber ich verstehe nicht, was
Vielen Dank im Voraus
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ist der Hinweis, dass alle Ihre Ziel-Mac-Adressen beginnen mit
0x45
. Das ist das erste byte des IP-header. Also dein code ist, immer alle IP-Pakete, nicht aber die MAC-Kopfzeile für diese Bilder.Ok Dank selbi ich verstehe das problem, ich danke Ihnen sehr viel
Aber fangen Ethernet-header mit Python in windows, die Sie haben zu verwenden:
1. PyPCap
2. Dpkt
Installieren dpkt öffnen Sie einfach cmd als admin und geben Sie:
py -2 -m pip installieren dpkt
Hier Verweise von Python Docs
3. Python 2.7
Aber Sie müssen zum installieren von Python 3.4> to get pip
Und schließlich, daß der code:
Ausgabe:
Ethernet-Frame:
Ziel-MAC: 5404a6f2740c <- Meine NIC MAC
Quelle: 6459f81dc690
Protokoll: 0800 <- Richtig-Protokoll
Ethernet-Frame:
Ziel-MAC: 6459f81dc690
Quelle: 5404a6f2740c
Protokoll: 0800
Ethernet-Frame:
Ziel-MAC: 5404a6f2740c
Quelle: 6459f81dc690
Protokoll: 0800
Ich hoffe, Sie genießen nochmals vielen Dank selbie
(Sorry, ich kann nicht mehr links, die Ursache für meinen Ruf)