def preprocess(sample):
"""Preprocess files after upload.
:param sample: :class:`~app.models.Sample`
:return:
"""
hash_path = os.path.join(
current_app.config['APP_UPLOADS_SAMPLES'],
sample.sha256
)
if zipfile.is_zipfile(hash_path):
mt = magic.from_file(hash_path, mime=True)
if mt in skip_mimes:
return None
current_app.log.debug('Extracting {}'.format(hash_path))
zfile = zipfile.ZipFile(hash_path)
for zipfo in zfile.namelist():
cfg = current_app.config
if zfile.getinfo(zipfo).compress_type == 99: # PK compat. v5.1
pwd = '-p{}'.format(cfg['INFECTED_PASSWD'])
with popen('7z', 'e', '-so', pwd, hash_path) as zproc:
buf, stderr = zproc.communicate()
else:
buf = zfile.read(zipfo,
pwd=bytes(cfg['INFECTED_PASSWD'], 'utf-8'))
digests = get_hashes(buf)
hash_path = os.path.join(cfg['APP_UPLOADS_SAMPLES'],
digests.sha256)
if not os.path.isfile(hash_path):
with open(hash_path, 'wb') as wf:
wf.write(buf)
s = Sample(user_id=sample.user_id, filename=zipfo,
parent_id=sample.id,
md5=digests.md5, sha1=digests.sha1,
sha256=digests.sha256, sha512=digests.sha512,
ctph=digests.ctph)
db.session.add(s)
db.session.commit()
评论列表
文章目录