How to protect web application from cookie stealing attack?

☆樱花仙子☆ 提交于 2019-12-10 11:29:39

问题


My web application's authentication mechanism currently is quite simple.

When a user logs in, the website sends back a session cookie which is stored (using localStorage) on the user's browser.

However, this cookie can too easily be stolen and used to replay the session from another machine. I notice that other sites, like Gmail for example, have much stronger mechanisms in place to ensure that just copying a cookie won't allow you access to that session.

What are these mechanisms and are there ways for small companies or single developers to use them as well?


回答1:


We ran into a similar issue. How do you store client-side data securely?

We ended up going with HttpOnly cookie that contains a UUID and an additional copy of that UUID (stored in localStorage). Every request, the user has to send both the UUID and the cookie back to the server, and the server will verify that the UUID match. I think this is how OWASP's double submit cookie works.

Essentially, the attacker needs to access the cookie and localStorage.




回答2:


Here are a few ideas:

Always use https - and https only cookies.

Save the cookie in a storage system (nosql/cache system/db) and set it a TTL(expiry).

Never save the cookie as received into the storage but add salt and hash it before you save or check it just like you would with a password.

Always clean up expired sessions from the store.

Save issuing IP and IP2Location area. So you can check if the IP changes.

Exclusive session, one user one session.

Session collision detected (another ip) kick user and for next login request 2 way authentication, for instance send an SMS to a registered phone number so he can enter it in the login.

Under no circumstances load untrusted libraries. Better yet host all the libraries you use on your own server/cdn.

Check to not have injection vulnerabilities. Things like profiles or generally things that post back to the user what he entered in one way or another must be heavily sanitized, as they are a prime vector of compromise. Same goes for data sent to the server via anything: cookies,get,post,headers everything you may or may not use from the client must be sanitized.

Should I mention SQLInjections?

Double session either using a url session or storing an encrypted session id in the local store are nice and all but they ultimately are useless as both are accessible for a malicious code that is already included in your site like say a library loaded from a domain that that has been highjacked in one way or another(dns poison, complomised server, proxies, interceptors etc...). The effort is valiant but ultimately futile.

There are a few other options that further increase the difficulty of fetching and effectively using a session. For instance You could reissue session id's very frequently say reissue a session id if it is older then 1 minute even if you keep the user logged in he gets a new session id so a possible attacker has just 1 minute to do something with a highjacked session id.

Even if you apply all of these there is no guarantee that your session won't be highjacked one way or the other, you just make it incredibly hard to do so to the point of being impractical, but make no mistake making it 100% secure will be impossible.

There are loads of other security features you need to consider at server level like execution isolation, data isolation etc. This is a very large discussion. Security is not something you apply to a system it must be how the system is built from ground up!




回答3:


Make sure you're absolutely not vulnerable to XSS attacks. Everything below is useless if you are!

Apparently, you mix two things: LocalStorage and Cookies.

They are absolutely two different storage mechanisms:

  • Cookies are a string of data, that is sent with every single request sent to your server. Cookies are sent as HTTP headers and can be read using JavaScript if HttpOnly is not set.
  • LocalStorage, on the other hand, is a key/value storage mechanism that is offered by the browser. The data is stored there, locally on the browser, and it's not sent anywhere. The only way to access this is using JavaScript.

Now I will assume you use a token (maybe JWT?) to authenticate users.

If you store your token in LocalStorage, then just make sure when you send it along to your server, send it as an HTTP header, and you'll be all done, you won't be vulnerable to anything virtually. This kind of storage/authentication technique is very good for Single-page applications (VueJS, ReactJS, etc.)

However, if you use cookies to store the token, then there comes the problem: while token can not be stolen by other websites, it can be used by them. This is called Cross-Site Request Forgery. (CSRF)

This kind of an attack basically works by adding something like:

<img src="https://yourdomain.com/account/delete">

When your browser loads their page, it'll attempt to load the image, and it'll send the authentication cookie along, too, and eventually, it'll delete the user's account.

Now there is an awesome CSRF prevention cheat sheet that lists possible ways to get around that kind of attacks.

One really good way is to use Synchronizer token method. It basically works by generating a token server-side, and then adding it as a hidden field to a form you're trying to secure. Then when the form is submitted, you simply verify that token before applying changes. This technique works well for websites that use templating engines with simple forms. (not AJAX)

The HttpOnly flag adds more security to cookies, too.




回答4:


You can use 2 Step Authentication via phone number or email. Steam is also a good example. Every time you log in from a new computer, either you'll have to mark it as a "Safe Computer" or verify using Phone Number/Email.



来源:https://stackoverflow.com/questions/49562828/how-to-protect-web-application-from-cookie-stealing-attack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!