|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from aboutcode.pipeline import LoopProgress |
| 12 | +from fetchcode.vcs import fetch_via_vcs |
| 13 | + |
| 14 | +from vulnerabilities.models import AdvisoryAlias |
| 15 | +from vulnerabilities.models import AdvisoryV2 |
| 16 | +from vulnerabilities.models import DetectionRule |
| 17 | +from vulnerabilities.models import DetectionRuleTypes |
| 18 | +from vulnerabilities.pipelines import VulnerableCodePipeline |
| 19 | +from vulnerabilities.utils import find_all_cve |
| 20 | +from vulnerabilities.utils import get_advisory_url |
| 21 | + |
| 22 | + |
| 23 | +class YaraRulesImproverPipeline(VulnerableCodePipeline): |
| 24 | + pipeline_id = "yara_rules" |
| 25 | + |
| 26 | + repo_urls = [ |
| 27 | + "git+https://github.com/elastic/protections-artifacts", |
| 28 | + "git+https://github.com/Yara-Rules/rules", |
| 29 | + "git+https://github.com/Xumeiquer/yara-forensics", |
| 30 | + "git+https://github.com/reversinglabs/reversinglabs-yara-rules", |
| 31 | + "git+https://github.com/advanced-threat-research/Yara-Rules", |
| 32 | + "git+https://github.com/bartblaze/Yara-rules", |
| 33 | + "git+https://github.com/godaddy/yara-rules", # archived |
| 34 | + "git+https://github.com/SupportIntelligence/Icewater", |
| 35 | + "git+https://github.com/jeFF0Falltrades/YARA-Signatures", |
| 36 | + "git+https://github.com/tjnel/yara_repo", |
| 37 | + "git+https://github.com/JPCERTCC/jpcert-yara", |
| 38 | + "git+https://github.com/mikesxrs/Open-Source-YARA-rules", |
| 39 | + "git+https://github.com/fboldewin/YARA-rules", |
| 40 | + "git+https://github.com/h3x2b/yara-rules", |
| 41 | + "git+https://github.com/roadwy/DefenderYara", |
| 42 | + "git+https://github.com/mthcht/ThreatHunting-Keywords-yara-rules", |
| 43 | + "git+https://github.com/Neo23x0/signature-base", |
| 44 | + "git+https://github.com/malpedia/signator-rules", |
| 45 | + "git+https://github.com/baderj/yara", |
| 46 | + "git+https://github.com/deadbits/yara-rules", # archived |
| 47 | + "git+https://github.com/pmelson/yara_rules", |
| 48 | + "git+https://github.com/sbousseaden/YaraHunts", |
| 49 | + "git+https://github.com/embee-research/Yara-detection-rules", |
| 50 | + "git+https://github.com/RussianPanda95/Yara-Rules", |
| 51 | + "git+https://github.com/ail-project/ail-yara-rules", |
| 52 | + "git+https://github.com/MalGamy/YARA_Rules", |
| 53 | + "git+https://github.com/elceef/yara-rulz", |
| 54 | + "git+https://github.com/tenable/yara-rules", |
| 55 | + "git+https://github.com/dr4k0nia/yara-rules", |
| 56 | + "git+https://github.com/umair9747/yara-rules", |
| 57 | + ] |
| 58 | + |
| 59 | + license_urls = """ |
| 60 | + https://github.com/elastic/protections-artifacts/blob/main/LICENSE.txt |
| 61 | + https://github.com/Yara-Rules/rules/blob/master/LICENSE |
| 62 | + https://github.com/Xumeiquer/yara-forensics/blob/master/LICENSE |
| 63 | + https://github.com/reversinglabs/reversinglabs-yara-rules/blob/develop/LICENSE |
| 64 | + https://github.com/advanced-threat-research/Yara-Rules/blob/master/LICENSE |
| 65 | + https://github.com/bartblaze/Yara-rules/blob/master/LICENSE |
| 66 | + https://github.com/godaddy/yara-rules/blob/master/LICENSE.md |
| 67 | + https://github.com/SupportIntelligence/Icewater/blob/master/LICENSE |
| 68 | + https://github.com/jeFF0Falltrades/YARA-Signatures/blob/master/LICENSE.md |
| 69 | + https://github.com/tjnel/yara_repo/blob/master/LICENSE |
| 70 | + https://github.com/JPCERTCC/jpcert-yara/blob/main/LICENSE |
| 71 | + https://github.com/mthcht/ThreatHunting-Keywords-yara-rules/blob/main/LICENSE |
| 72 | + https://github.com/malpedia/signator-rules -> https://creativecommons.org/licenses/by-sa/4.0/ |
| 73 | + https://github.com/baderj/yara/blob/main/LICENSE |
| 74 | + https://github.com/deadbits/yara-rules/blob/master/UNLICENSE |
| 75 | + https://github.com/embee-research/Yara-detection-rules/tree/main?tab=readme-ov-file#detection-rule-license-drl-11 |
| 76 | + https://github.com/ail-project/ail-yara-rules?tab=AGPL-3.0-1-ov-file |
| 77 | + https://github.com/MalGamy/YARA_Rules/blob/main/LICENSE.md |
| 78 | + https://github.com/elceef/yara-rulz/tree/main?tab=MIT-1-ov-file |
| 79 | + https://github.com/tenable/yara-rules/tree/master?tab=BSD-3-Clause-1-ov-file |
| 80 | + https://github.com/dr4k0nia/yara-rules/blob/main/LICENSE.md |
| 81 | + https://github.com/umair9747/yara-rules?tab=GPL-3.0-1-ov-file |
| 82 | + |
| 83 | + NO-LICENSE: https://github.com/mikesxrs/Open-Source-YARA-rules/ |
| 84 | + NO-LICENSE: https://github.com/fboldewin/YARA-rules |
| 85 | + NO-LICENSE: https://github.com/h3x2b/yara-rules |
| 86 | + NO-LICENSE: https://github.com/roadwy/DefenderYara |
| 87 | + NO-LICENSE: https://github.com/pmelson/yara_rules |
| 88 | + NO-LICENSE: https://github.com/sbousseaden/YaraHunts |
| 89 | + NO-LICENSE: https://github.com/RussianPanda95/Yara-Rules |
| 90 | + """ |
| 91 | + |
| 92 | + def __init__(self, *args, **kwargs): |
| 93 | + super().__init__(*args, **kwargs) |
| 94 | + self.vcs_responses = [] |
| 95 | + |
| 96 | + @classmethod |
| 97 | + def steps(cls): |
| 98 | + return ( |
| 99 | + cls.clone_repos, |
| 100 | + cls.collect_and_store_rules, |
| 101 | + cls.clean_downloads, |
| 102 | + ) |
| 103 | + |
| 104 | + def clone_repos(self): |
| 105 | + for repo_url in self.repo_urls: |
| 106 | + self.log(f"Cloning `{repo_url}`") |
| 107 | + try: |
| 108 | + response = fetch_via_vcs(repo_url) |
| 109 | + if response: |
| 110 | + self.vcs_responses.append((response, repo_url)) |
| 111 | + except Exception as e: |
| 112 | + self.log(f"Failed to clone {repo_url}: {e}") |
| 113 | + |
| 114 | + def collect_and_store_rules(self): |
| 115 | + for vcs_response, repo_url in self.vcs_responses: |
| 116 | + base_directory = Path(vcs_response.dest_dir) |
| 117 | + yara_files = [ |
| 118 | + p |
| 119 | + for p in base_directory.rglob("*") |
| 120 | + if p.suffix in (".yar", ".yara") and p.is_file() |
| 121 | + ] |
| 122 | + |
| 123 | + rules_count = len(yara_files) |
| 124 | + self.log(f"Processing {rules_count:,d} rules from {repo_url}") |
| 125 | + progress = LoopProgress(total_iterations=rules_count, logger=self.log) |
| 126 | + for file_path in progress.iter(yara_files): |
| 127 | + if not file_path.exists() or not file_path.is_file(): |
| 128 | + self.log( |
| 129 | + f"Skipping file as it no longer exists or is not a file: {file_path}", |
| 130 | + level="warning", |
| 131 | + ) |
| 132 | + continue |
| 133 | + |
| 134 | + raw_text = file_path.read_text(encoding="utf-8", errors="ignore") |
| 135 | + if not raw_text: |
| 136 | + continue |
| 137 | + raw_text = raw_text.replace("\x00", "") |
| 138 | + |
| 139 | + repo_url = repo_url.strip("git+") |
| 140 | + rule_url = get_advisory_url( |
| 141 | + file=file_path, |
| 142 | + base_path=base_directory, |
| 143 | + url=f"{repo_url}/blob/master/", |
| 144 | + ) |
| 145 | + |
| 146 | + cve_ids = find_all_cve(f"{file_path}\n{raw_text}") |
| 147 | + |
| 148 | + advisories = set() |
| 149 | + for cve_id in cve_ids: |
| 150 | + alias = AdvisoryAlias.objects.filter(alias=cve_id).first() |
| 151 | + if alias: |
| 152 | + for adv in alias.advisories.all(): |
| 153 | + advisories.add(adv) |
| 154 | + else: |
| 155 | + advs = AdvisoryV2.objects.filter(advisory_id=cve_id) |
| 156 | + for adv in advs: |
| 157 | + advisories.add(adv) |
| 158 | + |
| 159 | + detection_rule, _ = DetectionRule.objects.update_or_create( |
| 160 | + rule_type=DetectionRuleTypes.YARA, |
| 161 | + source_url=rule_url, |
| 162 | + defaults={ |
| 163 | + "rule_text": raw_text, |
| 164 | + }, |
| 165 | + ) |
| 166 | + |
| 167 | + for adv in advisories: |
| 168 | + detection_rule.related_advisories.add(adv) |
| 169 | + |
| 170 | + def clean_downloads(self): |
| 171 | + for vcs_response, _ in self.vcs_responses: |
| 172 | + if vcs_response: |
| 173 | + self.log(f"Removing cloned repository: {vcs_response.dest_dir}") |
| 174 | + vcs_response.delete() |
| 175 | + |
| 176 | + self.vcs_responses = [] |
| 177 | + |
| 178 | + def on_failure(self): |
| 179 | + self.clean_downloads() |
0 commit comments