CWE-96 静态存储代码中指令转义处理不恰当(静态代码注入)

admin 2022年1月7日03:01:14评论74 views字数 4249阅读14分9秒阅读模式

CWE-96 静态存储代码中指令转义处理不恰当(静态代码注入)

Improper Neutralization of Directives in Statically Saved Code ('Static Code Injection')

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: unkown

基本描述

The software receives input from an upstream component, but it does not neutralize or incorrectly neutralizes code syntax before inserting the input into an executable resource, such as a library, configuration file, or template.

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 94 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 94 cwe_View_ID: 699 cwe_Ordinal: Primary

适用平台

Language: [{'cwe_Name': 'PHP', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'Perl', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Class': 'Interpreted', 'cwe_Prevalence': 'Undetermined'}]

常见的影响

范围 影响 注释
Confidentiality ['Read Files or Directories', 'Read Application Data'] The injected code could access restricted data / files.
Access Control Bypass Protection Mechanism In some cases, injectable code controls authentication; this may lead to a remote vulnerability.
Access Control Gain Privileges or Assume Identity Injected code can access resources that the attacker is directly prevented from accessing.
['Integrity', 'Confidentiality', 'Availability', 'Other'] Execute Unauthorized Code or Commands Code injection attacks can lead to loss of data integrity in nearly all cases as the control-plane data injected is always incidental to data recall or writing. Additionally, code injection can often result in the execution of arbitrary code.
Non-Repudiation Hide Activities Often the actions performed by injected control code are unlogged.

可能的缓解方案

MIT-5 Implementation

策略: Input Validation

Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a whitelist of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does.
When performing input validation, consider all potentially relevant properties, including length, type of input, the full range of acceptable values, missing or extra inputs, syntax, consistency across related fields, and conformance to business rules. As an example of business rule logic, "boat" may be syntactically valid because it only contains alphanumeric characters, but it is not valid if the input is only expected to contain colors such as "red" or "blue."
Do not rely exclusively on looking for malicious or malformed inputs (i.e., do not rely on a blacklist). A blacklist is likely to miss at least one undesirable input, especially if the code's environment changes. This can give attackers enough room to bypass the intended validation. However, blacklists can be useful for detecting potential attacks or determining which inputs are so malformed that they should be rejected outright.

Implementation

策略: Output Encoding

Perform proper output validation and escaping to neutralize all code syntax from data written to code files.

示例代码

This example attempts to write user messages to a message file and allow users to view them.

bad PHP

$MessageFile = "cwe-94/messages.out";
if ($_GET["action"] == "NewMessage") {

$name = $_GET["name"];
$message = $_GET["message"];
$handle = fopen($MessageFile, "a+");
fwrite($handle, "$name says '$message'


n");
fclose($handle);
echo "Message Saved!

n";

}
else if ($_GET["action"] == "ViewMessages") {

include($MessageFile);

}

While the programmer intends for the MessageFile to only include data, an attacker can provide a message such as:

attack

name=h4x0r
message=%3C?php%20system(%22/bin/ls%20-l%22);?%3E

which will decode to the following:

attack

The programmer thought they were just including the contents of a regular data file, but PHP parsed it and executed the code. Now, this code is executed any time people view messages.

Notice that XSS (CWE-79) is also possible in this situation.

分析过的案例

标识 说明 链接
CVE-2002-0495 Perl code directly injected into CGI library file from parameters to another CGI program. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2002-0495
CVE-2005-1876 Direct PHP code injection into supporting template file. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2005-1876
CVE-2005-1894 Direct code injection into PHP script that can be accessed by attacker. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2005-1894
CVE-2003-0395 PHP code from User-Agent HTTP header directly inserted into log file implemented as PHP script. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2003-0395
CVE-2007-6652 chain: execution after redirect allows non-administrator to perform static code injection. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-6652

Notes

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
PLOVER Direct Static Code Injection
Software Fault Patterns SFP24 Tainted input to command

相关攻击模式

  • CAPEC-35
  • CAPEC-73
  • CAPEC-77
  • CAPEC-81
  • CAPEC-85

文章来源于互联网:scap中文网

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年1月7日03:01:14
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CWE-96 静态存储代码中指令转义处理不恰当(静态代码注入)http://cn-sec.com/archives/612660.html

发表评论

匿名网友 填写信息