Leo Zhang
菩提本无树,明镜亦非台!
 

1、ssh 端口转发

ssh 提供了一个非常有意思的功能,就是端口转发,它能够将其他 TCP 端口的网络数据通过 SSH 链接来转发,并且自动提供了相应的加密及解密服务。
# 本地端口转发
ssh -fgN -L 2222:localhost:22 localhost
 
# 远程端口转发
ssh -fgN -R 2222:host1:22 localhost
 
# 动态转发
ssh -fgN -D 12345 root@host1
 

 

2、iptables 端口转发

CentOS 7.0 以下使用的是iptables,可以通过iptables实现数据包的转发。
# 开启数据转发功能
vi /etc/sysctl.conf
net.ipv4.ip_forward=1
sysctl -p
 
# 将本地的端口转发到本机端口
iptables -t nat -A PREROUTING -p tcp --dport 2222 -j REDIRECT --to-port 22
 
# 将本机的端口转发到其他机器
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,通过命令行配置实现端口转发。
# 开启伪装IP
firewall-cmd --permanent --add-masquerade
 
# 配置端口转发,将到达本机的12345端口的访问转发到另一台服务器的22端口。
firewall-cmd --permanent --add-forward-port=port=12345:proto=tcp:toaddr=192.168.172.131:toport=22
 
# 重新载入,使其失效。
firewall-cmd --reload

 

4、socat 端口转发

socat是一个多功能的网络工具,使用socat进行端口转发。
# socat安装
yum install -y socat
 
# 在本地监听12345端口,并将请求转发至192.168.172.131的22端口。
socat TCP4-LISTEN:12345,reuseaddr,fork TCP4:192.168.172.131:22

 

5、ncat 端口转发

netcat(简称nc)被誉为网络安全界的”瑞士军刀“,这里介绍一种使用netcat实现端口转发的方法。
# 安装ncat
yum install nmap-ncat -y
 
# 监听本机 9876 端口,将数据转发到 192.168.172.131的 80 端口
ncat --sh-exec "ncat 192.168.172.131 80" -l 9876 --keep-open

 

 
posted on 2022-09-07 10:58  LeoZhanggg  阅读(1955)  评论(0编辑  收藏  举报