-
Notifications
You must be signed in to change notification settings - Fork 37
/
mapi.py
225 lines (190 loc) · 7.81 KB
/
mapi.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
"MAPI attribute definitions"
import logging
import sys
from decimal import Decimal
from .util import (
apptime,
dbl64,
float32,
guid,
int8,
int16,
int32,
int64,
systime,
uint8,
uint16,
uint32,
uint64,
)
from . import properties
if sys.hexversion < 0x03000000:
range = xrange
logger = logging.getLogger("mapi-decode")
SZMAPI_UNSPECIFIED = 0x0000 # MAPI Unspecified
SZMAPI_NULL = 0x0001 # MAPI null property
SZMAPI_SHORT = 0x0002 # MAPI short (signed 16 bits)
SZMAPI_INT = 0x0003 # MAPI integer (signed 32 bits)
SZMAPI_FLOAT = 0x0004 # MAPI float (4 bytes)
SZMAPI_DOUBLE = 0x0005 # MAPI double
SZMAPI_CURRENCY = 0x0006 # MAPI currency (64 bits)
SZMAPI_APPTIME = 0x0007 # MAPI application time
SZMAPI_ERROR = 0x000A # MAPI error (32 bits)
SZMAPI_BOOLEAN = 0x000B # MAPI boolean (16 bits)
SZMAPI_OBJECT = 0x000D # MAPI embedded object
SZMAPI_INT8BYTE = 0x0014 # MAPI 8 byte signed int
SZMAPI_STRING = 0x001E # MAPI string
SZMAPI_UNICODE_STRING = 0x001F # MAPI unicode-string (null terminated)
SZMAPI_SYSTIME = 0x0040 # MAPI time (64 bits)
SZMAPI_CLSID = 0x0048 # MAPI OLE GUID
SZMAPI_BINARY = 0x0102 # MAPI binary
SZMAPI_BEATS_THE_HELL_OUTTA_ME = 0x0033
MULTI_VALUE_FLAG = 0x1000
GUID_EXISTS_FLAG = 0x8000
def decode_mapi(data, codepage='cp1252', starting_offset=None):
"decode MAPI types"
dataLen = len(data)
attrs = []
offset = starting_offset or 0
num_properties = uint32(data, offset); offset += 4
try:
for i in range(num_properties):
if offset >= dataLen:
logger.warn("Skipping property '%i'" % i)
continue
attr_type = uint16(data, offset); offset += 2
attr_name = uint16(data, offset); offset += 2
# logger.debug("Attribute type: 0x%4.4x", attr_type)
# logger.debug("Attribute name: 0x%4.4x", attr_name)
guid_id = ''
guid_name = None
guid_prop = None
if attr_name >= GUID_EXISTS_FLAG:
guid_id = guid(data, offset); offset += 16
kind = uint32(data, offset); offset += 4
if kind == 0:
guid_prop = uint32(data, offset); offset += 4
else:
iidLen = uint32(data, offset); offset += 4
r = iidLen % 4
if r != 0:
iidLen += 4 - r
guid_name = data[offset : offset + iidLen].decode('utf-16')
offset += iidLen
num_mv_properties = None
if MULTI_VALUE_FLAG & attr_type:
attr_type ^= MULTI_VALUE_FLAG
num_mv_properties = uint32(data, offset); offset += 4
for mv in range(num_mv_properties or 1):
try:
attr_data, offset = parse_property(data, offset, attr_name, attr_type, codepage, num_mv_properties)
attr = TNEFMAPI_Attribute(attr_type, attr_name, attr_data, guid_id, guid_name, guid_prop)
attrs.append(attr)
# print(attr)
except Exception:
logger.debug("Attribute type: 0x%4.4x", attr_type)
logger.debug("Attribute name: 0x%4.4x (%s)", attr_name, TNEFMAPI_Attribute.codes.get(attr_name))
raise
if (num_mv_properties or 1) % 2 and attr_type in (SZMAPI_SHORT, SZMAPI_BOOLEAN):
offset += 2
except Exception as e:
import traceback
stack = traceback.format_exc()
logger.error('decode_mapi Exception %s' % e)
logger.debug(stack)
if starting_offset is not None:
return (offset, attrs)
else:
return attrs
def parse_property(data, offset, attr_name, attr_type, codepage, is_multi):
attr_data = None
if attr_type in (SZMAPI_SHORT, SZMAPI_BOOLEAN):
attr_data = data[offset : offset + 2]; offset += 2
elif attr_type in (SZMAPI_INT, SZMAPI_FLOAT, SZMAPI_ERROR):
attr_data = data[offset : offset + 4]; offset += 4
elif attr_type in (SZMAPI_DOUBLE, SZMAPI_APPTIME, SZMAPI_CURRENCY, SZMAPI_INT8BYTE, SZMAPI_SYSTIME):
attr_data = data[offset : offset + 8]; offset += 8
elif attr_type == SZMAPI_CLSID:
attr_data = data[offset : offset + 16]; offset += 16
elif attr_type in (SZMAPI_STRING, SZMAPI_UNICODE_STRING, SZMAPI_OBJECT, SZMAPI_BINARY, SZMAPI_UNSPECIFIED):
if is_multi:
num_vals = 1
else:
num_vals = uint32(data, offset); offset += 4
attr_data = []
for j in range(num_vals):
length = uint32(data, offset); offset += 4
r = length % 4
if r != 0:
length += 4 - r
if attr_type == SZMAPI_UNICODE_STRING:
attr_data.append(data[offset : offset + length].decode('utf-16'))
elif attr_type == SZMAPI_STRING:
attr_data.append(data[offset : offset + length].decode(codepage))
else:
attr_data.append(data[offset : offset + length])
offset += length
else:
raise ValueError("Unknown MAPI type 0x%4.4x" % attr_type)
return attr_data, offset
class TNEFMAPI_Attribute(object):
"""represents a mapi attribute
Property reference docs:
https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/mapping-canonical-property-names-to-mapi-names#tagged-properties
https://fossies.org/linux/libpst/xml/MAPI_definitions.pdf
Most these properties represent PidTag properties
"""
codes = properties.CODE_TO_NAME
OutlookGuid = '05133f00aa00da98101b450b6ed8da90'
AppointmentGuid = '46000000000000c00000000000062002'
def __init__(self, attr_type, name, data, guid_id, guid_name=None, guid_prop=None):
self.attr_type = attr_type
self.name = name
self.raw_data = data
self.guid = guid_id
self.guid_name = guid_name
self.guid_prop = guid_prop
if self.guid_name:
self.guid_name = self.guid_name.rstrip('\x00')
@property
def data(self):
if self.attr_type == SZMAPI_NULL:
return None
elif self.attr_type == SZMAPI_SHORT:
return int16(self.raw_data)
elif self.attr_type == SZMAPI_INT:
return int32(self.raw_data)
elif self.attr_type == SZMAPI_FLOAT:
return float32(self.raw_data)
elif self.attr_type == SZMAPI_DOUBLE:
return dbl64(self.raw_data)
elif self.attr_type == SZMAPI_CURRENCY:
return Decimal(int64(self.raw_data)) / Decimal(10000)
elif self.attr_type == SZMAPI_APPTIME:
return apptime(self.raw_data)
elif self.attr_type == SZMAPI_ERROR:
return uint32(self.raw_data)
elif self.attr_type == SZMAPI_BOOLEAN:
return bool(uint16(self.raw_data))
elif self.attr_type == SZMAPI_INT8BYTE:
return int8(self.raw_data)
elif self.attr_type == SZMAPI_SYSTIME:
return systime(self.raw_data)
elif self.attr_type == SZMAPI_CLSID:
return guid(self.raw_data)
elif self.attr_type == SZMAPI_BINARY:
return b''.join([s.rstrip(b'\x00') for s in self.raw_data])
elif self.attr_type in (SZMAPI_STRING, SZMAPI_UNICODE_STRING):
return ''.join([s.rstrip('\x00') for s in self.raw_data])
else:
return self.raw_data
@property
def name_str(self):
return (
self.guid_name or
TNEFMAPI_Attribute.codes.get(self.name) or
TNEFMAPI_Attribute.codes.get(self.guid_prop) or
hex(self.guid_prop or self.name)
)
def __str__(self):
return "<ATTR: %s>" % (self.name_str)