-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
kfuzzy.py
395 lines (328 loc) · 10 KB
/
kfuzzy.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python
"""
Fuzzy hashing algorithms
Copyright (C) 2009-2023, Joxean Koret
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""
import os
import sys
import base64
import functools
__all__ = ["CKoretFuzzyHashing"]
#-------------------------------------------------------------------------------
def modsum(buf):
""" Return the SUM(BUF) MOD 255 """
return sum(buf) % 255
#-------------------------------------------------------------------------------
class CFileStr(str):
"""
Files wrapper class
"""
def __init__(self, fd):
str.__init__(self)
self.fd = fd
def __getslice__(self, x, y):
self.fd.seek(x, 0)
buf = self.fd.read(y-x)
self.fd.seek(y)
return buf
def __len__(self):
old = self.fd.tell()
self.fd.seek(0, 2)
pos = self.fd.tell()
self.fd.seek(old)
return pos
#-------------------------------------------------------------------------------
class CKoretFuzzyHashing:
""" Generate fuzzy hashes of files or bytes """
bsize = 512
output_size = 32
ignore_range = 2
big_file_size = 1024*1024*10
algorithm = None
reduce_errors = True
remove_spaces = False
def get_bytes(self, f, initial, final):
"""
Get bytes from @initial to @final
"""
f.seek(initial)
return f.read(final)
def edit_distance(self, sign1, sign2):
"""
Trivially calculate the edit distance of @sign1 to @sign2
"""
if sign1 == sign2:
return 0
m = max(len(sign1), len(sign2))
distance = 0
for c in range(0, m):
if sign1[c:c+1] != sign2[c:c+1]:
distance += 1
return distance
def simplified(self, strbytes, _):
"""
Get a simplified hash
"""
output_size = self.output_size
bsize = self.bsize
total_size = len(strbytes)
size = (total_size/bsize) / output_size
buf = []
reduce_errors = self.reduce_errors
# Adjust the output to the desired output size
for c in range(0, output_size):
tmp = strbytes[c*size:(c*size+1)+bsize]
ret = sum(map(ord, tmp)) % 255
if reduce_errors:
if ret != 255 and ret != 0:
buf.append(chr(ret))
else:
buf.append(chr(ret))
buf = "".join(buf)
return base64.b64encode(buf).strip("=")[:output_size]
def _hash(self, strbytes, aggresive = False):
"""
Internal hashing method
"""
idx = 0
ret = []
output_size = self.output_size
ignore_range = self.ignore_range
bsize = self.bsize
total_size = len(strbytes)
rappend = ret.append
reduce_errors = self.reduce_errors
# Calculate the sum of every block
while 1:
chunk_size = idx*bsize
buf = strbytes[chunk_size:chunk_size+bsize]
char = modsum(buf)
if reduce_errors:
if char != 255 and char != 0:
rappend(chr(char))
else:
rappend(chr(char))
idx += 1
if chunk_size+bsize > total_size:
break
ret = "".join(ret)
size = len(ret) / output_size
size = min(int(size), 1)
buf = []
# Adjust the output to the desired output size
for c in range(0, output_size):
if aggresive:
buf.append(ret[c:c+size+1][ignore_range:ignore_range+1])
else:
buf.append(ret[c:c+size+1][1:2])
i = 0
for x in ret[c:c+size+1]:
i += 1
if i != ignore_range:
continue
i = 0
buf += x
break
ret = "".join(buf)
bret = ret.encode()
return base64.b64encode(bret).strip(b"=")[:output_size]
def _fast_hash(self, strbytes, _):
"""
Internal fast hashing method
"""
i = -1
ret = set()
output_size = self.output_size
bsize = self.bsize
radd = ret.add
while i < output_size:
i += 1
buf = strbytes[i*bsize:(i+1)*bsize]
char = sum(map(ord, buf)) % 255
if self.reduce_errors:
if char != 255 and char != 0:
radd(chr(char))
else:
radd(chr(char))
ret = "".join(ret)
return base64.b64encode(ret).strip("=")[:output_size]
def xor(self, strbytes):
"""
Xor all the bytes
"""
return functools.reduce(lambda x, y: x ^ y, strbytes)
def _experimental_hash(self, strbytes, _):
"""
An experimental (since 2009...) internal hasher
"""
idx = 0
ret = []
bsize = self.bsize
output_size = self.output_size
size = len(strbytes)
chunk_size = idx*self.bsize
byte = None
while size > chunk_size + (bsize/output_size):
chunk_size = idx*self.bsize
if byte is None:
val = bsize
elif ord(byte) > 0:
val = ord(byte)
else:
val = output_size
buf = strbytes[chunk_size:chunk_size+val]
byte = self.xor(list(map(ord, buf))) % 255
byte = chr(byte)
if byte != '\xff' and byte != '\x00':
ret.append(byte)
idx += 1
ret = "".join(ret)
buf = ""
size = len(ret)/output_size
for n in range(0, output_size):
buf += ret[n*size:(n*size)+1]
return base64.b64encode(buf).strip("=")[:output_size]
def mix_blocks(self, strbytes):
"""
Mix chunks of bytes from the beginning and ending
"""
idx = 0
buf = strbytes
ret = b""
size1 = 0
size2 = 0
while 1:
size1 = idx*self.bsize
size2 = (idx+1)*self.bsize
tmp = buf[size1:size2]
tm2 = tmp
ret += tmp
ret += tm2
idx += 1
if len(tmp) < self.bsize:
break
return ret
def clean_spaces(self, strbytes):
"""
Remove all white spaces
"""
strbytes = strbytes.replace(b" ", b"").replace(b"\r", b"").replace(b"\n", b"")
strbytes = strbytes.replace(b"\t", b"")
return strbytes
def hash_bytes(self, strbytes, aggresive = False):
"""
Hash the given bytes
"""
if isinstance(strbytes, str):
strbytes = strbytes.encode()
if self.remove_spaces:
strbytes = self.clean_spaces(strbytes)
mix = self.mix_blocks(strbytes)
if self.algorithm is None:
func = self._hash
else:
func = self.algorithm
hash1 = func(mix, aggresive)
hash2 = func(strbytes, aggresive)
hash3 = func(strbytes[::-1], aggresive)
return (hash1 + b";" + hash2 + b";" + hash3).decode("utf-8")
def hash_file(self, filename, aggresive = False):
"""
Hash the given file's bytes
"""
with open(filename, "rb") as f:
f.seek(0, 2)
size = f.tell()
if size > self.big_file_size:
print()
curr_size = size/1024/1024
big_size = self.big_file_size / 1024 / 1024
print(f"Warning! Support for big files ({curr_size} MB > {big_size} MB) is broken!")
fbytes = CFileStr(f)
else:
f.seek(0)
fbytes = f.read()
f.close()
return self.hash_bytes(fbytes, aggresive)
#-------------------------------------------------------------------------------
class kdha:
""" Interface to make partially compatible the KFuzzy hashing algorithms with
the standard python hashlib format. This is the Koret Default Hashing Algorithm """
def __init__(self, strbytes):
""" Initialize the object """
self.block_size = 512
self.digest_size = 32
self._bytes = strbytes
self._kfd = CKoretFuzzyHashing()
def update(self, strbytes):
""" Not very useful, just for compatibility... """
self._bytes += strbytes
def hexdigest(self):
""" Returns and hexadecimal digest """
self._kfd.bsize = self.block_size
self._kfd.output_size = self.digest_size
hasher = self._kfd.hash_bytes(self._bytes)
return hasher
def digest(self):
""" Same as hexdigest """
return self.hexdigest()
#-------------------------------------------------------------------------------
class kfha(kdha):
""" Interface to make partially compatible the KFuzzy hashing algorithms with
the standard python hashlib format. This is the Koret Fast Hashing Algorithm """
def __init__(self, strbytes):
kdha.__init__(self, strbytes)
self._bytes = strbytes
self._kfd = CKoretFuzzyHashing()
self._kfd.algorithm = self._kfd._fast_hash
#-------------------------------------------------------------------------------
class ksha(kdha):
""" Interface to make partially compatible the KFuzzy hashing algorithms with
the standard python hashlib format. This is the Koret Simplified Hashing Algorithm """
def __init__(self, strbytes):
kdha.__init__(self, strbytes)
self._bytes = strbytes
self._kfd = CKoretFuzzyHashing()
self._kfd.algorithm = self._kfd.simplified
#-------------------------------------------------------------------------------
def usage():
""" Show usage information """
print("Usage:", sys.argv[0], "<filename>")
#-------------------------------------------------------------------------------
def main(path):
"""
Hash the given input file name
"""
hasher = CKoretFuzzyHashing()
#hasher.algorithm = hasher._fast_hash
if os.path.isdir(path):
print("Signature;Simple Signature;Reverse Signature;Filename")
for root, _, files in os.walk(path):
for name in files:
tmp = os.path.join(root, name)
try:
ret = hasher.hash_file(tmp, True)
print(f"{ret};{tmp}")
except:
print(f"***ERROR with file {tmp}")
print(sys.exc_info()[1])
else:
hasher = CKoretFuzzyHashing()
ret = hasher.hash_file(path, True)
print(f"{path};{ret}")
if __name__ == "__main__":
if len(sys.argv) == 1:
usage()
else:
main(sys.argv[1])