-
Notifications
You must be signed in to change notification settings - Fork 354
/
catch_phishing.py
executable file
·158 lines (122 loc) · 5.22 KB
/
catch_phishing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# Copyright (c) 2017 @x0rz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import re
import math
import certstream
import tqdm
import yaml
import time
import os
from Levenshtein import distance
from termcolor import colored, cprint
from tld import get_tld
from confusables import unconfuse
certstream_url = 'wss://certstream.calidog.io'
log_suspicious = os.path.dirname(os.path.realpath(__file__))+'/suspicious_domains_'+time.strftime("%Y-%m-%d")+'.log'
suspicious_yaml = os.path.dirname(os.path.realpath(__file__))+'/suspicious.yaml'
external_yaml = os.path.dirname(os.path.realpath(__file__))+'/external.yaml'
pbar = tqdm.tqdm(desc='certificate_update', unit='cert')
def entropy(string):
"""Calculates the Shannon entropy of a string"""
prob = [ float(string.count(c)) / len(string) for c in dict.fromkeys(list(string)) ]
entropy = - sum([ p * math.log(p) / math.log(2.0) for p in prob ])
return entropy
def score_domain(domain):
"""Score `domain`.
The highest score, the most probable `domain` is a phishing site.
Args:
domain (str): the domain to check.
Returns:
int: the score of `domain`.
"""
score = 0
for t in suspicious['tlds']:
if domain.endswith(t):
score += 20
# Remove initial '*.' for wildcard certificates bug
if domain.startswith('*.'):
domain = domain[2:]
# Removing TLD to catch inner TLD in subdomain (ie. paypal.com.domain.com)
try:
res = get_tld(domain, as_object=True, fail_silently=True, fix_protocol=True)
domain = '.'.join([res.subdomain, res.domain])
except Exception:
pass
# Higer entropy is kind of suspicious
score += int(round(entropy(domain)*10))
# Remove lookalike characters using list from http://www.unicode.org/reports/tr39
domain = unconfuse(domain)
words_in_domain = re.split("\W+", domain)
# ie. detect fake .com (ie. *.com-account-management.info)
if words_in_domain[0] in ['com', 'net', 'org']:
score += 10
# Testing keywords
for word in suspicious['keywords']:
if word in domain:
score += suspicious['keywords'][word]
# Testing Levenshtein distance for strong keywords (>= 70 points) (ie. paypol)
for key in [k for (k,s) in suspicious['keywords'].items() if s >= 70]:
# Removing too generic keywords (ie. mail.domain.com)
for word in [w for w in words_in_domain if w not in ['email', 'mail', 'cloud']]:
if distance(str(word), str(key)) == 1:
score += 70
# Lots of '-' (ie. www.paypal-datacenter.com-acccount-alert.com)
if 'xn--' not in domain and domain.count('-') >= 4:
score += domain.count('-') * 3
# Deeply nested subdomains (ie. www.paypal.com.security.accountupdate.gq)
if domain.count('.') >= 3:
score += domain.count('.') * 3
return score
def callback(message, context):
"""Callback handler for certstream events."""
if message['message_type'] == "heartbeat":
return
if message['message_type'] == "certificate_update":
all_domains = message['data']['leaf_cert']['all_domains']
for domain in all_domains:
pbar.update(1)
score = score_domain(domain.lower())
# If issued from a free CA = more suspicious
if "Let's Encrypt" == message['data']['leaf_cert']['issuer']['O']:
score += 10
if score >= 100:
tqdm.tqdm.write(
"[!] Suspicious: "
"{} (score={})".format(colored(domain, 'red', attrs=['underline', 'bold']), score))
elif score >= 90:
tqdm.tqdm.write(
"[!] Suspicious: "
"{} (score={})".format(colored(domain, 'red', attrs=['underline']), score))
elif score >= 80:
tqdm.tqdm.write(
"[!] Likely : "
"{} (score={})".format(colored(domain, 'yellow', attrs=['underline']), score))
elif score >= 65:
tqdm.tqdm.write(
"[+] Potential : "
"{} (score={})".format(colored(domain, attrs=['underline']), score))
if score >= 75:
with open(log_suspicious, 'a') as f:
f.write("{}\n".format(domain))
if __name__ == '__main__':
with open(suspicious_yaml, 'r') as f:
suspicious = yaml.safe_load(f)
with open(external_yaml, 'r') as f:
external = yaml.safe_load(f)
if external['override_suspicious.yaml'] is True:
suspicious = external
else:
if external['keywords'] is not None:
suspicious['keywords'].update(external['keywords'])
if external['tlds'] is not None:
suspicious['tlds'].update(external['tlds'])
certstream.listen_for_events(callback, url=certstream_url)