[파이썬] socket 네트워크 스니핑

In this blog post, we will explore how to perform network sniffing using Python’s socket module. Network sniffing, also known as packet sniffing, is the process of capturing and analyzing network traffic to gain insights into the data being transmitted over a network.

Prerequisites

To follow along with the examples in this blog post, make sure you have Python installed on your machine. You can download the latest version of Python from the official website - https://www.python.org/downloads.

Socket Programming Basics

Before diving into network sniffing, let’s briefly understand the basics of socket programming. A socket is an endpoint for sending or receiving data across a computer network. Socket programming allows us to create, connect, send, and receive data through sockets.

Sniffing Network Traffic

To sniff network traffic, we need to create a raw socket and bind it to a network interface. A raw socket allows us to capture packets at the network layer, providing us with access to the complete packet including the headers.

Here’s an example of how to sniff network traffic using the socket module in Python:

import socket

# Create a raw socket
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003))

# Bind the socket to a network interface (e.g., eth0, wlan0)
sock.bind(('eth0', 0))

# Continuously sniff network traffic
while True:
    packet = sock.recvfrom(65565)
    data = packet[0]

    # Process the captured packet
    print(f"Received packet: {data}")

In the above code snippet, we first create a raw socket by specifying the address family (AF_PACKET), socket type (SOCK_RAW), and protocol (0x0003 for ETH_P_ALL - all Ethernet packets). We then bind the socket to a specific network interface (e.g., eth0).

Inside the loop, we continuously capture incoming network packets using the recvfrom method of the socket object. The captured packet is stored in the data variable, which can be further processed or analyzed as per your requirements.

Note: Sniffing network traffic requires administrative or root privileges.

Analyzing Captured Packets

Once we have captured the network packets, we can perform various analysis tasks such as extracting information from packet headers, filtering specific types of packets, or even building custom network monitoring applications.

Here’s a simple example of analyzing and printing the source and destination IP addresses from captured packets:

import socket
import struct

# Create a raw socket
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003))
sock.bind(('eth0', 0))

while True:
    packet = sock.recvfrom(65565)
    ethernet_header = packet[0][0:14]
    eth_header = struct.unpack('!6s6sH', ethernet_header)

    ip_header = packet[0][14:34]
    iph = struct.unpack('!BBHHHBBH4s4s', ip_header)

    source_ip = socket.inet_ntoa(iph[8])
    destination_ip = socket.inet_ntoa(iph[9])

    print(f"Source IP: {source_ip}, Destination IP: {destination_ip}")

In the above code snippet, we unpack the Ethernet and IP headers from the captured packet using the struct module. Then, we extract the source and destination IP addresses from the IP header using the inet_ntoa function.

Conclusion

Sniffing network traffic can be a powerful tool for analyzing and debugging network applications or monitoring network behavior. In this blog post, we explored how to perform network sniffing using Python’s socket module. Keep in mind that network sniffing should be done responsibly and within the legal boundaries of your environment.

Remember to practice good ethics and respect user privacy when performing network sniffing or any other activities that involve capturing and analyzing network traffic.

I hope you found this blog post helpful! Feel free to leave a comment if you have any questions or suggestions.

Happy coding!