-
Notifications
You must be signed in to change notification settings - Fork 28
/
analyticsrelationships.go
286 lines (258 loc) · 8.23 KB
/
analyticsrelationships.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
277
278
279
280
281
282
283
284
285
286
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"time"
)
const colorReset = "\033[0m"
const colorYellow = "\033[33m"
const colorRed = "\033[31m"
func crash(message string, err error) {
fmt.Print(string(colorRed) + "[ERROR] " + message + string(colorReset) + "\n")
panic(err)
}
func warning(message string) {
fmt.Print(string(colorYellow) + "[WARNING] " + message + string(colorReset) + "\n")
}
func info(message string) {
fmt.Print("[-] " + message + "\n")
}
func banner() {
data := `
██╗ ██╗ █████╗ ██╗██████╗
██║ ██║██╔══██╗ ██║██╔══██╗
██║ ██║███████║█████╗██║██║ ██║
██║ ██║██╔══██║╚════╝██║██║ ██║
╚██████╔╝██║ ██║ ██║██████╔╝
╚═════╝ ╚═╝ ╚═╝ ╚═╝╚═════╝
██████╗ ██████╗ ███╗ ███╗ █████╗ ██╗███╗ ██╗███████╗
██╔══██╗██╔═══██╗████╗ ████║██╔══██╗██║████╗ ██║██╔════╝
██║ ██║██║ ██║██╔████╔██║███████║██║██╔██╗ ██║███████╗
██║ ██║██║ ██║██║╚██╔╝██║██╔══██║██║██║╚██╗██║╚════██║
██████╔╝╚██████╔╝██║ ╚═╝ ██║██║ ██║██║██║ ╚████║███████║
╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚══════╝
`
data += "\033[32m> \033[0mGet related domains / subdomains by looking at Google Analytics IDs\n"
data += "\033[32m> \033[0mGO Version\n"
data += "\033[32m> \033[0mBy @JosueEncinar\n"
println(data)
}
func getURLResponse(url string) string {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Timeout: time.Second * 3}
res, err := client.Get(url)
if err != nil {
return ""
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return ""
}
return string(body)
}
func getGoogleTagManager(targetURL string) (bool, []string) {
var resultTagManager []string
response := getURLResponse(targetURL)
if response != "" {
pattern := regexp.MustCompile(`www\.googletagmanager\.com/ns\.html\?id=[A-Z0-9\-]+`)
data := pattern.FindStringSubmatch(response)
if len(data) > 0 {
resultTagManager = append(resultTagManager, "https://"+strings.Replace(data[0], "ns.html", "gtm.js", -1))
} else {
pattern = regexp.MustCompile("GTM-[A-Z0-9]+")
data = pattern.FindStringSubmatch(response)
if len(data) > 0 {
resultTagManager = append(resultTagManager, "https://www.googletagmanager.com/gtm.js?id="+data[0])
} else {
pattern = regexp.MustCompile(`UA-\d+-\d+`)
aux := pattern.FindAllStringSubmatch(response, -1)
var result []string
for _, r := range aux {
result = append(result, r[0])
}
return true, result
}
}
}
return false, resultTagManager
}
func getUA(url string) []string {
pattern := regexp.MustCompile("UA-[0-9]+-[0-9]+")
response := getURLResponse(url)
var result []string
if response != "" {
aux := pattern.FindAllStringSubmatch(response, -1)
for _, r := range aux {
result = append(result, r[0])
}
} else {
result = nil
}
return result
}
func cleanRelationShips(domains [][]string) []string {
var allDomains []string
for _, domain := range domains {
allDomains = append(allDomains, strings.Replace(domain[0], "/relationships/", "", -1))
}
return allDomains
}
func getDomainsFromBuiltWith(id string) []string {
pattern := regexp.MustCompile(`/relationships/[a-z0-9\-\_\.]+\.[a-z]+`)
url := "https://builtwith.com/relationships/tag/" + id
response := getURLResponse(url)
var allDomains []string = nil
if response != "" {
allDomains = cleanRelationShips(pattern.FindAllStringSubmatch(response, -1))
}
return allDomains
}
func getDomainsFromHackerTarget(id string) []string {
url := "https://api.hackertarget.com/analyticslookup/?q=" + id
response := getURLResponse(url)
var allDomains []string = nil
if response != "" && !strings.Contains(response, "API count exceeded") {
allDomains = strings.Split(response, "\n")
}
return allDomains
}
func getDomains(id string) []string {
var allDomains []string = getDomainsFromBuiltWith(id)
domains2 := getDomainsFromHackerTarget(id)
if len(domains2) != 0 {
for _, domain := range domains2 {
if !contains(allDomains, domain) {
allDomains = append(allDomains, domain)
}
}
}
return allDomains
}
func contains(data []string, value string) bool {
for _, v := range data {
if v == value {
return true
}
}
return false
}
func showDomains(ua string, chainMode bool) {
allDomains := getDomains(ua)
if !chainMode {
fmt.Println(">> " + ua)
if len(allDomains) == 0 {
fmt.Println("|__ NOT FOUND")
}
for _, domain := range allDomains {
fmt.Println("|__ " + domain)
}
fmt.Println("")
} else {
if len(allDomains) == 0 {
warning("NOT FOUND")
}
for _, domain := range allDomains {
if domain == "error getting results" {
var err error
crash("Server-side error on builtwith.com: error getting results", err)
}
fmt.Println(domain)
}
}
}
func start(url string, chainMode bool) {
if !strings.HasPrefix(url, "http") {
url = "https://" + url
}
if !chainMode {
info("Analyzing url: " + url)
}
uaResult, resultTagManager := getGoogleTagManager(url)
if len(resultTagManager) > 0 {
var visited = []string{}
var allUAs []string
if !uaResult {
urlGoogleTagManager := resultTagManager[0]
if !chainMode {
info("URL with UA: " + urlGoogleTagManager)
}
allUAs = getUA(urlGoogleTagManager)
} else {
if !chainMode {
info("Found UA directly")
}
allUAs = resultTagManager
}
if !chainMode {
info("Obtaining information from builtwith and hackertarget\n")
}
for _, ua := range allUAs {
baseUA := strings.Join(strings.Split(ua, "-")[0:2], "-")
if !contains(visited, baseUA) {
visited = append(visited, baseUA)
showDomains(baseUA, chainMode)
}
}
if !chainMode {
info("Done!")
}
} else {
warning("Tagmanager URL not found")
//Now, the program exits
}
}
//needs to be a global variable
var chainMode bool
func main() {
var url string
flag.StringVar(&url, "u", "", "URL to extract Google Analytics ID")
flag.StringVar(&url, "url", "", "URL to extract Google Analytics ID")
flag.BoolVar(&chainMode, "ch", false, "In \"chain-mode\" we only output the important information. No decorations.")
flag.BoolVar(&chainMode, "chain-mode", false, "In \"chain-mode\" we only output the important information. No decorations.")
const usage = `Usage: ./analyticsrelationships -u URL [--chain-mode]
-u, --url string
URL to extract Google Analytics ID
-ch, --chain-mode
In "chain-mode" we only output the important information. No decorations.
Default: false
`
//https://www.antoniojgutierrez.com/posts/2021-05-14-short-and-long-options-in-go-flags-pkg/
flag.Usage = func() { fmt.Print(usage) }
//parse CLI arguments
flag.Parse()
if !chainMode {
//display banner
banner()
}
if url != "" {
//start main processing
start(url, chainMode)
} else {
//read from standard input (stdin)
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
//data is being piped to stdin
//read stdin
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if err := scanner.Err(); err != nil {
crash("bufio couldn't read stdin correctly.", err)
} else {
start(scanner.Text(), chainMode)
}
}
} //else { //stdin is from a terminal }
}
}