| |
---|
| | # !!! make sure bluetoothd runs in --compat mode before executing this script !!! |
---|
| | def pair_with_pin(start_time, pin, time_limit=60): # int(time.time()), pin - \d{4}, time_limit - approximate pairing window time in seconds, it might take up to 2x (nested timeout conditions) |
---|
| | "exectutes pairing with entered PIN on bluetooth adapter side" |
---|
| | try: |
---|
| | |
---|
| | |
---|
| | newpid = os.fork() |
---|
| | if newpid == 0: |
---|
| | time.sleep(5) |
---|
| | ''' |
---|
| | Start bluepy stuff |
---|
| | ''' |
---|
| | subprocess.call(['hciconfig','hci0','down']) |
---|
| |
---|
| | |
---|
| | deviceMAC = open('ctf_mac.txt').read() |
---|
| | p = btle.Peripheral(deviceMAC) |
---|
| | svc=p.getServiceByUUID("0000180d-0000-1000-8000-00805f9b34fb") |
---|
| | print ("Attached to peripheral") |
---|
| | print ("Attached to peripheral (pid 0)") |
---|
| | hex1 = p.readCharacteristic(0x2C) |
---|
| | hex2 = binascii.b2a_hex(hex1) |
---|
| | hexlif2 = str(binascii.unhexlify(hex2)) |
---|
| | print("Flag: "+hexlif2) |
---|
| | p.disconnect() |
---|
| | exit() |
---|
| | else: |
---|
| | |
---|
| | ''' |
---|
| | Start actual pair stuff |
---|
| | Start actual pair stuff |
---|
| | ''' |
---|
| | subprocess.call(['hciconfig','hci0','sspmode', '0']) |
---|
| | |
---|
| | # bluetoothctl |
---|
| | print("Pairing") |
---|
| | child = pexpect.spawn('bluetoothctl') |
---|
| | child.logfile = open("/tmp/mylog", "w") |
---|
| | child.expect("#") |
---|
| | child.sendline('agent off') # might be unnecessary |
---|
| |
---|
| | child.sendline('discoverable on') |
---|
| | child.expect("discoverable on succeeded") |
---|
| | child.sendline('default-agent') |
---|
| | child.sendline('remove 3c:71:bf:f1:ef:c6') |
---|
| | child.sendline('scan on') |
---|
| | child.expect("Device 3C:71:BF:F1:EF:C6 FLAG_3") |
---|
| | child.sendline('pair 3c:71:bf:f1:ef:c6') |
---|
| | |
---|
| | child.expect('Request passkey', timeout = time_limit ) # timeout <= PAIRING_TIME_LIMIT to keep some kind of logic |
---|
| | print ('Sending PIN: ' + pin) |
---|
| |
---|
| | trust_mac = 'trust ' + re.search(r'(?:[0-9a-fA-F]:?){12}.+$', child.before).group(0) # extract MAC from last line, one with 'Paired: Yes' |
---|
| | child.sendline(trust_mac) # optionally add device to trusted |
---|
| | child.expect('trust succeeded', timeout = 10) |
---|
| | pairing_status = True |
---|
| | child.sendline('remove 3c:71:bf:f1:ef:c6') |
---|
| | else: # i == 1 |
---|
| | print('wrong PIN, retrying if time will allow') |
---|
| | except pexpect.EOF: |
---|
| | print ('!!!!!!!! EOF') |
---|
| |
---|
| | |
---|
| | status = pair_with_pin(int(time.time()), str(BT_PIN), PAIRING_TIME_LIMIT) |
---|
| | if status == True: |
---|
| | print('Pairing successful') |
---|
| | |
---|
| | |