297 lines
7.3 KiB
Go
Raw Normal View History

package generators
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
2020-10-16 14:18:50 +02:00
"fmt"
"html"
2020-10-11 20:26:27 +02:00
"math"
"math/rand"
"net/url"
"regexp"
"strings"
2020-10-23 10:13:34 +02:00
"time"
2020-10-16 22:27:25 +02:00
"github.com/Knetic/govaluate"
2020-10-23 10:13:34 +02:00
"github.com/projectdiscovery/nuclei/v2/pkg/collaborator"
2020-10-16 13:27:02 +02:00
"github.com/spaolacci/murmur3"
)
2020-10-16 22:07:00 +02:00
const (
withCutSetArgsSize = 2
withMaxRandArgsSize = withCutSetArgsSize
withBaseRandArgsSize = 3
)
2020-10-11 20:26:27 +02:00
var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var numbers = "1234567890"
// HelperFunctions contains the dsl functions
func HelperFunctions() (functions map[string]govaluate.ExpressionFunction) {
functions = make(map[string]govaluate.ExpressionFunction)
// strings
functions["len"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
length := len(toString(args[0]))
return float64(length), nil
}
functions["toupper"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.ToUpper(toString(args[0])), nil
}
functions["tolower"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.ToLower(toString(args[0])), nil
}
functions["replace"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.ReplaceAll(toString(args[0]), toString(args[1]), toString(args[2])), nil
2020-10-16 22:27:25 +02:00
}
functions["replace_regex"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
compiled, err := regexp.Compile(toString(args[1]))
if err != nil {
return nil, err
}
2020-11-26 09:57:26 +01:00
return compiled.ReplaceAllString(toString(args[0]), toString(args[2])), nil
}
functions["trim"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.Trim(toString(args[0]), toString(args[2])), nil
}
functions["trimleft"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.TrimLeft(toString(args[0]), toString(args[1])), nil
}
functions["trimright"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.TrimRight(toString(args[0]), toString(args[1])), nil
}
functions["trimspace"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.TrimSpace(toString(args[0])), nil
}
functions["trimprefix"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.TrimPrefix(toString(args[0]), toString(args[1])), nil
}
functions["trimsuffix"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.TrimSuffix(toString(args[0]), toString(args[1])), nil
}
functions["reverse"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return reverseString(toString(args[0])), nil
}
// encoding
functions["base64"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
sEnc := base64.StdEncoding.EncodeToString([]byte(toString(args[0])))
return sEnc, nil
}
2020-10-16 13:27:02 +02:00
// python encodes to base64 with lines of 76 bytes terminated by new line "\n"
functions["base64_py"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
sEnc := base64.StdEncoding.EncodeToString([]byte(toString(args[0])))
2020-10-16 13:27:02 +02:00
return insertInto(sEnc, 76, '\n'), nil
}
2020-09-23 23:27:48 +02:00
functions["base64_decode"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return base64.StdEncoding.DecodeString(toString(args[0]))
2020-09-23 23:27:48 +02:00
}
functions["url_encode"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return url.PathEscape(toString(args[0])), nil
}
functions["url_decode"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return url.PathUnescape(toString(args[0]))
}
functions["hex_encode"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return hex.EncodeToString([]byte(toString(args[0]))), nil
}
functions["hex_decode"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
hx, _ := hex.DecodeString(toString(args[0]))
return string(hx), nil
}
functions["html_escape"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return html.EscapeString(toString(args[0])), nil
}
functions["html_unescape"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return html.UnescapeString(toString(args[0])), nil
}
// hashing
functions["md5"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
hash := md5.Sum([]byte(toString(args[0])))
return hex.EncodeToString(hash[:]), nil
}
functions["sha256"] = func(args ...interface{}) (interface{}, error) {
h := sha256.New()
2020-11-26 09:57:26 +01:00
_, err := h.Write([]byte(toString(args[0])))
if err != nil {
return nil, err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
functions["sha1"] = func(args ...interface{}) (interface{}, error) {
h := sha1.New()
2020-11-26 09:57:26 +01:00
_, err := h.Write([]byte(toString(args[0])))
if err != nil {
return nil, err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
2020-10-15 12:39:59 +02:00
functions["mmh3"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return fmt.Sprintf("%d", int32(murmur3.Sum32WithSeed([]byte(toString(args[0])), 0))), nil
2020-10-15 12:39:59 +02:00
}
// search
functions["contains"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
return strings.Contains(toString(args[0]), toString(args[1])), nil
}
functions["regex"] = func(args ...interface{}) (interface{}, error) {
2020-11-26 09:57:26 +01:00
compiled, err := regexp.Compile(toString(args[0]))
if err != nil {
return nil, err
}
2020-11-26 09:57:26 +01:00
return compiled.MatchString(toString(args[1])), nil
}
2020-10-11 20:26:27 +02:00
// random generators
functions["rand_char"] = func(args ...interface{}) (interface{}, error) {
chars := letters + numbers
bad := ""
if len(args) >= 1 {
2020-11-26 09:57:26 +01:00
chars = toString(args[0])
2020-10-11 20:26:27 +02:00
}
2020-10-16 22:07:00 +02:00
if len(args) >= withCutSetArgsSize {
2020-11-26 09:57:26 +01:00
bad = toString(args[1])
2020-10-11 20:26:27 +02:00
}
chars = TrimAll(chars, bad)
return chars[rand.Intn(len(chars))], nil
}
functions["rand_base"] = func(args ...interface{}) (interface{}, error) {
l := 0
bad := ""
base := letters + numbers
if len(args) >= 1 {
l = args[0].(int)
}
2020-10-16 22:07:00 +02:00
if len(args) >= withCutSetArgsSize {
2020-11-26 09:57:26 +01:00
bad = toString(args[1])
2020-10-11 20:26:27 +02:00
}
2020-10-16 22:07:00 +02:00
if len(args) >= withBaseRandArgsSize {
2020-11-26 09:57:26 +01:00
base = toString(args[2])
2020-10-11 20:26:27 +02:00
}
base = TrimAll(base, bad)
return RandSeq(base, l), nil
}
functions["rand_text_alphanumeric"] = func(args ...interface{}) (interface{}, error) {
l := 0
bad := ""
chars := letters + numbers
if len(args) >= 1 {
l = args[0].(int)
}
2020-10-16 22:07:00 +02:00
if len(args) >= withCutSetArgsSize {
2020-11-26 09:57:26 +01:00
bad = toString(args[1])
2020-10-11 20:26:27 +02:00
}
chars = TrimAll(chars, bad)
return RandSeq(chars, l), nil
}
functions["rand_text_alpha"] = func(args ...interface{}) (interface{}, error) {
l := 0
bad := ""
chars := letters
if len(args) >= 1 {
l = args[0].(int)
}
2020-10-16 22:07:00 +02:00
if len(args) >= withCutSetArgsSize {
2020-11-26 09:57:26 +01:00
bad = toString(args[1])
2020-10-11 20:26:27 +02:00
}
chars = TrimAll(chars, bad)
return RandSeq(chars, l), nil
}
functions["rand_text_numeric"] = func(args ...interface{}) (interface{}, error) {
l := 0
bad := ""
chars := numbers
if len(args) >= 1 {
l = args[0].(int)
}
2020-10-16 22:07:00 +02:00
if len(args) >= withCutSetArgsSize {
2020-11-26 09:57:26 +01:00
bad = toString(args[1])
2020-10-11 20:26:27 +02:00
}
chars = TrimAll(chars, bad)
return RandSeq(chars, l), nil
}
functions["rand_int"] = func(args ...interface{}) (interface{}, error) {
min := 0
max := math.MaxInt32
if len(args) >= 1 {
min = args[0].(int)
}
2020-10-16 22:07:00 +02:00
if len(args) >= withMaxRandArgsSize {
2020-10-11 20:26:27 +02:00
max = args[1].(int)
}
return rand.Intn(max-min) + min, nil
}
2020-10-23 10:13:34 +02:00
// Time Functions
functions["waitfor"] = func(args ...interface{}) (interface{}, error) {
seconds := args[0].(float64)
time.Sleep(time.Duration(seconds) * time.Second)
return true, nil
}
// Collaborator
functions["collab"] = func(args ...interface{}) (interface{}, error) {
// check if collaborator contains a specific pattern
2020-11-26 09:57:26 +01:00
return collaborator.DefaultCollaborator.Has(toString(args[0])), nil
2020-10-23 10:13:34 +02:00
}
return functions
}