60 lines
1.4 KiB
Go
Raw Normal View History

2020-04-06 00:05:01 +05:30
package extractors
2020-12-24 12:13:18 +05:30
import "github.com/projectdiscovery/nuclei/v2/pkg/types"
2020-07-16 10:32:00 +02:00
2020-12-24 12:13:18 +05:30
// Extract extracts data from an output structure based on user options
func (e *Extractor) Extract(data map[string]interface{}) map[string]struct{} {
part, ok := data[e.Part]
if !ok {
return nil
2020-04-06 00:05:01 +05:30
}
2020-12-24 12:13:18 +05:30
partString := types.ToString(part)
2020-07-15 00:47:01 +02:00
switch e.extractorType {
case RegexExtractor:
2020-12-24 12:13:18 +05:30
return e.extractRegex(partString)
2020-07-15 00:47:01 +02:00
case KValExtractor:
2020-12-24 12:13:18 +05:30
return e.extractKVal(data)
2020-07-15 00:47:01 +02:00
}
return nil
2020-04-22 22:45:02 +02:00
}
2020-04-06 00:05:01 +05:30
// extractRegex extracts text from a corpus and returns it
2020-04-27 23:34:08 +05:30
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-12-24 12:13:18 +05:30
2020-04-27 23:34:08 +05:30
for _, match := range matches {
2020-12-24 12:13:18 +05:30
if len(match) < groupPlusOne {
continue
}
2020-12-24 12:13:18 +05:30
matchString := match[e.RegexGroup]
2020-12-24 12:13:18 +05:30
if _, ok := results[matchString]; !ok {
results[matchString] = struct{}{}
}
2020-07-16 10:32:00 +02:00
}
}
return results
}
2020-12-24 12:13:18 +05:30
// extractKVal extracts key value pairs from a data map
func (e *Extractor) extractKVal(data map[string]interface{}) map[string]struct{} {
2020-07-16 10:32:00 +02:00
results := make(map[string]struct{})
2020-07-16 12:58:56 +02:00
for _, k := range e.KVal {
2020-12-24 12:13:18 +05:30
item, ok := data[k]
if !ok {
continue
}
itemString := types.ToString(item)
if _, ok := results[itemString]; !ok {
results[itemString] = struct{}{}
2020-07-16 10:32:00 +02:00
}
}
return results
}