|
| 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 DetectionRule |
| 15 | +from vulnerabilities.models import DetectionRuleTypes |
| 16 | +from vulnerabilities.pipelines import VulnerableCodePipeline |
| 17 | + |
| 18 | + |
| 19 | +class YaraRulesImproverPipeline(VulnerableCodePipeline): |
| 20 | + pipeline_id = "yara_rules" |
| 21 | + |
| 22 | + repo_urls = [ |
| 23 | + "git+https://github.com/elastic/protections-artifacts", |
| 24 | + "git+https://github.com/Yara-Rules/rules", |
| 25 | + "git+https://github.com/Xumeiquer/yara-forensics", |
| 26 | + "git+https://github.com/reversinglabs/reversinglabs-yara-rules", |
| 27 | + "git+https://github.com/advanced-threat-research/Yara-Rules", |
| 28 | + "git+https://github.com/bartblaze/Yara-rules", |
| 29 | + "git+https://github.com/godaddy/yara-rules", # archived |
| 30 | + "git+https://github.com/SupportIntelligence/Icewater", |
| 31 | + "git+https://github.com/jeFF0Falltrades/YARA-Signatures", |
| 32 | + "git+https://github.com/tjnel/yara_repo", |
| 33 | + "git+https://github.com/JPCERTCC/jpcert-yara", |
| 34 | + "git+https://github.com/mikesxrs/Open-Source-YARA-rules", |
| 35 | + "git+https://github.com/fboldewin/YARA-rules", |
| 36 | + "git+https://github.com/h3x2b/yara-rules", |
| 37 | + ] |
| 38 | + |
| 39 | + license_urls = """ |
| 40 | + https://github.com/elastic/protections-artifacts/blob/main/LICENSE.txt |
| 41 | + https://github.com/Yara-Rules/rules/blob/master/LICENSE |
| 42 | + https://github.com/Xumeiquer/yara-forensics/blob/master/LICENSE |
| 43 | + https://github.com/reversinglabs/reversinglabs-yara-rules/blob/develop/LICENSE |
| 44 | + https://github.com/advanced-threat-research/Yara-Rules/blob/master/LICENSE |
| 45 | + https://github.com/bartblaze/Yara-rules/blob/master/LICENSE |
| 46 | + https://github.com/godaddy/yara-rules/blob/master/LICENSE.md |
| 47 | + https://github.com/SupportIntelligence/Icewater/blob/master/LICENSE |
| 48 | + https://github.com/jeFF0Falltrades/YARA-Signatures/blob/master/LICENSE.md |
| 49 | + https://github.com/tjnel/yara_repo/blob/master/LICENSE |
| 50 | + https://github.com/JPCERTCC/jpcert-yara/blob/main/LICENSE |
| 51 | + |
| 52 | + NO-LICENSE: https://github.com/mikesxrs/Open-Source-YARA-rules/ |
| 53 | + NO-LICENSE: https://github.com/fboldewin/YARA-rules |
| 54 | + NO-LICENSE: https://github.com/h3x2b/yara-rules |
| 55 | + """ |
| 56 | + |
| 57 | + def __init__(self, *args, **kwargs): |
| 58 | + super().__init__(*args, **kwargs) |
| 59 | + self.vcs_responses = [] |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def steps(cls): |
| 63 | + return ( |
| 64 | + cls.clone_repos, |
| 65 | + cls.collect_and_store_rules, |
| 66 | + cls.clean_downloads, |
| 67 | + ) |
| 68 | + |
| 69 | + def clone_repos(self): |
| 70 | + for url in self.repo_urls: |
| 71 | + self.log(f"Cloning `{url}`") |
| 72 | + try: |
| 73 | + response = fetch_via_vcs(url) |
| 74 | + if response: |
| 75 | + self.vcs_responses.append((response, url)) |
| 76 | + except Exception as e: |
| 77 | + self.log(f"Failed to clone {url}: {e}") |
| 78 | + |
| 79 | + def collect_and_store_rules(self): |
| 80 | + for vcs_response, repo_url in self.vcs_responses: |
| 81 | + base_directory = Path(vcs_response.dest_dir) |
| 82 | + yara_files = [ |
| 83 | + p |
| 84 | + for p in base_directory.rglob("*") |
| 85 | + if p.suffix in (".yar", ".yara") and p.is_file() |
| 86 | + ] |
| 87 | + |
| 88 | + rules_count = len(yara_files) |
| 89 | + self.log(f"Processing {rules_count:,d} rules from {repo_url}") |
| 90 | + |
| 91 | + progress = LoopProgress(total_iterations=rules_count, logger=self.log) |
| 92 | + for file_path in progress.iter(yara_files): |
| 93 | + if not file_path.exists() or not file_path.is_file(): |
| 94 | + self.log( |
| 95 | + f"Skipping file as it no longer exists or is not a file: {file_path}", |
| 96 | + level="warning", |
| 97 | + ) |
| 98 | + continue |
| 99 | + |
| 100 | + raw_text = file_path.read_text(encoding="utf-8", errors="ignore") |
| 101 | + if not raw_text: |
| 102 | + continue |
| 103 | + |
| 104 | + DetectionRule.objects.update_or_create( |
| 105 | + rule_text=raw_text, |
| 106 | + rule_type=DetectionRuleTypes.YARA, |
| 107 | + advisory=None, |
| 108 | + ) |
| 109 | + |
| 110 | + def clean_downloads(self): |
| 111 | + for vcs_response, _ in self.vcs_responses: |
| 112 | + if vcs_response: |
| 113 | + self.log(f"Removing cloned repository: {vcs_response.dest_dir}") |
| 114 | + vcs_response.delete() |
| 115 | + |
| 116 | + self.vcs_responses = [] |
| 117 | + |
| 118 | + def on_failure(self): |
| 119 | + self.clean_downloads() |
0 commit comments