본문 바로가기
IT/보안

[Python] pcap 파일 마스킹 소스코드

from scapy.all import rdpcap, wrpcap, IP

def mask_ip(ip):
    # IP 주소의 마지막 옥텟을 0으로 변경하여 C클래스까지 마스킹
    parts = ip.split('.')
    if len(parts) == 4:
        parts[-1] = '92'
        return '.'.join(parts)
    return ip

def mask_pcap(input_file, output_file, target_ips):
    packets = rdpcap(input_file)
    for packet in packets:
        if IP in packet:
            if packet[IP].src in target_ips:
                packet[IP].src = mask_ip(packet[IP].src)
            if packet[IP].dst in target_ips:
                packet[IP].dst = mask_ip(packet[IP].dst)
    # 수정 패킷 파일 저장
    wrpcap(output_file, packets)

# 파일 경로 설정
input_file = ''
output_file = ''

# 마스킹할 IP 주소 리스트 설정
target_ips = ['']

# IP 주소 마스킹 함수 호출
mask_pcap(input_file, output_file, target_ips)