def upload(): producer = Producer(conf) with NamedTemporaryFile() as f: request.files["file"].save(f.name) with open(f.name, "rb") as fr: sample = Resource("sample", fr.read()) task = Task({"type": "sample", "stage": "recognized", "platform": "win32"}) task.add_payload("override_uid", task.uid) # Add analysis timeout to task timeout = request.form.get("timeout") if timeout: task.add_payload("timeout", int(timeout)) # Add filename override to task if request.form.get("file_name"): filename = request.form.get("file_name") else: filename = request.files["file"].filename if not re.fullmatch( r"^((?![\\/><|:&])[\x20-\xfe])+\.(?:dll|exe|ps1|bat|doc|docm|docx|dotm|xls|xlsx|xlsm|xltx|xltm|ppt|pptx|vbs|js|jse|hta|html|htm)$", filename, flags=re.IGNORECASE, ): return jsonify({"error": "invalid file_name"}), 400 task.add_payload("file_name", os.path.splitext(filename)[0]) # Extract and add extension extension = os.path.splitext(filename)[1][1:] if extension: task.headers["extension"] = extension # Add startup command to task start_command = request.form.get("start_command") if start_command: task.add_payload("start_command", start_command) # Add plugins to task plugins = request.form.get("plugins") if plugins: plugins = json.loads(plugins) task.add_payload("plugins", plugins) task.add_resource("sample", sample) producer.send_task(task) return jsonify({"task_uid": task.uid})
def main(): parser = argparse.ArgumentParser(description="Push sample to the karton") parser.add_argument("sample", help="Path to the sample") parser.add_argument( "--start_command", help="e.g. start %f, %f will be replaced by file name", required=False, ) parser.add_argument( "--timeout", default=600, type=int, help="analysis timeout in seconds", required=False, ) args = parser.parse_args() conf = patch_config(Config(os.path.join(ETC_DIR, "config.ini"))) producer = Producer(conf) task = Task({"type": "sample", "stage": "recognized", "platform": "win32"}) with open(args.sample, "rb") as f: sample = Resource("sample", f.read()) task.add_resource("sample", sample) # Add filename filename = os.path.basename(args.sample) task.add_payload("file_name", os.path.splitext(filename)[0]) # Extract and add extension extension = os.path.splitext(filename)[1][1:] if extension: task.headers["extension"] = extension if args.start_command is not None: task.add_payload("start_command", args.start_command) if args.timeout is not None: task.add_payload("timeout", args.timeout) producer.send_task(task)