Skip to main content

Get attack infos in attack-defense CTFs quickly to your exploits. CTF-agnostic and cached.

Project description

CTF AttackAPI - Cached and Unified!

License: MIT Python Types Python package tests PyPI version Downloads Repo size

Gather attack information quickly in your attack-defense CTF exploits!

During attack-defense CTF competitions, you have to write exploits quickly and run them on a large scale. These exploits often require information about the targets to attack (teams and sometimes usernames). This attack info is available as a big JSON file which is updated every few minutes. Downloading that file for every exploit you're firing is costing time and bandwidth.

This package fetches, parses, and caches attack info for you, so you can focus on writing exploits!

Features

  • Efficient caching between threads, processes, or containers
  • Direct access from your Python exploits (sync or async)
  • Optional REST API for exploits in other languages (with OpenAPI spec)
  • Unifies team, IP, and flag info lookup between different CTFs:

Quick-Start

See examples directory for more full scripts.

Install the package (possibly in a virtual environment):

pip install ctf-attackapi

Get attack infos for your python exploit:

from attackapi import *

# 1. Set the API URL in code (or use CTF_API environment variable)
configure("https://scoreboard.ctf.saarland/api/attack.json")
# 2. Get attack infos!
for username in attack_info().flag_id_flat("no-service", "10.32.1.2"):
  pwn("10.32.1.2", username)

List all teams that you can attack:

from attackapi import *

configure("https://scoreboard.ctf.saarland/api/attack.json")
for team in attack_info().teams:
  print(team.id, team.ip, team.name)

Get attack infos from REST API if you're not pwning in Python:

python -m attackapi.server --url "https://scoreboard.ctf.saarland/api/attack.json"
curl "http://localhost:14320/api/v1/teams"

The server has documentation on its frontpage, and here is the OpenAPI specification.

If you're not pwning in Python and dislike pip, try docker:

# edit compose.yaml and insert your CTF API URL
docker compose up -d
# visit http://localhost:14320/

Structure

  • Attack info data is retrieved and cached twice: in-memory and on disk (/tmp by default)
  • Each request goes to the caches. If the cached data is outdated, it is refreshed in the background.
  • No concurrent requests are made to the game API.
  • Game-specific decoders process the game APIs data and make it accessible.
  • You can query the data via python API from your exploits, or via REST API from other languages.
  • Relying on the disk cache is good enough for typical exploitation scenarios.

Python Library Documentation

There are different ways to get an AttackInfo object:

# 1. Functional
from attackapi import *

# Set the API URL in code (or use CTF_API environment variable)
configure("https://scoreboard.ctf.saarland/api/attack.json")
# sync:
info: AttackInfo = attack_info()
# async
info: AttackInfo = await attack_info_async()

# 2. By manually using the classes
from attackapi.sync_api import AdCtfApiSync
from attackapi.async_api import AdCtfApiAsync

api = AdCtfApiSync("https://scoreboard.ctf.saarland/api/attack.json")
info = api.attack_info()
api2 = AdCtfApiAsync("https://scoreboard.ctf.saarland/api/attack.json")
info = await api2.attack_info()

Optional parameters can be passed to the configure function or the API constructors:

  • url: str (default: CTF_API environment variable)
  • tmp_directory: str | Path (default: /tmp or OS-specific alternative)
  • lifetime: float (default: 30 seconds) - after this time, cached data is invalidated and refreshed
  • timeout: float (default: 10 seconds) - abort game API requests after this duration
  • decoder: Decoder (default: generic decoder) - custom decoder, if your game's format is different from what we've seen so far
  • aiohttp_arguments: dict - additional arguments passed to the aiohttp Session which contacts the game API

The AttackInfo class itself has these methods:

info: AttackInfo

# Get attackable teams
print(info.teams)  # list of Team objects
print(info.teams[0].id, info.teams[0].ip, info.teams[0].name)  # Team is ID, IP, and optional name
print(info.team("10.32.1.2"))  # query Team object by ID, IP, or name

# set of service names
print(info.services)

# raw flag IDs for a service and team.
# team can be ID, IP, or name. 
# Return data format is determined by game API.
print(info.flag_id_raw("servicename", "10.32.1.2"))
# => {"227": "abc", "228": "def", ...}

# Get flag IDs as string list (independent of game API format, but less precise)
print(info.flag_id_flat("servicename", "10.32.1.2"))
# => ["abc", "def"]

Server Documentation

# Simple usage:
python -m attackapi.server --help

Options:

  • --port PORT
  • --url URL: API url to get CTF info from.
  • --tmp-directory TMP_DIRECTORY: Cache directory
  • --lifetime LIFETIME: Lifetime of cached data in seconds
  • --timeout TIMEOUT: Timeout for API calls in seconds
# Usage for higher load scenarios:
pip install ctf-attackapi[gunicorn]
gunicorn attackapi.server:create_app --bind :14320 --worker-class attackapi.server.worker.MyGunicornWebWorker --workers 4

Environment variables:

  • CTF_API: URL to get CTF info from.
  • CTF_API_TMP_DIR: Cache directory (gunicorn only)
  • CTF_API_LIFETIME: Lifetime of cached data in seconds (gunicorn only)
  • CTF_API_TIMEOUT: Timeout for API calls in seconds (gunicorn only)

You can also use docker to run the server:

# edit compose.yaml and insert your CTF API URL before!
docker compose up -d

Using attackapi for other information (scoreboard etc.)

Feel free to re-use the caching layers for other information, like the current scoreboard. The class JsonAdCtfApiAsync accepts arbitrary JSON endpoints:

from attackapi.async_api import JsonAdCtfApiAsync

info = await JsonAdCtfApiAsync("https://scoreboard.ctf.saarland/api/scoreboard_current.json").retrieve()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ctf_attackapi-0.1.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ctf_attackapi-0.1.1-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file ctf_attackapi-0.1.1.tar.gz.

File metadata

  • Download URL: ctf_attackapi-0.1.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for ctf_attackapi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 acb040a008ae72fee5355de9aad671e1ab2987af8744779e537d729a73fe9b77
MD5 86d6b8e07404fdbaccfa9b0d1c1381c9
BLAKE2b-256 dabdefeaee9fe4f31af183709422c1ec2522a748b370a3fc3410bd7f0a42167e

See more details on using hashes here.

File details

Details for the file ctf_attackapi-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ctf_attackapi-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for ctf_attackapi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9232d34a104e6ee85e7bc1cb75ec8a0c8f1f30d22e950088746d5405dfb21721
MD5 f55edaa439693f87cf232a5add953434
BLAKE2b-256 232aca9a0c318d9b15a32afd62a7c85fd1f1d0a99432e2fb7c2179cdad37057c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page