func (m *HttpTampererSymptom) MuckRequest(ctx *muxy.Context) { // Body if m.Request.Body != "" { newreq, err := http.NewRequest(ctx.Request.Method, ctx.Request.URL.String(), bytes.NewBuffer([]byte(m.Request.Body))) if err != nil { log.Error(err.Error()) } *ctx.Request = *newreq log.Debug("Spoofing HTTP Request Body with %s", log.Colorize(log.BLUE, m.Request.Body)) } // Set Cookies for _, c := range m.Request.Cookies { c.Expires = stringToDate(c.RawExpires) log.Debug("Spoofing Request Cookie %s => %s", log.Colorize(log.LIGHTMAGENTA, c.Name), c.String()) ctx.Request.Header.Add("Cookie", c.String()) } // Set Headers for k, v := range m.Request.Headers { key := strings.ToTitle(strings.Replace(k, "_", "-", -1)) log.Debug("Spoofing Request Header %s => %s", log.Colorize(log.LIGHTMAGENTA, key), v) ctx.Request.Header.Set(key, v) } // This Writes all headers, setting status code - so call this last if m.Request.Method != "" { ctx.Request.Method = m.Request.Method } }
func (m *Muxy) LoadPlugins() { // Load Configuration var err error var confLoader *plugo.ConfigLoader c := &PluginConfig{} if m.config.ConfigFile != "" { confLoader = &plugo.ConfigLoader{} err = confLoader.LoadFromFile(m.config.ConfigFile, &c) if err != nil { log.Fatalf("Unable to read configuration file: %s", err.Error()) } } else { log.Fatal("No config file provided") } log.SetLevel(log.LogLevel(c.LogLevel)) // Load all plugins m.middlewares = make([]Middleware, len(c.Middleware)) plugins := plugo.LoadPluginsWithConfig(confLoader, c.Middleware) for i, p := range plugins { log.Info("Loading plugin \t" + log.Colorize(log.YELLOW, c.Middleware[i].Name)) m.middlewares[i] = p.(Middleware) } m.proxies = make([]Proxy, len(c.Proxy)) plugins = plugo.LoadPluginsWithConfig(confLoader, c.Proxy) for i, p := range plugins { log.Info("Loading proxy \t" + log.Colorize(log.YELLOW, c.Proxy[i].Name)) m.proxies[i] = p.(Proxy) m.proxies[i].Setup(m.middlewares) } }
func (l *LoggerMiddleware) HandleEvent(e muxy.ProxyEvent, ctx *muxy.Context) { switch e { case muxy.EVENT_PRE_DISPATCH: if ctx.Request == nil { if len(ctx.Bytes) > 0 { log.Info("Handle TCP event " + log.Colorize(log.GREY, "PRE_DISPATCH") + fmt.Sprintf(" Received %d%s", len(ctx.Bytes), " bytes")) data := fmt.Sprintf(l.format, ctx.Bytes) log.Debug("Handle TCP event " + log.Colorize(log.GREY, "PRE_DISPATCH") + " Received request: " + bytesTab + log.Colorize(log.BLUE, data)) } } else { log.Info("Handle HTTP event " + log.Colorize(log.GREY, "PRE_DISPATCH") + " Proxying request " + log.Colorize(log.LIGHTMAGENTA, ctx.Request.Method) + log.Colorize(log.BLUE, " \""+ctx.Request.URL.String()+"\"")) } case muxy.EVENT_POST_DISPATCH: if ctx.Request == nil { if len(ctx.Bytes) > 0 { log.Info("Handle TCP event " + log.Colorize(log.GREY, "POST_DISPATCH") + fmt.Sprintf(" Sent %d%s", len(ctx.Bytes), " bytes")) data := fmt.Sprintf(l.format, ctx.Bytes) log.Debug("Handle TCP event " + log.Colorize(log.GREY, "POST_DISPATCH") + " Sent response: " + bytesTab + log.Colorize(log.BLUE, data)) } } else { log.Info("Handle HTTP event " + log.Colorize(log.GREY, "POST_DISPATCH") + " Returning " + log.Colorize(log.GREY, fmt.Sprintf("%s", ctx.Response.Status))) } } }
func (p *HttpProxy) Proxy() { log.Info("HTTP proxy listening on %s", log.Colorize(log.BLUE, fmt.Sprintf("%s://%s:%d", p.Protocol, p.Host, p.Port))) pkiMgr, err := pki.New() checkHttpServerError(err) config, err := pkiMgr.GetClientTLSConfig() checkHttpServerError(err) config.InsecureSkipVerify = false mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { director := func(req *http.Request) { req = r req.URL.Scheme = p.ProxyProtocol req.URL.Host = fmt.Sprintf("%s:%d", p.ProxyHost, p.ProxyPort) } proxy := &ReverseProxy{Director: director, Middleware: p.middleware} proxy.Transport = &http.Transport{ Proxy: http.ProxyFromEnvironment, TLSClientConfig: config, TLSHandshakeTimeout: 10 * time.Second, } proxy.ServeHTTP(w, r) }) if p.Protocol == "https" { checkHttpServerError(err) checkHttpServerError(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", p.Host, p.Port), pkiMgr.Config.ClientCertPath, pkiMgr.Config.ClientKeyPath, mux)) } else { checkHttpServerError(http.ListenAndServe(fmt.Sprintf("%s:%d", p.Host, p.Port), mux)) } }
func (m *HttpTampererSymptom) MuckResponse(ctx *muxy.Context) { // Body if m.Response.Body != "" { var cl io.ReadCloser cl = &responseBody{body: []byte(m.Response.Body)} r := &http.Response{ Request: ctx.Request, Header: ctx.Response.Header, Close: ctx.Response.Close, ContentLength: ctx.Response.ContentLength, Trailer: ctx.Response.Trailer, TLS: ctx.Response.TLS, TransferEncoding: ctx.Response.TransferEncoding, Status: ctx.Response.Status, StatusCode: ctx.Response.StatusCode, Proto: ctx.Response.Proto, ProtoMajor: ctx.Response.ProtoMajor, ProtoMinor: ctx.Response.ProtoMinor, Body: cl, } log.Debug("Injecting HTTP Response Body with %s", log.Colorize(log.BLUE, m.Response.Body)) *ctx.Response = *r } // Set Cookies for _, c := range m.Response.Cookies { c.Expires = stringToDate(c.RawExpires) log.Debug("Spoofing Response Cookie %s => %s", log.Colorize(log.LIGHTMAGENTA, c.Name), c.String()) ctx.Response.Header.Add("Set-Cookie", c.String()) } // Set Headers for k, v := range m.Response.Headers { key := strings.ToTitle(strings.Replace(k, "_", "-", -1)) log.Debug("Spoofing Response Header %s => %s", log.Colorize(log.LIGHTMAGENTA, key), v) ctx.Response.Header.Add(key, v) } // This Writes all headers, setting status code - so call this last if m.Response.Status != 0 { ctx.Response.StatusCode = m.Response.Status ctx.Response.Status = http.StatusText(m.Response.Status) } }
func (m *HttpTampererSymptom) HandleEvent(e muxy.ProxyEvent, ctx *muxy.Context) { switch e { case muxy.EVENT_POST_DISPATCH: log.Debug("Spoofing status code, responding with %s", log.Colorize(log.YELLOW, fmt.Sprintf("%d %s", m.Status, http.StatusText(m.Status)))) // Body if m.Body != "" { var cl io.ReadCloser cl = &responseBody{body: []byte(m.Body)} r := &http.Response{ Request: ctx.Request, Header: ctx.Response.Header, Close: ctx.Response.Close, ContentLength: ctx.Response.ContentLength, Trailer: ctx.Response.Trailer, TLS: ctx.Response.TLS, TransferEncoding: ctx.Response.TransferEncoding, Status: ctx.Response.Status, StatusCode: ctx.Response.StatusCode, Proto: ctx.Response.Proto, ProtoMajor: ctx.Response.ProtoMajor, ProtoMinor: ctx.Response.ProtoMinor, Body: cl, } log.Debug("Injecting HTTP Response Body with %s", log.Colorize(log.BLUE, m.Body)) *ctx.Response = *r } // Set Headers for k, v := range m.Headers { key := strings.ToTitle(strings.Replace(k, "_", "-", -1)) log.Debug("Spoofing Header %s => %s", log.Colorize(log.LIGHTMAGENTA, key), v) ctx.Response.Header.Add(key, v) ctx.ResponseWriter.Header().Add(key, v) } // This Writes all headers, setting status code - so call this last ctx.Response.StatusCode = m.Status ctx.Response.Status = http.StatusText(m.Status) ctx.ResponseWriter.WriteHeader(m.Status) } }
func (p *HttpProxy) Proxy() { log.Info("HTTP proxy listening on %s", log.Colorize(log.BLUE, fmt.Sprintf("http://%s:%d", p.Host, p.Port))) mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { director := func(req *http.Request) { req = r req.URL.Scheme = p.ProxyProtocol req.URL.Host = fmt.Sprintf("%s:%d", p.ProxyHost, p.ProxyPort) } proxy := &ReverseProxy{Director: director, Middleware: p.middleware} proxy.ServeHTTP(w, r) }) err := http.ListenAndServe(fmt.Sprintf("%s:%d", p.Host, p.Port), mux) if err != nil { log.Info("ListenAndServe error: ", err.Error()) } }
func (p *TcpProxy) Proxy() { log.Trace("Checking connection: %s:%d", p.Host, p.Port) laddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", p.Host, p.Port)) check(err) log.Trace("Checking connection: %s:%d", p.ProxyHost, p.ProxyPort) raddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", p.ProxyHost, p.ProxyPort)) check(err) listener, err := net.ListenTCP("tcp", laddr) check(err) for { log.Info("TCP proxy listening on %s", log.Colorize(log.BLUE, fmt.Sprintf("tcp://%s:%d", p.Host, p.Port))) conn, err := listener.AcceptTCP() if err != nil { fmt.Printf("Failed to accept connection '%s'\n", err) continue } p.connId++ p := &proxy{ lconn: conn, laddr: laddr, raddr: raddr, packetsize: p.PacketSize, erred: false, errsig: make(chan bool), prefix: fmt.Sprintf("Connection #%03d ", p.connId), hex: p.HexOutput, nagles: p.NaglesAlgorithm, middleware: p.middleware, // matcher: matcher, // replacer: replacer, } go p.start() } }