forked from Te-k/harpoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobtex.py
84 lines (73 loc) · 2.62 KB
/
robtex.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
from datetime import datetime
import json
import requests
class RobtexError(Exception):
def __init__(self, message):
Exception.__init__(self, message)
self.message = message
class Robtex(object):
"""
Library to request Robtex API
https://www.robtex.com/api/
"""
def _request(self, path):
r = requests.get("https://freeapi.robtex.com" + path)
if r.status_code != 200:
raise RobtexError("Wrong HTTP status code %i" % r.status_code)
else:
data = r.json()
if "status" not in data:
raise RobtexError("Wrong message format")
else:
if data["status"] != "ok":
raise RobtexError("Wrong status code %s" % data["status"])
else:
return data
def _parse_pdns(self, data):
return [json.loads(a) for a in data.split()]
def get_ip_info(self, ip):
"""
Request info on an IPv4
"""
data = self._request("/ipquery/%s" % ip)
for d in ["pas", "pash", "act", "acth"]:
if d in data:
for dd in data[d]:
if "t" in dd:
dd["date"] = datetime.fromtimestamp(dd["t"])
return data
def get_asn_info(self, asn):
"""
Get ASN info
"""
return self._request("/asquery/%i" % asn)
def get_pdns_domain(self, domain):
"""
Get passive DNS info on a domain
"""
r = requests.get("https://freeapi.robtex.com/pdns/forward/%s" % domain)
if r.status_code != 200:
raise RobtexError("Wrong HTTP status code %i" % r.status_code)
else:
data = self._parse_pdns(r.text)
for d in data:
if "time_first" in d:
d["time_first_o"] = datetime.fromtimestamp(d["time_first"])
if "time_last" in d:
d["time_last_o"] = datetime.fromtimestamp(d["time_last"])
return data
def get_pdns_ip(self, ip):
"""
Get passive DNS info on an IP address
"""
r = requests.get("https://freeapi.robtex.com/pdns/reverse/%s" % ip)
if r.status_code != 200:
raise RobtexError("Wrong HTTP status code %i" % r.status_code)
else:
data = self._parse_pdns(r.text)
for d in data:
if "time_first" in d:
d["time_first_o"] = datetime.fromtimestamp(d["time_first"])
if "time_last" in d:
d["time_last_o"] = datetime.fromtimestamp(d["time_last"])
return data