Hackergame nc 类题目的 Docker 容器资源限制、动态 flag、网页终端

Overview

Hackergame nc 类题目的 Docker 容器资源限制、动态 flag、网页终端

快速入门

配置证书

证书用于验证用户 Token。请确保这里的证书文件(cert.pem)与 Hackergame 平台 配置的证书相同,这样 Hackergame 平台为每个用户生成的 Token 才可以通过这里的用户验证。

如果你仅仅想测试一下,可以使用 dynamic_flag/cert.pem 自带的证书,以及这个 Token:

1:MEUCIQCjK1QcPFro86w3bKPb5zUZZd96ocp3EZDFcwLtJxNNDAIgEPk3Orw0mE+zHLQA7e31kSFupNtG9uepz2H4EqxlKWY=

在生产环境中,请使用自己生成的证书,方法如下:

生成私钥 openssl ecparam -name secp256k1 -genkey -noout -out private.pem

生成证书 openssl req -x509 -key private.pem -out cert.pem -days 365

然后将生成的 cert.pem 文件放在 dynamic_flag/cert.pem

配置题目

如果你仅仅想测试一下示例题目,那么可以跳过此步骤。

本项目的目录结构设计为可以被 Hackergame 平台的题目导入命令 直接导入。

dynamic_flag 目录中包含了题目容器化、连接限制、动态 flag 相关的逻辑。

web_netcat 目录中包含了网页终端的逻辑。

如果仅仅是使用本项目,那么以上两个目录中的内容都不需要修改,它们也不会被 Hackergame 平台导入(因为没有 README.md 文件)。

示例题目在 example 目录中,其中的 example/docker-compose.yml 中引用了以上两个目录中的内容。你可以把 example 目录复制多份为不同的名字,它们在被导入到 Hackergame 平台后会显示为多道题目。

题目是 Docker 化的,注意每次运行题目 Docker 时 只启动一个题目的实例,通过标准输入输出交互,你的题目不需要监听端口,也不需要做任何资源限制。 参见 example/Dockerfileexample/example.py

你需要修改 example/.env 文件,针对题目的情况进行配置,包括 nc 的端口(port)、网页终端的端口(web_port)、运行时间和资源限制、flag 文件位置、动态 flag 规则、题目的容器名称等。动态 flag 的生成方法可以由你自己决定,可以使用类似 'flag{prefix_' + sha256('secret'+token)[:10] + '}' 的方案,示例中使用了 Python 的 f-string。对于多个 flag 的情况,flag_path 中路径和 flag_rule 中 Python 表达式都用 , 分隔即可。容器名称(challenge_docker_name)是 docker-compose 自动命名的,请设置为目录名 + "_challenge"。对于每一个连接,如果 Token 合法并且连接频率没有超过限制,那么你的题目容器会以指定的资源限制启动,动态生成的 flag 会被挂载到指定的路径,选手的 TCP 连接将会被连接到容器的标准输入输出上。如果你的题目需要获得用户 Token,直接读取 hackergame_token 环境变量即可。

example/README.md 是用于导入 Hackergame 平台的,里面配置的 flag 需要与 .env 中配置的 flag 相同,端口也需要进行相应修改。

运行题目

example 目录中运行 docker-compose up --build 即可,然后你可以通过 nc 127.0.0.1 10000 来连接,也可以使用 http://127.0.0.1:10001/ 的网页终端。

本项目的背景

与很多 CTF 比赛类似,USTC Hackergame 需要运行选手与服务器交互的 nc 类题目。然而 CTF 比赛中常见的做法有以下问题:

  • 通过求解 PoW 来做题目的连接限制,对新手不友好,也在某种程度上影响比赛的体验

  • pwn 题缺少真正有效的资源限制。我调研了很多开源的 Docker 化方案,也咨询了很多比赛的出题人,结论是现有的方案都无法真正防止针对性的资源耗尽攻击(所谓“搅屎”)。很多 pwn 题的部署方案会限制选手能够使用的命令,这只是增加了资源耗尽攻击的难度而已,并没有从根本上解决问题。

  • 动态 flag、监听端口很多时候是题目逻辑的一部分,而我想做到这部分逻辑对出题人是透明的,这样也可以让题目更统一、更稳定。

因为以上提到的原因,我在 Hackergame 2019 前开发了这套系统,并且在 Hackergame 2020 前进行了一些改进,但是这部分代码一直没有开源。

如今,我把这份代码以 MIT 协议开源出来,欢迎大家测试、使用和改进。

本项目的功能

  • 对用户 Token 进行验证,只有合法的 Token 才可以运行题目

  • 根据 Token 中的用户 ID 进行连接频率限制

  • 根据 Token 动态生成 flag,并自动挂载进题目 Docker

  • 限制题目的资源使用,包括限时、进程数限制、内存限制、不允许联网等

  • 保证题目的稳定性和安全性,用户无论在题目 docker 中做什么,都不会影响其他用户做题

  • 为题目提供一个网页终端,方便新手直接在网页上尝试做题,配合 Hackergame 平台可以实现 Token 的自动填充。

本项目的限制

本项目只适用于每个连接启动一个进程的题目,包括 pwn 题和其他的 nc 连接服务器类题目。

如果你的题目是一个一直运行的应用,例如 flask app,那么请自己进行 Token 的验证和动态 flag 生成。

Token 的验证方法见 dynamic_flag/front.py 中的 validate 函数。由于 Token 是非对称签名,所以证书和验证代码完全可以公开。

如果不需要进行连接限制,那么不验证 Token 的合法性也无妨。

自己实现对用户的连接限制时,注意请按用户 id 限制,不要按 Token 限制,因为签名系统不保证每个 id 只有唯一的合法签名。

已知问题

  • 证书是否过期不会被检查

  • Windows 系统上可能无法正常使用,Linux 和 macOS 经测试没有问题

Owner
USTC Hackergame
USTC Hackergame
A colony of interacting processes

NColony Infrastructure for running "colonies" of processes. Hacking $ tox Should DTRT -- if it passes, it means unit tests are passing, and 100% cover

23 Apr 04, 2022
Convenient tool to manage multiple VMs at once using libvirt

Convenient tool to manage multiple VMs at once using libvirt Installing To install the tool and its dependencies: pip install -e . Getting completion

Cedric Bosdonnat 13 Nov 11, 2022
This projects provides the documentation and the automation(code) for the Oracle EMEA WLA COA Demo UseCase.

COA DevOps Training UseCase This projects provides the documentation and the automation(code) for the Oracle EMEA WLA COA Demo UseCase. Demo environme

Cosmin Tudor 1 Jan 28, 2022
DAMPP (gui) is a Python based program to run simple webservers using MySQL, Php, Apache and PhpMyAdmin inside of Docker containers.

DAMPP (gui) is a Python based program to run simple webservers using MySQL, Php, Apache and PhpMyAdmin inside of Docker containers.

Sehan Weerasekara 1 Feb 19, 2022
A lobby boy will create a VPS server when you need one, and destroy it after using it.

Lobbyboy What is a lobby boy? A lobby boy is completely invisible, yet always in sight. A lobby boy remembers what people hate. A lobby boy anticipate

226 Dec 29, 2022
This project shows how to serve an TF based image classification model as a web service with TFServing, Docker, and Kubernetes(GKE).

Deploying ML models with CPU based TFServing, Docker, and Kubernetes By: Chansung Park and Sayak Paul This project shows how to serve a TensorFlow ima

Chansung Park 104 Dec 28, 2022
A curated list of awesome DataOps tools

Awesome DataOps A curated list of awesome DataOps tools. Awesome DataOps Data Catalog Data Exploration Data Ingestion Data Lake Data Processing Data Q

Kelvin S. do Prado 40 Dec 23, 2022
Deploying a production-ready Django project using Nginx and Gunicorn

django-nginx-gunicorn This project is for deploying a production-ready Django project using Nginx and Gunicorn. Running a local server of Django is no

Arash Sayareh 8 Jul 03, 2022
Caboto, the Kubernetes semantic analysis tool

Caboto Caboto, the Kubernetes semantic analysis toolkit. It contains a lightweight Python library for semantic analysis of plain Kubernetes manifests

Michael Schilonka 8 Nov 26, 2022
A Simple script to hunt unused Kubernetes resources.

K8SPurger A Simple script to hunt unused Kubernetes resources. Release History Release 0.3 Added Ingress Added Services Account Adding RoleBindding Re

Yogesh Kunjir 202 Nov 19, 2022
A cron monitoring tool written in Python & Django

Healthchecks Healthchecks is a cron job monitoring service. It listens for HTTP requests and email messages ("pings") from your cron jobs and schedule

Healthchecks 5.8k Jan 02, 2023
a CLI that provides a generic automation layer for assessing the security of ML models

Counterfit About | Getting Started | Learn More | Acknowledgments | Contributing | Trademarks | Contact Us -------------------------------------------

Microsoft Azure 575 Jan 02, 2023
Containerize a python web application

containerize a python web application introduction this document is part of GDSC at the university of bahrain you don't need to follow along, fell fre

abdullah mosibah 1 Oct 19, 2021
Ganeti is a virtual machine cluster management tool built on top of existing virtualization technologies such as Xen or KVM and other open source software.

Ganeti 3.0 =========== For installation instructions, read the INSTALL and the doc/install.rst files. For a brief introduction, read the ganeti(7) m

395 Jan 04, 2023
This repository contains code examples and documentation for learning how applications can be developed with Kubernetes

BigBitBus KAT Components Click on the diagram to enlarge, or follow this link for detailed documentation Introduction Welcome to the BigBitBus Kuberne

51 Oct 16, 2022
GitGoat enables DevOps and Engineering teams to test security products intending to integrate with GitHub

GitGoat is an open source tool that was built to enable DevOps and Engineering teams to design and implement a sustainable misconfiguration prevention strategy. It can be used to test with products w

Arnica 149 Dec 22, 2022
Pulumi - Developer-First Infrastructure as Code. Your Cloud, Your Language, Your Way 🚀

Pulumi's Infrastructure as Code SDK is the easiest way to create and deploy cloud software that use containers, serverless functions, hosted services,

Pulumi 14.7k Jan 08, 2023
docker-compose工程部署时的辅助脚本

okta-cmd Introduction docker-compose 辅助脚本

完美风暴666 4 Dec 09, 2021
Travis CI testing a Dockerfile based on Palantir's remix of Apache Cassandra, testing IaC, and testing integration health of Debian

Testing Palantir's remix of Apache Cassandra with Snyk & Travis CI This repository is to show Travis CI testing a Dockerfile based on Palantir's remix

Montana Mendy 1 Dec 20, 2021
Dynamic DNS service

About nsupdate.info https://nsupdate.info is a free dynamic DNS service. nsupdate.info is also the name of the software used to implement it. If you l

nsupdate.info development 880 Jan 04, 2023