2018-10-04 11:15:15 +00:00
|
|
|
import re, socket
|
2018-10-04 09:18:44 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
|
|
|
URL_GEOIP = "http://ip-api.com/json/%s"
|
2018-11-15 15:41:55 +00:00
|
|
|
REGEX_IPv6 = r"(?:(?:[a-f0-9]{1,4}:){2,}|[a-f0-9:]*::)[a-f0-9:]*"
|
|
|
|
REGEX_IPv4 = r"(?:\d{1,3}\.){3}\d{1,3}"
|
|
|
|
REGEX_IP = re.compile("%s|%s" % (REGEX_IPv4, REGEX_IPv6), re.I)
|
2018-10-04 09:18:44 +00:00
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
@utils.hook("received.command.dns", min_args=1)
|
|
|
|
def dns(self, event):
|
|
|
|
"""
|
|
|
|
:help: Get all addresses for a given hostname (IPv4/IPv6)
|
|
|
|
:usage: <hostname>
|
2018-10-04 10:04:24 +00:00
|
|
|
:prefix: DNS
|
2018-10-04 09:18:44 +00:00
|
|
|
"""
|
|
|
|
hostname = event["args_split"][0]
|
|
|
|
try:
|
|
|
|
address_info = socket.getaddrinfo(hostname, 1, 0,
|
|
|
|
socket.SOCK_DGRAM)
|
|
|
|
except socket.gaierror:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("Failed to find hostname")
|
|
|
|
|
2018-10-04 09:18:44 +00:00
|
|
|
ips = []
|
|
|
|
for _, _, _, _, address in address_info:
|
|
|
|
ips.append(address[0])
|
|
|
|
event["stdout"].write("%s: %s" % (hostname, ", ".join(ips)))
|
|
|
|
|
|
|
|
|
|
|
|
@utils.hook("received.command.geoip", min_args=1)
|
|
|
|
def geoip(self, event):
|
|
|
|
"""
|
|
|
|
:help: Get geoip data on a given IPv4/IPv6 address
|
|
|
|
:usage: <IP>
|
2018-10-04 10:04:24 +00:00
|
|
|
:prefix: GeoIP
|
2018-10-04 09:18:44 +00:00
|
|
|
"""
|
|
|
|
page = utils.http.get_url(URL_GEOIP % event["args_split"][0],
|
|
|
|
json=True)
|
|
|
|
if page:
|
|
|
|
if page["status"] == "success":
|
|
|
|
data = page["query"]
|
|
|
|
data += " | Organisation: %s" % page["org"]
|
|
|
|
data += " | City: %s" % page["city"]
|
|
|
|
data += " | Region: %s (%s)" % (page["regionName"],
|
|
|
|
page["countryCode"])
|
|
|
|
data += " | ISP: %s" % page["isp"]
|
|
|
|
data += " | Lon/Lat: %s/%s" % (page["lon"],
|
|
|
|
page["lat"])
|
|
|
|
data += " | Timezone: %s" % page["timezone"]
|
|
|
|
event["stdout"].write(data)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No geoip data found")
|
|
|
|
else:
|
2018-10-20 19:51:29 +00:00
|
|
|
raise utils.EventsResultsError()
|
2018-10-04 09:18:44 +00:00
|
|
|
|
2018-10-04 11:15:15 +00:00
|
|
|
@utils.hook("received.command.rdns")
|
2018-10-04 09:18:44 +00:00
|
|
|
def rdns(self, event):
|
|
|
|
"""
|
|
|
|
:help: Do a reverse-DNS look up on an IPv4/IPv6 address
|
|
|
|
:usage: <IP>
|
2018-10-04 10:04:24 +00:00
|
|
|
:prefix: rDNS
|
2018-10-04 09:18:44 +00:00
|
|
|
"""
|
2018-10-04 11:15:15 +00:00
|
|
|
ip = event["args_split"][0] if event["args"] else ""
|
|
|
|
if not ip:
|
|
|
|
line = event["target"].buffer.find(REGEX_IP)
|
|
|
|
if line:
|
|
|
|
match = REGEX_IP.search(line.message)
|
|
|
|
ip = match.group(1) or match.group(2)
|
|
|
|
if not ip:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("No IP provided")
|
2018-10-04 11:15:15 +00:00
|
|
|
|
2018-10-04 09:18:44 +00:00
|
|
|
try:
|
2018-10-04 11:15:15 +00:00
|
|
|
hostname, alias, ips = socket.gethostbyaddr(ip)
|
2018-10-04 09:29:22 +00:00
|
|
|
except (socket.herror, socket.gaierror) as e:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError(e.strerror)
|
2018-10-04 12:19:59 +00:00
|
|
|
event["stdout"].write("(%s) %s" % (ips[0], hostname))
|