def create_model_thr(model_id): db = DB() model = db.get_model_by_id(model_id) job = db.add(Job("Generating model " + model.name)) bin_path = os.path.join(FILE_UPLOAD_FOLDER, model.filename) model_path = os.path.join(MODEL_FOLDER, model.filename) diss_path = os.path.join(model_path, "diss") map_path = os.path.join(model_path, "map.json") model_file_path = os.path.join(model_path, "model") os.system("mkdir {}".format(bin_path)) os.system("mkdir {}".format(model_path)) os.system("mkdir {}".format(diss_path)) os.system("unzip {} -d {} > /dev/null".format(bin_path + ".zip", bin_path)) bin_files = os.listdir(bin_path) for i, binf in enumerate(bin_files): db.job_add_log( job.id, "Disassembling file {}/{}".format(len(bin_files), i + 1)) Disassembler(os.path.join(bin_path, binf)).disassemble_to_file( os.path.join(diss_path, str(i) + "-" + binf + ".json")) db.job_add_log(job.id, "Creating map") MapCreator(diss_path, map_path).create_map() db.job_add_log(job.id, "Generating model") ModelCreator(diss_path, map_path, model_file_path).create_model() db.job_add_log(job.id, "Model generated") db.set_model_analyzed(model.id) db.job_add_log(job.id, "Cleaning up the file mess") os.system("rm {}".format(bin_path + ".zip")) os.system("rm -rf {}".format(bin_path)) db.set_job_ended(job.id)
def get_model(model_id): try: db = DB() model = db.get_model_by_id(model_id) if model: return jsonify({"model": model.serialize()}), 200 else: return jsonify({"error": "Model not found"}), 400 except Exception as e: print(e) abort(500)
def get_results(binary_id): db = DB() res = [] binary = db.get_binary_by_id(binary_id) results = db.get_results_by_binary_id(binary.id) if len(results) != 0: results = parse_results(results) for r in results: m = db.get_model_by_id(r.model) res.append({"model": m.name, "score": str(int(r.score))}) return res
def train_model_thr(model_id, bin_path): db = DB() model = db.get_model_by_id(model_id) model_path = os.path.join(MODEL_FOLDER, model.filename) diss_path = os.path.join(model_path, "diss") map_path = os.path.join(model_path, "map.json") model_file_path = os.path.join(model_path, "model") bin_files = os.listdir(bin_path) for i, binf in enumerate(bin_files): Disassembler(os.path.join(bin_path, binf)).disassemble_to_file( os.path.join(diss_path, str(i) + "-" + binf + ".json")) ModelCreator(diss_path, map_path, model_file_path).train_model()
def upload_model_file(model_id): try: db = DB() model = db.get_model_by_id(model_id) if not model: return jsonify({"error": "Model not found"}), 400 file = request.files['file'] if file and file.filename: file.save(os.path.join(FILE_UPLOAD_FOLDER, model.filename + ".zip")) hm.create_model(model.id) return jsonify({}), 200 except Exception as e: print(e) abort(500)
def get_results(binary_id): try: db = DB() rt = [] binary = db.get_binary_by_id(binary_id) results = db.get_results_by_binary_id(binary.id) if len(results) != 0: results = parse_results(results) for r in results: m = db.get_model_by_id(r.model) rt.append({"model": m.name, "score": str(int(r.score))}) return jsonify({"results": rt, "analyzed": binary.analyzed}), 200 except Exception as e: print(e) abort(500)