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/

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