简体   繁体   English

如何在双引号内执行Linux shell变量?

[英]How to execute Linux shell variables within double quotes?

I have the following hacking-challenge, where we don't know, if there is a valid solution. 如果没有有效的解决方案,我将面临以下黑客挑战,我们不知道。

We have the following server script: 我们有以下服务器脚本:

read s  # read user input into var s
echo "$s"

# tests if it starts with 'a-f'

echo "$s" > "/home/user/${s}.txt"

We only control the input "$s". 我们仅控制输入“ $ s”。 Is there a possibility to send OS-commands like uname or do you think "no way"? 是否可以发送诸如uname类的OS命令,或者您认为“没有办法”?

I don't see any avenue for executing arbitrary commands. 我看不到执行任意命令的任何途径。 The script quotes $s every time it is referenced, so that limits what you can do. 脚本每次被引用时都引用$s ,以限制您可以执行的操作。

The only serious attack vector I see is that the echo statement writes to a file name based on $s . 我看到的唯一严重的攻击媒介是echo语句写入基于$s的文件名。 Since you control $s , you can cause the script to write to some unexpected locations. 由于您控制$s ,因此可以使脚本写入一些意外的位置。

  • $s could contain a string like bob/important.txt . $s可以包含类似bob/important.txt的字符串。 This script would then overwrite /home/user/bob/important.txt if executed with sufficient permissions. 如果以足够的权限执行,此脚本将覆盖/home/user/bob/important.txt Sorry, Bob! 抱歉,鲍勃!

  • Or, worse, $s could be bob/../../../etc/passwd . 或者更糟的是, $s可能是bob/../../../etc/passwd The script would try to write to /home/user/bob/../../../etc/passwd . 该脚本将尝试写入/home/user/bob/../../../etc/passwd If the script is running as root... uh oh! 如果脚本以root身份运行...哦!

It's important to note that the script can only write to these places if it has the right permissions. 请务必注意,如果脚本具有正确的权限,则只能将其写入这些位置。

  • You could embed unusual characters in $s that would cause irregular file names to be created. 您可以在$s中嵌入不寻常的字符,这会导致创建不规则的文件名。 Un-careful scripts could be taken advantage of. 粗心的脚本可能会被利用。 For example, if $s were foo -rf . bar 例如,如果$sfoo -rf . bar foo -rf . bar , then the file /home/user/foo -rf . bar.txt foo -rf . bar ,然后是文件/home/user/foo -rf . bar.txt /home/user/foo -rf . bar.txt would be created. /home/user/foo -rf . bar.txt将被创建。

    If someone ran for file in /home/user; rm $file; done 如果有人for file in /home/user; rm $file; done运行for file in /home/user; rm $file; done for file in /home/user; rm $file; done for file in /home/user; rm $file; done they'd have a surprise on their hands. for file in /home/user; rm $file; done他们不得不在他们的手一个惊喜。 They would end up running rm /home/user/foo -rf . bar.txt 他们最终将运行rm /home/user/foo -rf . bar.txt rm /home/user/foo -rf . bar.txt , which is a disaster. rm /home/user/foo -rf . bar.txt ,这是一场灾难。 If you take out /home/user/foo and bar.txt you're left with rm -rf . 如果取出/home/user/foobar.txt ,则剩下rm -rf . — everything in the current directory is deleted. —当前目录中的所有内容都将被删除。 Oops! 糟糕!

    (They should have quoted "$file" !) (他们应该引用"$file" !)

And there are two other minor things which, while I don't know how to take advantage of them maliciously, do cause the script to behave slightly differently than intended. 还有另外两个小问题,尽管我不知道如何恶意利用它们,但确实会导致脚本的行为与预期的有所不同。

  • read allows backslashes to escape characters like space and newline. read允许反斜杠转义字符,例如空格和换行符。 You can enter \\ space to embed spaces and \\ enter to have read parse multiple lines of input. 您可以输入\\ 空间嵌入空格和\\ 回车read分析输入的多条线。

  • echo accepts a couple of flags. echo接受几个标志。 If $s is -n or -e then it won't actually echo $s ; 如果$s-n-e那么它实际上不会回显$s rather, it will interpret $s as a command-line flag. 而是将$s解释为命令行标志。

Use read -rs or any \\ will be lost/missinterpreted by your command. 使用read -rs或任何\\将丢失/未命中。

read -r s?"Your input: "
if [ -n "${s}" ]
 then
   # "filter" file name from command
   echo "${s##*/}" | sed 's|^ *\([[:alnum:]_]\{1,\}\)[[:blank:]].*|/home/user/\1.txt|' | read Output

   (
    # put any limitation on user here
    ulimit -t 5 1>/dev/null 2>&1

    `${read}`    
   ) > ${OutPut}
 else
   echo "Bad command" > /home/user/Error.txt
 fi

Sure: 当然:

read s
$s > /home/user/"$s".txt

If I enter uname , this prints Linux . 如果输入uname ,将显示Linux But beware: this is a security nightmare. 但是请注意:这是一场安全噩梦。 What if someone enters rm -rf $HOME ? 如果有人输入rm -rf $HOME怎么办? You'd also have issues with commands containing a slash. 您还会对包含斜杠的命令有疑问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM