summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xhexnoise.py88
-rw-r--r--keymap.py176
2 files changed, 239 insertions, 25 deletions
diff --git a/hexnoise.py b/hexnoise.py
index 31d7695..df71d93 100755
--- a/hexnoise.py
+++ b/hexnoise.py
@@ -38,8 +38,8 @@ def send_packet(ser, data, width=16):
def receive_packet(ser, width=16):
packet = ser.read_until(b'\0')
data = cobs.decode(packet[:-1])
- #print(f'\033[93mReceived {len(data)} bytes\033[0m')
- #hexdump(print, data, width)
+ print(f'\033[93mReceived {len(data)} bytes\033[0m')
+ hexdump(print, data, width)
return data
if __name__ == '__main__':
@@ -54,6 +54,23 @@ if __name__ == '__main__':
ser = serial.Serial(args.serial, args.baudrate)
+ import uinput
+ ALL_KEYS = [ v for k, v in uinput.ev.__dict__.items() if k.startswith('KEY_') ]
+ MODIFIERS = [
+ uinput.ev.KEY_LEFTCTRL,
+ uinput.ev.KEY_LEFTSHIFT,
+ uinput.ev.KEY_LEFTALT,
+ uinput.ev.KEY_LEFTMETA,
+ uinput.ev.KEY_RIGHTCTRL,
+ uinput.ev.KEY_RIGHTSHIFT,
+ uinput.ev.KEY_RIGHTALT,
+ uinput.ev.KEY_RIGHTMETA,
+ ]
+ map_modifiers = lambda x: [ mod for i, mod in enumerate(MODIFIERS) if x & (1<<i) ]
+ import keymap
+ map_regular = { v: getattr(uinput.ev, k) for k, v in keymap.__dict__.items() if k.startswith('KEY_') }
+ map_regulars = lambda keycodes: [ map_regular[kc] for kc in keycodes if kc != 0 and kc in map_regular ]
+
from noise.connection import NoiseConnection, Keypair
from noise.exceptions import NoiseInvalidMessage
@@ -81,29 +98,50 @@ if __name__ == '__main__':
print('Handshake finished, handshake hash:')
hexdump(print, proto.get_handshake_hash(), args.width)
- def noise_rx(received):
+ old_kcs = set()
+ def noise_rx(received, ui):
+ global old_kcs
+
data = proto.decrypt(received)
- #print('Decrypted data:')
- #hexdump(print, data, args.width)
+ print('Decrypted data:')
+ hexdump(print, data, args.width)
- while True:
- try:
- received = receive_packet(ser, args.width)
+ rtype, rlen, *report = data
+ if rtype != 1 or rlen != 8:
+ return
+
+ modbyte, _reserved, *keycodes = report
+ keys = map_modifiers(modbyte) + map_regulars(keycodes)
+ print('Emitting:', keys)
+ keyset = set(keys)
+
+ for key in keyset - old_kcs:
+ ui.emit(key, 1, syn=False)
+ for key in old_kcs - keyset:
+ ui.emit(key, 0, syn=False)
+ ui.syn()
+
+ old_kcs = keyset
+
+ with uinput.Device(ALL_KEYS) as ui:
+ while True:
try:
- noise_rx(received)
- except NoiseInvalidMessage as e:
- orig_n = proto.noise_protocol.cipher_state_decrypt.n
- print('Invalid noise message', e)
- for n in [orig_n+1, orig_n+2, orig_n+3]:
- try:
- proto.noise_protocol.cipher_state_decrypt.n = n
- noise_rx(received)
- print(f' Recovered. n={n}')
- break
- except NoiseInvalidMessage as e:
- pass
- else:
- print(' Unrecoverable.')
- proto.noise_protocol.cipher_state_decrypt.n = orig_n
- except Exception as e:
- print('Invalid framing:', e)
+ received = receive_packet(ser, args.width)
+ try:
+ noise_rx(received, ui)
+ except NoiseInvalidMessage as e:
+ orig_n = proto.noise_protocol.cipher_state_decrypt.n
+ print('Invalid noise message', e)
+ for n in [orig_n+1, orig_n+2, orig_n+3]:
+ try:
+ proto.noise_protocol.cipher_state_decrypt.n = n
+ noise_rx(received, ui)
+ print(f' Recovered. n={n}')
+ break
+ except NoiseInvalidMessage as e:
+ pass
+ else:
+ print(' Unrecoverable.')
+ proto.noise_protocol.cipher_state_decrypt.n = orig_n
+ except Exception as e:
+ print('Invalid framing:', e)
diff --git a/keymap.py b/keymap.py
new file mode 100644
index 0000000..efef507
--- /dev/null
+++ b/keymap.py
@@ -0,0 +1,176 @@
+KEY_RESERVED = 0x00
+KEY_A = 0x04
+KEY_B = 0x05
+KEY_C = 0x06
+KEY_D = 0x07
+KEY_E = 0x08
+KEY_F = 0x09
+KEY_G = 0x0a
+KEY_H = 0x0b
+KEY_I = 0x0c
+KEY_J = 0x0d
+KEY_K = 0x0e
+KEY_L = 0x0f
+KEY_M = 0x10
+KEY_N = 0x11
+KEY_O = 0x12
+KEY_P = 0x13
+KEY_Q = 0x14
+KEY_R = 0x15
+KEY_S = 0x16
+KEY_T = 0x17
+KEY_U = 0x18
+KEY_V = 0x19
+KEY_W = 0x1a
+KEY_X = 0x1b
+KEY_Y = 0x1c
+KEY_Z = 0x1d
+
+KEY_1 = 0x1e
+KEY_2 = 0x1f
+KEY_3 = 0x20
+KEY_4 = 0x21
+KEY_5 = 0x22
+KEY_6 = 0x23
+KEY_7 = 0x24
+KEY_8 = 0x25
+KEY_9 = 0x26
+KEY_0 = 0x27
+
+KEY_ENTER = 0x28
+KEY_ESC = 0x29
+KEY_BACKSPACE = 0x2a
+KEY_TAB = 0x2b
+KEY_SPACE = 0x2c
+KEY_MINUS = 0x2d
+KEY_EQUAL = 0x2e
+KEY_LEFTBRACE = 0x2f
+KEY_RIGHTBRACE = 0x30
+KEY_BACKSLASH = 0x31
+KEY_SEMICOLON = 0x33
+KEY_APOSTROPHE = 0x34
+KEY_GRAVE = 0x35
+KEY_COMMA = 0x36
+KEY_DOT = 0x37
+KEY_SLASH = 0x38
+KEY_CAPSLOCK = 0x39
+
+KEY_F1 = 0x3a
+KEY_F2 = 0x3b
+KEY_F3 = 0x3c
+KEY_F4 = 0x3d
+KEY_F5 = 0x3e
+KEY_F6 = 0x3f
+KEY_F7 = 0x40
+KEY_F8 = 0x41
+KEY_F9 = 0x42
+KEY_F10 = 0x43
+KEY_F11 = 0x44
+KEY_F12 = 0x45
+
+KEY_SYSRQ = 0x46
+KEY_SCROLLLOCK = 0x47
+KEY_PAUSE = 0x48
+KEY_INSERT = 0x49
+KEY_HOME = 0x4a
+KEY_PAGEUP = 0x4b
+KEY_DELETE = 0x4c
+KEY_END = 0x4d
+KEY_PAGEDOWN = 0x4e
+KEY_RIGHT = 0x4f
+KEY_LEFT = 0x50
+KEY_DOWN = 0x51
+KEY_UP = 0x52
+
+KEY_NUMLOCK = 0x53
+KEY_KPSLASH = 0x54
+KEY_KPASTERISK = 0x55
+KEY_KPMINUS = 0x56
+KEY_KPPLUS = 0x57
+KEY_KPENTER = 0x58
+KEY_KP1 = 0x59
+KEY_KP2 = 0x5a
+KEY_KP3 = 0x5b
+KEY_KP4 = 0x5c
+KEY_KP5 = 0x5d
+KEY_KP6 = 0x5e
+KEY_KP7 = 0x5f
+KEY_KP8 = 0x60
+KEY_KP9 = 0x61
+KEY_KP0 = 0x62
+KEY_KPDOT = 0x63
+
+KEY_102ND = 0x64
+KEY_COMPOSE = 0x65
+KEY_POWER = 0x66
+KEY_KPEQUAL = 0x67
+
+KEY_F13 = 0x68
+KEY_F14 = 0x69
+KEY_F15 = 0x6a
+KEY_F16 = 0x6b
+KEY_F17 = 0x6c
+KEY_F18 = 0x6d
+KEY_F19 = 0x6e
+KEY_F20 = 0x6f
+KEY_F21 = 0x70
+KEY_F22 = 0x71
+KEY_F23 = 0x72
+KEY_F24 = 0x73
+
+KEY_OPEN = 0x74
+KEY_HELP = 0x75
+KEY_PROPS = 0x76
+KEY_FRONT = 0x77
+KEY_STOP = 0x78
+KEY_AGAIN = 0x79
+KEY_UNDO = 0x7a
+KEY_CUT = 0x7b
+KEY_COPY = 0x7c
+KEY_PASTE = 0x7d
+KEY_FIND = 0x7e
+KEY_MUTE = 0x7f
+KEY_VOLUMEUP = 0x80
+KEY_VOLUMEDOWN = 0x81
+KEY_KPCOMMA = 0x85
+KEY_RO = 0x87
+KEY_KATAKANAHIRAGANA = 0x88
+KEY_YEN = 0x89
+KEY_HENKAN = 0x8a
+KEY_MUHENKAN = 0x8b
+KEY_KPJPCOMMA = 0x8c
+KEY_HANGEUL = 0x90
+KEY_HANJA = 0x91
+KEY_KATAKANA = 0x92
+KEY_HIRAGANA = 0x93
+KEY_ZENKAKUHANKAKU = 0x94
+KEY_KPLEFTPAREN = 0xb6
+KEY_KPRIGHTPAREN = 0xb7
+KEY_LEFTCTRL = 0xe0
+KEY_LEFTSHIFT = 0xe1
+KEY_LEFTALT = 0xe2
+KEY_LEFTMETA = 0xe3
+KEY_RIGHTCTRL = 0xe4
+KEY_RIGHTSHIFT = 0xe5
+KEY_RIGHTALT = 0xe6
+KEY_RIGHTMETA = 0xe7
+KEY_PLAYPAUSE = 0xe8
+KEY_STOPCD = 0xe9
+KEY_PREVIOUSSONG = 0xea
+KEY_NEXTSONG = 0xeb
+KEY_EJECTCD = 0xec
+KEY_VOLUMEUP = 0xed
+KEY_VOLUMEDOWN = 0xee
+KEY_MUTE = 0xef
+KEY_WWW = 0xf0
+KEY_BACK = 0xf1
+KEY_FORWARD = 0xf2
+KEY_STOP = 0xf3
+KEY_FIND = 0xf4
+KEY_SCROLLUP = 0xf5
+KEY_SCROLLDOWN = 0xf6
+KEY_EDIT = 0xf7
+KEY_SLEEP = 0xf8
+KEY_COFFEE = 0xf9
+KEY_REFRESH = 0xfa
+KEY_CALC = 0xfb