Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <?php
  2. // APR1-MD5 encryption method (windows compatible)
  3. function crypt_apr1_md5($plainpasswd, $salt)
  4. {
  5. $tmp = "";
  6. $len = strlen($plainpasswd);
  7. $text = $plainpasswd.'$apr1$'.$salt;
  8. $bin = pack("H32", md5($plainpasswd.$salt.$plainpasswd));
  9. for($i = $len; $i > 0; $i -= 16) { $text .= substr($bin, 0, min(16, $i)); }
  10. for($i = $len; $i > 0; $i >>= 1) { $text .= ($i & 1) ? chr(0) : $plainpasswd{0}; }
  11. $bin = pack("H32", md5($text));
  12. for($i = 0; $i < 1000; $i++)
  13. {
  14. $new = ($i & 1) ? $plainpasswd : $bin;
  15. if ($i % 3) $new .= $salt;
  16. if ($i % 7) $new .= $plainpasswd;
  17. $new .= ($i & 1) ? $bin : $plainpasswd;
  18. $bin = pack("H32", md5($new));
  19. }
  20. for ($i = 0; $i < 5; $i++)
  21. {
  22. $k = $i + 6;
  23. $j = $i + 12;
  24. if ($j == 16) $j = 5;
  25. $tmp = $bin[$i].$bin[$k].$bin[$j].$tmp;
  26. }
  27. $tmp = chr(0).chr(0).$bin[11].$tmp;
  28. $tmp = strtr(strrev(substr(base64_encode($tmp), 2)),
  29. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  30. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
  31.  
  32. return "$"."apr1"."$".$salt."$".$tmp;
  33. }
  34.  
  35. function get_htpasswd ( $passwdFile, $username )
  36. {
  37. $lines = file($passwdFile);
  38. foreach ($lines as $line)
  39. {
  40. $arr = explode(":", $line);
  41. $fileUsername = $arr[0];
  42. if ($fileUsername == $username)
  43. {
  44. $filePasswd = trim($arr[1]);
  45. return $filePasswd;
  46. }
  47. }
  48. return false;
  49. }
  50.  
  51. function matches($password, $filePasswd)
  52. {
  53. if (strpos($filePasswd, '$apr1') === 0)
  54. {
  55. // MD5
  56. $passParts = explode('$', $filePasswd);
  57. $salt = $passParts[2];
  58. $hashed = crypt_apr1_md5($password, $salt);
  59. return $hashed == $filePasswd;
  60. }
  61. elseif (strpos($filePasswd, '{SHA}') === 0)
  62. {
  63. // SHA1
  64. $hashed = "{SHA}" . base64_encode(sha1($password, TRUE));
  65. return $hashed == $filePasswd;
  66. }
  67. elseif (strpos($filePasswd, '$2y$') === 0)
  68. {
  69. // Bcrypt
  70. return password_verify ($password, $filePasswd);
  71. }
  72. else
  73. {
  74. // Crypt
  75. $salt = substr($filePasswd, 0, 2);
  76. $hashed = crypt($password, $salt);
  77. return $hashed == $filePasswd;
  78. }
  79. return false;
  80. }
  81.  
  82. $username = $argv[1];
  83. $password = $argv[2];
  84.  
  85. $filePasswd = get_htpasswd( 'pass', $username );
  86.  
  87. if ( matches($password, $filePasswd) )
  88. {
  89. echo "Correct password\n";
  90. }
  91. else
  92. {
  93. echo "Incorrect username or password\n";
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement