【问题标题】:Bash - exit parent script from user input [closed]Bash - 从用户输入中退出父脚本[关闭]
【发布时间】:2020-12-09 20:21:34
【问题描述】:

在下面的脚本中,在four() 中,我将如何期望脚本并返回到 bash?我无法修改脚本,所以我只能向name提供输入

我试过$(/bin/bash),但它存在于子shell而不是父shell

read -r -p "Enter your team" name
bash -c "/usr/games/team $name"
pause

【问题讨论】:

  • 修改 /usr/games/team 并在输入特殊代码时终止父进程。
  • 如前所述,我无法修改脚本。我实际上只能为这个脚本提供内容。我可以访问 bash,但它只能来自子 shell。我需要退出这个脚本。
  • /usr/games/team 是另一个脚本吗?
  • 你可以忽略那一行,直接把它看成 bash -c "$name"
  • pkill 怎么样

标签: bash kill-process


【解决方案1】:

在无法修改任何脚本的情况下(在我看来)我们需要看看是否可以将某种 exit/abort 命令注入到我们的响应中(存储在变量name) ...对于那些熟悉关系数据库的人来说,这类似于SQL injection

问题是因为我们提供的值(存储在name 变量中)在子外壳(bash -c ...)中被引用,所以我们需要一种方法来中止,不仅是子外壳shell 也是父 shell(由#!/bin/bash shebang 调用)。

注意:我不确定'return to bash' 的 OP 是什么意思,所以为了回答这个问题,我假设这意味着返回到父级的命令提示符脚本被调用。

一个想法是将kill 0 注入到答案中,这样子shell 实际运行kill 0,这反过来应该中止所有子shell 并将我们返回到命令提示符。

当前脚本正在寻找要提供给/usr/games/team 的某种答案,因此我们需要让该调用完成,然后发出kill 0

由于我们没有得到/usr/games/team,我将four() 函数中的一行代码替换为以下代码:

set -xv                                      # enable debugging so we can see how the next line is expanded at run time
bash -c "echo team name: ${name}"
set +xv                                      # disable debugging

一种注入思路,假设父脚本名为team.bash

$ team.bash
4. Print Team Badge
Enter choice [1 - 5]: 4
Enter your team: abc ; kill 0                # yeah, team name is "abc ; kill 0"
+ bash -c 'echo team name: abc ; kill 0'     # this is what our injection expands to
team name: abc                               # our "echo" works as expected and ...
Terminated                                   # here we see the result of running "kill 0" as a 2nd command
$                                            # and voilà, we're out of the script and back at the command prompt

没有set -xv/+xv 我们有:

$ team.bash
4. Print Team Badge
Enter choice [1 - 5]: 4
Enter your team: abc ; kill 0
team name: abc
Terminated
$ 

【讨论】:

  • 这将杀死当前进程树中的所有进程。另一个选项是 kill -15 $PPID 它只会杀死父进程而不是上面的进程。
  • 对,'will kill all processes in the current process tree' 让我回到原来的命令提示符(我的假设之一,直到 OP 可以定义 return to bash);我提供了abc;kill -15 $PPID 的团队名称并得到了相同的结果:Terminated 并返回到命令提示符,所以...kill 0 少了一些击键:-)
  • 只是为了提高认识;@)。但在这种特殊情况下,它并不那么重要。
猜你喜欢
  • 1970-01-01
  • 2017-10-07
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
相关资源
最近更新 更多