nuclei/v2/pkg/operators/extractors/extract.go

96 lines
2.1 KiB
Go
Raw Normal View History

2020-04-05 18:35:01 +00:00
package extractors
2020-07-16 08:32:00 +00:00
import (
"net/http"
"github.com/miekg/dns"
)
2020-04-05 18:35:01 +00:00
// Extract extracts response from the parts of request using a regex
2020-07-16 08:32:00 +00:00
func (e *Extractor) Extract(resp *http.Response, body, headers string) map[string]struct{} {
2020-07-14 22:47:01 +00:00
switch e.extractorType {
case RegexExtractor:
if e.part == BodyPart {
return e.extractRegex(body)
} else if e.part == HeaderPart {
return e.extractRegex(headers)
} else {
matches := e.extractRegex(headers)
if len(matches) > 0 {
return matches
}
return e.extractRegex(body)
2020-04-05 18:35:01 +00:00
}
2020-07-14 22:47:01 +00:00
case KValExtractor:
2020-07-16 08:32:00 +00:00
if e.part == HeaderPart {
return e.extractKVal(resp)
}
matches := e.extractKVal(resp)
if len(matches) > 0 {
return matches
}
return e.extractCookieKVal(resp)
2020-04-05 18:35:01 +00:00
}
2020-07-14 22:47:01 +00:00
return nil
2020-04-05 18:35:01 +00:00
}
2020-04-22 20:45:02 +00:00
// ExtractDNS extracts response from dns message using a regex
// nolint:interfacer // dns.Msg is out of current scope
2020-07-16 08:32:00 +00:00
func (e *Extractor) ExtractDNS(msg *dns.Msg) map[string]struct{} {
2020-07-14 22:47:01 +00:00
switch e.extractorType {
case RegexExtractor:
2020-07-16 08:32:00 +00:00
return e.extractRegex(msg.String())
2020-07-14 22:47:01 +00:00
case KValExtractor:
}
return nil
2020-04-22 20:45:02 +00:00
}
2020-04-05 18:35:01 +00:00
// extractRegex extracts text from a corpus and returns it
2020-04-27 18:04:08 +00:00
func (e *Extractor) extractRegex(corpus string) map[string]struct{} {
results := make(map[string]struct{})
groupPlusOne := e.RegexGroup + 1
for _, regex := range e.regexCompiled {
matches := regex.FindAllStringSubmatch(corpus, -1)
2020-04-27 18:04:08 +00:00
for _, match := range matches {
if len(match) >= groupPlusOne {
results[match[e.RegexGroup]] = struct{}{}
}
2020-04-27 18:04:08 +00:00
}
}
return results
2020-04-05 18:35:01 +00:00
}
2020-07-16 08:32:00 +00:00
// extractKVal extracts text from http response
func (e *Extractor) extractKVal(r *http.Response) map[string]struct{} {
results := make(map[string]struct{})
2020-07-16 08:32:00 +00:00
for _, k := range e.KVal {
for _, v := range r.Header.Values(k) {
results[v] = struct{}{}
}
}
2020-07-16 08:32:00 +00:00
return results
}
2020-07-16 10:58:56 +00:00
// extractCookieKVal extracts text from cookies
func (e *Extractor) extractCookieKVal(r *http.Response) map[string]struct{} {
2020-07-16 08:32:00 +00:00
results := make(map[string]struct{})
2020-07-16 10:58:56 +00:00
for _, k := range e.KVal {
for _, cookie := range r.Cookies() {
if cookie.Name == k {
results[cookie.Value] = struct{}{}
2020-07-16 08:32:00 +00:00
}
}
}
2020-07-16 08:32:00 +00:00
return results
}