-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPAbuseChecker.py
executable file
·71 lines (62 loc) · 2.62 KB
/
IPAbuseChecker.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
import requests
import sys
import json
import configparser
import time
import re
import constant
from constant import Colors
AbuseIPDB_API_KEY = ""
def Call_API_Key():
config = configparser.ConfigParser()
try:
config.read('conf.cfg')
AbuseIPDB_API_KEY = config['API_KEY']['AbuseIPDB_APIKEY']
except:
print(Colors.REDBG+"[Error]: "+Colors.END+"Config file not found")
sys.exit(1)
#Check API key format
if AbuseIPDB_API_KEY == "" or not re.match(r"^[0-9a-fA-F]{80}$",AbuseIPDB_API_KEY):
print(Colors.REDBG+"[Error]: "+Colors.END+"API key format is not correct")
sys.exit(1)
else:
return AbuseIPDB_API_KEY
def API_CALL(ip,apikey):
headers = {
'Key': apikey,
'Accept': 'application/json',
}
parameters = {
'maxAgeInDays': constant.AbuseIPDB_Days,
'ipAddress': ip
}
api_request = requests.get('https://api.abuseipdb.com/api/v2/check', headers=headers, params=parameters)
response = api_request.json()
if response['data']['totalReports'] > 0:
if response['data']['abuseConfidenceScore'] >= 70:
print(Colors.REDBG + str(response['data']['ipAddress'])+ Colors.END+
" -> Abuse Confidence Score : "+str(response['data']['abuseConfidenceScore'])
+", Total Reports : "+ str(response['data']['totalReports'])+
", Country Code :"+str(response['data']['countryCode'])+"\n")
elif 40 <= response['data']['abuseConfidenceScore'] < 70:
print(Colors.ORANGEBG + str(response['data']['ipAddress'])+ Colors.END+
" -> Abuse Confidence Score : "+str(response['data']['abuseConfidenceScore'])
+", Total Reports : "+ str(response['data']['totalReports'])+
", Country Code :"+str(response['data']['countryCode'])+"\n")
else:
print(str(response['data']['ipAddress'])+
" -> Abuse Confidence Score : "+str(response['data']['abuseConfidenceScore'])
+", Total Reports : "+ str(response['data']['totalReports'])+
", Country Code :"+str(response['data']['countryCode'])+"\n")
def check_IP(files):
AbuseIPDB_API_KEY = Call_API_Key()
if files == "":
print(Colors.REDBG+"[Error]: "+Colors.END+"Provide an input file for process")
else:
try:
with open(files,'r', newline=None) as file:
for line in file:
API_CALL(line,AbuseIPDB_API_KEY)
except Exception as excp:
print(Colors.REDBG+"[Error]: "+Colors.END+"Provide an input file for process")
sys.exit(1)