好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

***基础——使用Go语言开发socks代理工具

***基础——使用Go语言开发socks代理工具

3gstudent 嘶吼专业版

0x00 前言

在上篇文章《***基础——端口转发与代理》提到了使用go语言分别实现正向和反向socks代理的方法,不仅开发效率高,而且能够很方便的实现跨平台编译。 本文将要进一步介绍Windows系统和Kali系统下使用Go语言开发的完整过程,并基于开源代码,实现一个socks正向和反向代理的工具,记录细节。

0x01 简介

本文将要介绍以下内容:

· Windows系统下Go语言开发环境的搭建

· Kali系统下Go语言开发环境的搭建

· 工具代码细节

· 开源完整实现代码

图片0x02 Windows系统下Go语言开发环境的搭建

测试系统: Win7x64

1、安装Go

下载安装:

https://golang.org/dl

或者

https://studygolang测试数据/dl

2、安装git

https://gitforwindows.org/

用来下载第三方开发包

0x03 代码实现与Windows系统下的跨平台编译

1、安装第三方包

需要以下三个:

· golang.org/x/net/context

· https://github测试数据/armon/go-socks5

· https://github测试数据/hashicorp/yamux

安装流程如下:

(1)安装golang.org/x/net/context

go-socks5依赖,否则安装时会提示:

 
go\src\github测试数据\armon\go-socks5\request.go:10:2: cannot find package "golang.o rg/x/net/context" in any of: C:\Go\src\golang.org\x\net\context (from $GOROOT ) C:\Users\a\go\src\golang.org\x\net\context (from $GOPATH) 

在线安装:

go get golang.org/x/net/context

通常会失败,这里可以先从github下载再离线安装。

完整命令如下:

 
md %GOROOT%\src\golang.org\xcd %GOROOT%\src\golang.org\xgit clone https://github测试数据/golang/net.gitgo install golang.org/x/net/context 

注意这里使用的路径为GOROOT,默认路径为C:\Go,可通过输入go env查看。

(2)安装go-socks5

在线安装:

go get github测试数据/armon/go-socks5 如果安装失败,同样先从github下载再离线安装。

完整命令如下:

 
md %USERPROFILE%\go\srccd %USERPROFILE%\go\srcgit clone https://github测试数据/armon/go-socks5.git
go install go-socks5 

需要注意这里使用的路径为%USERPROFILE%\go\,即GOPATH,而不是GOROOT,可通过输入go env查看。

如果使用GOROOT,会出现如下错误:

 
can't load package: C:\Go\src\go-socks5\request.go:10:2: non-standard import "go lang.org/x/net/context" in standard package "go-socks5" 

(3)安装yamux

在线安装:

go get github测试数据/hashicorp/yamux

离线安装:

 
cd %GOROOT%\srcgit clone https://github测试数据/hashicorp/yamux
go install yamux 

2、实现代码

我这里参考了https://github测试数据/brimstone/rsocks

添加了正向代理的功能,并在结构上做了调整,区分正向和反向代理。

完整实现代码:

https://github测试数据/3gstudent/Homework-of-Go/blob/master/frsocks.go

3、跨平台编译

正常编译命令如下:

go build frsocks.go

编译成功后生成文件frsocks.exe

想要指定输出文件名,这里需要先将frsocks.go重命名为main.go,再分别使用以下代码进行跨平台编译

(1)Windows 32位

 
SET CGO_ENABLED=0
SET GOOS=windows
SET GOARCH=386
go build -o frsocks_windows_386
(2)Windows 64位

SET CGO_ENABLED=0
SET GOOS=windows
SET GOARCH=amd64
go build -o frsocks_windows_adm64
(3) Linux  arm64

SET CGO_ENABLED=0
SET GOOS= Linux 
SET GOARCH=amd64
go build -o frsocks_ Linux _amd64 

所有支持的系统如下:

 
· androidarm

· darwin386

· darwinamd64

· darwinarm

· darwinarm64

· dragonflyamd64

· freebsd386

· freebsdamd64

· freebsdarm

·  Linux 386

·  Linux amd64

·  Linux arm

·  Linux arm64

·  Linux ppc64

·  Linux ppc64le

·  Linux mips

·  Linux mipsle

·  Linux mips64

·  Linux mips64le

·  Linux s390x

· netbsd386

· netbsdamd64

· netbsdarm

· openbsd386

· openbsdamd64

· openbsdarm

· plan9386

· plan9amd64

· solarisamd64

· windows386

· windowsamd64 

来自https://golang.org/doc/install/source#environment

图片 0x04 Kali系统下Go语言开发环境的搭建

测试系统: Kali2

1、安装Go

下载:

wget https://dl.google测试数据/go/go1.11.2. Linux -amd64.tar.gz 或者

wget https://studygolang测试数据/dl/golang/go1.11. Linux -amd64.tar.gz 安装:

tar -xzf go1.11. Linux -amd64.tar.gz -C /usr/local 测试:

cd /usr/local/go echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile source /etc/profile go 图片0x05 代码实现与Kali系统下的跨平台编译

1、安装第三方包

需要以下三个:

 
· golang.org/x/net/context

· https://github测试数据/armon/go-socks5

· https://github测试数据/hashicorp/yamux 

(1)安装golang.org/x/net/context

 
mkdir /usr/local/go/src/golang.org/
mkdir /usr/local/go/src/golang.org/x
cd /usr/local/go/src/golang.org/x
git clone https://github测试数据/golang/net.git
go install golang.org/x/net/context 

(2)安装go-socks5

 
mkdir /root/go
mkdir /root/go/src
cd /root/go/src
git clone https://github测试数据/armon/go-socks5.git
go install go-socks5 

(3)安装yamux

 
cd /usr/local/go/src/
git clone https://github测试数据/hashicorp/yamux
go install yamux 

2、实现代码

https://github测试数据/3gstudent/Homework-of-Go/blob/master/frsocks.go

3、跨平台编译

正常编译命令如下:

go build frsocks.go

编译成功后生成文件frsocks。

想要指定输出文件名,这里需要先将frsocks.go重命名为main.go,再分别使用以下代码进行跨平台编译。

(1)Windows 32位

 
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -o frsocks_windows_386.exe 

(2)Windows 64位

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o frsocks_windows_amd64.exe (3) Linux arm64

CGO_ENABLED=0 GOOS= Linux GOARCH=amd64 go build -o frsocks_ Linux _amd64 其它环境见https://golang.org/doc/install/source#environment

实现全平台编译的批处理文件已上传至github,地址如下:

https://github测试数据/3gstudent/Homework-of-Go/blob/master/windows_build.bat

0x06 工具测试

1、正向代理

如下图:

Client:

frsocks -sockstype fsocks -listen 1080

使用代理工具连接Client的1080端口

2、反向代理

如下图:

Client:

frsocks -sockstype rsocks -listen 1111 -socks 127.0.0.1:2222 Transit server:

frsocks -sockstype rsocks -connect 1.1.1.1:1111 使用代理工具连接Client的2222端口。

0x07 小结

本文介绍了Windows系统和Kali系统下使用Go语言开发的完整过程,基于开源代码,实现了一个socks正向和反向代理的工具。

查看更多关于***基础——使用Go语言开发socks代理工具的详细内容...

  阅读:44次