new: centralized logging and implemented DELETE /api/events route, closes #5

This commit is contained in:
evilsocket 2018-01-08 06:39:44 +01:00
parent 269d7d845b
commit f1f146d3d7
21 changed files with 144 additions and 184 deletions

View file

@ -5,17 +5,12 @@ import (
"net/http"
"strings"
"github.com/op/go-logging"
"github.com/elazarl/goproxy"
"github.com/evilsocket/bettercap-ng/core"
"github.com/evilsocket/bettercap-ng/firewall"
"github.com/evilsocket/bettercap-ng/session"
)
var log = logging.MustGetLogger("mitm")
type HttpProxy struct {
session.SessionModule
@ -27,13 +22,19 @@ type HttpProxy struct {
}
func (p HttpProxy) logAction(req *http.Request, jsres *JSResponse) {
fmt.Printf("[%s] %s > '%s %s%s' | Sending %d bytes of spoofed response.\n",
core.Green("http.proxy"),
core.Bold(strings.Split(req.RemoteAddr, ":")[0]),
p.Session.Events.Add("http.proxy.spoofed-response", struct {
To string
Method string
Host string
Path string
Size int
}{
strings.Split(req.RemoteAddr, ":")[0],
req.Method,
req.Host,
req.URL.Path,
len(jsres.Body))
len(jsres.Body),
})
}
func NewHttpProxy(s *session.Session) *HttpProxy {
@ -169,12 +170,13 @@ func (p *HttpProxy) Start() error {
if err, p.script = LoadProxyScript(scriptPath, p.Session); err != nil {
return err
} else {
log.Debugf("Proxy script %s loaded.", scriptPath)
p.Session.Events.Log(session.DEBUG, "Proxy script %s loaded.", scriptPath)
}
}
}
if p.Session.Firewall.IsForwardingEnabled() == false {
p.Session.Events.Log(session.INFO, "Enabling forwarding.")
p.Session.Firewall.EnableForwarding(true)
}
@ -188,15 +190,15 @@ func (p *HttpProxy) Start() error {
return err
}
address := fmt.Sprintf("%s:%d", p.address, proxy_port)
log.Infof("Starting proxy on %s.\n", address)
p.Session.Events.Log(session.DEBUG, "Applied redirection %s", p.redirection.String())
address := fmt.Sprintf("%s:%d", p.address, proxy_port)
p.server = http.Server{Addr: address, Handler: p.proxy}
go func() {
p.SetRunning(true)
if err := p.server.ListenAndServe(); err != nil {
p.SetRunning(false)
log.Warning(err)
p.Session.Events.Log(session.WARNING, "%s", err)
}
}()
@ -207,8 +209,8 @@ func (p *HttpProxy) Stop() error {
if p.Running() == true {
p.SetRunning(false)
p.server.Shutdown(nil)
log.Info("HTTP proxy stopped.\n")
if p.redirection != nil {
p.Session.Events.Log(session.DEBUG, "Disabling redirection %s", p.redirection.String())
if err := p.Session.Firewall.EnableRedirection(p.redirection, false); err != nil {
return err
}
@ -228,13 +230,13 @@ func (p HttpProxy) doProxy(req *http.Request) bool {
}
if req.Host == "" {
log.Errorf("Got request with empty host: %v\n", req)
p.Session.Events.Log(session.ERROR, "Got request with empty host: %v", req)
return false
}
for _, blacklisted := range blacklist {
if strings.HasPrefix(req.Host, blacklisted) {
log.Errorf("Got request with blacklisted host: %s\n", req.Host)
p.Session.Events.Log(session.ERROR, "Got request with blacklisted host: %s", req.Host)
return false
}
}