forked from jabb3rd/RouterOS_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readcdb.py
79 lines (69 loc) · 1.83 KB
/
readcdb.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
#!/usr/bin/python3
import binascii, struct, sys
if len(sys.argv) != 2:
print('Usage %s <filename.cdb>' % sys.argv[0])
exit(1)
filename = sys.argv[1]
# Addresses.cdb file signature
signature = b'\x0d\xf0\x1d\xc0'
# Block signature
M2 = b'\x4d\x32'
# Data types
MT_DWORD = 0x08
MT_BOOL_FALSE = 0x00
MT_BOOL_TRUE = 0x01
MT_ARRAY = 0x88
MT_STRING = 0x21
MT_BYTE = 0x09
MT_BOOL = {MT_BOOL_FALSE: False, MT_BOOL_TRUE: True}
# Addressbook field names
ADDR_BOOK_FIELD = {1: 'host', 2: 'login', 3: 'password', 4: 'note', 6: 'session', 8: 'group', 11: 'romon-agent'}
with open(filename, mode='rb') as file:
content = file.read()
ptr = 0
if content[ptr:ptr+4] != signature:
print('Bad signature in', filename)
exit(1)
ptr += 4
block_no = 0
while ptr < len(content):
block_size = struct.unpack('<I', content[ptr:ptr+4])[0]
print('Record #%s' % block_no)
ptr += 4
if content[ptr:ptr+2] != M2:
print('Bad block #%s' % block_no)
exit(1)
block_start = ptr
ptr += 2
while ptr < (block_size + block_start):
record_code = struct.unpack('<I', content[ptr:ptr+3] + b'\x00')[0]
ptr += 3
record_type = ord(content[ptr:ptr+1])
ptr += 1
# Skip records with the following types: DWORD, BYTE, ARRAY
if record_type == MT_DWORD:
ptr += 4
elif record_type == MT_BYTE:
ptr += 1
elif record_type == MT_BOOL_FALSE or record_type == MT_BOOL_TRUE:
ptr += 0
elif record_type == MT_ARRAY:
length = struct.unpack('<H', content[ptr:ptr+2])[0]
ptr += 2
element = 0
while element < length:
element += 1
ptr += 4
# Process strings
elif record_type == MT_STRING:
length = ord(content[ptr:ptr+1])
ptr += 1
value = content[ptr:ptr+length]
ptr += length
try:
decoded_value = value.decode('UTF-8')
except:
decoded_value = value
print('%s = %s' % (ADDR_BOOK_FIELD[record_code], decoded_value))
block_no += 1
print('')