CVE-2023-41580 (published 2023-10-02T13:15Z)

Phpipam before v1.5.2 was discovered to contain a LDAP injection vulnerability via the dname parameter at /users/ad-search-result.php. This vulnerability allows attackers to enumerate arbitrary fields in the LDAP server and access sensitive data via a crafted POST request.

CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-74
cwe_name: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')
description: The product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component.
extended_description: Software or other automated logic has certain assumptions about what constitutes data and control respectively. It is the lack of verification of these assumptions for user-controlled input that leads to injection problems. Injection problems encompass a wide variety of issues -- all mitigated in very different ways and usually attempted in order to alter the control flow of the process. For this reason, the most effective way to discuss these weaknesses is to note the distinct features that classify them as injection weaknesses. The most important issue to note is that all injection problems share one thing in common -- i.e., they allow for the injection of control plane data into the user-controlled data plane. This means that the execution of the process may be altered by sending code in through legitimate data channels, using no other mechanism. While buffer overflows, and many other flaws, involve the use of some further issue to gain execution, injection problems need only for the data to be parsed.
         

Fix information:
repo_url: https://github.com/phpipam/phpipam
commit_details: repo_url: https://github.com/phpipam/phpipam
author: Gary Allan
author_date: 2023-02-05 21:45:04+00:00
msg: Bugfix: XSS and LDAP injection in ad-search-group-result.php

Reported by Elias Hohl
repository: repo_url: https://github.com/phpipam/phpipam
repo_name: phpipam/phpipam
description: phpipam development repository
repo_language: PHP
owner: phpipam
file_changes: filename: ad-search-group-result.php
change_type: ModificationType.MODIFY
diff: @@ -6,7 +6,7 @@
 
 /* functions */
 require_once( dirname(__FILE__) . '/../../../functions/functions.php' );
-require( dirname(__FILE__) . "/../../../functions/adLDAP/src/adLDAP.php");
+require_once( dirname(__FILE__) . "/../../../functions/adLDAP/src/adLDAP.php");
 
 # initialize user object
 $Database 	= new Database_PDO;
@@ -59,13 +59,14 @@ try {
 	// Use credentials if they've been provided
 	if (isset($params->adminUsername) && isset($params->adminPassword)) {
 		$authUser = $adldap->authenticate($params->adminUsername, $params->adminPassword);
-		if ($authUser == false) {
+		if (!$authUser) {
 			$Result->show("danger", _("Invalid credentials"), true);
 		}
 	}
 
 	//search groups
-	$groups = $adldap->group()->search(adLDAP::ADLDAP_SECURITY_GLOBAL_GROUP,true,"*$_POST[dfilter]*");
+	$esc_dfilter = ldap_escape($_POST["dfilter"], null, LDAP_ESCAPE_FILTER);
+	$groups = $adldap->group()->search(adLDAP::ADLDAP_SECURITY_GLOBAL_GROUP, true, "*$esc_dfilter*");
 
 	//echo $adldap->getLastError();
 }
@@ -81,8 +82,8 @@ if(sizeof($groups)==0) {
 	print _('No groups found')."!<hr>";
 	print _('Possible reasons').":";
 	print "<ul>";
-	print "<li>"._('Invalid baseDN setting for ' . $server->type)."</li>";
-	print "<li>"._($server->type . ' account does not have enough privileges for search')."</li>";
+	print "<li>"._('Invalid baseDN setting for ') . escape_input($server->type)."</li>";
+	print "<li>".escape_input($server->type). ' '. _('account does not have enough privileges for search')."</li>";
 	print "</div>";
 } else {
 	print _(" Following groups were found").": (".sizeof($groups)."):<hr>";
@@ -91,32 +92,33 @@ if(sizeof($groups)==0) {
 
 	// loop
  	foreach($groups as $k=>$g) {
+		$esc_k = escape_input($k);
+		$esc_g = escape_input($g);
+
+		// search members
+		$groupMembers = $adldap->group()->members($k) ?: [];
+		$esc_members = !empty($groupMembers) ? escape_input(implode(";", $groupMembers)) : '';
+
 		print "<tr>";
-		print "	<td>$k</td>";
-		print "	<td>$g</td>";
+		print "	<td>$esc_k</td>";
+		print "	<td>$esc_g</td>";
 		//actions
 		print " <td style='width:10px;'>";
-		print "		<a href='' class='btn btn-sm btn-default btn-success groupselect' data-gname='$k' data-gdescription='$g' data-members='$members' data-gid='$k' data-csrf_cookie='$csrf'>"._('Add group')."</a>";
+		print "		<a href='' class='btn btn-sm btn-default btn-success groupselect' data-gname='$esc_k' data-gdescription='$esc_g' data-members='$esc_members' data-gid='$esc_k' data-csrf_cookie='$csrf'>"._('Add group')."</a>";
 		print "	</td>";
 		print "</tr>";
 
 		print "<tr>";
 		print "	<td>"._("Members:")."</td>";
 		print "<td colspan='2'>";
-		print "	<div class='adgroup-$k'></div>";
-		// search members
-		$groupMembers = $adldap->group()->members($k);
-		unset($members);
-		if($groupMembers!==false) {
+		print "	<div class='adgroup-$esc_k'></div>";
+
+		if (!empty($groupMembers)) {
 			foreach($groupMembers as $m) {
-				print "<span class='muted'>$m</span><br>";
-				$members[] = $m;
+				print "<span class='muted'>".escape_input($m)."</span><br>";
 			}
-			if(isset($members))
-			$members = implode(";", $members);
 		}
 		else {
-			$members = "";
 			print "<span class='muted'>"._("No members")."</span>";
 		}
 		print "	</td>";
@@ -125,5 +127,3 @@ if(sizeof($groups)==0) {
 	}
 	print "</table>";
 }
-
-?>
\ No newline at end of file

programming_language: PHP
filename: ad-search-result.php
change_type: ModificationType.MODIFY
diff: @@ -6,7 +6,7 @@
 
 /* functions */
 require_once( dirname(__FILE__) . '/../../../functions/functions.php' );
-require( dirname(__FILE__) . "/../../../functions/adLDAP/src/adLDAP.php");
+require_once( dirname(__FILE__) . "/../../../functions/adLDAP/src/adLDAP.php");
 
 # initialize user object
 $Database 	= new Database_PDO;
@@ -52,13 +52,13 @@ try {
 
 	//try to login with higher credentials for search
 	$authUser = $adldap->authenticate($params->adminUsername, $params->adminPassword);
-	if ($authUser == false) {
+	if (!$authUser) {
 		$Result->show("danger", _("Invalid credentials")."<br>".$adldap->getLastError(), true);
 	}
 
 	//search for domain user!
-	$escaped_dn = ldap_escape($_POST["dname"], null, LDAP_ESCAPE_DN);
-	$userinfo = $adldap->user()->info("$escaped_dn*", array("*"), false, $server->type);
+	$esc_dname = ldap_escape($_POST["dname"], null, LDAP_ESCAPE_FILTER);
+	$userinfo = $adldap->user()->info("*$esc_dname*", array("*"), false, $server->type);
 
 	//echo $adldap->getLastError();
 }
@@ -87,8 +87,7 @@ if(!isset($userinfo['count'])) {
 		// loop
 		foreach($userinfo as $u) {
 			print "<tr>";
-			print "	<td>".escape_input($u['displayname'][0]);
-			print "</td>";
+			print "	<td>".escape_input($u['displayname'][0])."</td>";
 			print "	<td>".escape_input($u['samaccountname'][0])."</td>";
 			print "	<td>".escape_input($u['mail'][0])."</td>";
 			//actions

programming_language: PHP
filename: CHANGELOG
change_type: ModificationType.MODIFY
diff: @@ -9,6 +9,7 @@
     + XSS (reflected) in 'bw-calulator-result.php';
     + XSS (reflected) by invalid email address response;
     + XSS and LDAP injection in ad-search-result.php;
+    + XSS and LDAP injection in ad-search-group-result.php;
     + Restrict find_full_subnets.php to CLI;
     + Ensure confidentiality of database password;
 

programming_language: TypeScript