众所周知,将文本从网站复制粘贴到终端是非常危险的,因为它可能包含额外的文本、控制代码和换行符,这些在复制时不可见,但保存在粘贴缓冲区,如果粘贴到终端会导致恶意代码执行。
上述链接的网站包含以下 HTML 和 CSS:
<p class="codeblock">
<!-- Oh noes, you found it! -->
git clone
<span style="position: absolute; left: -100px; top: -100px">/dev/null; clear; echo -n "Hello ";whoami|tr -d '\n';echo -e '!\nThat was a bad idea. Don'"'"'t copy code from websites you don'"'"'t trust!<br>Here'"'"'s the first line of your /etc/passwd: ';head -n1 /etc/passwd<br>git clone </span>
git://git.kernel.org/pub/scm/utils/kup/kup.git
</p>
这呈现为git clone git://git.kernel.org/pub/scm/utils/kup/kup.git
,但是当您突出显示并复制它时,它将多行粘贴到终端中,导致它们被执行:
git clone /dev/null; clear; echo -n "Hello ";whoami|tr -d '\n';echo -e '!\nThat was a bad idea. Don'"'"'t copy code from websites you don'"'"'t trust!
Here'"'"'s the first line of your /etc/passwd: ';head -n1 /etc/passwd
git clone git://git.kernel.org/pub/scm/utils/kup/kup.git
对于单行字符串,是否有任何简单、快速的方法来解决这个问题?例如,复制文本,将其粘贴到浏览器的搜索栏中,然后从那里复制似乎会删除所有换行符:
git clone /dev/null; clear; echo -n "Hello ";whoami|tr -d '\n';echo -e '!\nThat was a bad idea. Don'"'"'t copy code from websites you don'"'"'t trust!Here'"'"'s the first line of your /etc/passwd: ';head -n1 /etc/passwdgit clone git://git.kernel.org/pub/scm/utils/kup/kup.git
现在,您必须显式按 Enter 才能使代码执行,而将其粘贴到终端中的行为是不够的。这是防止这种攻击的所有变体的安全方法吗?