Skip to content
RCE Write-up Advanced

One LFI bypass to rule them all (using base64)

A PHP page included a user-supplied file path after only checking that the value ended in 'txt' and blocking PHP tags and the pipe character. By abusing PHP's base64-decode filter, which ignores non-base64 characters, the author crafted a value ending in 'txt' that still decoded to PHP code, achieving remote code execution.

matan-h Follow

Published on matan-h.com

On one of the [real world] PHP websites, I found an LFI [there was a URL parameter that checked if the value ends with ‘txt’. If so, it would just display it].

matan-h, on matan-h.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the file inclusion check only validated the trailing characters of the input and relied on string blocking of PHP tags, while PHP's base64-decode filter silently discards non-base64 characters, so the appended extension did not break decoding.

Impact

An attacker could run arbitrary commands on the server, though the exploit required the include/require construct and allow_url_include enabled, and the owner later blocked the php:// protocol.

What the attacker needed

Allow_url_include enabled; include or require used, not file_get_contents; attacker can control the file parameter

The fix

The input should be validated against an allowlist of known files rather than a trailing-extension check, and PHP wrappers such as php:// and data:// should not be reachable from user input.

How it was found

  1. Set up a local PHP test page that includes the file parameter when it ends in txt
  2. Enable allow_url_include in php.ini and start the PHP built-in server
  3. Send a php://filter base64-decode request pointing at a data:// resource
  4. Remove the base64 padding and append the txt extension so the value passes the check
  5. Send the crafted request with a command parameter and observe command output

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

The site let visitors choose a file to display, and only checked that the name ended in 'txt'. PHP can decode base64 text (a way of writing data using letters and digits) and quietly skips characters that are not valid base64. So the attacker hid PHP code in base64, added 'txt' at the end, and the site ran the decoded code on the server.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.