btaptap (5cf4c9cb441aa735be0ac77081632db31c920700) btaptap (48fe012ac445727d6a0bf0074d6f218d5616862f)
1#!/usr/bin/env python
1#!/usr/bin/env python3
2#
3# Copyright 2009 Joshua Wright, Michael Ossmann
4#
5# This file is part of gr-bluetooth
6#
7# gr-bluetooth is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2, or (at your option)

--- 305 unchanged lines hidden (view full) ---

315 pass
316 try:
317 code = USBHID_MAP[scancode]
318 except KeyError:
319 return "[Reserved]"
320 return code
321
322def usage():
2#
3# Copyright 2009 Joshua Wright, Michael Ossmann
4#
5# This file is part of gr-bluetooth
6#
7# gr-bluetooth is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2, or (at your option)

--- 305 unchanged lines hidden (view full) ---

315 pass
316 try:
317 code = USBHID_MAP[scancode]
318 except KeyError:
319 return "[Reserved]"
320 return code
321
322def usage():
323 print >>sys.stderr, "Usage: btaptap [-r pcapfile.pcap | -e ellisysfile.csv] [-c count] [-h]\n"
323 print("Usage: btaptap [-r pcapfile.pcap | -e ellisysfile.csv] [-c count] [-h]\n", file=sys.stderr)
324 sys.exit(0)
325
326def parse_l2cap_keydata(l2cappkt):
327 global active_keys
328
329 TRANS_HDR_IN_DATA = 0xA1
330 REPORT_ID_KEYBOARD = 0x01
331 CTRL = 1

--- 55 unchanged lines hidden (view full) ---

387 sys.stdout.write(hid2ascii(keystroke, mod & SHIFT))
388
389 active_keys = keycodes
390
391def parse_ellisys_export(exportfile):
392 try:
393 cap = open(exportfile, "r")
394 except (OSError, IOError) as e:
324 sys.exit(0)
325
326def parse_l2cap_keydata(l2cappkt):
327 global active_keys
328
329 TRANS_HDR_IN_DATA = 0xA1
330 REPORT_ID_KEYBOARD = 0x01
331 CTRL = 1

--- 55 unchanged lines hidden (view full) ---

387 sys.stdout.write(hid2ascii(keystroke, mod & SHIFT))
388
389 active_keys = keycodes
390
391def parse_ellisys_export(exportfile):
392 try:
393 cap = open(exportfile, "r")
394 except (OSError, IOError) as e:
395 print >>sys.stderr, "Unable to open Ellisys capture file."
395 print("Unable to open Ellisys capture file.", file=sys.stderr)
396 return
397
398 # Check to make sure the CSV file header matches out expectations
399 hdr=cap.readline()
400 if (hdr != ELLISYS_CSV_HDR):
396 return
397
398 # Check to make sure the CSV file header matches out expectations
399 hdr=cap.readline()
400 if (hdr != ELLISYS_CSV_HDR):
401 print >>sys.stderr, "Invalid CSV file (does not match Ellisys export format)"
401 print("Invalid CSV file (does not match Ellisys export format)", file=sys.stderr)
402 return
403
404 for packetline in cap.xreadlines():
405 try:
406 (edepth, etime, ename, edata) = packetline.replace('"', '').strip().split(",")
407 except ValueError:
408 continue
409

--- 34 unchanged lines hidden (view full) ---

444 type = (ord(btbbhdr[0]) & BTBBHDR_TYPE_MASK) >> BTBBHDR_TYPE_SHIFT
445 if type != BTBBHDR_TYPE_DM1:
446 return
447
448 # Keyboard keystrokes are only seen in L2CAP packets 14 bytes long
449 btbbpayloadhdr = ord(packet[23])
450 llid = btbbpayloadhdr & (BTBBPAYLOADHDR_LLID_MASK) >> BTBBPAYLOADHDR_LLID_SHIFT
451 l2clen = (btbbpayloadhdr & BTBBPAYLOADHDR_LEN_MASK) >> BTBBPAYLOADHDR_LEN_SHIFT
402 return
403
404 for packetline in cap.xreadlines():
405 try:
406 (edepth, etime, ename, edata) = packetline.replace('"', '').strip().split(",")
407 except ValueError:
408 continue
409

--- 34 unchanged lines hidden (view full) ---

444 type = (ord(btbbhdr[0]) & BTBBHDR_TYPE_MASK) >> BTBBHDR_TYPE_SHIFT
445 if type != BTBBHDR_TYPE_DM1:
446 return
447
448 # Keyboard keystrokes are only seen in L2CAP packets 14 bytes long
449 btbbpayloadhdr = ord(packet[23])
450 llid = btbbpayloadhdr & (BTBBPAYLOADHDR_LLID_MASK) >> BTBBPAYLOADHDR_LLID_SHIFT
451 l2clen = (btbbpayloadhdr & BTBBPAYLOADHDR_LEN_MASK) >> BTBBPAYLOADHDR_LEN_SHIFT
452 #print "Debug btbbpayloadhdr 0x%02x, llid %d, l2clen %d"%(btbbpayloadhdr, llid, l2clen)
452 #print("Debug btbbpayloadhdr 0x%02x, llid %d, l2clen %d"%(btbbpayloadhdr, llid, l2clen))
453 if llid != LLID_L2CAP or l2clen < 14:
454 return
455
456 parse_l2cap_keydata(packet[24:38])
457
458def parse_hci_keydata(packet):
459
460 HCI_TYPE_ACL_DATA = 2

--- 24 unchanged lines hidden (view full) ---

485 arg_count = int(sys.argv.pop(1))
486 if op == '-e':
487 arg_ellisysfile = sys.argv.pop(1)
488 if op == '-h':
489 usage()
490 sys.exit(0)
491
492 if (arg_ellisysfile == None and arg_pcapfile == None):
453 if llid != LLID_L2CAP or l2clen < 14:
454 return
455
456 parse_l2cap_keydata(packet[24:38])
457
458def parse_hci_keydata(packet):
459
460 HCI_TYPE_ACL_DATA = 2

--- 24 unchanged lines hidden (view full) ---

485 arg_count = int(sys.argv.pop(1))
486 if op == '-e':
487 arg_ellisysfile = sys.argv.pop(1)
488 if op == '-h':
489 usage()
490 sys.exit(0)
491
492 if (arg_ellisysfile == None and arg_pcapfile == None):
493 print >>sys.stderr, "Must specify a libpcap capture or an Ellisys CSV file"
493 print("Must specify a libpcap capture or an Ellisys CSV file", file=sys.stderr)
494 usage()
495 sys.exit(0)
496
497 if arg_pcapfile != None:
498 cap = PcapReader(arg_pcapfile)
499
500 while arg_count != packetcount:
501 try:
502 (pheader, packet) = cap.pnext()
503 pkttime = pheader[0]
504 packetcount+=1
505
506 if cap.datalink() == DLT_EN10MB:
507 parse_bb_keydata(packet)
508 elif cap.datalink() == DLT_BLUETOOTH_HCI_H4:
509 parse_hci_keydata(packet)
510 else:
494 usage()
495 sys.exit(0)
496
497 if arg_pcapfile != None:
498 cap = PcapReader(arg_pcapfile)
499
500 while arg_count != packetcount:
501 try:
502 (pheader, packet) = cap.pnext()
503 pkttime = pheader[0]
504 packetcount+=1
505
506 if cap.datalink() == DLT_EN10MB:
507 parse_bb_keydata(packet)
508 elif cap.datalink() == DLT_BLUETOOTH_HCI_H4:
509 parse_hci_keydata(packet)
510 else:
511 print >>sys.stderr, "Unsupported libpcap data link layer: %d\n" % cap.datalink()
511 print("Unsupported libpcap data link layer: %d\n" % cap.datalink(), file=sys.stderr)
512 except TypeError: # raised when pnext returns Null (end of capture)
513 break
514
515 cap.close()
516
517 if arg_ellisysfile != None:
518 parse_ellisys_export(arg_ellisysfile)
519
520 print
512 except TypeError: # raised when pnext returns Null (end of capture)
513 break
514
515 cap.close()
516
517 if arg_ellisysfile != None:
518 parse_ellisys_export(arg_ellisysfile)
519
520 print