查看: 2184|回复: 25

Linux fork炸弹及其预防方法

[复制链接]
发表于 2013-2-9 09:28:48 | 显示全部楼层 |阅读模式

在Linux系统下执行这段fork 炸弹代码就会引起死机,一旦执行起来后,唯一的方法就是重启系统。实际上这段代码是一段无限递归代码,将系统资源耗尽。

fork 炸弹代码:

.(){ .|.& };.

本文下面有这段代码的详细解释,为了防止fork炸弹,方法就是限制用户能够启动的进程数。具体做法,编辑/etc/security/limits.conf文件,在末尾加入 :

* hard nproc 200

将用户的进程数限制为200,经过测试,root账户不受这个限制。

Q. Can you explain following bash code or bash fork() bomb?

:{ :|:& };:

A. This is a bash function. It gets called recursively (recursive function). This is most horrible code for any Unix / Linux box. It is often used by sys admin to test user processes limitations (Linux process limits can be configured via /etc/security/limits.conf and PAM).

Once a successful fork bomb has been activated in a system it may not be possible to resume normal operation without rebooting, as the only solution to a fork bomb is to destroy all instances of it.

WARNING! These examples may crash your computer if executed.

Understanding :{ :|:& };: fork() bomb code

: - It is a function name. It accepts no arguments at all. Generally, bash function is defined as follows:

foo(){

arg1=

echo ''

#do_something on $arg argument

}

fork() bomb is defined as follows:

:{

:|:&

};:

:|: - Next it call itself using programming technique called recursion and pipes the output to another call of the function ':'. The worst part is function get called two times to bomb your system.

& - Puts the function call in the background so child cannot die at all and start eating system resources.

; - Terminate the function definition

: - Call (run) the function aka set the fork() bomb.

Here is more human readable code:


bomb() {bomb | bomb &}; bombProperly configured Linux / UNIX box should not go down when fork() bomb sets off.


发表于 2013-2-19 08:12:08 | 显示全部楼层
声明一下:本人看贴和回贴的规则,好贴必看,精华贴必回。  
发表于 2013-2-20 02:37:55 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2013-2-20 11:22:05 | 显示全部楼层
努力~~各位。。。  
发表于 2013-2-22 03:34:33 | 显示全部楼层
初来乍到,请多多关照。。。嘿嘿,回个贴表明我来过。  
发表于 2013-2-22 22:06:32 | 显示全部楼层
先看看怎么样!  
发表于 2014-12-4 05:57:57 | 显示全部楼层
老大,我好崇拜你哟  
发表于 2014-12-26 22:23:09 | 显示全部楼层
没人理我。。。。  
发表于 2015-1-27 02:57:50 | 显示全部楼层
呵呵,明白了  
发表于 2015-3-1 02:03:08 | 显示全部楼层
支持你就顶你  
高级模式
B Color Image Link Quote Code Smilies

本版积分规则