-
Notifications
You must be signed in to change notification settings - Fork 10
Sadservers with instance on demand #451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kunrex
wants to merge
23
commits into
sdslabs:sadserver
Choose a base branch
from
kunrex:sadserver-instance
base: sadserver
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
2d1df72
Fix: fix util prompt int function (#452)
kunrex d3a8728
Add Empty Spawn Instance Handler
gqvz 9b1168e
Add redis cache backup and restore commands
gqvz b873f82
Refactor Redis command execution to use environment variable for pass…
gqvz d0707e5
Enhance Redis backup command to conditionally include user and passwo…
gqvz 07cec29
Cleanup: add cache cleanup
kunrex 3d967ef
Feat: Add redis set up to beast init and beast config
kunrex 4961b17
Add instanced challenges
kunrex f068061
Add redis config setup
kunrex 27575b2
Fix: fix beast setup postgres dsn
kunrex cd6ca21
Feat: Add healthproiber init to beast init
kunrex 66f44b7
Warn: add redis warning
kunrex 42be447
Feat: add acl save to redis init
kunrex bc814f5
Erros: Add better error messages
kunrex 4e1eb19
Fix: fix readme port mapping
kunrex 53d1f18
Feat: Add util functions for executing commands
kunrex 944fa0f
Feat: Add check.sh validation for instance challenges
kunrex ba713b7
Feat: Add check route with validation
kunrex be6db2b
Fix: fix unnecessary change
kunrex 8aca4ca
Checks: Check if port 22 is exposed on all containers and refactor ut…
kunrex cef3871
Add: Add chmod to challenge setup
kunrex eef3cac
Fix: fix check path bugs and confirm test
kunrex e496285
Fix: fix bugs, update typos, add temp utils
kunrex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| FROM php:7.4-apache | ||
|
|
||
| # Install MySQL extension | ||
| RUN docker-php-ext-install mysqli pdo pdo_mysql | ||
|
|
||
| # Copy challenge files | ||
| COPY challenge/ /var/www/html/ | ||
|
|
||
| # Set permissions | ||
| RUN chown -R www-data:www-data /var/www/html | ||
|
|
||
| EXPOSE 80 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Instanced Docker Compose Challenge Example | ||
|
|
||
| This is an example of an **instanced challenge using Docker Compose** - a multi-container challenge where each user gets their own isolated environment with a web server and database. | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| ┌──────────────────────────────────────────┐ | ||
| │ User's Instanced Environment │ | ||
| │ ┌─────────────┐ ┌─────────────┐ │ | ||
| │ │ PHP/Apache │ ───▶ │ MySQL │ │ | ||
| │ │ (web) │ │ (db) │ │ | ||
| │ └─────────────┘ └─────────────┘ │ | ||
| │ │ │ | ||
| │ ▼ │ | ||
| │ Port: 31234 (dynamically assigned) │ | ||
| └──────────────────────────────────────────┘ | ||
| ``` | ||
|
|
||
| ## Key Configuration | ||
|
|
||
| In `beast.toml`: | ||
|
|
||
| ```toml | ||
| [challenge.metadata] | ||
| instanced = true | ||
| instance_expiration = 600 # 10 minutes | ||
|
|
||
| [challenge.env] | ||
| docker_compose = "docker-compose.yml" | ||
| default_port = 8080 | ||
| ``` | ||
|
|
||
| In `docker-compose.yml`, use the `INSTANCE_PORT` environment variable: | ||
|
|
||
| ```yaml | ||
| services: | ||
| web: | ||
| ports: | ||
| - "${INSTANCE_PORT:-8080}:80" | ||
| ``` | ||
|
|
||
| ## Challenge Details | ||
|
|
||
| This is a SQL injection challenge: | ||
|
|
||
| 1. The login form is vulnerable to SQL injection | ||
| 2. Bypass authentication to login as admin | ||
| 3. The flag is stored in the `secrets` table | ||
|
|
||
| ### Solution | ||
|
|
||
| ``` | ||
| Username: admin' OR '1'='1' -- | ||
| Password: anything | ||
| ``` | ||
|
|
||
| Or use UNION-based injection to extract data directly. | ||
|
|
||
| ## Testing Locally | ||
|
|
||
| ```bash | ||
| # Build and run locally (for testing) | ||
| cd _examples/instanced-compose | ||
| docker-compose up -d | ||
|
|
||
| # Access at http://localhost:8080 | ||
| ``` | ||
|
|
||
| ## Usage via Beast API | ||
|
|
||
| ```bash | ||
| # Spawn your instance | ||
| curl -X POST -H "Authorization: Bearer $TOKEN" \ | ||
| http://localhost:8080/api/instances/instanced-compose/spawn | ||
|
|
||
| # Response: | ||
| # { | ||
| # "instance_id": "abc123def456", | ||
| # "challenge_name": "instanced-compose", | ||
| # "hosted_address": "localhost", | ||
| # "port": 31234, | ||
| # "expires_at": "2024-01-15T10:40:00Z", | ||
| # "ttl_seconds": 600 | ||
| # } | ||
|
|
||
| # Access your instance | ||
| open http://localhost:31234 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| [author] | ||
| name = "beast-admin" | ||
| email = "admin@beast.local" | ||
| ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ" | ||
|
|
||
| [challenge.metadata] | ||
| name = "instanced-compose" | ||
| flag = "FLAG{c0mp0s3_1nst4nc3s_r0ck!}" | ||
| type = "web" | ||
| description = "A web challenge with database backend. Each user gets their own isolated environment!" | ||
| points = 200 | ||
| difficulty = "medium" | ||
| tags = ["web", "sql", "instanced"] | ||
| maxAttemptLimit = 100 | ||
| instanced = true | ||
| instance_expiration = 12 | ||
|
|
||
| [[challenge.metadata.hints]] | ||
| text = "Check for SQL injection vulnerabilities" | ||
| points = 30 | ||
|
|
||
| [[challenge.metadata.hints]] | ||
| text = "The admin password might be in the database..." | ||
| points = 50 | ||
|
|
||
| [challenge.env] | ||
| docker_compose = "docker-compose.yml" | ||
| default_port = 8080 | ||
|
|
||
| [resource] | ||
| cpu_shares = 1024 | ||
| memory_limit = 536870912 | ||
| pids_limit = 100 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Secret Vault - Login</title> | ||
| <style> | ||
| body { | ||
| font-family: 'Segoe UI', Arial, sans-serif; | ||
| background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); | ||
| min-height: 100vh; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| margin: 0; | ||
| color: #fff; | ||
| } | ||
| .container { | ||
| background: rgba(255, 255, 255, 0.1); | ||
| padding: 40px; | ||
| border-radius: 15px; | ||
| box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3); | ||
| backdrop-filter: blur(10px); | ||
| width: 350px; | ||
| } | ||
| h1 { | ||
| text-align: center; | ||
| margin-bottom: 30px; | ||
| color: #00d9ff; | ||
| } | ||
| .form-group { | ||
| margin-bottom: 20px; | ||
| } | ||
| label { | ||
| display: block; | ||
| margin-bottom: 8px; | ||
| color: #aaa; | ||
| } | ||
| input[type="text"], input[type="password"] { | ||
| width: 100%; | ||
| padding: 12px; | ||
| border: none; | ||
| border-radius: 8px; | ||
| background: rgba(255, 255, 255, 0.1); | ||
| color: #fff; | ||
| font-size: 16px; | ||
| box-sizing: border-box; | ||
| } | ||
| input[type="submit"] { | ||
| width: 100%; | ||
| padding: 14px; | ||
| border: none; | ||
| border-radius: 8px; | ||
| background: #00d9ff; | ||
| color: #1a1a2e; | ||
| font-size: 16px; | ||
| font-weight: bold; | ||
| cursor: pointer; | ||
| transition: background 0.3s; | ||
| } | ||
| input[type="submit"]:hover { | ||
| background: #00b8d4; | ||
| } | ||
| .error { | ||
| background: rgba(255, 0, 0, 0.2); | ||
| padding: 10px; | ||
| border-radius: 8px; | ||
| margin-bottom: 20px; | ||
| text-align: center; | ||
| } | ||
| .success { | ||
| background: rgba(0, 255, 0, 0.2); | ||
| padding: 10px; | ||
| border-radius: 8px; | ||
| margin-bottom: 20px; | ||
| text-align: center; | ||
| } | ||
| .hint { | ||
| text-align: center; | ||
| margin-top: 20px; | ||
| color: #666; | ||
| font-size: 12px; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="container"> | ||
| <h1>Secret Vault</h1> | ||
|
|
||
| <?php | ||
| $error = ''; | ||
| $success = ''; | ||
|
|
||
| if ($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
| $host = getenv('DB_HOST') ?: 'db'; | ||
| $user = getenv('DB_USER') ?: 'challenge'; | ||
| $pass = getenv('DB_PASS') ?: 'challengepass'; | ||
| $dbname = getenv('DB_NAME') ?: 'ctf'; | ||
|
|
||
| $conn = new mysqli($host, $user, $pass, $dbname); | ||
|
|
||
| if ($conn->connect_error) { | ||
| $error = "Connection failed. Please try again."; | ||
| } else { | ||
| $username = $_POST['username']; | ||
| $password = $_POST['password']; | ||
|
|
||
| // VULNERABLE: SQL Injection! | ||
| $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; | ||
| $result = $conn->query($query); | ||
|
|
||
| if ($result && $result->num_rows > 0) { | ||
| $row = $result->fetch_assoc(); | ||
|
|
||
| if ($row['role'] === 'admin') { | ||
| // Admin login - show secrets | ||
| $secrets_query = "SELECT * FROM secrets"; | ||
| $secrets_result = $conn->query($secrets_query); | ||
|
|
||
| $success = "Welcome Admin! Here are your secrets:<br><br>"; | ||
| while ($secret = $secrets_result->fetch_assoc()) { | ||
| $success .= "<b>" . htmlspecialchars($secret['secret_name']) . ":</b> " . | ||
| htmlspecialchars($secret['secret_value']) . "<br>"; | ||
| } | ||
| } else { | ||
| $success = "Welcome, " . htmlspecialchars($row['username']) . "! You're logged in as a regular user."; | ||
| } | ||
| } else { | ||
| $error = "Invalid username or password!"; | ||
| } | ||
|
|
||
| $conn->close(); | ||
| } | ||
| } | ||
| ?> | ||
|
|
||
| <?php if ($error): ?> | ||
| <div class="error"><?php echo $error; ?></div> | ||
| <?php endif; ?> | ||
|
|
||
| <?php if ($success): ?> | ||
| <div class="success"><?php echo $success; ?></div> | ||
| <?php else: ?> | ||
| <form method="POST"> | ||
| <div class="form-group"> | ||
| <label>Username</label> | ||
| <input type="text" name="username" required placeholder="Enter username"> | ||
| </div> | ||
| <div class="form-group"> | ||
| <label>Password</label> | ||
| <input type="password" name="password" required placeholder="Enter password"> | ||
| </div> | ||
| <input type="submit" value="Login"> | ||
| </form> | ||
| <?php endif; ?> | ||
|
|
||
| <p class="hint">Hint: Try logging in as admin to see the secrets!</p> | ||
| </div> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| version: '3.8' | ||
|
|
||
| services: | ||
| web: | ||
| build: | ||
| context: . | ||
| dockerfile: Dockerfile | ||
| ports: | ||
| # Use INSTANCE_PORT env var if available, otherwise default to 8080 | ||
| - "${INSTANCE_PORT:-8080}:80" | ||
| environment: | ||
| - DB_HOST=db | ||
| - DB_USER=challenge | ||
| - DB_PASS=challengepass | ||
| - DB_NAME=ctf | ||
| depends_on: | ||
| - db | ||
| restart: unless-stopped | ||
|
|
||
| db: | ||
| image: mysql:5.7 | ||
| environment: | ||
| - MYSQL_ROOT_PASSWORD=rootpass | ||
| - MYSQL_DATABASE=ctf | ||
| - MYSQL_USER=challenge | ||
| - MYSQL_PASSWORD=challengepass | ||
| volumes: | ||
| - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro | ||
| restart: unless-stopped |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The login handler builds an SQL query by directly interpolating
$_POST['username']and$_POST['password']into the string, which makes this endpoint vulnerable to SQL injection. An attacker can craft input such asadmin' OR '1'='1in theusernamefield to bypass authentication and read arbitrary data from theusersandsecretstables (including the flag). Use parameterized queries/prepared statements and avoid concatenating raw user input into SQL strings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @gqvz