CWE-77 在命令中使用的特殊元素转义处理不恰当(命令注入)

admin 2021年12月12日05:45:17评论116 views字数 6595阅读21分59秒阅读模式

CWE-77 在命令中使用的特殊元素转义处理不恰当(命令注入)

Improper Neutralization of Special Elements used in a Command ('Command Injection')

结构: Simple

Abstraction: Class

状态: Draft

被利用可能性: High

基本描述

The software constructs all or part of a command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended command when it is sent to a downstream component.

扩展描述

Command injection vulnerabilities typically occur when:

Command injection is a common problem with wrapper programs.

相关缺陷

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

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

适用平台

Language: {'cwe_Class': 'Language-Independent', 'cwe_Prevalence': 'Undetermined'}

常见的影响

范围 影响 注释
['Integrity', 'Confidentiality', 'Availability'] Execute Unauthorized Code or Commands If a malicious user injects a character (such as a semi-colon) that delimits the end of one command and the beginning of another, it may be possible to then insert an entirely new and unrelated command that was not intended to be executed.

可能的缓解方案

Architecture and Design

策略:

If at all possible, use library calls rather than external processes to recreate the desired functionality.

Implementation

策略:

If possible, ensure that all external commands called from the program are statically created.

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.

Operation

策略:

Run time: Run time policy enforcement may be used in a whitelist fashion to prevent use of any non-sanctioned commands.

System Configuration

策略:

Assign permissions to the software system that prevents the user from accessing/opening privileged files.

示例代码

The following simple program accepts a filename as a command line argument and displays the contents of the file back to the user. The program is installed setuid root because it is intended for use as a learning tool to allow system administrators in-training to inspect privileged system files without giving them the ability to modify them or damage the system.

bad C

int main(int argc, char** argv) {

char cmd[CMD_MAX] = "/usr/bin/cat ";
strcat(cmd, argv[1]);
system(cmd);

}

Because the program runs with root privileges, the call to system() also executes with root privileges. If a user specifies a standard filename, the call works as expected. However, if an attacker passes a string of the form ";rm -rf /", then the call to system() fails to execute cat due to a lack of arguments and then plows on to recursively delete the contents of the root partition.

Note that if argv[1] is a very long argument, then this issue might also be subject to a buffer overflow (CWE-120).

The following code is from an administrative web application designed to allow users to kick off a backup of an Oracle database using a batch-file wrapper around the rman utility and then run a cleanup.bat script to delete some temporary files. The script rmanDB.bat accepts a single command line parameter, which specifies what type of backup to perform. Because access to the database is restricted, the application runs the backup as a privileged user.

bad Java

...
String btype = request.getParameter("backuptype");
String cmd = new String("cmd.exe /K "

c:utilrmanDB.bat "
+btype+
"&&c:utlcleanup.bat"")

System.Runtime.getRuntime().exec(cmd);
...

The problem here is that the program does not do any validation on the backuptype parameter read from the user. Typically the Runtime.exec() function will not execute multiple commands, but in this case the program first runs the cmd.exe shell in order to run multiple commands with a single call to Runtime.exec(). Once the shell is invoked, it will happily execute multiple commands separated by two ampersands. If an attacker passes a string of the form "& del c:dbms.", then the application will execute this command along with the others specified by the program. Because of the nature of the application, it runs with the privileges necessary to interact with the database, which means whatever command the attacker injects will run with those privileges as well.

The following code from a system utility uses the system property APPHOME to determine the directory in which it is installed and then executes an initialization script based on a relative path from the specified directory.

bad Java

...
String home = System.getProperty("APPHOME");
String cmd = home + INITCMD;
java.lang.Runtime.getRuntime().exec(cmd);
...

The code above allows an attacker to execute arbitrary commands with the elevated privilege of the application by modifying the system property APPHOME to point to a different path containing a malicious version of INITCMD. Because the program does not validate the value read from the environment, if an attacker can control the value of the system property APPHOME, then they can fool the application into running malicious code and take control of the system.

The following code is a wrapper around the UNIX command cat which prints the contents of a file to standard out. It is also injectable:

bad C

#include
#include

int main(int argc, char argv) {


char cat[] = "cat ";
char command;
size_t commandLength;

commandLength = strlen(cat) + strlen(argv[1]) + 1;
command = (char ) malloc(commandLength);
strncpy(command, cat, commandLength);
strncat(command, argv[1], (commandLength - strlen(cat)) );

system(command);
return (0);

}

Used normally, the output is simply the contents of the file requested:

informative

$ ./catWrapper Story.txt
When last we left our heroes...

However, if we add a semicolon and another command to the end of this line, the command is executed by catWrapper with no complaint:

attack

$ ./catWrapper Story.txt; ls
When last we left our heroes...
Story.txt
SensitiveFile.txt
PrivateData.db
a.out*

If catWrapper had been set to have a higher privilege level than the standard user, arbitrary commands could be executed with that higher privilege.

Notes

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
7 Pernicious Kingdoms Command Injection
CLASP Command injection
OWASP Top Ten 2007 A2 CWE More Specific Injection Flaws
OWASP Top Ten 2004 A1 CWE More Specific Unvalidated Input
OWASP Top Ten 2004 A6 CWE More Specific Injection Flaws
Software Fault Patterns SFP24 Tainted input to command
SEI CERT Perl Coding Standard IDS34-PL CWE More Specific Do not pass untrusted, unsanitized data to a command interpreter

相关攻击模式

  • CAPEC-136
  • CAPEC-15
  • CAPEC-183
  • CAPEC-248
  • CAPEC-40
  • CAPEC-43
  • CAPEC-75
  • CAPEC-76

引用

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

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年12月12日05:45:17
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CWE-77 在命令中使用的特殊元素转义处理不恰当(命令注入)http://cn-sec.com/archives/613312.html

发表评论

匿名网友 填写信息