The goal of this project was to create a keylogger that would accurately track the keystrokes that a user would make on their computer, whether that be searching up something on the internet or typing in their password, and then add those findings to a .txt file and send that file to me via email.
Visual Studio Code Wireshark Gmail
In order for this project to function properly, 4 Python libraries have to be imported: Pynput, Keyboard, smtplib, and SSL. Each of these libraries plays its own role in either recording input or sending the data to the indicated email. After importing these libraries, some important constants must be defined so that the keylogger will know where to send the data that has been logged, how to send it (i.e. which protocol to use), and what to include in the message. As seen in the image to the left, the sender and receiving email must be included as well as the password to the sending email so that the keylogger can access it. Also, there is the option to customize the message that is being sent, so in the future, more data and information can be included in the message. Finally below the message is the port number that the keylogger will use to send the data. I used port 25, which is the default port for SMTP but this program will also work on ports 465 and 587. However, the method of connecting to the SMTP server will be different. For reference to how to set up the keylogger using the other protocol look at this: https://realpython.com/python-send-email/. Now that the constants have been added, a write function must be defined. This write function will simply open the file called keylogger.txt or create it if it does not exist on the computer. the logic for how the keylogger will start and stop recording what the user types must then be added. In order for the keylogger to start logging what the user has typed, there must be some logic that tells the logger when the “enter” key is pressed, and if pressed a new line starts, or else the character is just written into the .txt file. However, there are some exceptions which include backspace, tab, and other non-character keys, so that when pressed it will log that non-character key in the file. After starting the logging another function must be added to stop the logging process. I simply added that when the “esc” key is pressed the keylogger will return false meaning that it would stop logging keystrokes into the .txt file. I used the “esc” key because it was convenient for me, however, this key can be changed to whatever the user would prefer. Although the start and stop commands have been defined for the keylogger, no logic has been indicated that tells the keylogger how to start and stop listening. This piece of logic was indicated in the next couple of lines where the keyboard library is used to indicate that when a key is pressed the keylogger should start logging and on release the logger should stop. Then the logger is instructed to update the keylogger.txt file by adding what it recorded. All the code so far allows for the keylogger to function however there is no way for the keylogger to send an email with the .txt file so it must be indicated how the logger will communicate with the email server. As seen in the image below, a connection to the SMTP email server must be defined and then the “login” and “sendmail” commands allow the keylogger to actually log in to the email provided in the constants section and then send the email to the provided destination email. Then the connection is broken as it quits from the server and the data is in the Gmail account provided.
The first challenge that I faced was that I had a very difficult time installing Pynput, which is one of the required libraries. This was because my default python interpreter path was incorrect and because of this, my IDE could not access this library to get input from the users. The second challenge that I faced what using the correct port number and using the correct associated method for connecting the respective SMTP server. However, this was resolved by using some online resources that clarified the difference between these ports.