Trustwave Unveils New Offerings to Maximize Value of Microsoft Security Investments. Learn More

Trustwave Unveils New Offerings to Maximize Value of Microsoft Security Investments. Learn More

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.

Offensive Security
Solutions to maximize your security ROI
Microsoft Exchange Server Attacks
Stay protected against emerging threats
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
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
SpiderLabs Blog

ModSecurity Advanced Topic of the Week: Malware Link Removal

This is a follow-up post to ModSecurity Advanced Topic of the Week: Malware Link Detection in which we will highlight a new capability within ModSecurity v2.6 that allows for removal of data within response bodies.

Malware Link Identification

In the previous blog post, we should how you can use the new @gsbLookup operator to query a local Google Safe Browsing DB file to identify potential malware links within response bodies of your web traffic. While identifying malware links is obviously quite useful, the next logical question is - what do you want to do about it?

In the previous blog post, when the GSB rule identified a malware link within the response body, we isused a redirect action within ModSecurity.

SecGsbLookupDB GsbMalware.datSecRule RESPONSE_BODY "@gsbLookup =\"https?\:\/\/(.*?)\""   "phase:4,capture,log,redirect:,msg:'Bad url detected in RESPONSE_BODY (Google Safe Browsing Check)',logdata:'{tx.0}'"

This action does help to ensure that the end client does not recieve the response page with the infected link in it, however the end user experience is certianly still impacted as they have no real indication as to why they were suddednly redirected.

Malware Link Removal

What would be ideal from both a security and end user experience perspectives would be to sanitize the response bodies and remove the offending data while still delivering the page to the client.

Data Substitution Operator (@rsub)

In the latest version of ModSecurity (2.6), we also introduced an extremely powerful new operator called @rsub which is short for RegEx Substitution. As the name indicates, this operator allows you to match variable data and then do a substitution. What makes this new operator even more powerful is that is has macro expansion capabilities. This is extremely useful when you need generic data matching (see below).

Modify Live Data (STREAM_OUTPUT_BODY variable)

The @rsub operator is really cool, but what makes this use-case possible is another new feature in ModSecurity v.2.6 and that is the new STREAM_OUTPUT_BODY variable which gives direct filter level access to the live data. Here is an updated ruleset that uses these new capabilities:

SecGsbLookupDB GsbMalware.datSecStreamOutBodyInspection OnSecContentInjection OnSecRule STREAM_OUTPUT_BODY "@gsbLookup \Whttps?\:\/\/(.*?)(\"|>)" "chain,phase:4,capture,log,pass,msg:'Bad url detected in RESPONSE_BODY (Google Safe Browsing Check)',logdata:'{tx.0}'"        SecRule STREAM_OUTPUT_BODY "@rsub s/%{tx.0}/MALICIOUS_URL_REMOVED/"

This ruleset will play Whack-a-Mole and actively swap out the malicious domains found within the GSB DB with the string MALICIOUS_URL_REMOVED in the response pages and still issue alert messages locally in ModSecurity so that security teams can investigate infected pages.

Live Examples

I decided to test out this new feature. The first step was to identify a site that was infected by LizaMoon malware links. After some quick google searches, I found the following site.


I then tested the new GSB data substitution capabilities by configuring my local browser to use my local Apache+ModSecurity server as a forward proxy. I then browsed to this same site. Here is a screenshot of the infected after being cleansed by ModSecurity:



The ability to alter live http transactional data is a huge step foward, not only for ModSecurity, but for WAFs in general. This allows for much more flexibility in responding to issues vs. the limited choice of blocking the entire transaction. This particular use-case of removing malware links from infected pages is just the first example of this new feature. More to come...

Latest SpiderLabs Blogs

Search & Spoof: Abuse of Windows Search to Redirect to Malware

Trustwave SpiderLabs has detected a sophisticated malware campaign that leverages the Windows search functionality embedded in HTML code to deploy malware. We found the threat actors utilizing a...

Read More

The Sentinel’s Watch: Building a Security Reporting Framework

Imagine being on shift as the guard of a fortress. Your job is to identify threats as they approach the perimeter. The more methods you have for detecting those threats, the better your chances of...

Read More

Fake Advanced IP Scanner Installer Delivers Dangerous CobaltStrike Backdoor

During a recent client investigation, Trustwave SpiderLabs found a malicious version of the Advanced IP Scanner installer, which contained a backdoored DLL module. Our client had been searching for...

Read More