Ejemplo n.º 1
0
def generate_tactic_md(tactic, domain, tactic_list, techniques, side_nav_data,
                       notes):
    """Generate markdown for given tactic"""

    attack_id = util.buildhelpers.get_attack_id(tactic)

    # Add if attack id is found
    if attack_id:

        data = {}

        # Fill out data

        data['attack_id'] = attack_id
        data['name'] = tactic['name']
        data['name_lower'] = tactic['name'].lower()
        data['side_menu_data'] = side_nav_data
        data['domain'] = domain.split("-")[0]
        data['notes'] = notes.get(tactic['id'])

        if tactic.get('x_mitre_deprecated'):
            data['deprecated'] = True
        else:
            data['deprecated'] = False

        # Add more detail if descriptione exists and it is not deprecated
        if tactic.get("description") and not data['deprecated']:
            data['descr'] = tactic['description']

            dates = util.buildhelpers.get_created_and_modified_dates(tactic)

            if dates.get('created'):
                data['created'] = dates['created']

            if dates.get('modified'):
                data['modified'] = dates['modified']

            # Get techniques that are in the given tactic
            techniques_list = get_techniques_of_tactic(tactic,
                                                       techniques[domain])

            data[
                'techniques_table'] = util.buildhelpers.get_technique_table_data(
                    tactic, techniques_list)
            data['techniques_table_len'] = str(len(techniques_list))

            data['versioning_feature'] = site_config.check_versions_module()

        else:
            if data['deprecated']:
                data['descr'] = tactic.get('description')

        subs = tactics_config.tactic_md.substitute(data)
        subs = subs + json.dumps(data)

        with open(os.path.join(tactics_config.tactics_markdown_path,
                               data['attack_id'] + ".md"),
                  "w",
                  encoding='utf8') as md_file:
            md_file.write(subs)
Ejemplo n.º 2
0
def generate_mitigation_md(mitigation, domain, side_menu_data,
                           side_menu_mobile_data):
    """Generates the markdown for the given mitigation"""

    attack_id = util.buildhelpers.get_attack_id(mitigation)

    if attack_id:
        data = {}

        data['attack_id'] = attack_id

        data['domain'] = domain.split("-")[0]
        data['side_menu_data'] = side_menu_data
        data['side_menu_mobile_view_data'] = side_menu_mobile_data
        data['name'] = mitigation['name']

        dates = util.buildhelpers.get_created_and_modified_dates(mitigation)

        if dates.get('created'):
            data['created'] = dates['created']

        if dates.get('modified'):
            data['modified'] = dates['modified']

        # Get initial reference list
        reference_list = {'current_number': 0}

        # Get initial reference list from mitigation object
        reference_list = util.buildhelpers.update_reference_list(
            reference_list, mitigation)

        if mitigation.get('description'):
            data['descr'] = mitigation['description']
            data['descr'] = data['descr']

        if mitigation.get('x_mitre_deprecated'):
            data['deprecated'] = True
        if mitigation.get('x_mitre_version'):
            data['version'] = mitigation["x_mitre_version"]

        data['techniques_addressed_data'] = get_techniques_addressed_data(
            mitigation, reference_list)

        data['citations'] = reference_list

        data['versioning_feature'] = site_config.check_versions_module()

        subs = mitigations_config.mitigation_md.substitute(data)
        subs = subs + json.dumps(data)

        with open(os.path.join(mitigations_config.mitigation_markdown_path,
                               data['attack_id'] + ".md"),
                  "w",
                  encoding='utf8') as md_file:
            md_file.write(subs)
Ejemplo n.º 3
0
def generate_platform_matrices(matrix, notes, side_menu_data=None):
    """Given a matrix, generates the matrix markdown"""

    has_data = False
    data = {}
    data['menu'] = side_menu_data
    data['domain'] = matrix['matrix'].split("-")[0]
    data['name'] = matrix['name']

    data['matrices'], data["has_subtechniques"], data[
        "tour_technique"] = get_sub_matrices(matrix)
    if data['matrices']:
        has_data = True
        matrix_ids = get_matrix_ids(data['matrices'])
        data['notes'] = []
        for matrix_id in matrix_ids:
            data['notes'].append(notes.get(matrix_id))

    data['platforms'] = [{
        "name": platform,
        "path": matrices_config.platform_to_path[platform]
    } for platform in matrix['platforms']]
    data['navigator_link'] = site_config.navigator_link

    data['descr'] = matrix['descr']
    data['path'] = matrix['path']

    data['versioning_feature'] = site_config.check_versions_module()
    data['resources'] = site_config.check_resources_module()

    subs = matrices_config.matrix_md.substitute(data)
    subs = subs + json.dumps(data)

    with open(os.path.join(matrices_config.matrix_markdown_path,
                           data['domain'] + "-" + matrix['name'] + ".md"),
              "w",
              encoding='utf8') as md_file:
        md_file.write(subs)

    for subtype in matrix['subtypes']:
        generate_platform_matrices(subtype, notes, side_menu_data)

    return has_data
Ejemplo n.º 4
0
def generate_mitigation_md(mitigation, domain, side_menu_data,
                           side_menu_mobile_data, notes):
    """Generates the markdown for the given mitigation"""
    attack_id = util.buildhelpers.get_attack_id(mitigation)

    if attack_id:
        data = {}

        data["attack_id"] = attack_id

        data["domain"] = domain.split("-")[0]
        data["side_menu_data"] = side_menu_data
        data["side_menu_mobile_view_data"] = side_menu_mobile_data
        data["name"] = mitigation["name"]
        data["notes"] = notes.get(mitigation["id"])

        dates = util.buildhelpers.get_created_and_modified_dates(mitigation)

        if dates.get("created"):
            data["created"] = dates["created"]

        if dates.get("modified"):
            data["modified"] = dates["modified"]

        # Get initial reference list
        reference_list = {"current_number": 0}

        # Get initial reference list from mitigation object
        reference_list = util.buildhelpers.update_reference_list(
            reference_list, mitigation)

        if mitigation.get("description"):
            data["descr"] = mitigation["description"]
            data["descr"] = data["descr"]

        if mitigation.get("x_mitre_deprecated"):
            data["deprecated"] = True
        if mitigation.get("x_mitre_version"):
            data["version"] = mitigation["x_mitre_version"]

        if mitigation.get("labels"):
            mitigation["labels"].sort()
            data["security_controls"] = ", ".join(mitigation["labels"])

        data["techniques_addressed_data"] = get_techniques_addressed_data(
            mitigation, reference_list)

        # Get navigator layers for this group
        layers = util.buildhelpers.get_navigator_layers(
            data["name"],
            data["attack_id"],
            "mitigation",
            data["version"] if "version" in data else None,
            data["techniques_addressed_data"],
        )

        data["layers"] = []
        for layer in layers:
            with open(
                    os.path.join(
                        mitigations_config.mitigation_markdown_path,
                        "-".join([
                            data["attack_id"], "techniques", layer["domain"]
                        ]) + ".md",
                    ),
                    "w",
                    encoding="utf8",
            ) as layer_json:
                subs = site_config.layer_md.substitute({
                    "attack_id":
                    data["attack_id"],
                    "path":
                    "mitigations/" + data["attack_id"],
                    "domain":
                    layer["domain"],
                })
                subs = subs + layer["layer"]
                layer_json.write(subs)
            data["layers"].append({
                "domain":
                layer["domain"],
                "filename":
                "-".join([data["attack_id"], layer["domain"], "layer"]) +
                ".json",
                "navigator_link":
                site_config.navigator_link,
            })

        data["citations"] = reference_list

        data["versioning_feature"] = site_config.check_versions_module()

        subs = mitigations_config.mitigation_md.substitute(data)
        subs = subs + json.dumps(data)

        with open(os.path.join(mitigations_config.mitigation_markdown_path,
                               data["attack_id"] + ".md"),
                  "w",
                  encoding="utf8") as md_file:
            md_file.write(subs)
Ejemplo n.º 5
0
def generate_data_for_md(technique_dict,
                         technique,
                         tactic_list,
                         is_sub_technique=False):
    """Given a technique or subtechnique, fill technique dictionary to create
       markdown file
    """

    technique_dict['name'] = technique.get('name')

    if is_sub_technique:
        technique_dict['attack_id'] = util.buildhelpers.get_attack_id(
            technique)
        technique_dict['sub_number'] = technique_dict['attack_id'].split(
            ".")[1]
        technique_dict['is_subtechnique'] = True

    if technique_dict['attack_id']:
        # Get capecs and mtcs
        for ref in technique['external_references']:
            if ref.get('source_name'):
                if ref['source_name'] == "capec":
                    if technique_dict.get('capecs') is None:
                        technique_dict['capecs'] = []
                    capec_dict = {'id': ref['external_id'], 'url': ref['url']}
                    technique_dict['capecs'].append(capec_dict)

                if ref['source_name'] == "NIST Mobile Threat Catalogue":
                    if technique_dict.get('mtcs') is None:
                        technique_dict['mtcs'] = []

                    mtcs_dict = {'id': ref['external_id'], 'url': ref['url']}
                    technique_dict['mtcs'].append(mtcs_dict)

        # Get initial reference list
        reference_list = {'current_number': 0}

        # Get initial reference list from technique object
        reference_list = util.buildhelpers.update_reference_list(
            reference_list, technique)

        dates = util.buildhelpers.get_created_and_modified_dates(technique)

        if dates.get('created'):
            technique_dict['created'] = dates['created']

        if dates.get('modified'):
            technique_dict['modified'] = dates['modified']

        if technique.get('x_mitre_deprecated'):
            technique_dict['deprecated'] = True
        else:
            technique_dict['deprecated'] = False

        # Get technique description with citations
        if technique.get("description") and not technique_dict['deprecated']:

            technique_dict['descr'] = technique['description']

            # Get mitigation table
            technique_dict['mitigation_table'] = get_mitigations_table_data(
                technique, reference_list)

            # Get related techniques
            technique_dict[
                'rel_techniques_table'] = get_related_techniques_data(
                    technique, tactic_list)

            # Get examples
            technique_dict['examples_table'] = get_examples_table_data(
                technique, reference_list)

            # Get technique version
            if technique.get("x_mitre_version"):
                technique_dict['version'] = technique["x_mitre_version"]

            # Get tactics of technique
            if technique.get('kill_chain_phases'):
                technique_dict['tactics'] = []
                for elem in technique['kill_chain_phases']:
                    technique_dict['tactics'].append(
                        elem['phase_name'].title().replace('-', ' '))

            # Get platforms that technique uses
            if technique.get('x_mitre_platforms'):
                technique['x_mitre_platforms'].sort()
                technique_dict['platforms'] = ", ".join(
                    technique['x_mitre_platforms'])

            # Get system requirements
            if technique.get('x_mitre_system_requirements'):
                technique['x_mitre_system_requirements'].sort()
                technique_dict['sysreqs'] = ", ".join(
                    technique['x_mitre_system_requirements'])
                technique_dict['sysreqs'] = re.sub("\.?\\n+", "; ",
                                                   technique_dict['sysreqs'])

            # Get permissions required
            if technique.get('x_mitre_permissions_required'):
                technique['x_mitre_permissions_required'].sort()
                technique_dict['perms'] = ", ".join(
                    technique['x_mitre_permissions_required'])

            # Get effective permissions
            if technique.get('x_mitre_effective_permissions'):
                technique['x_mitre_effective_permissions'].sort()
                technique_dict['eff_perms'] = ", ".join(
                    technique['x_mitre_effective_permissions'])

            # Get data sources
            if technique.get('x_mitre_data_sources'):
                technique['x_mitre_data_sources'].sort()
                technique_dict['data_sources'] = ", ".join(
                    technique['x_mitre_data_sources'])

            # Get if technique supports remote
            if technique.get('x_mitre_remote_support'):
                if technique['x_mitre_remote_support']:
                    technique_dict['supports_remote'] = " Yes"
                else:
                    technique_dict['supports_remote'] = " No"

            # Get network requirements
            if technique.get('x_mitre_network_requirements'):
                if technique['x_mitre_network_requirements']:
                    technique_dict['network_reqs'] = " Yes"
                else:
                    technique_dict['network_reqs'] = " No"

            # Get list of impacts
            if technique.get('x_mitre_impact_type'):
                technique['x_mitre_impact_type'].sort()
                technique_dict['impact_type'] = ", ".join(
                    technique['x_mitre_impact_type'])

            # Get list of defenses bypassed
            if technique.get('x_mitre_defense_bypassed'):
                technique['x_mitre_defense_bypassed'].sort()
                technique_dict['def_bypass'] = "******".join(
                    technique['x_mitre_defense_bypassed'])

            # Get list of contributors
            if technique.get('x_mitre_contributors'):
                technique['x_mitre_contributors'].sort()
                technique_dict['contributors'] = "; ".join(
                    technique['x_mitre_contributors'])

            # Get list of tactic types
            if technique.get('x_mitre_tactic_type'):
                technique['x_mitre_tactic_type'].sort()
                technique_dict['tactic_type'] = ", ".join(
                    technique['x_mitre_tactic_type'])

            # Get detection data
            if technique.get('x_mitre_detection'):
                technique_dict['detection'] = technique['x_mitre_detection']

            # Get if technique is detectable by common defenses
            if technique.get('x_mitre_detectable_by_common_defenses'):
                technique_dict['detectable'] = technique.get(
                    'x_mitre_detectable_by_common_defenses')

            # Get explanation of detecatable by common defenses
            if technique.get(
                    'x_mitre_detectable_by_common_defenses_explanation'):
                technique_dict[
                    'detectable_exp'] = util.buildhelpers.replace_html_chars(
                        technique[
                            'x_mitre_detectable_by_common_defenses_explanation']
                    )

            # Get diffulty for adversaries
            if technique.get('x_mitre_difficulty_for_adversary'):
                technique_dict['diff_for_adv'] = technique[
                    'x_mitre_difficulty_for_adversary']

            # Get explanation of difficulty for adversaries
            if technique.get('x_mitre_difficulty_for_adversary_explanation'):
                technique_dict[
                    'diff_for_adv_exp'] = util.buildhelpers.replace_html_chars(
                        technique[
                            'x_mitre_difficulty_for_adversary_explanation'])

            technique_dict['citations'] = reference_list

            technique_dict[
                'versioning_feature'] = site_config.check_versions_module()

        else:
            if technique_dict['deprecated']:
                technique_dict['descr'] = technique.get('description')

        return technique_dict