|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +import requests |
| 5 | +from cortexutils.responder import Responder |
| 6 | + |
| 7 | + |
| 8 | +class AbuseIPDBResponder(Responder): |
| 9 | + """ |
| 10 | + AbuseIPDB Responder - Report malicious IP addresses to AbuseIPDB |
| 11 | + API documentation: https://docs.abuseipdb.com/#report-endpoint |
| 12 | + """ |
| 13 | + |
| 14 | + # Mapping from category ID to name (for display) |
| 15 | + CATEGORIES = { |
| 16 | + "1": "DNS Compromise", |
| 17 | + "2": "DNS Poisoning", |
| 18 | + "3": "Fraud Orders", |
| 19 | + "4": "DDoS Attack", |
| 20 | + "5": "FTP Brute-Force", |
| 21 | + "6": "Ping of Death", |
| 22 | + "7": "Phishing", |
| 23 | + "8": "Fraud VoIP", |
| 24 | + "9": "Open Proxy", |
| 25 | + "10": "Web Spam", |
| 26 | + "11": "Email Spam", |
| 27 | + "12": "Blog Spam", |
| 28 | + "13": "VPN IP", |
| 29 | + "14": "Port Scan", |
| 30 | + "15": "Hacking", |
| 31 | + "16": "SQL Injection", |
| 32 | + "17": "Spoofing", |
| 33 | + "18": "Brute Force", |
| 34 | + "19": "Bad Web Bot", |
| 35 | + "20": "Exploited Host", |
| 36 | + "21": "Web App Attack", |
| 37 | + "22": "SSH", |
| 38 | + "23": "IoT Targeted", |
| 39 | + } |
| 40 | + |
| 41 | + # Reverse mapping from category name to ID (for API calls) |
| 42 | + CATEGORY_NAME_TO_ID = {name: id for id, name in CATEGORIES.items()} |
| 43 | + |
| 44 | + def __init__(self): |
| 45 | + Responder.__init__(self) |
| 46 | + self.api_key = self.get_param("config.key", None, "Missing AbuseIPDB API key") |
| 47 | + self.categories = self.get_param("config.categories", ["Brute Force"]) |
| 48 | + self.comment = self.get_param("config.comment", "") |
| 49 | + self.service = self.get_param("config.service", "report") |
| 50 | + |
| 51 | + def run(self): |
| 52 | + Responder.run(self) |
| 53 | + |
| 54 | + if self.service == "report": |
| 55 | + self.report_ip() |
| 56 | + else: |
| 57 | + self.error(f"Unknown service: {self.service}") |
| 58 | + |
| 59 | + def report_ip(self): |
| 60 | + """Report an IP address to AbuseIPDB""" |
| 61 | + data_type = self.get_param("data.dataType", None) |
| 62 | + |
| 63 | + if data_type != "ip": |
| 64 | + self.error("This responder only supports IP addresses") |
| 65 | + return |
| 66 | + |
| 67 | + ip_address = self.get_param("data.data", None, "No IP address provided") |
| 68 | + |
| 69 | + # Ensure categories is a list |
| 70 | + category_list = self.categories if isinstance(self.categories, list) else [self.categories] |
| 71 | + |
| 72 | + # Validate and translate category names to IDs |
| 73 | + category_ids = [] |
| 74 | + invalid_categories = [] |
| 75 | + for cat_name in category_list: |
| 76 | + cat_name = cat_name.strip() |
| 77 | + if cat_name in self.CATEGORY_NAME_TO_ID: |
| 78 | + category_ids.append(self.CATEGORY_NAME_TO_ID[cat_name]) |
| 79 | + else: |
| 80 | + invalid_categories.append(cat_name) |
| 81 | + |
| 82 | + if invalid_categories: |
| 83 | + valid_names = ", ".join(self.CATEGORY_NAME_TO_ID.keys()) |
| 84 | + self.error(f"Invalid category names: {', '.join(invalid_categories)}. Valid categories are: {valid_names}") |
| 85 | + return |
| 86 | + |
| 87 | + if not category_ids: |
| 88 | + self.error("At least one category must be specified") |
| 89 | + return |
| 90 | + |
| 91 | + # Convert category IDs to comma-separated string for API |
| 92 | + categories_str = ",".join(category_ids) |
| 93 | + |
| 94 | + # Prepare the API request |
| 95 | + url = "https://api.abuseipdb.com/api/v2/report" |
| 96 | + headers = { |
| 97 | + "Accept": "application/json", |
| 98 | + "Key": self.api_key, |
| 99 | + "User-Agent": "strangebee-thehive/1.0", |
| 100 | + } |
| 101 | + payload = { |
| 102 | + "ip": ip_address, |
| 103 | + "categories": categories_str, |
| 104 | + } |
| 105 | + |
| 106 | + if self.comment: |
| 107 | + # Truncate comment to 1024 characters as per API limit |
| 108 | + payload["comment"] = self.comment[:1024] |
| 109 | + |
| 110 | + try: |
| 111 | + response = requests.post(url, headers=headers, data=payload) |
| 112 | + |
| 113 | + if response.status_code == 200: |
| 114 | + result = response.json() |
| 115 | + data = result.get("data", {}) |
| 116 | + |
| 117 | + self.report({ |
| 118 | + "success": True, |
| 119 | + "message": f"IP {ip_address} reported successfully to AbuseIPDB", |
| 120 | + "ip_address": data.get("ipAddress", ip_address), |
| 121 | + "abuse_confidence_score": data.get("abuseConfidenceScore"), |
| 122 | + "categories_reported": category_list, |
| 123 | + }) |
| 124 | + elif response.status_code == 422: |
| 125 | + # Validation error from API |
| 126 | + error_detail = response.json().get("errors", [{}])[0].get("detail", "Validation error") |
| 127 | + self.error(f"AbuseIPDB validation error: {error_detail}") |
| 128 | + elif response.status_code == 429: |
| 129 | + self.error("AbuseIPDB rate limit exceeded. Please try again later.") |
| 130 | + elif response.status_code == 401: |
| 131 | + self.error("Invalid AbuseIPDB API key") |
| 132 | + else: |
| 133 | + self.error(f"AbuseIPDB API error (HTTP {response.status_code}): {response.text}") |
| 134 | + |
| 135 | + except requests.exceptions.RequestException as e: |
| 136 | + self.error(f"Network error while contacting AbuseIPDB: {str(e)}") |
| 137 | + |
| 138 | + def operations(self, raw): |
| 139 | + """Add a tag to the artifact after successful reporting""" |
| 140 | + operations = [] |
| 141 | + if raw.get("success"): |
| 142 | + operations.append( |
| 143 | + self.build_operation("AddTagToArtifact", tag="AbuseIPDB:reported") |
| 144 | + ) |
| 145 | + return operations |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + AbuseIPDBResponder().run() |
0 commit comments