def test_open_exclusive(): fpath = os.path.join(tempfile.mkdtemp(), "yeet.exclusive") with open(fpath, "w") as fp: fp.write("42421337Test") with pytest.raises(OSError): open_exclusive(fpath, bufsize=1)
def handle(self): # Read until newline for file path, e.g., # shots/0001.jpg or files/9498687557/libcurl-4.dll.bin self.handler.sock.settimeout(30) dump_path = netlog_sanitize_fname(self.handler.read_newline()) if self.version and self.version >= 2: # NB: filepath is only used as metadata filepath = self.handler.read_newline() pids = list(map(int, self.handler.read_newline().split())) metadata = self.handler.read_newline() category = self.handler.read_newline() else: filepath, pids, metadata, category = None, [], b"", b"" log.debug("Task #%s: File upload for %r", self.task_id, dump_path) file_path = os.path.join(self.storagepath, dump_path.decode("utf-8")) try: self.fd = open_exclusive(file_path) except OSError as e: if e.errno == errno.EEXIST: raise CuckooOperationalError("Analyzer for task #%s tried to " "overwrite an existing file" % self.task_id) raise # ToDo we need Windows path # filter screens/curtain/sysmon if not dump_path.startswith( (b"shots/", b"curtain/", b"aux/", b"sysmon/", b"debugger/")): # Append-writes are atomic with open(self.filelog, "a") as f: print( json.dumps( { "path": dump_path.decode("utf-8", "replace"), "filepath": filepath.decode("utf-8", "replace") if filepath else "", "pids": pids, "metadata": metadata.decode("utf-8", "replace"), "category": category.decode("utf-8") if category in (b"CAPE", b"files", b"memory", b"procdump") else "", }, ensure_ascii=False, ), file=f, ) self.handler.sock.settimeout(None) try: return self.handler.copy_to_fd(self.fd, self.upload_max_size) finally: log.debug("Task #%s uploaded file length: %s", self.task_id, self.fd.tell())
def init(self): self.logpath = os.path.join(self.handler.storagepath, "analysis.log") try: self.fd = open_exclusive(self.logpath, bufsize=1) except OSError: log.error("Task #%s: attempted to reopen live log analysis.log", self.task_id) return log.debug("Task #%s: live log analysis.log initialized", self.task_id)