/
abuseipdb.go
82 lines (68 loc) · 1.81 KB
/
abuseipdb.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
package runner
import (
"io"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/projectdiscovery/gologger"
)
func getAbuseipdbSubs(domain, out, sessionCookie string) error {
gologger.Debug().Msg("gathering subdomains from AbuseIPDB")
// create output file
file, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return err
}
defer file.Close()
req, err := http.NewRequest("GET", "https://www.abuseipdb.com/whois/"+domain, nil)
if err != nil {
return err
}
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:110.0) Gecko/20100101 Firefox/110.0")
// create a new session cookie
cookie := &http.Cookie{
Name: "abuseipdb_session",
Value: sessionCookie,
}
// add the session cookie to the request
req.AddCookie(cookie)
// send get request
client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
gologger.Warning().Msg("get request to abuseipdb failed")
return err
}
defer resp.Body.Close()
// check if the response code is not code 200
if resp.StatusCode != 200 {
gologger.Warning().Msg("abuseipdb failed")
return nil
}
// convert the response to bytes
content, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
// grep matches
reg, err := regexp.Compile(`<li>\w.*</li>`)
if err != nil {
return err
}
submatches := reg.FindAllString(string(content), -1)
// write trimmed matches to output file
for _, submatch := range submatches {
submatch = strings.TrimPrefix(submatch, "<li>")
submatch = strings.TrimSuffix(submatch, "</li>")
subdomain := submatch + "." + domain
file.WriteString(subdomain + "\n")
}
// display number of subdomains found
gologger.Debug().Msg("AbuseIPDB: " + strconv.Itoa(len(submatches)) + " subdomains were found")
return nil
}