SSH–;在登录时强制执行命令,即使没有Shell
我正在创建一个没有shell的受限用户,仅用于端口转发,我需要在通过pubkey登录时执行一个脚本,即使该用户是通过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
ssh-N连接的user@host
它不会向SSH服务器请求shell
脚本应该在使用pubkey验证的连接上警告管理员,因此连接的用户不应该能够跳过脚本的执行(例如,通过连接ssh-N
)
我试过了,但没有用:
- 将命令设置为
/etc/ssh/sshrc
- 在
中使用command=“command”(手动授权_-keys).ssh/authorized_-keys
- 使用命令作为用户的shell设置脚本。(
)chsh-s/sbin/myscript.sh用户名
- 在
中匹配用户,如:/etc/ssh/sshd\u config
匹配用户MYUSERNAME ForceCommand“/sbin/myscript.sh”
ssh-N
),则无法工作。除非客户端请求PTY,否则该选项在没有PTY的情况下运行。因此,您实际上没有一个shell来以您可能期望的方式执行脚本。此外,OpenSSH SSHD_CONFIG(5)手册页明确指出:
通过使用带有-c选项的用户登录shell调用该命令
这意味着,如果您禁用了用户的登录shell,或者将其设置为类似于/bin/false
,那么ForceCommand将无法工作。假设:
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服务器启动
CMD\u CYCLE\u OPEN
选项
3.更多关于fwknop的资源
(GitHub项目)
(项目现场)
(DO's community教程)我是这篇文章的作者。此外,您还可以实现一个简单的logwatcher,如下面用python3编写的那样,它不断读取文件,并在行包含模式时执行命令
logwatcher.python3
用法
chmod+x logwatcher.python3
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