Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SSH–;在登录时强制执行命令,即使没有Shell_Ssh_Sshd - Fatal编程技术网

SSH–;在登录时强制执行命令,即使没有Shell

SSH–;在登录时强制执行命令,即使没有Shell,ssh,sshd,Ssh,Sshd,我正在创建一个没有shell的受限用户,仅用于端口转发,我需要在通过pubkey登录时执行一个脚本,即使该用户是通过ssh-N连接的user@host它不会向SSH服务器请求shell 脚本应该在使用pubkey验证的连接上警告管理员,因此连接的用户不应该能够跳过脚本的执行(例如,通过连接ssh-N) 我试过了,但没有用: 将命令设置为/etc/ssh/sshrc 在.ssh/authorized_-keys中使用command=“command”(手动授权_-keys) 使用命令作为用户的s

我正在创建一个没有shell的受限用户,仅用于端口转发,我需要在通过pubkey登录时执行一个脚本,即使该用户是通过
ssh-N连接的user@host
它不会向SSH服务器请求shell

脚本应该在使用pubkey验证的连接上警告管理员,因此连接的用户不应该能够跳过脚本的执行(例如,通过连接
ssh-N

我试过了,但没有用:

  • 将命令设置为
    /etc/ssh/sshrc
  • .ssh/authorized_-keys
    中使用command=“command”(手动授权_-keys)
  • 使用命令作为用户的shell设置脚本。(
    chsh-s/sbin/myscript.sh用户名
  • /etc/ssh/sshd\u config
    中匹配用户,如:
    
    匹配用户MYUSERNAME
    ForceCommand“/sbin/myscript.sh”
    
当用户请求shell时,所有这些都可以工作,但如果只记录端口转发而没有shell(
ssh-N
),则无法工作。

除非客户端请求PTY,否则该选项在没有PTY的情况下运行。因此,您实际上没有一个shell来以您可能期望的方式执行脚本。此外,OpenSSH SSHD_CONFIG(5)手册页明确指出:

通过使用带有-c选项的用户登录shell调用该命令

这意味着,如果您禁用了用户的登录shell,或者将其设置为类似于
/bin/false
,那么ForceCommand将无法工作。假设:

  • 用户定义了一个合理的shell
  • 目标脚本是可执行的,以及
  • 你的脚本有一个合适的shebang行
  • 使用正确的用户名和自定义脚本的完全限定路径名对全局sshd_config文件进行正确修改后,以下内容应能在该文件中工作:

    Match User foo
        ForceCommand /path/to/script.sh
    

    我是作品的作者;我得出的结论是,我需要实现的是,仅使用SSH到目前为止是不可能的(
    OpenSSH_6.9p1 Ubuntu-2,OpenSSL 1.0.2d 2015年7月9日
    ),但我发现了一个伟大的软件,它使用加密垃圾邮件验证来打开SSH端口及其新版本(到本文发表之日,它是GitHub master分支)具有始终执行用户成功授权的命令的功能

    FWKNOP-加密单包授权 FWKNOP设置iptables规则,允许通过UDP发送的单个加密数据包访问给定端口。然后在授权之后,它允许授权用户在给定的时间内访问,例如30秒,在此之后关闭端口,保持连接打开

    1.要在Ubuntu linux上安装,请执行以下操作: 到目前为止,Ubuntu存储库上的当前版本(2.6.0-2.1build1)仍然不允许在成功的SPA上执行命令;(请改用GitHub的2.6.8)

    在客户端计算机上:

    sudo-apt-get-install-fwknop客户端

    在服务器端:

    sudo-apt-get-install-fwknop服务器

    下面是关于如何设置客户端和服务器机器的教程

    然后在设置后,在服务器端:

  • 编辑
    /etc/default/fwknop服务器
  • 将行
    START\u DAEMON=“no”
    更改为
    START\u DAEMON=“yes”
  • 然后运行:

    sudo服务fwknop服务器停止

    sudo服务fwknop服务器启动

  • 2.成功SPA时警告管理员(电子邮件、pushover脚本等) 因此,如上所述,Ubuntu存储库中的当前版本(2.6.0-2.1build1)无法在成功的SPA上执行命令。如果您在OP时需要此功能,但将在fwknop版本(2.6.8)中发布,如下所述:

    因此,如果您现在需要使用它,您可以从github branch master构建,它具有
    CMD\u CYCLE\u OPEN
    选项

    3.更多关于fwknop的资源

    (GitHub项目)

    (项目现场)


    (DO's community教程)

    我是这篇文章的作者。此外,您还可以实现一个简单的logwatcher,如下面用python3编写的那样,它不断读取文件,并在行包含模式时执行命令

    logwatcher.python3
    用法
  • 使上述脚本可执行:
  • chmod+x logwatcher.python3

  • 添加cronjob以在重新启动后启动它
  • crontab-e

    然后在此处写下这一行,然后保存:

    @reboot/home/YOURUSERNAME/logwatcher.python3”/var/log/auth.log“会话已为用户”“/sbin/myscript.sh”“打开”

    此脚本的第一个参数是要查看的日志文件,第二个参数是要在其中查找的字符串。第三个参数是在文件中找到行时要执行的脚本


    最好使用更可靠的方法来启动/重新启动脚本,以防脚本崩溃。

    如果只需要运行脚本,则可以依靠
    pam\u exec

    基本上,您引用了需要在
    /etc/pam.d/sshd
    配置中运行的脚本:

    session optional pam_exec.so seteuid /path/to/script.sh
    
    经过一些测试后,您可能需要将
    可选
    更改为
    必需

    有关类似的请求,请参阅此答案“”

    事实上,在脚本中,只有有限的环境变量子集可用:

    LANGUAGE=en_US.UTF-8
    PAM_USER=bitnami
    PAM_RHOST=192.168.1.17
    PAM_TYPE=open_session
    PAM_SERVICE=sshd
    PAM_TTY=ssh
    LANG=en_US.UTF-8
    LC_ALL=en_US.UTF-8
    PWD=/
    
    如果您想从
    授权密钥
    获取用户信息,此脚本可能会有所帮助:

    #!/bin/bash
    # Get user from authorized_keys
    # pam_exec_login.sh
    # * [ssh - What is the SHA256 that comes on the sshd entry in auth.log? - Server Fault](https://serverfault.com/questions/888281/what-is-the-sha256-that-comes-on-the-sshd-entry-in-auth-log)
    # * [bash - How to get all fingerprints for .ssh/authorized_keys(2) file - Server Fault](https://serverfault.com/questions/413231/how-to-get-all-fingerprints-for-ssh-authorized-keys2-file)
    
    # Setup log
    b=$(basename $0| cut -d. -f1)
    log="/tmp/${b}.log"
    
    function timeStamp () {
      echo "$(date '+%b %d %H:%M:%S') ${HOSTNAME} $b[$$]:"
    }
    
    # Check if opening a remote session with sshd
    if [ "${PAM_TYPE}" != "open_session" ] || [ $PAM_SERVICE != "sshd" ] || [ $PAM_RHOST == "::1" ]; then
      exit $PAM_SUCCESS
    fi
    
    # Get info from auth.log
    authLogLine=$(journalctl -u ssh.service |tail -100 |grep "sshd\[${PPID}\]" |grep "${PAM_RHOST}")
    echo ${authLogLine} >> ${log}
    
    PAM_USER_PORT=$(echo ${authLogLine}| sed -r 's/.*port (.*) ssh2.*/\1/')
    PAM_USER_SHA256=$(echo ${authLogLine}| sed -r 's/.*SHA256:(.*)/\1/')
    
    # Get details from .ssh/authorized_keys
    authFile="/home/${PAM_USER}/.ssh/authorized_keys"
    PAM_USER_authorized_keys=""
    
    while read l; do
      if [[ -n "$l" && "${l###}" = "$l" ]]; then
        authFileSHA256=$(ssh-keygen -l -f <(echo "$l"))
        if [[ "${authFileSHA256}" == *"${PAM_USER_SHA256}"* ]]; then
          PAM_USER_authorized_keys=$(echo ${authFileSHA256}| cut -d" " -f3)
          break
        fi
      fi
    done < ${authFile}
    
    if [[ -n ${PAM_USER_authorized_keys} ]]
    then
      echo "$(timeStamp) Local user: ${PAM_USER}, authorized_keys user: ${PAM_USER_authorized_keys}" >> ${log}
    else
      echo "$(timeStamp) WARNING: no matching user in authorized_keys" >> ${log}
    fi
    
    #/bin/bash
    #从授权密钥获取用户
    #pam_exec_login.sh
    #*[ssh-auth.log中sshd条目上的SHA256是什么?-服务器故障](https://serverfault.com/questions/888281/what-is-the-sha256-that-comes-on-the-sshd-entry-in-auth-log)
    #*[bash-如何获取.ssh/authorized_key(2)文件的所有指纹-服务器故障](https://serverfault.com/questions/413231/how-to-get-all-fingerprints-for-ssh-authorized-keys2-f
    
    #!/bin/bash
    # Get user from authorized_keys
    # pam_exec_login.sh
    # * [ssh - What is the SHA256 that comes on the sshd entry in auth.log? - Server Fault](https://serverfault.com/questions/888281/what-is-the-sha256-that-comes-on-the-sshd-entry-in-auth-log)
    # * [bash - How to get all fingerprints for .ssh/authorized_keys(2) file - Server Fault](https://serverfault.com/questions/413231/how-to-get-all-fingerprints-for-ssh-authorized-keys2-file)
    
    # Setup log
    b=$(basename $0| cut -d. -f1)
    log="/tmp/${b}.log"
    
    function timeStamp () {
      echo "$(date '+%b %d %H:%M:%S') ${HOSTNAME} $b[$$]:"
    }
    
    # Check if opening a remote session with sshd
    if [ "${PAM_TYPE}" != "open_session" ] || [ $PAM_SERVICE != "sshd" ] || [ $PAM_RHOST == "::1" ]; then
      exit $PAM_SUCCESS
    fi
    
    # Get info from auth.log
    authLogLine=$(journalctl -u ssh.service |tail -100 |grep "sshd\[${PPID}\]" |grep "${PAM_RHOST}")
    echo ${authLogLine} >> ${log}
    
    PAM_USER_PORT=$(echo ${authLogLine}| sed -r 's/.*port (.*) ssh2.*/\1/')
    PAM_USER_SHA256=$(echo ${authLogLine}| sed -r 's/.*SHA256:(.*)/\1/')
    
    # Get details from .ssh/authorized_keys
    authFile="/home/${PAM_USER}/.ssh/authorized_keys"
    PAM_USER_authorized_keys=""
    
    while read l; do
      if [[ -n "$l" && "${l###}" = "$l" ]]; then
        authFileSHA256=$(ssh-keygen -l -f <(echo "$l"))
        if [[ "${authFileSHA256}" == *"${PAM_USER_SHA256}"* ]]; then
          PAM_USER_authorized_keys=$(echo ${authFileSHA256}| cut -d" " -f3)
          break
        fi
      fi
    done < ${authFile}
    
    if [[ -n ${PAM_USER_authorized_keys} ]]
    then
      echo "$(timeStamp) Local user: ${PAM_USER}, authorized_keys user: ${PAM_USER_authorized_keys}" >> ${log}
    else
      echo "$(timeStamp) WARNING: no matching user in authorized_keys" >> ${log}
    fi