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

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:http://www.example.com/,msg:'Bad url detected in RESPONSE_BODY (Google Safe Browsing Check)',logdata:'http://www.google.com/safebrowsing/diagnostic?site=%{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:'http://www.google.com/safebrowsing/diagnostic?site=%{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.

9590_6204eeff-9078-4f72-9391-ec1e9756b5f9

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:

11241_afe8dbc7-8715-430b-97bc-7b6b2836ea19

Conclusions

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

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