forked from tdh8316/Investigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvestigo.go
416 lines (373 loc) · 9.57 KB
/
investigo.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync"
color "github.com/fatih/color"
"golang.org/x/net/proxy"
)
const (
dataFileName string = "data.json"
userAgent string = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
maxGoroutines int = 64
)
// Initialize sites not included in Sherlock
func initializeExtraSiteData() {
siteData["Pornhub"] = SiteData{
ErrorType: "status_code",
URLMain: "https://www.pornhub.com/",
URL: "https://www.pornhub.com/users/{}",
}
siteData["NAVER"] = SiteData{
ErrorType: "status_code",
URLMain: "https://www.naver.com/",
URL: "https://blog.naver.com/{}",
}
siteData["xvideos"] = SiteData{
ErrorType: "status_code",
URLMain: "https://xvideos.com/",
URL: "https://xvideos.com/profiles/{}",
}
}
func main() {
fmt.Println(`Investigo - Investigate User Across Social Networks.`)
args := os.Args[1:]
var argIndex int
options.noColor, argIndex = HasElement(args, "--no-color")
if options.noColor {
logger = log.New(os.Stdout, "", 0)
args = append(args[:argIndex], args[argIndex+1:]...)
}
options.withTor, argIndex = HasElement(args, "-t", "--tor")
if options.withTor {
args = append(args[:argIndex], args[argIndex+1:]...)
}
options.verbose, argIndex = HasElement(args, "-v", "--verbose")
if options.verbose {
args = append(args[:argIndex], args[argIndex+1:]...)
}
options.checkForUpdate, argIndex = HasElement(args, "--update")
if options.checkForUpdate {
args = append(args[:argIndex], args[argIndex+1:]...)
}
// Loads site data from sherlock database and assign to a variable.
initializeSiteData(options.checkForUpdate)
if help, _ := HasElement(args, "-h", "--help"); help || len(args) < 1 {
os.Exit(0)
}
// Loads extra site data
initializeExtraSiteData()
for _, username := range args {
if options.noColor {
fmt.Printf("Investigating %s on:\n", username)
} else {
fmt.Fprintf(color.Output, "Investigating %s on:\n", color.HiGreenString(username))
}
waitGroup.Add(len(siteData))
for site := range siteData {
guard <- 1
go func(site string) {
defer waitGroup.Done()
res := Investigo(username, site, siteData[site])
WriteResult(res)
<-guard
}(site)
}
waitGroup.Wait()
}
return
}
// Result of Investigo function
type Result struct {
Usernane string
Exist bool
Proxied bool
Site string
URL string
URLProbe string
Link string
Err bool
ErrMsg string
}
var (
guard = make(chan int, maxGoroutines)
waitGroup = &sync.WaitGroup{}
logger = log.New(color.Output, "", 0)
siteData = map[string]SiteData{}
options struct {
noColor bool
updateBeforeRun bool
withTor bool
verbose bool
checkForUpdate bool
}
)
// A SiteData struct for json datatype
type SiteData struct {
ErrorType string `json:"errorType"`
ErrorMsg string `json:"errorMsg"`
URL string `json:"url"`
URLMain string `json:"urlMain"`
URLProbe string `json:"urlProbe"`
URLError string `json:"errorUrl"`
// UsedUsername string `json:"username_claimed"`
// UnusedUsername string `json:"username_unclaimed"`
// RegexCheck string `json:"regexCheck"`
// Rank int`json:"rank"`
}
// RequestError interface
type RequestError interface {
Error() string
}
func initializeSiteData(forceUpdate bool) {
jsonFile, err := os.Open(dataFileName)
if err != nil || forceUpdate {
if options.noColor {
fmt.Printf(
"%s Update %s:%s",
("->"),
dataFileName,
("Downloading..."),
)
} else {
fmt.Fprintf(
color.Output,
"%s Update %s:%s",
color.HiRedString("->"),
dataFileName,
color.HiYellowString("Downloading..."),
)
}
if forceUpdate {
jsonFile.Close()
}
r, err := Request("https://raw.githubusercontent.com/sherlock-project/sherlock/master/data.json")
if err != nil || r.StatusCode != 200 {
if options.noColor {
fmt.Printf(" [%s]\n", ("Failed"))
} else {
fmt.Fprintf(color.Output, " [%s]\n", color.HiRedString("Failed"))
}
panic("Failed to connect to Investigo repository.")
} else {
defer r.Body.Close()
}
if _, err := os.Stat(dataFileName); !os.IsNotExist(err) {
if err = os.Remove(dataFileName); err != nil {
panic(err)
}
}
_updateFile, _ := os.OpenFile(dataFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if _, err := _updateFile.WriteString(ReadResponseBody(r)); err != nil {
if options.noColor {
fmt.Printf("Failed to update data.\n")
} else {
fmt.Fprintf(color.Output, color.RedString("Failed to update data.\n"))
}
panic(err)
}
_updateFile.Close()
jsonFile, _ = os.Open(dataFileName)
fmt.Println(" [Done]")
}
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
panic("Error while read " + dataFileName)
} else {
json.Unmarshal([]byte(byteValue), &siteData)
}
return
}
// Specify Tor proxy ip and port
// var torProxy string = "socks5://127.0.0.1:9050" // 9150 w/ Tor Browser
// var UseTor bool = true
// Request makes HTTP request
func Request(target string) (*http.Response, RequestError) {
request, err := http.NewRequest("GET", target, nil)
if err != nil {
return nil, err
}
request.Header.Set("User-Agent", userAgent)
client := &http.Client{}
// client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
// return errors.New("Redirect")
// }
if options.withTor {
tbProxyURL, err := url.Parse("socks5://127.0.0.1:9050")
if err != nil {
return nil, err
}
tbDialer, err := proxy.FromURL(tbProxyURL, proxy.Direct)
if err != nil {
return nil, err
}
tbTransport := &http.Transport{
Dial: tbDialer.Dial,
}
client.Transport = tbTransport
}
return client.Do(request)
}
// ReadResponseBody reads response body and return string
func ReadResponseBody(response *http.Response) string {
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
return string(bodyBytes)
}
// HasElement reports whether elements is within array.
func HasElement(array []string, targets ...string) (bool, int) {
for index, item := range array {
for _, target := range targets {
if item == target {
return true, index
}
}
}
return false, -1
}
// Investigo investigate if username exists on social media.
func Investigo(username string, site string, data SiteData) Result {
var url, urlProbe string
result := Result{
Usernane: username,
URL: data.URL,
URLProbe: data.URLProbe,
Proxied: options.withTor,
Exist: false,
Site: site,
Err: true,
ErrMsg: "No return value",
}
// string to display
url = strings.Replace(data.URL, "{}", username, 1)
if data.URLProbe != "" {
urlProbe = strings.Replace(data.URLProbe, "{}", username, 1)
} else {
urlProbe = url
}
r, err := Request(urlProbe)
if err != nil {
if r != nil {
r.Body.Close()
}
return Result{
Usernane: username,
URL: data.URL,
URLProbe: data.URLProbe,
Proxied: options.withTor,
Exist: false,
Site: site,
Err: true,
ErrMsg: err.Error(),
}
}
// check error types
switch data.ErrorType {
case "status_code":
if r.StatusCode <= 300 || r.StatusCode < 200 {
result = Result{
Usernane: username,
URL: data.URL,
URLProbe: data.URLProbe,
Proxied: options.withTor,
Exist: true,
Link: url,
Site: site,
}
} else {
result = Result{
Site: site,
Usernane: username,
}
}
case "message":
if !strings.Contains(ReadResponseBody(r), data.ErrorMsg) {
result = Result{
Usernane: username,
URL: data.URL,
URLProbe: data.URLProbe,
Proxied: options.withTor,
Exist: true,
Link: url,
Site: site,
}
} else {
// check if 404
result = Result{
URL: data.URL,
URLProbe: data.URLProbe,
Proxied: options.withTor,
Usernane: username,
Site: site,
}
}
case "response_url":
// In the original Sherlock implementation,
// the error type `response_url` works as `status_code`.
if (r.StatusCode <= 300 || r.StatusCode < 200) && r.Request.URL.String() == url {
result = Result{
Usernane: username,
URL: data.URL,
URLProbe: data.URLProbe,
Proxied: options.withTor,
Exist: true,
Link: url,
Site: site,
}
} else {
result = Result{
Usernane: username,
URL: data.URL,
URLProbe: data.URLProbe,
Proxied: options.withTor,
Site: site,
}
}
default:
result = Result{
Usernane: username,
Proxied: options.withTor,
Exist: false,
Err: true,
ErrMsg: "Unsupported error type `" + data.ErrorType + "`",
Site: site,
}
}
r.Body.Close()
return result
}
// Check content of
// WriteResult writes investigation result to stdout and file
func WriteResult(result Result) {
if options.noColor {
if result.Exist {
logger.Printf("[%s] %s: %s\n", ("+"), result.Site, result.Link)
} else {
if result.Err {
logger.Printf("[%s] %s: ERROR: %s", ("!"), result.Site, (result.ErrMsg))
} else if options.verbose {
logger.Printf("[%s] %s: %s", ("-"), result.Site, ("Not Found!"))
}
}
} else {
if result.Exist {
logger.Printf("[%s] %s: %s\n", color.HiGreenString("+"), color.HiWhiteString(result.Site), result.Link)
} else {
if result.Err {
logger.Printf("[%s] %s: %s: %s", color.HiRedString("!"), result.Site, color.HiMagentaString("ERROR"), color.HiRedString(result.ErrMsg))
} else if options.verbose {
logger.Printf("[%s] %s: %s", color.HiRedString("-"), result.Site, color.HiYellowString("Not Found!"))
}
}
}
return
}