fix: fixed a json encoding issue that caused the api.rest controller to return an empty list of events when a net.sniff http event is triggered

This commit is contained in:
evilsocket 2018-09-27 16:09:11 +02:00
commit 0ada0fdf7b
4 changed files with 46 additions and 16 deletions

View file

@ -3,7 +3,9 @@ package modules
import (
"bufio"
"bytes"
"io/ioutil"
"net/http"
"net/url"
"github.com/bettercap/bettercap/core"
@ -11,6 +13,35 @@ import (
"github.com/google/gopacket/layers"
)
type HTTPRequest struct {
Method string `json:"method"`
Host string `json:"host"`
URL string `json:"url:"`
Headers http.Header `json:"headers"`
Form url.Values `json:"form"`
Body []byte `json:"body"`
}
func toSerializableRequest(req *http.Request) HTTPRequest {
body := []byte(nil)
form := (url.Values)(nil)
if err := req.ParseForm(); err == nil {
form = req.Form
} else if req.Body != nil {
body, _ = ioutil.ReadAll(req.Body)
}
return HTTPRequest{
Method: req.Method,
Host: req.Host,
URL: req.URL.String(),
Headers: req.Header,
Form: form,
Body: body,
}
}
func httpParser(ip *layers.IPv4, pkt gopacket.Packet, tcp *layers.TCP) bool {
data := tcp.Payload
reader := bufio.NewReader(bytes.NewReader(data))
@ -22,7 +53,7 @@ func httpParser(ip *layers.IPv4, pkt gopacket.Packet, tcp *layers.TCP) bool {
"http",
ip.SrcIP.String(),
req.Host,
req,
toSerializableRequest(req),
"%s %s %s %s%s %s",
core.W(core.BG_RED+core.FG_BLACK, "http"),
vIP(ip.SrcIP),