1. 程式人生 > >SSH反向隧道的內網穿透

SSH反向隧道的內網穿透

pre ssp sts mnt ast rap root eve ports

環境如下:

A機器兩塊網卡eth0(192.168.0.173)、eth1(192.168.100.1),eth0可以上外網,eth1僅僅是內部網絡,B機器只有eth1(192.168.100.3),和A機器eth1可以通信互聯,外網無法ssh進入B主機,可以使用ssh的反向隧道實現。

A:

1、首先在A 上編輯sshd 的配置文件/etc/ssh/sshd_config,將GatewayPorts 開關打開:


vim /etc/ssh/sshd_config

GatewayPorts yes

2、重啟sshd服務,使用修改生效

systemctl restart sshd
ssh -ngfNTR 1222:192.168.100.3:22 [email protected] -o ServerAliveInterval=300

-f 表示後臺執行
-N 表示不執行任何命令
-R 建立反向隧道
1222 A機用來外面ssh的監聽端口
-o ServerAliveInterval=300 的意思是讓ssh client每300秒就給server發個心跳,以免鏈路被RST.
-f Requests ssh to go to background just before command execution. 讓該命令後臺運行 .
-n Redirects stdin from /dev/null (actually, prevents reading from stdin).
-N Do not execute a remote command. 不執行遠程命令 .

-T Disable pseudo-tty allocation. 不占用 shell .
-g Allows remote hosts to connect to local forwarded ports.

[root@aiker01 ~]# netstat -antp | grep 1222
tcp        0      0 0.0.0.0:1222            0.0.0.0:*               LISTEN      16182/sshd: root    
tcp6       0      0 :::1222                 :::*                    LISTEN      16182/sshd: root

3、外部主機連接B就直接連接A的1222端口就可以了,1222要被防火墻允許

ssh -p 1222 [email protected]
root@aiker:/mnt/c/Users/aikera# ssh -p 1222 [email protected]
Last failed login: Tue Feb 13 11:19:53 CST 2018 from gateway on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Tue Feb 13 10:52:09 2018 from gateway
[root@aiker03 ~]#

4、A上查看端口的監聽狀態:

[root@aiker01 ~]# netstat -antp | grep 1222
tcp        0      0 0.0.0.0:1222            0.0.0.0:*               LISTEN      16182/sshd: root    
tcp        0      0 192.168.0.173:1222      192.168.0.190:60738     ESTABLISHED 16182/sshd: root    
tcp6       0      0 :::1222                 :::*                    LISTEN      16182/sshd: root    

5、保持連接

我們需要這個隧道能夠一直保持連接狀態,在需要的時候可以隨時接入,我們需要安裝使用autossh

B:

yum install autossh -y

B:

 autossh -p 22 -M 6777 -fNR 1322:127.0.0.1:22 [email protected]   #-M 參數指定的端口用來監聽隧道的狀態,與端口轉發無關;同時需要在A防火墻打開1322端口主機之間可以使用不用密碼的key

The authenticity of host ‘192.168.0.173 (192.168.0.173)‘ can‘t be established.
ECDSA key fingerprint is d5:1c:36:d7:57:64:3d:5b:8a:e8:aa:93:54:1d:8c:22.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.0.173‘ (ECDSA) to the list of known hosts.
Enter passphrase for key ‘/root/.ssh/id_rsa‘: 

A:


[root@aiker01 ~]# netstat -antp | grep 1322
tcp        0      0 0.0.0.0:1322            0.0.0.0:*               LISTEN      16798/sshd: root    
tcp6       0      0 :::1322                 :::*                    LISTEN      16798/sshd: root    

外面ssh B:

root@aiker:/mnt/c/Users/aikera# ssh -p 1322 [email protected]

[email protected]‘s password:
Last login: Tue Feb 13 15:29:30 2018 from gateway
[root@aiker03 ~]#

添加服務:

B:

useradd autosshuser
passwd autosshuser
su - autossh
ssh-keygen -t rsa  #生成密匙對,按回車,不使用密碼的密匙對
ssh-copy-id [email protected]  #copy密匙到A

B

創建以autosshuser 用戶權限調用autosshd 的service 文件。將下面文本寫入到文件/lib/systemd/system/autosshd.service,並設置權限為644:

[Unit]Description=Auto SSH Tunnel
After=network-online.target
[Service]
User=autosshuser
Type=simple
ExecStart=/bin/autossh -p 22 -M 5689 -NR ‘*1322:127.0.0.1:22‘ [email protected] -i /home/autossh/.ssh/id_rsa
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=always
[Install]
WantedBy=multi-user.target
WantedBy=graphical.target
systemctl enable autosshd    #允許自啟動
systemctl start autosshd

A
使用這條反向隧道穿透B 所在的NAT SSH 連接到B

ssh -p 1322 root@localhost

外部:

ssh -p 1322 [email protected]

C主機:

通過ssh做端口轉發代理上網:

ssh -p 1322 -qngfNTD 3128 [email protected]

C 是外面的電腦,A 是你的雲主機,B 是你公司的電腦。這樣做就可以給瀏覽器設置端口為3128 的sock4 本地(127.0.0.1)代理

瀏覽公司內網web

SSH反向隧道的內網穿透