这个神秘的 Bash 命令是什么意思?

The*_*eLQ 24 bash

我正在阅读 Ubuntu 论坛关于恶意命令的警告,并发现了这个有趣的 gem:

:(){ :|:& };:

警告:上面的代码会使你的机器崩溃,除非你有严格的 proc 限制(你可能没有)提示硬重启。

考虑这个类似于运行的代码sudo rm -rf /

但是,这是什么意思?即使有我的编程经验,我也从未见过一个不是汇编语言的神秘命令。

jtb*_*des 43

正如你所说,它是一个叉形炸弹。它所做的是定义一个函数,然后调用它。该函数被调用:

让我们命名它,forkbomb以便我们可以更好地了解发生了什么:

forkbomb(){ forkbomb|forkbomb& };forkbomb
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,并且可能从您的编程经验中猜到,第一部分是函数定义 ( forkbomb(){ ... }),最后一部分:是调用函数的位置(;Bash 中的 justs 分隔语句)。

现在,这个函数有什么作用?如果您熟悉 Bash,您就会知道该|字符将一个命令/程序的标准输出通过管道传输到另一个命令/程序的标准输入。所以基本上,:|:启动函数的两个实例(这是它“分叉”的地方)。

然后是魔法:&将这些命令放在后台,允许原始函数返回,而每个实例分叉直到奶牛在后台回家,从而耗尽您的所有资源并关闭系统(除非它有限制强加于它)。

  • 不要忘记解释最后的`:`,它实际执行了函数! (3认同)

Jam*_*s T 9

摘自维基百科文章Forkbomb

:()      # define ':' -- whenever we say ':', do this:
{        # beginning of what to do when we say ':'
    :    # load another copy of the ':' function into memory...
    |    # ...and pipe its output to...
    :    # ...another copy of ':' function, which has to be loaded into memory
         # (therefore, ':|:' simply gets two copies of ':' loaded whenever ':' is called)
    &    # disown the functions -- if the first ':' is killed,
         #     all of the functions that it has started should NOT be auto-killed
}        # end of what to do when we say ':'
;        # Having defined ':', we should now...
:        # ...call ':', initiating a chain-reaction: each ':' will start two more.
Run Code Online (Sandbox Code Playgroud)


Jos*_*h K 7

分解:

: () // Define ':' as a function. When you type ':' do the following
{
    : // Call ':' 
    | // Redirect output
    : // Into ':'
    & // Push process to the background
}; // End of ':' def
: // Now do ':'
Run Code Online (Sandbox Code Playgroud)

更改:bomb,您将拥有:

bomb(){ bomb|bomb& };bomb

它真的很优雅。