graftcp一种把指定程序的 TCP 流量重定向到代理的方法

时间:2022-07-27
本文章向大家介绍graftcp一种把指定程序的 TCP 流量重定向到代理的方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

graftcp

一个可以把指定程序的 TCP 连接重定向到 SOCKS5 proxy 的工具。

简介

graftcp?可以把任何指定程序(应用程序脚本、shell 等)的 TCP 连接重定向到 SOCKS5 代理

对比?tsocksproxychains?或?proxyChains-nggraftcp?并不使用?LD_PRELOAD 技巧来劫持共享库的 connect()、getaddrinfo() 等系列函数达到重定向目的,这种方法只对使用动态链接编译的程序有效,对于静态链接编译出来的程序,例如默认选项编译的 Go 程序proxychains-ng 就无效了graftcp?使用?ptrace(2)?系统调用跟踪或修改任意指定程序的 connect 信息,对任何程序都有效。工作原理后面将会解释。

用法参数

graftcp-local:

$ graftcp-local/graftcp-local -h
Usage of graftcp-local/graftcp-local:
  -config string
        Path to the configuration file
  -listen string
        Listen address (default ":2233")
  -logfile string
        Write logs to file
  -loglevel value
        Log level (0-6) (default 1)
  -pipepath string
        Pipe path for graftcp to send address info (default "/tmp/graftcplocal.fifo")
  -service string
        Control the system service: ["start" "stop" "restart" "install" "uninstall"]
  -socks5 string
        SOCKS5 address (default "127.0.0.1:1080")
  -syslog
        Send logs to the local system logger (Eventlog on Windows, syslog on Unix)

graftcp:

$ graftcp -h
Usage: graftcp [options] prog [prog-args]

Options:
  -a --local-addr=<graftcp-local-IP-addr 
                    graftcp-local's IP address. Default: localhost
  -p --local-port=<graftcp-local-port 
                    Which port is graftcp-local listening? Default: 2233
  -f --local-fifo=<fifo-path 
                    Path of fifo to communicate with graftcp-local.
                    Default: /tmp/graftcplocal.fifo
  -b --blackip-file=<black-ip-file-path 
                    The IP in black-ip-file will connect direct
  -w --whiteip-file=<white-ip-file-path 
                    Only redirect the connect that destination ip in the
                    white-ip-file to SOCKS5
  -n --not-ignore-local
                    Connecting to local is not changed by default, this
                    option will redirect it to SOCKS5
  -h --help
                    Display this help and exit

快速开始

假设你正在运行默认地址 “localhost:1080” 的 SOCKS5 代理,首先启动?graftcp-local

./graftcp-local/graftcp-local

通过?graftcp?安装来自?golang.org?的 Go 包:

./graftcp go get -v golang.org/x/net/proxy

通过?graftcp?打开?Chromium?/?Chrome?/?Firefox?浏览器,网页的所有请求都会重定向到 SOCKS5 代理

./graftcp chromium-browser

通过?graftcp?启动?Bash?/?Zsh?/?Fish,在这个新开的 shell 里面执行的任何新命令产生的 TCP 连接都会重定向到 SOCKS5 代理

% ./graftcp bash
$ wget https://www.google.com

工作原理

要达到重定向一个 app 发起的的 TCP 连接到其他目标地址并且该 app 本身对此毫无感知(透明代理)的目的,大概需要这些条件:

  • fork(2)?一个新进程,通过?execv(2)?启动该 app,并使用?ptrace(2)?进行跟踪,在 app 执行每一次 TCP 连接前,捕获并拦截这次?connect(2)?系统调用,获取目标地址的参数,并通过管道传给?graftcp-local
  • 修改这次?connect(2)?系统调用的目标地址参数为?graftcp-local?的地址,然后恢复执行被中断的系统调用。返回成功后,这个程序以为自己连的是原始的地址,但其实连的是?graftcp-local?的地址。这个就叫“移花接木”。
  • graftcp-local?根据连接信息和目标地址信息,与 SOCKS5 proxy 建立连接,把 app 的请求的数据重定向到 SOCKS5 proxy

简单的流程如下:

+---------------+             +---------+         +--------+         +------+
|   graftcp     |  dest host  |         |         |        |         |      |
|   (tracer)    +---PIPE----- |         |         |        |         |      |
|      ^        |  info       |         |         |        |         |      |
|      | ptrace |             |         |         |        |         |      |
|      v        |             |         |         |        |         |      |
|  +---------+  |             |         |         |        |         |      |
|  |         |  |  connect    |         | connect |        | connect |      |
|  |         +--------------- | graftcp +-------- | socks5 +-------- | dest |
|  |         |  |             | -local  |         | proxy  |         | host |
|  |  app    |  |  req        |         |  req    |        |  req    |      |
|  |(tracee) +--------------- |         +-------- |        +-------- |      |
|  |         |  |             |         |         |        |         |      |
|  |         |  |  resp       |         |  resp   |        |  resp   |      |
|  |         |<---------------+         |<--------+        |<--------+      |
|  +---------+  |             |         |         |        |         |      |
+---------------+             +---------+         +--------+         +------+

更多信息:?https://github.com/hmgle/graftcp