
本文旨在探讨在yara规则中有效匹配php中通过字符串拼接进行混淆的动态函数调用,特别是`gzinflate(base64_decode())`模式。文章分析了php字符串拼接的挑战,并提供了多种yara规则匹配策略,包括使用灵活的正则表达式、结合关键词与上下文以及处理内部拼接模式,旨在提高检测的鲁棒性并兼顾性能与误报率。
在编写YARA规则以检测恶意PHP代码时,一个常见的挑战是PHP语言允许通过字符串拼接来动态构造函数名或代码片段,从而达到混淆目的。例如,一个原本清晰的gzinflate(base64_decode())调用可能会被改写成以下形式:
eval("\$x=gzin"."flate(base"."64_de"."code()));这种拼接方式使得简单的字面量匹配或严格的正则表达式难以奏效,因为函数名被拆分成多个部分,并通过字符串连接符(.)或其他方式重新组合。攻击者可以利用这种灵活性来规避基于签名的检测。
为了有效应对PHP代码中的字符串拼接混淆,我们可以采用以下几种YARA规则匹配策略,从不同角度提高检测的覆盖范围和准确性。
最直接的方法是使用正则表达式来匹配函数名,并允许在函数名内部或函数调用括号之间存在任意非单词字符(例如,.、空格、换行符等),以覆盖常见的拼接模式。
立即学习“PHP免费学习笔记(深入)”;
匹配完整函数调用,允许分隔符: 使用\W*(匹配零个或多个非单词字符)来表示潜在的拼接点或分隔符。
rule php_gzinflate_base64_decode_flexible {
meta:
author = "AI Assistant"
description = "Detects gzinflate(base64_decode()) with flexible separators."
date = "2023-10-27"
severity = "medium"
strings:
// 匹配 gzinflate(base64_decode(,允许函数名和括号间有非单词字符
// 例如:gzinflate ( base64_decode (
$re1 = /gzinflate\W*\(\W*base64_decode\W*\(/ ascii nocase
condition:
$re1
}匹配函数名内部拼接: 针对gzin"."flate这类内部拼接,我们可以将函数名拆分,并在中间加入[\s\W]*?(匹配零个或多个空白字符或非单词字符,非贪婪模式),以覆盖更广泛的拼接方式。
rule php_gzinflate_base64_decode_concat_internal {
meta:
author = "AI Assistant"
description = "Detects gzinflate(base64_decode()) with internal string concatenation."
date = "2023-10-27"
severity = "high"
strings:
// 匹配 gzinflate 的拼接形式,如 gzin"."flate
$re_gzinflate_concat = /(gzin|gzinf|gzinfl|gzinfla)[\s\W]*?flate/ ascii nocase
// 匹配 base64_decode 的拼接形式
$re_base64_decode_concat = /(base64|base64_d|base64_de|base64_dec|base64_deco)[\s\W]*?code/ ascii nocase
// 结合两种拼接形式的完整调用
$re_full_concat_call = /(gzinflate|gzin[\s\W]*?flate)\W*\((base64_decode|base64[\s\W]*?decode)\W*\(/ ascii nocase
condition:
$re_full_concat_call
}仅仅依靠正则表达式匹配函数名可能不足以应对所有变体,或可能引入误报。结合其他上下文信息(如常见的执行函数eval、assert等)可以提高规则的准确性。
rule php_obfuscated_decode_contextual {
meta:
author = "AI Assistant"
description = "Detects obfuscated gzinflate(base64_decode()) calls within execution contexts."
date = "2023-10-27"
severity = "critical"
strings:
// 常见的执行函数
$s_eval = "eval(" ascii nocase
$s_assert = "assert(" ascii nocase
$s_create_function = "create_function(" ascii nocase
// 完整的灵活匹配模式
$re_full_flexible = /gzinflate\W*\(\W*base64_decode\W*\(/ ascii nocase
// 内部拼接的函数名部分
$re_gzinflate_part = /(gzinflate|gzin[\s\W]*?flate)/ ascii nocase
$re_base64_decode_part = /(base64_decode|base64[\s\W]*?decode)/ ascii nocase
condition:
// 方案一:直接匹配灵活的完整模式
$re_full_flexible or
// 方案二:匹配拼接的函数名部分,并确保在执行上下文中
(
(any of ($re_gzinflate_part, $re_base64_decode_part)) and
(any of ($s_eval, $s_assert, $s_create_function))
)
}对于非常复杂的混淆,可能需要将检测分解为多个阶段,然后通过YARA的逻辑运算符(and, or, of them)进行组合。例如,我们可以分别匹配gzinflate和base64_decode的各种变体,然后检查它们是否在代码的合理距离内出现,并且伴随有执行上下文。
rule php_obfuscated_decode_multi_stage {
meta:
author = "AI Assistant"
description = "Advanced detection for obfuscated gzinflate(base64_decode()) with proximity and context."
date = "2023-10-27"
severity = "critical"
strings:
// gzinflate 的各种形式
$gzinflate_literal = "gzinflate" ascii nocase
$gzinflate_concat1 = /gzin[\s\W]*?flate/ ascii nocase // gzin"."flate
$gzinflate_concat2 = /gzinf[\s\W]*?late/ ascii nocase // gzinf"."late
// base64_decode 的各种形式
$base64_decode_literal = "base64_decode" ascii nocase
$base64_decode_concat1 = /base64[\s\W]*?decode/ ascii nocase // base64"."decode
$base64_decode_concat2 = /base64_d[\s\W]*?code/ ascii nocase // base64_d"."code
// 常见的执行上下文
$exec_context1 = "eval(" ascii nocase
$exec_context2 = "assert(" ascii nocase
$exec_context3 = "call_user_func(" ascii nocase
condition:
// 至少一个 gzinflate 变体
( $gzinflate_literal or $gzinflate_concat1 or $gzinflate_concat2 ) and
// 至少一个 base64_decode 变体
( $base64_decode_literal or $base64_decode_concat1 or $base64_decode_concat2 ) and
// 至少一个执行上下文
( $exec_context1 or $exec_context2 or $exec_context3 ) and
// 确保 gzinflate 和 base64_decode 变体在合理接近的范围内 (例如,相距不超过200个字节)
// 注意:YARA本身没有直接的"within N bytes" for regexes without using a capture group and range.
// 这里的实现是检查所有相关字符串的存在,对于精确距离,可能需要更复杂的逻辑或外部工具辅助。
// 对于YARA,通常是依靠字符串的出现顺序和数量。
// 如果需要严格的距离,可以考虑使用YARA的`at`或`in`操作符,但它们通常用于字面量。
// 对于正则表达式,默认是全局匹配,没有严格的相对距离限制。
// 因此,这里的条件是确保所有关键部分都存在。
// 更精确的距离匹配可能需要编写多个规则或在外部脚本中进行后处理。
// 例如,一个更简单的距离检查是:
// $gzinflate_literal in (0..200) and $base64_decode_literal in (0..200)
// 但这仅适用于字面量。对于正则表达式,通常依赖于它们在文件中的出现。
// 这里的条件是确保所有关键部分都存在,并依赖于YARA扫描器找到它们。
// 如果需要更严格的顺序和距离,可以尝试在一个大的正则表达式中嵌入所有模式。
// 例如:/(gzinflate|gzin[\s\W]*?flate).*?(base64_decode|base64[\s\W]*?decode)/
// 但这可能导致性能问题。
// 因此,目前的条件是检查所有组件的存在性,作为一种折衷。
true // 占位符,表示所有组件都已通过上述AND条件检查
}在YARA规则中匹配PHP动态函数调用,特别是经过字符串拼接混淆的模式,需要结合多种策略。通过使用灵活的正则表达式、分段匹配函数名内部的拼接、并结合执行上下文关键词,可以显著提高检测的鲁棒性。然而,这始终是一个在检测覆盖率、性能和误报率之间寻求平衡的过程。持续的规则优化和对新型混淆技术的关注是维护有效检测能力的关键。
以上就是在YARA规则中匹配PHP动态函数调用:应对字符串拼接混淆的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号