一、SSH 端口转发能够提供两大功能:
1、加密SSH Client 端至SSH Server 端之间的通讯数据
2、突破防火墙的限制完成一些之前无法建立的TCP 连接 (隧道功能)
3、ssh的选项
-b:指定密钥长度;
-e:读取openssh的私钥或者公钥文件;
-C:添加注释;
-f:指定用来保存密钥的文件名;
-i:读取未加密的ssh-v2兼容的私钥/公钥文件,然后在标准输出设备上显示openssh兼容的私钥/公钥;
-l:显示公钥文件的指纹数据;
-N:提供一个新密语;
-P:提供(旧)密语;
-q:静默模式;
-t:指定要创建的密钥类型。
常用示例
# 更安全 Ed25519 算法
ssh-keygen -t ed25519 -N '' -f ~/.ssh/id_ed25519
# 或者传统 RSA 算法
ssh-keygen -t rsa -b 2048 -N '' -f ~/.ssh/id_rsa
ssh-copy-id $IPs #$IPs为所有节点地址包括自身,按照提示输入yes 和root密码
二:SSH端口本地转发
运用场景图:
![](https://i-blog.csdnimg.cn/blog_migrate/b4b29db8f6e60d758c1d1b85af526a4a.png)
-L localport:remotehost:remotehostportsshserver
选项: -f 后台启用 -N 不打开远程shell,处于等待状态 -g 启用网关功能
[root@telnetserver ~]# iptables -A INPUT -s 192.168.40.132 -j REJECT
[root@telnetserver ~]# yum install -y telnet-server
[root@telnetserver ~]# systemctl start telnet.socket
[root@sshserver ~]# yum install -y telnet-server.x86_64
[root@sshserver ~]# systemctl start telnet.socket
The authenticity of host '192.168.40.211 (192.168.40.211)' can't be established.
ECDSA key fingerprint is SHA256:pNJw/K2ipf4PO9OZtkTmisNZ+ctpN02yRIMxTvNGlrA.
ECDSA key fingerprint is MD5:42:0a:fb:3f:74:1f:12:fc:f1:6a:20:00:43:0a:0f:c3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.40.211' (ECDSA) to the list of known hosts.
root@192.168.40.211's password:
Last login: Sun Oct 6 12:58:31 2019 from 192.168.40.1
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 192.168.40.211:22 192.168.40.1:50804
ESTAB 0 0 192.168.40.211:22 192.168.40.132:46418
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Kernel 3.10.0-693.el7.x86_64 on an x86_64
telnetserver login: aa
Password:
[aa@telnetserver ~]$ (登录成功)
root@192.168.40.211's password:
[root@client ~]#
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 52 192.168.40.211:22 192.168.40.1:50804
ESTAB 0 0 192.168.40.211:22 192.168.40.132:46448
三:SSH端口远程转发
![](https://i-blog.csdnimg.cn/blog_migrate/d030c8c2c8c2198e3d78a5f4cb62a7d7.png)
root@192.168.40.211's password:
Last login: Sun Oct 6 13:45:44 2019 from 192.168.40.1
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:9527 *:*
LISTEN 0 100 127.0.0.1:25 *:*
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
telnetserver login: aa
Password:
Last login: Sun Oct 6 01:21:13 from localhost
[aa@telnetserver ~]$
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 52 192.168.40.155:22 192.168.40.1:50809
ESTAB 0 0 ::ffff:192.168.40.155:23 ::ffff:192.168.40.211:36364
四、SSH动态端口转发
运用场景图: (翻墙,ss国外服务器做代理)
![](https://i-blog.csdnimg.cn/blog_migrate/e6cc21eb101f422583d6bb7332edd90f.png)
原理:
Chain INPUT (policy ACCEPT 78337 packets, 18M bytes)
pkts bytes target prot opt in out source destination
3 252 REJECT all -- * * 192.168.40.132 0.0.0.0/0 reject-with icmp-port-unreachable (挡住132)
root@192.168.40.211's password:
Last login: Sun Oct 6 14:31:08 2019 from 192.168.40.1
# curl --socks5 127.0.0.1:1080 192.168.40.155 #默认端口就是1080,可以不写
www.google.com
五、常用的端口转发
1、SSH 端口转发
SSH 提供了一个非常有意思的功能,就是端口转发,它能够将其他 TCP 端口的网络数据通过 SSH 链接来转发,
并且自动提供了相应的加密及解密服务。
(1)本地端口转发
ssh -fgN -L 2222:localhost:22 localhost
(2)远程端口转发
ssh -fgN -R 2222:host1:22 localhost
(3)动态转发
ssh -fgN -D 12345 root@host1
2、iptables 端口转发
CentOS 7.0 以下使用的是iptables,可以通过iptables实现数据包的转发。
(1)开启数据转发功能
vi /etc/sysctl.conf
#增加一行 net.ipv4.ip_forward=1
//使数据转发功能生效
sysctl -p
(2)将本地的端口转发到本机端口
iptables -t nat -A PREROUTING -p tcp --dport 2222 -j REDIRECT --to-port 22
(3)将本机的端口转发到其他机器
iptables -t nat -A PREROUTING -d 192.168.172.130 -p tcp --dport 8000 -j DNAT --to-destination 192.168.172.131:80
iptables -t nat -A POSTROUTING -d 192.168.172.131 -p tcp --dport 80 -j SNAT --to 192.168.172.130
#清空nat表的所有链
iptables -t nat -F PREROUTING
3、firewall 端口转发
CentOS 7.0以上使用的是firewall,通过命令行配置实现端口转发。
(1)开启伪装IP
firewall-cmd --permanent --add-masquerade
(2)配置端口转发,将到达本机的12345端口的访问转发到另一台服务器的22端口。
firewall-cmd --permanent --add-forward-port=port=12345:proto=tcp:toaddr=192.168.172.131:toport=22
(3)重新载入,使其失效。
firewall-cmd --reload
4、ncat 端口转发
netcat(简称nc)被誉为网络安全界的”瑞士军刀“,一个简单而有用的工具,这里介绍一种使用netcat实现端口转发的方法。
(1)安装ncat
yum install nmap-ncat -y
(2)监听本机 9876 端口,将数据转发到 192.168.172.131的 80 端口
ncat --sh-exec "ncat 192.168.172.131 80" -l 9876 --keep-open
nc -lp port 监听本机端口
-l 开启 监听模式,用于指定nc将处于监听模式。通常 这样代表着为一个 服务等待客户端来链接指定的端口。
-p<通信端口> 设置本地主机使用的通信端口。
-k<通信端口>强制 nc 待命链接.当客户端从服务端断开连接后,过一段时间服务端也会停止监听。通过选项 -k 我们可以强制服务器保持连接并继续监听端口。
5、NATBypass端口转发
一款lcx(htran)在golang下的实现
Gihub项目地址:
https://github.com/cw1997/NATBypass
(1)内网主机主动连接外网主机打通隧道
在目标机器上执行:nb -slave 127.0.0.1:3389 公网IP:51
在公网的机器执行:nb -listen 51 3340
在公网主机上连接 127.0.0.1:3340,即可连接上内网机器的3389端口。
https://mp.weixin.qq.com/s/PZEeLrRHb97udXrbPCU54A 多种常见转发