-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopendocument_security.py
More file actions
executable file
·221 lines (197 loc) · 7.13 KB
/
opendocument_security.py
File metadata and controls
executable file
·221 lines (197 loc) · 7.13 KB
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
#!/usr/bin/env python
"""
OpenDocument Security
Copyright 2020-2023 Nicolas BEGUIER
Licensed under the Apache License, Version 2.0
Written by Nicolas BEGUIER (nicolas_beguier@hotmail.com)
"""
# Standard library imports
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import logging
from os.path import exists
import sys
import zipfile
# Third party library imports
import xml.etree.ElementTree as ET
# Debug
# from pdb import set_trace as st
VERSION = '1.3.1'
logging.basicConfig(format='%(message)s')
LOGGER = logging.getLogger('opendocument-security')
def get_content_zip_path(content_path):
"""
Returns the content path inside zip archive
"""
content_path = content_path.replace('/./', '/')
if content_path.startswith('/'):
content_path = content_path[1:]
if content_path == '':
return 'content.xml'
return '{}/content.xml'.format(content_path)
def display_macro_od(od_zipfile):
"""
This function returns the list of existing macro
"""
display_banner = False
for filepath in od_zipfile.namelist():
if 'Basic/' in filepath:
if not display_banner:
LOGGER.critical('> This OpenDocument contains macro !')
display_banner = True
LOGGER.critical(' > %s', filepath)
if 'Scripts/' in filepath:
if not display_banner:
LOGGER.critical('> This OpenDocument contains macro !')
display_banner = True
LOGGER.critical(' > %s', filepath)
return display_banner
def display_macro_flat(od_file):
"""
This function returns the list of existing macro
"""
display_banner = False
with open(od_file, 'r') as content:
raw_content = content.read()
try:
root = ET.fromstring(raw_content)
except ET.ParseError:
LOGGER.critical('This file seems corrupted')
return False
ooo_prefix = get_tag(raw_content, 'xmlns:ooo')
list_library_embedded = list()
find_rec(root, '{'+ooo_prefix+'}'+'library-embedded', list_library_embedded)
if list_library_embedded:
if not display_banner:
LOGGER.critical('> This Flat OpenDocument contains macro !')
display_banner = True
LOGGER.critical(' > %s', list_library_embedded)
return display_banner
def find_rec(node, element, result):
"""
This function is parsing the xml node to update the
result by a list of found elements
"""
for item in node:
for match_item in item.findall(element):
result.append(match_item)
find_rec(item, element, result)
return result
def get_event_listeners(content, script_prefix, indent):
"""
This function returns every script:event-listeners in the document
"""
result = list()
find_rec(content, '{'+script_prefix+'}'+'event-listener', result)
for script in result:
LOGGER.critical('%s> Found event listener: %s', indent, script.attrib)
def get_ole_objects(content, draw_prefix):
"""
This function returns the nested ole objects in the document
"""
result = list()
return find_rec(content, '{'+draw_prefix+'}'+'object', result)
def get_tag(content, tag):
"""
This function returns the tag value
"""
if isinstance(content, bytes):
content = content.decode('utf-8', 'ignore')
for i in content.split(' '):
if i.startswith(tag+'='):
return i.split('"')[-2]
return None
def display_event_listener_flat(od_file, indent=''):
"""
This function is parsing the file and displaying macro in event-listener
"""
LOGGER.warning('%s> Entering in %s', indent, od_file)
with open(od_file, 'r') as content:
raw_content = content.read()
try:
root = ET.fromstring(raw_content)
except ET.ParseError:
LOGGER.critical('This file seems corrupted')
return False
office_prefix = get_tag(raw_content, 'xmlns:office')
script_prefix = get_tag(raw_content, 'xmlns:script')
if not office_prefix:
LOGGER.warning('%s> Exiting %s', indent, od_file)
return False
if script_prefix:
get_event_listeners(root, script_prefix, indent+' ')
LOGGER.warning('%s> Exiting %s', indent, od_file)
return True
def display_event_listener_od(od_zipfile, content_path, indent=''):
"""
This function is parsing the file and displaying macro in event-listener
"""
content_zip_path = get_content_zip_path(content_path)
LOGGER.warning('%s> Entering in %s', indent, content_zip_path)
with od_zipfile.open(content_zip_path) as content:
raw_content = content.read()
try:
root = ET.fromstring(raw_content)
except ET.ParseError:
LOGGER.critical('This file seems corrupted')
return False
office_prefix = get_tag(raw_content, 'xmlns:office')
script_prefix = get_tag(raw_content, 'xmlns:script')
draw_prefix = get_tag(raw_content, 'xmlns:draw')
xlink_prefix = get_tag(raw_content, 'xmlns:xlink')
if not office_prefix:
LOGGER.warning('%s> Exiting %s', indent, content_zip_path)
return False
if script_prefix:
get_event_listeners(root, script_prefix, indent+' ')
if draw_prefix and xlink_prefix:
for ole_object in get_ole_objects(root, draw_prefix):
draw_href = ole_object.attrib['{'+xlink_prefix+'}'+'href']
display_event_listener_od(
od_zipfile,
'{}/{}'.format(content_path, draw_href),
indent=indent+' ')
LOGGER.warning('%s> Exiting %s', indent, content_zip_path)
return True
def main(od_path):
"""
Main function
"""
if not exists(od_path):
LOGGER.critical('%s is not a file...', od_path)
sys.exit(1)
is_flat_opendocument = False
try:
od_zip_file = zipfile.ZipFile(od_path, 'r')
except IOError:
LOGGER.critical('This file seems corrupted')
sys.exit(1)
except zipfile.LargeZipFile as err:
LOGGER.critical(err)
sys.exit(1)
except zipfile.BadZipfile:
is_flat_opendocument = True
if is_flat_opendocument:
LOGGER.warning('> Parsing Flat OpenDocument %s', od_path)
is_macro = display_macro_flat(od_path)
if is_macro:
display_event_listener_flat(od_path)
LOGGER.warning('> Closing Flat OpenDocument %s', od_path)
else:
LOGGER.warning('> Parsing OpenDocument %s', od_path)
is_macro = display_macro_od(od_zip_file)
if is_macro:
display_event_listener_od(od_zip_file, '')
LOGGER.warning('> Closing OpenDocument %s', od_path)
od_zip_file.close()
if __name__ == '__main__':
PARSER = ArgumentParser(
formatter_class=ArgumentDefaultsHelpFormatter
)
PARSER = ArgumentParser()
PARSER.add_argument('--version', action='version', version=VERSION)
PARSER.add_argument(
'od_path',
action='store',
help='An opendocument path')
ARGS = PARSER.parse_args()
main(ARGS.od_path)