-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added initial live DAST server implementation
- Loading branch information
1 parent
49811d2
commit 0db2332
Showing
12 changed files
with
589 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package server | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"net/url" | ||
"sort" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/projectdiscovery/nuclei/v3/pkg/input/types" | ||
mapsutil "github.com/projectdiscovery/utils/maps" | ||
) | ||
|
||
var dynamicHeaders = map[string]bool{ | ||
"date": true, | ||
"if-modified-since": true, | ||
"if-unmodified-since": true, | ||
"cache-control": true, | ||
"if-none-match": true, | ||
"if-match": true, | ||
"authorization": true, | ||
"cookie": true, | ||
"x-csrf-token": true, | ||
"content-length": true, | ||
"content-md5": true, | ||
"host": true, | ||
"x-request-id": true, | ||
"x-correlation-id": true, | ||
"user-agent": true, | ||
"referer": true, | ||
} | ||
|
||
type requestDeduplicator struct { | ||
hashes map[string]struct{} | ||
lock *sync.RWMutex | ||
} | ||
|
||
func newRequestDeduplicator() *requestDeduplicator { | ||
return &requestDeduplicator{ | ||
hashes: make(map[string]struct{}), | ||
lock: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
func (r *requestDeduplicator) isDuplicate(req *types.RequestResponse) bool { | ||
hash, err := hashRequest(req) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
r.lock.RLock() | ||
_, ok := r.hashes[hash] | ||
r.lock.RUnlock() | ||
if ok { | ||
return true | ||
} | ||
|
||
r.lock.Lock() | ||
r.hashes[hash] = struct{}{} | ||
r.lock.Unlock() | ||
return false | ||
} | ||
|
||
func hashRequest(req *types.RequestResponse) (string, error) { | ||
normalizedURL, err := normalizeURL(req.URL.URL) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var hashContent strings.Builder | ||
hashContent.WriteString(req.Request.Method) | ||
hashContent.WriteString(normalizedURL) | ||
|
||
headers := sortedNonDynamicHeaders(req.Request.Headers) | ||
for _, header := range headers { | ||
hashContent.WriteString(header.Key) | ||
hashContent.WriteString(header.Value) | ||
} | ||
|
||
if len(req.Request.Body) > 0 { | ||
hashContent.Write([]byte(req.Request.Body)) | ||
} | ||
|
||
// Calculate the SHA256 hash | ||
hash := sha256.Sum256([]byte(hashContent.String())) | ||
return hex.EncodeToString(hash[:]), nil | ||
} | ||
|
||
func normalizeURL(u *url.URL) (string, error) { | ||
query := u.Query() | ||
sortedQuery := make(url.Values) | ||
for k, v := range query { | ||
sort.Strings(v) | ||
sortedQuery[k] = v | ||
} | ||
u.RawQuery = sortedQuery.Encode() | ||
|
||
if u.Path == "" { | ||
u.Path = "/" | ||
} | ||
return u.String(), nil | ||
} | ||
|
||
type header struct { | ||
Key string | ||
Value string | ||
} | ||
|
||
func sortedNonDynamicHeaders(headers mapsutil.OrderedMap[string, string]) []header { | ||
var result []header | ||
headers.Iterate(func(k, v string) bool { | ||
if !dynamicHeaders[strings.ToLower(k)] { | ||
result = append(result, header{Key: k, Value: v}) | ||
} | ||
return true | ||
}) | ||
sort.Slice(result, func(i, j int) bool { | ||
return result[i].Key < result[j].Key | ||
}) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package server | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/projectdiscovery/nuclei/v3/pkg/output" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// proxifyRequest is a request for proxify | ||
type proxifyRequest struct { | ||
URL string `json:"url"` | ||
Request struct { | ||
Header map[string]string `json:"header"` | ||
Body string `json:"body"` | ||
Raw string `json:"raw"` | ||
} `json:"request"` | ||
} | ||
|
||
func runNucleiWithFuzzingInput(target PostReuestsHandlerRequest, templates []string) ([]output.ResultEvent, error) { | ||
cmd := exec.Command("nuclei") | ||
|
||
tempFile, err := os.CreateTemp("", "nuclei-fuzz-*.yaml") | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating temp file: %s", err) | ||
} | ||
defer os.Remove(tempFile.Name()) | ||
|
||
payload := proxifyRequest{ | ||
URL: target.URL, | ||
Request: struct { | ||
Header map[string]string `json:"header"` | ||
Body string `json:"body"` | ||
Raw string `json:"raw"` | ||
}{ | ||
Raw: target.RawHTTP, | ||
}, | ||
} | ||
|
||
marshalledYaml, err := yaml.Marshal(payload) | ||
if err != nil { | ||
return nil, fmt.Errorf("error marshalling yaml: %s", err) | ||
} | ||
|
||
if _, err := tempFile.Write(marshalledYaml); err != nil { | ||
return nil, fmt.Errorf("error writing to temp file: %s", err) | ||
} | ||
|
||
argsArray := []string{ | ||
"-duc", | ||
"-dast", | ||
"-silent", | ||
"-no-color", | ||
"-jsonl", | ||
} | ||
for _, template := range templates { | ||
argsArray = append(argsArray, "-t", template) | ||
} | ||
argsArray = append(argsArray, "-l", tempFile.Name()) | ||
argsArray = append(argsArray, "-im=yaml") | ||
cmd.Args = append(cmd.Args, argsArray...) | ||
|
||
data, err := cmd.Output() | ||
if err != nil { | ||
return nil, fmt.Errorf("error running nuclei: %w", err) | ||
} | ||
|
||
var nucleiResult []output.ResultEvent | ||
decoder := json.NewDecoder(bytes.NewReader(data)) | ||
for { | ||
var result output.ResultEvent | ||
if err := decoder.Decode(&result); err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return nil, fmt.Errorf("error decoding nuclei output: %w", err) | ||
} | ||
// Filter results with a valid template-id | ||
if result.TemplateID != "" { | ||
nucleiResult = append(nucleiResult, result) | ||
} | ||
} | ||
|
||
return nucleiResult, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/projectdiscovery/gologger" | ||
"github.com/projectdiscovery/nuclei/v3/pkg/input/types" | ||
) | ||
|
||
func (s *DASTServer) setupWorkers() { | ||
go s.tasksConsumer() | ||
} | ||
|
||
func (s *DASTServer) tasksConsumer() { | ||
for req := range s.fuzzRequests { | ||
parsedReq, err := parseRawRequest(req) | ||
if err != nil { | ||
gologger.Warning().Msgf("Could not parse raw request: %s\n", err) | ||
continue | ||
} | ||
|
||
inScope, err := s.scopeManager.Validate(parsedReq.URL.URL, "") | ||
if err != nil { | ||
gologger.Warning().Msgf("Could not validate scope: %s\n", err) | ||
continue | ||
} | ||
if !inScope { | ||
gologger.Warning().Msgf("Request is out of scope: %s %s\n", parsedReq.Request.Method, parsedReq.URL.String()) | ||
continue | ||
} | ||
|
||
if s.deduplicator.isDuplicate(parsedReq) { | ||
gologger.Warning().Msgf("Duplicate request detected: %s %s\n", parsedReq.Request.Method, parsedReq.URL.String()) | ||
continue | ||
} | ||
|
||
gologger.Verbose().Msgf("Fuzzing request: %s %s\n", parsedReq.Request.Method, parsedReq.URL.String()) | ||
s.tasksPool.Go(func() { | ||
s.fuzzRequest(req) | ||
}) | ||
} | ||
} | ||
|
||
func (s *DASTServer) fuzzRequest(req PostReuestsHandlerRequest) { | ||
results, err := runNucleiWithFuzzingInput(req, s.options.Templates) | ||
if err != nil { | ||
gologger.Warning().Msgf("Could not run nuclei: %s\n", err) | ||
return | ||
} | ||
|
||
for _, result := range results { | ||
if err := s.options.OutputWriter.Write(&result); err != nil { | ||
gologger.Error().Msgf("Could not write result: %s\n", err) | ||
} | ||
} | ||
} | ||
|
||
func parseRawRequest(req PostReuestsHandlerRequest) (*types.RequestResponse, error) { | ||
parsedReq, err := types.ParseRawRequestWithURL(req.RawHTTP, req.URL) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not parse raw HTTP") | ||
} | ||
return parsedReq, nil | ||
} |
Oops, something went wrong.