diff --git a/modules/api_rest_controller.go b/modules/api_rest_controller.go index e50fe3a6..65cba86f 100644 --- a/modules/api_rest_controller.go +++ b/modules/api_rest_controller.go @@ -40,7 +40,9 @@ func setSecurityHeaders(w http.ResponseWriter) { func toJSON(w http.ResponseWriter, o interface{}) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(o) + if err := json.NewEncoder(w).Encode(o); err != nil { + log.Error("error while encoding object to JSON: %v", err) + } } func (api *RestAPI) checkAuth(r *http.Request) bool { diff --git a/modules/events_view.go b/modules/events_view.go index 967748b3..1f7ca65b 100644 --- a/modules/events_view.go +++ b/modules/events_view.go @@ -2,8 +2,6 @@ package modules import ( "fmt" - "io/ioutil" - "net/http" "os" "strings" @@ -128,17 +126,17 @@ func (s *EventsStream) viewSnifferEvent(e session.Event) { misc := "" if e.Tag == "net.sniff.leak.http" { - req := se.Data.(*http.Request) + req := se.Data.(HTTPRequest) if req.Method != "GET" { misc += "\n\n" misc += fmt.Sprintf(" Method: %s\n", core.Yellow(req.Method)) - misc += fmt.Sprintf(" URL: %s\n", core.Yellow(req.URL.String())) + misc += fmt.Sprintf(" URL: %s\n", core.Yellow(req.URL)) misc += fmt.Sprintf(" Headers:\n") - for name, values := range req.Header { + for name, values := range req.Headers { misc += fmt.Sprintf(" %s => %s\n", core.Green(name), strings.Join(values, ", ")) } - if err := req.ParseForm(); err == nil { + if req.Form != nil { misc += " \n Form:\n\n" if len(req.Form) == 0 { misc += fmt.Sprintf(" %s\n", core.Dim("")) @@ -148,8 +146,7 @@ func (s *EventsStream) viewSnifferEvent(e session.Event) { } } } else if req.Body != nil { - b, _ := ioutil.ReadAll(req.Body) - misc += fmt.Sprintf(" \n %s:\n\n %s\n", core.Bold("Body"), string(b)) + misc += fmt.Sprintf(" \n %s:\n\n %s\n", core.Bold("Body"), string(req.Body)) } } } diff --git a/modules/net_sniff_event.go b/modules/net_sniff_event.go index 0757241e..5f1e482d 100644 --- a/modules/net_sniff_event.go +++ b/modules/net_sniff_event.go @@ -10,12 +10,12 @@ import ( type SniffData map[string]interface{} type SnifferEvent struct { - PacketTime time.Time - Protocol string - Source string - Destination string - Message string - Data interface{} + PacketTime time.Time `json:"time"` + Protocol string `json:"protocol"` + Source string `json:"from"` + Destination string `json:"to"` + Message string `json:"message"` + Data interface{} `json:"data"` } func NewSnifferEvent(t time.Time, proto string, src string, dst string, data interface{}, format string, args ...interface{}) SnifferEvent { diff --git a/modules/net_sniff_http.go b/modules/net_sniff_http.go index 28aa42b3..7f8501b4 100644 --- a/modules/net_sniff_http.go +++ b/modules/net_sniff_http.go @@ -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),