黑帽联盟

 找回密码
 会员注册
查看: 328|回复: 0
打印 上一主题 下一主题

[资源教程] CentOS 7使用ttyd搭建一个WEB共享终端(WebSSH)

[复制链接]

852

主题

38

听众

3177

积分

白金VIP

Rank: 8Rank: 8

  • TA的每日心情
    开心
    2024-3-7 12:52
  • 签到天数: 1538 天

    [LV.Master]伴坛终老

    ttyd 是一个简单的命令行工具,用于在 Web 上共享终端,简单点说就是可以实现在网页上使用SSH终端服务,并且该软件是免费开源的。
    42da1587138982-1.gif


    安装ttyd
    ttyd作者已经提供编译好的二进制文件,直接下载即可使用,最新版下载地址为:https://github.com/tsl0922/ttyd/releases,这里以CentOS 7为例:
    1. #下载ttyd
    2. wget -O ttyd https://github.com/tsl0922/ttyd/releases/download/1.6.0/ttyd_linux.x86_64

    3. #添加执行权限
    4. chmod +x ttyd

    5. #移动目录
    6. mv ttyd /usr/sbin
    复制代码


    通过上面的几个步骤,我们已经完成ttyd安装,输入命令ttyd -v可查看当前版本:
    1. [root@hosta29d0ffef5 ~]# ttyd -v
    2. ttyd version 1.6.0-c15cfb7
    复制代码


    运行ttyd
    输入命令ttyd bash运行ttyd,注意防火墙需要放行7681端口,然后浏览器访问http://IP:7681即可打开WEB终端,如下图。
    2121.png

    不过ttyd并没有保持后台运行,访问7681也不需要任何密码验证,非常不安全,接下来我们为ttyd创建一个systemd服务并设置用户名、密码验证。

    新建服务
    创建一个ttyd.service文件:vi /etc/systemd/system/ttyd.service内容如下:
    1. [Unit]
    2. Description=ttyd
    3. After=network.target

    4. [Service]
    5. ExecStart=/usr/sbin/ttyd -c cnblackhat:cnblackhat.com bash

    6. [Install]
    7. WantedBy=multi-user.target
    复制代码


    创建完毕后输入命令:systemctl daemon-reload让daemon生效。

    上面使用了-c参数,这个参数的含义是设置用户名、密码验证,格式为-c 用户名:密码,上方设置的用户名为cnblackhat,密码为cnblackhat.com,请自行修改为自己的用户名、密码。

    服务创建后,我们可以使用systemd命令来进行管理了,命令如下:
    1. #启动ttyd
    2. systemctl start ttyd

    3. #停止ttyd
    4. systemctl stop ttyd

    5. #重启ttyd
    6. systemctl restart ttyd

    7. #开机启动
    8. systemctl enable ttyd
    复制代码


    Nginx反向代理
    如果您不喜欢通过IP + 端口的访问形式,也可以设置Nginx反向代理通过域名访问,配置如下:
    如果是网站根目录
    1. location / {
    2.     proxy_http_version 1.1;
    3.     proxy_set_header Host $host;
    4.     proxy_set_header X-Forwarded-Proto $scheme;
    5.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    6.     proxy_set_header Upgrade $http_upgrade;
    7.     proxy_set_header Connection "upgrade";
    8.     proxy_pass http://127.0.0.1:7681;
    9. }
    复制代码


    如果是网站二级目录
    1. location ~ ^/ttyd(.*)$ {
    2.     proxy_http_version 1.1;
    3.     proxy_set_header Host $host;
    4.     proxy_set_header X-Forwarded-Proto $scheme;
    5.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    6.     proxy_set_header Upgrade $http_upgrade;
    7.     proxy_set_header Connection "upgrade";
    8.     proxy_pass http://127.0.0.1:7681/$1;
    9. }
    复制代码
    注意上面的ttyd可以修改为自己想要的路径。


    ttyd参数说明
    输入ttyd -h可以查看ttyd帮助,说明如下:
    1. USAGE:
    2.     ttyd [options] <command> [<arguments...>]

    3. VERSION:
    4.     1.6.0

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

    30. Visit https://github.com/tsl0922/ttyd to get more information and report bugs.
    复制代码



    总结
    使用ttyd可以很方便快速的搭建一个WebSSH服务,但便利就意味着要承担更多的安全风险,虽然ttyd提供了基本的密码验证,但这种验证方式仍然不安全,使用ttyd的同时意味着你的服务器也多了一个入口,所以不建议用在生产环境,自己折腾倒是无所谓。

    • ttyd项目地址:https://github.com/tsl0922/ttyd
    • ttyd官方主页:https://tsl0922.github.io/ttyd/

    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    勿忘初心,方得始终!
    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部