|
| 1 | +import hashlib |
| 2 | +import json |
| 3 | + |
| 4 | +from dojo.models import Finding |
| 5 | + |
| 6 | + |
| 7 | +class ProwlerParserJSON: |
| 8 | + |
| 9 | + """This parser is for Prowler JSON files.""" |
| 10 | + |
| 11 | + def get_findings(self, file, test): |
| 12 | + data = json.load(file) |
| 13 | + |
| 14 | + dupes = {} |
| 15 | + for node in data: |
| 16 | + # Skip vulnerability if the status is "PASS", continue parsing is status is "FAIL" or "MANUAL" |
| 17 | + if node.get("status_code") == "PASS": |
| 18 | + continue |
| 19 | + |
| 20 | + cloudtype = self.get_cloud_type(node) |
| 21 | + description = ( |
| 22 | + "**Cloud Type** : " |
| 23 | + + cloudtype |
| 24 | + + "\n\n" |
| 25 | + + "**Finding Description** : " |
| 26 | + + node.get("finding_info", {}).get("desc", "N/A") |
| 27 | + + "\n\n" |
| 28 | + + "**Product Name** : " |
| 29 | + + node.get("metadata", {}).get("product", {}).get("name", "N/A") |
| 30 | + + "\n\n" |
| 31 | + + "**Status Detail** : " |
| 32 | + + node.get("status_detail", "N/A") |
| 33 | + + "\n\n" |
| 34 | + + "**Finding Created Time** : " |
| 35 | + + node.get("finding_info", {}).get("created_time_dt", "N/A") |
| 36 | + ) |
| 37 | + # Add cloud type sepecific information to description |
| 38 | + description = self.add_cloud_type_metadata(node, cloudtype, description) |
| 39 | + |
| 40 | + title = node.get("message", "") |
| 41 | + severity = self.convert_severity(node.get("severity")) |
| 42 | + mitigation = ( |
| 43 | + "**Remediation Description** : " |
| 44 | + + node.get("remediation", {}).get("desc", "N/A") |
| 45 | + + "\n\n" |
| 46 | + + "**Remediation References** : " |
| 47 | + + ", ".join(node.get("remediation", {}).get("references", [])) |
| 48 | + ) |
| 49 | + impact = node.get("risk_details", "") |
| 50 | + compliance = node.get("unmapped", {}).get("compliance", {}) |
| 51 | + references = "**Related URL** : " + node.get("unmapped", {}).get("related_url", "") |
| 52 | + # Add data presnet in scan to References |
| 53 | + for key, values in compliance.items(): |
| 54 | + joined = ", ".join(values) |
| 55 | + # Ex: CIS-1.10 : 1.2.16 |
| 56 | + references += f"\n\n**{key}** : {joined}" |
| 57 | + |
| 58 | + finding = Finding( |
| 59 | + title=title, |
| 60 | + test=test, |
| 61 | + description=description, |
| 62 | + severity=severity, |
| 63 | + references=references, |
| 64 | + mitigation=mitigation, |
| 65 | + impact=impact, |
| 66 | + static_finding=False, |
| 67 | + dynamic_finding=True, |
| 68 | + ) |
| 69 | + |
| 70 | + # internal de-duplication |
| 71 | + dupe_key = hashlib.sha256(str(description + title).encode("utf-8")).hexdigest() |
| 72 | + if dupe_key in dupes: |
| 73 | + find = dupes[dupe_key] |
| 74 | + if finding.description: |
| 75 | + find.description += "\n" + finding.description |
| 76 | + # find.unsaved_endpoints.extend(finding.unsaved_endpoints) |
| 77 | + dupes[dupe_key] = find |
| 78 | + else: |
| 79 | + dupes[dupe_key] = finding |
| 80 | + |
| 81 | + return list(dupes.values()) |
| 82 | + |
| 83 | + def convert_severity(self, severity: str) -> str: |
| 84 | + """Convert severity value""" |
| 85 | + if not severity: |
| 86 | + return "Info" |
| 87 | + |
| 88 | + s = severity.lower() |
| 89 | + if s == "critical": |
| 90 | + return "Critical" |
| 91 | + if s == "high": |
| 92 | + return "High" |
| 93 | + if s == "medium": |
| 94 | + return "Medium" |
| 95 | + if s == "low": |
| 96 | + return "Low" |
| 97 | + return "Info" |
| 98 | + |
| 99 | + def get_cloud_type(self, node: dict) -> str: |
| 100 | + """Determine the cloud type of a Prowler JSON finding. Returns one of: AWS, Azure, Kubernetes, GCP, or N/A""" |
| 101 | + # Check for GCP, AWS, or Azure |
| 102 | + account_type = node.get("cloud", {}).get("provider") |
| 103 | + if account_type: |
| 104 | + account_type.lower() |
| 105 | + if account_type == "gcp": |
| 106 | + return "GCP" |
| 107 | + if account_type == "aws": |
| 108 | + return "AWS" |
| 109 | + if account_type == "azure": |
| 110 | + return "AZURE" |
| 111 | + |
| 112 | + # Check for Kubernetes |
| 113 | + for resource in node.get("resources", []): |
| 114 | + namespace = resource.get("data", {}).get("metadata", {}).get("namespace") |
| 115 | + if namespace is not None: |
| 116 | + return "KUBERNETES" |
| 117 | + |
| 118 | + # No Cloud Type information was found |
| 119 | + return "N/A" |
| 120 | + |
| 121 | + def add_cloud_type_metadata(self, node: dict, cloudtype: str, description: str) -> str: |
| 122 | + # Add metadata for GCP, AWS, and Azure |
| 123 | + if cloudtype in {"GCP", "AWS", "AZURE"}: |
| 124 | + description += "\n\n" + "**" + cloudtype + " Region** : " + node.get("cloud", {}).get("region", "N/A") |
| 125 | + return description |
| 126 | + |
| 127 | + # Add metadata for Kubernetes |
| 128 | + if cloudtype == "KUBERNETES": |
| 129 | + for resource in node.get("resources", []): |
| 130 | + pod = resource.get("data", {}).get("metadata", {}).get("name") |
| 131 | + namespace = resource.get("data", {}).get("metadata", {}).get("namespace") |
| 132 | + if pod is not None: |
| 133 | + description += "\n\n" + "**Pod Name** : " + pod |
| 134 | + if namespace is not None: |
| 135 | + description += "\n\n" + "**Namespace** : " + namespace |
| 136 | + return description |
| 137 | + return description |
0 commit comments