OpenEMR 5.0.1.3 Arbitrary File Actions
Three endpoints in OpenEMR's template import script passed user-supplied input straight into file functions, letting an authenticated attacker read, write and delete arbitrary files on the server. Chaining the write with the read allowed the author to achieve remote code execution.
Back in 2018, a group of security researchers and I decided to try our hands at OpenEMR and find security vulnerabilities.The full report can be found here.This a very good read and I recommend reading it in its entirety. However this blog post is just documenting my contribution to the project.The following are the…
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the script trusted the mode, docid and content parameters and passed them directly to file_get_contents, file_put_contents and unlink without validating or restricting the path.
Impact
An authenticated attacker could read, write and delete arbitrary files, and by combining the write and read primitives could achieve remote code execution; the text notes the write alone cannot execute the file.
What the attacker needed
Authenticated session; known target file path for read or delete
The fix
The issues were patched, and the author advises upgrading to the latest version.
How it was found
- Send a request to the template import script with the mode parameter set to get and a chosen file path as docid.
- Observe the contents of that local file echoed back in the HTML response.
- Send a request with mode set to save, a filename as docid and PHP code as content to write a file.
- Send a request with mode set to delete and a known filename as docid to remove that file.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
The application takes a filename and file contents from the user and hands them to built-in functions that read, write or delete files, without checking what the user is asking for. Because nothing restricts the path, a logged-in user can point those functions at any file on the server, including ones they should never touch. Writing a script file and then reading it back can turn this into running their own code.
What is Path traversal?
Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.