-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcraxcel.py
333 lines (267 loc) · 11.6 KB
/
craxcel.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
craXcel ("crack-cel") - removes password protection from Microsoft Office XML
based applications.
"""
import abc
import argparse
import os
import shutil
import uuid
import zipfile
import mmap
import re
from lxml import etree
APP_NAME = 'craXcel'
APP_ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
APP_SAVE_DIR = os.path.join(APP_ROOT_DIR, 'unlocked')
APP_TEMP_DIR = os.path.join(APP_ROOT_DIR, 'temp')
MICROSOFT_EXCEL = 'MicrosoftExcel'
MICROSOFT_WORD = 'MicrosoftWord'
MICROSOFT_POWERPOINT = 'MicrosoftPowerpoint'
SUPPORTED_EXTENSIONS = {
'.xlsx': MICROSOFT_EXCEL,
'.xlsm': MICROSOFT_EXCEL,
'.docx': MICROSOFT_WORD,
'.docm': MICROSOFT_WORD,
'.pptx': MICROSOFT_POWERPOINT,
'.pptm': MICROSOFT_POWERPOINT
}
class FileInfo():
"""
Class that encapsulates information related to a specified filepath.
"""
def __init__(self, filepath):
self.full_name = filepath
self.name = os.path.basename(filepath)
self.directory, self.extension = os.path.splitext(filepath)
class MicrosoftOfficeFile(metaclass=abc.ABCMeta):
"""
Base class containing common logic for unlocking Microsoft Office XML
based applications.
"""
def __init__(self, user_args, filepath, xml_root_dir_name):
self._file = FileInfo(filepath)
self._args = user_args
# Creates a universally unique path in the app temp directory
self._temp_processing_dir = os.path.join(APP_TEMP_DIR, str(uuid.uuid4()))
# The root directory where XML files are stored when unpackaged
self._xml_root_dir = os.path.join(self._temp_processing_dir, xml_root_dir_name)
self._vba_filepath = os.path.join(self._xml_root_dir, 'vbaProject.bin')
def unlock(self):
"""
Unlocks the specified file according to arguments passed in by the user.
"""
self._unpackage()
self._remove_application_specific_protection()
if self._args.vba:
self._remove_vba_protection()
self._repackage()
if not self._args.debug:
self._cleanup()
print('Completed unlocking file!')
def _unpackage(self):
"""
Treats the target file as if it were a ZIP file and extracts the
underlying XMLs.
"""
zipfile.ZipFile(self._file.full_name,'r').extractall(self._temp_processing_dir)
print('File unpacked...')
def _repackage(self):
"""
Takes the unpackaged XML files and repackages them into a ZIP file
with the original file's extension restored. This makes the newly
repackaged file openable by the original application.
"""
file_suffix = f'_{APP_NAME}{self._file.extension}'
filename = self._file.name.replace(self._file.extension, file_suffix)
unlocked_filepath = os.path.join(APP_SAVE_DIR, filename)
filepaths = self._get_file_listing(self._temp_processing_dir)
with zipfile.ZipFile(unlocked_filepath,'w') as repackaged_zip:
for filepath in filepaths:
rel_filepath = filepath.replace(self._temp_processing_dir,'')
repackaged_zip.write(filepath,arcname=rel_filepath)
print('File repackaged...')
def _cleanup(self):
"""
Recursively deletes all files in the temporary processing directory.
"""
shutil.rmtree(self._temp_processing_dir)
print('Cleaning up temporary files...')
def _get_file_listing(self, directory):
"""
Retrieves a list of files from the specified directory.
"""
filepaths = []
for root, folder, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
filepaths.append(filepath)
return filepaths
def _remove_protection_element(self, xml_filepath, tag_names_to_remove):
"""
Reads through the XML in the specified filepath and removes the
elements containing the specified tag names.
"""
tree = etree.parse(xml_filepath)
root = tree.getroot()
for element in root.iter():
for tag_name in tag_names_to_remove:
if tag_name in element.tag:
root.remove(element)
tree.write(xml_filepath, encoding='UTF-8', xml_declaration=True)
def _remove_vba_protection(self):
"""
Reads the file's underlying vbaProject.bin file in HEX form,
replacing the string responsible for protecting the file with a
password.
"""
if os.path.isfile(self._vba_filepath):
with open(self._vba_filepath, 'r+b') as f:
m = mmap.mmap(f.fileno(), 0)
m[:] = re.sub(b'DPB', b'DPx', m[:])
print('VBA protection removed...')
@abc.abstractmethod
def _remove_application_specific_protection(self):
"""
Removes protection specific to the target application. Abstract method
that requires implementation in all child classes.
"""
class MicrosoftExcel(MicrosoftOfficeFile):
"""
Class encapsulating all specifc fields and logic required for the unlocking
of Microsoft Excel XML based files.
"""
def __init__(self, user_args, locked_filepath):
super().__init__(user_args, locked_filepath, 'xl')
self._workbook_xml_filepath = os.path.join(self._xml_root_dir, 'workbook.xml')
self._worksheet_xml_dir = os.path.join(self._xml_root_dir, 'worksheets')
self._workbook_tag_names = ['fileSharing', 'workbookProtection']
self._worksheet_tag_names = ['sheetProtection']
def _remove_application_specific_protection(self):
if self._args.workbook:
self._remove_workbook_protection()
elif self._args.worksheet:
self._remove_worksheet_protection()
else:
self._remove_workbook_protection()
self._remove_worksheet_protection()
def _remove_workbook_protection(self):
"""
Takes the workbook XML and removes the protections within.
"""
self._remove_protection_element(self._workbook_xml_filepath, self._workbook_tag_names)
print('Workbook protection removed...')
def _remove_worksheet_protection(self):
"""
Iterates through the directory holding the worksheet XMLs and removes
the protections in each file.
"""
worksheet_xml_filepaths = self._get_file_listing(self._worksheet_xml_dir)
for xml_filepath in worksheet_xml_filepaths:
self._remove_protection_element(xml_filepath, self._worksheet_tag_names)
print('Worksheet protection removed...')
class MicrosoftWord(MicrosoftOfficeFile):
"""
Class encapsulating all specifc fields and logic required for the unlocking
of Microsoft Word XML based files.
"""
def __init__(self, user_args, locked_filepath):
super().__init__(user_args, locked_filepath, 'word')
self._document_xml_filepath = os.path.join(self._xml_root_dir, 'settings.xml')
self._document_tag_names = ['writeProtection', 'documentProtection']
def _remove_application_specific_protection(self):
self._remove_protection_element(self._document_xml_filepath, self._document_tag_names)
print('Document protection removed...')
class MicrosoftPowerpoint(MicrosoftOfficeFile):
"""
Class encapsulating all specifc fields and logic required for the unlocking
of Microsoft Powerpoint XML based files.
"""
def __init__(self, user_args, locked_filepath):
super().__init__(user_args, locked_filepath, 'ppt')
self._presentation_xml_filepath = os.path.join(self._xml_root_dir, 'presentation.xml')
self._presentation_tag_names = ['modifyVerifier']
def _remove_application_specific_protection(self):
self._remove_protection_element(self._presentation_xml_filepath, self._presentation_tag_names)
print('Presentation protection removed...')
def Main():
"""
Main entry point of the application.
"""
args = handle_args()
print('\ncraXcel started')
if args.list:
print('\nList mode enabled')
filepaths = read_list_of_filepaths(args.filepath)
print(f'{len(filepaths)} files detected')
else:
filepaths = [args.filepath]
files_unlocked = 0
for locked_filepath in filepaths:
print(f'\nChecking file {locked_filepath}...')
if os.path.isfile(locked_filepath):
file_info = FileInfo(locked_filepath)
# Checks the extension of the file against the dictionary of
# supported applications, returning the application name.
try:
detected_application = SUPPORTED_EXTENSIONS[file_info.extension]
except:
detected_application = 'unsupported'
# Uses the deteted application to create the correct instance.
if detected_application == MICROSOFT_EXCEL:
cxl = MicrosoftExcel(args, locked_filepath)
elif detected_application == MICROSOFT_WORD:
cxl = MicrosoftWord(args, locked_filepath)
elif detected_application == MICROSOFT_POWERPOINT:
cxl = MicrosoftPowerpoint(args, locked_filepath)
elif file_info.extension == '.txt':
print('File rejected. Did you mean to use list mode? Try "python craxcel.py --help" for more info.')
break
else:
print('File rejected. Unsupported file extension.')
break
print('File accepted...')
try:
cxl.unlock()
files_unlocked += 1
except Exception:
print(f'An error occured while unlocking {locked_filepath}')
else:
print('File not found...')
print(f'\nSummary: {files_unlocked}/{len(filepaths)} files unlocked')
print('\ncraXcel finished')
def read_list_of_filepaths(list_filepath):
"""
Reads a .txt file of line seperated filepaths and returns them as a list.
"""
return [line.rstrip() for line in open(list_filepath, 'r')]
def handle_args():
"""
Handles the command line arguments passed in by the user, returns them
as an args object.
"""
parser = argparse.ArgumentParser(description='Remove Workbook and Worksheet protection on Microsoft Excel files.')
parser.add_argument('filepath', help='Target filepath')
excel_group = parser.add_mutually_exclusive_group()
excel_group.add_argument('-ws', '--worksheet', action='store_true',
help='microsoft excel files: unlocks the Worksheets only (leaves Workbook Protection intact)')
excel_group.add_argument('-wb', '--workbook', action='store_true',
help='microsoft excel files: unlocks the Workbook only (leaves Worksheet Protection intact)')
parser.add_argument('-vba', '--vba', action='store_true',
help='removes projection from the VBA project of the file')
parser.add_argument('--debug', action='store_true',
help='retains the temp folder. Useful for dubugging exceptions')
parser.add_argument('--list', action='store_true',
help='unlock a list of files specified in a line-seperated .txt file')
return parser.parse_args()
def create_directory_structure():
"""
Creates the directory structure if it doesn't already exist.
"""
if not os.path.exists(APP_SAVE_DIR):
os.mkdir(APP_SAVE_DIR)
if not os.path.exists(APP_TEMP_DIR):
os.mkdir(APP_TEMP_DIR)
if __name__ == '__main__':
create_directory_structure()
Main()