python-3.x - Python Pyramid 捕获空字节攻击

标签 python-3.x security pyramid

我有一个带有 fail2ban 的 Pyramid web 应用程序,设置为监禁十个连续的 404 状态(即探测漏洞的机器人)、Sentry 错误日志记录,据我所知,没有安全漏洞。然而,每隔几天我就会收到一个由空字节攻击引起的 502 的通知。这是无害的,但它变得非常令人厌烦,结果我忽略了一个奇怪但合法的人类用户生成的 502 状态。

Pyramid 中的空字节攻击,在我的设置中,在 url 处引发 URLDecodeError('utf-8' 编解码器无法解码位置 16 中的字节 0xc0:无效的起始字节)调度级别,因此不会路由到 notfound_view_config 装饰 View 。

有什么方法可以在 Pyramid 的请求中捕获 %EF/%BF 还是我应该在 Apache 中阻止它们?

最佳答案

Comment by Steve Piercy converted into an Answer: A search in the Pyramid issue tracker yields several related results. The first hit provides one way to deal with it.

简而言之, View 构造器类 exception_view_config(ExceptionClass, renderer) 捕获它的行为类似于 notfound_view_configforbidden_​​view_config(未通过与 view_config 对比声明的路由。

因此 404 View 可能如下所示:

from pyramid.view import notfound_view_config
from pyramid.exceptions import URLDecodeError
from pyramid.view import exception_view_config

@exception_view_config(context=URLDecodeError, renderer='json')
@notfound_view_config(renderer='json')
def notfound_view(request):
    request.response.status = 404
    return {"status": "error"}

这可以通过访问浏览器 http://0.0.0.0:👾👾/%EF%BF(其中 👾👾 是提供服务的端口)来测试。

但是,还有两个额外的注意事项。

  • 它不能很好地与调试工具栏(本地配置 ini 文件中的 pyramid.includes = pyramid_debugtoolbar)一起使用。
  • 此外,如果访问任何动态属性(如 request.path_info),则会引发错误。因此,要么响应被最小化格式化,要么 request.environ['PATH_INFO'] 在 View 中的任何操作(例如使用数据等)之前被分配一个新值。 但是, View 调用发生在引发 debugtoolbar 错误之后,因此即使 request.environ['PATH_INFO'] = 'hacked',第一点仍然成立。

奖金

由于这无疑是一次攻击,因此可以对其进行自定义以与 fail2ban 配合使用以阻止黑客 IP 为 described here。通过使用 unique status code ,例如 418,在第一次出现时。

关于python-3.x - Python Pyramid 捕获空字节攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67901041/

相关文章:

security - Mandrill Webhooks - 安全

python - 如何在Python上否定match_param? ( Pyramid )

python - SQL Alchemy 和 Pyramid 能否处理日期有效场景

python - 带有悬停按钮的顶层窗口和顶层窗口本身

python-3.x - Azure 计算机视觉 API 中从本地存储的图像文件进行手写识别

python - 为什么 tkinter.ttk Combobox 仅在初始选择时清除显示值?

python - 在 python 中显示原始图像像素而不是掩码

apache - 如何使用 .htaccess 在 apache 上启用 Expect-Ct

algorithm - 使用哈希在数据库中存储密码的替代方法

python - 如何使用 Pyramid(Python 框架)模拟 mod_rewrite 的 url 别名功能?