-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
182 lines (147 loc) · 4.64 KB
/
main.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
package main
import (
"context"
"fmt"
"github.com/dsecuredcom/dynamic-file-searcher/pkg/config"
"github.com/dsecuredcom/dynamic-file-searcher/pkg/domain"
"github.com/dsecuredcom/dynamic-file-searcher/pkg/fasthttp"
"github.com/dsecuredcom/dynamic-file-searcher/pkg/http"
"github.com/dsecuredcom/dynamic-file-searcher/pkg/result"
"github.com/dsecuredcom/dynamic-file-searcher/pkg/utils"
"github.com/fatih/color"
"golang.org/x/time/rate"
"math/rand"
"os"
"sync"
"sync/atomic"
"time"
)
const (
urlBufferSize = 15000
)
func main() {
var markers []string
cfg := config.ParseFlags()
initialDomains := domain.GetDomains(cfg.DomainsFile, cfg.Domain)
paths := utils.ReadLines(cfg.PathsFile)
if cfg.MarkersFile != "" {
markers = utils.ReadLines(cfg.MarkersFile)
}
limiter := rate.NewLimiter(rate.Limit(cfg.Concurrency), 1)
validateInput(initialDomains, paths, markers)
rand.Seed(time.Now().UnixNano())
printInitialInfo(cfg, initialDomains, paths)
urlChan := make(chan string, urlBufferSize)
resultsChan := make(chan result.Result, cfg.Concurrency)
var client interface {
MakeRequest(url string) result.Result
}
if cfg.FastHTTP {
client = fasthttp.NewClient(cfg)
} else {
client = http.NewClient(cfg)
}
var processedCount int64
var totalURLs int64
go generateURLs(initialDomains, paths, cfg, urlChan, &totalURLs)
var wg sync.WaitGroup
for i := 0; i < cfg.Concurrency; i++ {
wg.Add(1)
go worker(urlChan, resultsChan, &wg, client, &processedCount, limiter)
}
done := make(chan bool)
go trackProgress(&processedCount, &totalURLs, done)
go func() {
wg.Wait()
close(resultsChan)
done <- true
}()
for res := range resultsChan {
result.ProcessResult(res, cfg, markers)
}
color.Green("\n[✔] Scan completed.")
}
func validateInput(initialDomains, paths, markers []string) {
if len(initialDomains) == 0 {
color.Red("[✘] Error: The domain list is empty. Please provide at least one domain.")
os.Exit(1)
}
if len(paths) == 0 {
color.Red("[✘] Error: The path list is empty. Please provide at least one path.")
os.Exit(1)
}
if len(markers) == 0 {
color.Yellow("[!] Warning: The marker list is empty. The scan will just use the size filter which might not be very useful.")
}
}
func printInitialInfo(cfg config.Config, initialDomains, paths []string) {
color.Cyan("[i] Scanning %d domains with %d paths", len(initialDomains), len(paths))
color.Cyan("[i] Minimum file size to detect: %d bytes", cfg.MinContentSize)
color.Cyan("[i] Filtering for HTTP status code: %s", cfg.HTTPStatusCodes)
if len(cfg.ExtraHeaders) > 0 {
color.Cyan("[i] Using extra headers:")
for key, value := range cfg.ExtraHeaders {
color.Cyan(" %s: %s", key, value)
}
}
}
func generateURLs(initialDomains, paths []string, cfg config.Config, urlChan chan<- string, totalURLs *int64) {
defer close(urlChan)
for _, d := range initialDomains {
domainURLs, _ := domain.GenerateURLs([]string{d}, paths, &cfg)
atomic.AddInt64(totalURLs, int64(len(domainURLs)))
for _, url := range domainURLs {
urlChan <- url
}
}
}
func worker(urls <-chan string, results chan<- result.Result, wg *sync.WaitGroup, client interface {
MakeRequest(url string) result.Result
}, processedCount *int64, limiter *rate.Limiter) {
defer wg.Done()
for url := range urls {
err := limiter.Wait(context.Background())
if err != nil {
continue
}
res := client.MakeRequest(url)
atomic.AddInt64(processedCount, 1)
results <- res
}
}
func trackProgress(processedCount, totalURLs *int64, done chan bool) {
start := time.Now()
lastProcessed := int64(0)
lastUpdate := start
for {
select {
case <-done:
return
default:
now := time.Now()
elapsed := now.Sub(start)
currentProcessed := atomic.LoadInt64(processedCount)
total := atomic.LoadInt64(totalURLs)
// Calculate RPS
intervalElapsed := now.Sub(lastUpdate)
intervalProcessed := currentProcessed - lastProcessed
rps := float64(intervalProcessed) / intervalElapsed.Seconds()
if total > 0 {
percentage := float64(currentProcessed) / float64(total) * 100
estimatedTotal := float64(elapsed) / (float64(currentProcessed) / float64(total))
remainingTime := time.Duration(estimatedTotal - float64(elapsed))
fmt.Printf("\r%-100s", "")
fmt.Printf("\rProgress: %.2f%% (%d/%d) | RPS: %.2f | Elapsed: %s | ETA: %s",
percentage, currentProcessed, total, rps,
elapsed.Round(time.Second), remainingTime.Round(time.Second))
} else {
fmt.Printf("\r%-100s", "")
fmt.Printf("\rProcessed: %d | RPS: %.2f | Elapsed: %s",
currentProcessed, rps, elapsed.Round(time.Second))
}
lastProcessed = currentProcessed
lastUpdate = now
time.Sleep(time.Second)
}
}
}