Skip to content

Commit b948392

Browse files
authored
Merge pull request #185 from edoardottt/devel
Add ratelimit option
2 parents 25415ab + cfa1138 commit b948392

File tree

10 files changed

+48
-4
lines changed

10 files changed

+48
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ lint:
1616
@golangci-lint run
1717

1818
linux:
19-
@go build -o csprecon ./main.go
19+
@go build -o csprecon ./cmd/csprecon
2020
@sudo mv csprecon /usr/local/bin/
2121
@echo "Done."
2222

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ Install 📡
4141

4242
### Using Snap
4343

44-
```bash
44+
```console
4545
sudo snap install csprecon
4646
```
4747

4848
### Using Go
4949

50-
```
50+
```console
5151
go install github.com/edoardottt/csprecon/cmd/csprecon@latest
5252
```
5353

@@ -68,6 +68,7 @@ CONFIGURATIONS:
6868
-d, -domain string[] Filter results belonging to these domains (comma separated)
6969
-c, -concurrency int Concurrency level (default 50)
7070
-t, -timeout int Connection timeout in seconds (default 10)
71+
-rl, -rate-limit int Set a rate limit (per second)
7172

7273
OUTPUT:
7374
-o, -output string File to write output results
@@ -110,6 +111,12 @@ Grab all possible results from single CIDR
110111
csprecon -u 192.168.1.0/24 -cidr
111112
```
112113

114+
Set a rate limit of 10 requests per second
115+
116+
```bash
117+
cat targets.txt | csprecon -rl 10
118+
```
119+
113120
Changelog 📌
114121
-------
115122

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ require (
1212
github.com/stretchr/testify v1.8.4
1313
)
1414

15+
require github.com/benbjohnson/clock v1.3.0 // indirect
16+
1517
require (
1618
github.com/andybalholm/brotli v1.0.6 // indirect
1719
github.com/andybalholm/cascadia v1.3.2 // indirect
@@ -39,6 +41,7 @@ require (
3941
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
4042
github.com/ulikunitz/xz v0.5.11 // indirect
4143
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
44+
go.uber.org/ratelimit v0.3.0
4245
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
4346
golang.org/x/mod v0.13.0 // indirect
4447
golang.org/x/net v0.17.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3d
1010
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
1111
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
1212
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
13+
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
14+
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
1315
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08 h1:ox2F0PSMlrAAiAdknSRMDrAr8mfxPCfSZolH+/qQnyQ=
1416
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08/go.mod h1:pCxVEbcm3AMg7ejXyorUXi6HQCzOIBf7zEDVPtw0/U4=
1517
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -81,6 +83,8 @@ github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0o
8183
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
8284
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
8385
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
86+
go.uber.org/ratelimit v0.3.0 h1:IdZd9wqvFXnvLvSEBo0KPcGfkoBGNkpTHlrE3Rcjkjw=
87+
go.uber.org/ratelimit v0.3.0/go.mod h1:So5LG7CV1zWpY1sHe+DXTJqQvOx+FFPFaAs2SnoyBaI=
8488
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8589
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
8690
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=

pkg/csprecon/csprecon.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func execute(r *Runner) {
127127
defer r.InWg.Done()
128128

129129
dregex := CompileRegex(DomainRegex)
130+
rl := rateLimiter(r)
130131

131132
for i := 0; i < r.Options.Concurrency; i++ {
132133
r.InWg.Add(1)
@@ -144,6 +145,8 @@ func execute(r *Runner) {
144145
return
145146
}
146147

148+
rl.Take()
149+
147150
client := customClient(r.Options.Timeout)
148151

149152
result, err := CheckCSP(targetURL, r.UserAgent, dregex, client)
File renamed without changes.
File renamed without changes.

pkg/csprecon/ratelimit.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
csprecon - Discover new target domains using Content Security Policy
3+
4+
This repository is under MIT License https://github.com/edoardottt/csprecon/blob/main/LICENSE
5+
*/
6+
7+
package csprecon
8+
9+
import "go.uber.org/ratelimit"
10+
11+
func rateLimiter(r *Runner) ratelimit.Limiter {
12+
var ratelimiter ratelimit.Limiter
13+
if r.Options.RateLimit > 0 {
14+
ratelimiter = ratelimit.New(r.Options.RateLimit)
15+
} else {
16+
ratelimiter = ratelimit.NewUnlimited()
17+
}
18+
19+
return ratelimiter
20+
}

pkg/input/check.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ func (options *Options) validateOptions() error {
3131
}
3232

3333
if options.Concurrency <= 0 {
34-
return fmt.Errorf("%w", ErrNegativeValue)
34+
return fmt.Errorf("concurrency: %w", ErrNegativeValue)
35+
}
36+
37+
if options.RateLimit != 0 && options.RateLimit <= 0 {
38+
return fmt.Errorf("rate limit: %w", ErrNegativeValue)
3539
}
3640

3741
return nil

pkg/input/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
const (
2121
DefaultTimeout = 10
2222
DefaultConcurrency = 50
23+
DefaultRateLimit = 0
2324
)
2425

2526
type Options struct {
@@ -33,6 +34,7 @@ type Options struct {
3334
Concurrency int
3435
Timeout int
3536
Cidr bool
37+
RateLimit int
3638
}
3739

3840
// configureOutput configures the output on the screen.
@@ -62,6 +64,7 @@ func ParseOptions() *Options {
6264
flagSet.StringSliceVarP(&options.Domain, "domain", "d", nil, `Filter results belonging to these domains (comma separated)`, goflags.CommaSeparatedStringSliceOptions),
6365
flagSet.IntVarP(&options.Concurrency, "concurrency", "c", DefaultConcurrency, `Concurrency level`),
6466
flagSet.IntVarP(&options.Timeout, "timeout", "t", DefaultTimeout, `Connection timeout in seconds`),
67+
flagSet.IntVarP(&options.RateLimit, "rate-limit", "rl", DefaultRateLimit, `Set a rate limit (per second)`),
6568
)
6669

6770
// Output

0 commit comments

Comments
 (0)