Skip to content

Commit 610066d

Browse files
authored
Use pycryptodome instead of numpy for obfuscated download (#733)
1 parent d4ec346 commit 610066d

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

mwdb/model/file.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import shutil
55
import tempfile
66

7-
import numpy as np
87
import pyzipper
8+
from Cryptodome.Util.strxor import strxor_c
99
from sqlalchemy import or_
1010
from sqlalchemy.dialects.postgresql.array import ARRAY
1111
from sqlalchemy.ext.mutable import MutableList
@@ -280,30 +280,25 @@ def iterate(self, chunk_size=1024 * 256):
280280
finally:
281281
File.close(fh)
282282

283-
def negate_bits(self, chunk):
284-
"""
285-
xor data with key equal 255 of length of chunk; using numpy
286-
https://stackoverflow.com/questions/23312571/fast-xoring-bytes-in-python-3
287-
"""
288-
key = np.frombuffer(b"\xff" * len(chunk), dtype="uint8")
289-
chunk = np.frombuffer(chunk, dtype="uint8")
290-
return (key ^ chunk).tobytes()
291-
292283
def iterate_obfuscated(self, chunk_size=1024 * 256):
293284
r"""
294285
Iterates over bytes in the file contents with xor applied
295286
The idea behind xoring before send is to prevent browsers
296287
from caching original samples (malware). Unxoring is provided
297288
in mwdb\web\src\components\ShowSample.js in SamplePreview
298289
"""
290+
291+
def negate_bits(chunk):
292+
return strxor_c(chunk, 255)
293+
299294
fh = self.open()
300295
try:
301296
if hasattr(fh, "stream"):
302-
yield from map(self.negate_bits, fh.stream(chunk_size))
297+
yield from map(negate_bits, fh.stream(chunk_size))
303298
else:
304299
while True:
305300
chunk = fh.read(chunk_size)
306-
chunk = self.negate_bits(chunk)
301+
chunk = negate_bits(chunk)
307302
if chunk:
308303
yield chunk
309304
else:

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ pyJWT==2.4.0
2828
Flask-Limiter==2.1.3
2929
python-dateutil==2.8.2
3030
pyzipper==0.3.5
31-
numpy==1.23.5
31+
pycryptodomex==3.16.0

0 commit comments

Comments
 (0)