-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabuse_check.py
194 lines (168 loc) · 7.61 KB
/
abuse_check.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import subprocess
import requests
import json
import io
def check_ip_reputation(ip_address, api_key, max_age_in_days=90):
"""
Check the reputation of an IP address using AbuseIPDB.
Args:
ip_address (str): The IP address to check.
api_key (str): Your AbuseIPDB API key.
max_age_in_days (int): The maximum age of reports in days. Default is 90.
Returns:
dict: Parsed JSON response from AbuseIPDB.
"""
# API endpoint
url = 'https://api.abuseipdb.com/api/v2/check'
# Query parameters
querystring = {
'ipAddress': ip_address,
'maxAgeInDays': str(max_age_in_days),
'verbose': 'true'
}
# Headers
headers = {
'Accept': 'application/json',
'Key': api_key
}
try:
# Make the request
response = requests.get(url, headers=headers, params=querystring)
response.raise_for_status() # Raise an error for HTTP codes 4xx or 5xx
# Parse and return the response
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error while checking IP reputation: {e}")
return None
# def analyze_ip_reputation(data):
# """
# Analyze the reputation of an IP address from the AbuseIPDB response.
#
# Args:
# data (dict): Parsed JSON response from AbuseIPDB.
#
# Returns:
# str: Prints the analysis results and returns ip_reputation_string.
# """
# if not data or "data" not in data:
# print("Invalid or empty data received.")
# return
#
# ip_info = data["data"]
#
# # print("ip_info:", ip_info)
#
# # Create an StringIO object to capture all prints
# with io.StringIO() as output:
# # Basic Information
# output.write(f"IP Address: {ip_info['ipAddress']}\n")
# output.write(f"Abuse Confidence Score: {ip_info['abuseConfidenceScore']} (High risk if > 50)\n")
# output.write(f"Country: {ip_info['countryName']} ({ip_info['countryCode']})\n")
# output.write(f"ISP: {ip_info.get('isp', 'Unknown')} ({ip_info.get('domain', 'No domain')})\n")
# output.write(f"Usage Type: {ip_info.get('usageType', 'Unknown')}\n")
# output.write(f"Total Reports: {ip_info['totalReports']} from {ip_info['numDistinctUsers']} distinct users\n")
#
# # Check if the IP is whitelisted
# if ip_info["isWhitelisted"]:
# output.write("This IP is whitelisted.\n")
# else:
# output.write("This IP is NOT whitelisted.\n")
#
# # Tor Detection
# if ip_info["isTor"]:
# output.write("Warning: This IP is associated with a Tor exit node.\n")
#
# # Last Report Information
# last_reported = ip_info.get("lastReportedAt")
# if last_reported:
# output.write(f"Last Reported At: {last_reported}\n")
#
# # Detailed Report Comments
# if "reports" in ip_info and ip_info["reports"]:
# output.write("\nRecent Reports:\n")
# for report in ip_info["reports"]:
# output.write(f" - Reported At: {report['reportedAt']}\n")
# output.write(f" Comment: {report['comment']}\n")
# output.write(f" Categories: {report['categories']}\n")
# output.write(
# f" Reporter Country: {report['reporterCountryName']} ({report['reporterCountryCode']})\n\n")
#
# print(f"IP Reputation Analysis Output: {output.readlines()}") # Print the captured output
# # output.read().strip()) # Print the captured output
# return output.read().strip() # Remove trailing newline
#
# # return ip_reputation_string
# def append_and_print(target_string, string):
# # Append the string to the variable
# # target_string += string + "\n"
# target_string.join(string + "\n")
# print(f">>> {string}") # Print the string
# return target_string
def analyze_ip_reputation(data):
"""
Analyze the reputation of an IP address from the AbuseIPDB response.
Args:
data (dict): Parsed JSON response from AbuseIPDB.
Returns:
str: Prints the analysis results.
"""
if not data or "data" not in data:
print("Invalid or empty data received.")
return
ip_info = data["data"]
ip_reputation_string = ""
# Basic Information
print(f"IP Address: {ip_info['ipAddress']}")
ip_reputation_string += f"IP Address: {ip_info['ipAddress']}\n"
print(f"Abuse Confidence Score: {ip_info['abuseConfidenceScore']} (High risk if > 50)")
ip_reputation_string += f"Abuse Confidence Score: {ip_info['abuseConfidenceScore']} (High risk if > 50)\n"
print(f"Country: {ip_info['countryName']} ({ip_info['countryCode']})")
ip_reputation_string += f"Country: {ip_info['countryName']} ({ip_info['countryCode']})\n"
print(f"ISP: {ip_info.get('isp', 'Unknown')} ({ip_info.get('domain', 'No domain')})")
ip_reputation_string += f"ISP: {ip_info.get('isp', 'Unknown')} ({ip_info.get('domain', 'No domain')})\n"
print(f"Usage Type: {ip_info.get('usageType', 'Unknown')}")
ip_reputation_string += f"Usage Type: {ip_info.get('usageType', 'Unknown')}\n"
print(f"Total Abuse Reports: {ip_info['totalReports']} from {ip_info['numDistinctUsers']} distinct users")
ip_reputation_string += f"Total Reports: {ip_info['totalReports']} from {ip_info['numDistinctUsers']} distinct users\n"
# Check if the IP is whitelisted
if ip_info["isWhitelisted"]:
print("This IP is whitelisted by the Abuse IPDB.")
ip_reputation_string += "This IP is whitelisted by the Abuse IPDB.\n"
else:
print("This IP is NOT whitelisted by the Abuse IPDB.")
ip_reputation_string += "This IP is NOT whitelisted by the Abuse IPDB.\n"
# Tor Detection
if ip_info["isTor"]:
print("Warning: This IP is associated with a Tor exit node.")
ip_reputation_string += "Warning: This IP is associated with a Tor exit node.\n"
# Last Report Information
last_reported = ip_info.get("lastReportedAt")
if last_reported:
print(f"Last Reported At: {last_reported}")
ip_reputation_string += f"Last Reported At: {last_reported}\n"
# Detailed Report Comments
if "reports" in ip_info and ip_info["reports"]:
print("\nRecent Reports:")
ip_reputation_string += "\nRecent Reports:\n"
for report in ip_info["reports"]:
print(f" - Reported At: {report['reportedAt']}")
ip_reputation_string += f" - Reported At: {report['reportedAt']}\n"
print(f" Comment: {report['comment']}")
ip_reputation_string += f" Comment: {report['comment']}\n"
print(f" Categories: {report['categories']}")
ip_reputation_string += f" Categories: {report['categories']}\n"
print(f" Reporter Country: {report['reporterCountryName']} ({report['reporterCountryCode']})\n")
ip_reputation_string += f" Reporter Country: {report['reporterCountryName']} ({report['reporterCountryCode']})\n\n"
# print(f"Returning ip_reputation_string: {ip_reputation_string}") # Print the captured output
return ip_reputation_string
# Example usage
# if __name__ == "__main__":
#
# AbuseIPDB_API_KEY = subprocess.check_output(["/opt/homebrew/bin/op", "read", "op://Private/AbuseIPDB/AbuseIPDB_API_KEY"]).decode('utf-8').strip()
#
# home_ip = "10.10.10.10" # Replace with your home broadband IP
# # home_ip = check_external_ip.get_current_external_ip(silent=True)
# reputation_data = check_ip_reputation(home_ip, AbuseIPDB_API_KEY)
#
# if reputation_data:
# analyze_ip_reputation(reputation_data)