-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGetipinfo.py
172 lines (145 loc) · 4.36 KB
/
Getipinfo.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
#!/usr/bin/python3
import sys
import os
print ('**************** start *********************')
measurement_name = (sys.argv[5]) # get measurement from argv
print ('Measurement-name: '+measurement_name)
# argv1 = outsideip, agrv2 = Domain, argv3 length, argv4 tragetip, sys.argv[5] bucketname, sys.argv[6] date, sys.argv[7] asn, sys.argv[8] abuse
abuseip_key = os.getenv('ABUSEIP_KEY')
if abuseip_key is not None:
import requests
import json
url = 'https://api.abuseipdb.com/api/v2/check'
querystring = {
'ipAddress': str(sys.argv[1]),
'maxAgeInDays': '90'
}
headers = {
'Accept': 'application/json',
'Key': abuseip_key
}
response = requests.request(method='GET', url=url, headers=headers, params=querystring)
abuseip_response = json.loads(response.text)
abuseConfidenceScore = str(abuseip_response["data"]["abuseConfidenceScore"])
totalReports = str(abuseip_response["data"]["totalReports"])
#print(json.dumps(abuseip_response, sort_keys=True, indent=4))
asn = str(sys.argv[7])
import geoip2.database
import socket
# IP gets infos from the DB
reader = geoip2.database.Reader('/geolite/GeoLite2-City.mmdb')
response = reader.city(str(sys.argv[1]))
Lat = response.location.latitude
ISO = response.country.iso_code
Long = response.location.longitude
State = response.subdivisions.most_specific.name
City = response.city.name
Country = response.country.name
Zip = response.postal.code
IP = str(sys.argv[1])
Domain = str(sys.argv[2])
duration = int(sys.argv[3])
Target = str(sys.argv[4])
reader.close()
if asn =='true':
reader = geoip2.database.Reader('/geolite/GeoLite2-ASN.mmdb')
response = reader.asn(str(sys.argv[1]))
Asn = response.autonomous_system_organization
reader.close()
# print to log
print (Country)
print (State)
print (City)
print (Zip)
print (Long)
print (Lat)
print (ISO)
if asn =='true':
print (Asn)
print ('Outside IP: ', IP)
print ('Target IP: ', Target)
print ('Domain: ', Domain)
if abuseip_key is not None:
print("abuseConfidenceScore: " + abuseConfidenceScore)
print("totalReports: " + totalReports)
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
# influx configuration - edit these
npmhome = "/root/.config/NPMGRAF"
ifhost = os.getenv('INFLUX_HOST')
ifbucket = os.getenv('INFLUX_BUCKET')
iforg = os.getenv('INFLUX_ORG')
iftoken = os.getenv('INFLUX_TOKEN')
# take a timestamp for this measurement
oldtime = str(sys.argv[6]) #30/May/2023:14:16:48 +0000 to 2009-11-10T23:00:00.123456Z
#transform month
month = oldtime[3:6]
if month == 'Jan':
month = '01'
elif month =='Feb':
month = '02'
elif month =='Mar':
month = '03'
elif month =='Apr':
month = '04'
elif month =='May':
month = '05'
elif month =='Jun':
month = '06'
elif month =='Jul':
month = '07'
elif month =='Aug':
month = '08'
elif month =='Sep':
month = '09'
elif month =='Oct':
month = '10'
elif month =='Nov':
month = '11'
else:
month = '12'
# build new time
time=oldtime[7:11]+'-'+month+'-'+oldtime[0:2]+'T'+oldtime[12:20]+oldtime[21:24]+':'+oldtime[24:26]
print('Measurement Time: ', time)
ifclient = influxdb_client.InfluxDBClient(
url=ifhost,
token=iftoken,
org=iforg
)
# write the measurement
write_api = ifclient.write_api(write_options=SYNCHRONOUS)
point = influxdb_client.Point(measurement_name)
point.tag("key", ISO)
point.tag("latitude", Lat)
point.tag("longitude", Long)
point.tag("Domain", Domain)
point.tag("City", City)
point.tag("State", State)
point.tag("Name", Country)
point.tag("IP", IP),
point.tag("Target", Target)
if asn =='true':
point.tag("Asn", Asn)
if abuseip_key is not None:
point.tag("abuseConfidenceScore", abuseConfidenceScore)
point.tag("totalReports", totalReports)
point.field("Domain", Domain)
point.field("latitude", Lat)
point.field("longitude", Long)
point.field("State", State)
point.field("City", City)
point.field("key", ISO)
point.field("IP", IP)
point.field("Target", Target)
if asn =='true':
point.field("Asn", Asn)
point.field("Name", Country)
point.field("duration", duration)
point.field("metric", 1)
if abuseip_key is not None:
point.field("abuseConfidenceScore", abuseConfidenceScore)
point.field("totalReports", totalReports)
point.time(time)
write_api.write(bucket=ifbucket, org=iforg, record=point)
ifclient.close()
print ('*************** data send ******************')