-
Notifications
You must be signed in to change notification settings - Fork 22
/
subhunter.go
355 lines (299 loc) · 9.41 KB
/
subhunter.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
// Created by Nemesis
// Contact: [email protected]
package main
import (
"bufio"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/common-nighthawk/go-figure"
"github.com/parnurzeal/gorequest"
)
// ANSI Color Codes
const (
Reset = "\x1b[0m"
Red = "\x1b[31;1m"
Green = "\x1b[32;1m"
Blue = "\x1b[34;1m"
)
// Prints a result message with the specified color
func PrintResult(colorCode, resultMessage string) {
result := fmt.Sprintf("[%s+%s]%s %s%s", colorCode, Reset, colorCode, resultMessage, Reset)
fmt.Println(result)
}
// Structure of fingerprint.json
type FingerprintData struct {
Name string `json:"service"`
Cname []string `json:"cname"`
Fingerprint interface{} `json:"fingerprint"`
Response []string `json:"response"`
}
var Fingerprints []FingerprintData
var Targets []string
var (
HostsList string
Threads int
All bool
Verbose bool
Timeout int
OutputFile string
)
var VulnerableResults []string
var NotVulnerableResults []string
// User agents
func getRandomUserAgent() string {
userAgents := []string{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.43",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.3",
}
rand.Seed(time.Now().UnixNano())
return userAgents[rand.Intn(len(userAgents))]
}
func InitializeFingerprints() {
currentDir, err := os.Getwd()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
filePath := filepath.Join(currentDir, "fingerprint.json")
// Checks if the file exists, if not downloads it
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Println("Downloading fingerprint.json...")
if err := downloadFile(filePath, "https://raw.githubusercontent.com/Nemesis0U/Subhunter/main/fingerprint.json"); err != nil {
fmt.Printf("Error downloading fingerprint.json: %s\n", err)
os.Exit(1)
}
fmt.Println("Download complete.")
}
raw, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
err = json.Unmarshal(raw, &Fingerprints)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
}
// Downloads a file from the URL and saves it to the local path
func downloadFile(filepath string, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP request failed with status code: %d", resp.StatusCode)
}
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func ReadFile(file string) (lines []string, err error) {
fileHandle, err := os.Open(file)
if err != nil {
return lines, err
}
defer fileHandle.Close()
fileScanner := bufio.NewScanner(fileHandle)
for fileScanner.Scan() {
lines = append(lines, fileScanner.Text())
}
return lines, nil
}
func Get(url string, timeout int) (resp gorequest.Response, body string, errs []error) {
url = fmt.Sprintf("https://%s/", url) // Uses https
userAgent := getRandomUserAgent()
resp, body, errs = gorequest.New().TLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
Timeout(time.Duration(timeout)*time.Second).Get(url).
Set("User-Agent", userAgent). // Sets random user agent
End()
return resp, body, errs
}
func ParseArguments() {
flag.StringVar(&HostsList, "l", "", "File including a list of hosts to scan")
flag.IntVar(&Timeout, "timeout", 20, "Timeout in seconds")
flag.StringVar(&OutputFile, "o", "", "File to save results")
flag.IntVar(&Threads, "t", 50, "Number of threads for scanning")
flag.Parse()
}
func CNAMEExists(key string) bool {
for _, fingerprint := range Fingerprints {
for _, cname := range fingerprint.Cname {
if strings.Contains(key, cname) {
return true
}
}
}
return false
}
func Check(target string, TargetCNAME string) {
_, body, errs := Get(target, Timeout)
if len(errs) == 0 {
if TargetCNAME == "All" {
for _, fingerprint := range Fingerprints {
for _, response := range fingerprint.Response {
if strings.Contains(body, response) {
resultMessage := fmt.Sprintf("%s: Possible takeover found at %s: Vulnerable", fingerprint.Name, target)
PrintResult(Green, resultMessage)
VulnerableResults = append(VulnerableResults, resultMessage)
return
}
}
}
resultMessage := fmt.Sprintf("Nothing found at %s: Not Vulnerable", target)
PrintResult(Blue, resultMessage)
NotVulnerableResults = append(NotVulnerableResults, resultMessage)
} else {
for _, fingerprint := range Fingerprints {
for _, cname := range fingerprint.Cname {
if strings.Contains(TargetCNAME, cname) {
for _, response := range fingerprint.Response {
if strings.Contains(body, response) {
if fingerprint.Name == "cloudfront" {
_, body2, _ := Get(target, 120)
if strings.Contains(body2, response) {
resultMessage := fmt.Sprintf("%s: Possible takeover found at %s: Vulnerable", fingerprint.Name, target)
PrintResult(Green, resultMessage)
VulnerableResults = append(VulnerableResults, resultMessage)
}
} else {
resultMessage := fmt.Sprintf("%s: Possible takeover found at %s with CNAME record %s: Vulnerable", fingerprint.Name, target, TargetCNAME)
PrintResult(Green, resultMessage)
VulnerableResults = append(VulnerableResults, resultMessage)
}
}
return
}
}
}
}
resultMessage := fmt.Sprintf("Nothing found at %s with CNAME record %s: Not Vulnerable", target, TargetCNAME)
PrintResult(Blue, resultMessage)
NotVulnerableResults = append(NotVulnerableResults, resultMessage)
}
} else {
if Verbose {
log.Printf("(Error) Get: %s => %v", target, errs)
}
resultMessage := fmt.Sprintf("Failed to check %s: Error", target)
PrintResult(Red, resultMessage)
NotVulnerableResults = append(NotVulnerableResults, resultMessage)
}
}
var CheckedTargetsMutex sync.Mutex // Declares the mutex globally
var CheckedTargets = make(map[string]bool) // Declares CheckedTargets globally
func Checker(target string) {
TargetCNAME, err := net.LookupCNAME(target)
if err != nil {
return
}
CheckedTargetsMutex.Lock() // Locks the mutex
if All != true && CNAMEExists(TargetCNAME) == true {
if Verbose == true {
log.Printf("(CNAME Selected) %s => %s", target, TargetCNAME)
}
Check(target, TargetCNAME)
} else if All == true && !CheckedTargets[target] { // Checks if the target hasn't been checked already
CheckedTargets[target] = true // Marks the target as checked
Check(target, "All")
}
CheckedTargetsMutex.Unlock() // Unlocks the mutex
}
func main() {
All = true
Verbose = true
ParseArguments()
// Initializes fingerprints before using them
InitializeFingerprints()
for _, Host := range Targets {
go Checker(Host)
}
fmt.Println("")
Banner := figure.NewColorFigure("Subhunter", "", "red", true)
Banner.Print()
fmt.Println("\n\nA fast subdomain takeover tool\n")
fmt.Println("Created by Nemesis")
fmt.Printf("\nLoaded %d fingerprints for current scan\n", len(Fingerprints))
fmt.Println("\n-----------------------------------------------------------------------------\n")
if HostsList == "" {
fmt.Printf("Subhunter: No subdomains list specified for the scan!")
fmt.Printf("\n\nInfo: Use -h for showing the help message\n\n")
os.Exit(1)
}
Hosts, err := ReadFile(HostsList)
if err != nil {
fmt.Printf("\nread: %s\n", err)
os.Exit(1)
}
Targets = append(Targets, Hosts...)
hosts := make(chan string, Threads)
processGroup := new(sync.WaitGroup)
processGroup.Add(Threads)
for i := 0; i < Threads; i++ {
go func() {
for {
host := <-hosts
if host == "" {
break
}
Checker(host)
}
processGroup.Done()
}()
}
for _, Host := range Targets {
hosts <- Host
}
close(hosts)
processGroup.Wait()
fmt.Printf("\nSubhunter exiting...\n")
// Writes the results to the output file if provided
if OutputFile != "" {
WriteResultsToFile(OutputFile, VulnerableResults, NotVulnerableResults)
}
}
func WriteResultsToFile(filename string, vulnerableResults []string, notVulnerableResults []string) {
file, err := os.Create(filename)
if err != nil {
log.Fatalf("Error creating output file: %v", err)
}
defer file.Close()
// Writes vulnerable results
for _, result := range vulnerableResults {
_, err := file.WriteString(result + "\n")
if err != nil {
log.Fatalf("Error writing to output file: %v", err)
}
}
// Writes other results
for _, result := range notVulnerableResults {
_, err := file.WriteString(result + "\n")
if err != nil {
log.Fatalf("Error writing to output file: %v", err)
}
}
fmt.Printf("Results written to %s\n", filename)
}