当前位置: 首页 > article >正文

反向 SSH 隧道技术实现内网穿透

反向 SSH 隧道技术实现内网穿透
场景描述

有一台内网的 Linux PC 机,想在其他地方(如家中)使用浏览器,在浏览器中能够使用内网 Linux PC 机的命令行。

实现思路

内网 Linux PC 机在内网可以使用 SSH 进行连接,但内网的 Linux PC 机 SSH 是作为服务端使用的,只能被动等待连接。我们需要一种技术,让内网 Linux PC 机以客户端的形式连接到一台公网服务器,其他 SSH 客户端也连接到公网服务器,通过公网服务器能够使用命令控制内网 Linux PC 机。

参考资料
  • SSH Tunnel 强大的隧道功能
  • 只需 2 条指令,轻松实现 SSH 反向隧道!
  • ssh反向隧道实现内网穿透
  • 使用SSH反向隧道进行内网穿透
实现步骤
场景模拟
  • Ubuntu 电脑:IP 为 172.18.1.168/24,用户名为 systemuser2
  • Windows 电脑:IP 为 172.16.30.87/24 和 192.168.92.1/24。
  • Debian 虚拟机:IP 为 192.168.92.141/24,用户名为 tukuku01

根据上述 IP 信息:

  • Windows 可以通过内部路由器访问 Ubuntu,Ubuntu 也可以通过内部路由器访问 Windows。
  • Windows 可以通过虚拟机网络访问 Debian,Debian 也可以通过虚拟网络访问 Windows。
  • Debian 可以通过虚拟机网络访问 Ubuntu,但 Ubuntu 不能访问 Debian 系统。
目标

在 Windows 上通过 Ubuntu 进行反向代理,直接访问 Debian 系统。使用一系列的 SSH 命令,但命令中不可出现 Debian 的 IP 地址。

前提
  • Ubuntu 电脑需要有全套的 SSH 服务(sshsshd)。
  • Debian 也需要有全套的 SSH 服务。
  • Windows 中需要有 SSH 客户端。
具体命令
在 Ubuntu 中
  1. 修改 SSH 配置文件

    sudo nano /etc/ssh/sshd_config
    

    修改或添加以下内容:

    GatewayPorts yes
    
  2. 重启 SSH 服务

    sudo systemctl restart sshd
    sudo systemctl restart ssh
    
  3. 开放端口 6666 并允许通过防火墙
    如何正确打开端口6666并允许通过防火墙,可自行搜索,本次测试使用:

    sudo ufw disable  # 关闭防火墙
    sudo nft add rule inet filter input tcp dport 6666 accept
    

    如果没有 nft,可以使用以下命令安装:

    sudo apt install nft
    

    注意:如果重启或注销,6666 端口可能会被关闭。由于验证功能,不需要固化到系统中。

在 Debian 中
  1. 安装 SSH 服务

    sudo apt update
    sudo apt install ssh
    
  2. 重启 SSH 服务

    sudo systemctl restart sshd
    sudo systemctl restart ssh
    
  3. 建立反向隧道

    ssh -p 22 -qngfNTR 0.0.0.0:6666:0.0.0.0:22 systemuser2@172.18.1.168
    

    提示

    The authenticity of host '172.18.1.168 (172.18.1.168)' can't be established.
    ED25519 key fingerprint is SHA256:/4KPZ2tdSMIKRIMfWRI7TPzWMli5b1OHtQdh0zRT9b0.
    This key is not known by any other names.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes  # 输入yes
    systemuser2@172.18.1.168's password:	#输入Ubuntu的密码
    

    执行完毕后自动退出,因为有一个参数是后台执行的意思。

在 Windows 中
  1. 使用 SSH 命令连接到 Ubuntu

    ssh systemuser2@172.18.1.168
    

    输入 Ubuntu 的密码。

  2. 通过 Ubuntu 的反向隧道连接到 Debian

    ssh -p 6666 tukuku01@127.0.0.1
    

    输入 Debian 的密码。

    完整输出

    C:\Users\shaoz>ssh systemuser2@172.18.1.168
    systemuser2@172.18.1.168's password:
    Welcome to Ubuntu 20.04.6 LTS (GNU/Linux 5.15.0-131-generic x86_64)
    ******
    Last login: Mon Mar 24 10:18:49 2025 from 172.16.30.87
    xhost:  unable to open display ""
    systemuser2@kukutu:~$ ssh -p 6666 tukuku01@127.0.0.1
    tukuku01@127.0.0.1's password:
    Linux tukuku-vm1 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1 (2025-03-06) x86_64
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: Mon Mar 24 10:19:38 2025 from ::1
    tukuku01@tukuku-vm1:~$
    
  3. 直接从 Windows 连接到 Debian

    ssh -p 6666 tukuku01@172.18.1.168
    

    完整输出

    C:\Users\shaoz>ssh -p 6666 tukuku01@172.18.1.168
    The authenticity of host '[172.18.1.168]:6666 ([172.18.1.168]:6666)' can't be established.
    ED25519 key fingerprint is SHA256:HoC4CmexS/2TWJxn3MZkKgSLsIUmaPQuyezPC5F2EmQ.
    This host key is known by the following other names/addresses:
        C:\Users\shaoz/.ssh/known_hosts:15: 192.168.92.141
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    Warning: Permanently added '[172.18.1.168]:6666' (ED25519) to the list of known hosts.
    tukuku01@172.18.1.168's password:
    Linux tukuku-vm1 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1 (2025-03-06) x86_64
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: Mon Mar 24 14:47:53 2025 from 127.0.0.1
    tukuku01@tukuku-vm1:~$ ls
    公共  模板  视频  图片  文档  下载  音乐  桌面
    tukuku01@tukuku-vm1:~$ ll
    -bash: ll: 未找到命令
    tukuku01@tukuku-vm1:~$
    
待优化
  • 命令中未出现 Debian 的地址:虽然实现了目标,但命令中并未直接使用 Debian 的地址,而是通过 Ubuntu 的反向隧道间接访问。
  • 直接从 Windows 连接到 Debian:目前需要先连接到 Ubuntu,再通过 Ubuntu 连接到 Debian,这不太合理。可能是因为在 Ubuntu 中未正确监听 0.0.0.0:6666 地址,而是监听了 127.0.0.1:6666。可以通过以下命令检查端口绑定:
    sudo netstat -tulnap | grep 6666
    
  • 修改配置文件:可能需要修改 /etc/ssh/ssh_config 文件,而不是 /etc/ssh/sshd_config 文件。
高级操作
  • 使用 WebSSH 在浏览器中访问
    • 在服务器上(本场景下使用 Ubuntu),环境和操作同上。
    • 参考:GitHub - huashengdun/webssh: Web based ssh client
    • 使用命令安装 WebSSH:
      pip3 install webssh
      wssh
      
      更多参数参考官网。
    • 在 Windows 浏览器中输入 Ubuntu 的环境地址,本次验证为:
      http://172.18.1.168:8888/
    • 输入 SSH 反向隧道的信息:
      • hostname: 172.18.1.168
      • port: 6666
      • Username: tukuku01
      • Password: Debian 的密码
    • 进入 Debian 系统中的命令行。

通过上述步骤,你可以在 Windows 上通过浏览器访问内网的 Debian 系统,而无需直接使用 Debian 的 IP 地址。
在这里插入图片描述


http://www.kler.cn/a/611688.html

相关文章:

  • Golang io模块详细功能介绍与示例
  • 数据大屏点亮工业互联网的智慧之眼
  • linux网络编程以及epoll IO多路复用
  • 计算机网络基础:量子通信技术在网络中的应用前景
  • 解决Cubemx生产的 .ioc文件不能外部打开的方法
  • Vulhub靶机--FAll
  • 数据湖的崛起:从大数据到智能未来的钥匙
  • CMake入门及生成windows下的项目示例讲解
  • Postman 请求头详解:快速掌握
  • flutter 获取设备的唯一标识
  • 国产 FPGA 的崛起之路,能否打破 Xilinx 的垄断?
  • nodejs-原型污染链
  • 基于核选择融合注意力机制TCN-MTLATTENTION-MAMBA模型(Python\matlab代码)
  • 【点盾云】加密技术如何防止视频内容随意传播?
  • Windows卸载以压缩包形式安装的MySQL
  • qt+opengl 加载三维obj文件
  • 跨网段投屏(by quqi99)
  • STM32编写触摸按键
  • 安全工具膨胀的隐性成本及其解决方法
  • 使用string和string_view(二)——数值转换、std::string_view和非标准字符串