35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
from flask import Blueprint, request, abort
|
|
from werkzeug.utils import secure_filename
|
|
import capa.main
|
|
import capa.rules
|
|
import capa.loader
|
|
import capa.render.json
|
|
import capa.capabilities.common
|
|
from capa.features.common import OS_AUTO, FORMAT_AUTO
|
|
from os import path
|
|
from pathlib import Path
|
|
import config
|
|
import json
|
|
import os
|
|
|
|
capa_bp = Blueprint('capa', __name__)
|
|
|
|
@capa_bp.route('/analyze', methods=['GET'])
|
|
def analyze_capa():
|
|
file = secure_filename(request.args.get('file', ''))
|
|
if file == '':
|
|
abort(400)
|
|
filepath = Path(path.join(config.Config.FILE_DIRECTORY, file))
|
|
if not os.path.exists(filepath):
|
|
print(f"Error: File not found at '{filepath}'")
|
|
abort(400)
|
|
|
|
|
|
rules = capa.rules.get_rules([capa.main.get_default_root()/ "rules"])
|
|
extractor = capa.loader.get_extractor(filepath, FORMAT_AUTO, OS_AUTO, capa.main.BACKEND_VIV, [], should_save_workspace=False, disable_progress=True)
|
|
capabilities = capa.capabilities.common.find_capabilities(rules, extractor, disable_progress=True)
|
|
|
|
meta = capa.loader.collect_metadata([], filepath, FORMAT_AUTO, OS_AUTO, [capa.main.get_default_root()/ "rules"], extractor, capabilities)
|
|
|
|
|
|
return json.loads(capa.render.json.render(meta=meta, rules=rules, capabilities=capabilities.matches))
|