3.2 Writing a Keylogger in Python Part 2

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
1 hour 12 minutes
Difficulty
Intermediate
CEU/CPE
2
Video Transcription
00:00
Hello and welcome to another ethical hacking tools with python video.
00:04
Today we continue working on our key logger and python.
00:08
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.
00:16
So, um,
00:20
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.
00:35
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.
00:48
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
00:59
keyboard
01:00
because this is where the keyboard, the methods are
01:04
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,
01:17
so we'll just say death releasing key. And it's gonna take key as a parameter.
01:23
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,
01:44
we'll just return
01:47
false.
01:49
Okay, um, now we want to start the listener. So before that, let's actually have ah, print, uh,
01:57
to the screen, telling us that the listener is starting. So we'll say print,
02:00
then on the new line will say, started listening, Dr Todd, and also move on to a new line
02:07
and then actually start the listener
02:12
with a with
02:15
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
02:30
on on release. It's gonna take
02:32
the function, as you might have guessed. So on release
02:38
is gonna take the releasing a key
02:42
function. Okay. And this is gonna be so with listener as listener, we're gonna say listener doll join.
02:51
All right, so, um,
02:54
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
03:10
or you'll be sending up to the FTP server at a chosen specific time, like
03:15
once a day or once per hour.
03:19
All right, so now let's get into coding descending to FTP.
03:24
So add another statement, print statement saying that,
03:29
um, connecting
03:31
to the FTP
03:34
and sending the data daughter thought and then we need to. Instead, she ate an FTP session so says equals.
03:44
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
03:55
1 45
03:58
with the user name MSF admin
04:00
End the password. MSF admin.
04:04
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.
04:15
So then we need to open the file and read buying a remote so we'll open the output file.
04:23
Uh, which is K log rest stop D x d
04:28
in read finery mode.
04:30
And then, um, we'll use the store binary method to save the following the server. So we're gonna say cess store binary,
04:40
and we're actually going to use the store command
04:44
to store Kellogg rest a t x t.
04:46
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.
04:58
And then as a good practice, we close the file souls. They filed up close
05:02
and also terminate the FDP session. So says stop, quit.
05:09
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
05:27
what key are we using to stop the key logger from listening?
05:30
Is it the control key?
05:33
Is it be the escape key Or is it C shift Plus? All plus e.
05:38
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.
05:53
Then we also coated the functionality toe handle, sending the output file to an FTP server.
06:00
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.
Up Next