2022-10-03 12:12:20 +02:00
|
|
|
package contextargs
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http/cookiejar"
|
2023-09-12 04:34:15 +05:30
|
|
|
"strings"
|
2022-10-03 12:12:20 +02:00
|
|
|
|
2023-07-24 16:50:28 +02:00
|
|
|
mapsutil "github.com/projectdiscovery/utils/maps"
|
2023-09-12 04:34:15 +05:30
|
|
|
sliceutil "github.com/projectdiscovery/utils/slice"
|
|
|
|
|
stringsutil "github.com/projectdiscovery/utils/strings"
|
|
|
|
|
urlutil "github.com/projectdiscovery/utils/url"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
// reservedPorts contains list of reserved ports for non-network requests in nuclei
|
|
|
|
|
reservedPorts = []string{"80", "443", "8080", "8443", "8081", "53"}
|
2022-10-03 12:12:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Context implements a shared context struct to share information across multiple templates within a workflow
|
|
|
|
|
type Context struct {
|
2022-11-09 14:18:56 +01:00
|
|
|
// Meta is the target for the executor
|
|
|
|
|
MetaInput *MetaInput
|
|
|
|
|
|
2022-10-03 12:12:20 +02:00
|
|
|
// CookieJar shared within workflow's http templates
|
|
|
|
|
CookieJar *cookiejar.Jar
|
|
|
|
|
|
|
|
|
|
// Args is a workflow shared key-value store
|
2023-07-24 16:50:28 +02:00
|
|
|
args *mapsutil.SyncLockMap[string, interface{}]
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new contextargs instance
|
|
|
|
|
func New() *Context {
|
2022-11-09 14:18:56 +01:00
|
|
|
return &Context{MetaInput: &MetaInput{}}
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new contextargs instance with input string
|
|
|
|
|
func NewWithInput(input string) *Context {
|
2022-11-09 14:18:56 +01:00
|
|
|
return &Context{MetaInput: &MetaInput{Input: input}}
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctx *Context) initialize() {
|
2023-07-24 16:50:28 +02:00
|
|
|
ctx.args = &mapsutil.SyncLockMap[string, interface{}]{Map: mapsutil.Map[string, interface{}]{}}
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the specific key-value pair
|
|
|
|
|
func (ctx *Context) Set(key string, value interface{}) {
|
|
|
|
|
if !ctx.isInitialized() {
|
|
|
|
|
ctx.initialize()
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 16:50:28 +02:00
|
|
|
_ = ctx.args.Set(key, value)
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctx *Context) isInitialized() bool {
|
|
|
|
|
return ctx.args != nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctx *Context) hasArgs() bool {
|
|
|
|
|
return ctx.isInitialized() && !ctx.args.IsEmpty()
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 04:34:15 +05:30
|
|
|
// UseNetworkPort updates input with required/default network port for that template
|
|
|
|
|
// but is ignored if input/target contains non-http ports like 80,8080,8081 etc
|
|
|
|
|
func (ctx *Context) UseNetworkPort(port string, excludePorts string) error {
|
|
|
|
|
ignorePorts := reservedPorts
|
|
|
|
|
if excludePorts != "" {
|
|
|
|
|
// TODO: add support for service names like http,https,ssh etc once https://github.com/projectdiscovery/netdb is ready
|
|
|
|
|
ignorePorts = sliceutil.Dedupe(strings.Split(excludePorts, ","))
|
|
|
|
|
}
|
|
|
|
|
if port == "" {
|
|
|
|
|
// if template does not contain port, do nothing
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
target, err := urlutil.Parse(ctx.MetaInput.Input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
inputPort := target.Port()
|
|
|
|
|
if inputPort == "" || stringsutil.EqualFoldAny(inputPort, ignorePorts...) {
|
|
|
|
|
// replace port with networkPort
|
|
|
|
|
target.UpdatePort(port)
|
|
|
|
|
ctx.MetaInput.Input = target.Host
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 12:12:20 +02:00
|
|
|
// Get the value with specific key if exists
|
|
|
|
|
func (ctx *Context) Get(key string) (interface{}, bool) {
|
|
|
|
|
if !ctx.hasArgs() {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 16:50:28 +02:00
|
|
|
return ctx.args.Get(key)
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 16:50:28 +02:00
|
|
|
func (ctx *Context) GetAll() *mapsutil.SyncLockMap[string, interface{}] {
|
2022-10-03 12:12:20 +02:00
|
|
|
if !ctx.hasArgs() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 16:50:28 +02:00
|
|
|
return ctx.args.Clone()
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctx *Context) ForEach(f func(string, interface{})) {
|
2023-07-24 16:50:28 +02:00
|
|
|
_ = ctx.args.Iterate(func(k string, v interface{}) error {
|
2022-10-03 12:12:20 +02:00
|
|
|
f(k, v)
|
2023-07-24 16:50:28 +02:00
|
|
|
return nil
|
|
|
|
|
})
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Has check if the key exists
|
|
|
|
|
func (ctx *Context) Has(key string) bool {
|
2023-07-24 16:50:28 +02:00
|
|
|
return ctx.hasArgs() && ctx.args.Has(key)
|
2022-10-03 12:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctx *Context) HasArgs() bool {
|
|
|
|
|
return ctx.hasArgs()
|
|
|
|
|
}
|
2022-11-09 14:18:56 +01:00
|
|
|
|
|
|
|
|
func (ctx *Context) Clone() *Context {
|
|
|
|
|
newCtx := &Context{
|
|
|
|
|
MetaInput: ctx.MetaInput.Clone(),
|
|
|
|
|
args: ctx.args,
|
|
|
|
|
CookieJar: ctx.CookieJar,
|
|
|
|
|
}
|
|
|
|
|
return newCtx
|
|
|
|
|
}
|