Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubermagal committed Jan 13, 2024
1 parent 30673d4 commit f6c5665
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
venv/
evilurl.egg-info/
dist/
.eggs/
.eggs/
__pycache__/
*.pyc
*.pyo
*.pyd
*.so
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='evilurl',
version='0.0.11',
version='0.0.12',
packages=['src'],
package_data={'src': ['unicode_combinations.json']},
setup_requires=['wheel'],
Expand Down
12 changes: 7 additions & 5 deletions src/evilurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ def analyze_domain(self, domain):

domains = []
for combination in product(*combinations):
new_domain = ''.join(combination) + '.' + domain_parts[1]
new_domain = ''.join(combination) + '.' + '.'.join(domain_parts[1:])
domains.append(new_domain)

if len(domains) <= 1:
print(f"IDN homograph attack is not possible for this domain")
return print(f"IDN homograph attack is not possible for this domain with the current character set")

if not self.show_domains_only:
print(header)
Expand All @@ -82,11 +82,13 @@ def analyze_domain(self, domain):
print(new_domain)
else:
for index, new_domain in enumerate(domains[1:]):
print(f"\n{index + 1} -------------------------------")

dns = self.check_domain_registration(new_domain)
punycode_encoded_domain = self.convert_to_punycode(new_domain)


if new_domain == punycode_encoded_domain:
continue

print(f"\n{index + 1} -------------------------------")
print(f"homograph domain: {new_domain}")
print(f"punycode: {punycode_encoded_domain}")
if dns:
Expand Down
Empty file added tests/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import unittest
from unittest.mock import patch

from src.evilurl import (HomographAnalyzer, load_unicode_combinations_from_file, main)


class TestHomographAnalyzer(unittest.TestCase):

def setUp(self):
unicode_combinations = [
{"latin": "a", "similar": ["а", "ɑ"]},
{"latin": "c", "similar": ["ϲ", "с", "ƈ"]},
{"latin": "e", "similar": ["е"]},
{"latin": "o", "similar": ["о", "ο", "o", "օ"]},
{"latin": "p", "similar": ["р"]},
{"latin": "s", "similar": ["ѕ"]},
{"latin": "i", "similar": ["і"]},
{"latin": "d", "similar": ["ԁ"]},
{"latin": "l", "similar": ["ɩ"]},
{"latin": "g", "similar": ["ɡ"]},
{"latin": "n", "similar": ["ո"]},
{"latin": "u", "similar": ["ս"]},
{"latin": "k", "similar": ["κ"]},
{"latin": "h", "similar": ["һ"]},
{"latin": "x", "similar": ["х"]},
{"latin": "y", "similar": ["у"]}
]
self.analyzer = HomographAnalyzer(unicode_combinations, show_domains_only=False)

def test_convert_to_punycode(self):
punycode = self.analyzer.convert_to_punycode("gіtһսb.com")
self.assertEqual(punycode, "xn--gtb-jhd02cr1b.com")

@patch('socket.gethostbyname')
def test_check_domain_registration(self, mock_gethostbyname):
mock_gethostbyname.return_value = "127.0.0.1"
result = self.analyzer.check_domain_registration("example.com")
self.assertEqual(result, "127.0.0.1")

def test_extract_domain_parts(self):
url = "example.com"
result = self.analyzer.extract_domain_parts(url)
self.assertEqual(result, ['example', 'com'])

def test_generate_combinations(self):
result = self.analyzer.generate_combinations('x')
self.assertEqual(result, ([['x', 'х']], ['х']))

@patch('builtins.print')
def test_analyze_domain(self, mock_print):
domain = "r.com"
self.analyzer.analyze_domain(domain)
mock_print.assert_called_with("IDN homograph attack is not possible for this domain with the current character set")

def test_load_unicode_combinations_from_file(self):
with patch('builtins.open', create=True) as mock_open:
mock_open.return_value.__enter__.return_value.read.return_value = '[{"latin": "a", "similar": ["а", "ɑ"]}]'
result = load_unicode_combinations_from_file("fake_file.json")
self.assertEqual(result, [{"latin": "a", "similar": ["а", "ɑ"]}])

def test_main(self):
with patch('sys.argv', ['src.evilurl.py', 'example.com']):
with patch('src.evilurl.HomographAnalyzer.analyze_domain') as mock_analyze_domain:
main()
mock_analyze_domain.assert_called_with('example.com')

if __name__ == '__main__':
unittest.main()

0 comments on commit f6c5665

Please sign in to comment.