Can SVG Files Contain Malware? How Attackers Use SVGs for Phishing

Yes. SVG files are XML documents that browsers render, and the SVG standard allows embedded JavaScript, HTML content and links. Attackers send SVG attachments that, when opened in a browser, run scripts that redirect to phishing pages, display fake login forms, or assemble and download malware ("HTML smuggling").

Jul 18, 2025 - 09:49
Updated: 
103.2k

Cyber Threats, Scams & Online Safety in Cybersecurity & Ethical Hacking · Explainer

Can SVG Files Contain Malware? How Attackers Use SVGs for Phishing

Quick answer: Yes. SVG files are XML documents that browsers render, and the SVG standard allows embedded JavaScript, HTML content and links. Attackers send SVG attachments that, when opened in a browser, run scripts that redirect to phishing pages, display fake login forms, or assemble and download malware ("HTML smuggling"). Because SVGs look like harmless images and are often allowed by email filters, they have become a popular phishing lure. Defend by blocking or sandboxing SVG attachments, sanitising SVG uploads on websites, using a strict Content Security Policy, and teaching users that an "image" opening in a browser and asking for a login is a red flag.

Key takeaways

  • SVG is code, not just a picture: it can contain <script> and <foreignObject>.
  • Opening an SVG attachment usually launches the browser, where scripts can run.
  • SVG phishing often redirects to credential-harvesting pages.
  • Websites accepting SVG uploads must sanitise them to prevent stored XSS.
  • Many organisations block SVG attachments from external senders.

Table of Contents

Why Can SVG Files Be Dangerous?

The SVG 2 specification includes a <script> element, event handlers such as onload, links, and <foreignObject> for embedding HTML (MDN). When an SVG is opened directly in a browser, these features can run with the page's privileges. When an SVG is displayed through an <img> tag, scripts do not run, which is why the delivery method matters.

How Do Attackers Use SVGs?

TechniqueWhat happens
RedirectScript sends the browser to a phishing site, often with the victim's email pre-filled
Embedded fake loginforeignObject renders an HTML form that posts credentials to the attacker
HTML smuggling (T1027.006)Script assembles a malicious file in the browser and triggers a download
Stored XSS on websitesUploaded SVG avatars or images run scripts for other visitors

How Do You Inspect a Suspicious SVG?

Open it in a text editor, not a browser, preferably in an isolated machine:

# Look for script, event handlers, embedded HTML and links
grep -nEi "<script|onload=|onclick=|foreignObject|href=|base64|atob\(" suspicious.svg

Base64 strings and atob() calls often hide redirect URLs or payloads. Extract URLs and check them in a sandbox. See PDF malware analysis for a similar workflow.

How Do You Block SVG Phishing in Email?

  • Block or quarantine SVG attachments from external senders unless needed.
  • Use attachment sandboxing, such as Microsoft Defender Safe Attachments.
  • Strip active content or convert SVGs to PNG at the gateway.
  • Combine with phishing-resistant MFA so stolen passwords are less useful.

How Do Websites Handle SVG Uploads Safely?

  • Avoid accepting SVG uploads where raster formats suffice.
  • Sanitise with a maintained library such as DOMPurify using its SVG profile.
  • Serve user uploads from a separate domain with Content-Security-Policy: script-src 'none' and Content-Disposition: attachment.
  • Follow the OWASP XSS Prevention Cheat Sheet.
// Node.js with DOMPurify and jsdom: sanitise an uploaded SVG
const createDOMPurify = require('dompurify');
const { JSDOM } = require('jsdom');
const DOMPurify = createDOMPurify(new JSDOM('').window);
const clean = DOMPurify.sanitize(uploadedSvg, { USE_PROFILES: { svg: true, svgFilters: true } });

What Should Users Do?

  • Be suspicious of image attachments that open in a browser and show a login page.
  • Check the address bar before entering credentials.
  • Report suspicious attachments to IT.

Related: QR code phishing in PDFs. WebAsha offers phishing defence training.

References

Frequently Asked Questions

Yes. SVGs can include JavaScript, embedded HTML and links that run when opened in a browser.

They look like images, are often allowed by filters, and open in browsers where scripts can run.

No. Scripts in SVGs referenced by img tags do not execute; the risk is when opened directly.

Open it in a text editor and look for script tags, event handlers, foreignObject, links and base64 data.

Using scripts in HTML or SVG to build a malicious file inside the browser and trigger a download.

Sanitise them with a library like DOMPurify and serve them from a separate domain with a strict CSP.

Many block or sandbox SVGs from external senders, since few business emails need them.

Do not enter credentials; check the URL and report it to your IT team.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.