Golang 反向代理 Http 服务器,并修改服务器的 html

如下图所示,在代理服务器层伪造消息。
这种是一种非常简单的中间人攻击
 
package main

import (
 "bytes"
 "io/ioutil"
 "net/http"
 "net/http/httputil"
 "net/url"
 "strconv"
)

type transport struct {
 http.RoundTripper
}

func (t *transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
 resp, err = t.RoundTripper.RoundTrip(req)
 if err != nil {
  return nil, err
 }
 b, err := ioutil.ReadAll(resp.Body)
 if err != nil {
  return nil, err
 }
 err = resp.Body.Close()
 if err != nil {
  return nil, err
 }
 b = bytes.Replace(b, []byte("Fedora"), []byte("hello this is replace content"), -1)
 body := ioutil.NopCloser(bytes.NewReader(b))
 resp.Body = body
 resp.ContentLength = int64(len(b))
 resp.Header.Set("Content-Length", strconv.Itoa(len(b)))
 return resp, nil
}

var _ http.RoundTripper = &transport{}

func main() {
 target, err := url.Parse("http://35.201.172.1/")
 if err != nil {
  panic(err)
 }
 proxy := httputil.NewSingleHostReverseProxy(target)
 proxy.Transport = &transport{http.DefaultTransport}

 http.Handle("/", proxy)
 http.ListenAndServe(":8080", nil)
}

评论