-
Notifications
You must be signed in to change notification settings - Fork 0
/
xod_to_pdf.py
152 lines (119 loc) · 5.59 KB
/
xod_to_pdf.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
import sys
import os
import zlib
import binascii
import struct
import argparse
from PDFNetPython3.PDFNetPython import PDFDoc, Convert, SDFDoc, PDFNet
from cryptography.hazmat.primitives.ciphers.algorithms import AES
def main(args):
print("Ola")
parser = argparse.ArgumentParser(description='Converts password-protected XOD files to PDF files')
parser.add_argument('xod_file', type=str, help='path to the encrypted XOD file')
parser.add_argument('password', type=str, help='the password to decrypt the file, in the form of 01-02-03-...')
args = parser.parse_args()
# alright.
fileHandler = open(args.xod_file, "rb")
password = args.password
file = bytearray(fileHandler.read())
new_file = bytearray()
new_file[:] = file
centeral_directory_end = file[-22:]
centeral_directory_location = to_dword(centeral_directory_end[16:16 + 4])
centeral_directory_size = to_dword(centeral_directory_end[12:12 + 4])
centeral_directory = file[centeral_directory_location: centeral_directory_location + centeral_directory_size]
print("[+] file loaded. starting to decrypt the file...")
while len(centeral_directory) >= 42:
file_name = centeral_directory[46:46 + to_short(centeral_directory[28:28 + 2])]
start = to_dword(centeral_directory[42:42 + 4]) + 30 + len(file_name) + to_dword(centeral_directory[30:30 + 4])
size = to_dword(centeral_directory[20:20 + 4])
key, iv, encrypted_data = key_and_iv_from_password(str(new_file[start:start + size]), str(file_name), password)
aes = AES.new(key, AES.MODE_CBC, iv)
print("[+] decrypting part " + file_name + " with key " + toHex(key))
decrypted_data = aes.decrypt(encrypted_data)
new_centeral_directory_index = fix_offsets(new_file, file_name, len(decrypted_data))
del new_file[start:start + size]
new_file[start:start] = bytearray(decrypted_data)
centeral_directory = new_file[new_centeral_directory_index:]
if centeral_directory[0] != 0x50:
print("problem")
break
file_header_size = 46 + len(file_name) + to_short(centeral_directory[30:30 + 2]) + to_short(
centeral_directory[32:32 + 2])
centeral_directory = centeral_directory[file_header_size:]
print("[+] everything decrypted. converting to pdf...")
new_file_handler = open("new.xod", "wb")
new_file_handler.write(new_file)
new_file_handler.close()
toPdf("new.xod", "result.pdf")
os.remove("new.xod")
print("[+] Done!")
# some helper functions
def toPdf(xodFilename, outputPdfName):
PDFNet.Initialize()
pdf_doc = PDFDoc()
Convert.ToPdf(pdf_doc, xodFilename)
pdf_doc.Save(outputPdfName, SDFDoc.e_remove_unused)
pdf_doc.Close()
def toXod(pdfFilename, outputXodName):
pdf_doc = PDFDoc()
Convert.ToXod(pdfFilename, outputXodName)
def toHex(byte_array):
if byte_array is str:
byte_array = bytearray(byte_array)
hex = str(binascii.hexlify(byte_array))
formatted_hex = ':'.join(hex[i:i + 2] for i in range(0, len(hex), 2))
return formatted_hex
def to_dword(byte_array):
return struct.unpack('I', byte_array)[0]
def to_short(byte_array):
return struct.unpack('H', byte_array)[0]
def key_and_iv_from_password(encrypted_data, filename, password):
key = bytearray([0] * 16)
for i in range(16):
key[i] = i
if i < len(password):
key[i] |= ord(password[i])
g = len(filename) + i - 16
if 0 <= g:
key[i] |= ord(filename[g])
iv = []
for i in range(16):
iv.append(encrypted_data[i])
encrypted_data = encrypted_data[16:]
return (str(key), str(bytearray(iv)), encrypted_data)
def fix_offsets(array, alterted_filename, new_size):
centeral_directory_end = array[-22:]
centeral_directory_location = to_dword(centeral_directory_end[16:16 + 4])
centeral_directory_size = to_dword(centeral_directory_end[12:12 + 4])
file_offset_from_centeral_directory = 0
index = 0
offset_fix_delta = 0
# fixing the alterted-file's local header
while index < centeral_directory_location:
file_name = array[index + 30: index + 30 + to_short(array[index + 26:index + 26 + 2])]
if file_name == alterted_filename:
offset_fix_delta = new_size - to_dword(array[index + 18:index + 18 + 4])
array[index + 18:index + 18 + 4] = struct.pack('I', new_size)
break
index += 30 + len(file_name) + to_short(array[index + 28:index + 28 + 2]) + to_dword(
array[index + 18:index + 18 + 4])
# fixing the centeral directory
index = centeral_directory_location
should_fix_offset = False
while index < centeral_directory_location + centeral_directory_size:
file_name = array[index + 46:index + 46 + to_short(array[index + 28:index + 28 + 2])]
if file_name == alterted_filename:
should_fix_offset = True
array[index + 20:index + 20 + 4] = struct.pack('I', new_size)
file_offset_from_centeral_directory = index - centeral_directory_location
elif should_fix_offset:
array[index + 42:index + 42 + 4] = struct.pack('I', to_dword(
array[index + 42:index + 42 + 4]) + offset_fix_delta)
file_header_size = 46 + len(file_name) + to_short(array[index + 30:index + 30 + 2]) + to_short(
array[index + 32:index + 32 + 2])
index += file_header_size
array[-6:-6 + 4] = struct.pack('I', to_dword(array[-6:-6 + 4]) + offset_fix_delta)
return to_dword(array[-6:-6 + 4]) + file_offset_from_centeral_directory
if __name__ == "__main__":
main(sys.argv)