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-08-01 14:42:04 +02:00
|
|
|
|
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{})
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-09-17 12:01:51 +05:30
|
|
|
groupPlusOne := e.RegexGroup + 1
|
2020-04-06 00:44:45 +05:30
|
|
|
for _, regex := range e.regexCompiled {
|
2020-09-17 12:01:51 +05:30
|
|
|
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-09-17 12:01:51 +05:30
|
|
|
}
|
2020-12-24 12:13:18 +05:30
|
|
|
matchString := match[e.RegexGroup]
|
2020-08-25 23:24:31 +02:00
|
|
|
|
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-08-25 23:24:31 +02:00
|
|
|
|
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
|
|
|
|
2021-08-01 14:42:04 +02:00
|
|
|
// ExtractJSON extracts key value pairs from a data map
|
|
|
|
|
func (e *Extractor) ExtractJSON(corpus string) map[string]struct{} {
|
2021-07-31 22:49:23 +02:00
|
|
|
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
|
|
|
|
|
}
|
2021-07-31 23:48:14 +02:00
|
|
|
itemString := types.ToString(v)
|
|
|
|
|
if _, ok := results[itemString]; !ok {
|
|
|
|
|
results[itemString] = struct{}{}
|
2021-07-31 22:49:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return results
|
|
|
|
|
}
|