跳转到主要内容

为您的Python项目测试hpfeeds提供辅助工具

项目描述

pytest-hpfeeds

pytest-hpfeeds 是一个用于帮助在 hpfeeds 代理上对蜜罐进行烟雾/集成测试的样板代码集合。它利用 pytest-docker-tools 在 docker 内运行测试代理。它提供了一个 hpfeeds_client 修复程序,为您的 pytest 提供一个连接到该代理的客户端。

hpfeeds_broker

此软件包提供了一个 hpfeeds_broker 修复程序。通过从测试中引用此修复程序,pytest-hpfeeds 将在测试之前自动启动一个代理(在容器中),并在测试完成后销毁它。

def test_my_broker(hpfeeds_broker):
    assert hpfeeds_broker.ips.primary is not None

默认情况下,代理配置了一个单一用户(test,密码为 test)和一个名为 test 的单一频道。

hpfeeds_client

此软件包还提供了一个 hpfeeds_client 修复程序。这是一个已连接到您的代理的 hpfeeds.asyncio.ClientSession 实例。因为客户端依赖于 hpfeeds_broker,您无需引用它,pytest 仍会自动根据需要启动和停止代理。

async def test_my_client(hpfeeds_client):
    hpfeeds_client.subscribe('test')
    hpfeeds_client.publish('test', 'hello')
    assert await hpfeeds_client.read() == ('test', 'test', b'hello')

hpfeeds_broker_channels

您可以在您的 conftest.py 中实现此修复程序,以更改您的代理知道的频道。

import pytest

@pytest.fixture()
def hpfeeds_broker_channels():
    return ["cowrie.sessions"]

async def test_my_client(hpfeeds_client):
    hpfeeds_client.subscribe('cowrie.sessions"')
    hpfeeds_client.publish('cowrie.sessions"', 'hello')
    assert await hpfeeds_client.read() == ('test', 'cowrie.sessions"', b'hello')

在实际中测试蜜罐

您已打包了一个蜜罐,并希望编写一个端到端测试以确保其按预期工作。

如果您当前目录中有一个带有 Dockerfile 的蜜罐,您可以编写一个类似这样的 conftest.py

import pathlib

from pytest_docker_tools import image_or_build

CURRENT_DIR = pathlib.Path(__file__).parent

image = image_or_build(
    environ_key='IMAGE_ID',
    path=str(CURRENT_DIR),
)

honeypot = container(
    image=image,
    environment={
        "OUTPUT_HPFEEDS_HOST": "{hpfeeds_broker.ips.primary}",
        "OUTPUT_HPFEEDS_PORT": "20000",
        "OUTPUT_HPFEEDS_IDENT": "test",
        "OUTPUT_HPFEEDS_SECRET": "test",
        "OUTPUT_HPFEEDS_CHANNEL": "test",
    },
    ports={"8443/tcp": None},
    user="nobody",
    read_only=True,
)

要了解更多关于这个工具的功能,您应该阅读 pytest-docker-tools 的 README。但一些关键点包括:

  • 变量会自动与 pytest 的 fixtures 进行插值。因此,"{hpfeeds_broker.ips.primary}" 解析 hpfeeds_broker fixture(导致启动一个临时的代理容器)并获取其主 IP 以传递给您的诱饵镜像。
  • image fixture 允许您测试现有的镜像(本地存在的镜像)。build fixture 允许您进行迭代开发 - 每次运行测试时,它实际上都会执行 docker build。有时您两者都需要。您希望您的开发环境使用 build fixture,但您的发布管道应该使用 image fixture 以测试将部署的确切镜像(逐位)。这正是 image_or_build fixture 的用途。如果您的 CI 管道设置了 IMAGE_ID 环境变量,则将测试现有镜像。否则,pytest 将构建一个新的镜像。

现在要测试这个诱饵,您可以编写一个测试

import json

import httpx


async def test_honeypot_logs_data(honeypot, hpfeeds_client):
    hpfeeds_client.subscribe("test")

    ip, port = honeypot.get_addr("8443/tcp")

    # Simulate simulating an attack on the honeypot
    async with httpx.AsyncClient() as client:
        response = await client.get(f"http://{ip}:{port}/some-endpoint")
        assert r.status_code == 200

    ident, channel, event = await hpfeeds_client.read()

    # Verify the event is correct and that the structure hasn't changed
    assert json.loads(event) == {
        "event": "http.get",
        # ....
    }

通过使用 pytest-hpfeedspytest-docker-tools,构建和启动您的容器化诱饵并将其连接到 hpfeeds 代理的大部分繁重工作都隐藏起来了。您可以将注意力集中在模拟对诱饵的攻击并验证 hpfeeds 输出,这样就可以在快速部署到生产环境的同时,不会回退您的事件处理后端。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分布

pytest_hpfeeds-0.2.8.tar.gz (7.6 kB 查看哈希值)

上传时间

构建分布

pytest_hpfeeds-0.2.8-py3-none-any.whl (8.4 kB 查看哈希值)

上传时间 Python 3

支持者: