-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash_esp32.py
121 lines (101 loc) · 3.71 KB
/
flash_esp32.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import serial
import json
import os
import msvcrt
import base64
import subprocess
import random
import string
command = "esptool --baud 921600 write_flash 0x1000 sniffer_bootloader.bin 0x8000 sniffer_partitions.bin 0x10000 airtag_sniffer.bin"
try:
subprocess.run(command, shell=True)
print("sniffer installed..")
print("proceeding...")
except Exception as e:
print(e)
def gen(addr_hex, payload_hex):
addr = bytearray.fromhex(addr_hex)
key = bytearray(28)
key[0:6] = addr[0:6]
key[0] &= 0b00111111
payload = bytearray.fromhex(payload_hex)
key[6:28] = payload[7:29]
key[0] |= (payload[29] << 6)
return key
os.system('cls')
def randomness(length):
characters = string.ascii_lowercase + string.digits
return ''.join(random.choice(characters) for _ in range(length))
def select_serial_port():
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
print("Available ports:")
for i, port in enumerate(ports):
print(f"{i + 1}: {port.device}")
selection = input("Select the port number: ")
try:
selection = int(selection)
if 1 <= selection <= len(ports):
return ports[selection - 1].device
else:
print("Invalid selection. Please choose a number from 1 to", len(ports))
return select_serial_port()
except ValueError:
print("Invalid input. Please enter a number.")
return select_serial_port()
esp32_port = select_serial_port()
airtags = []
ser = serial.Serial(esp32_port, 115200, timeout=1)
try:
while True:
data = ser.readline().strip().decode('utf-8')
if msvcrt.kbhit():
key = msvcrt.getwche()
if key == '\r': # Enter key
break
if data:
try:
os.system('cls')
json_data = json.loads(data)
index_to_insert = 0
try:
for i in range(len(airtags)):
if airtags[i]["RSSI"] < json_data["RSSI"]:
index_to_insert = i
break
except:
index_to_insert = 0
airtags.insert(index_to_insert, json_data)
count = 0
for item in airtags:
count += 1
rssi = item['RSSI']
payload = item['Payload_Data']
mac = item['MAC_Address']
print(f"{count}:")
print("RSSI:", rssi)
print("Payload data:", payload)
print("MAC:", mac, "\n")
print("Press Enter if Airtag found")
except json.JSONDecodeError as e:
print("Error decoding JSON:", e)
except KeyboardInterrupt:
print("KeyboardInterrupt: Exiting...")
finally:
ser.close()
choice = input("Which Number?: ")
try:
print("Selected Airtag:", airtags[(int(choice))-1]["MAC_Address"])
actual_payload = (airtags[(int(choice))-1]["Payload_Data"]).replace(" ", "")
actual_mac = (airtags[(int(choice))-1]["MAC_Address"]).replace(" ", "").replace(":", "")
adv_key = bytes.fromhex(gen(actual_mac.lower(), actual_payload.lower()).hex())
filename = f"{randomness(6)}.key"
with open(filename, "wb") as f:
f.write(adv_key)
subprocess.run(["pip", "install", "esptool"])
command = f"esptool --baud 921600 write_flash 0x1000 bootloader.bin 0x8000 partitions.bin 0x10000 clone_firmware.bin 0xe000 {filename}"
subprocess.run(command, shell=True)
print("enjoy your airtag :)")
os.remove(f"{filename}")
except Exception as e:
print("Incorrect value", e)