3.2 Writing a Keylogger in Python Part 2
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
1 hour 12 minutes
Hello and welcome to another ethical hacking tools with python video.
Today we continue working on our key logger and python.
If you don't know what I'm talking about, please make sure towards the previous lesson, which is the first in this module. Now let's move to the code editor.
what do we have so far? We've configured the logging to save key events with time stamped into a text file. And we also defined a pressing key function to output key press events into the logging.
What will do further as to the final release key function, we also need to start listening to key events. And we also need to call the functionality that's going to send our output file to an FTP server of our choice.
Okay, I want to start with a few minor corrections to this previous code. So here where we import pimp what we want to import from pin put dot
because this is where the keyboard, the methods are
all right. And then when writing the file name, uh, here there is a missing parenthesis at the beginning. Okay, Now let's move on to the finding the releasing a key function,
so we'll just say death releasing key. And it's gonna take key as a parameter.
Eso here will want to stop listening for events when pressing the escape key. So we'll say if he equals, equals key escape, then we need to return false. Which is how the been put library knows that we want to terminate the listening process. So if ke equals equals key escape,
we'll just return
Okay, um, now we want to start the listener. So before that, let's actually have ah, print, uh,
to the screen, telling us that the listener is starting. So we'll say print,
then on the new line will say, started listening, Dr Todd, and also move on to a new line
and then actually start the listener
with a with
listener statement, which is gonna take two parameters. So it's going to take on press and on release and on press is gonna take the function pressing key
on on release. It's gonna take
the function, as you might have guessed. So on release
is gonna take the releasing a key
function. Okay. And this is gonna be so with listener as listener, we're gonna say listener doll join.
All right, so, um,
this is going to happen. The listening process is going to take place Until we pressed the escape key. I'm using the escape key press to stop just to show you a clean way of how it works. However, in a real world, the scenario you to stop the key logger after some arbitrary time
or you'll be sending up to the FTP server at a chosen specific time, like
once a day or once per hour.
All right, so now let's get into coding descending to FTP.
So add another statement, print statement saying that,
to the FTP
and sending the data daughter thought and then we need to. Instead, she ate an FTP session so says equals.
So we're gonna use the FTP live module, FT. Peak, and we're gonna connect to the I P off the vulnerable machine. One I need to 1 68 68
with the user name MSF admin
End the password. MSF admin.
Okay, so we have the i p the user and the password of the vulnerable machine, which is installed on my system. And I'm going to tell you more about that in the next lesson.
So then we need to open the file and read buying a remote so we'll open the output file.
Uh, which is K log rest stop D x d
in read finery mode.
And then, um, we'll use the store binary method to save the following the server. So we're gonna say cess store binary,
and we're actually going to use the store command
to store Kellogg rest a t x t.
So that's gonna be the name of the files on the server. And what we want to save in that file is what we have here opened, okay.
And then as a good practice, we close the file souls. They filed up close
and also terminate the FDP session. So says stop, quit.
And that's all there is. So this is actually the code for a very basic but yet really powerful key logger and python with the pin put module. Now comes the fun part, actually testing it. But before we finish this lesson, let's do a quick knowledge, Jack. So
what key are we using to stop the key logger from listening?
Is it the control key?
Is it be the escape key Or is it C shift Plus? All plus e.
So this is actually really simple. We're using the escape key to stop the key logger. So be escape is the correct answer. What did we do today? We've coated another function, releasing a key to handle key events.
Then we also coated the functionality toe handle, sending the output file to an FTP server.
Next, we're going to actually test how this key logger works. My name is Christian and I'm looking forward to seeing you in the next video.