@@ -2,21 +2,38 @@ package extractors
22
33import (
44 "fmt"
5+ "regexp"
56 "strings"
67
78 "github.com/antchfx/htmlquery"
89 "github.com/antchfx/xmlquery"
10+ "github.com/itchyny/gojq"
911
12+ "github.com/projectdiscovery/gologger"
13+ "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/expressions"
1014 "github.com/projectdiscovery/nuclei/v3/pkg/types"
1115 "github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
1216)
1317
1418// ExtractRegex extracts text from a corpus and returns it
15- func (e * Extractor ) ExtractRegex (corpus string ) map [string ]struct {} {
19+ func (e * Extractor ) ExtractRegex (corpus string , data map [ string ] interface {} ) map [string ]struct {} {
1620 results := make (map [string ]struct {})
1721
1822 groupPlusOne := e .RegexGroup + 1
19- for _ , regex := range e .regexCompiled {
23+ for i , regex := range e .regexCompiled {
24+ if varErr := expressions .ContainsUnresolvedVariables (e .Regex [i ]); varErr != nil {
25+ regexStr , err := expressions .Evaluate (e .Regex [i ], data )
26+ if err != nil {
27+ gologger .Warning ().Msgf ("Could not evaluate expression: %s, error: %s" , e .Regex [i ], err .Error ())
28+ continue
29+ }
30+ regex , err = regexp .Compile (regexStr )
31+ if err != nil {
32+ gologger .Warning ().Msgf ("Could not compile regex: %s, error: %s" , regexStr , err .Error ())
33+ continue
34+ }
35+ }
36+
2037 // skip prefix short-circuit for case-insensitive patterns
2138 rstr := regex .String ()
2239 if ! strings .Contains (rstr , "(?i" ) {
@@ -138,7 +155,7 @@ func (e *Extractor) ExtractXML(corpus string) map[string]struct{} {
138155}
139156
140157// ExtractJSON extracts text from a corpus using JQ queries and returns it
141- func (e * Extractor ) ExtractJSON (corpus string ) map [string ]struct {} {
158+ func (e * Extractor ) ExtractJSON (corpus string , data map [ string ] interface {} ) map [string ]struct {} {
142159 results := make (map [string ]struct {})
143160
144161 var jsonObj interface {}
@@ -147,7 +164,25 @@ func (e *Extractor) ExtractJSON(corpus string) map[string]struct{} {
147164 return results
148165 }
149166
150- for _ , k := range e .jsonCompiled {
167+ for i , k := range e .jsonCompiled {
168+ if varErr := expressions .ContainsUnresolvedVariables (e .JSON [i ]); varErr != nil {
169+ jsonStr , err := expressions .Evaluate (e .JSON [i ], data )
170+ if err != nil {
171+ gologger .Warning ().Msgf ("Could not evaluate expression: %s, error: %s" , e .JSON [i ], err .Error ())
172+ continue
173+ }
174+ query , err := gojq .Parse (jsonStr )
175+ if err != nil {
176+ gologger .Warning ().Msgf ("Could not parse json: %s, error: %s" , jsonStr , err .Error ())
177+ continue
178+ }
179+ k , err = gojq .Compile (query )
180+ if err != nil {
181+ gologger .Warning ().Msgf ("Could not compile json: %s, error: %s" , jsonStr , err .Error ())
182+ continue
183+ }
184+ }
185+
151186 iter := k .Run (jsonObj )
152187 for {
153188 v , ok := iter .Next ()
0 commit comments