def taskProcess(self): logging.debug(self.__class__) logging.debug(self.currentTask) logging.debug(self.objects) jobId = self.currentTask.job taskId = self.currentTask.task_id external = External() if len(self.objects) == 0: raise ObjectStoreException("Task processing didn't find task object.") if not self.objects[0].isSet("pcap_content"): raise ParamException("pcap_content param is missing.") outputDir = "/tmp/%s" % uuid.uuid4() config = Config().getConfig() external.runExternal(["mkdir", "-p", outputDir]) pcapFilePath = self.getPcapFilePath() external.runExternal(["tcpxtract", "-f", pcapFilePath, "-o", outputDir]) dirList = os.listdir(outputDir) verifierFactory = VerifierFactory() objects = list() for fname in dirList: filepath = "%s/%s" % (outputDir, fname) m = magic.open(magic.MAGIC_MIME) m.load() result = m.file(filepath) mimetype = result.split(";")[0] _, fileExtension = os.path.splitext(filepath) fileExtension = fileExtension[1:] verifierList = verifierFactory.getVerifierList(fileExtension, config) result = True for ver in verifierList: logging.info("%s, %s, %s" % (filepath, mimetype, fileExtension)) result = result and ver.verify(filepath, mimetype, fileExtension, config) if not result: break if result: objects.append(self.createNewObject(filepath, fileExtension)) if len(objects) > 0: newObjIds = self.osAdapter.objectsPut(jobId, taskId, objects) self.newObjects.extend(newObjIds) external.runExternal(["rm", "-rf", outputDir, pcapFilePath]) return []
def verify(self, filepath, mimetype, extension, config): external = External() output = external.runExternal(["zip", "--test", filepath]) if re.match("(.*)OK(.*)", output[0], re.I) is not None: return True return False