forked from phishdetect/phishdetect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlchecks.go
276 lines (237 loc) · 6.04 KB
/
urlchecks.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// PhishDetect
// Copyright (c) 2018-2019 Claudio Guarnieri.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package phishdetect
import (
"encoding/base64"
"regexp"
"strings"
"github.com/google/safebrowsing"
log "github.com/sirupsen/logrus"
)
// SafeBrowsingKey contains the API key to use Google SafeBrowsing API.
var SafeBrowsingKey string
func checkSuspiciousHostname(link *Link, page *Page, brands *Brands) bool {
lowSuspects := []string{
"auth", "authorize", "authenticate", "authentication",
"account", "myaccount",
"activation",
"apps",
"confirm",
"credential",
"drive",
"login",
"mails", "rnail",
"managment",
"password",
"permission", "permision",
"recovery", "recover",
"register",
"secure", "safe",
"signin",
"support", "suport",
"unlock",
"update",
"verify", "verification", "everivcation", "verifications", "veryfication", "veryfications",
"wallet",
}
normalized := strings.Replace(link.Domain, ".", "|", -1)
normalized = strings.Replace(normalized, "-", "|", -1)
words := strings.Split(normalized, "|")
high := 0
low := 0
for _, word := range words {
for _, brand := range brands.List {
if SliceContains(brand.Suspicious, word) {
// A suspicious brand name in the domain should have more weight than
// anything.
brand.Matches += 10
return true
} else if SliceContains(brand.Original, word) {
// A brand name in a domain should have more weight than a brand name in the
// page HTML.
brand.Matches += 3
high++
}
}
if SliceContains(lowSuspects, word) {
low++
}
}
if high >= 2 || (high >= 1 && low >= 1) || low >= 2 {
return true
}
return false
}
func checkSuspiciousTLD(link *Link, page *Page, brands *Brands) bool {
suspects := []string{".ga", ".gq", ".ml", ".cf", ".tk", ".xyz", "cc", ".gb",
".info", ".biz", ".cm", ".online", ".support", ".click", ".pro", ".icu",}
for _, suspect := range suspects {
if strings.HasSuffix(link.Domain, suspect) {
return true
}
}
return false
}
func checkSuspiciousBridges(link *Link, page *Page, brands *Brands) bool {
suspects := []string{".com-"}
for _, suspect := range suspects {
if strings.Contains(link.Domain, suspect) {
return true
}
}
return false
}
func checkEncodedDomain(link *Link, page *Page, brands *Brands) bool {
if !strings.Contains(link.Domain, "xn--") {
return false
}
for _, brand := range brands.List {
for _, word := range brand.Suspicious {
if !strings.Contains(word, "xn--") {
continue
}
if strings.Contains(link.Domain, word) {
brand.Matches++
return true
}
}
}
return false
}
func checkExcessivePunct(link *Link, page *Page, brands *Brands) bool {
regex, _ := regexp.Compile("\\.")
dots := regex.FindAllString(link.Domain, -1)
dotsCount := len(dots)
dashesCount := 0
if !strings.Contains(link.Domain, "xn--") {
regex, _ = regexp.Compile("-")
dashes := regex.FindAllString(link.Domain, -1)
dashesCount = len(dashes)
}
total := dotsCount + dashesCount
if total >= 4 {
return true
}
return false
}
func checkNoTLS(link *Link, page *Page, brands *Brands) bool {
if strings.HasPrefix(link.Scheme, "http") {
if link.Scheme != "https" {
return true
}
}
return false
}
func checkB64Parameters(link *Link, page *Page, brands *Brands) bool {
for _, value := range link.Parameters {
// We skip strings that are too short, because they could significantly
// raise false positives.
if len(value) <= 8 {
continue
}
_, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return true
}
}
return false
}
func checkGoogleSafeBrowsing(link *Link, page *Page, brands *Brands) bool {
if SafeBrowsingKey == "" {
return false
}
log.Debug("Using Google SafeBrowsing API key: ", SafeBrowsingKey)
sb, err := safebrowsing.NewSafeBrowser(safebrowsing.Config{
APIKey: SafeBrowsingKey,
})
if err != nil {
log.Error(err.Error())
return false
}
threats, err := sb.LookupURLs([]string{link.URL})
if err != nil {
log.Error(err.Error())
return false
}
if len(threats[0]) > 0 {
for _, threat := range threats {
log.Debug(threat)
}
return true
}
log.Debug("No Google SafeBrowsing threats found for this URL")
return false
}
// GetDomainChecks returns a list of only the checks that work for domain names.
func GetDomainChecks() []Check {
return []Check{
{
checkSuspiciousTLD,
5,
"suspicious-tld",
"The domain uses a suspicious TLD",
},
{
checkExcessivePunct,
20,
"excessive-punct",
"The domain has suspicious amount of dots and dashes",
},
{
checkSuspiciousHostname,
30,
"suspicious-hostname",
"The domain contains suspicious words",
},
{
checkSuspiciousBridges,
30,
"suspicious-bridges",
"The domain uses very suspicious patterns used for bad domains composition",
},
{
checkEncodedDomain,
50,
"encoded-domain",
"The domain contains special characters to mimic known brands",
},
{
checkGoogleSafeBrowsing,
50,
"google-safebrowsing",
"The link is listed in Google SafeBrowsing as malicious",
},
}
}
// GetURLChecks returns a list of all the available URL checks.
func GetURLChecks() []Check {
checks := GetDomainChecks()
checks = append(checks, []Check{
{
checkB64Parameters,
5,
"base64-parameters",
"The link might contain base64 encoded parameters (low confidence)",
},
{
checkNoTLS,
20,
"no-tls",
"The website is not using a secure transport layer (HTTPS)",
},
}...)
return checks
}