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

updated Maxmind analyzer #1

Merged
merged 1 commit into from
Jan 9, 2020
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ Main features:
* GoogleSafeBrowsing
* AbuseIPDB
* AlienVault OTX
* MaxMind
#### needed access request
* CIRCL PassiveDNS + PassiveSSL
#### without api key
* Fortiguard URL Analyzer
* MaxMind
* GreyNoise Alpha API
* Talos Reputation
* Tor Project
Expand Down
24 changes: 17 additions & 7 deletions api_app/script_analyzers/observable_analyzers/maxmind.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from api_app.exceptions import AnalyzerRunException
from api_app.script_analyzers import general
from intel_owl import settings
from intel_owl import settings, secrets

logger = get_task_logger(__name__)

Expand All @@ -22,10 +22,9 @@ def run(analyzer_name, job_id, observable_name, observable_classification, addit
"".format(analyzer_name, job_id, observable_name))
report = general.get_basic_report_template(analyzer_name)
try:

try:
if not os.path.isfile(database_location):
updater()
updater(additional_config_params)
reader = maxminddb.open_database(database_location)
maxmind_result = reader.get(observable_name)
reader.close()
Expand Down Expand Up @@ -62,11 +61,19 @@ def run(analyzer_name, job_id, observable_name, observable_classification, addit
return report


def updater():
def updater(additional_config_params):

try:
api_key_name = additional_config_params.get('api_key_name', '')
if not api_key_name:
api_key_name = "MAXMIND_KEY"
api_key = secrets.get_secret(api_key_name)
if not api_key:
raise AnalyzerRunException("no api key retrieved")

logger.info("starting download of db from maxmind")
url = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz"
url = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key={}" \
"&suffix=tar.gz".format(api_key)
r = requests.get(url)
if r.status_code >= 300:
raise AnalyzerRunException("failed request for new maxmind db. Status code: {}".format(r.status_code))
Expand All @@ -82,6 +89,7 @@ def updater():
today = datetime.datetime.now().date()
counter = 0
directory_found = False
downloaded_db_path = ""
# this is because we do not know the exact date of the db we downloaded
while counter < 10 or not directory_found:
date_to_check = today - datetime.timedelta(days=counter)
Expand All @@ -96,14 +104,16 @@ def updater():
else:
directory_found = True

if not directory_found:
if directory_found:
logger.info("maxmind directory found {}".format(downloaded_db_path))
else:
raise AnalyzerRunException("failed extraction of maxmind db, reached max number of attempts")

logger.info("ended download of db from maxmind")

except Exception as e:
traceback.print_exc()
logger.exception(e)
logger.exception(str(e))

return database_location

2 changes: 1 addition & 1 deletion api_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_remove_old_jobs(self):
self.assertTrue(True)

def test_maxmind_updater(self):
db_file_path = maxmind.updater()
db_file_path = maxmind.updater({})
self.assertTrue(os.path.exists(db_file_path))

def test_talos_updater(self):
Expand Down
1 change: 1 addition & 0 deletions env_file_app_template
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ VT_KEY=
HA_KEY=
INTEZER_KEY=
FIRST_MISP_API=
MAXMIND_KEY=

# Test tokens
TEST_TOKEN=
Expand Down
2 changes: 1 addition & 1 deletion intel_owl/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
},
'handlers': {
'api_app': {
'level': 'INFO',
'level': 'DEBUG' if DEBUG else 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '{}/api_app.log'.format(DJANGO_LOG_DIRECTORY),
'formatter': 'stdfmt',
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jsbeautifier==1.6.2
jsonschema==3.1.1
kombu==4.6.5
lightgbm==2.1.2
maxminddb==1.4.1
maxminddb==1.5.2
more-itertools==7.2.0
msoffcrypto-tool==4.10.1
numpy==1.17.1
Expand Down