Skip to content

Commit

Permalink
add visual map option --map to FindMy_client.py
Browse files Browse the repository at this point in the history
  • Loading branch information
biemster committed Sep 16, 2022
1 parent 7816c64 commit dad63cf
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions FindMy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def decode_tag(data):

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--hours', help='only show reports not older than these hours', type=int, default=24)
parser.add_argument('-p', '--prefix', help='only use keyfiles starting with this prefix', default='')
parser.add_argument('-m', '--map', help='show map using OSM', default=False, action='store_true')
args = parser.parse_args()

ids = {}
Expand All @@ -57,9 +57,7 @@ def decode_tag(data):
else:
print("Couldn't find key pair in", keyfile)

unixEpoch = int(datetime.datetime.now().strftime('%s'))
startdate = unixEpoch - 60 * 60 * args.hours
data = '{"search": [{"endDate": %d, "startDate": %d, "ids": %s}]}' % ((unixEpoch -978307200) *1000000, (startdate -978307200)*1000000, list(ids.keys()))
data = '{"search": [{%s"ids": %s}]}' % ('' if args.map else '"startDate": 0, ', list(ids.keys()))

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
Expand All @@ -83,25 +81,35 @@ def decode_tag(data):

# the following is all copied from https://github.com/hatomist/openhaystack-python, thanks @hatomist!
timestamp = bytes_to_int(data[0:4])
if timestamp + 978307200 >= startdate:
eph_key = ec.EllipticCurvePublicKey.from_encoded_point(ec.SECP224R1(), data[5:62])
shared_key = ec.derive_private_key(priv, ec.SECP224R1(), default_backend()).exchange(ec.ECDH(), eph_key)
symmetric_key = sha256(shared_key + b'\x00\x00\x00\x01' + data[5:62])
decryption_key = symmetric_key[:16]
iv = symmetric_key[16:]
enc_data = data[62:72]
tag = data[72:]
eph_key = ec.EllipticCurvePublicKey.from_encoded_point(ec.SECP224R1(), data[5:62])
shared_key = ec.derive_private_key(priv, ec.SECP224R1(), default_backend()).exchange(ec.ECDH(), eph_key)
symmetric_key = sha256(shared_key + b'\x00\x00\x00\x01' + data[5:62])
decryption_key = symmetric_key[:16]
iv = symmetric_key[16:]
enc_data = data[62:72]
tag = data[72:]

decrypted = decrypt(enc_data, algorithms.AES(decryption_key), modes.GCM(iv, tag))
res = decode_tag(decrypted)
res['timestamp'] = timestamp + 978307200
res['isodatetime'] = datetime.datetime.fromtimestamp(res['timestamp']).isoformat()
res['key'] = names[report['id']]
res['goog'] = 'https://maps.google.com/maps?q=' + str(res['lat']) + ',' + str(res['lon'])
found.add(res['key'])
ordered.append(res)
print('%d reports used.' % len(ordered))
decrypted = decrypt(enc_data, algorithms.AES(decryption_key), modes.GCM(iv, tag))
res = decode_tag(decrypted)
res['timestamp'] = timestamp + 978307200
res['isodatetime'] = datetime.datetime.fromtimestamp(res['timestamp']).isoformat()
res['key'] = names[report['id']]
res['goog'] = 'https://maps.google.com/maps?q=' + str(res['lat']) + ',' + str(res['lon'])
found.add(res['key'])
ordered.append(res)
ordered.sort(key=lambda item: item.get('timestamp'))
for rep in ordered: print(rep)

if args.map:
import folium,webbrowser
iconcolors = ['red','blue','green','purple','pink','orange','beige','darkred','darkblue','darkgreen','darkpurple','lightred','lightblue','lightgreen','cadetblue','gray','lightgray','black']
osmap = folium.Map((ordered[-1]['lat'],ordered[-1]['lon']), zoom_start=15)
for rep in ordered:
dt = rep["isodatetime"].split('T')
popup = folium.Popup(folium.IFrame(html=f'<h1>{rep["key"]}</h1> <h3>{dt[0]}</h3> <h3>{dt[1]}</h3>', width=150, height=150))
osmap.add_child(folium.Marker(location=(rep['lat'],rep['lon']), popup=popup, icon=folium.Icon(color=iconcolors[list(found).index(rep['key'])])))
osmap.save('/tmp/tags.html')
webbrowser.open('file:///tmp/tags.html')
else:
for rep in ordered: print(rep)
print('found: ', list(found))
print('missing: ', [key for key in names.values() if key not in found])

0 comments on commit dad63cf

Please sign in to comment.