Bash 命令 :(){ :|:& };: 将产生进程导致内核死亡。你能解释一下语法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/515844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The Bash command :(){ :|:& };: will spawn processes to kernel death. Can you explain the syntax?
提问by silviot
回答by dbr
:(){ :|:& };:
..defines a function named :, which spawns itself (twice, one pipes into the other), and backgrounds itself.
.. 定义了一个名为 的函数:,它产生自身(两次,一个管道进入另一个管道),以及背景自身。
With line breaks:
带换行符:
:()
{
:|:&
};
:
Renaming the :function to forkbomb:
将:函数重命名为forkbomb:
forkbomb()
{
forkbomb | forkbomb &
};
forkbomb
You can prevent such attacks by using ulimitto limit the number of processes-per-user:
您可以通过使用ulimit来限制每个用户的进程数来防止此类攻击:
$ ulimit -u 50
$ :(){ :|:& };:
-bash: fork: Resource temporarily unavailable
$
More permanently, you can use /etc/security/limits.conf(on Debian and others, at least), for example:
更永久地,您可以使用/etc/security/limits.conf(至少在 Debian 和其他平台上),例如:
* hard nproc 50
Of course that means you can only run 50 processes, you may want to increase this depending on what the machine is doing!
当然,这意味着您只能运行 50 个进程,您可能希望根据机器的运行情况增加这个!
回答by Johannes Weiss
That defines a function called :which calls itself twice (Code: : | :). It does that in the background (&). After the ;the function definition is done and the function :gets started.
这定义了一个:调用的函数 ,它调用自己两次(代码:): | :。它在后台执行此操作 ( &)。在之后;的函数定义完成后,功能:得到启动。
So every instance of : starts two new : and so on... Like a binary tree of processes...
所以 : 的每个实例都会启动两个新的 : 等等......就像进程的二叉树......
Written in plain C that is:
用普通的 C 编写,即:
fork();
fork();
回答by silviot
This defines a function called :(:()). Inside the function ({...}), there's a :|:&which is like this:
这定义了一个名为:( :())的函数。在函数 ( {...})里面,有一个:|:&是这样的:
:calls this:function again.|signifies piping the output to a command.:after|means pipe to the function:.&, in this case, means run the preceding in the background.
::再次调用这个函数。|表示通过管道将输出传递给命令。:after|表示函数的管道:。&,在这种情况下,意味着在后台运行前一个。
Then there's a ;that is known as a command separator.
然后有一个;被称为命令分隔符的。
Finally, the :starts this "chain reaction", activating the fork bomb.
最后,:启动这个“连锁反应”,激活分叉炸弹。
The C equivalent would be:
C 等效项将是:
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork();
fork();
}

