一:什么是端口轉發?
SSH 會自動加密和解密所有SSH 客戶端與服務端之間的網絡數據。但是,SSH 還能夠將其他TCP 端口的網絡數據通過SSH 鏈接來轉發,並且自動提供了相應的加密及解密服務。這一過程也被叫做“隧道”(tunneling),這是因為SSH 為其他TCP 鏈接提供了一個安全的通道來進行傳輸而得名。例如,Telnet,SMTP,LDAP 這些TCP 應用均能夠從中得益,避免了用戶名,密碼以及隱私信息的明文傳輸。而與此同時,如果工作環境中的防火牆限制了一些網絡端口的使用,但是允許SSH 的連接,也能夠通過將TCP 端口轉發來使用SSH 進行通訊。
二:SSH 端口轉發能夠提供兩大功能:
1.加密SSH Client 端至SSH Server 端之間的通訊數據2.突破防火牆的限制完成一些之前無法建立的TCP 連接
三:SSH端口本地轉發
本地轉發機制:
-L localport:remotehost:remotehostportsshserver
選項:
-f 后台啟用
-N 不打開遠程shell,處於等待狀態
-g 啟用網關功能
舉例:
ssh–L 9527:telnetsrv:23 -N sshsrv
telnet 127.0.0.1 9527
當訪問本機的9527的端口時,被加密后轉發到sshsrv的ssh服務,再解密被轉發到telnetsrv:23
data < >localhost:9527 < > localhost:XXXXX < > sshsrv:22 < > sshrv:yyyyy < > telnetsrv:23
流程解釋:
數據一旦telnet以后,數據會發送到本機9527端口,再在本機開一個隨機端口,充當ssh客戶端,再把數據流量發送到22端口的ssh服務端,收到數據以后,解密數據,臨時開一個隨機端口充當客戶端,再把流量發送到23端口telnet服務端
ssh協議里面封裝了telnet,一旦A連接了B主機,立即使用telnet連接C主機,此過程可以
突破防火牆的限制
#實驗
A->C 訪問被限制
A-B->C 使用B主機作為跳板突破訪問限制
選項回顧
-L 本機端口
-f 后台啟用,可以在本機直接執行命令,無需另開新終端
-N 不打開遠程shell,處於等待狀態,不跳到遠程主機,還在主機上,只是搭好了隧道,橋搭好,不ssh上去
-g 啟用網關功能
-R 服務端口
環境:
[root@Centos6 ~]# vim /etc/hosts A:Centos7 172.18.254.173 B:Centos6 172.18.253.175 C:Centos6-1 172.18.253.192
注:每個機器都添加hosts
條件:設置防火牆策略不讓Centos7直連Centos6-1
[root@centos6-1 ~]# iptables -A INPUT -s centos7 -j REJECT
操作:
[root@centos7 ~]# ssh -L 9527:centos6-1:23 -Nf centos6 #通過本地9527端口訪問centos6-1服務器IP地址使用telnet協議23端口,跳板機ip地址 [root@centos7 ~]# ss -tn State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 96 172.18.254.173:22 172.18.0.100:57618 ESTAB 0 0 172.18.254.173:35024 172.18.253.175:22 #隧道已經搭建好了,此時A主機可以通過telnet訪問C主機 [root@centos7 ~]# telnet 127.0.0.1 9527 #訪問成功 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. CentOS release 6.9 (Final) Kernel 2.6.32-696.el6.x86_64 on an x86_64 Centos6-1 login: docker Password: Last login: Mon Apr 9 21:33:43 from centos7 #刪除ssh端口本地裝發 [docker@centos6-1 ~]$ exit #退出telnet但是隧道還是搭着可以使用ps aux查看到 [root@centos7 ~]# ps aux root 6624 0.0 0.1 180392 1392 ? Ss 21:59 0:00 ssh -L 9527:centos6-1:23 -Nf centos6 [root@centos7 ~]# killall ssh #刪除搭建的橋 [root@centos7 ~]# telnet centos6-1 #此時可以說明清除成功 Trying 172.18.253.192... telnet: connect to address 172.18.253.192: Connection refused
總結:
Centos6上面實現代理科學上網不能用1080端口
有的公司管理的比較嚴格,指向外面開放80端口,如果想從我外面連接主機可以改ssh接口,在連接指定端口-p
四:SSH端口遠程裝發
遠程轉發機制:
-R sshserverport:remotehost:remotehostportsshserver
舉例:
ssh–R 9527:telnetsrv:23 –N sshsrv
讓sshsrv偵聽9527端口的訪問,如有訪問,就加密后通過ssh服務轉發請求到本機ssh客戶端,再由本機解密后轉發到telnetsrv:23
Data < > sshsrv:9527 < > sshsrv:22 < > localhost:XXXXX < > localhost:YYYYY< >telnetsrv:23
需求:
在A(Centos7)上開啟smtp服務(postfix),B(Centos6)做跳板,C(Centos6-1)客戶端給Centos7發送郵件
流程解析:
C-x->A (拒絕訪問)
C-B->A (通過遠程代理,接受訪問)
查看環境:
[root@centos7 ~]# ss -ntl #查看25端口有沒有開啟 LISTEN 0 100 127.0.0.1:25 *:* [root@centos7 ~]# vim /etc/postfix/main.cf #對文件配置文件進行修改,Centos7已經不同早期系統使用sendmail作為默認郵件服務器 #目的:修改綁定接口配置 # 不同文件有些localhost意思完全相反,此處localhost再郵件是127,而DNS中是表示的是本地的所有IP,相當於此處的all #inet_interfaces = all #此行不是所有主機而是所有ip意思 #此處去掉注釋 #inet_interfaces = $myhostname #inet_interfaces = $myhostname, localhost inet_interfaces = localhost #這一行說明接口是綁定在127.0.0.1上 #加上注釋 [root@centos7 ~]# ss -ntl LISTEN 0 100 *:25 *:*
環境配置:
[root@centos7 ~]# systemctl start telnet.socket #開啟telnet-server [root@centos7 ~]# systemctl status telnet.socket #查看一下是否已經開啟 [root@centos7 ~]# iptables -nvL #查看一下防火牆策略 [root@centos7 ~]# systemctl stop firewalld.service #關閉防火牆 [root@Centos6 ~]# chkconfig --list iptables #查看各運行級別防火牆開機運行狀態 iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@Centos6 ~]# chkconfig iptables off #關閉防火牆 [root@Centos6 ~]# chkconfig --list iptables #所有運行級別防火牆都關閉 iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@Centos6 ~]# chkconfig --del iptables #移除開機自動啟動防火牆 [root@Centos6-1 ~]# telnet centos7 25 #測試centos6-1可以通過telnet 25端口連接Centos7 Trying 172.18.254.173... Connected to centos7. Escape character is '^]'. 220 centos7.localdomain ESMTP Postfix #測試成功,25端口可以連接 #關閉跳板機(Centos6)和遠程服務器(Centos7)上的SELINUX [root@centos7 ~]# vim /etc/selinux/config SELINUX=disabled [root@centos7 ~]# setenforce 0
創建目標條件:
[root@centos7 ~]# iptables -A INPUT -s centos6-1 -j REJECT #設置防火牆策略,使Centos7不接受Centos6一切請求 [root@centos6-1 ~]# telnet centos7 25 Trying 172.18.254.173... telnet: connect to address 172.18.254.173: Connection refused
操作:Centos6-1使用遠程轉發,發送郵件給Centos7
[root@Centos6 ~]# ssh -R 9527:centos7:25 -fN centos6-1 #建立隧道,此過程確保Centos6可以公鑰連接centos6-1,否則需要手動口令驗證 [root@Centos6 ~]# ss -ntl #檢查郵件服務,可以知道服務已經開啟 LISTEN 0 100 ::1:25 :::* LISTEN 0 100 127.0.0.1:25 *:* [root@centos6-1 .ssh]# ss -ntl #此時也可以看到跳板機Centos6-1上9527端口已經在監聽 LISTEN 0 128 127.0.0.1:9527 *:* LISTEN 0 128 ::1:9527 :::* [root@centos6-1 ~]# ps -ef |grep ssh #查看一下ssh運行的進程 root 2288 1 0 09:58 ? 00:00:00 ssh -R 9527:centos7:25 -fN centos6-1 [root@centos6-1 .ssh]# telnet 127.0.0.1 9527 #此時可以通過telnet連接Cetnos7繞過了防火牆 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. 220 centos7.localdomain ESMTP Postfix #如果想退出進入telnet> 快捷鍵ctrl + ] [root@centos7 ~]# ss -nt State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 0 172.18.254.173:25 172.18.253.175:32898 ESTAB 0 96 172.18.254.173:22 172.18.0.100:58652 #此時在Centos7上發現有IP尾號為100的機子在連接實際上是Centos6-1在連接 #最后驗證是否能成功發送郵件 [root@centos6-1 ~]# telnet localhost 9527 #給Centos7發送郵件 Trying ::1... Connected to localhost. Escape character is '^]'. 220 centos7.localdomain ESMTP Postfix mail from:ceo@app.com 250 2.1.0 Ok rcpt to:root@centos7 250 2.1.5 Ok data 354 End data with <CR><LF>.<CR><LF> hello . 250 2.0.0 Ok: queued as 86A5411EA866 quit 221 2.0.0 Bye Connection closed by foreign host. [root@centos7 ~]# mail #可以收到郵件信息 Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/root": 12 messages 12 new >N 1 root Fri Mar 16 10:31 14/496 "Output from your job 4" N 2 (Cron Daemon) Sat Mar 17 08:00 25/886 "Cron <root@zangfans> /root/bin/etcback.sh" N 3 root Sat Mar 17 10:20 14/506 "Output from your job 2" N 4 (Cron Daemon) Tue Mar 20 08:00 25/886 "Cron <root@zangfans> /root/bin/etcback.sh" N 5 user@localhost.local Wed Mar 28 08:19 2129/142373 "[abrt] kernel: WARNING: CPU: 1 PID: 0 at net/sched/sch_generic.c:300 dev" N 6 user@localhost.local Fri Apr 6 19:41 1151/90323 "[abrt] nautilus: nautilus-desktop killed by SIGSEGV" N 7 HR@centos7.localdoma Tue Apr 10 14:44 16/633 "*** SECURITY information for centos7 ***" N 8 HR@centos7.localdoma Tue Apr 10 16:38 16/637 "*** SECURITY information for centos7 ***" N 9 HR@centos7.localdoma Tue Apr 10 16:38 16/641 "*** SECURITY information for centos7 ***" N 10 HR@centos7.localdoma Tue Apr 10 16:39 16/637 "*** SECURITY information for centos7 ***" N 11 HR@centos7.localdoma Tue Apr 10 16:39 16/641 "*** SECURITY information for centos7 ***" N 12 ceo@app.com Wed Apr 11 10:20 10/314 & 12 Message 12: From ceo@app.com Wed Apr 11 10:20:29 2018 Return-Path: <ceo@app.com> X-Original-To: root@centos7 Delivered-To: root@centos7.localdomain Status: R hello
五:動態端口轉發
原理:
當用firefox訪問internet時,本機的1080端口做為代理服務器,firefox的訪問請求被轉發到sshserver上,由sshserver替之訪問internet
動態轉發機制:
ssh-D 1080 root@sshserver
在本機firefox設置代理socket proxy:127.0.0.1:1080
測試:
curl --socks5 127.0.0.1:1080 http://zangfans.com
在企業內部或者網絡中,基於安全策略不能隨便訪問互聯網某些站點
需求:
訪問站點:Centos7
代理服務:centos6
企業內部主機:Centos6-1
環境配置:
[root@centos7 ~]# systemctl status httpd #查看Cetnos7上httpd服務有沒有開啟 [root@centos7 ~]# iptables -A INPUT -s centos6-1 -j REJECT #設置防火牆策略,使Centos7不接受Centos6一切請求 #確保Centos6,Centos6-1開啟了防火牆都關閉了 #設置Centos7網頁顯示內容 [root@centos7 html]# vim index.html <h1>www.google.com</h1>
#虛擬機上此時是否能夠訪問Centos7站點
#在此處需要多加,主機自己現在init如果從runlevel 3到5可能會出現重啟防火牆策略,開啟則關閉策略,對Centos7的策略重置
操作:
選項: -D 本地端口號
[root@centos6-1 ~]# ssh -D 1080 centos6 #無秘鑰登錄,則需要密碼驗證 [root@Centos6 ~]# ss -nt State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 0 172.18.253.175:47618 172.18.253.192:22 ESTAB 0 0 172.18.253.175:22 172.18.253.192:45114 ESTAB 0 0 172.18.253.175:22 172.18.0.100:58896 [root@centos6-1 ~]# ss -nt State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 0 172.18.253.192:45172 172.18.253.175:22 ESTAB 0 0 172.18.253.192:22 172.18.0.100:58868 ESTAB 0 0 172.18.253.192:22 172.18.253.175:47618 #最后通過curl命令頁面驗證 [root@centos6-1 ~]# curl --socks5 127.0.0.1 centos7 #默認端口就是1080 <h1>www.google.com</h1>
#此方法也只能通過linux機器才進行互聯網訪問,windows機器暫不支持
知識補充
curl里面--socks5選項
--socks5 <host[:port]> Use the specified SOCKS5 proxy - but resolve the host name locally. If the port number is not specified, it is assumed at port 1080. This option overrides any previous use of -x, --proxy, as they are mutually exclusive. Since 7.21.7, this option is superfluous since you can specify a socks5 proxy with -x, --proxy using a socks5:// protocol prefix. If this option is used several times, the last one will be used. (This option was previously wrongly documented and used as --socks without the number appended.) This option (as well as --socks4) does not work with IPV6, FTPS or LDAP.
基於網頁驗證
此時虛擬機任不能直接訪問Centos7,下面設置socks代理

當本地1080端口受到外部網站請求會轉發到外部服務器上面去,代理服務器通過1080端口把請求轉發到站點服務器上面去
此時方可正常訪問外部站點
