首页 > 解决方案 > 用 scapy 回复数据包

问题描述

对于 POC,我需要创建一个 MITM 设置,在该设置中我将侦听接口上的 ICMP 流量,并且对于收到的所有 ping 命令,我将发送自己的回复。到目前为止,使用 python scapy,我已经能够拦截所有 ICMP 数据包。如何阻止实际的 ping 回复数据包并发送我自己的数据包作为回复?

我的代码看起来像这样

import scapy.all as scapy
import socket
from scapy.arch import get_if_hwaddr
from scapy.interfaces import get_if_list
from scapy.layers import http
from scapy.layers.inet import TCP, ICMP, IP
from scapy.layers.l2 import Ether
from uuid import getnode as get_mac

def sniffer(interface):
    scapy.sniff(iface=interface, store=False, prn=process_packet)

def process_packet(packet):
    if packet.haslayer(ICMP):
        print("DUMP\n")
        print(packet.show(dump=True))
        print(packet[Ether].src)
        print(Ether().src)
        if packet[Ether].src == Ether().src:
            print("OUTGOING PACKET")
        else:
            print("INCOMING PACKET")


interface = "Wi-Fi"
sniffer(interface)

标签: networkingscapyman-in-the-middle

解决方案


由于 ICMP 回复是由主机的 IP 堆栈发送的,因此您必须找到一个技巧来阻止它发送回复。如果您正在使用 linux 系统,例如,您可以:

  • 使 IP 堆栈忽略回显请求:
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
  • 配置防火墙,使得只能发送 scapy 发送的 ICMP 回复。例如,您可以将XXX您的 ICMP 回复的有效负载放在您的 scapy 脚本中,并让防火墙只接受此类 ICMP 回复数据包。

推荐阅读