Skip to content
KitploitKITPLOIT
工具漏洞利用博客
Log in
提交
工具漏洞利用博客
提交

黑客、渗透测试和网络安全工具,武装您的安全武器库!

Kitploit 是一个黑客、网络安全和渗透测试工具的目录。发现最新的项目更新,查找漏洞、分析系统、自动化测试并加强你的安全。

订阅源联系隐私© 2026 Kitploit

工具目录

分类

查看所有分类
Loading categories
sshtunnel — SSH 隧道到远程服务器。 | Kitploit
工具/GitHubGitHub/pahaz/sshtunnel
通用工具网络安全实用工具与框架
GitHubpahaz/sshtunnel

sshtunnel

SSH 隧道到远程服务器。

查看仓库
1.3k202487天前Kitploit 审核通过
分享

|测试| |AppVeyor| |readthedocs| |coveralls| |版本|

|pyversions| |许可证|

作者:Pahaz_

仓库:https://github.com/pahaz/sshtunnel/

灵感来自 https://github.com/jmagnusson/bgtunnel,但该工具在 Windows 上无法运行。

另请参阅:https://github.com/paramiko/paramiko/blob/master/demos/forward.py

依赖要求

  • paramiko_

安装

sshtunnel_ 已发布在 PyPI 上,因此只需运行:

::

pip install sshtunnel

或 ::

easy_install sshtunnel

或 ::

conda install -c conda-forge sshtunnel

即可将其安装到你的环境中。

如需从源码安装,请克隆 仓库 <https://github.com/pahaz/sshtunnel>_ 并运行::

python setup.py install

测试软件包

要运行测试,你首先需要 tox <https://testrun.org/tox/latest/>_ 并运行::

python setup.py test

使用场景

下图展示了 sshtunnel 发挥作用的一个典型场景。用户可能需要连接远程服务器的某个端口(例如 8080),而该服务器只有 SSH 端口(通常为 22 端口)可达。::

----------------------------------------------------------------------

                            |
-------------+              |    +----------+
    LOCAL    |              |    |  REMOTE  | :22 SSH
    CLIENT   | <== SSH ========> |  SERVER  | :8080 web service
-------------+              |    +----------+
                            |
                         FIREWALL (only port 22 is open)

----------------------------------------------------------------------

图1:如何通过 SSH 隧道连接到被防火墙阻止的服务。

如果 SSH 服务器允许,还可以访问一个(从 REMOTE SERVER 的视角看)无法从外部(从 LOCAL CLIENT 的视角看)直接访问的私有服务器。::

----------------------------------------------------------------------

                            |
-------------+              |    +----------+               +---------
    LOCAL    |              |    |  REMOTE  |               | PRIVATE
    CLIENT   | <== SSH ========> |  SERVER  | <== local ==> | SERVER
-------------+              |    +----------+               +---------
                            |
                         FIREWALL (only port 443 is open)

----------------------------------------------------------------------

图2:如何通过 SSH 隧道连接到 PRIVATE SERVER。

使用示例

API 既支持初始化隧道并启动它,也支持使用 with 上下文,后者会自动负责启动和停止隧道:

示例 1

以下代码对应上面的图1,假设远程服务器地址为 pahaz.urfuclub.ru,使用密码认证,并随机分配本地绑定端口。

.. code-block:: python

from sshtunnel import SSHTunnelForwarder

server = SSHTunnelForwarder(
    'alfa.8iq.dev',
    ssh_username="pahaz",
    ssh_password="secret",
    remote_bind_address=('127.0.0.1', 8080)
)

server.start()

print(server.local_bind_port)  # show assigned local port
# work with `SECRET SERVICE` through `server.local_bind_port`.

server.stop()

示例 2

以下示例演示如何将端口转发到无法直接访问的私有服务器, 假设使用受密码保护的 pkey 认证,远程服务器的 SSH 服务 监听 443 端口,且该端口在防火墙中已开放(图2):

.. code-block:: python

import paramiko
import sshtunnel

with sshtunnel.open_tunnel(
    (REMOTE_SERVER_IP, 443),
    ssh_username="",
    ssh_pkey="/var/ssh/rsa_key",
    ssh_private_key_password="secret",
    remote_bind_address=(PRIVATE_SERVER_IP, 22),
    local_bind_address=('0.0.0.0', 10022)
) as tunnel:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('127.0.0.1', 10022)
    # do some operations with client session
    client.close()

print('FINISH!')

示例 3

为 Vagrant MySQL 本地端口进行端口转发的示例:

.. code-block:: python

from sshtunnel import open_tunnel
from time import sleep

with open_tunnel(
    ('localhost', 2222),
    ssh_username="vagrant",
    ssh_password="vagrant",
    remote_bind_address=('127.0.0.1', 3306)
) as server:

    print(server.local_bind_port)
    while True:
        # press Ctrl-C for stopping
        sleep(1)

print('FINISH!')

或者直接使用 CLI:

.. code-block:: console

(bash)$ python -m sshtunnel -U vagrant -P vagrant -L :3306 -R 127.0.0.1:3306 -p 2222 localhost

示例 4

打开一个跨越两条隧道的 SSH 会话。SSH 传输和隧道 将被守护化,在关闭时不会等待连接停止。

.. code-block:: python

import sshtunnel
from paramiko import SSHClient


with sshtunnel.open_tunnel(
    ssh_address_or_host=('GW1_ip', 20022),
    remote_bind_address=('GW2_ip', 22),
) as tunnel1:
    print('Connection to tunnel1 (GW1_ip:GW1_port) OK...')
    with sshtunnel.open_tunnel(
        ssh_address_or_host=('localhost', tunnel1.local_bind_port),
        remote_bind_address=('target_ip', 22),
        ssh_username='GW2_user',
        ssh_password='GW2_pwd',
    ) as tunnel2:
        print('Connection to tunnel2 (GW2_ip:GW2_port) OK...')
        with SSHClient() as ssh:
            ssh.connect('localhost',
                port=tunnel2.local_bind_port,
                username='target_user',
                password='target_pwd',
            )
            ssh.exec_command(...)

CLI 用法

::

$ sshtunnel --help
usage: sshtunnel [-h] [-U SSH_USERNAME] [-p SSH_PORT] [-P SSH_PASSWORD] -R
                 IP:PORT [IP:PORT ...] [-L [IP:PORT ...]] [-k SSH_HOST_KEY]
                 [-K KEY_FILE] [-S KEY_PASSWORD] [-t] [-v] [-V] [-x IP:PORT]
                 [-c SSH_CONFIG_FILE] [-z] [-n] [-d [FOLDER ...]]
                 ssh_address

Pure python ssh tunnel utils
Version 0.5.1

positional arguments:
  ssh_address           SSH server IP address (GW for SSH tunnels)
                        set with "-- ssh_address" if immediately after -R or -L

options:
  -h, --help            显示此帮助信息并退出
  -U SSH_USERNAME, --username SSH_USERNAME
                        SSH 服务器账户用户名
  -p SSH_PORT, --server_port SSH_PORT
                        SSH 服务器 TCP 端口(默认:22)
  -P SSH_PASSWORD, --password SSH_PASSWORD
                        SSH 服务器账户密码
  -R IP:PORT [IP:PORT ...], --remote_bind_address IP:PORT [IP:PORT ...]
                        远程绑定地址序列:ip_1:port_1 ip_2:port_2 ... ip_n:port_n
                        等同于 ssh -Lxxxx:IP_ADDRESS:PORT
                        如果省略端口,则默认为 22。
                        示例:-R 10.10.10.10: 10.10.10.10:5900
  -L [IP:PORT ...], --local_bind_address [IP:PORT ...]
                        本地绑定地址序列:ip_1:port_1 ip_2:port_2 ... ip_n:port_n
                        元素也可以是有效的 UNIX 套接字域:
                        /tmp/foo.sock /tmp/bar.sock ... /tmp/baz.sock
                        等同于 ssh -LPORT:xxxxxxxxx:xxxx,其中本地 IP 地址为可选。
                        默认情况下,它将监听所有接口(0.0.0.0)并选择一个随机端口。
                        示例:-L :40000
  -k SSH_HOST_KEY, --ssh_host_key SSH_HOST_KEY
                        网关的主机密钥
  -K KEY_FILE, --private_key_file KEY_FILE
                        RSA/DSS/ECDSA 私钥文件
  -S KEY_PASSWORD, --private_key_password KEY_PASSWORD
                        RSA/DSS/ECDSA 私钥密码
  -t, --threaded        允许对每个隧道进行并发连接
  -v, --verbose         增加输出详细程度(默认:ERROR)
  -V, --version         显示版本号并退出
  -x IP:PORT, --proxy IP:PORT
                        指向目的地的 SSH 代理的 IP 和端口
  -c SSH_CONFIG_FILE, --config SSH_CONFIG_FILE
                        SSH 配置文件,默认为 ~/.ssh/config
  -z, --compress        请求服务器通过 SSH 传输进行压缩
  -n, --noagent         禁用从 SSH 代理查找密钥
  -d [FOLDER ...], --host_pkey_directories [FOLDER ...]
                        可找到 SSH 公钥(格式为 `id_*`)的目录列表
下载工具

最受欢迎

查看全部 →

发现我们社区最常用的工具。

  • 1

    OsintHunter

    letox4/osinthunter

    🎯 终极 OSINT 工具包:调查图谱、置信度评分、别名家族、地图、时间线和报告。49 个模块、400+ 个站点,无需 API 密钥。

    806
  • 2

    ghostlock-app

    yukonga/ghostlock-app

    GhostLock 一键执行应用(CVE-2026-43499)

    523
  • 3

    ARTEX

    baarai/artex

    基于LLM的多智能体自主渗透测试平台,具备规划器/执行器智能体、资产与探索图谱、MITM流量记录以及漏洞复测功能。

    441
  • 4

    redforge

    cybersecurity91/redforge

    浏览器端红队工具包,包含 102 个离线工具,用于侦察、载荷生成、凭据攻击、加密、编码和交战报告。

    346
  • 5

    looking-glass

    anonghosty/looking-glass

    CSIRT 平台,可将 Shadowserver 报告导入 MongoDB,并将其重放为交互式地图、分析、发现、公告和高管仪表板。

    270
  • 6

    CyberStrikeAI

    ed1s0nz/cyberstrikeai

    AI原生网络安全的行动系统——在这里,意图转化为受治理的执行,证据沉淀为运营记忆,每一次操作都让下一次更出色。

    264

探索所有工具

浏览我们的工具集合

查看所有工具 →