CVE-2015-7698 (published 2015-10-21T18:59Z)

icewind1991 SMB before 1.0.3 allows remote authenticated users to execute arbitrary SMB commands via shell metacharacters in the user argument in the (1) listShares function in Server.php or the (2) connect or (3) read function in Share.php.

Severity: HIGH
CVSS v2 base score: 9.0 (AV:N/AC:L/Au:S/C:C/I:C/A:C)

Weakness information:
cwe_id: CWE-78
cwe_name: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
description: The product constructs all or part of an OS command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended OS command when it is sent to a downstream component.
extended_description: 
		   This weakness can lead to a vulnerability in environments in which the attacker does not have direct access to the operating system, such as in web applications. Alternately, if the weakness occurs in a privileged program, it could allow the attacker to specify commands that normally would not be accessible, or to call alternate commands with privileges that the attacker does not have. The problem is exacerbated if the compromised process does not follow the principle of least privilege, because the attacker-controlled commands may run with special system privileges that increases the amount of damage.
            There are at least two subtypes of OS command injection:
               
				 The application intends to execute a single, fixed program that is under its own control. It intends to use externally-supplied inputs as arguments to that program. For example, the program might use system("nslookup [HOSTNAME]") to run nslookup and allow the user to supply a HOSTNAME, which is used as an argument. Attackers cannot prevent nslookup from executing. However, if the program does not remove command separators from the HOSTNAME argument, attackers could place the separators into the arguments, which allows them to execute their own program after nslookup has finished executing.
				 The application accepts an input that it uses to fully select which program to run, as well as which commands to use. The application simply redirects this entire command to the operating system. For example, the program might use "exec([COMMAND])" to execute the [COMMAND] that was supplied by the user. If the COMMAND is under attacker control, then the attacker can execute arbitrary commands or programs. If the command is being executed using functions like exec() and CreateProcess(), the attacker might not be able to combine multiple commands together in the same line.
               
            From a weakness standpoint, these variants represent distinct programmer errors. In the first variant, the programmer clearly intends that input from untrusted parties will be part of the arguments in the command to be executed. In the second variant, the programmer does not intend for the command to be accessible to any untrusted party, but the programmer probably has not accounted for alternate ways in which malicious attackers can provide input.
         
         

Fix information:
repo_url: https://github.com/icewind1991/SMB
commit_details: repo_url: https://github.com/icewind1991/SMB
author: Robin Appelman
author_date: 2015-08-13 16:17:46+02:00
msg: improve support for workgroups/domains
repository: repo_url: https://github.com/icewind1991/SMB
repo_name: icewind1991/SMB
description: PHP wrapper for smbclient
repo_language: PHP
owner: icewind1991
file_changes: filename: NativeServer.php
change_type: ModificationType.MODIFY
diff: @@ -24,12 +24,7 @@ class NativeServer extends Server {
 	}
 
 	protected function connect() {
-		$user = $this->getUser();
-		$workgroup = null;
-		if (strpos($user, '/')) {
-			list($workgroup, $user) = explode('/', $user);
-		}
-		$this->state->init($workgroup, $user, $this->getPassword());
+		$this->state->init($this->getWorkgroup(), $this->getUser(), $this->getPassword());
 	}
 
 	/**

programming_language: PHP
filename: NativeShare.php
change_type: ModificationType.MODIFY
diff: @@ -43,15 +43,7 @@ class NativeShare implements IShare {
 			return;
 		}
 
-		$user = $this->server->getUser();
-		if (strpos($user, '/')) {
-			list($workgroup, $user) = explode('/', $user);
-		} elseif (strpos($user, '\\')) {
-			list($workgroup, $user) = explode('\\', $user);
-		} else {
-			$workgroup = null;
-		}
-		$this->state->init($workgroup, $user, $this->server->getPassword());
+		$this->state->init($this->server->getWorkgroup(), $this->server->getUser(), $this->server->getPassword());
 	}
 
 	/**

programming_language: PHP
filename: Server.php
change_type: ModificationType.MODIFY
diff: @@ -29,6 +29,11 @@ class Server {
 	 */
 	protected $password;
 
+	/**
+	 * @var string $workgroup
+	 */
+	protected $workgroup;
+
 	/**
 	 * Check if the smbclient php extension is available
 	 *
@@ -45,10 +50,28 @@ class Server {
 	 */
 	public function __construct($host, $user, $password) {
 		$this->host = $host;
+		list($workgroup, $user) = $this->splitUser($user);
 		$this->user = $user;
+		$this->workgroup = $workgroup;
 		$this->password = $password;
 	}
 
+	/**
+	 * Split workgroup from username
+	 *
+	 * @param $user
+	 * @return string[] [$workgroup, $user]
+	 */
+	public function splitUser($user) {
+		if (strpos($user, '/')) {
+			return explode('/', $user, 2);
+		} elseif (strpos($user, '\\')) {
+			return explode('\\', $user);
+		} else {
+			return [null, $user];
+		}
+	}
+
 	/**
 	 * @return string
 	 */
@@ -77,6 +100,13 @@ class Server {
 		return $this->host;
 	}
 
+	/**
+	 * @return string
+	 */
+	public function getWorkgroup() {
+		return $this->workgroup;
+	}
+
 	/**
 	 * @return \Icewind\SMB\IShare[]
 	 *
@@ -84,7 +114,8 @@ class Server {
 	 * @throws \Icewind\SMB\Exception\InvalidHostException
 	 */
 	public function listShares() {
-		$command = Server::CLIENT . ' --authentication-file=/proc/self/fd/3' .
+		$workgroupArgument = ($this->workgroup) ? ' -W ' . escapeshellarg($this->workgroup) : '';
+		$command = Server::CLIENT . $workgroupArgument . ' --authentication-file=/proc/self/fd/3' .
 			' -gL ' . escapeshellarg($this->getHost());
 		$connection = new RawConnection($command);
 		$connection->writeAuthentication($this->getUser(), $this->getPassword());

programming_language: PHP
filename: Share.php
change_type: ModificationType.MODIFY
diff: @@ -57,8 +57,10 @@ class Share implements IShare {
 		if ($this->connection and $this->connection->isValid()) {
 			return;
 		}
-		$command = sprintf('%s --authentication-file=/proc/self/fd/3 //%s/%s',
+		$workgroupArgument = ($this->server->getWorkgroup()) ? ' -W ' . escapeshellarg($this->server->getWorkgroup()) : '';
+		$command = sprintf('%s %s --authentication-file=/proc/self/fd/3 //%s/%s',
 			Server::CLIENT,
+			$workgroupArgument,
 			$this->server->getHost(),
 			$this->name
 		);
@@ -260,8 +262,10 @@ class Share implements IShare {
 		$source = str_replace('\'', '\'"\'"\'', $source);
 		// since returned stream is closed by the caller we need to create a new instance
 		// since we can't re-use the same file descriptor over multiple calls
-		$command = sprintf('%s --authentication-file=/proc/self/fd/3 //%s/%s -c \'get %s /proc/self/fd/5\'',
+		$workgroupArgument = ($this->server->getWorkgroup()) ? ' -W ' . escapeshellarg($this->server->getWorkgroup()) : '';
+		$command = sprintf('%s %s --authentication-file=/proc/self/fd/3 //%s/%s -c \'get %s /proc/self/fd/5\'',
 			Server::CLIENT,
+			$workgroupArgument,
 			$this->server->getHost(),
 			$this->name,
 			$source
@@ -288,8 +292,10 @@ class Share implements IShare {
 		$target = str_replace('\'', '\'"\'"\'', $target);
 		// since returned stream is closed by the caller we need to create a new instance
 		// since we can't re-use the same file descriptor over multiple calls
-		$command = sprintf('%s --authentication-file=/proc/self/fd/3 //%s/%s -c \'put /proc/self/fd/4 %s\'',
+		$workgroupArgument = ($this->server->getWorkgroup()) ? ' -W ' . escapeshellarg($this->server->getWorkgroup()) : '';
+		$command = sprintf('%s %s --authentication-file=/proc/self/fd/3 //%s/%s -c \'put /proc/self/fd/4 %s\'',
 			Server::CLIENT,
+			$workgroupArgument,
 			$this->server->getHost(),
 			$this->name,
 			$target

programming_language: PHP