Remote code execution via polyglot web shell upload
This lab demonstrates a more advanced file upload bypass where the attacker creates a polyglot file—a file that is simultaneously valid as an image and as executable PHP. Instead of relying on filename tricks or server configuration, the exploit abuses how applications validate file content versus how the server later executes it. By embedding PHP code inside image metadata (specifically the EXIF comment field) using tools like exiftool, the attacker produces a file that passes content validation checks while still containing executable code. This approach is particularly effective against applications that verify file signatures (magic bytes) but fail to sanitize or strip metadata.
The attack begins by taking a legitimate image file (for example, a .jpg) and injecting PHP code into its metadata. Using a tool like exiftool, the attacker writes malicious code into the comment field, such as a payload that leverages file_get_contents to retrieve commands or external input. The resulting file remains a valid image: it retains correct headers, structure, and visual integrity, meaning it will pass checks that confirm the file is indeed a JPEG. However, hidden within the metadata is PHP code that will execute if the file is interpreted by the PHP engine. This creates a dual-nature file—harmless to validators, but dangerous when executed.
The vulnerability is triggered when the server treats the uploaded image as executable PHP. This can occur due to misconfigurations such as the server processing files based on location rather than extension, or incorrectly mapping .jpg files to the PHP handler. When the file is requested, the PHP interpreter parses the entire file, including metadata sections, and executes any embedded PHP code it encounters. The injected payload—using file_get_contents—can then be used to fetch remote commands, read sensitive files, or interact with the system depending on how it is written. This results in full remote code execution despite the application successfully verifying that the uploaded file is a “valid image.”
The root cause of this vulnerability lies in a mismatch between file validation and execution context. The application performs content-based validation (checking magic bytes or file structure), which is stronger than extension filtering, but still incomplete because it does not sanitize embedded metadata. At the same time, the server is misconfigured to allow execution of files that should be treated as static content. This combination creates a dangerous scenario where a file can be both valid and malicious at the same time. The attack does not break validation—it complies with it, while hiding the payload in overlooked areas of the file format.
To mitigate this vulnerability, multiple layers of defense are required. First, uploaded files should be sanitized, meaning metadata should be stripped or rewritten to remove any embedded code. Second, files must be stored outside the web root or served through a mechanism that does not allow execution. Third, the server must be configured to ensure that image files are never passed to the PHP interpreter under any circumstances. Additionally, validating file content should go beyond simple signature checks and include re-encoding images using trusted libraries, which effectively removes hidden payloads. Finally, strict separation between user-uploaded content and executable environments is essential to prevent any possibility of code execution.
The key takeaway from this lab is that even robust validation techniques like checking file signatures are not sufficient on their own. Attackers can craft files that are structurally valid yet still contain hidden executable payloads. By embedding PHP code within EXIF metadata and leveraging server misconfigurations, the attacker achieves remote code execution without triggering traditional defenses. This reinforces a critical principle in security: never trust uploaded files, even if they appear valid—always control how and where they are executed.
Comments