-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bruteforce.go
132 lines (110 loc) · 2.89 KB
/
bruteforce.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"sync"
"github.com/0x4f53/textsubs"
)
func bruteforce() {
err := os.MkdirAll("output", os.ModePerm)
if err != nil {
fmt.Println("Error creating output directory:", err)
os.Exit(-1)
}
files, err := os.ReadDir(batchCache)
if err != nil {
fmt.Println("Error reading files:", err)
os.Exit(-1)
}
for _, file := range files {
if file.IsDir() {
continue
}
entries, err := readFile2(batchCache + file.Name())
if err != nil {
fmt.Println("Error reading file:", file.Name(), err)
continue
}
var wg sync.WaitGroup
workerChan := make(chan struct{}, maxWorkers)
for _, entry := range entries {
for _, domain := range domains {
wg.Add(1)
go func(subdomain, domain string) {
defer wg.Done()
workerChan <- struct{}{}
defer func() { <-workerChan }()
outputFilePath := filepath.Join("output", domain+".txt")
if *pairs {
outputFilePath = filepath.Join("output", domain+".json")
}
outputFile, err := os.OpenFile(outputFilePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
fmt.Println("Error opening output file:", outputFilePath, err)
return
}
defer outputFile.Close()
checkAndLogSubdomain(subdomain, domain, outputFile)
}(entry, domain)
}
}
wg.Wait()
err = os.Remove(batchCache + file.Name())
if err != nil {
fmt.Println("Error deleting file:", file.Name(), err)
} else {
fmt.Println("[✓] Processed and deleted file ", file.Name())
}
}
fmt.Println("Finished processing all files and subdomains.")
}
func readFile2(filePath string) ([]string, error) {
var entries []string
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
entries = append(entries, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return entries, nil
}
func checkAndLogSubdomain(subdomain, domain string, outputFile *os.File) {
fullDomain := fmt.Sprintf("%s.%s", subdomain, domain)
_, err := net.LookupHost(fullDomain)
if err == nil {
fmt.Println(" - " + fullDomain)
// Check if the subdomain already exists in the file
exists, readErr := stringInFile(fullDomain, outputFile)
if readErr != nil {
fmt.Println("Error reading from output file:", readErr)
return
}
if !exists {
if *pairs {
pairedSubdomains, _ := textsubs.SubdomainAndDomainPair(fullDomain, false, false)
for _, item := range pairedSubdomains {
jsonOutput, _ := json.Marshal(item)
_, writeErr := outputFile.WriteString(string(jsonOutput) + "\n")
if writeErr != nil {
fmt.Println("Error writing to output file:", writeErr)
}
}
} else {
_, writeErr := outputFile.WriteString(fullDomain + "\n")
if writeErr != nil {
fmt.Println("Error writing to output file:", writeErr)
}
}
}
}
}