Skip to content

Scapy

Scapy is a powerful interactive packet manipulation library written in Python. Scapy is able to forge or decode packets of a wide number of protocols, send them on the wire, capture them, match requests and replies, and much more.

About

Scapy는 네트워크 패킷을 보내고, 훔치고 또는 패킷을 위조할 수 있는 파이썬(Python) 프로그램이다. 이러한 기능은 네트워크를 점검하거나 스캔하거나 공격하도록 할 수 있다. 다시 말해 Scapy는 아주 강력한 패킷 조작 프로그램이라는 것이다.

GPL 라이선스 인듯함.

Stripping tcpdump payload

from scapy.all import *

INFILE = 'sample.pcap'
OUTFILE = 'stripped.pcap'

paks = rdpcap(INFILE)

for pak in paks:
    pak[TCP].remove_payload()

wrpcap(OUTFILE, paks)

PCAP 파일 재생

네트워크 트래픽을 재현하기 위해 PCAP 파일을 재생할 수 있습니다.

from scapy.all import rdpcap, sendp

packets = rdpcap("example.pcap")
for packet in packets:
    sendp(packet)

Network Mesh Drawing

라이브러리 설치:

pip install scapy networkx matplotlib
  • scapy - 네트워크 스캔 도구
  • networkx - 그래프 생성을 위한 라이브러리
  • matplotlib - 시각화

네트워크 스캔으로 이웃 장치 찾기:

from scapy.all import ARP, Ether, srp

def scan_network(ip_range="192.168.1.0/24"):
    """현재 네트워크의 IP 대역에서 장치를 탐색합니다."""
    arp = ARP(pdst=ip_range)
    ether = Ether(dst="ff:ff:ff:ff:ff:ff")
    packet = ether / arp
    result = srp(packet, timeout=2, verbose=0)[0]

    devices = []
    for sent, received in result:
        devices.append({'ip': received.psrc, 'mac': received.hwsrc})
    return devices

Mesh 그래프 구성 및 시각화:

import networkx as nx
import matplotlib.pyplot as plt

def draw_mesh(devices):
    G = nx.Graph()
    for device in devices:
        G.add_node(device['ip'])

    # Mesh 연결: 모든 노드를 서로 연결
    for i in range(len(devices)):
        for j in range(i + 1, len(devices)):
            G.add_edge(devices[i]['ip'], devices[j]['ip'])

    plt.figure(figsize=(10, 8))
    nx.draw_networkx(G, with_labels=True, node_color='skyblue', node_size=1500, font_size=10)
    plt.title("Network Mesh Graph")
    plt.axis("off")
    plt.show()

실행 예시:

if __name__ == "__main__":
    devices = scan_network("192.168.0.0/24")  # 본인 IP 대역 확인 필요
    draw_mesh(devices)
  • scapy를 사용한 ARP 스캔은 sudo 권한이 필요할 수 있습니다.
  • 방화벽에 따라 일부 장치가 탐지되지 않을 수 있습니다.

See also

Favorite site