Skip to main content

Unofficial Hack The Box Python SDK

Project description

Unofficial Hack The Box (HTB) Python SDK & API Client (pyhtb)

PyPI Version Supported Python Versions License Code Style: Ruff

An unofficial, modern Python SDK and API client wrapper for the Hack The Box (HTB) platform. This library exposes synchronous and asynchronous interfaces with generated typed request and response models for interacting programmatically with HTB APIs.

Whether you are automating machine status checking, managing teams, tracking starting point progression, or scripting challenge submissions, pyhtb provides a single unified interface.

pyhtb is an independent project and is not affiliated with, endorsed by, or sponsored by Hack The Box.


Key Features

  • Broad API coverage: Clients generated from v4, v5, and Experience v1 specifications.
  • Synchronous and asynchronous APIs: Built on HTTPX.
  • Typed data models: Generated request and response models using attrs.
  • Unified authentication: Configure one token across supported API clients.
  • Optional CLI: Common machine, challenge, VPN, search, and profile workflows.

Installation

Install the package via pip from PyPI:

pip install pyhtb

For local development or running modifications, clone the repository and install in editable mode:

pip install -e .

Quick Start

1. Basic Client Initialization

Initialize the PyHTB client wrapper. If you want to access protected endpoints, provide your Hack The Box API token (generated on the app.hackthebox.com settings page).

from pyhtb import PyHTB

# Initialize authenticated SDK
client = PyHTB(token="YOUR_HTB_API_TOKEN")

# The unified client organizes endpoints by API version:
# - client.v4
# - client.v5
# - client.experience_v1

2. Accessing Endpoints (v5 Example)

All OpenAPI operations are dynamically bound as callable methods directly on the version clients.

Synchronous Call

from pyhtb import PyHTB

client = PyHTB(token="YOUR_HTB_API_TOKEN")

# Call any endpoint directly. Client authentication parameters are injected automatically!
machines_response = client.v5.get_machines()

for machine in machines_response.data:
    print(f"Machine: {machine.name} | OS: {machine.os} | Difficulty: {machine.difficulty}")

Asynchronous Call

All asynchronous methods share the same name as sync ones but are postfixed with _async.

import asyncio
from pyhtb import PyHTB

async def main():
    client = PyHTB(token="YOUR_HTB_API_TOKEN")
    
    # Asynchronous endpoint invocation
    machines_response = await client.v5.get_machines_async()
    
    for machine in machines_response.data:
        print(f"Async loaded: {machine.name}")

asyncio.run(main())

Accessing Detailed Responses (Metadata & Headers)

If you need to inspect HTTP status codes or response headers, append _detailed or _async_detailed to the method name:

response = client.v5.get_machines_detailed()
print(f"HTTP Status: {response.status_code}")
print(f"Response Headers: {response.headers}")
print(f"Parsed Body Model: {response.parsed}")

Supported APIs and Specs

The SDK is compiled from official and community-maintained reverse-engineered OpenAPI specifications:

Version Description Target Submodule
API v4 Primary platform endpoints (User, Teams, Challenges, Tracks, etc.) client.v4
API v5 Modern dashboard & user activity endpoints (Machines status, University, User dashboard) client.v5
Experience v1 Experience, Levels, and Streak endpoints client.experience_v1

CLI Tool (Proof of Concept)

pyhtb ships with an experimental command-line interface that wraps the SDK into a quick-fire workflow tool. After installing the package, the pyhtb command is available globally.

Authentication

# Store your HTB App Token (generates from app.hackthebox.com → Settings → App Tokens)
pyhtb auth set

# Verify your credentials
pyhtb auth status

# You can also export the token as an environment variable
export HTB_TOKEN="eyJ..."

Interactive token input is hidden. Environment variables take precedence over the stored token. If you store a token with pyhtb auth set, it is written as plaintext to $XDG_CONFIG_HOME/pyhtb/token, or ~/.config/pyhtb/token when XDG_CONFIG_HOME is not set. On Unix-like systems, the token file is created with owner-only 0600 permissions.

Machines

# List active machines
pyhtb machine list

# List retired machines, filtered by difficulty
pyhtb machine list --retired --difficulty hard --os linux

# Show detailed info for a machine (by name or ID)
pyhtb machine info Checkpoint

# Start a machine and wait for IP assignment
pyhtb machine start Checkpoint

# Check your active machine
pyhtb machine active

# Submit a flag
pyhtb machine own Checkpoint <flag>

# Stop or reset the active machine
pyhtb machine stop
pyhtb machine reset

Challenges

# List challenges, optionally filtered by category
pyhtb challenge list --category Web --search void

# Show challenge details (docker status, ports, etc.)
pyhtb challenge info spookifier

# Start/stop a Docker container (by name or ID)
pyhtb challenge start spookifier
pyhtb challenge stop spookifier

# Submit a flag
pyhtb challenge own spookifier <flag>

VPN

# Check active VPN connections
pyhtb vpn status

# List available VPN servers
pyhtb vpn servers --pool labs

# Switch to a different VPN server
pyhtb vpn switch 113

# Download an .ovpn config file
pyhtb vpn download 113 -o lab.ovpn
pyhtb vpn download 113 --tcp

Search & Profile

# Global search across machines and challenges
pyhtb search void

# Display your authenticated profile
pyhtb whoami

Commands exit with a nonzero status on failure (missing token, unresolved target, API errors, rejected flags), so they compose cleanly in scripts:

pyhtb machine own Checkpoint "$FLAG" && echo "owned!" || echo "submission failed"

Note: The CLI is a proof-of-concept companion to the SDK. It covers the most common workflows but does not expose every API endpoint. Use the SDK directly for broader endpoint access.


Code Regeneration

The SDK contains a build pipeline to automatically compile clients from raw OpenAPI YAML specs and refresh the generated public facade plus typing stub. To regenerate the submodules after modifying any specification, execute:

./api/regen.sh

This requires the code generation extra:

python -m pip install -e '.[codegen]'

License

This project is released under the Unlicense — dedicated to the public domain. See the LICENSE file for details.

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

pyhtb-0.1.4.tar.gz (241.7 kB view details)

Uploaded Source

Built Distribution

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

pyhtb-0.1.4-py3-none-any.whl (611.0 kB view details)

Uploaded Python 3

File details

Details for the file pyhtb-0.1.4.tar.gz.

File metadata

  • Download URL: pyhtb-0.1.4.tar.gz
  • Upload date:
  • Size: 241.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyhtb-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5269bd4f1971bac4fd07f6be0fa7c1e8e9bf2b097b41e446f830ee39f5097f0e
MD5 f23e7e1cd79fa89d1158b3dff1c27404
BLAKE2b-256 5889f656c7b4cbd8ea55566d05c9dcab81d4bfdd092cd004b159ec32d5d7773e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhtb-0.1.4.tar.gz:

Publisher: publish.yml on Gubarz/pyhtb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhtb-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: pyhtb-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 611.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyhtb-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 97dba658615b4b5079e230cc3b689696dbdcf81b31f5658e5de319037851cbcb
MD5 28461f90161cf0742ca0225fec5eb8be
BLAKE2b-256 56cf90fd816f5faab6182b75cf35fb2ba6eedca5f72ef330c6c898c8a14f6c8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhtb-0.1.4-py3-none-any.whl:

Publisher: publish.yml on Gubarz/pyhtb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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