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

New analyzer to check URL categories #24

Merged
merged 1 commit into from
Nov 22, 2016
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
18 changes: 18 additions & 0 deletions analyzers/URLCategory/report/success_long.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="panel panel-info">
<div class="panel-heading">
URL Categories of <strong>{{artifact.data}}</strong>
</div>
<div class="panel-body">
<dl class="dl-horizontal" ng-if="content.fortinet_category">
<dt>Fortinet URL Category: </dt>
<dd class="wrap">{{content.fortinet_category}}&nbsp;
<i class="fa fa-search"></i>
<a ng-href="http://www.fortiguard.com/iprep?data={{artifact.data}}&lookup=Lookup" target="_blank">
View Full Report</a>
<i class="fa fa-recycle"></i>
<a ng-href="http://www.fortiguard.com/iprep_form?data={{artifact.data}}" target="_blank">
Request Recategorization</a>
</dd>
</dl>
</div>
</div>
4 changes: 4 additions & 0 deletions analyzers/URLCategory/report/success_short.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<span class="label label-info">
URLCat:
<span ng-if="content.fortinet_category">{{content.fortinet_category}}&nbsp;</span>
</span>
85 changes: 85 additions & 0 deletions analyzers/URLCategory/urlcategory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
import json
import codecs
import time
import re
import requests

if sys.stdout.encoding != 'UTF-8':
if sys.version_info.major == 3:
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
else:
sys.stdout = codecs.getwriter('utf-8')(sys.stdout, 'strict')
if sys.stderr.encoding != 'UTF-8':
if sys.version_info.major == 3:
sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')
else:
sys.stderr = codecs.getwriter('utf-8')(sys.stderr, 'strict')

# load artifact
artifact = json.load(sys.stdin)

def error(message):
print('{{"errorMessage":"{}"}}'.format(message))
sys.exit(1)

def get_param(name, default=None, message=None, current=artifact):
if isinstance(name, str):
name = name.split('.')
if len(name) == 0:
return current
else:
value = current.get(name[0])
if value == None:
if message != None:
error(message)
else:
return default
else:
return get_param(name[1:], default, message, value)

def debug(msg):
#print >> sys.stderr, msg
pass

def fortinet_category(data):
debug('>> fortinet_category ' + str(data))
pattern = re.compile("(?:Category: )([\w\s]+)")
baseurl = 'http://www.fortiguard.com/iprep?data='
tailurl = '&lookup=Lookup'
url = baseurl + data + tailurl
r = requests.get(url)
category_match = re.search(pattern, r.content, flags=0)
return category_match.group(1)

http_proxy = get_param('config.proxy.http')
https_proxy = get_param('config.proxy.https')
max_tlp = get_param('config.max_tlp', 1)
tlp = get_param('tlp', 2) # amber by default
data_type = get_param('dataType', None, 'Missing dataType field')
service = get_param('config.service', None, 'Service parameter is missing')

# run only if TLP condition is met
if tlp > max_tlp:
error('Error with TLP value ; see max_tlp in config or tlp value in input data')

# setup proxy
if http_proxy != None:
os.environ['http_proxy'] = http_proxy
if https_proxy != None:
os.environ['https_proxy'] = https_proxy

if service == 'query':
if data_type == 'url' or data_type == 'domain':
data = get_param('data', None, 'Data is missing')
json.dump({
'fortinet_category': fortinet_category(data)
}, sys.stdout, ensure_ascii=False)
else:
error('Invalid data type')
else:
error('Invalid service')

13 changes: 13 additions & 0 deletions analyzers/URLCategory_1.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "URLCategory",
"version": "1.0",
"report": "URLCategory/report",
"description": "URL Category query: checks the category of a specific URL or domain",
"dataTypeList": ["url", "domain"],
"baseConfig" : "URLCategory",
"config": {
"service": "query",
"max_tlp": 10
},
"command": "URLCategory/urlcategory.py"
}