【问题标题】:Reverse SSH tunnel monitor反向 SSH 隧道监控
【发布时间】:2010-09-27 05:06:04
【问题描述】:

我使用 PuTTY 设置了一个反向 ssh 隧道,让我无需启用 NAT 端口转发即可将 VNC 连接到家用计算机。效果很好,没问题。

我想将隧道设置为“持久服务”,该服务将在启动时连接并在丢弃时重新连接。 PS。这是在 Windows 上。

详尽的谷歌搜索发现了一些产品,但许多产品似乎已被放弃,而且似乎没有一个具有主要的“街头信誉”。

有没有人使用过这种类型的东西或任何这些产品?我不需要所有的花里胡哨,只需要可靠性。

【问题讨论】:

标签: windows ssh monitoring tunnel


【解决方案1】:

您是否考虑过使用 plink 并将其作为srvany 的服务?

【讨论】:

【解决方案2】:

wikipedia's comparison of ssh clients 有用于隧道、SOCKS 等的列,可以帮助您找到合适的东西

【讨论】:

    【解决方案3】:

    您可以将任何应用程序设置为从 Windows 启动并在启动时自动连接您的隧道。我个人使用Easytunnel ...刚刚检查了启动时连接所有隧道的选项,并设置了启动时启动Easytunnel的窗口。效果很好,但您需要设置服务器的不活动超时,否则您将每 10 分钟左右断开连接。

    希望你能成功!

    【讨论】:

      【解决方案4】:

      使用 PuTTY 中的 plink 并在批处理文件中运行。当连接真的挂掉时,plink 将退出,这意味着您可以循环运行 plink。

      像这样:

        :: This is a batch file. Save with file name: plink_forever.bat
        :restart
        plink saved_session_name
        goto restart
      

      最后用 srvany 包装它,让它在登录时启动。

      或者更简单:将 .bat 放入 Windows 调度程序并设置为每次启动时运行一次。

      文档:http://the.earth.li/~sgtatham/putty/0.58/htmldoc/Chapter7.html

      【讨论】:

        【解决方案5】:

        我经常使用 ssh 隧道,但所有管理器都对我不方便(UI 屏幕太多,不太稳定)。我想要一个可以轻松配置和维护的脚本,所以我想出了一个 PowerShell 脚本。发布here。 SO 规则也要求我在答案中发布解决方案,很高兴这样做:

        要开始使用它,您需要这样的配置:

        # LocalPort TargetHost  TargetPort  SshHost SshUsername SshKeyPath 
        18080   google.com  80  bastion.example.com User    D:\secure\path\to\private_key.ppk
        

        将其保存为 config.csv。并使用 powershell 脚本来保持它是:

        <#
        .SYNOPSIS
          Powershell script for keeping ssh tunnel up and running
        
        .DESCRIPTION
          This script uses configuration of tunnels located in config.csv. For more information visit http://tsherlock.tech/2019/03/13/simple-ssh-tunnel-auto-reconnect-using-putty-and-powershell/
        
        .NOTES
          Version:        1.0
          Author:         Anton Shkuratov
          Creation Date:  2019-03-13
          Purpose/Change: Initial script development
        
        #>
        
        $currentDir = $PSScriptRoot
        if (-not $env:PATH.Contains($currentDir)) {
          $env:PATH="$env:PATH;$currentDir"
        }
        
        # Check plink is accessible
        try {
          Start-Process plink.exe -WindowStyle Hidden
        } catch {
          Write-Host Error running plink.exe Please make sure its path is in PATH environment variable
          EXIT 1
        }
        
        # Parse config
        $config = [System.IO.File]::ReadAllLines("$currentDir\config.csv");
        $bindings = New-Object System.Collections.ArrayList
        $regex = New-Object System.Text.RegularExpressions.Regex("(\d)+\s([^ ]+)\s(\d+)\s([^ ]+)\s([^ ]+)\s([^ ]+)", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase);
        $keyPasswords = @{}
        $procs = @{}
        
        foreach($line in $config) {
          $match = $regex.Match($line)
        
          if ($match.Success) {
            $sshKey = $match.Groups[6];
        
            $bindings.Add(@{
              LocalPort = $match.Groups[1];
              TargetHost = $match.Groups[2];
              TargetPort = $match.Groups.Groups[3];
              SshHost = $match.Groups[4];
              SshUser = $match.Groups[5];
              SshKey = $match.Groups[6];
            });
        
            if (-not $keyPasswords.ContainsKey($sshKey)) {
              $pass = Read-Host "Please enter password for key (if set): $sshKey" -AsSecureString
              $keyPasswords.Add($sshKey, $pass);
            }
          }
        }
        
        # Starting Processes
        function EnsureRunning($procs, $keyPasswords, $binding) {
        
          if ($procs.ContainsKey($binding) -and $procs[$binding].HasExited) {
        
            $proc = $procs[$binding]
            $sshKey = $binding.sshKey
            $out = $proc.StandardError.ReadToEnd()
        
            if ($out.Contains("Wrong passphrase")) {
              Write-Host "Wrong pass phrase for $sshKey, please re-enter"
              $pass = Read-Host "Please enter password for key: $sshKey" -AsSecureString
              $keyPasswords[$sshKey] = $pass;
            } else {
              $exitCode = $proc.ExitCode
              $tHost = $binding.sshHost
        
              Write-Host "Connection to $tHost is lost, exit code: $exitCode"
            }
          }
        
          if (-not $procs.ContainsKey($binding) -or $procs[$binding].HasExited) {
            $sshUser = $binding.SshUser
            $sshHost = $binding.SshHost
            $sshKey = $binding.SshKey
            $lPort = $binding.LocalPort
            $tPort = $binding.TargetPort
            $tHost = $binding.TargetHost
            $sshKeyPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($keyPasswords[$sshKey]))
        
            $psi = New-Object System.Diagnostics.ProcessStartInfo;
            $psi.FileName = "plink.exe";
            $psi.UseShellExecute = $false;
        
            $psi.CreateNoWindow = $true;
            $psi.RedirectStandardInput = $true;
            $psi.RedirectStandardError = $true;
        
            $psi.Arguments = "-ssh $sshUser@$sshHost -i `"$sshKey`" -batch -pw $sshKeyPass -L $lPort`:$tHost`:$tPort"
        
            $proc = [System.Diagnostics.Process]::Start($psi);
        
            Start-Sleep 1
        
            if (-not $proc.HasExited) {
              Write-Host Connected to $sshUser@$sshHost
            }
        
            $procs[$binding] = $proc;
          }
        }
        
        function EnsureAllRunning($procs, $keyPasswords, $bindings) {
          while($true) {
            foreach($binding in $bindings) {
              EnsureRunning $procs $keyPasswords $binding
            }
            Start-Sleep 1
          }
        }
        
        
        try {
          # Waiting for exit command
          Write-Host Working... Press Ctrl+C to stop execution...
          EnsureAllRunning $procs $keyPasswords $bindings
        } finally {
          # Clean up
          Write-Host Clean up
        
          foreach($proc in $procs.Values) {
            if ($proc -ne $null -and -not $proc.HasExited) {
              $proc.Kill();
            }
          }
        }
        

        然后运行它:

        powershell -File autossh.ps1
        

        要在 Windows 启动时自动启动它,请使用 Windows 调度程序。

        【讨论】:

          【解决方案6】:

          永久隧道是一种安全漏洞。 只要您在网络上,我就已经设置了一个安全的开放服务并打开。它也有内置超时,没有活动 2 分钟,否则 10。超过 https 并且顶部有一些 XTEA 加密。可在 mylinuz.com 获得

          【讨论】:

            【解决方案7】:

            我有两个主要建议:

            • Teleport: 很棒的工具,开源且相对容易使用
            • Ngrok:简单,做你想做的事

            我建议您使用其中一项服务,而不是自己动手。自行设置此类设置可能很危险,因为任何错误配置都会导致攻击者获得对所有连接设备的完全访问权限。

            【讨论】:

              猜你喜欢
              • 2012-06-06
              • 1970-01-01
              • 2018-09-01
              • 2021-09-06
              • 1970-01-01
              • 2015-07-28
              • 2020-09-15
              • 2015-03-09
              • 2019-08-12
              相关资源
              最近更新 更多