79 lines
1.6 KiB
Go
Raw Normal View History

2020-04-06 00:05:01 +05:30
package extractors
2021-01-12 11:21:32 +05:30
import (
2021-07-31 22:49:23 +02:00
"encoding/json"
2021-01-12 11:21:32 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/types"
)
2020-07-16 10:32:00 +02:00
2020-12-24 20:47:41 +05:30
// ExtractRegex extracts text from a corpus and returns it
func (e *Extractor) ExtractRegex(corpus string) map[string]struct{} {
2020-04-27 23:34:08 +05:30
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 20:47:41 +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
}
2021-07-31 22:49:23 +02:00
// ExtractJson extracts key value pairs from a data map
func (e *Extractor) ExtractJson(corpus string) map[string]struct{} {
results := make(map[string]struct{})
var jsonObj interface{}
err := json.Unmarshal([]byte(corpus), &jsonObj)
if err != nil {
return results
}
for _, k := range e.jsonCompiled {
iter := k.Run(jsonObj)
for {
v, ok := iter.Next()
if !ok {
break
}
if _, ok := v.(error); ok {
break
}
bytes, err := json.Marshal(v)
if err != nil {
break
}
results[string(bytes)] = struct{}{}
}
}
return results
}