Newer
Older
BLE_CTF_V2 / lvl_07.py
root on 11 Mar 2022 3 KB tidying for public release
  1. #! /usr/bin/python
  2. from __future__ import print_function # import print from python3: end=""
  3. import time
  4. import re
  5. import pexpect # sudo apt-get install python-pexpect
  6. import subprocess
  7. import random
  8. import binascii
  9. import struct
  10. import sys, os, time
  11. import bluepy.btle as btle
  12. '''
  13. Service <uuid=Generic Access handleStart=20 handleEnd=28>
  14. 22 0x16 READ FLAG_7
  15. 24 0x18 READ
  16. 26 0x1A READ
  17. Service <uuid=Heart Rate handleStart=40 handleEnd=65535>
  18. 42 0x2A READ Pair with me
  19.  
  20. a16ee1a4001c66c3a670
  21. '''
  22.  
  23. # !!! make sure bluetoothd runs in --compat mode before executing this script !!!
  24. def pair_with_pin(start_time, time_limit=60): # int(time.time()), time_limit - approximate pairing window time in seconds, it might take up to 2x (nested timeout conditions)
  25. "exectutes pairing on bluetooth adapter side"
  26. try:
  27. '''
  28. Start actual pair stuff
  29. '''
  30. subprocess.call(['hciconfig','hci0','sspmode', '0'])
  31. print("[sp] starting bluetoothctl")
  32. # bluetoothctl
  33. child = pexpect.spawn('bluetoothctl')
  34. child.logfile = open("/tmp/mylog", "w")
  35. child.expect("#")
  36. child.sendline('agent off') # might be unnecessary
  37. child.expect("unregistered")
  38. child.sendline('scan on') # might be unnecessary
  39. child.sendline('agent KeyboardDisplay ')
  40. child.expect("Agent registered")
  41. child.sendline('pairable on')
  42. child.expect("pairable on succeeded")
  43. child.sendline('discoverable on')
  44. child.expect("discoverable on succeeded")
  45. child.sendline('default-agent')
  46. child.sendline('remove 3c:71:bf:f1:ef:c6')
  47. child.sendline('scan on')
  48. child.expect("Device 3C:71:BF:F1:EF:C6 FLAG_3")
  49. child.sendline('pair 3c:71:bf:f1:ef:c6')
  50.  
  51. i = child.expect('Paired: yes', timeout = time_limit)
  52. if i == 0: # found 'Paired: yes' == successful pairing
  53. trust_mac = 'trust ' + re.search(r'(?:[0-9a-fA-F]:?){12}.+$', child.before).group(0) # extract MAC from last line, one with 'Paired: Yes'
  54. child.sendline(trust_mac) # optionally add device to trusted
  55. child.expect('trust succeeded', timeout = 10)
  56. else: # i == 1
  57. print('[sp] Retrying if time will allow')
  58. except pexpect.EOF:
  59. print ('[sp] EOF')
  60. except pexpect.TIMEOUT:
  61. print ('[sp] Timeout')
  62. return True
  63.  
  64. #main program body
  65. PAIRING_TIME_LIMIT = 60
  66.  
  67. subprocess.call(['hciconfig','hci0','down'])
  68. subprocess.call(['hciconfig','hci0','up'])
  69. deviceMAC = open('ctf_mac.txt').read()
  70. p = btle.Peripheral(deviceMAC)
  71. print ("[bp] Attached to peripheral")
  72.  
  73. print("[++] Loading level 07")
  74. hex1 = binascii.unhexlify(str('%0*x' % (4,7)))
  75. p.writeCharacteristic(0x30, hex1, withResponse=False)
  76.  
  77. p.disconnect()
  78.  
  79. status = pair_with_pin(int(time.time()), PAIRING_TIME_LIMIT)
  80. if status == True:
  81. print('[sp] Pairing successful')
  82.  
  83. '''
  84. Start bluepy stuff
  85. '''
  86. time.sleep(2)
  87.  
  88. deviceMAC = open('ctf_mac.txt').read()
  89. p = btle.Peripheral(deviceMAC)
  90. svc=p.getServiceByUUID("0000180d-0000-1000-8000-00805f9b34fb")
  91. print ("[bp] Attached to peripheral")
  92. hex1 = p.readCharacteristic(0x2C)
  93. hex2 = binascii.b2a_hex(hex1)
  94. hexlif2 = str(binascii.unhexlify(hex2))
  95. print("[==] Flag: "+hexlif2)
  96. p.disconnect()
  97. exit()
Buy Me A Coffee