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").
Topic: Cyber Threats, Scams & Online Safety in Cybersecurity & Ethical Hacking · Explainer
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?
- How Do Attackers Use SVGs?
- How Do You Inspect a Suspicious SVG?
- How Do You Block SVG Phishing in Email?
- How Do Websites Handle SVG Uploads Safely?
- What Should Users Do?
- References
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?
| Technique | What happens |
|---|---|
| Redirect | Script sends the browser to a phishing site, often with the victim's email pre-filled |
| Embedded fake login | foreignObject 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 websites | Uploaded 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'andContent-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
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0