Skip to content

Commit ac05e87

Browse files
committed
added PostJSON
1 parent 3910592 commit ac05e87

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

engine/engine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (e *Engine) CreateVM() {
8585
e.VM.Set("Matches", e.VMMatches)
8686
e.VM.Set("CanSSHLogin", e.VMCanSSHLogin)
8787
e.VM.Set("RetrieveFileFromURL", e.VMRetrieveFileFromURL)
88+
e.VM.Set("PostJSON", e.VMPostJSON)
8889
e.VM.Set("DNSQuery", e.VMDNSQuery)
8990
e.VM.Set("HTTPRequest", e.VMHTTPRequest)
9091
e.VM.Set("Exec", e.VMExec)

engine/net.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package engine
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"io/ioutil"
57
"net"
68
"net/http"
@@ -82,6 +84,26 @@ func HTTPGetFile(url string) (int, []byte, error) {
8284
return respCode, pageData, nil
8385
}
8486

87+
func PostJSON(url string, jsonString []byte) (int, []byte, error) {
88+
// encode json to sanity check, then decode to ensure the transmition syntax is clean
89+
var jsonObj interface{}
90+
if err := json.Unmarshal(jsonString, &jsonObj); err != nil {
91+
return 0, nil, err
92+
}
93+
jsonStringCleaned, err := json.Marshal(jsonObj)
94+
if err != nil {
95+
return 0, nil, err
96+
}
97+
resp, err := http.Post(url, " application/json", bytes.NewReader(jsonStringCleaned))
98+
if err != nil {
99+
return 0, nil, err
100+
}
101+
respCode := resp.StatusCode
102+
pageData, err := ioutil.ReadAll(resp.Body)
103+
resp.Body.Close()
104+
return respCode, pageData, nil
105+
}
106+
85107
func TCPRead(ip, port string) ([]byte, error) {
86108
host := ip + ":" + port
87109
conn, err := net.Dial("tcp", host)

0 commit comments

Comments
 (0)