Linux搭建Socks v5正向代理服务器

很多中小企业为了隔离办公环境与因特网环境,使用Socks5或者HTTP代理的方式。HTTP代理的方式选择很多,常见的正向代理服务器有squid,privoxy,nginx,windows端有CCProxy等。但是Socks v4/v5协议的Linux代理选择很少,百度上搜索到的都是使用一个叫ss5的服务搭建的,功能比较简陋,最后一次更新是2013年2月。在一些比较新的Linux发行版上,如Ubuntu18.04编译都很困难。所以这里推荐一个产品Dante ,中文名但丁。以Ubuntu18.04为例介绍它的安装和部署方法。

Dante这个产品功能很丰富,有服务端和客户端。服务端可以很快的作为普通socks5代理服务器使用,而且可以输出丰富的日志。指定各种灵活的网络规则。比如允许哪些IP连接该服务,允许或禁止哪些IP地址或段使用代理进行访问,还可以设置多种用户认证方式,代理UDP协议等。非常强大。

首先Ubuntu安装有三种方式,分别是apt源安装,deb包安装和编译安装。

apt源安装最为简易,但是版本会比较旧,有些功能尤其是认证功能可能不能正常使用,安装方法如下

sudo apt-get install dante-server

deb包安装也很简单,是别人编译好打包的,不过版本也不是很新,可以到下面链接去下载http://ppa.launchpad.net/dajhorn/dante/ubuntu/pool/main/d/dante/

最推荐的还是源码编译,编译本身也不麻烦,可以根据自己的需要设置编译参数和依赖库

首先我们可以去官网查找最新版本的源码包,官网地址https://www.inet.no/dante/

然后下载并解压

sudo apt-get install gcc make
wget https://www.inet.no/dante/files/dante-1.4.2.tar.gz
tar -xzvf ./dante-1.4.2.tar.gz 
cd ./dante-1.4.2/

然后你可以选择添加编译参数或者不写,它会自动的检测你有哪些依赖库,然后自动配置编译参数,如下

./configure

...
                     Configure status:

Client:            Enabled
Server:            Enabled
Preloading:        Enabled
Libwrap:           Disabled, tcpd.h missing
BSD Auth:          Disabled, usable bsd_auth.h not found
PAM:               Disabled, security/pam_appl.h missing
GSSAPI:            Not found/disabled
KRB5:              Not found/disabled
SASL:              Not found/disabled
UPNP:              Not found/disabled
Compatability:     issetugid setproctitle strlcpy strvis

                     Modules:

redirect:          Not found
bandwidth:         Not found
ldap:              Not found

由于我没有安装某些依赖,如libpam-dev等,所以很多认证方式如PAM,GSSAPI等都是disabled。然后下面的redirect,bandwidth,ldap是额外附加的模块,这些模块并不是免费的,最便宜的也要400美刀。如bandwidth就可以设置客户端的速度限制。但是这么贵为啥不用CCProxy呢。。。

即使一个依赖库都不装,也是会有用户认证功能的,下面会介绍。也可以使用参数指定编译或者不编译的模块,如:

./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-client --without-libwrap --without-bsdauth --without-gssapi --without-krb5 --without-upnp --without-pam

然后编译并安装就可以了,推荐使用root用户

sudo make && sudo make install

如果你在configure的时候没有指定--prefix和--sysconfdir,那么服务端可执行文件编译后路径为

/usr/local/sbin/sockd

然后配置文件可以参考源码包下面的example文件夹

精简版的可以参考sockd-basic.conf,功能全面的参考sockd.conf

我们可以把精简版的放到sockd默认加载配置文件的路径

cp ./sockd-basic.conf /etc/sockd.conf

然后编辑这个文件,首先介绍一下匿名用户(不做用户名密码限制)的配置方法

## general configuration (taken from FAQ; <URL:http://www.inet.no/dante/FAQ>)

internal: eth0 port = 1080 #此处规定给客户端连接的端口和网卡,其中网卡可以为网卡名,或者是IP段,如0.0.0.0.0
external: eth0  #此处规定代理程序连接外网使用的网卡,可以和internal是同一块网卡
socksmethod: username none #有none关健字则支持匿名用户使用代理,不做认证
user.notprivileged: nobody #不做认证功能且端口号大于1024则不需要使用root权限
logoutput: stderr #日志的输出位置,默认为控制台,可以改成syslog,stdin,stdout或者具体的文件路径如/var/log/sockd.log

## client access rules
#下面是客户端接入的限制,不可省略,client block为拒绝某些客户端接入,client pass为接受某些客户端接入,0.0.0.0/0代表任意IP
#client pass { from: fxp0 to: fxp0 } # address-range on internal nic.
client pass {
        from: 0.0.0.0/0 to: 0.0.0.0/0
        log: error connect disconnect #指定日志打印的事件
}


## server operation access rules

# block connections to localhost, or they will appear to come from the proxy.
socks block { from: 0/0 to: lo log: connect }

#socks block为服务端屏蔽的IP规则,socks pass为服务端允许的IP规则
# block bind to ports lower than 1023
#socks block {
#        from: 0/0 to: 0/0 port le 1023
#        command: bind
#        log: connect
#}
#填写完禁止的目标IP段之后(可以填写多组socks block{}),设置允许通过的IP范围。from为发起方的IP(客户端的IP),to是要代理的目标IP(网站的IP)。
# allow the rest
socks pass {
        from: 0/0 to: 0/0
        log: connect disconnect error
}

启动Dante非常简单,只需执行sockd即可,也可以通过-f参数指定配置文件,-D参数进入后台运行模式(默认为前台执行,可以使用ctrl+c来停止)

/usr/local/sbin/sockd -f /etc/sockd.conf -D

如果配置文件中使用了user.notprivileged参数,则无需使用root启动,如果填写了user.privileged,则需要使用root启动。

如果不做用户名密码限制的话,往往会被滥用,所以我们需要打开Dante的认证功能。即使前面编译的时候没有选择PAM或者GSSAP,也是可以通过linux系统的用户名密码作为参考进行认证的,需要新建系统用户并修改一下配置文件。

首先新建系统用户,但不给其赋予登录服务器的权限,并且可以设置有效期(通过-e参数),然后设置一个密码

sudo useradd alex -s /usr/sbin/nologin
sudo passwd alex

然后修改配置文件如下,多数都和上面的一样

## general configuration (taken from FAQ; <URL:http://www.inet.no/dante/FAQ>)

internal: 0.0.0.0 port = 1080
external: eth0
#socksmethod: username none
socksmethod: username #没有none关健字代表仅限用户名密码认证
user.privileged: root #由于认证需要读取系统用户权限,所以要以root用户启动
user.notprivileged: nobody
logoutput: stderr

## client access rules

#client pass { from: fxp0 to: fxp0 } # address-range on internal nic.
client pass {
        from: 0.0.0.0/0 to: 0.0.0.0/0
        log: error connect disconnect #log有五种记录,这三种非常详细
}


## server operation access rules
#禁止通过socks5端口访问服务器本身
# block connections to localhost, or they will appear to come from the proxy.
socks block { from: 0/0 to: lo log: connect }

# block bind to ports lower than 1023
#socks block {
#        from: 0/0 to: 0/0 port le 1023
#        command: bind
#        log: connect
#}

# allow the rest
socks pass {
        from: 0/0 to: 0/0
        command: bind connect udpassociate #此处有五种可以选择
        log: error #不想log日志过大的话就仅打印error
        #socksmethod: username #如果上面的socksmethod有none关键字,则可以设置某些IP段必须由用户名密码认证,有些不用,通过填写多个socks pass {}来分别实现
}

上面的log有四种选择,可以有选择性的进行打印一种或多种,分别是error  connect disconnect iooperation,如果写前三种,那么就会有非常详细的信息,包括哪个IP在几点连接了哪个目标IP,可以用来做审计用户行为。如果不像log文件过大,那么使用error就可以了。

同时command也有五种选择,分别是bind, connect, udpassociate, bindrepy, udpreply。分为两组,前三个为一组,后两个为一组。如果想要同时开启TCP和UDP的代理端口,一般是添加两对socks pass{},socks block{}配置,如下

#block communication with www.example.org
socks block {
       from: 0.0.0.0/0 to: www.example.org
       command: bind connect udpassociate
       log: error # connect disconnect iooperation
}

#generic pass statement - bind/outgoing traffic
socks pass {  
       from: 0.0.0.0/0 to: 0.0.0.0/0
       command: bind connect udpassociate
       log: error # connect disconnect iooperation
}

#block incoming connections/packets from ftp.example.org 
socks block {
       from: 0.0.0.0/0 to: ftp.example.org
       command: bindreply udpreply
       log: error # connect disconnect iooperation
}

#generic pass statement for incoming connections/packets
socks pass {
       from: 0.0.0.0/0 to: 0.0.0.0/0
       command: bindreply udpreply
       log: error # connect disconnect iooperation
}

 

此时一个支持用户名密码认证和TCP+UDP的Socks5服务器就搭建好了。提示:在访问HTTP网站时,Socks5代理默认会把DNS请求通过Socks5代理服务器进行解析,所以你的Socks5代理服务器需要配置正确的DNS服务器地址,方法就是修改/etc/resolv.conf文件。如果代理服务器没有指定DNS服务器,那么客户端会无法解析域名。

提示2,使用Chrome或者FireFox浏览器都可以设置Socks5代理,Firefox原生支持,Chrome需要安装一个Switch Omega的插件,但是两个服务器均不支持用户名密码认证的代理,如果你需要用户名密码认证需要使用其他的软件,如Privoxy

 

参考文章

https://www.proxyrack.com/how-to-setup-a-socks5-proxy-server-using-dante/

https://www.binarytides.com/setup-dante-socks5-server-on-ubuntu/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章