一个简单的tcp代理实现

简介: There are a number of reasons to have a TCP proxy in your tool belt, bothfor forwarding traffic to bounce from host to host, but also when assessingnetwork-based software.

There are a number of reasons to have a TCP proxy in your tool belt, both
for forwarding traffic to bounce from host to host, but also when assessing
network-based software. When performing penetration tests in enterprise
environments, you'll commonly be faced with the fact that you can't run
Wireshark, that you can't load drivers to sniff the loopback on Windows, or
that network segmentation prevents you from running your tools directly
against your target host. I have employed a simple Python proxy in a number of cases to help understand unknown protocols, modify traffic being
sent to an application, and create test cases for fuzzers. Let's get to it.

--black python

 

一个简单的tcp代理实现,当然是socket层面的实现。

可以说对应用是透明的。

用法如下:

tcpproxy -localhost 0.0.0.0 -localport 9000 -remotehost 20.3.3.3 -remoteport 80

browser open http://127.0.0.1:9000 is the same as open http://20.3.3.3:80

 

实现的时候还考虑unix环境高级编程中的select之类的多路复用,后来觉得还是gopher的方式比较好,直接上goroutine,既简单清晰,还高效。

实现思路基本上和blackhat python是一样的,不过它是基于线程,其实没啥差别。

package main

import (
"flag"
"fmt"
"os"
"net"
"encoding/hex"
)

func usage() {
s := `
a tcp proxy util
tcpproxy -localhost 127.0.0.1 -localport 9000 -remotehost 192.168.1.2 -remoteport 80
-localhost 127.0.0.1 default is 0.0.0.0 - listen on [localhost]:[localport] for incoming connections
-localport
-remotehost
-remoteport redirect incomming connection to [remotehost]:[remoteport]

Examples:
tcpproxy -localhost 0.0.0.0 -localport 9000 -remotehost 20.3.3.3 -remoteport 80

browser open http://127.0.0.1:9000 is the same as open http://20.3.3.3:80

`
fmt.Println(s)
os.Exit(0)
}

//send all received data to my client
func remoteConnHandler(rc, lc net.Conn) {
buf := make([]byte, 4096)
for {
n, err := rc.Read(buf)
if err != nil {
break
}
if n > 0 {
fmt.Printf("[<==] Sending %d bytes to localhost.\n", n)
hex.Dump(buf[:n])
lc.Write(buf[:n])
}
}
fmt.Println("[*] Closing remote connection...")
rc.Close()
lc.Close()
}
//first connect to remote server
//then send all received data to remote server
func localConnHandler(c net.Conn, remoteHost string, remotePort int) {
rc, err := net.Dial("tcp", fmt.Sprintf("%s:%d", remoteHost, remotePort))
if err != nil {
fmt.Printf("connect to remote server failed!")
return
}
go remoteConnHandler(rc, c)
buf := make([]byte, 4096)
for {
n, err := c.Read(buf) // only proxy normal data, urgent data is ignored
if err != nil {
break
}
if n > 0 {
fmt.Printf("[==>] Received %d bytes from localhost.\n", n)
hex.Dump(buf[:n])
rc.Write(buf[:n])
}
}
fmt.Println("[*] close local connection...")
//duplicat close is ok
rc.Close()
c.Close()
}
func serverLoop(localHost string, localPort int, remoteHost string, remotePort int) {
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", localHost, localPort))
if err != nil {
fmt.Errorf("listen on port %d error\n", localPort)
return
}
fmt.Printf("Listenging on %s:%d \n", localHost, localPort)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Errorf("accept error:", err)
break
}
fmt.Printf("[==>] Received incoming connection from %s\n", conn.RemoteAddr())
go localConnHandler(conn, remoteHost, remotePort)
}
}
func main() {
var (
localHost string
localPort int
remoteHost string
remotePort int
)

flag.StringVar(&localHost, "localhost", "0.0.0.0", "listening local ip")
flag.StringVar(&remoteHost, "remotehost", "", "remote host address")
flag.IntVar(&localPort, "localport", 0, "listening local port")
flag.IntVar(&remotePort, "remoteport", 0, "remote host's port to connect")
flag.Parse()
if len(remoteHost) <= 0 || localPort == 0 || remotePort == 0 {
usage()
}
serverLoop(localHost, localPort, remoteHost, remotePort)
}

 

目录
相关文章
|
负载均衡 网络协议 应用服务中间件
CentOS7下使用nginx实现TCP和UDP代理
CentOS7下使用nginx实现TCP和UDP代理
964 0
CentOS7下使用nginx实现TCP和UDP代理
|
网络协议
Wireshark抓包分析/TCP/Http/Https及代理IP的识别
前言   坦白讲,没想好怎样的开头。辗转三年过去了。一切已经变化了许多,一切似乎从没有改变。   前段时间调研了一次代理相关的知识,简单整理一下分享之。如有错误,欢迎指正。 涉及 Proxy IP应用 原理/层级wireshark抓包分析 HTTP head: X-Forwarded-For/ Proxy-Connection/伪造  X-Forwarded-For/ 以及常见的识别手段等几个方面。
2445 0
|
网络协议 应用服务中间件 nginx
nginx代理socket tcp/udp
准备一台linux服务器。nginx官网:http://nginx.org/ 。在网上搜到大致用的是 ngx_stream_core_module 这个模块,这里你也可以关注一下官方文档中的其他模块都是做什么的,那么这有相关的启用配置说明,与示例配置。
3660 0
|
网络协议 应用服务中间件 nginx
|
Web App开发 测试技术 应用服务中间件
|
网络协议 应用服务中间件 nginx
|
网络协议 应用服务中间件 nginx
|
网络协议
应用代理 socket TCP协议 的资料
http://blog.csdn.net/guowake/article/details/6615728 Linux下高并发socket最大连接数所受的各种限制   http://stackoverflow.
1133 0
下一篇
无影云桌面