-
Notifications
You must be signed in to change notification settings - Fork 299
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
DomainTools Expert Bot (initial version) #1004
Draft
aaronkaplan
wants to merge
5
commits into
certtools:develop
Choose a base branch
from
aaronkaplan:domaintools-expert
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4536da5
initial version of the domain tools expert
aaronkaplan 5c61f0a
add requirement for domaintools_api
aaronkaplan 5348b39
add BOTS config for domaintools
aaronkaplan 9e9fa96
ENH: required improvements on domaintools expert
SYNchroACK 3a400cf
Merge pull request #1 from SYNchroACK/domaintools-expert
aaronkaplan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Domaintools expert | ||
|
||
This expert bot is an example on how to query domaintools. | ||
|
||
It does require an API from domaintools. | ||
|
||
Documentation on domaintools: https://www.domaintools.com/resources/api-documentation/ | ||
Specifically, this bot can query a domain for the reputation score in domaintools: https://www.domaintools.com/resources/api-documentation/reputation/ | ||
|
||
It will add the score in the extra field: ``extra.domaintools_score``. | ||
|
||
|
||
Authors: Juan Ortega Valiente, Aaron Kaplan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
domaintools_api==0.1.7 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
domaintools expert: query domtaintools.com to get a reputation score for a domain name | ||
|
||
""" | ||
from intelmq.lib.bot import Bot | ||
try: | ||
from domaintools import API, exceptions | ||
except ImportError: | ||
API = None | ||
|
||
|
||
class DomaintoolsExpertBot(Bot): | ||
|
||
def init(self): | ||
self.logger.info("Loading Domaintools expert.") | ||
|
||
if not API: | ||
raise ValueError("need to have the domaintools API installed. See https://github.com/domaintools/python_api.") | ||
|
||
if not self.parameters.user: | ||
raise ValueError("need to specify user for domaintools expert in runtime.conf.") | ||
|
||
if not self.parameters.password: | ||
raise ValueError("need to specify password for the user for domaintools expert in runtime.conf.") | ||
|
||
self.api = API(self.parameters.user, self.parameters.password) | ||
|
||
if not self.valid_credentials(): | ||
raise ValueError("invalid credentials found in runtime.conf.") | ||
|
||
def valid_credentials(self): | ||
resp = self.api.reputation(fqdn, include_reasons=False) | ||
try: | ||
resp['risk_score'] | ||
return True | ||
except exceptions.NotAuthorizedException: | ||
return False | ||
|
||
def get_score(self, fqdn): | ||
# don't include a reason in the JSON response | ||
resp = self.api.reputation(fqdn, include_reasons=False) | ||
|
||
try: | ||
score = resp['risk_score'] | ||
except exceptions.NotFoundException: | ||
score = None | ||
except exceptions.BadRequestException: | ||
raise | ||
|
||
return score | ||
|
||
def process(self): | ||
event = self.receive_message() | ||
extra = {} | ||
|
||
for key in ["source.", "destination."]: | ||
key_fqdn = key + "fqdn" | ||
|
||
if key_fqdn not in event: | ||
continue | ||
|
||
score = self.get_score(event.get(key_fqdn)) | ||
|
||
if score: | ||
extra["domaintools_score_" + key_fqdn] = score | ||
|
||
extra.update(event.get("extra")) | ||
event.update("extra", extra) | ||
|
||
self.send_message(event) | ||
self.acknowledge_message() | ||
|
||
|
||
BOT = DomaintoolsExpertBot |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Testing GethostbynameExpertBot. | ||
""" | ||
|
||
import unittest | ||
|
||
import intelmq.lib.test as test | ||
from intelmq.bots.experts.domaintools.expert import DomaintoolsExpertBot | ||
|
||
EXAMPLE_INPUT = {"__type": "Event", | ||
"source.fqdn": "google.com", | ||
"time.observation": "2015-01-01T00:00:00+00:00" | ||
} | ||
EXAMPLE_OUTPUT = {"__type": "Event", | ||
"source.fqdn": "google.com", | ||
"extra": '{"domaintools_score": 0}', | ||
"time.observation": "2015-01-01T00:00:00+00:00" | ||
} | ||
NONEXISTING_INPUT = {"__type": "Event", | ||
"source.fqdn": "example.invalid", | ||
"destination.fqdn": "example.invalid", | ||
"time.observation": "2015-01-01T00:00:00+00:00" | ||
} | ||
|
||
|
||
@test.skip_internet() | ||
class TestDomaintoolsExpertBot(test.BotTestCase, unittest.TestCase): | ||
""" | ||
A TestCase for DomaintoolsExpertBot. | ||
""" | ||
|
||
@classmethod | ||
def set_bot(self): | ||
self.bot_reference = DomaintoolsExpertBot | ||
self.sysconfig = {'user': 'example01', 'password': 'mysecret'} | ||
|
||
def test_existing(self): | ||
self.input_message = EXAMPLE_INPUT | ||
self.run_bot() | ||
self.assertMessageEqual(0, EXAMPLE_OUTPUT) | ||
|
||
def test_non_existing(self): | ||
self.input_message = NONEXISTING_INPUT | ||
self.run_bot() | ||
self.assertMessageEqual(0, NONEXISTING_INPUT) | ||
|
||
if __name__ == '__main__': # pragma: no cover | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually the lib raises the
BadRequestException
for this test which is not equivalent to 'not existing', but the result is similar.