更優於 Shellinabox 的 web shell 工具 -- ttyd

ttyd 是一個運行在服務端,客戶端通過web瀏覽器訪問從而連接後臺 tty (pts僞終端)接口的程序,把 shell 終端搬到 web 瀏覽器中。
更優於 Shellinabox 的 web shell 工具 -- ttyd


WebSocket

WebSocket 是 HTML5 開始提供的一種基於HTTP協議與服務器WebServer進行單會話、長連接、全雙工的網絡技術,依靠這種技術可以實現客戶端和服務端的長連接,雙向實時通信,以及基於服務端主動向客戶端的實時消息推送交互的底層技術棧。
它的最大特點就是,服務器可以主動向客戶端推送信息,客戶端也可以主動向服務器發送信息, 是真正的雙向平等對話,屬於服務器推送技術的一種。

其他特點包括:

  • 建立在 TCP 協議之上,服務器端的實現比較容易。
  • 與 HTTP 協議有着良好的兼容性。默認端口也是 80 和 443,並且握手階段採用 HTTP 協議,因此握手時不容易屏蔽,能通過各種 HTTP 代理服務器。
  • 數據格式比較輕量,性能開銷小,通信高效。
  • 可以發送文本,也可以發送二進制數據
  • 沒有同源限制,客戶端可以與任意服務器通信。
  • 協議標識符是ws(如果加密,則爲wss),服務器網址就是 URL。

    ttyd 不同於 Shellinabox 項目的底層原理,它使用基於HTML5的 WebSocket 協議,更少的http交互帶來更高效、更高性能、更低延時的交互式會話服務。

ttyd 也可以通過 Nginx 7層HTTP代理訪問,更高的靈活性。

ttyd 內建支持 SSL/TLS 傳輸層認證加密,支持客戶端/服務端的單向/雙向證書認證加密,更高的安全性。

OS Support:

* macOS
* Linux
* Microsoft Windows
* FreeBSD/OpenBSD
* OpenWrt/LEDE

Feature:

* C語言內建`libwebsockets`核心庫,高速、低內存消耗、高性能特性。 
* 基於`Xterm.js`的全功能終端,支持 CJK 不同語言和 IME 輸入法字符輸入。
* 窗口圖形化 `ZMODEM` 支持,服務端需安裝 `lrzsz` 程序。
* 可實現基於簡單的 Basic Auth 基本認證(不加密通信)。
* 可實現基於 OpenSSL 的傳輸層加密通信支持(加密通信)。
* 跨平臺: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows

Linux 平臺安裝方式:

  • 方式一:下載二進制程序文件,本地直接運行即可(推薦)
  • 方式二:下載源碼文件,安裝依賴庫編譯安裝
    sudo apt-get install cmake g++ pkg-config git vim-common libwebsockets-dev libjson-c-dev libssl-dev
    git clone https://github.com/tsl0922/ttyd.git
    cd ttyd && mkdir build && cd build
    cmake ..
    make && make install

    如果安裝libwebsockets-dev(版本過時)失敗,你可能需要單獨編譯安裝libwebsockets

使用方式:

####程序幫助信息:

ttyd is a tool for sharing terminal over the web

USAGE:
    ttyd [options] <command> [<arguments...>]

VERSION:
    1.5.2

OPTIONS:
    -p, --port              Port to listen (default: 7681, use `0` for random port)
    -i, --interface         Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock)
    -c, --credential        Credential for Basic Authentication (format: username:password)
    -u, --uid               User id to run with
    -g, --gid               Group id to run with
    -s, --signal            Signal to send to the command when exit it (default: 1, SIGHUP)
    -a, --url-arg           Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)
    -R, --readonly          Do not allow clients to write to the TTY
    -t, --client-option     Send option to client (format: key=value), repeat to add more options
    -T, --terminal-type     Terminal type to report, default: xterm-256color
    -O, --check-origin      Do not allow websocket connection from different origin
    -m, --max-clients       Maximum clients to support (default: 0, no limit)
    -o, --once              Accept only one client and exit on disconnection
    -B, --browser           Open terminal with the default system browser
    -I, --index             Custom index.html path
    -6, --ipv6              Enable IPv6 support
    -S, --ssl               Enable SSL
    -C, --ssl-cert          SSL certificate file path
    -K, --ssl-key           SSL key file path
    -A, --ssl-ca            SSL CA file path for client certificate verification
    -d, --debug             Set log level (default: 7)
    -v, --version           Print the version and exit
    -h, --help              Print this text and exit

Visit https://github.com/tsl0922/ttyd to get more information and report bugs.

用例:

./ttyd -p 8080 bash -x
# 頁面以當前用戶的身份和環境變量進行後臺連接,fork 的方式爲 bash 調試方式運行。
./ttyd --max-clients 2 -u 500 top
# 以指定的系統用戶身份的uid運行服務,登錄頁面即爲運行 top 命令的輸出結果。

運行系統賬戶登錄認證

./ttyd --max-clients 2 login
# 以當前用戶身份運行服務。

注意:如需root身份登錄終端,需要在屬主屬組爲 root 的 /etc/securetty文件中增加可登錄的終端類型pts/0 pts/1 pts/2,當然也需要運行 ttyd 程序的用戶有可讀 /etc/securetty 文件的權限(當然也就是以 root 身份啓動 ttyd 服務)。
所以,爲安全性考慮,不建議以 root 身份啓動 ttyd 服務,也就不建議直接以 root 賬戶登錄 ttyd websockets頁面僞終端。

運行特定的後臺交互式服務

# console
./ttyd --max-clients 2 -a mysql -h localhost 
// -a, --url-arg 允許url攜帶參數,參數將直接應用到 command 程序中。

# browser 1  使用 command 默認參數
http://localhost:7681

Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

# browser 2   在url請求中使用指定的 command 參數
http://localhost:7681/?arg=-uadmin&arg=-pPASSWORD&arg=mysql

mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

通過 ZMODEM 上傳下載文件,服務端需安裝 lrzsz

# 向客戶端發送本地文件
sz a.log 

更優於 Shellinabox 的 web shell 工具 -- ttyd

# 服務端接收客戶端上傳的文件
rz

更優於 Shellinabox 的 web shell 工具 -- ttyd

使用 SSL 加密傳輸流量

本例使用本地自簽證書示例

# CA certificate (FQDN must be different from server/client)
openssl genrsa -out ca.key 2048
openssl req -new -x509 -days 365 -key ca.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./CN=Acme Root CA" -out ca.crt

# server certificate (for multiple domains, change subjectAltName to: DNS:example.com,DNS:www.example.com)
openssl req -newkey rsa:2048 -nodes -keyout server.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./CN=localhost" -out server.csr
openssl x509 -sha256 -req -extfile <(printf "subjectAltName=DNS:localhost") -days 365 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt

# 雙向認證 client certificate (the p12/pem format may be useful for some clients)(可選)
openssl req -newkey rsa:2048 -nodes -keyout client.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./CN=client" -out client.csr
openssl x509 -req -days 365 -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt
openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.p12
openssl pkcs12 -in client.p12 -out client.pem -clcerts

服務端單向認證

./ttyd --ssl --ssl-cert server.crt --ssl-key server.key bash

更優於 Shellinabox 的 web shell 工具 -- ttyd

客戶端/服務端雙向認證,需增加--ssl-ca選項,桌面瀏覽器另外需要單獨安裝 client.p12證書到本地

./ttyd --ssl --ssl-cert server.crt --ssl-key server.key --ssl-ca ca.crt bash

更優於 Shellinabox 的 web shell 工具 -- ttyd
更優於 Shellinabox 的 web shell 工具 -- ttyd

在 curl 中測試證書驗證

curl --insecure --cert client.p12[:password] -v https://localhost:7681
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章