开发者

Submitting passwords via ajax

开发者 https://www.devze.com 2023-03-01 08:20 出处:网络
given the following scenario: We have a html form for changing an account\'s password. It looks like this:

given the following scenario: We have a html form for changing an account's password. It looks like this:

CurrentPassword:   __________________
NewPassword:       __________________
NewPasswordAgain:  __________________

We want to send this request via an ajax call. If we send it and we leave our computer (without logging out and staying on the exact same page) someone could open the webkit inspector (or firebug) and see something like this:

http://cl.ly/3y213W1q0U2y2e251k0O

What would be your solution for making this more secure? Is it even possible using an ajax开发者_高级运维 call here or would it be better to use a "normal" html form which reloads the whole page after sending?


Using a "normal" html form has the same problem, as packet sniffing could reveal the same data in a POST or GET header just as easily.

The best solution I can think of is to encrypt the password user-side via javascript. You don't really have to worry about the "what if the user has javascript disabled?" case since, in that case, the AJAX request won't go through either. Obviously this may have ramifications regarding how you store the password, but it will allow you to continue to use AJAX requests for the password update.


The author is not interested in encrypted connections here. He may as well be doing that already. What he wants is to be able to hide the password (and username) from any one who has an access to the computer, and can open the inspector tools to view the networking that occurred on the page.

One of the simplest things you could do is to refresh the page in case the authentication succeeded.

Something that you should do is to refresh the page whenever the user pressed "log out". This should clear all previous network data.

The less good options are about encrypting, obfuscating and hashing the password prior to sending it.

Hashing the password on client-side is not ideal because this prevents the use of hashed passwords with keys on the server-side (think HMAC). HMAC'd passwords are the best, because the key is kept on the filesystem whereas the salt is kept on the database. Cracking the password hash requires a rather solid access to the system.

Obfuscating and encrypting the password can be reversed. If someone sees a login request on the Webkit Inspector, he might be very interested in spending the time to undress your defenses.

I highly recommend refreshing the page at some point to avoid the problem entirely. Other options do not seem as good.


Encrypt the password on transport and make sure the calls you are making are being done over SSL!


To make this secure without using SSL, hash the passwords on the client using SHA-2. While that will protect the password itself, it won't protect someone from sniffing the hashed password. So you can't simply authenticate with the hashed password, either.

One way to do this is to use a server-generated random salt when authenticating. To authenticate, the client requests salt from the server, then hashes the password once (in order to match the hashed version stored on the server), then hashes again using that salt that it received from the server, then finally authenticates using a second ajax query with the salted-hashed password.

The server will authenticate only if this matches its own stored hashed password, hashed with the same salt it previously provided the client.

This way, it is impossible for someone to authenticate using the simple hashed version of the password. Since each salt provided by the server is valid only once, it would be essentially impossible for someone to intercept it and authenticate. (They would have to intercept the salt request, and then try to authenticate before the legitimate client could, all the while spoofing their session).

This protects users' passwords without using SSL, prevents logging in using data intercepted while the legitimate user is authenticating, and is fairly easy to implement. Of course there is no substitute for SSL as far as protecting the actual data on your site, but for a lot of typical web sites where there's not really any sensitive information, you should be more concerned about preventing theft of your users' passwords since people use the same password so often. This addresses that problem.

Note that this also does nothing to prevent session hijacking, but you can minimize the risk and damage of this by doing things like including browsers-specific information with the users's session, and allowing only a single active session at once, and requiring re-authentication to change email address or password.


Depending on the level of security you need, you could use RSA and public-key cryptography to encrypt the password within the browser prior to sending the ajax request. On the server-side, you would decrypt the passwords and process them as normal.

Of course, you would also need to be careful to delete any variables used to hold the entered passwords, and I am sure there are other security holes in this, but encryption will at least offer you some large degree of protection against that sort of attack.

Here's one library I found with a quick search. (disclaimer: I have not tested this, but it looks pretty good)

Lastly, I would strongly recommend that you transmit all login information via SSL. This adds an extra layer of security on top of the whole browser session between the browser and your server.

0

精彩评论

暂无评论...
验证码 换一张
取 消