-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextractor.go
More file actions
240 lines (210 loc) · 6.51 KB
/
Copy pathextractor.go
File metadata and controls
240 lines (210 loc) · 6.51 KB
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
package main
import (
"encoding/hex"
"fmt"
"html"
"net/http"
"net/url"
"regexp"
"strings"
"unicode"
"github.com/PuerkitoBio/goquery"
"github.com/robertkrimen/otto"
)
func GetEmails(htmlContent string, scrapedURL string) ([]string, error) {
emails := make(map[string]struct{})
doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlContent))
if err != nil {
return nil, err
}
doc.Find("[data-cfemail]").Each(func(_ int, el *goquery.Selection) {
cfemail, _ := el.Attr("data-cfemail")
emails[deCFEmail(cfemail)] = struct{}{}
})
doc.Find("object[data], img[src], embed[src]").Each(func(_ int, el *goquery.Selection) {
svgLink, exists := el.Attr("data")
if !exists {
svgLink, exists = el.Attr("src")
}
if exists && strings.HasSuffix(svgLink, ".svg") {
parsedURL, err := url.Parse(svgLink)
if err != nil {
fmt.Println("Error parsing URL:", err)
return
}
if !parsedURL.IsAbs() {
baseURL, err := url.Parse(scrapedURL)
if err != nil {
fmt.Println("Error parsing base URL:", err)
return
}
parsedURL = baseURL.ResolveReference(parsedURL)
}
absoluteSVGLink := parsedURL.String()
svgEmails, err := extractEmailsFromSVG(absoluteSVGLink)
if err == nil {
for _, email := range svgEmails {
emails[email] = struct{}{}
}
}
}
})
extractedEmails := extractEmails(htmlContent)
for _, email := range extractedEmails {
emails[email] = struct{}{}
}
doc.Find("a").Each(func(_ int, el *goquery.Selection) {
href, _ := el.Attr("href")
hrefEmails := extractEmailsFromHref(href)
for _, email := range hrefEmails {
emails[email] = struct{}{}
}
})
var result []string
for email := range emails {
result = append(result, email)
}
return result, nil
}
func deCFEmail(encodedString string) string {
r, _ := hex.DecodeString(encodedString[:2])
decoded := make([]byte, len(encodedString)/2-1)
for i := 2; i < len(encodedString); i += 2 {
val, _ := hex.DecodeString(encodedString[i : i+2])
decoded[(i/2)-1] = val[0] ^ r[0]
}
return string(decoded)
}
func rotIsValid(email string) bool {
emailRegex := regexp.MustCompile(`^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.(com|edu|net|ca|co.uk|co|org|gov|info|de|au|nl|ru|fr|br|uk|io|me|dev)+$`)
return emailRegex.MatchString(email)
}
func rotateChar(char rune, x int) rune {
switch {
case unicode.IsLower(char):
return 'a' + (char-'a'+rune(x))%26
case unicode.IsUpper(char):
return 'A' + (char-'A'+rune(x))%26
default:
return char
}
}
func tryRotDecryption(encryptedEmail string) []string {
var potentialEmails []string
for x := 1; x < 26; x++ {
decrypted := strings.Map(func(r rune) rune { return rotateChar(r, x) }, encryptedEmail)
if rotIsValid(decrypted) {
potentialEmails = append(potentialEmails, decrypted)
}
}
return potentialEmails
}
func normalizeHTML(htmlStr string) string {
decoded, err := url.QueryUnescape(htmlStr)
if err != nil {
decoded = htmlStr
}
decoded = html.UnescapeString(decoded)
decoded = decodeUnicodeEscape(decoded)
return decoded
}
func decodeUnicodeEscape(str string) string {
unicodeEscapePattern := regexp.MustCompile(`\\u[0-9a-fA-F]{4}`)
return unicodeEscapePattern.ReplaceAllStringFunc(str, func(match string) string {
r, _ := hex.DecodeString(match[2:])
return string(r)
})
}
func processHTML(text string) string {
text = normalizeHTML(text)
text += " " + strings.Join(tryRotDecryption(text), " ")
text = regexp.MustCompile(`<!--(.|\s|\n)*?-->`).ReplaceAllString(text, "")
text = regexp.MustCompile(`<[^>]+>`).ReplaceAllString(text, "")
return text
}
func preProcessEmail(text string) string {
text = processHTML(text)
text = regexp.MustCompile(`\W*\.\W*|\W+(D|d)(O|0|o)(t|T)\W+`).ReplaceAllString(text, ".")
text = regexp.MustCompile(`([a-z0-9])(DOT|D0T|DoT)([a-z0-9])`).ReplaceAllString(text, "$1.$3")
text = regexp.MustCompile(`([A-Z0-9])(dot|d0t|dOt)([A-Z0-9])`).ReplaceAllString(text, "$1.$3")
text = regexp.MustCompile(`\W*@\W*|\W+(A|a)(T|t)\W+`).ReplaceAllString(text, "@")
text = regexp.MustCompile(`([a-z0-9])AT([a-z0-9])`).ReplaceAllString(text, "$1@$2")
text = regexp.MustCompile(`([A-Z0-9])at([A-Z0-9])`).ReplaceAllString(text, "$1@$2")
text = regexp.MustCompile(`([a-z0-9])REMOVE([a-z0-9@\.])`).ReplaceAllString(text, "$1$2")
text = regexp.MustCompile(`([A-Z0-9])remove([A-Z0-9@\.])`).ReplaceAllString(text, "$1$2")
text = regexp.MustCompile(`[_\W]*n[_\W]*(o|0)[_\W]*(s|5)[_\W]*p[_\W]*a[_\W]*m[_\W]*`).ReplaceAllStringFunc(text, func(match string) string {
if strings.Contains(match, ".") {
return "."
} else if strings.Contains(match, "@") {
return "@"
}
return ""
})
return text
}
func evaluateJSExpression(expression string) string {
vm := otto.New()
value, err := vm.Run(expression)
if err != nil {
return ""
}
result, err := value.ToString()
if err != nil {
return ""
}
return result
}
func extractEmailsFromHref(href string) []string {
if strings.HasPrefix(href, "javascript:") {
expression := strings.TrimPrefix(href, "javascript:")
decoded := evaluateJSExpression(expression)
return extractEmails(decoded)
}
return extractEmails(href)
}
func extractEmails(htmlString string) []string {
cleanedString := preProcessEmail(htmlString)
emailRegex := regexp.MustCompile(`\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[a-zA-Z]{2,}\b`)
rtlPattern := regexp.MustCompile(`\b(moc|ac|gro|ten|ppa|due|ku\.oc)\.[a-zA-Z0-9.-]+@[A-Za-z0-9._%+-]+\b`)
rtlEmails := rtlPattern.FindAllString(cleanedString, -1)
for i := range rtlEmails {
rtlEmails[i] = reverseString(rtlEmails[i])
}
foundEmails := emailRegex.FindAllString(cleanedString, -1)
allExtractedEmails := append(foundEmails, rtlEmails...)
return allExtractedEmails
}
func reverseString(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func fetchSVGContent(svgURL string) (string, error) {
resp, err := http.Get(svgURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return "", err
}
return doc.Text(), nil
}
func decodeHexAndUnicode(input string) string {
re := regexp.MustCompile(`&#x([0-9a-fA-F]+);`)
return re.ReplaceAllStringFunc(input, func(hexStr string) string {
code, _ := hex.DecodeString(hexStr[3 : len(hexStr)-1])
return string(code)
})
}
func extractEmailsFromSVG(svgURL string) ([]string, error) {
svgContent, err := fetchSVGContent(svgURL)
if err != nil {
return nil, err
}
decodedContent := decodeHexAndUnicode(svgContent)
return extractEmails(decodedContent), nil
}