3.1 Writing a Keylogger in Python Part 1

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. 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?
00:13
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.
00:31
So a double functionality key logger plus automatic upload to a server.
00:36
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
00:52
at a very high level, writing only a few lines of code to actually get a functional key arbor.
00:57
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.
01:11
What's great about PIN put is that its operating system in variant, meaning that it doesn't
01:18
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.
01:26
Also for uploading to the FTP server, we're using the FDP lib
01:33
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.
01:53
For now, please look into the resource is and references document accompany this video
01:59
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.
02:19
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
02:30
okay, and then we're also going to import the logging library for saving to file. So import logging
02:38
all right, s o first will define the directory or the folder of the key logger output,
02:45
which is going to be the current directory. So let's say logger equals
02:50
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,
03:00
and we're gonna do the basic conflict. The file name
03:05
is gonna be the logger. So our current directory plus, let's name it K log
03:12
minus rest on T X t.
03:15
Okay. And of course, this is
03:17
in quotes
03:20
K log minus, rest a dx dy
03:23
Okay.
03:23
And then the level is gonna be logging
03:29
debug.
03:31
The format
03:34
is gonna be equal. Tow percent ask time
03:38
as per cent.
03:42
So
03:44
colon
03:47
percent
03:51
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
04:08
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
04:27
we actually string defy the key. And then when it catches an attribute, terrible, say except
04:34
attributes error.
04:36
And we'll print a generic message so we'll say print
04:42
a special
04:44
key
04:45
Zero
04:46
has been pressed dot
04:50
So let's say also dot format
04:54
key, of course. All right,
04:56
so
04:57
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,
05:14
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
05:29
if you've been paying close attention, you know that we're saving the results in the President Working Directory or the current folder.
05:35
So the correct answer is C, of course.
05:40
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
05:55
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.
Up Next