Bash 登录脚本奇怪的行为

Bash 登录脚本奇怪的行为

我目前正在制作一个蜜罐,一旦用户通过 SSH 成功登录,它将记录所有用户输入。

它的工作原理如下:我将蜜罐用户admin的默认 shell 设置为 bash 脚本:

admin:x:1001:1001::/home/admin:/home/admin/HoneyPot/spawner.sh

spawner.sh脚本将继续启动except脚本并使用 记录输出script

#!/bin/bash
cd /home/admin/HoneyPot/template/

pwd
script -c "./script.exp" -t 2> timing.log -a output.session #start recording session, execute except script
echo "we should not get here"

以下是前几行script.exp

#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Mon Oct  5 13:55:35 
# boilerplate comments and code:

set force_conservative 0  ;# set to 1 to force conservative mode even if
              ;# script wasn't run conservatively originally
if {$force_conservative} {
    set send_slow {1 .1}
    proc send {ignore arg} {
        sleep .1
        exp_send -s -- $arg
    }
}

# start of my code

set timeout -1
spawn emulator #emulator is a simh emulator executable. not a shell script.
...
interact

./template.sh当我使用as adminusing运行脚本时bash,脚本运行得很好。但是,当我使用 登录时su,会发生这种情况:

austin@ubuntu:~$ su admin
Password: 
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
...

为什么我的 bash 脚本无法与设置的用户 shell 一起工作?我的脚本中没有递归调用,并且该script命令应该是阻塞的!

为了以防万一有人担心,这台机器没有传出网络连接。它只能接收 SSH 连接。是的,我知道用户可以摆脱这个困境。这是在虚拟机中运行的。

答案1

我认为你正在使用一个没有 shebang 的脚本。

这个答案:

如果 execl() 函数由于相当于 POSIX.1-2008 系统接口卷中定义的 [ENOEXEC] 错误而失败,则 shell 应执行相当于使用搜索结果的路径名调用 shell 的命令作为其第一个操作数,将任何剩余参数传递给新 shell,但新 shell 中的“$0”值可以设置为命令名称。如果可执行文件不是文本文件,shell 可能会绕过此命令执行。在这种情况下,它应写入一条错误消息,并应返回退出状态 126。

在您的spawner.sh添加中echo "$@"获得更多提示。

好的,所以答案更复杂,但这应该有所帮助。在spawner.sh顶部添加这个:

if [[ $1 == -c ]]; then
    exec bash "$@"
fi

相关内容