-
Notifications
You must be signed in to change notification settings - Fork 48
/
verifier.py
274 lines (240 loc) · 11.3 KB
/
verifier.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
from copy import deepcopy
from model.model_verification import FillType
import logging
from typing import List
from model.model_data import Match
from model.model_verification import *
from model.file_model import BaseFile
# Middle: 8
# Thirds: 4 * 2
MATCH_SIZE_CUTOFF = 16
def toTestEntry(scanIndex, result):
scanResult = ScanResult.NOT_SCANNED
if not result:
scanResult = ScanResult.NOT_DETECTED
else:
scanResult = ScanResult.DETECTED
testEntry = MatchTest(scanIndex, scanResult)
return testEntry
def getMatchTestsFor(verifications: List[VerificationEntry], matchOrder: TestMatchOrder, matchModify: TestMatchModify):
for verification in verifications:
if verification.info == matchOrder and verification.type == matchModify:
return verification.matchTests
return None
def verify(file: BaseFile, matches: List[Match], scanner) -> Verification:
"""Verify matches in file with scanner, and return the result"""
verifications = runVerifications(file, matches, scanner)
matchConclusions = verificationAnalyzer(verifications)
verify = Verification(verifications, matchConclusions)
return verify
def verificationAnalyzer(verifications: List[VerificationEntry]) -> MatchConclusion:
"""Do some analysis on the verifications, and return the result"""
verifyResults = []
if len(verifications) == 0:
matchConclusion = MatchConclusion(verifyResults)
return matchConclusion
# check if robust:
# no ISOLATED.FULL not detected
hasDominant = False
matchTests = getMatchTestsFor(verifications, TestMatchOrder.ISOLATED, TestMatchModify.FULL)
for matchTest in matchTests:
if matchTest.scanResult == ScanResult.NOT_DETECTED:
hasDominant = True
break
if not hasDominant:
# set all to ROBUST
for _ in verifications[0].matchTests: # just iterate through it
res = VerifyStatus.ROBUST
verifyResults.append(res)
matchConclusion = MatchConclusion(verifyResults)
return matchConclusion
matchCount = len(verifications[0].matchTests)
idx = 0
while idx < matchCount:
middleRes = getMatchTestsFor(verifications, TestMatchOrder.ISOLATED, TestMatchModify.MIDDLE8)[idx].scanResult
thirdsRes = getMatchTestsFor(verifications, TestMatchOrder.ISOLATED, TestMatchModify.THIRDS4)[idx].scanResult
fullRes = getMatchTestsFor(verifications, TestMatchOrder.ISOLATED, TestMatchModify.FULL)[idx].scanResult
# very weak signature
if middleRes == ScanResult.NOT_DETECTED and thirdsRes == ScanResult.NOT_DETECTED:
res = VerifyStatus.DOMINANT
# for small signatures, ignore MIDDLE/THIRDS and just make result depend on FULL
elif fullRes == ScanResult.NOT_DETECTED:
res = VerifyStatus.DOMINANT
# incremental and stuff, just everything in between
else:
res = VerifyStatus.IRRELEVANT
verifyResults.append(res)
idx += 1
matchConclusion = MatchConclusion(verifyResults)
return matchConclusion
def runVerifications(file: BaseFile, matches: List[Match], scanner) -> List[VerificationEntry]:
"""Perform modifications on file from matches, scan with scanner and return those results"""
verificationRuns: List[VerificationEntry] = []
if len(matches) == 0:
return verificationRuns
logging.info(f"Verify {len(matches)} matches")
# Independant, Middle
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.ISOLATED,
matchModify=TestMatchModify.MIDDLE8)
for match in matches:
if match.size < MATCH_SIZE_CUTOFF:
verificationRun.matchTests.append(MatchTest('', ScanResult.NOT_SCANNED))
continue
fileCopy = deepcopy(file)
offset = match.fileOffset + int((match.size) // 2) - 4
fileCopy.Data().hidePart(offset, 8, fillType=FillType.lowentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
verificationRun.matchTests.append(toTestEntry('', result))
logging.info("Verification run: {}".format(verificationRun))
verificationRuns.append(verificationRun)
# Independant, Thirds
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.ISOLATED,
matchModify=TestMatchModify.THIRDS4)
for match in matches:
if match.size < MATCH_SIZE_CUTOFF:
verificationRun.matchTests.append(MatchTest('', ScanResult.NOT_SCANNED))
continue
fileCopy = deepcopy(file)
offset1 = match.fileOffset + int( (match.size // 3) * 1) - 2
offset2 = match.fileOffset + int( (match.size // 3) * 2) - 2
fileCopy.Data().hidePart(offset1, 4, fillType=FillType.lowentropy)
fileCopy.Data().hidePart(offset2, 4, fillType=FillType.lowentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
verificationRun.matchTests.append(toTestEntry('', result))
verificationRuns.append(verificationRun)
logging.info("Verification run: {}".format(verificationRun))
# Independant, Full
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.ISOLATED,
matchModify=TestMatchModify.FULL
)
for match in matches:
fileCopy = deepcopy(file)
fileCopy.Data().hidePart(match.fileOffset, match.size, fillType=FillType.lowentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
verificationRun.matchTests.append(toTestEntry('', result))
verificationRuns.append(verificationRun)
logging.info("Verification run: {}".format(verificationRun))
# Independant, Full B
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.ISOLATED,
matchModify=TestMatchModify.FULLB
)
for match in matches:
fileCopy = deepcopy(file)
fileCopy.Data().hidePart(match.fileOffset, match.size, fillType=FillType.highentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
verificationRun.matchTests.append(toTestEntry('', result))
verificationRuns.append(verificationRun)
logging.info("Verification run: {}".format(verificationRun))
if len(matches) == 1:
return verificationRuns
# Incremental, Middle
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.INCREMENTAL,
matchModify=TestMatchModify.MIDDLE8,
)
fileCopy = deepcopy(file)
for match in matches:
if match.size < MATCH_SIZE_CUTOFF:
verificationRun.matchTests.append(MatchTest('', ScanResult.NOT_SCANNED))
continue
offset = match.fileOffset + int((match.size) // 2) - 4
fileCopy.Data().hidePart(offset, 8, fillType=FillType.lowentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
verificationRun.matchTests.append(toTestEntry(match.idx, result))
verificationRuns.append(verificationRun)
logging.info("Verification run: {}".format(verificationRun))
# Incremental, Full
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.INCREMENTAL,
matchModify=TestMatchModify.FULL)
fileCopy = deepcopy(file)
for match in matches:
fileCopy.Data().hidePart(match.fileOffset, match.size, fillType=FillType.lowentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
verificationRun.matchTests.append(toTestEntry(match.idx, result))
verificationRuns.append(verificationRun)
logging.info("Verification run: {}".format(verificationRun))
# Decremental, Full
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.DECREMENTAL,
matchModify=TestMatchModify.FULL)
fileCopy = deepcopy(file)
n = 0
for match in reversed(matches):
fileCopy.Data().hidePart(match.fileOffset, match.size, fillType=FillType.lowentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
verificationRun.matchTests.append(toTestEntry(n, result))
n += 1
verificationRun.matchTests = list(reversed(verificationRun.matchTests))
verificationRuns.append(verificationRun)
logging.info("Verification run: {}".format(verificationRun))
# All, Middle
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.ALL,
matchModify=TestMatchModify.MIDDLE8)
fileCopy = deepcopy(file)
for match in matches:
if match.size < MATCH_SIZE_CUTOFF:
# no need to a matchTest, as it is done later
continue
offset = match.fileOffset + int((match.size) // 2) - 4
fileCopy.Data().hidePart(offset, 8, fillType=FillType.lowentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
for match in matches:
if match.size < MATCH_SIZE_CUTOFF:
verificationRun.matchTests.append(MatchTest('', ScanResult.NOT_SCANNED))
else:
verificationRun.matchTests.append(toTestEntry(0, result))
verificationRun.matchTests = list(reversed(verificationRun.matchTests))
verificationRuns.append(verificationRun)
logging.info("Verification run: {}".format(verificationRun))
# All, Thirds
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.ALL,
matchModify=TestMatchModify.THIRDS4)
fileCopy = deepcopy(file)
for match in matches:
if match.size < MATCH_SIZE_CUTOFF:
# no need to a matchTest, as it is done in the end
continue
offset1 = match.fileOffset + int( (match.size // 3) * 1) - 2
offset2 = match.fileOffset + int( (match.size // 3) * 2) - 2
fileCopy.Data().hidePart(offset1, 4, fillType=FillType.lowentropy)
fileCopy.Data().hidePart(offset2, 4, fillType=FillType.lowentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
for match in matches:
if match.size < MATCH_SIZE_CUTOFF:
verificationRun.matchTests.append(MatchTest('', ScanResult.NOT_SCANNED))
else:
verificationRun.matchTests.append(toTestEntry(0, result))
verificationRun.matchTests = list(reversed(verificationRun.matchTests))
verificationRuns.append(verificationRun)
logging.info("Verification run: {}".format(verificationRun))
# All, Full
verificationRun = VerificationEntry(
index=len(verificationRuns),
matchOrder=TestMatchOrder.ALL,
matchModify=TestMatchModify.FULL)
fileCopy = deepcopy(file)
for match in matches:
fileCopy.Data().hidePart(match.fileOffset, match.size, fillType=FillType.lowentropy)
result = scanner.scannerDetectsBytes(fileCopy.DataAsBytes(), file.filename)
for match in matches:
verificationRun.matchTests.append(toTestEntry(0, result))
verificationRun.matchTests = list(reversed(verificationRun.matchTests))
verificationRuns.append(verificationRun)
logging.info("Verification run: {}".format(verificationRun))
return verificationRuns