def processDownload(tmpFilePath, fileName, fileUrl):
logging.info('Downloaded as temporary file: {0}. Beginning processing...'.format(tmpFilePath))
fileSize = os.path.getsize(tmpFilePath) >> 20
if (fileSize > 10):
logging.error('File is {0}MB. Too large to process.'.format(fileSize))
cleanUp(tmpFilePath)
return False
fileHash = sha256SumFile(tmpFilePath)
if not isAcceptedHash(fileHash):
cleanUp(tmpFilePath)
return False
filePath = os.path.join(baseConfig.outputFolder, fileHash)
os.rename(tmpFilePath, filePath)
# Trust only the content type of the downloaded file.
mimeType = magic.from_file(filePath, mime=True)
if mimeType not in ['application/octet-stream', 'application/x-dosexec', 'application/x-msdownload', 'application/x-ms-installer', 'application/pdf', 'application/x-pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'application/vnd.ms-word.document.macroEnabled', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'application/vnd.ms-excel.sheet.macroEnabled', 'application/vnd.ms-excel.template.macroEnabled', 'application/vnd.ms-excel.addin.macroEnabled', 'application/vnd.ms-excel.sheet.binary.macroEnabled', 'application/x-shockwave-flash']:
logging.error('Detected non-binary or executable file type ({0}). Skipping: {1}'.format(mimeType, filePath))
cleanUp(filePath)
return False
logging.info('File with hash: {0} identified as type: {1}'.format(fileHash, mimeType))
uploaded = uploadToViper(filePath, fileName, fileHash, fileUrl)
addToHashCache(fileHash)
cleanUp(filePath)
return uploaded
评论列表
文章目录