Skip to content

Add extras: real_ip plugin, Discord solve bot, Python challenge sync - #8

Merged
CorentinFelten merged 1 commit into
CorentinFelten:mainfrom
SenequeZ:extras-sync-solvebot-realip
Nov 10, 2026
Merged

CorentinFelten merged 1 commit into
CorentinFelten:mainfrom
SenequeZ:extras-sync-solvebot-realip

Conversation

@SenequeZ

@SenequeZ SenequeZ commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds three opt-in tools we used to run UnitedCTF 2026 on the CTFd + Zync + Galvanize stack. Each lives in its own folder under extras/, and extras/README.md explains how to set it up in a deploy/ folder created by setup.sh. setup.sh itself and the default deployment are unchanged.

Related: 28Pollux28/galvanize#7 and 28Pollux28/zync#1.

extras/real_ip: real client IPs behind Cloudflare

Problem: when the CTFd domain is proxied by Cloudflare, Admin → Tracking, the gunicorn logs and the IP-based rate limits all record Cloudflare edge addresses (172.69.*, 162.158.*, …). That happens for two reasons:

  • Traefik doesn't trust Cloudflare, so it replaces X-Forwarded-For with the edge address.
  • CTFd's TRUSTED_PROXIES only covers private ranges by default, so get_ip() stops at the Cloudflare hop.

Fix:

  • A small CTFd plugin adds Cloudflare's published IPv4 and IPv6 ranges to TRUSTED_PROXIES when CTFd loads.
  • The README explains the matching Traefik forwardedHeaders.trustedIPs setting, and recommends against insecure: true.
  • It also explains the optional REVERSE_PROXY=2,1,1,1,1 setting, so gunicorn's access log shows the real client too.

extras/solve-bot: Discord solve and first-blood announcements

  • Polls /api/v1/submissions?type=correct and posts each new solve in French and English, with its rank and a 🩸 FIRST BLOOD message for the first solver.
  • Posts through a Discord webhook, or through a bot token and channel id. It always sends a User-Agent, because Discord returns 403 for urllib's default one.
  • Staff never count: admins, hidden users and SKIP_USERS are neither announced nor counted. CTFd's API returns 404 for hidden and admin users, and the bot treats that as staff. Before this, a hidden admin's test solve was announced as first blood.
  • Restart-safe: the last seen submission id and each challenge's solve count are saved to STATE_PATH. On its first start the bot catches up on existing solves without posting them.
  • Configured entirely through environment variables, including BOT_USERNAME and SKIP_CHALLENGES.

extras/ctfd-sync: Python challenge sync

  • ctfd_sync.py: syncs every challenge.yml and numbered challenge-*.yml into CTFd through the admin API. Running it again only adds what's missing.
    • Challenges: created, or updated if one with the same name exists.
    • Flags and hints: missing ones are added.
    • Tags: missing ones are added, and ones removed from the YAML are deleted.
    • Prerequisites: set from requirements, by challenge name.
    • Bilingual text: {fr, en} descriptions and hints are supported.
    • Challenge type: zync stays zync; everything else becomes standard.
    • Event settings: optionally sent to /api/v1/configs from a JSON file.
  • ctfd_upload_inside.py: attaches the files listed in the manifest written by the sync. It runs inside the CTFd container and uses CTFd's own upload_file(), which avoids proxy request-size limits.
  • Everything is configured through environment variables (CTFD_URL, CTFD_HOST, CTFD_ADMIN_*, CHALLENGE_DIR, MANIFEST_PATH, CTFD_CONFIGS_JSON). The original scripts had credentials, paths and event dates written into the code.

This overlaps with challenges.sh sync. It's offered as an alternative for repositories that use bilingual fields or several challenges per folder, which matches Galvanize's challenge-*.yml and instance_group support.

Also

  • README.md and README_FR.md each get a short "Extras" entry pointing to extras/README.md.
  • .gitignore now ignores __pycache__/.

Test plan

  • All three ran in production at UnitedCTF 2026 (CTFd 3.7.7, individual mode, behind Cloudflare). The only changes since then: settings read from the environment, the bot's display name configurable, and docstrings.
  • python3 -m py_compile passes on every script. ctfd_sync.load_all() parses a sample challenge-1.yml with bilingual description and hints. The sync refuses to run without CHALLENGE_DIR.
  • Try real_ip + Traefik trustedIPs on a deployment made with setup.sh behind Cloudflare
  • Run the solve bot against a test CTFd with a Discord webhook

- extras/real_ip: CTFd plugin appending Cloudflare ranges to
  TRUSTED_PROXIES so Tracking, logs and rate limits see the real client
  IP behind Cloudflare (with the matching Traefik trustedIPs setup).
- extras/solve-bot: posts solves and first bloods to Discord (webhook or
  bot token), never counting admins/hidden/SKIP_USERS, with persisted
  state so restarts do not re-announce.
- extras/ctfd-sync: idempotent sync of challenge.yml / challenge-*.yml
  into CTFd (challenges, flags, hints, tags, prerequisites, bilingual
  descriptions/hints, optional event configs), plus an in-container
  uploader for challenge files.

Used for UnitedCTF 2026; credentials and event settings moved to
environment variables / an optional JSON file.
@CorentinFelten
CorentinFelten merged commit d0814c1 into CorentinFelten:main Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants