53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
|
from base64 import decode
|
||
|
import json
|
||
|
import os
|
||
|
import requests
|
||
|
import requests_cache
|
||
|
from dotenv import load_dotenv
|
||
|
from pprint import pprint
|
||
|
|
||
|
|
||
|
class API_error(Exception):
|
||
|
pass
|
||
|
|
||
|
|
||
|
def environment():
|
||
|
requests_cache.install_cache(expire_after=360, allowable_methods=("POST"))
|
||
|
load_dotenv()
|
||
|
api_key = os.getenv("ABUSEIPDB_API")
|
||
|
return api_key
|
||
|
|
||
|
|
||
|
def lookup(api_key, host):
|
||
|
url = "https://api.abuseipdb.com/api/v2/check"
|
||
|
payload = {"ipAddress": "", "maxAgeInDays": "90"}
|
||
|
payload.update({"ipAddress": host})
|
||
|
headers = {"Accept": "application/json", "Key": api_key}
|
||
|
response = requests.request(
|
||
|
method="GET", url=url, params=payload, headers=headers, verify=False
|
||
|
) # TODO: remove SSL verify=False and add signed certificate if possible.
|
||
|
# Figure out how caching functions here: https://requests-cache.readthedocs.io/en/stable/examples.html
|
||
|
response_dict = json.loads(response.text)
|
||
|
lookup = dict.fromkeys(
|
||
|
["score", "last_reported", "IP_address", "CDN", "Tor", "total_reports"]
|
||
|
)
|
||
|
print(response_dict)
|
||
|
lookup["score"] = response_dict["data"]["abuseConfidenceScore"]
|
||
|
lookup["last_reported"] = response_dict["data"]["lastReportedAt"]
|
||
|
lookup["IP_address"] = response_dict["data"]["ipAddress"]
|
||
|
lookup["usage"] = response_dict["data"]["usageType"]
|
||
|
lookup["Tor"] = response_dict["data"]["isTor"]
|
||
|
lookup["total_reports"] = response_dict["data"]["totalReports"]
|
||
|
|
||
|
print(requests_cache.get_cache())
|
||
|
print("Cached:")
|
||
|
print("\n".join(requests_cache.get_cache().urls()))
|
||
|
|
||
|
return lookup
|
||
|
|
||
|
|
||
|
def analyse(host):
|
||
|
api_key = environment()
|
||
|
result = lookup(api_key, host)
|
||
|
return result
|