-
Notifications
You must be signed in to change notification settings - Fork 4
/
checksums.py
executable file
·375 lines (306 loc) · 11.7 KB
/
checksums.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import zlib
import hashlib
import binascii
import array
from lxml import etree
from Peach.fixup import Fixup
from Peach.Engine.common import *
class ExpressionFixup(Fixup):
"""
Sometimes you need to perform some math as the fixup. This relation will
take a ref, then a python expression.
"""
def __init__(self, ref, expression):
Fixup.__init__(self)
self.ref = ref
self.expression = expression
def fixup(self):
ref = self.context.findDataElementByName(self.ref)
stuff = ref.getValue()
if stuff is None:
raise Exception("Error: ExpressionFixup was unable to locate "
"[{}]".format(self.ref))
return evalEvent(self.expression,
{"self": self, "ref": ref, "data": stuff}, ref)
class SHA224Fixup(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref).getValue()
if stuff is None:
raise Exception("Error: SHA1Fixup was unable to locate "
"[{}]".format(self.ref))
h = hashlib.sha224()
h.update(stuff)
return h.digest()
class SHA256Fixup(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref).getValue()
if stuff is None:
raise Exception("Error: SHA256Fixup was unable to locate "
"[{}]".format(self.ref))
h = hashlib.sha256()
h.update(stuff)
return h.digest()
class SHA384Fixup(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref).getValue()
if stuff is None:
raise Exception("Error: SHA384Fixup was unable to locate "
"[{}]".format(self.ref))
h = hashlib.sha384()
h.update(stuff)
return h.digest()
class SHA512Fixup(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref).getValue()
if stuff is None:
raise Exception("Error: SHA512Fixup was unable to locate "
"[{}]".format(self.ref))
h = hashlib.sha512()
h.update(stuff)
return h.digest()
class SHA1Fixup(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref).getValue()
if stuff is None:
raise Exception("Error: SHA1Fixup was unable to locate "
"[{}]".format(self.ref))
h = hashlib.sha1()
h.update(stuff)
return h.digest()
class MD5Fixup(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref.getValue())
if stuff is None:
raise Exception("Error: MD5Fixup was unable to locate "
"[{}]".format(self.ref))
h = hashlib.md5()
h.update(stuff)
return h.digest()
class Crc32Fixup(Fixup):
"""
Standard CRC32 as defined by ISO 3309. Used by PNG, ZIP, etc.
"""
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref).getValue()
if stuff is None:
raise Exception("Error: Crc32Fixup was unable to locate "
"[{}]".format(self.ref))
crc = zlib.crc32(stuff)
if crc < 0:
crc = ~crc ^ 0xffffffff
return crc
class LRCFixup(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref).getValue()
if stuff is None:
raise Exception("Error: LRCFixup was unable to locate "
"[{}]".format(self.ref))
lrc = 0
for b in stuff:
lrc ^= ord(b)
return chr(lrc)
class Crc32DualFixup(Fixup):
"""
Standard CRC32 as defined by ISO 3309. Used by PNG, ZIP, etc.
"""
def __init__(self, ref1, ref2):
Fixup.__init__(self)
self.ref1 = ref1
self.ref2 = ref2
def fixup(self):
self.context.defaultValue = "0"
stuff1 = self.context.findDataElementByName(self.ref1).getValue()
stuff2 = self.context.findDataElementByName(self.ref2).getValue()
if stuff1 is None or stuff2 is None:
raise Exception("Error: Crc32DualFixup was unable to locate [{}] "
"or [{}]".format(self.ref1, self.ref2))
crc1 = zlib.crc32(stuff1)
crc = zlib.crc32(stuff2, crc1)
if crc < 0:
crc = ~crc ^ 0xffffffff
return crc
class EthernetChecksumFixup(Fixup):
"""
Ethernet Chucksum Fixup.
"""
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def _checksum(self, checksum_packet):
ethernetKey = 0x04C11DB7
return binascii.crc32(checksum_packet, ethernetKey)
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref).getValue()
if stuff is None:
raise Exception("Error: EthernetChecksumFixup was unable to locate"
" [{}]".format(self.ref))
return self._checksum(stuff)
class IcmpChecksumFixup(Fixup):
"""
Ethernet Checksum Fixup.
"""
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def _checksum(self, checksum_packet):
# Add byte if not dividable by 2
if len(checksum_packet) & 1:
checksum_packet += '\0'
# Split into 16-bit word and insert into a binary array
words = array.array('h', checksum_packet)
sum = 0
# Perform ones complement arithmetic on 16-bit words
for word in words:
sum += (word & 0xffff)
hi = sum >> 16
lo = sum & 0xffff
sum = hi + lo
sum += sum >> 16
return (~sum) & 0xffff # return ones complement
def fixup(self):
self.context.defaultValue = "0"
stuff = self.context.findDataElementByName(self.ref).getValue()
if stuff is None:
raise Exception("Error: IcmpChecksumFixup was unable to locate "
"[{}]".format(self.ref))
return self._checksum(stuff)
class FontTableChecksum(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def calc(self, stuff):
sum = 0x00
length = len(stuff)
#stuff += '\x00' * (length % 4)
for c in range(length):
sum += int(ord(stuff[c]))
return sum
def fixup(self):
ref = self.context.findDataElementByName(self.ref)
#print "self.context.findDataElementByName:", ref.getFullname()
stuff = ref.getValue()
#print "First 10 bytes of Data:", repr(stuff[0:10])
if stuff is None:
raise Exception("Error: FontTableChecksumFixup was unable to "
"locate [{}]".format(self.ref))
return self.calc(stuff)
class FontChecksum(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def calc(self, stuff):
sum = 0x00
length = len(stuff)
#stuff += '\x00' * (length % 4)
for c in range(length):
sum += int(ord(stuff[c]))
return sum
def fixup(self):
ref = self.context.findDataElementByName(self.ref)
stuff = ref.getValue()
if stuff is None:
raise Exception("Error: FontChecksum Fixup was unable to locate "
"[{}]".format(self.ref))
return 0xB1B0AFBA - self.calc(stuff)
try:
import sspi, sspicon
class SspiAuthenticationFixup(Fixup):
"""
Perform basic SSPI authentication. Assumes a two step auth.
"""
_sspi = None
_firstObj = None
_secondObj = None
_data = None
def __init__(self, firstSend, secondSend, user=None, group=None, password=None):
Fixup.__init__(self)
self.firstSend = firstSend
self.secondSend = secondSend
self.username = user
self.workgroup = group
self.password = password
def getXml(self):
dict = {}
doc = etree.fromstring("<Peach/>", base_url="http://phed.org")
self.context.getRoot().toXmlDom(doc, dict)
return doc
def fixup(self):
try:
fullName = self.context.getFullname()
xml = self.getXml()
firstFullName = str(xml.xpath(self.firstSend)[0].get("fullName"))
firstFullName = firstFullName[firstFullName.index('.') + 1:]
if fullName.find(firstFullName) > -1 and SspiAuthenticationFixup._firstObj != self.context:
#scflags = sspicon.ISC_REQ_INTEGRITY|sspicon.ISC_REQ_SEQUENCE_DETECT|\
# sspicon.ISC_REQ_REPLAY_DETECT|sspicon.ISC_REQ_CONFIDENTIALITY
scflags = sspicon.ISC_REQ_INTEGRITY | sspicon.ISC_REQ_SEQUENCE_DETECT | \
sspicon.ISC_REQ_REPLAY_DETECT
SspiAuthenticationFixup._firstObj = self.context
SspiAuthenticationFixup._sspi = sspi.ClientAuth(
"Negotiate",
"", # client_name
(self.username, self.workgroup, self.password), # auth_info
None, # targetsn (target security context provider)
scflags, #scflags # None, # security context flags
)
(done, data) = SspiAuthenticationFixup._sspi.authorize(None)
data = data[0].Buffer
SspiAuthenticationFixup._data = data
return data
if fullName.find(firstFullName) > -1:
return SspiAuthenticationFixup._data
secondFullName = str(xml.xpath(self.secondSend)[0].get("fullName"))
secondFullName = secondFullName[secondFullName.index('.') + 1:]
if fullName.find(secondFullName) > -1 and SspiAuthenticationFixup._secondObj != self.context:
inputData = self.context.getInternalValue()
if len(inputData) < 5:
return None
(done, data) = SspiAuthenticationFixup._sspi.authorize(inputData)
data = data[0].Buffer
SspiAuthenticationFixup._secondObj = self.context
SspiAuthenticationFixup._data = data
return data
if fullName.find(secondFullName) > -1:
return SspiAuthenticationFixup._data
except:
print("!!! EXCEPTION !!!")
print(repr(sys.exc_info()))
pass
except:
pass