Ir al contenido


Foto

Modificar archivo shadow


  • Por favor identifícate para responder
No hay respuestas en este tema

#1 reevil

reevil

    Member

  • Miembros
  • PipPip
  • 32 mensajes
  • LocationMéxico

Escrito 28 abril 2014 - 05:22

Hola a todos.

Tengo una pagina en la que he pienso otrogar algunas direcciones de correo, y permitir que los usuarios cambien sus claves.
Obviamente no es posible dejarlos acceder al Cpanel para hacer esa modificación.
Así que buscando un poco encontré un código que permite modificar las contraseñas desde PHP.

El codigo en cuestión es este:


delphi
  1. <?php
  2.  
  3. $message = "";
  4. $found = $valid = false;
  5.  
  6. if ($_POST['username'] != "") {
  7.     $domain_pos = strpos($_POST['username'], "@");
  8.     if ($domain_pos === false) {
  9.         $username = $_POST['username'];
  10.         $domain = $_POST['domain'];
  11.     } else {
  12.         $username = substr($_POST['username'], 0, $domain_pos);
  13.         $domain = substr($_POST['username'], $domain_pos + 1);
  14.     }
  15.    
  16.     $current_password = $_POST['current_password'];
  17.     $new_password1 = $_POST['new_password1'];
  18.     $new_password2 = $_POST['new_password2'];
  19.    
  20.     $root = $_SERVER['DOCUMENT_ROOT'];
  21.     $path_elements = explode('/', $root);
  22.     $root = "/{$path_elements[1]}/{$path_elements[2]}"; // for bluehost, extracts homedir ex: /homeN/blueuser may work with other hosts?
  23.     $shadow_file = "$root/etc/$domain/shadow";
  24.  
  25.     // check if the shadow file exists. if not, either an invalid
  26.     // domain was entered or this may not be a bluehost account...?
  27.     if (file_exists($shadow_file)) {
  28.         // compare the new passwords entered to ensure they match.   
  29.         if ($new_password1 == $new_password2) {
  30.             if (trim($new_password1) != "") {
  31.                 // get the contents of the shadow file.
  32.                 $shadow = file_get_contents($shadow_file);
  33.                 $lines = explode("\n", $shadow);
  34.                
  35.                 // go through each line of shadow file, looking for username entered.
  36.                 for ($i = 0; $i < count($lines); $i++) {
  37.                     $elements = explode(":", $lines[$i]);
  38.                     // found the user...
  39.                     if ($elements[0] == $username) {
  40.                         $found = true;
  41.                         $passwd = explode("$", $elements[1]);
  42.                         $salt = $passwd[2]; // get the salt currently used
  43.                        
  44.                         // crypt the "Current Password" entered by user. Can use either builtin
  45.                         // php crypt function or command line openssl command.
  46.                         if (CRYPT_MD5 == 1) { // indicates whether or not the crypt command supports MD5.
  47.                             $current = crypt($current_password, '$1$'.$salt.'$');
  48.                         } else {
  49.                             $current = trim(`openssl passwd -1 -salt "$salt" "$current_password"`);
  50.                         }
  51.                         // check if the current password entered by the user
  52.                         // matches the password in the shadow file.
  53.                         $valid = ($current == $elements[1]);
  54.                        
  55.                         if ($valid) {
  56.                             // if they match then crypt the new password using the same salt
  57.                             // and modify the line in the shadow file with the new hashed password
  58.                             if (CRYPT_MD5 == 1) {
  59.                                 $new = crypt($new_password1, '$1$'.$salt.'$');
  60.                             } else {
  61.                                 $new = trim(`openssl passwd -1 -salt "$salt" "$new_password1"`);
  62.                             }
  63.                             $elements[1] = $new;
  64.                             $lines[$i] = implode(":", $elements);
  65.                         }
  66.                        
  67.                         break;
  68.                     }
  69.                 }
  70.                
  71.                 if (!$found) {
  72.                     $message = "The username you entered is not valid.";
  73.                 } else if (!$valid) {
  74.                     $message = "The password you entered is not valid.";
  75.                 } else {
  76.                     // write the new contents of the shadow back to the shadow file.
  77.                     $shadow = implode("\n", $lines);
  78.                     file_put_contents($shadow_file, $shadow);
  79.                     $message = 'Your password has been updated.';
  80.                 }
  81.             } else {
  82.                 $message = "Your password cannot be blank.";
  83.             }
  84.         } else {
  85.             $message = "Both new passwords must match.";
  86.         }
  87.     } else {
  88.         $message = "The domain you entered is not valid.";
  89.     }
  90. }
  91.  
  92. ?>
  93. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  94. <html xmlns="http://www.w3.org/1999/xhtml">
  95.     <head>
  96.         <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  97.         <title>Change Password</title>
  98.     </head>
  99.    
  100.     <body>           
  101.         <?php
  102.             if ($message != "") {
  103.                 $color = $found && $valid ? "green" : "red";
  104.                 echo "<span style=\"color:$color;\">$message</span>";
  105.             }
  106.         ?>
  107.        
  108.         <form action="" method="post">
  109.             <input type="hidden" name="domain" value="somebluehostdomain.com" />
  110.             <table>
  111.                 <tbody>
  112.                     <tr>
  113.                         <td><label for="username">Username</label></td>
  114.                         <td><input name="username" id="username" type="text" /></td>
  115.                     </tr>
  116.                     <tr>
  117.                         <td><label for="current_password">Current Password</label></td>
  118.                         <td><input name="current_password" id="current_password" type="password" /></td>
  119.                     </tr>
  120.                     <tr>
  121.                         <td><label for="new_password1">New Password</label></td>
  122.                         <td><input name="new_password1" id="new_password1" type="password" /></td>
  123.                     </tr>
  124.                     <tr>
  125.                         <td><label for="new_password2">New Password</label></td>
  126.                         <td><input name="new_password2" id="new_password2" type="password" /></td>
  127.                     </tr>
  128.                     <tr>
  129.                         <td colspan="2" style="text-align:center;">
  130.                             <input type="submit" value="Change Password" />
  131.                         </td>
  132.                     </tr>
  133.                 </tbody>
  134.             </table>
  135.         </form>
  136.     </body>
  137. </html>



Modifica localiza y modifica un archivo shadow, al parecer funciona la primera parte, pero al llegar a esta sección:



delphi
  1. if ($elements[0] == $username) {
  2.                         $found = true;
  3.                         $passwd = explode("$", $elements[1]);
  4.                         $salt = $passwd[2]; // get the salt currently used
  5.                        
  6.                         // crypt the "Current Password" entered by user. Can use either builtin
  7.                         // php crypt function or command line openssl command.
  8.                         if (CRYPT_MD5 == 1) { // indicates whether or not the crypt command supports MD5.
  9.                             $current = crypt($current_password, '$1$'.$salt.'$');
  10.                         } else {
  11.                             $current = trim(`openssl passwd -1 -salt "$salt" "$current_password"`);
  12.                         }
  13.                         // check if the current password entered by the user
  14.                         // matches the password in the shadow file.
  15.                         $valid = ($current == $elements[1]);
  16.                        
  17.                         if ($valid) {
  18.                             // if they match then crypt the new password using the same salt
  19.                             // and modify the line in the shadow file with the new hashed password
  20.                             if (CRYPT_MD5 == 1) {
  21.                                 $new = crypt($new_password1, '$1$'.$salt.'$');
  22.                             } else {
  23.                                 $new = trim(`openssl passwd -1 -salt "$salt" "$new_password1"`);
  24.                             }
  25.                             $elements[1] = $new;
  26.                             $lines[$i] = implode(":", $elements);
  27.                         }[/b]



Me pierdo, pues mis conocimientos en criptografía son nulos.
El mensaje que me arroja es que  "El password ingresasado no es valido"


delphi
  1. $message = "The password you entered is not valid.";



El formato del archivo shadow es este:


delphi
  1. prueba1:$6$3H43CuBDVvvAelSW$ZVzh9IAvZV.D2xtILRWutYPoU9DCjmWppZdWpl9rF4K6.7t6QnWaTypN6a7eEycua/miaiT1SQstYGxSYFgMC/:16128::::::
  2. prueba2:$6$3H43CuBDVvvAelSW$ZVzh9IAvZV.D2xtILRWutYPoU9DCjmWppZdWpl9rF4K6.7t6QnWaTypN6a7eEycua/miaiT1SQstYGxSYFgMC/:16128::::::
  3. prueba3:$1$PrLoWZDE$Bru2j2HN8475ic5SPdeQL1:16128::::::



Ojala alguien pueda ayudarme o darme un empujón en la dirección correcta.
Saludos.
  • 0




IP.Board spam blocked by CleanTalk.