Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Analyzers Shodan #125

Merged
merged 3 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions analyzers/Shodan/Shodan_Host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Shodan_Host",
"version": "1.0",
"author": "Sebastien Larinier @Sebdraven",
"url": "https://github.com/CERT-BDF/Cortex-Analyzers/Shodan",
"license": "AGPL-V3",
"description": "",
"dataTypeList": ["ip"],
"baseConfig": "Shodan",
"config": {
"check_tlp": true,
"service": "host",
"max_tlp": 1
},
"command": "Shodan/shodan_analyzer.py"
}
16 changes: 16 additions & 0 deletions analyzers/Shodan/Shodan_Search.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Shodan_Search",
"version": "1.0",
"author": "Sebastien Larinier @Sebdraven",
"url": "https://github.com/CERT-BDF/Cortex-Analyzers/Shodan",
"license": "AGPL-V3",
"description": "",
"dataTypeList": ["domain"],
"baseConfig": "Shodan",
"config": {
"check_tlp": true,
"service": "search",
"max_tlp": 1
},
"command": "Shodan/shodan_analyzer.py"
}
51 changes: 51 additions & 0 deletions analyzers/Shodan/shodan_analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
# encoding: utf-8
from cortexutils.analyzer import Analyzer
from shodan_api import ShodanAPIPublic


class ShodanAnalyzer(Analyzer):
def __init__(self):
Analyzer.__init__(self)
self.service = self.getParam('config.service', None, 'Service parameter is missing')
self.shodan_key = self.getParam('config.key', None, 'Missing Shodan API key')
self.shodan_client = None
self.polling_interval = self.getParam('config.polling_interval', 60)

def summary(self, raw):

taxonomy = {"level": "info", "namespace": "Shodan", "predicate": "Location", "value": 0}
taxonomies = []
level = "info"
namespace = "Shodan"
predicate = "Location"
if self.service == 'host':
if 'country_name' in raw['host']:
value = raw['host']['country_name']
taxonomies.append(self.build_taxonomy(level, namespace, predicate, value))
if 'org' in raw['host']:
taxonomies.append(self.build_taxonomy(level, namespace, 'org', raw['host']['org']))
if 'asn' in raw['host']:
taxonomies.append(self.build_taxonomy(level, namespace, 'asn', raw['host']['asn']))
elif self.service == 'search':
taxonomies.append(self.build_taxonomy(level,namespace, 'search', 'OSINT'))
return {'taxonomies': taxonomies}

def run(self):
Analyzer.run(self)

self.shodan_client = ShodanAPIPublic(self.shodan_key)
if self.service == 'host':
ip = self.getParam('data', None, 'Data is missing')
results = {'reverse_dns': {'hostnames': self.shodan_client.reverse_dns(ip)[ip]},
'host': self.shodan_client.host(ip)}
self.report(results)
if self.service == 'search':
domain = self.getParam('data', None, 'Data is missing')
result = {'dns_resolve': self.shodan_client.dns_resolve(domain),
'infos_domain': self.shodan_client.info_domains(domain)}
self.report(result)


if __name__ == '__main__':
ShodanAnalyzer().run()
46 changes: 46 additions & 0 deletions analyzers/Shodan/shodan_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from shodan import Shodan
from urllib.parse import urljoin

import requests


class ShodanAPIPublic(Shodan):
def __init__(self, api_key):
Shodan.__init__(self, api_key)

def host(self, ips, history=False, minify=False):

host = Shodan.host(self, ips)
if host:
return host

def info_domains(self, domain):
results = Shodan.search(self, 'hostname:%s' % domain)

if 'matches' in results:
all_domains = list(set([item for r in results['matches'] for item in r['hostnames']]))
ips = list(set([r['ip_str'] for r in results['matches']]))
ports = list(set([r['port'] for r in results['matches']]))
transports = list(set([r['transport'] for r in results['matches']]))
isp = list(set([r['isp'] for r in results['matches'] if 'isp' in r]))
asn = list(set([r['asn'] for r in results['matches'] if 'asn' in r]))
orgs = list(set([r['org'] for r in results['matches'] if 'org' in r]))

return {'all_domains': all_domains, 'ips': ips, 'ports': ports, 'transports': transports,
'isp': isp, 'asn': asn, 'orgs': orgs
}

def dns_resolve(self, domain):
payload = {'hostnames': [domain], 'key': self.api_key}
r = requests.get(urljoin(self.base_url, 'dns/resolve'), params=payload)
if r.status_code == requests.codes.ok:
return r.json()

def reverse_dns(self, ip):
payload = {'ips': [ip], 'key': self.api_key}
r = requests.get(urljoin(self.base_url, 'dns/reverse'), params=payload)
if r.status_code == requests.codes.ok:
result = r.json()
if not result[ip]:
result[ip] = []
return result
29 changes: 29 additions & 0 deletions thehive-templates/Shodan_Host_1_0/long.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="panel panel-info" ng-if="success">
<div class="panel-heading">
Host Information of <strong>{{(artifact.data || artifact.attachment.name) | fang}}</strong>
</div>
<div class="panel-body">
asn: {{content.asn}}
<br>
isp: {{content.isp}}
<br>
hostnames: {{content.hostnames.join(',')}}
<br>
Country: {{content.country_code}}
<br>
ports: {{content.ports.join(',')}}
<br>
last_update: {{content.last_update}}
<br>
<a ng-href="http://google.com/maps/place/{{content.latitude}},{{content.longitude}}",target="_blank">Geolocalisation</a>
</div>
</div>

<div class="panel panel-danger" ng-if="!success">
<div class="panel-heading">
<strong>{{(artifact.data || artifact.attachment.name) | fang}}</strong>
</div>
<div class="panel-body">
{{content.errorMessage}}
</div>
</div>
3 changes: 3 additions & 0 deletions thehive-templates/Shodan_Host_1_0/short.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span class="label" ng-repeat="t in content.taxonomies" ng-class="{'info': 'label-info', 'safe': 'label-success', 'suspicious': 'label-warning', 'malicious':'label-danger'}[t.level]">
{{t.namespace}}:{{t.predicate}}={{t.value}}
</span>
52 changes: 52 additions & 0 deletions thehive-templates/Shodan_Search_1_0/long.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<div class="panel panel-info" ng-if="success">
<div class="panel-heading">
Domain Information of <strong>{{(artifact.data || artifact.attachment.name) | fang}}</strong>
</div>
<div class="panel-body">
asn:
<li data-ng-repeat="i in content.infos_domain.asn">
{{i}}
</li>
<br>
isp:
<li data-ng-repeat=" i in content.infos_domain.isp">
{{i}}
</li>
<br>
all_domains:
<li data-ng-repeat="i in content.infos_domain.all_domains">
{{i}}
</li>
<br>
ports:
<li data-ng-repeat="i in content.infos_domain.ports">
{{i}}
</li>
<br>
orgs:
<li data-ng-repeat="i in content.infos_domain.orgs">
{{i}}
</li>
<br>
transports:
<li data-ng-repeat="i in content.infos_domain.transports">
{{i}}
</li>
<br>
ips:
<li data-ng-repeat="i in content.infos_domain.ips">
{{i}}
</li>
<br>

</div>
</div>

<div class="panel panel-danger" ng-if="!success">
<div class="panel-heading">
<strong>{{(artifact.data || artifact.attachment.name) | fang}}</strong>
</div>
<div class="panel-body">
{{content.errorMessage}}
</div>
</div>
3 changes: 3 additions & 0 deletions thehive-templates/Shodan_Search_1_0/short.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span class="label" ng-repeat="t in content.taxonomies" ng-class="{'info': 'label-info', 'safe': 'label-success', 'suspicious': 'label-warning', 'malicious':'label-danger'}[t.level]">
{{t.namespace}}:{{t.predicate}}={{t.value}}
</span>