:(){:|:&};:解释

本文详细解析了一段在Bash环境下导致无限递归并最终耗尽系统资源的恶意代码。该代码利用了Bash中函数优先级高于内置命令的特点,通过定义特殊函数名并结合管道操作实现在特定条件下不断创建新进程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

netman的一个签名,居然引起这么大的影响.当然小弟我也是膜拜netman的,连鸟哥都是netman的弟子^_^

CODE:
:(){ :|:&};:

转换下书写格式:


CODE:

:()
{
        : | : &
}
:

其中



CODE:
:()
{
        : | : &
}

(即除最后一行外)定义了一个 shell 函数,函数名是“:”,而这个函数体执行一个后台命令“: | :” ——即冒号命令(或函数,下文会解释)的输出通过管道再传给冒号命令做输入(够清楚了吧)
最后一行执行“:”命令,其实把:换成一个函数名,你应该好理解些^_^


这个代码只有在 bash 中执行才会出现不断创建进程而耗尽系统资源的严重后果,在 ksh(Korn shell)、sh(Bourne shell)中并不会出现,在 ksh87 和传统 unix Bourne shell 中冒号不能做函数名,即便是在后来的 posix sh 和 pdksh(ksh93 手边没有,没试)中冒号可以做函数名,但还是不会出现那个效果。

原因是 sh、ksh 中内置命令的优先级高于函数,所以执行“:”,总是执行内置命令“:”而不是刚才定义的那个恐怖函数。
但是在 bash 中就不一样,bash 中函数的优先级高于内置命令,所以执行“:”结果会导致不断的递归,而其中有管道操作,这就需要创建两个子进程来实现,这样就会不断的创建进程而导致资源耗尽。

需要注意的是,很多 linux 发行的 sh 命令并不是真正的 Bourne shell,而是 bash 的一个符号链接或副本。如果不能确定请不要轻易尝试用 sh 执行这段代码,否则也可能会导致资源耗尽。
root@prigz-k8s-bgp-yiduncpuyw87:~# ipmitool sel elist 1 | 01/17/2025 | 17:54:04 | Event Logging Disabled Event_log | Log area reset/cleared | Asserted 2 | 02/21/2025 | 17:10:29 | System ACPI Power State ACPI_state | S5/G2: soft-off | Asserted 3 | 02/21/2025 | 17:13:28 | System ACPI Power State ACPI_state | S0/G0: working | Asserted 4 | 02/21/2025 | 17:13:28 | Processor CPU0_Status | Presence detected | Asserted 5 | 02/21/2025 | 17:13:28 | Processor CPU1_Status | Presence detected | Asserted 6 | 02/21/2025 | 17:13:29 | Power Supply PSU1_Status | Presence detected | Asserted 7 | 02/21/2025 | 17:13:29 | Power Supply PSU2_Status | Presence detected | Asserted 8 | 02/21/2025 | 17:13:29 | Microcontroller BMC_Start | Device Enabled | Asserted 9 | 02/21/2025 | 17:13:48 | Add-in Card OCP_Card1_Sta | Device Absent | Asserted a | 02/21/2025 | 17:13:48 | Add-in Card OCP_Card2_Sta | Device Absent | Asserted b | 02/21/2025 | 17:14:12 | System Event #0xf7 | Timestamp Clock Sync | Asserted c | 02/21/2025 | 17:14:16 | Memory DIMM2 | Presence Detected | Asserted d | 02/21/2025 | 17:14:16 | Memory DIMM6 | Presence Detected | Asserted e | 02/21/2025 | 17:14:16 | Memory DIMM9 | Presence Detected | Asserted f | 02/21/2025 | 17:14:16 | Memory DIMM13 | Presence Detected | Asserted 10 | 02/21/2025 | 17:14:16 | Memory DIMM18 | Presence Detected | Asserted 11 | 02/21/2025 | 17:14:16 | Memory DIMM22 | Presence Detected | Asserted 12 | 02/21/2025 | 17:14:16 | Memory DIMM25 | Presence Detected | Asserted 13 | 02/21/2025 | 17:14:16 | Memory DIMM29 | Presence Detected | Asserted 14 | 02/21/2025 | 17:14:16 | Drive Slot / Bay Disk0 | Drive Present | Asserted 15 | 02/21/2025 | 17:16:34 | System ACPI Power State ACPI_state | S5/G2: soft-off | Asserted 16 | 02/21/2025 | 17:16:39 | System ACPI Power State ACPI_state | S0/G0: working | Asserted 17 | 02/21/2025 | 17:17:25 | System Boot Initiated RestartInitiated | Initiated by warm reset | Asserted 18 | 02/21/2025 | 17:19:44 | System Event #0xf7 | Timestamp Clock Sync | Asserted 19 | 02/21/2025 | 17:22:26 | OS Boot | boot completed - device not specified | Asserted root@prigz-k8s-bgp-yiduncpuyw87:~# ^C
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值