PDA

View Full Version : PHP password change script


Riane
16.04.2005, 23:26
since i still dont have a server running, i need you guys to test this script of mine

change as you wish, but plz leave the copyright ^_^

oh and of course report any bug to me (i guess there will be many as always)

heres the scripts:

password.php:
[php:1:4a8c9bfe1a]<?
require "./pcfg.php";
mssql_connect($dbhost,$dbuser,$dbpass) OR die('Cant establish database connection.');
mssql_select_db("lin2db");

if($_POST['change'])
{
//change PW
if(!$_POST['accountname'])
$error.="
You typed in an incorrect Account Name.";
if(!$_POST['newpass'] || !$_POST['oldpass'])
$error.="
You typed in an incorrect Password.";
if($_POST['newpass'] != $_POST['newpass2'])
$error.="
Your Password does not match your retype.";
$query=mssql_query("SELECT account, password FROM user_auth WHERE account = '".$_POST['accountname']."' AND password = '".encrypt($_POST['oldpass'])."'");
if(!mssql_num_rows($query))
$error.="
Password doesn't match the account.";
if(!$error)
{
//change PW
mssql_query("UPDATE user_auth SET password = '".encrypt($_POST[newpass])."' WHERE account = '".$_POST['accountname']."' AND password = '".encrypt($_POST['oldpass'])."'");
echo"Password successfully changed.";
}
else
echo $error."
<a href=\"javascript:history.back();\">try again</a>";
}
else
{
//view form
echo"<form action=password.php method=post>";
echo"<input type=text name=accountname>Your Accountname
";
echo"<input type=password name=oldpass>Your Current Password

";
echo"<input type=password name=newpass>New Password
";
echo"<input type=password name=newpass2>Retype New Password

";
echo"<input type=submit name=change value=\"[change]\">";
echo"</form>";
}

#[C] by Riane!!!11#
?>[/php:1:4a8c9bfe1a]


pcfg.php
[php:1:4a8c9bfe1a]<?
$dbhost="localhost"; //server hostname / IP
$dbuser="sa"; //MSSQL username
$dbpassword=""; //MSSQL password
$encrypted=1; //1 if passwords are encrypted, 0 if not


//do not change after this line\\
function encrypt($str) {
global $encrypted;
if($encrypted==1)
return encrypt2($str);
}

function encrypt2($str) {
$key = array();
$dst = array();
$i = 0;

$nBytes = strlen($str);
while ($i < $nBytes){
$i++;
$key[$i] = ord(substr($str, $i - 1, 1));
$dst[$i] = $key[$i];
}

$rslt = $key[1] + $key[2]*256 + $key[3]*65536 + $key[4]*16777216;
$one = $rslt * 213119 + 2529077;
$one = $one - intval($one/ 4294967296) * 4294967296;

$rslt = $key[5] + $key[6]*256 + $key[7]*65536 + $key[8]*16777216;
$two = $rslt * 213247 + 2529089;
$two = $two - intval($two/ 4294967296) * 4294967296;

$rslt = $key[9] + $key[10]*256 + $key[11]*65536 + $key[12]*16777216;
$three = $rslt * 213203 + 2529589;
$three = $three - intval($three/ 4294967296) * 4294967296;

$rslt = $key[13] + $key[14]*256 + $key[15]*65536 + $key[16]*16777216;
$four = $rslt * 213821 + 2529997;
$four = $four - intval($four/ 4294967296) * 4294967296;

$key[4] = intval($one/16777216);
$key[3] = intval(($one - $key[4] * 16777216) / 65535);
$key[2] = intval(($one - $key[4] * 16777216 - $key[3] * 65536) / 256);
$key[1] = intval(($one - $key[4] * 16777216 - $key[3] * 65536 - $key[2] * 256));

$key[8] = intval($two/16777216);
$key[7] = intval(($two - $key[8] * 16777216) / 65535);
$key[6] = intval(($two - $key[8] * 16777216 - $key[7] * 65536) / 256);
$key[5] = intval(($two - $key[8] * 16777216 - $key[7] * 65536 - $key[6] * 256));

$key[12] = intval($three/16777216);
$key[11] = intval(($three - $key[12] * 16777216) / 65535);
$key[10] = intval(($three - $key[12] * 16777216 - $key[11] * 65536) / 256);
$key[9] = intval(($three - $key[12] * 16777216 - $key[11] * 65536 - $key[10] * 256));

$key[16] = intval($four/16777216);
$key[15] = intval(($four - $key[16] * 16777216) / 65535);
$key[14] = intval(($four - $key[16] * 16777216 - $key[15] * 65536) / 256);
$key[13] = intval(($four - $key[16] * 16777216 - $key[15] * 65536 - $key[14] * 256));

$dst[1] = $dst[1] ^ $key[1];

$i=1;
while ($i<16){
$i++;
$dst[$i] = $dst[$i] ^ $dst[$i-1] ^ $key[$i];
}

$i=0;
while ($i<16){
$i++;
if ($dst[$i] == 0) {
$dst[$i] = 102;
}
}

$encrypt = "0x";
$i=0;
while ($i<16){
$i++;
if ($dst[$i] < 16) {
$encrypt = $encrypt . "0" . dechex($dst[$i]);
} else {
$encrypt = $encrypt . dechex($dst[$i]);
}
}

return $encrypt;
}

#[C] by Riane!!!11#
?>[/php:1:4a8c9bfe1a]

Riane
17.04.2005, 12:15
no bugs?
or noone tryed it yet?

Zenith
16.05.2005, 10:48
working good.. thnx

EverCooL
17.05.2005, 12:08
I have few comments ...

1st: This script works ONLY if you are using encypted passwords for your server
If you are NOT using encryption on the acc passwords it will not work for your server...

2nd: It is very UNSECURE since your password for the SQL is not encrypted and can be fetched easyly :)

3rd: When someone stoles your password the first thing to to is change you r the password he has with something else ... meaning there is no security test... and this renderes this page not USELESS but BIGGER pain in the a$$


I'm doing some modifications right now ... will post them latter :)

BiGBoY
17.05.2005, 13:14
Change pass page
[hr:49134192da]
[php:1:49134192da]<?php
securitycheck($securitycheck);

$error = 0;

echo '
<script>
function isAlphaNumeric(value) {
if (value.match(/^[a-zA-Z0-9]+$/)) {
return true;
} else {
return false;
}
}

function checkform() {
if(changepassword.oldpassword.value=="") {
alert("Please enter your old password.")
return false;
}
if(changepassword.newpassword.value=="") {
alert("Please enter a new password.")
return false;
}
if (!isAlphaNumeric(changepassword.newpassword.value) ) {
alert("Your new password must be alphanumeric!");
return false;
}
if(changepassword.newpassword2.value=="") {
alert("Retype your new password.")
return false;
}
if(changepassword.oldpassword.value==changepasswor d.newpassword.value) {
alert("Old password cannot be the same as new password.")
return false;
}
return true;
}
</script>
';

echopage('header', 'Change Password');

if($_POST['action']=='changepassword') {

$error = 2;

connectdb($CONFIG['dbdbname'], $CONFIG['dbaddress'], $CONFIG['dbuser'], $CONFIG['dbpass']);

$username = $_COOKIE['user'];
$oldpassword = $_POST['oldpassword'];
$newpassword = $_POST['newpassword'];
$newpassword2 = $_POST['newpassword2'];
$username = antiinjection($username);
$oldpassword = antiinjection($oldpassword);
$newpassword = antiinjection($newpassword);
$newpassword2 = antiinjection($newpassword2);

echo "<div align=center>";

$result = mssql_query (sprintf(SELECT_USER_PASS, $username));
$rows=mssql_fetch_assoc($result);
extract($rows);

$password = '0x' . bin2hex($password);
$oldpassword = encrypt($oldpassword);

if ($password!=$oldpassword) {
echo "Old password is incorrect.
";
$error = 1;
}

if ((strlen($newpassword)<4 ||strlen($newpassword)>16) && $newpassword!="") {
echo "Password length must be 4 to 16 characters long.
";
$error = 1;
}
if ($newpassword!=$newpassword2) {
echo "Retyped password is incorrect.
";
$error = 1;
}


echo "</div>";

}

if($error<2) {
echo "
<form name='changepassword' action='home.php?page=changepassword' method='post' onsubmit='return checkform()' autocomplete='off'>
<table cellspacing=0 border=0 cellpadding=0 align=center>
<tr>
<td width=200>
Old Password
</td>
<td>
<div align=right>
<input type=password maxlength=16 name=oldpassword>
</div>
</td>
</tr>
<tr>
<td>
New Password
</td>
<td>
<div align=right>
<input type=password maxlength=16 name=newpassword>
</div>
</td>
</tr>
<tr>
<td>
Retype New Password
</td>
<td>
<div align=right>
<input type=password maxlength=16 name=newpassword2>
</div>
</td>
</tr>
</table>
<div align=center>


<input type=hidden name=action value='changepassword'>
<input type=submit name=submit value=' Submit '>
</div>
</form>
";
}

if($error==2) {

$newpassword = encrypt($newpassword);
mssql_query(sprintf(UPDATE_PASSWORD, $newpassword, $account));

echo '
<div align=center>
Password has been successfully changed.


You will be redirected to the login page in 3 seconds or you can click here (logout.php).
</div>
';
delayedrefresh('logout.php');

}

echopage('footer', '');


?>
[/php:1:49134192da]
~~~~~~~~~
credit: L2CP - Re-worked by FORT staff
[hr:49134192da]
Anti injection (put in functions)
[hr:49134192da]
[php:1:49134192da]
<?php
function antiinjection($str) {
$banwords = array ("'", ",", ";", "--", " or ", ")", "(", " OR ", " and ", " AND ","/n","/r");
if ( eregi ( "[a-zA-Z0-9]+", $str ) ) {
$str = str_replace ( $banwords, '', strtolower ( $str ) );
} else {
$str = NULL;
}
[/php:1:49134192da]
[hr:49134192da]
An if it possible decrese the input lenght to 11 chars (no need to change this on sql database)

Kane Hart
17.05.2005, 15:15
anyone have old copy cp cuz I need the account maker off it..

PhDoc
18.05.2005, 03:20
This:
[php:1:c7308d8577]$oldpassword = antiinjection($oldpassword); [/php:1:c7308d8577]
and
[php:1:c7308d8577]$str = str_replace ( $banwords, '', strtolower ( $str ) );[/php:1:c7308d8577]
is wrong. Ex. PaSsWoRd...

Riane
18.05.2005, 12:38
umm @ evercool

umm yah ive no idea yet how to make stuff more secure than that lol
would like to see your 'better' release so i can learn some stuff

and for the unencrypted passwords, i have no clue how they are saved in the DB, if they are saved like they're typed, this should work (UNencrypted)

BiGBoY
18.05.2005, 13:51
there is no way to add non encripted pass to db.
---------
Kane u need L2CP?

Riane
18.05.2005, 15:39
there is a l2auth with unencrypted password, but never tryed it, so how should i know?