Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法修改 HTTPS 的 Host #58

Closed
117503445 opened this issue Aug 31, 2023 · 2 comments
Closed

无法修改 HTTPS 的 Host #58

117503445 opened this issue Aug 31, 2023 · 2 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@117503445
Copy link

希望实现的效果: 在发出请求前修改 Host

比如下面的 Demo

package main

import (
	"regexp"
	"strconv"
	"strings"

	"github.com/lqqyt2423/go-mitmproxy/proxy"
	log "github.com/sirupsen/logrus"
)

type RewriteHost struct {
	proxy.BaseAddon
}

var titleRegexp1 = regexp.MustCompile("(<TITLE>)(.*?)(</TITLE>)")
var titleRegexp2 = regexp.MustCompile("(<title>)(.*?)(</title>)")

func (a *RewriteHost) Request(f *proxy.Flow) {
	f.Request.URL.Host = "google.com"
}

func (a *RewriteHost) Response(f *proxy.Flow) {
	contentType := f.Response.Header.Get("Content-Type")
	if !strings.Contains(contentType, "text/html") {
		return
	}

	// change html <title> end with: " - hack by go-mitmproxy"
	f.Response.ReplaceToDecodedBody()
	f.Response.Body = titleRegexp1.ReplaceAll(f.Response.Body, []byte("${1}${2} - hack by go-mitmproxy ${3}"))
	f.Response.Body = titleRegexp2.ReplaceAll(f.Response.Body, []byte("${1}${2} - hack by go-mitmproxy ${3}"))
	f.Response.Header.Set("Content-Length", strconv.Itoa(len(f.Response.Body)))
}

func main() {
	opts := &proxy.Options{
		Addr:              ":8000",
		StreamLargeBodies: 1024 * 1024 * 5,
		// SslInsecure:       true,
	}

	p, err := proxy.NewProxy(opts)
	if err != nil {
		log.Fatal(err)
	}

	p.AddAddon(&RewriteHost{})

	log.Fatal(p.Start())
}

运行命令 http_proxy=http://localhost:8000 curl http://baidu.com 返回

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved - hack by go-mitmproxy </TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

可以看到 Host 已经被修改为 google.com 了,并且返回的 HTML 也被修改了。这是预期的行为。

但是运行命令 https_proxy=http://localhost:8000 curl https://baidu.com 返回

<html>
<head><title>405 Not Allowed - hack by go-mitmproxy </title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>bfe/1.0.8.18</center>
</body>
</html>

分析返回体可以发现,返回 HTML 的 Title 被成功修改了,但是返回的 HTML 仍然是由 baidu 返回的,所以说 Host 修改失败了。

@lqqyt2423 lqqyt2423 added bug Something isn't working enhancement New feature or request labels Sep 1, 2023
@117503445
Copy link
Author

谢啦:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants