Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/common/helpers/eventcreator/eventcreator.go
2073 views
1
package eventcreator
2
3
import (
4
"github.com/projectdiscovery/gologger"
5
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
6
"github.com/projectdiscovery/nuclei/v3/pkg/output"
7
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
8
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/utils/vardump"
9
"golang.org/x/text/cases"
10
"golang.org/x/text/language"
11
)
12
13
// CreateEvent wraps the outputEvent with the result of the operators defined on the request
14
func CreateEvent(request protocols.Request, outputEvent output.InternalEvent, isResponseDebug bool) *output.InternalWrappedEvent {
15
return CreateEventWithAdditionalOptions(request, outputEvent, isResponseDebug, nil)
16
}
17
18
// CreateEventWithAdditionalOptions wraps the outputEvent with the result of the operators defined on the request
19
// and enables extending the resulting event with additional attributes or values.
20
func CreateEventWithAdditionalOptions(request protocols.Request, outputEvent output.InternalEvent, isResponseDebug bool,
21
addAdditionalOptions func(internalWrappedEvent *output.InternalWrappedEvent)) *output.InternalWrappedEvent {
22
event := &output.InternalWrappedEvent{InternalEvent: outputEvent}
23
24
// Dump response variables if ran in debug mode
25
if vardump.EnableVarDump {
26
protoName := cases.Title(language.English).String(request.Type().String())
27
gologger.Debug().Msgf("%v Protocol response variables: %s\n", protoName, vardump.DumpVariables(outputEvent))
28
}
29
for _, compiledOperator := range request.GetCompiledOperators() {
30
if compiledOperator != nil {
31
result, ok := compiledOperator.Execute(outputEvent, request.Match, request.Extract, isResponseDebug)
32
if ok && result != nil {
33
// if result has both extracted values and dynamic values, put dynamic values in data
34
// and remove dynamic values to avoid skipping legitimate event
35
if (len(result.Extracts) > 0 || len(result.OutputExtracts) > 0) && len(result.DynamicValues) > 0 {
36
for k, v := range result.DynamicValues {
37
event.InternalEvent[k] = v
38
}
39
result.DynamicValues = nil
40
}
41
event.OperatorsResult = result
42
if addAdditionalOptions != nil {
43
addAdditionalOptions(event)
44
}
45
event.Results = append(event.Results, request.MakeResultEvent(event)...)
46
}
47
}
48
}
49
return event
50
}
51
52
func CreateEventWithOperatorResults(request protocols.Request, internalEvent output.InternalEvent, operatorResult *operators.Result) *output.InternalWrappedEvent {
53
event := &output.InternalWrappedEvent{InternalEvent: internalEvent}
54
event.OperatorsResult = operatorResult
55
event.Results = append(event.Results, request.MakeResultEvent(event)...)
56
return event
57
}
58
59