2021-05-17 16:03:30 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
|
2021-05-27 15:28:20 +00:00
|
|
|
import gzip
|
|
|
|
from base64 import b64encode
|
2021-06-08 14:46:36 +00:00
|
|
|
from distutils.dir_util import copy_tree
|
2021-05-27 15:28:20 +00:00
|
|
|
|
2021-05-17 16:03:30 +00:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2024-02-27 09:15:40 +00:00
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from lib.ConfigLoader import ConfigLoader
|
2021-05-17 16:03:30 +00:00
|
|
|
# Modules Classes
|
2021-06-02 12:42:23 +00:00
|
|
|
from modules.ApiKey import ApiKey
|
|
|
|
from modules.Categ import Categ
|
|
|
|
from modules.CreditCards import CreditCards
|
|
|
|
from modules.DomClassifier import DomClassifier
|
|
|
|
from modules.Global import Global
|
|
|
|
from modules.Keys import Keys
|
|
|
|
from modules.Onion import Onion
|
2022-01-19 15:20:18 +00:00
|
|
|
from modules.Telegram import Telegram
|
2021-05-17 16:03:30 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
# project packages
|
2022-11-28 14:01:40 +00:00
|
|
|
import lib.objects.Items as Items
|
2021-05-17 16:03:30 +00:00
|
|
|
|
2021-06-08 14:46:36 +00:00
|
|
|
#### COPY SAMPLES ####
|
|
|
|
config_loader = ConfigLoader()
|
2024-02-27 09:15:40 +00:00
|
|
|
ITEMS_FOLDER = Items.ITEMS_FOLDER
|
2021-06-08 14:46:36 +00:00
|
|
|
TESTS_ITEMS_FOLDER = os.path.join(ITEMS_FOLDER, 'tests')
|
|
|
|
sample_dir = os.path.join(os.environ['AIL_HOME'], 'samples')
|
|
|
|
copy_tree(sample_dir, TESTS_ITEMS_FOLDER)
|
2024-02-27 10:03:11 +00:00
|
|
|
|
|
|
|
|
2021-06-08 14:46:36 +00:00
|
|
|
#### ---- ####
|
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
class TestModuleApiKey(unittest.TestCase):
|
2021-05-19 12:54:34 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module = ApiKey()
|
|
|
|
self.module.debug = True
|
2021-05-19 12:54:34 +00:00
|
|
|
|
|
|
|
def test_module(self):
|
|
|
|
item_id = 'tests/2021/01/01/api_keys.gz'
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module.obj = Items.Item(item_id)
|
2021-05-19 12:54:34 +00:00
|
|
|
google_api_key = 'AIza00000000000000000000000_example-KEY'
|
|
|
|
aws_access_key = 'AKIAIOSFODNN7EXAMPLE'
|
|
|
|
aws_secret_key = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
|
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
matches = self.module.compute('3', r_result=True)
|
2023-05-23 11:38:33 +00:00
|
|
|
self.assertCountEqual(matches[0], {google_api_key})
|
|
|
|
self.assertCountEqual(matches[1], {aws_access_key})
|
|
|
|
self.assertCountEqual(matches[2], {aws_secret_key})
|
2021-05-19 12:54:34 +00:00
|
|
|
|
2024-02-27 10:03:11 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
class TestModuleCateg(unittest.TestCase):
|
2021-05-19 14:57:20 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module = Categ()
|
|
|
|
self.module.debug = True
|
2021-05-19 14:57:20 +00:00
|
|
|
|
|
|
|
def test_module(self):
|
|
|
|
item_id = 'tests/2021/01/01/categ.gz'
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module.obj = Items.Item(item_id)
|
2022-05-30 07:03:27 +00:00
|
|
|
test_categ = ['CreditCards', 'Mail', 'Onion', 'Urls', 'Credential', 'Cve']
|
2021-05-19 14:57:20 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
result = self.module.compute(None, r_result=True)
|
2021-05-19 14:57:20 +00:00
|
|
|
self.assertCountEqual(result, test_categ)
|
|
|
|
|
2024-02-27 10:03:11 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
class TestModuleCreditCards(unittest.TestCase):
|
2021-05-27 15:28:20 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module = CreditCards()
|
|
|
|
self.module.debug = True
|
2021-05-27 15:28:20 +00:00
|
|
|
|
|
|
|
def test_module(self):
|
2024-02-27 09:15:40 +00:00
|
|
|
item_id = 'tests/2021/01/01/credit_cards.gz'
|
|
|
|
self.module.obj = Items.Item(item_id)
|
2022-11-28 14:01:40 +00:00
|
|
|
test_cards = ['341039324930797', # American Express
|
|
|
|
'6011613905509166', # Discover Card
|
|
|
|
'3547151714018657', # Japan Credit Bureau (JCB)
|
|
|
|
'5492981206527330', # 16 digits MasterCard
|
|
|
|
'4024007132849695', # '4532525919781' # 16-digit VISA, with separators
|
2024-02-27 10:03:11 +00:00
|
|
|
]
|
2021-05-27 15:28:20 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
result = self.module.compute('7', r_result=True)
|
2021-05-27 15:28:20 +00:00
|
|
|
self.assertCountEqual(result, test_cards)
|
|
|
|
|
2024-02-27 10:03:11 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
class TestModuleDomClassifier(unittest.TestCase):
|
2021-05-27 15:28:20 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module = DomClassifier()
|
|
|
|
self.module.debug = True
|
2021-05-27 15:28:20 +00:00
|
|
|
|
|
|
|
def test_module(self):
|
2022-05-30 07:03:27 +00:00
|
|
|
test_host = 'foo.be'
|
2021-05-27 15:28:20 +00:00
|
|
|
item_id = 'tests/2021/01/01/domain_classifier.gz'
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module.obj = Items.Item(item_id)
|
|
|
|
result = self.module.compute(f'{test_host}', r_result=True)
|
2021-05-27 15:28:20 +00:00
|
|
|
self.assertTrue(len(result))
|
|
|
|
|
2024-02-27 10:03:11 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
class TestModuleGlobal(unittest.TestCase):
|
2021-05-27 15:28:20 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module = Global()
|
|
|
|
self.module.debug = True
|
2021-05-27 15:28:20 +00:00
|
|
|
|
|
|
|
def test_module(self):
|
|
|
|
# # TODO: delete item
|
|
|
|
item_id = 'tests/2021/01/01/global.gz'
|
2022-11-28 14:01:40 +00:00
|
|
|
item = Items.Item(item_id)
|
2021-05-27 15:28:20 +00:00
|
|
|
item.delete()
|
|
|
|
|
|
|
|
item_content = b'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
|
|
|
|
item_content_1 = b64encode(gzip.compress(item_content)).decode()
|
2021-06-08 14:46:36 +00:00
|
|
|
item_content_2 = b64encode(gzip.compress(item_content + b' more text ...')).decode()
|
2021-05-27 15:28:20 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module.obj = Items.Item(item_id)
|
2021-05-27 15:28:20 +00:00
|
|
|
# Test new item
|
2024-02-27 09:15:40 +00:00
|
|
|
result = self.module.compute(item_content_1, r_result=True)
|
2021-05-27 15:28:20 +00:00
|
|
|
self.assertEqual(result, item_id)
|
|
|
|
|
|
|
|
# Test duplicate
|
2024-02-27 09:15:40 +00:00
|
|
|
result = self.module.compute(item_content_1, r_result=True)
|
2021-05-27 15:28:20 +00:00
|
|
|
self.assertIsNone(result)
|
|
|
|
|
|
|
|
# Test same id with != content
|
2022-11-28 14:01:40 +00:00
|
|
|
item = Items.Item('tests/2021/01/01/global_831875da824fc86ab5cc0e835755b520.gz')
|
2021-06-08 14:46:36 +00:00
|
|
|
item.delete()
|
2024-02-27 09:15:40 +00:00
|
|
|
result = self.module.compute(item_content_2, r_result=True)
|
2021-05-28 15:23:51 +00:00
|
|
|
self.assertIn(item_id[:-3], result)
|
2021-05-27 15:28:20 +00:00
|
|
|
self.assertNotEqual(result, item_id)
|
|
|
|
|
|
|
|
# cleanup
|
2022-11-28 14:01:40 +00:00
|
|
|
# item = Items.Item(result)
|
2021-06-08 14:46:36 +00:00
|
|
|
# item.delete()
|
2021-05-27 15:28:20 +00:00
|
|
|
# # TODO: remove from queue
|
|
|
|
|
2024-02-27 10:03:11 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
class TestModuleKeys(unittest.TestCase):
|
2021-05-28 15:23:51 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module = Keys()
|
|
|
|
self.module.debug = True
|
2021-05-28 15:23:51 +00:00
|
|
|
|
|
|
|
def test_module(self):
|
|
|
|
item_id = 'tests/2021/01/01/keys.gz'
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module.obj = Items.Item(item_id)
|
2021-05-28 15:23:51 +00:00
|
|
|
# # TODO: check results
|
2024-02-27 10:03:11 +00:00
|
|
|
self.module.compute(None)
|
|
|
|
|
2021-05-28 15:23:51 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
class TestModuleOnion(unittest.TestCase):
|
2021-05-17 16:03:30 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module = Onion()
|
|
|
|
self.module.debug = True
|
2021-05-17 16:03:30 +00:00
|
|
|
|
|
|
|
def test_module(self):
|
|
|
|
item_id = 'tests/2021/01/01/onion.gz'
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module.obj = Items.Item(item_id)
|
2024-02-27 10:03:11 +00:00
|
|
|
# domain_1 = 'eswpccgr5xyovsahffkehgleqthrasfpfdblwbs4lstd345dwq5qumqd.onion'
|
|
|
|
# domain_2 = 'www.facebookcorewwwi.onion'
|
2021-05-17 16:03:30 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module.compute(f'3')
|
2021-05-19 12:54:34 +00:00
|
|
|
|
2024-02-27 10:03:11 +00:00
|
|
|
|
2024-02-27 09:15:40 +00:00
|
|
|
class TestModuleTelegram(unittest.TestCase):
|
2022-01-19 15:20:18 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module = Telegram()
|
|
|
|
self.module.debug = True
|
2022-01-19 15:20:18 +00:00
|
|
|
|
|
|
|
def test_module(self):
|
|
|
|
item_id = 'tests/2021/01/01/keys.gz'
|
2024-02-27 09:15:40 +00:00
|
|
|
self.module.obj = Items.Item(item_id)
|
2022-01-19 15:20:18 +00:00
|
|
|
# # TODO: check results
|
2024-02-27 10:03:11 +00:00
|
|
|
self.module.compute(None)
|
2022-01-19 15:20:18 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|