CVE-2011-3229 - Improper Control of Generation of Code ('Code Injection')

Severity

68%

Complexity

86%

Confidentiality

106%

Directory traversal vulnerability in Apple Safari before 5.1.1 allows remote attackers to execute arbitrary JavaScript code, in a Safari Extensions context, via a crafted safari-extension: URL.

Directory traversal vulnerability in Apple Safari before 5.1.1 allows remote attackers to execute arbitrary JavaScript code, in a Safari Extensions context, via a crafted safari-extension: URL.

CVSS 2.0 Base Score 6.8. CVSS Attack Vector: network. CVSS Attack Complexity: medium. CVSS Vector: (AV:N/AC:M/Au:N/C:P/I:P/A:P).

Demo Examples

Improper Control of Generation of Code ('Code Injection')

CWE-94

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


               
}
echo "Message Saved!<p>\n";
include($MessageFile);

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


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

which will decode to the following:


               
<?php system("/bin/ls -l");?>

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.

Improper Control of Generation of Code ('Code Injection')

CWE-94

edit-config.pl: This CGI script is used to modify settings in a configuration file.


               
}
# code to add a field/key to a file goes here
# code to set key to a particular file goes here
# code to delete key from a particular file goes here
eval($code);# this is super-efficient code, especially if you have to invoke# any one of dozens of different functions!
handleConfigAction($configfile, param('action'));
print "No action specified!\n";

The script intends to take the 'action' parameter and invoke one of a variety of functions based on the value of that parameter - config_file_add_key(), config_file_set_key(), or config_file_delete_key(). It could set up a conditional to invoke each function separately, but eval() is a powerful way of doing the same thing in fewer lines of code, especially when a large number of functions or variables are involved. Unfortunately, in this case, the attacker can provide other values in the action parameter, such as: add_key(",","); system("/bin/ls"); This would produce the following string in handleConfigAction(): config_file_add_key(",","); system("/bin/ls"); Any arbitrary Perl code could be added after the attacker has "closed off" the construction of the original function call, in order to prevent parsing errors from causing the malicious eval() to fail before the attacker's payload is activated. This particular manipulation would fail after the system() call, because the "_key(\$fname, \$key, \$val)" portion of the string would cause an error, but this is irrelevant to the attack because the payload has already been activated.

Demo Examples

Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')

CWE-22

The following code could be for a social networking application in which each user's profile information is stored in a separate file. All files are stored in a single directory.


               
print "</ul>\n";
print "<li>$_</li>\n";

While the programmer intends to access files such as "/users/cwe/profiles/alice" or "/users/cwe/profiles/bob", there is no verification of the incoming user parameter. An attacker could provide a string such as:


               
../../../etc/passwd

The program would generate a profile pathname like this:


               
/users/cwe/profiles/../../../etc/passwd

When the file is opened, the operating system resolves the "../" during path canonicalization and actually accesses this file:


               
/etc/passwd

As a result, the attacker could read the entire text of the password file.

Notice how this code also contains an error message information leak (CWE-209) if the user parameter does not produce a file that exists: the full pathname is provided. Because of the lack of output encoding of the file that is retrieved, there might also be a cross-site scripting problem (CWE-79) if profile contains any HTML, but other code would need to be examined.

Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')

CWE-22

In the example below, the path to a dictionary file is read from a system property and used to initialize a File object.


               
File dictionaryFile = new File(filename);

However, the path is not validated or modified to prevent it from containing relative or absolute path sequences before creating the File object. This allows anyone who can control the system property to determine what file is used. Ideally, the path should be resolved relative to some kind of application or user home directory.

Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')

CWE-22

The following code takes untrusted input and uses a regular expression to filter "../" from the input. It then appends this result to the /home/user/ directory and attempts to read the file in the final resulting path.


               
ReadAndSendFile($filename);

Since the regular expression does not have the /g global match modifier, it only removes the first instance of "../" it comes across. So an input value such as:


               
../../../etc/passwd

will have the first "../" stripped, resulting in:


               
../../etc/passwd

This value is then concatenated with the /home/user/ directory:


               
/home/user/../../etc/passwd

which causes the /etc/passwd file to be retrieved once the operating system has resolved the ../ sequences in the pathname. This leads to relative path traversal (CWE-23).

Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')

CWE-22

The following code attempts to validate a given input path by checking it against a whitelist and once validated delete the given file. In this specific case, the path is considered valid if it starts with the string "/safe_dir/".


               
}
f.delete()

An attacker could provide an input such as this:


               
/safe_dir/../important.dat

The software assumes that the path is valid because it starts with the "/safe_path/" sequence, but the "../" sequence will cause the program to delete the important.dat file in the parent directory

Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')

CWE-22

The following code demonstrates the unrestricted upload of a file with a Java servlet and a path traversal vulnerability. The HTML code is the same as in the previous example with the action attribute of the form sending the upload file request to the Java servlet instead of the PHP code.


               
</form>
<input type="submit" name="submit" value="Submit"/>

When submitted the Java servlet's doPost method will receive the request, extract the name of the file from the Http request header, read the file contents from the request and output the file to the local upload directory.


               
}
{...}// the starting position of the boundary header// verify that content type is multipart form data
// output the file to the local upload directory
bw.close();
}
bw.flush();
// output successful upload response HTML page
// output unsuccessful upload response HTML page
...

This code does not check the filename that is provided in the header, so an attacker can use "../" sequences to write to files outside of the intended directory. Depending on the executing environment, the attacker may be able to specify arbitrary files to write to, leading to a wide variety of consequences, from code execution, XSS (CWE-79), or system crash.

Also, this code does not perform a check on the type of the file being uploaded. This could allow an attacker to upload any executable file or other file with malicious code (CWE-434).

Overview

First reported 13 years ago

2011-10-14 10:55:00

Last updated 7 years ago

2017-08-29 01:30:00

Affected Software

Apple Safari

Apple Safari 1.0

1.0

Apple Safari 1.0 Beta

1.0

Apple Safari 1.0 Beta2

1.0

Apple Safari 1.0.0

1.0.0

Apple Safari 1.0.0b1

1.0.0b1

Apple Safari 1.0.0b2

1.0.0b2

Apple Safari 1.0.1

1.0.1

Apple Safari 1.0.2

1.0.2

Apple Safari 1.0.3

1.0.3

Apple Safari 1.0.3 85.8

1.0.3

Apple Safari 1.0.3 85.8.1

1.0.3

Apple Safari 1.0b1 for Mac OS X

1.0b1

Apple Safari 1.1

1.1

Apple Safari 1.1.0

1.1.0

Apple Safari 1.1.1

1.1.1

Apple Safari 1.2

1.2

Apple Safari 1.2.0

1.2.0

Apple Safari 1.2.1

1.2.1

Apple Safari 1.2.2

1.2.2

Apple Safari 1.2.3

1.2.3

Apple Safari 1.2.4

1.2.4

Apple Safari 1.2.5

1.2.5

Apple Safari 1.3

1.3

Apple Safari 1.3.0

1.3.0

Apple Safari 1.3.1

1.3.1

Apple Safari 1.3.2

1.3.2

Apple Safari 1.3.2 312.5

1.3.2

Apple Safari 1.3.2 312.6

1.3.2

Apple Safari 2

2

Apple Safari 2.0

2.0

Apple Safari 2.0.0

2.0.0

Apple Safari 2.0.1

2.0.1

Apple Safari 2.0.2

2.0.2

Apple Safari 2.0.3

2.0.3

Apple Safari 2.0.3 417.8

2.0.3

Apple Safari 2.0.3 417.9

2.0.3

Apple Safari 2.0.3 417.9.2

2.0.3

Apple Safari 2.0.3 417.9.3

2.0.3

Apple Safari 2.0.4

2.0.4

Apple Safari 2.0.4 for Mac OS X

2.0.4

Apple Safari 3

3

Apple Safari 3.0

3.0

Apple Safari 3.0.0

3.0.0

Apple Safari 3.0.0 for Mac OS X

3.0.0

Apple Safari 3.0.0b

3.0.0b

Apple Safari 3.0.0b for Windows

3.0.0b

Apple Safari 3.0.1

3.0.1

Apple Safari 3.0.1 for Mac OS X

3.0.1

Apple Safari 3.0.1 Beta

3.0.1

Apple Safari 3.0.1b

3.0.1b

Apple Safari 3.0.1b for Windows

3.0.1b

Apple Safari 3.0.2

3.0.2

Apple Safari 3.0.2 for Mac OS X

3.0.2

Apple Safari 3.0.2b

3.0.2b

Apple Safari 3.0.2b for Windows

3.0.2b

Apple Safari 3.0.3

3.0.3

Apple Safari 3.0.3 for Mac OS X

3.0.3

Apple Safari 3.0.3b

3.0.3b

Apple Safari 3.0.3b for Windows

3.0.3b

Apple Safari 3.0.4

3.0.4

Apple Safari 3.0.4 for Mac OS X

3.0.4

Apple Safari 3.0.4b

3.0.4b

Apple Safari 3.0.4b for Windows

3.0.4b

Apple Safari 3.1.0

3.1.0

Apple Safari 3.1.0 for Mac OS X

3.1.0

Apple Safari 3.1.0b

3.1.0b

Apple Safari 3.1.0b for Windows

3.1.0b

Apple Safari 3.1.1

3.1.1

Apple Safari 3.1.1b for Windows

3.1.1b

Apple Safari 3.1.2

3.1.2

Apple Safari 3.1.2b for Windows

3.1.2b

Apple Safari 3.2.0

3.2.0

Apple Safari 3.2.0b for Windows

3.2.0b

Apple Safari 3.2.1

3.2.1

Apple Safari 3.2.1b for Windows

3.2.1b

Apple Safari 3.2.2

3.2.2

Apple Safari 3.2.2b for Windows

3.2.2b

Apple Safari 4.0

4.0

Apple Safari 4 Beta

4.0

Apple Safari 4.0.0b

4.0.0b

Apple Safari 4.0.1

4.0.1

Apple Safari 4.0.2

4.0.2

Apple Safari 4.0.3

4.0.3

Apple Safari 4.0.4

4.0.4

Apple Safari 4.0.5

4.0.5

Apple Safari 4.1

4.1

Apple Safari 4.1.1

4.1.1

Apple Safari 4.1.2

4.1.2

Apple Safari 5.0

5.0

Apple Safari 5.0.1

5.0.1

Apple Safari 5.0.2

5.0.2

Apple Safari 5.0.4

5.0.4

Apple Safari 5.0.5

5.0.5

Apple Safari 5.0.6

5.0.6

Stay updated

ExploitPedia is constantly evolving. Sign up to receive a notification when we release additional functionality.

Get in touch

If you'd like to report a bug or have any suggestions for improvements then please do get in touch with us using this form. We will get back to you as soon as we can.