ntlm_sasl_client.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /*
  3. * ntlm_sasl_client.php
  4. *
  5. * @(#) $Id: ntlm_sasl_client.php,v 1.3 2004/11/17 08:00:37 mlemos Exp $
  6. *
  7. */
  8. define("SASL_NTLM_STATE_START", 0);
  9. define("SASL_NTLM_STATE_IDENTIFY_DOMAIN", 1);
  10. define("SASL_NTLM_STATE_RESPOND_CHALLENGE", 2);
  11. define("SASL_NTLM_STATE_DONE", 3);
  12. define("SASL_FAIL", -1);
  13. define("SASL_CONTINUE", 1);
  14. class ntlm_sasl_client_class
  15. {
  16. public $credentials = array();
  17. public $state = SASL_NTLM_STATE_START;
  18. public function initialize(&$client)
  19. {
  20. if (!function_exists($function = "mcrypt_encrypt")
  21. || !function_exists($function = "mhash")
  22. ) {
  23. $extensions = array(
  24. "mcrypt_encrypt" => "mcrypt",
  25. "mhash" => "mhash"
  26. );
  27. $client->error = "the extension " . $extensions[$function] .
  28. " required by the NTLM SASL client class is not available in this PHP configuration";
  29. return (0);
  30. }
  31. return (1);
  32. }
  33. public function ASCIIToUnicode($ascii)
  34. {
  35. for ($unicode = "", $a = 0; $a < strlen($ascii); $a++) {
  36. $unicode .= substr($ascii, $a, 1) . chr(0);
  37. }
  38. return ($unicode);
  39. }
  40. public function typeMsg1($domain, $workstation)
  41. {
  42. $domain_length = strlen($domain);
  43. $workstation_length = strlen($workstation);
  44. $workstation_offset = 32;
  45. $domain_offset = $workstation_offset + $workstation_length;
  46. return (
  47. "NTLMSSP\0" .
  48. "\x01\x00\x00\x00" .
  49. "\x07\x32\x00\x00" .
  50. pack("v", $domain_length) .
  51. pack("v", $domain_length) .
  52. pack("V", $domain_offset) .
  53. pack("v", $workstation_length) .
  54. pack("v", $workstation_length) .
  55. pack("V", $workstation_offset) .
  56. $workstation .
  57. $domain
  58. );
  59. }
  60. public function NTLMResponse($challenge, $password)
  61. {
  62. $unicode = $this->ASCIIToUnicode($password);
  63. $md4 = mhash(MHASH_MD4, $unicode);
  64. $padded = $md4 . str_repeat(chr(0), 21 - strlen($md4));
  65. $iv_size = mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_ECB);
  66. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  67. for ($response = "", $third = 0; $third < 21; $third += 7) {
  68. for ($packed = "", $p = $third; $p < $third + 7; $p++) {
  69. $packed .= str_pad(decbin(ord(substr($padded, $p, 1))), 8, "0", STR_PAD_LEFT);
  70. }
  71. for ($key = "", $p = 0; $p < strlen($packed); $p += 7) {
  72. $s = substr($packed, $p, 7);
  73. $b = $s . ((substr_count($s, "1") % 2) ? "0" : "1");
  74. $key .= chr(bindec($b));
  75. }
  76. $ciphertext = mcrypt_encrypt(MCRYPT_DES, $key, $challenge, MCRYPT_MODE_ECB, $iv);
  77. $response .= $ciphertext;
  78. }
  79. return $response;
  80. }
  81. public function typeMsg3($ntlm_response, $user, $domain, $workstation)
  82. {
  83. $domain_unicode = $this->ASCIIToUnicode($domain);
  84. $domain_length = strlen($domain_unicode);
  85. $domain_offset = 64;
  86. $user_unicode = $this->ASCIIToUnicode($user);
  87. $user_length = strlen($user_unicode);
  88. $user_offset = $domain_offset + $domain_length;
  89. $workstation_unicode = $this->ASCIIToUnicode($workstation);
  90. $workstation_length = strlen($workstation_unicode);
  91. $workstation_offset = $user_offset + $user_length;
  92. $lm = "";
  93. $lm_length = strlen($lm);
  94. $lm_offset = $workstation_offset + $workstation_length;
  95. $ntlm = $ntlm_response;
  96. $ntlm_length = strlen($ntlm);
  97. $ntlm_offset = $lm_offset + $lm_length;
  98. $session = "";
  99. $session_length = strlen($session);
  100. $session_offset = $ntlm_offset + $ntlm_length;
  101. return (
  102. "NTLMSSP\0" .
  103. "\x03\x00\x00\x00" .
  104. pack("v", $lm_length) .
  105. pack("v", $lm_length) .
  106. pack("V", $lm_offset) .
  107. pack("v", $ntlm_length) .
  108. pack("v", $ntlm_length) .
  109. pack("V", $ntlm_offset) .
  110. pack("v", $domain_length) .
  111. pack("v", $domain_length) .
  112. pack("V", $domain_offset) .
  113. pack("v", $user_length) .
  114. pack("v", $user_length) .
  115. pack("V", $user_offset) .
  116. pack("v", $workstation_length) .
  117. pack("v", $workstation_length) .
  118. pack("V", $workstation_offset) .
  119. pack("v", $session_length) .
  120. pack("v", $session_length) .
  121. pack("V", $session_offset) .
  122. "\x01\x02\x00\x00" .
  123. $domain_unicode .
  124. $user_unicode .
  125. $workstation_unicode .
  126. $lm .
  127. $ntlm
  128. );
  129. }
  130. public function start(&$client, &$message, &$interactions)
  131. {
  132. if ($this->state != SASL_NTLM_STATE_START) {
  133. $client->error = "NTLM authentication state is not at the start";
  134. return (SASL_FAIL);
  135. }
  136. $this->credentials = array(
  137. "user" => "",
  138. "password" => "",
  139. "realm" => "",
  140. "workstation" => ""
  141. );
  142. $defaults = array();
  143. $status = $client->GetCredentials($this->credentials, $defaults, $interactions);
  144. if ($status == SASL_CONTINUE) {
  145. $this->state = SASL_NTLM_STATE_IDENTIFY_DOMAIN;
  146. }
  147. unset($message);
  148. return ($status);
  149. }
  150. public function step(&$client, $response, &$message, &$interactions)
  151. {
  152. switch ($this->state) {
  153. case SASL_NTLM_STATE_IDENTIFY_DOMAIN:
  154. $message = $this->TypeMsg1($this->credentials["realm"], $this->credentials["workstation"]);
  155. $this->state = SASL_NTLM_STATE_RESPOND_CHALLENGE;
  156. break;
  157. case SASL_NTLM_STATE_RESPOND_CHALLENGE:
  158. $ntlm_response = $this->NTLMResponse(substr($response, 24, 8), $this->credentials["password"]);
  159. $message = $this->TypeMsg3(
  160. $ntlm_response,
  161. $this->credentials["user"],
  162. $this->credentials["realm"],
  163. $this->credentials["workstation"]
  164. );
  165. $this->state = SASL_NTLM_STATE_DONE;
  166. break;
  167. case SASL_NTLM_STATE_DONE:
  168. $client->error = "NTLM authentication was finished without success";
  169. return (SASL_FAIL);
  170. default:
  171. $client->error = "invalid NTLM authentication step state";
  172. return (SASL_FAIL);
  173. }
  174. return (SASL_CONTINUE);
  175. }
  176. }