2018-05-04 11:53:29 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-04-26 12:42:39 +00:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
"""
|
|
|
|
The ApiKey Module
|
|
|
|
======================
|
|
|
|
|
|
|
|
This module is consuming the Redis-list created by the Categ module.
|
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
Search for API keys on an item content.
|
2018-04-26 12:42:39 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-06-02 12:42:23 +00:00
|
|
|
import os
|
2022-10-25 14:25:19 +00:00
|
|
|
import re
|
2021-06-02 12:42:23 +00:00
|
|
|
import sys
|
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
2021-06-02 12:42:23 +00:00
|
|
|
from modules.abstract_module import AbstractModule
|
2022-10-25 14:25:19 +00:00
|
|
|
from lib.objects.Items import Item
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
class ApiKey(AbstractModule):
|
|
|
|
"""ApiKey module for AIL framework"""
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
def __init__(self):
|
|
|
|
super(ApiKey, self).__init__()
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
# # TODO: ENUM or dict
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
# TODO improve REGEX
|
2022-10-25 14:25:19 +00:00
|
|
|
# r'(?<![A-Z0-9])=[A-Z0-9]{20}(?![A-Z0-9])'
|
|
|
|
# r'(?<!=[A-Za-z0-9+])=[A-Za-z0-9+]{40}(?![A-Za-z0-9+])'
|
2021-05-19 12:54:34 +00:00
|
|
|
self.re_aws_access_key = r'AKIA[0-9A-Z]{16}'
|
|
|
|
self.re_aws_secret_key = r'[0-9a-zA-Z/+]{40}'
|
|
|
|
re.compile(self.re_aws_access_key)
|
|
|
|
re.compile(self.re_aws_secret_key)
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
# r'=AIza[0-9a-zA-Z-_]{35}' keep equal ????
|
|
|
|
self.re_google_api_key = r'AIza[0-9a-zA-Z-_]{35}'
|
|
|
|
re.compile(self.re_google_api_key)
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
# Send module state to logs
|
|
|
|
self.redis_logger.info(f"Module {self.module_name} initialized")
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 14:57:20 +00:00
|
|
|
def compute(self, message, r_result=False):
|
2022-10-25 14:25:19 +00:00
|
|
|
item_id, score = message.split()
|
|
|
|
item = Item(item_id)
|
2021-05-19 12:54:34 +00:00
|
|
|
item_content = item.get_content()
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
google_api_key = self.regex_findall(self.re_google_api_key, item.get_id(), item_content)
|
|
|
|
aws_access_key = self.regex_findall(self.re_aws_access_key, item.get_id(), item_content)
|
2021-05-19 12:54:34 +00:00
|
|
|
if aws_access_key:
|
2022-10-25 14:25:19 +00:00
|
|
|
aws_secret_key = self.regex_findall(self.re_aws_secret_key, item.get_id(), item_content)
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
if aws_access_key or google_api_key:
|
|
|
|
to_print = f'ApiKey;{item.get_source()};{item.get_date()};{item.get_basename()};'
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
if google_api_key:
|
|
|
|
print(f'found google api key: {to_print}')
|
|
|
|
self.redis_logger.warning(f'{to_print}Checked {len(google_api_key)} found Google API Key;{item.get_id()}')
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
msg = f'infoleak:automatic-detection="google-api-key";{item.get_id()}'
|
2021-05-27 15:28:20 +00:00
|
|
|
self.send_message_to_queue(msg, 'Tags')
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
# # TODO: # FIXME: AWS regex/validate/sanitize KEY + SECRET KEY
|
2021-05-19 12:54:34 +00:00
|
|
|
if aws_access_key:
|
|
|
|
print(f'found AWS key: {to_print}')
|
|
|
|
self.redis_logger.warning(f'{to_print}Checked {len(aws_access_key)} found AWS Key;{item.get_id()}')
|
|
|
|
if aws_secret_key:
|
|
|
|
print(f'found AWS secret key')
|
|
|
|
self.redis_logger.warning(f'{to_print}Checked {len(aws_secret_key)} found AWS secret Key;{item.get_id()}')
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
msg = 'infoleak:automatic-detection="aws-key";{}'.format(item.get_id())
|
2021-05-27 15:28:20 +00:00
|
|
|
self.send_message_to_queue(msg, 'Tags')
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
# Tags
|
|
|
|
msg = f'infoleak:automatic-detection="api-key";{item.get_id()}'
|
2021-05-27 15:28:20 +00:00
|
|
|
self.send_message_to_queue(msg, 'Tags')
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
# Send to duplicate
|
2021-05-27 15:28:20 +00:00
|
|
|
self.send_message_to_queue(item.get_id(), 'Duplicate')
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 14:57:20 +00:00
|
|
|
if r_result:
|
2022-10-25 14:25:19 +00:00
|
|
|
return google_api_key, aws_access_key, aws_secret_key
|
|
|
|
|
2018-04-26 12:42:39 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
module = ApiKey()
|
|
|
|
module.run()
|