misp-modules/misp_modules/modules/expansion/dns.py

64 lines
1.8 KiB
Python
Raw Normal View History

2016-02-17 16:05:06 +01:00
import json
import dns.resolver
misperrors = {'error': 'Error'}
mispattributes = {'input': ['hostname', 'hostname|port', 'domain', 'domain|ip'],
'output': ['ip-src', 'ip-dst']}
moduleinfo = {'version': '0.2', 'author': 'Alexandre Dulaunoy',
'description': 'Simple DNS expansion service to resolve IP address from MISP attributes',
'module-type': ['expansion', 'hover']}
moduleconfig = ['nameserver']
2016-02-17 16:05:06 +01:00
2016-02-24 02:50:35 +01:00
2016-02-17 16:05:06 +01:00
def handler(q=False):
if q is False:
return False
request = json.loads(q)
if request.get('hostname'):
toquery = request['hostname']
elif request.get('domain'):
toquery = request['domain']
elif request.get('domain|ip'):
toquery = request['domain|ip'].split('|')[0]
elif request.get('hostname|port'):
toquery = request['hostname|port'].split('|')[0]
2016-02-17 16:05:06 +01:00
else:
return False
r = dns.resolver.Resolver()
2016-02-29 21:44:50 +01:00
r.timeout = 2
r.lifetime = 2
if request.get('config'):
if request['config'].get('nameserver'):
nameservers = []
nameservers.append(request['config'].get('nameserver'))
r.nameservers = nameservers
else:
r.nameservers = ['8.8.8.8']
2016-02-17 16:05:06 +01:00
try:
answer = r.query(toquery, 'A')
except dns.resolver.NXDOMAIN:
2016-02-29 21:44:50 +01:00
misperrors['error'] = "NXDOMAIN"
return misperrors
2016-02-17 16:05:06 +01:00
except dns.exception.Timeout:
2016-02-29 21:44:50 +01:00
misperrors['error'] = "Timeout"
return misperrors
2018-12-11 15:29:09 +01:00
except Exception:
2016-02-29 21:44:50 +01:00
misperrors['error'] = "DNS resolving error"
return misperrors
r = {'results': [{'types': mispattributes['output'],
'values':[str(answer[0])]}]}
2016-02-17 16:05:06 +01:00
return r
2016-02-24 02:50:35 +01:00
def introspection():
2016-02-24 00:53:15 +01:00
return mispattributes
2016-02-24 02:50:35 +01:00
2016-02-24 00:53:15 +01:00
def version():
moduleinfo['config'] = moduleconfig
2016-02-24 00:53:15 +01:00
return moduleinfo