当前位置: 首页 > news >正文

通过Golang实现中间人攻击,查看和修改https流量包

要查看和修改 HTTPS 流量包,需要使用一个能够执行 中间人攻击(Man-in-the-Middle, MITM) 的代理工具。这个工具将拦截并解密 HTTPS 流量,然后允许查看和修改流量包的内容,再将其重新加密并发送到目标服务器。

完整的 MITM 代理工具(Golang 版)

以下是一个完整的 Golang 实现,可以查看和修改 HTTP 和 HTTPS 流量包。工具会使用自签名证书来解密 HTTPS 流量,并打印出请求和响应的详细内容,可以在代码中加入修改流量的逻辑。

代码
package mainimport ("crypto/tls""crypto/x509""fmt""io""io/ioutil""log""net""net/http""time"
)// Load the CA certificate and key
func loadCA(certFile, keyFile string) (tls.Certificate, *x509.CertPool, error) {ca, err := tls.LoadX509KeyPair(certFile, keyFile)if err != nil {return tls.Certificate{}, nil, err}caCert, err := x509.ParseCertificate(ca.Certificate[0])if err != nil {return tls.Certificate{}, nil, err}caPool := x509.NewCertPool()caPool.AddCert(caCert)return ca, caPool, nil
}// Function to modify the HTTP request (you can add your own logic here)
func modifyRequest(req *http.Request) {// Example: Adding a custom headerreq.Header.Set("X-Custom-Header", "Intercepted-By-MITM-Proxy")
}// Function to modify the HTTP response (you can add your own logic here)
func modifyResponse(resp *http.Response) {// Example: Modifying the response body (this example appends a string to HTML responses)if resp.Header.Get("Content-Type") == "text/html" {body, _ := ioutil.ReadAll(resp.Body)body = append(body, []byte("<p>Injected by MITM proxy</p>")...)resp.Body = ioutil.NopCloser(io.MultiReader(bytes.NewReader(body)))resp.ContentLength = int64(len(body))resp.Header.Set("Content-Length", fmt.Sprint(len(body)))}
}// Handles HTTP requests
func handleHTTP(w http.ResponseWriter, r *http.Request) {log.Printf("HTTP Request: %s %s", r.Method, r.URL.String())// Modify the request as neededmodifyRequest(r)// Forward the request to the actual servertransport := &http.Transport{}resp, err := transport.RoundTrip(r)if err != nil {http.Error(w, err.Error(), http.StatusBadGateway)return}defer resp.Body.Close()// Modify the response as neededmodifyResponse(resp)// Print response statuslog.Printf("HTTP Response: %d %s", resp.StatusCode, resp.Status)// Copy headers and body to the clientfor k, v := range resp.Header {w.Header()[k] = v}w.WriteHeader(resp.StatusCode)io.Copy(w, resp.Body)
}// Handles HTTPS requests
func handleHTTPS(w http.ResponseWriter, r *http.Request, ca tls.Certificate, caPool *x509.CertPool) {log.Printf("HTTPS Request: %s %s", r.Method, r.URL.String())// Hijack the connectionhijacker, ok := w.(http.Hijacker)if !ok {http.Error(w, "HTTP Server does not support hijacking", http.StatusInternalServerError)return}clientConn, _, err := hijacker.Hijack()if err != nil {http.Error(w, err.Error(), http.StatusServiceUnavailable)return}defer clientConn.Close()// Create a new TLS connection with the client using a dynamically generated certificatetlsConfig := &tls.Config{Certificates: []tls.Certificate{ca},ClientAuth:   tls.NoClientCert,RootCAs:      caPool,}serverName := r.HosttlsConn := tls.Server(clientConn, tlsConfig)if err := tlsConn.Handshake(); err != nil {log.Printf("TLS handshake error: %v", err)return}defer tlsConn.Close()// Connect to the real serverdestConn, err := net.Dial("tcp", r.Host)if err != nil {log.Printf("Failed to connect to destination: %v", err)return}defer destConn.Close()// Start a TLS session with the real serverserverTLSConn := tls.Client(destConn, &tls.Config{ServerName:         serverName,InsecureSkipVerify: true,})if err := serverTLSConn.Handshake(); err != nil {log.Printf("Failed to perform TLS handshake with the server: %v", err)return}defer serverTLSConn.Close()// Copy data between client and servergo io.Copy(serverTLSConn, tlsConn)io.Copy(tlsConn, serverTLSConn)
}// Main function to start the proxy server
func main() {// Load the CA certificate and keyca, caPool, err := loadCA("ca.crt", "ca.key")if err != nil {log.Fatalf("Failed to load CA: %v", err)}http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {if r.Method == http.MethodConnect {handleHTTPS(w, r, ca, caPool)} else {handleHTTP(w, r)}})log.Println("Listening on :8080...")log.Fatal(http.ListenAndServe(":8080", nil))
}

代码功能说明

  1. HTTPS 处理

    • 当客户端发起 CONNECT 请求(用于 HTTPS),代理服务器会拦截并建立与客户端的 TLS 连接。
    • 使用自签名证书与客户端进行 TLS 握手,接着与目标服务器建立第二个 TLS 连接。
    • 解密客户端和服务器之间的 HTTPS 流量,允许你查看和修改这些数据。
  2. HTTP 处理

    • 对于普通的 HTTP 请求,代理直接进行转发,并允许在 modifyRequestmodifyResponse 函数中修改请求和响应数据。
  3. 证书生成和加载

    • 代理工具使用自签名根证书生成中间证书,模拟目标服务器的证书。这需要事先生成并加载一个 CA 证书(根证书)。

如何使用

  1. 生成自签名根证书
    可以使用 OpenSSL 来生成一个自签名的 CA 证书:

    openssl genrsa -out ca.key 2048
    openssl req -new -x509 -key ca.key -out ca.crt -days 3650
    
    • ca.key 是 CA 的私钥。
    • ca.crt 是自签名的根证书。
  2. 编译并运行代理工具

    • 将代码保存为 proxy.go,然后编译:
      go build -o proxy proxy.go
      
    • 运行工具:
      ./proxy
      
  3. 配置浏览器代理

    • 在浏览器中设置代理为 localhost:8080
    • 将生成的 ca.crt 证书导入到浏览器的受信任根证书列表中。
  4. 查看和修改流量

    • 工具会在终端中打印所有经过代理的 HTTP 和 HTTPS 请求与响应的详细信息。
    • 可以在 modifyRequestmodifyResponse 函数中添加自定义逻辑来修改流量包。

安全性和合法性

  • 合法性:在没有适当授权的情况下,拦截和修改 HTTPS 流量可能是非法的,请确保你在合适的环境中使用,如安全测试或开发环境中,并且得到了相关方的同意。
  • 证书管理:务必保护好 CA 私钥,避免被不当使用。
  • 浏览器信任问题:浏览器必须信任生成的自签名根证书,否则会显示安全警告。

通过上述步骤,可以成功地拦截、查看和修改 HTTP 和 HTTPS 流量。

http://www.lryc.cn/news/429461.html

相关文章:

  • MySQL 安装与配置指南
  • android13布局查看工具 无源码查看布局 在线查找ui布局id
  • 【自动化测试必学语言】python:UnitTest框架
  • 大话LLM之向量数据库
  • EmguCV学习笔记 C# 2.2 Matrix类
  • [Windows CMD] 查看网络连接状态 netstat -na | findstr “TCP“
  • 「OC」视图控制器的懒加载策略
  • android studio 中 .gitignore 文件改动后 忽略的文件夹或文件无效
  • 鸿蒙 next 实现摄像头视频预览编码(一)
  • YOLO-V3
  • golang提案,内置 Go 错误检查函数
  • 零售业务产品系统应用架构设计(三)
  • 【GD32】从零开始学GD32单片机 | PMU电源管理单元+深度睡眠和待机例程(GD32F470ZGT6)
  • 公司员工电脑桌面太乱如何解决?桌面管理软件一招解决!
  • leetcode:2119. 反转两次的数字(python3解法)
  • 5.vue中axios封装工程化
  • 实验六:动态数码管实验
  • 《Cloud Native Data Center Networking》(云原生数据中心网络设计)读书笔记 -- 05网络虚拟化
  • 奥威BI数据可视化展示:如何充分发挥数据价值
  • jenkins工具配置
  • VAuditDemo文件漏洞
  • [Meachines] [Medium] poison LFI+日志投毒+VNC权限提升
  • EtherCAT运动控制器上位机开发之Python+Qt(三):PDO配置与SDO读写
  • MyBatis源码系列1(使用JDBC查询数据)
  • 【微服务】Nacos配置中心和客户端数据同步模式
  • WebRTC音视频开发读书笔记(六)
  • 高级列表组件ReList
  • Vxe UI vue vxe-table 实现表格数据分组功能,根据字段数据分组
  • oracle创建账户
  • 2024新型数字政府综合解决方案(五)