CWE-470 使用外部可控制的输入来选择类或代码(不安全的反射)

admin 2021年12月16日16:30:51评论174 views字数 4930阅读16分26秒阅读模式

CWE-470 使用外部可控制的输入来选择类或代码(不安全的反射)

Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: unkown

基本描述

The application uses external input with reflection to select which classes or code to use, but it does not sufficiently prevent the input from selecting improper classes or code.

扩展描述

If the application uses external inputs to determine which class to instantiate or which method to invoke, then an attacker could supply values to select unexpected classes or methods. If this occurs, then the attacker could create control flow paths that were not intended by the developer. These paths could bypass authentication or access control checks, or otherwise cause the application to behave in an unexpected manner. This situation becomes a doomsday scenario if the attacker can upload files into a location that appears on the application's classpath (CWE-427) or add new entries to the application's classpath (CWE-426). Under either of these conditions, the attacker can use reflection to introduce new, malicious behavior into the application.

相关缺陷

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

  • cwe_Nature: ChildOf cwe_CWE_ID: 913 cwe_View_ID: 1003 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 610 cwe_View_ID: 1000

  • cwe_Nature: ChildOf cwe_CWE_ID: 20 cwe_View_ID: 700 cwe_Ordinal: Primary

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

适用平台

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

常见的影响

范围 影响 注释
['Integrity', 'Confidentiality', 'Availability', 'Other'] ['Execute Unauthorized Code or Commands', 'Alter Execution Logic'] The attacker might be able to execute code that is not directly accessible to the attacker. Alternately, the attacker could call unexpected code in the wrong place or the wrong time, possibly modifying critical system state.
['Availability', 'Other'] ['DoS: Crash, Exit, or Restart', 'Other'] The attacker might be able to use reflection to call the wrong code, possibly with unexpected arguments that violate the API (CWE-227). This could cause the application to exit or hang.
Confidentiality Read Application Data By causing the wrong code to be invoked, the attacker might be able to trigger a runtime error that leaks sensitive information in the error message, such as CWE-536.

可能的缓解方案

Architecture and Design

策略:

Refactor your code to avoid using reflection.

Architecture and Design

策略:

Do not use user-controlled inputs to select and load classes or code.

Implementation

策略:

Apply strict input validation by using whitelists or indirect selection to ensure that the user is only selecting allowable classes or code.

示例代码

A common reason that programmers use the reflection API is to implement their own command dispatcher. The following example shows a command dispatcher that does not use reflection:

good Java

String ctl = request.getParameter("ctl");
Worker ao = null;
if (ctl.equals("Add")) {

ao = new AddCommand();

}
else if (ctl.equals("Modify")) {

ao = new ModifyCommand();

}
else {

throw new UnknownActionError();

}
ao.doAction(request);

A programmer might refactor this code to use reflection as follows:

bad Java

String ctl = request.getParameter("ctl");
Class cmdClass = Class.forName(ctl + "Command");
Worker ao = (Worker) cmdClass.newInstance();
ao.doAction(request);

The refactoring initially appears to offer a number of advantages. There are fewer lines of code, the if/else blocks have been entirely eliminated, and it is now possible to add new command types without modifying the command dispatcher. However, the refactoring allows an attacker to instantiate any object that implements the Worker interface. If the command dispatcher is still responsible for access control, then whenever programmers create a new class that implements the Worker interface, they must remember to modify the dispatcher's access control code. If they do not modify the access control code, then some Worker classes will not have any access control.

One way to address this access control problem is to make the Worker object responsible for performing the access control check. An example of the re-refactored code follows:

bad Java

String ctl = request.getParameter("ctl");
Class cmdClass = Class.forName(ctl + "Command");
Worker ao = (Worker) cmdClass.newInstance();
ao.checkAccessControl(request);
ao.doAction(request);

Although this is an improvement, it encourages a decentralized approach to access control, which makes it easier for programmers to make access control mistakes. This code also highlights another security problem with using reflection to build a command dispatcher. An attacker can invoke the default constructor for any kind of object. In fact, the attacker is not even constrained to objects that implement the Worker interface; the default constructor for any object in the system can be invoked. If the object does not implement the Worker interface, a ClassCastException will be thrown before the assignment to ao, but if the constructor performs operations that work in the attacker's favor, the damage will already have been done. Although this scenario is relatively benign in simple applications, in larger applications where complexity grows exponentially it is not unreasonable that an attacker could find a constructor to leverage as part of an attack.

分析过的案例

标识 说明 链接

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
7 Pernicious Kingdoms Unsafe Reflection
The CERT Oracle Secure Coding Standard for Java (2011) SEC06-J Do not use reflection to increase accessibility of classes, methods, or fields

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

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年12月16日16:30:51
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CWE-470 使用外部可控制的输入来选择类或代码(不安全的反射)https://cn-sec.com/archives/613015.html

发表评论

匿名网友 填写信息