refactor(runner): adjust max-host-error if gt concurrency (#5633)

* refactor(common): use `ParseRequestURI` instead when `NormalizeCacheValue`

also it exports the method

Signed-off-by: Dwi Siswanto <git@dw1.io>

* refactor(runner): adjust `max-host-error` if gt `concurrency`

Signed-off-by: Dwi Siswanto <git@dw1.io>

* fix lint

* chore(runner): expose adjusted `max-host-error` value

Signed-off-by: Dwi Siswanto <git@dw1.io>

---------

Signed-off-by: Dwi Siswanto <git@dw1.io>
Co-authored-by: Doğan Can Bakır <dogancanbakir@protonmail.com>
This commit is contained in:
Dwi Siswanto 2024-09-23 17:27:30 +07:00 committed by GitHub
parent a118daa375
commit 9983d7415c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 19 deletions

View File

@ -501,8 +501,17 @@ func (r *Runner) RunEnumeration() error {
} }
if r.options.ShouldUseHostError() { if r.options.ShouldUseHostError() {
cache := hosterrorscache.New(r.options.MaxHostError, hosterrorscache.DefaultMaxHostsCount, r.options.TrackError) maxHostError := r.options.MaxHostError
if r.options.TemplateThreads > maxHostError {
gologger.Print().Msgf("[%v] The concurrency value is higher than max-host-error", r.colorizer.BrightYellow("WRN"))
gologger.Info().Msgf("Adjusting max-host-error to the concurrency value: %d", r.options.TemplateThreads)
maxHostError = r.options.TemplateThreads
}
cache := hosterrorscache.New(maxHostError, hosterrorscache.DefaultMaxHostsCount, r.options.TrackError)
cache.SetVerbose(r.options.Verbose) cache.SetVerbose(r.options.Verbose)
r.hostErrors = cache r.hostErrors = cache
executorOpts.HostErrorsCache = cache executorOpts.HostErrorsCache = cache
} }

View File

@ -75,24 +75,34 @@ func (c *Cache) Close() {
c.failedTargets.Purge() c.failedTargets.Purge()
} }
func (c *Cache) normalizeCacheValue(value string) string { // NormalizeCacheValue processes the input value and returns a normalized cache
finalValue := value // value.
if strings.HasPrefix(value, "http") { func (c *Cache) NormalizeCacheValue(value string) string {
if parsed, err := url.Parse(value); err == nil { var normalizedValue string = value
hostname := parsed.Host
finalPort := parsed.Port() u, err := url.ParseRequestURI(value)
if finalPort == "" { if err != nil || u.Host == "" {
if parsed.Scheme == "https" { u, err2 := url.ParseRequestURI("https://" + value)
finalPort = "443" if err2 != nil {
} else { return normalizedValue
finalPort = "80" }
}
hostname = net.JoinHostPort(parsed.Host, finalPort) normalizedValue = u.Host
} else {
port := u.Port()
if port == "" {
switch u.Scheme {
case "https":
normalizedValue = net.JoinHostPort(u.Host, "443")
case "http":
normalizedValue = net.JoinHostPort(u.Host, "80")
} }
finalValue = hostname } else {
normalizedValue = u.Host
} }
} }
return finalValue
return normalizedValue
} }
// ErrUnresponsiveHost is returned when a host is unresponsive // ErrUnresponsiveHost is returned when a host is unresponsive
@ -166,7 +176,7 @@ func (c *Cache) GetKeyFromContext(ctx *contextargs.Context, err error) string {
address = tmp.String() address = tmp.String()
} }
} }
finalValue := c.normalizeCacheValue(address) finalValue := c.NormalizeCacheValue(address)
return finalValue return finalValue
} }

View File

@ -109,7 +109,7 @@ func TestCacheMarkFailedConcurrent(t *testing.T) {
// the cache is not atomic during items creation, so we pre-create them with counter to zero // the cache is not atomic during items creation, so we pre-create them with counter to zero
for _, test := range tests { for _, test := range tests {
normalizedValue := cache.normalizeCacheValue(test.host) normalizedValue := cache.NormalizeCacheValue(test.host)
newItem := &cacheItem{errors: atomic.Int32{}} newItem := &cacheItem{errors: atomic.Int32{}}
newItem.errors.Store(0) newItem.errors.Store(0)
_ = cache.failedTargets.Set(normalizedValue, newItem) _ = cache.failedTargets.Set(normalizedValue, newItem)
@ -131,7 +131,7 @@ func TestCacheMarkFailedConcurrent(t *testing.T) {
for _, test := range tests { for _, test := range tests {
require.True(t, cache.Check(newCtxArgs(test.host))) require.True(t, cache.Check(newCtxArgs(test.host)))
normalizedCacheValue := cache.normalizeCacheValue(test.host) normalizedCacheValue := cache.NormalizeCacheValue(test.host)
failedTarget, err := cache.failedTargets.Get(normalizedCacheValue) failedTarget, err := cache.failedTargets.Get(normalizedCacheValue)
require.Nil(t, err) require.Nil(t, err)
require.NotNil(t, failedTarget) require.NotNil(t, failedTarget)