forked from 0ki/mikrotik-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode_backup.py
executable file
·65 lines (46 loc) · 1.65 KB
/
decode_backup.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
#!/usr/bin/python
# (C) Kirils Solovjovs, 2017
import sys,os,re
from struct import unpack
if len(sys.argv) > 1:
if len(sys.argv) > 2:
dir = sys.argv[2]
else:
dir = sys.argv[1]+"_contents/"
else:
raise Exception("Usage: "+sys.argv[0]+" <RouterOS.backup> [output_folder]")
if not os.access(sys.argv[1], os.R_OK):
raise Exception("Can't read file "+sys.argv[1])
if not os.path.exists(dir):
os.makedirs(dir)
if not os.access(dir, os.W_OK):
raise Exception("Directory "+dir+" not writeable")
if os.listdir(dir)!=[]:
raise Exception("Directory "+dir+" not empty")
realsize=os.path.getsize(sys.argv[1])
with open(sys.argv[1], 'rb') as backup:
hdr = backup.read(4)
if realsize>=8 and hdr == "\xEF\xA8\x91\x72":
raise Exception("Encrypted RouterOS backup files are not supported.")
if realsize<8 or hdr != "\x88\xAC\xA1\xB1":
raise Exception("Not a RouterOS backup file.")
matchsize, = unpack("<I",backup.read(4))
if matchsize != realsize:
raise Exception("File is damaged.")
while backup.tell()+4 < matchsize:
file_name = backup.read(unpack("<I",backup.read(4))[0])
index_cont = backup.read(unpack("<I",backup.read(4))[0])
data_cont = backup.read(unpack("<I",backup.read(4))[0])
print '%3d entries, %8d bytes, ./%s' % (len(index_cont)/4, len(data_cont),file_name)
file_name=re.sub('\.{2,}','_',file_name); #would not wanna be writing all over the place
try:
os.makedirs(dir+"/"+os.path.dirname(file_name))
except OSError as exc:
if exc.errno == 17:
pass
fo = open(dir+"/"+file_name+".idx", "wb")
fo.write(index_cont);
fo.close()
fo = open(dir+"/"+file_name+".dat", "wb")
fo.write(data_cont);
fo.close()