Python Network Packet Sniffer Tutorial – 3 – Capturing Traffic

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

24 responses to “Python Network Packet Sniffer Tutorial – 3 – Capturing Traffic”

  1. Muhammad Hasnain Avatar

    Can someone tell me, how did he refresh the browser while checking if program works fine till here.

  2. Martin Gatto Avatar

    hi i'm trying to make the tutorial, but nothing happens when i run the code. I don`t understand why.

    S.O: OSx 10.12.6 (16G29)
    i.d.e: PyCharm 2016.2.3

    code: 
    ============================
    import socket
    import struct
    import textwrap

    def main():
        conn = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.ntohl(3))

        while True:
            raw_data, addr = conn.recvfrom(65536)
            dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
            print('nEthernet Frame:')
            print('Destination: {}, Source: {}, Protocol: {}, Data{}'.format(dest_mac, src_mac, eth_proto, data))

    # unpack ethernet frame

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

    # Return properly formatted MAC address (ie AA;BB;CC;DD;EE;FF

    def get_mac_addr(bytes_addr):
        bytes_str = map('{:02X}'.format, bytes_addr)
        return ':'.join(bytes_str).upper()

    main()

    =========================

    the app runs, but dont capture any package from the socket. 

    some one can help me ?

  3. sudan suwal Avatar

    hey I want to capture the youtube requests using python to capture video urls … HOw can I do it>????

  4. Dipen bhatt Avatar

    For those facing problem on unpacking the ethernet header in python 2.7
    Just use this simple code

    def unpack_ethernet_frame(data):
    unpacked_data=struct.unpack('! 6B 6B H',data[:14])
    dest_mac=':'.join(map('{:02x}'.format,unpacked_data[0:6]))
    src_mac=':'.join(map('{:02x}'.format,unpacked_data[6:12]))
    proto=unpacked_data[12]
    return dest_mac,src_mac,socket.htons(proto),data[14:]

  5. Misael Gallegos Avatar

    I got the following error in this line of code:

        conn = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.ntohs(3))

    OSError: [WinError 10043] The requested protocol has not been configured into the system, or no implementation for it exists

  6. Håkan Sjöberg Avatar

    Hi!
    I'm using Windows 10, Anaconda, python 3.x something.
    I have changed my script to fit with Windows, and the only thing that remains is to start the script as administrator. Does someone know how to do that?

  7. Suraj kumar Mahto Avatar

    I'm getting this error. Plz help

    C:Python34python.exe F:/sniffer/sniffer.py
    Traceback (most recent call last):
    File "F:/sniffer/sniffer.py", line 27, in <module>
    main()
    File "F:/sniffer/sniffer.py", line 7, in main
    conn = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.ntohs(3))
    File "C:Python34libsocket.py", line 126, in _init_
    socket.socket.__init_(self, family, type, proto, fileno)
    OSError: [WinError 10043] The requested protocol has not been configured into the system, or no implementation for it exists

  8. Zohar Cochavi Avatar

    Should I bind the socket to a port? And if so, which port?

  9. HYPED247 Avatar

    I have Lenix and it's not excepting AF_PACKET?How can I fix this?

  10. Александр Жуков Avatar

    Bucky how could you? We all have windows and cannot use AF_PACKE you have to help us

  11. Yogigo raindgdh Avatar

    I got this error
    conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
    AttributeError: module 'socket' has no attribute 'AF_PACKET'

  12. omri Fainaro Avatar

    raw_data, addr = conn.recvfrom(1500)
    socket.error: [Errno 10022] An invalid argument was supplied
    help me!

  13. matthew taormina Avatar

    Traceback (most recent call last):
    File "C:/Python27/packet sniffer.py", line 22, in <module>
    main()
    File "C:/Python27/packet sniffer.py", line 6, in main
    conn = socket.socket(socket.AF_INET, socket.SOCK_RAW,socket.IPPROTO_IP)
    File "C:Python27libsocket.py", line 191, in _init_
    _sock = _realsocket(family, type, proto)
    error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

  14. Aashiq Prince Avatar

    socket.error: [Errno 10022] An invalid argument was supplied
    raw_data,addr = conn.recvfrom (65536)

  15. D. Refaeli Avatar

    I also got the [WinError 10043] error. After reading the other comments and changing the conn to: "conn = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)" I still get the following error:

    Traceback (most recent call last):
    File "C:/Python34/WorkDir/PacketSniffer/sniffer.py", line 93, in <module>
    main()
    File "C:/Python34/WorkDir/PacketSniffer/sniffer.py", line 27, in main
    raw_data, addr = conn.recvfrom(65535)
    OSError: [WinError 10022] An invalid argument was supplied

  16. Phobic Avatar

    File "C:UsersBenDesktoppacketS.py", line 9
    while True:
    ^
    SyntaxError: invalid syntax

  17. Július Marko Avatar

    I got this error
    PermissionError: [Errno 1] Operation not permitted
    File "/usr/lib/python3.4/socket.py", line 126, in _init_
    socket.socket.__init_(self, family, type, proto, fileno)

  18. the Decoy Avatar

    my protocol: ______ shows a lot higher numbers then you. is this because i use windows?

  19. Saad Avatar

    found the solution for

    ValueError: Unknown format code 'x' for object of type 'str'

    use python 3.x

    for example in ubuntu: (sudo) python3 sniffer.py

  20. JazzJazzy Avatar

    never heard of vertical mouse before lol, hope you get better awesome vid.

  21. Nuclear Cupcake65 Avatar

    I get this error when I run the program:

    Traceback (most recent call last):
    File "C:/Users/Nucle/Desktop/Packet Sniffer/Python Packet Sniffer.py", line 29, in <module>
    main()
    File "C:/Users/Nucle/Desktop/Packet Sniffer/Python Packet Sniffer.py", line 7, in main
    conn = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.ntohs(3))
    File "C:UsersNucleAppDataLocalProgramsPythonPython35-32libsocket.py", line 134, in _init_
    socket.socket.__init_(self, family, type, proto, fileno)
    OSError: [WinError 10043] The requested protocol has not been configured into the system, or no implementation for it exists

    all the code is the same, except socket.AF_PACKET I changed to socket.AF_INET

    I'm on Windows, using Python 3.5.1

  22. peril Avatar

    bytes_str = map('{:02x}'.format, bytes_addr)
    ValueError: Unknown format code 'x' for object of type 'str'

    any ideas? Is it '! 6s 6s ' If it's formatting as a string on capture what do?

  23. Aayush Gupta Avatar

    When I run the program, I get this error:

    File "C:/Users/Aayush/PycharmProjects/Python-Packet-Sniffer/sniffer_demo.py", line 7, in main
    conn = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.ntohs(3))
    File "C:UsersAayushAppDataLocalProgramsPythonPython35libsocket.py", line 134, in _init_
    socket.socket.__init_(self, family, type, proto, fileno)
    OSError: [WinError 10043] The requested protocol has not been configured into the system, or no implementation for it exists

Leave a Reply

Your email address will not be published. Required fields are marked *