3.1 Writing a Keylogger in Python Part 1
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. I'm very excited about what we're going to code over the next few lessons. Now you might wonder, What is that all about?
So we're going to call it a key logger, which is a simple program that is going to record all the keys that are pressed on a computer. And that's actually not all, because we're also going to code a future that will send the key logger log file to an FTP server.
So a double functionality key logger plus automatic upload to a server.
Now, of course, you could further develop this toe, add more functionality, but I'll leave that up to your imagination. So what are the prerequisites? The great thing about this is that there's a library in python cold, then put, which laws us to work
at a very high level, writing only a few lines of code to actually get a functional key arbor.
Now there are many other libraries, more lower level or even basic python that ah lets you do the same thing, but it would require much more quoting, which is not bad, but that's not our purpose. here.
What's great about PIN put is that its operating system in variant, meaning that it doesn't
need customized instructions depending on the OS. If you'd be using other libraries, you would probably need to write different code for a different OS.
Also for uploading to the FTP server, we're using the FDP lib
that comes with the Python standard library. So no additional tool or library needs to be installed, which is actually really convenient. Okay, so you only need to install pen put later on when we test this key logger. You also need a vulnerable machine installed on your system. But I'll tell you more about that when we get there.
For now, please look into the resource is and references document accompany this video
on hug will install pen put. But I'll actually tell you it's a simple as defense stall, Ben put All right, now let's jump right into it. So I'm moving into visual studio code here and were actually gonna import from thin put.
We're gonna import the key and the listener. So we need these methods to handle keyboard events. Then we also need to import ftp lib FTP left
okay, and then we're also going to import the logging library for saving to file. So import logging
all right, s o first will define the directory or the folder of the key logger output,
which is going to be the current directory. So let's say logger equals
empty quotes. Then we need to configure the lock file and how it's going to save the keyboard events. So, uh, we're gonna use logging,
and we're gonna do the basic conflict. The file name
is gonna be the logger. So our current directory plus, let's name it K log
minus rest on T X t.
Okay. And of course, this is
K log minus, rest a dx dy
And then the level is gonna be logging
is gonna be equal. Tow percent ask time
as per cent.
message s. So it's going to display the time stamp plus the keyboard event for each line in the K log that Kellogg rest a t X t file. Okay, Now let's define what happens when pressing a key. So let's say death
pressing key, and it's going to take a key parameter and we'll use a try except for Matt over here. So, uh, first we're going to log everything so we'll make sure the key is a strength. So we'll say logging thought info. And then
we actually string defy the key. And then when it catches an attribute, terrible, say except
And we'll print a generic message so we'll say print
has been pressed dot
So let's say also dot format
key, of course. All right,
let's stop here for now with the coding and continue in the next lesson will further define a key release function. Similar. Tow this. Keep pressing function on dhe, then code the uploading to FTP. All right, now, before we go,
let's actually do a quick knowledge check. So what is the folder in which we save? Ah, the key logger results. Is it the temp folder, the system folder or the current folder? And
if you've been paying close attention, you know that we're saving the results in the President Working Directory or the current folder.
So the correct answer is C, of course.
Now in review. Today we started working on a key. Logger and python were using the pen, but module, ftp lip and also the log in module to save keyboard invest will fall and then send it to an FTP server. F our choice
In the next lesson, we're going to continue working on this key logger in Python. I am Christian and I'm looking forward to seeing you in the next video.