Skip to content

Instantly share code, notes, and snippets.

@0i0
Created February 22, 2019 03:25
Show Gist options
  • Save 0i0/0b0a210a3f73f66a52132dac37adc75f to your computer and use it in GitHub Desktop.
Save 0i0/0b0a210a3f73f66a52132dac37adc75f to your computer and use it in GitHub Desktop.
import re
import io
from configparser import ConfigParser
filename = '/media/lior/Data1/TEMP/005.reg'
def format_mac(macstring):
t = iter(macstring)
return ':'.join(a+b for a,b in zip(t, t)).upper()
with io.open(filename, encoding='utf-16') as f:
data = f.read()
data = re.sub(r'^[^\[]*\n', '', data, flags=re.S)
cfg = ConfigParser(strict=False)
cfg.optionxform=str
cfg.read_string(data)
recieverMAC = cfg.sections()[2].split('\\')[-1]
def format_value(key,val):
if key == 'IRK':
formatted_val = ''.join(val.split(',')).upper()
elif key == 'CSRK':
formatted_val = ''.join(val.split(',')).upper()
elif key == 'LTK':
formatted_val = ''.join(val.split(',')).upper()
elif key == 'EDIV':
formatted_val = int(re.sub(r'\b0+(\d)', r'\1', val),base=16)
elif key == 'ERand':
formatted_val = int(''.join(val.split(',')[::-1]).upper(),base=16)
else:
formatted_val = 'none'
return formatted_val
d = {}
for s in cfg.sections():
if f'{recieverMAC}\\' in s:
MAC = s.split('\\')[-1]
d[MAC] = {'MAC':MAC}
for key in cfg[s]:
tp = val = None
if ':' in cfg[s][key]:
tp, val = cfg[s][key].split(':')
else:
val = cfg[s][key].replace('"', '').replace(r'\\\n', '')
key = re.sub(r'"', '', key, flags=re.S)
d[MAC][key] = format_value(key,val)
records = []
for key, value in d.items():
print(key)
print(
'IRK :' ,value['IRK'] ,'\n',
'CSRK :' ,value['CSRK'] ,'\n',
'LTK :' ,value['LTK'] ,'\n',
'EDIV :' ,value['EDIV'] ,'\n',
'ERand :' ,value['ERand'] ,'\n'
)
records.append(value)
for value in range(len(records)):
if value == 1:
text = f'''[General]
Name=Designer Keyboard
Appearance=0x03c1
AddressType=static
SupportedTechnologies=LE;
Trusted=true
Blocked=false
Services=00001800-0000-1000-8000-00805f9b34fb;00001801-0000-1000-8000-00805f9b34fb;0000180a-0000-1000-8000-00805f9b34fb;0000180f-0000-1000-8000-00805f9b34fb;00001812-0000-1000-8000-00805f9b34fb;
[IdentityResolvingKey]
Key={records[value]['IRK']}
[LocalSignatureKey]
Key={records[value]['CSRK']}
Counter=0
Authenticated=false
[LongTermKey]
Key={records[value]['LTK']}
Authenticated=0
EncSize=16
EDiv={records[value]['EDIV']}
Rand={records[value]['ERand']}
[DeviceID]
Source=2
Vendor=1118
Product=2054
Version=288
[ConnectionParameters]
MinInterval=12
MaxInterval=16
Latency=30
Timeout=300
'''
if value == 1:
text = f'''[General]
Name=Designer Mouse
Appearance=0x03c2
AddressType=static
SupportedTechnologies=LE;
Trusted=true
Blocked=false
Services=00001800-0000-1000-8000-00805f9b34fb;00001801-0000-1000-8000-00805f9b34fb;0000180a-0000-1000-8000-00805f9b34fb;0000180f-0000-1000-8000-00805f9b34fb;00001812-0000-1000-8000-00805f9b34fb;
[IdentityResolvingKey]
Key={records[value]['IRK']}
[LocalSignatureKey]
Key={records[value]['CSRK']}
Counter=0
Authenticated=false
[LongTermKey]
Key={records[value]['LTK']}
Authenticated=0
EncSize=16
EDiv={records[value]['EDIV']}
Rand={records[value]['ERand']}
[DeviceID]
Source=2
Vendor=1118
Product=2053
Version=272
[ConnectionParameters]
MinInterval=6
MaxInterval=6
Latency=60
Timeout=300
'''
file_path = f'/var/lib/bluetooth/{format_mac(recieverMAC)}/{format_mac(records[value]["MAC"])}/info'
print(file_path)
with open(file_path, 'a') as the_file:
the_file.write(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment