Repository navigation
Expand file tree
/
Copy pathenhance_with_github_poc.py
More file actions
88 lines (71 loc) · 3.09 KB
/
Copy pathenhance_with_github_poc.py
File metadata and controls
88 lines (71 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import json
from pathlib import Path
from aboutcode.pipeline import LoopProgress
from fetchcode.vcs import fetch_via_vcs
from vulnerabilities.models import AdvisoryPOC
from vulnerabilities.pipelines import VulnerableCodePipeline
from vulnerabilities.utils import relate_aliases_with_advisories
class GithubPocsImproverPipeline(VulnerableCodePipeline):
"""
Pipeline to Collect an exploit-PoCs repository, parse exploit JSON files,
match them to advisories via aliases, and update/create POC records.
"""
pipeline_id = "enhance_with_github_poc"
repo_url = "git+https://github.com/nomi-sec/PoC-in-GitHub"
@classmethod
def steps(cls):
return (
cls.clone_repo,
cls.collect_and_store_exploits,
cls.clean_downloads,
)
def clone_repo(self):
self.log(f"Cloning `{self.repo_url}`")
self.vcs_response = fetch_via_vcs(self.repo_url)
def collect_and_store_exploits(self):
"""
Parse PoC JSON files, match them to advisories via aliases,
and create or update related exploit records.
"""
base_directory = Path(self.vcs_response.dest_dir)
json_files = list(base_directory.rglob("**/*.json"))
exploits_count = len(json_files)
self.log(f"Enhancing the vulnerability with {exploits_count:,d} exploit records")
progress = LoopProgress(total_iterations=exploits_count, logger=self.log)
for file_path in progress.iter(json_files):
with open(file_path, "r") as f:
try:
exploits_data = json.load(f)
except json.JSONDecodeError:
self.log(f"Invalid JSON in {file_path}, skipping.")
continue
filename = file_path.stem.strip()
advisories = relate_aliases_with_advisories([filename])
for advisory in advisories:
for exploit_data in exploits_data:
exploit_repo_url = exploit_data.get("html_url")
if not exploit_repo_url:
continue
AdvisoryPOC.objects.update_or_create(
advisory=advisory,
url=exploit_repo_url,
defaults={
"created_at": exploit_data.get("created_at"),
"updated_at": exploit_data.get("updated_at"),
},
)
self.log(f"Successfully added {exploits_count:,d} poc exploit advisory")
def clean_downloads(self):
if self.vcs_response:
self.log(f"Removing cloned repository")
self.vcs_response.delete()
def on_failure(self):
self.clean_downloads()