2023年9月7日 星期四

Windows 開發筆記 - 開機自動啟動 SSH Reverse Tunnel / autossh @ Windows 11, cygwin

視窗鍵 + R 輸入 shell:startup

想說要善加利用 Windows 筆電,就想到把它規劃成算力單位後,以及思考如何自動化叫他做事。在資安角度上就可採用 SSH Reverse Tunnel 方案,讓筆電開機啟動後,建立一個連線到指定的機器候命。如此,在 Windows 筆上安裝了一些服務後,就可以用遠端 Port Forwarding 的方式存取到。

首先,要先找到 autossh 這種用法,不然單建立一個 ssh 掛了很麻煩的。雖然有一些 github 的可挑,但整體上要找一個夠信任的來源,最後選擇知名的 cygwin ,就順便安裝 autossh, tmux, vim, wget, lftp, git, zip, unzip 等,如此 autossh.exe 就搞定,位置在 C:\cygwin64\bin\autossh.exe

之前的文章已提到,我在 Windows 11 下已啟用 OpenSSH server 了,那我可以測試把 openssh server 服務建立反向的連線(須留意 cygwin 認定的使用者家目錄跟 Windows PowerShell 的不一樣,需要先建立好 keypair 等資料)

C:\cygwin64\bin\autossh.exe -M 0 -N -R 10022:localhost:22 ServerUser@RemotServer

如此,在指定的機器(RemoteServer)上,就可以測試連線:

$ telnet localhost 10022
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_for_Windows_8.6
^C^C
Connection closed by foreign host.

對於 cygwin 方面,目前採用 Windows 內建的 OpenSSH server 方案,連入後是 Windows Powershell 環境,若想切換到 cygwin ,可以多打一下:

Microsoft Windows [版本 10.0.22621.2215]

(c) Microsoft Corporation. 著作權所有,並保留一切權利。


user@WINDOWS-DESKTOP C:\Users\user>c:\cygwin64\Cygwin.bat


user@windows-desktop ~

$ 


對於 cygwin 用法,正規的安裝軟體還是透過原本的 setup.exe 去擴充,然而,可以去下載 github.com/transcode-open/apt-cyg 來使用,他可以提供一些便利的 command line 安裝套件的方式(但實務上不幸踩過失敗),當作一個備用方式:

user@windows-desktop ~

$ curl -s https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg


user@windows-desktop ~

$ chmod 700 ./apt-cyg 


user@windows-desktop ~

$ ./apt-cyg 

NAME

  apt-cyg - package manager utility


SYNOPSIS

  apt-cyg [operation] [options] [targets]


DESCRIPTION

  apt-cyg is a package management utility that tracks installed packages on a   

  Cygwin system. Invoking apt-cyg involves specifying an operation with any     

  potential options and targets to operate on. A target is usually a package    

  name, file name, URL, or a search string. Targets can be provided as command  

  line arguments.


OPERATIONS

  install

    Install package(s).


  remove

    Remove package(s) from the system.


  update

    Download a fresh copy of the master package list (setup.ini) from the       

    server defined in setup.rc.


  download

    Retrieve package(s) from the server, but do not install/upgrade anything.   


  show

    Display information on given package(s).


  depends

    Produce a dependency tree for a package.


  rdepends

    Produce a tree of packages that depend on the named package.


  list

    Search each locally-installed package for names that match regexp. If no    

    package names are provided in the command line, all installed packages will 

    be queried.


  listall

    This will search each package in the master package list (setup.ini) for    

    names that match regexp.


  category

    Display all packages that are members of a named category.


  listfiles

    List all files owned by a given package. Multiple packages can be specified 

    on the command line.


  search

    Search for downloaded packages that own the specified file(s). The path can 

    be relative or absolute, and one or more files can be specified.


  searchall

    Search cygwin.com to retrieve file information about packages. The provided 

    target is considered to be a filename and searchall will return the

    package(s) which contain this file.


  mirror

    Set the mirror; a full URL to a location where the database, packages, and  

    signatures for this repository can be found. If no URL is provided, display 

    current mirror.


  cache

    Set the package cache directory. If a file is not found in cache directory, 

    it will be downloaded. Unix and Windows forms are accepted, as well as      

    absolute or regular paths. If no directory is provided, display current     

    cache.


OPTIONS

  --nodeps

    Specify this option to skip all dependency checks.


  --version

    Display version and exit.


下一步回歸到正題 - 開機自動執行,則是可以參考微軟官方文件 - 新增的應用程式以在 Windows 10 啟動時自動執行。此例在 Windows 11 上,透過 視窗鍵+R 執行 shell:startup 可快速開啟對應的目錄位置 ( %HOME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup ),只需在此目錄添加幾個 bat 檔案,開啟就會幫我運行這指令了,如:

00_ssh-reverse-tunnel_openssh-server.bat

內容:

C:\cygwin64\bin\autossh.exe -M 0 -N -R 10022:localhost:22 ServerUser@RemotServer

開啟後,他會佔一個小視窗。由於那台筆電本身就是閒置的,所以佔著小視窗反而更好觀測 autossh 運作是否正常

收工!

沒有留言:

張貼留言