{"title":"funcptr - security","subtitle":"An engineer's technical notebook","link":[{"@attributes":{"rel":"self","type":"application\/atom+xml","href":"https:\/\/funcptr.net\/category\/security\/atom.xml"}},{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/"}}],"generator":"Zola","updated":"2018-12-24T06:19:52+00:00","id":"https:\/\/funcptr.net\/category\/security\/atom.xml","entry":[{"title":"Change one password","published":"2018-12-24T06:19:52+00:00","updated":"2018-12-24T06:19:52+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2018\/change-one-password\/"}},"id":"https:\/\/funcptr.net\/2018\/change-one-password\/","content":"<p>It is that time of year where security professionals the world over end up\ntalking with friends and family about security. It will be inevitable, almost\nas inevitable as someone wearing a stupid Christmas sweater they are a little\ntoo proud of.<\/p>\n<p>The standard advice we've been giving for years is pretty simple:<\/p>\n<ol>\n<li>Don't re-use your passwords across sites<\/li>\n<li>Use a password manager<\/li>\n<\/ol>\n<p>Anyone that has done technical support for anyone that isn't as familiar with\nIT knows well that as soon as you complicate something, you end up getting\ntwice the calls, even for things that are not your fault; \"Well, since you\nsetup that password thing my printer won't print\" ...<\/p>\n<p>It is fantastic advice, it is where we should all strive to be, we should all\nhave password managers and should never re-use passwords.<\/p>\n<p>However let's change one single password. Start small.<\/p>\n<p>There is likely to be a single account that is the root of trust for all other\naccounts. An email address, either at an ISP somewhere (and maybe this is the\nyear you get them to switch from that old <a rel=\"external\" href=\"https:\/\/www.earthlink.net\/\">Earthlink<\/a> email address?) or\nmore likely a free email provider.<\/p>\n<p>That's the account we want to target.<\/p>\n<p>If we can secure the root of trust, the email address that can be used for\npassword reset emails and for phishing we've already won a large battle.\nIndividual accounts may still be \"vulnerable\", but now we've closed one giant\nhole.<\/p>\n<p>After all, we all learn to walk before we learn how to run. This small step can\nset the tone for even more and better security later.<\/p>\n<p>Should we go further? Absolutely, identify the primary accounts that are high\nrisk, as an example:<\/p>\n<ol>\n<li><a rel=\"external\" href=\"https:\/\/www.facebook.com\">Facebook<\/a><\/li>\n<li><a rel=\"external\" href=\"https:\/\/www.icloud.com\">Apple iCloud<\/a><\/li>\n<li><a rel=\"external\" href=\"https:\/\/account.microsoft.com\/\">Microsoft account<\/a><\/li>\n<li><a rel=\"external\" href=\"https:\/\/twitter.com\">Twitter<\/a><\/li>\n<\/ol>\n<p>Facebook Login\/Twitter is used across many different websites, Apple's iCloud\nallows remote wipe of devices, and Microsoft Account is used for access to\nlocal machines and likely to OneDrive and other online accounts storing\npersonal documents and files.<\/p>\n<p>There are many more that I am missing, those can be next, but even the above\ntend to roll back up to a single email address.<\/p>\n<p>There is nothing new under the sun, and password re-use is well known and\nridiculed, even Randall Munroe of <a rel=\"external\" href=\"https:\/\/xkcd.com\/\">XKCD<\/a> fame published a comic about\n<a rel=\"external\" href=\"https:\/\/xkcd.com\/792\/\">password re-use<\/a> a long time ago, however there is one comic that comes to\nmind to help create better passwords:<\/p>\n<p><a rel=\"external\" href=\"https:\/\/xkcd.com\/936\/\">correct horse battery staple<\/a><\/p>\n<p>Pick four random words from the English language, create a funny sentence and\nyou are off to the races. Don't use <code>correct horse battery staple<\/code> as a\npassword, it's a terrible password now, but the idea behind generating such a\npassword is fantastic.<\/p>\n<p>Just changing one password can increase someones security posture just a little\nbit, and who knows, next year you'll have received less spam email that can be\ntraced back to their address book being siphoned off and then abused.<\/p>\n<p>For bonus points, have them sign up for <a rel=\"external\" href=\"https:\/\/haveibeenpwned.com\/\">';--have i been pwned?<\/a>, now each\ntime a new service is breached your friends or relatives will get a little bit\nof notice, and can get an idea for why different passwords are a necessity\nthese days, and maybe next year they will ask you to show them how to set up\nthat password manager so they can be even more secure!<\/p>\n<p>Happy Holidays, and good luck with your IT help desk duties this year,\nespecially getting that printer driver installed, because lets be honest, we'll\nget blamed for the broken printer in two months whether we touched it or not.<\/p>\n"},{"title":"Tamper proof session cookies and session storage","published":"2014-06-10T17:09:45+00:00","updated":"2014-06-10T17:09:45+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2014\/secure-cookies-for-secure-session-information\/"}},"id":"https:\/\/funcptr.net\/2014\/secure-cookies-for-secure-session-information\/","content":"<p>As a follow-up to my previous article regarding <a href=\"\/2013\/user-sessions-and-the-data-to-be-stored\/\">User sessions, what data\nshould be stored where?<\/a>, I wanted to discuss how to store the session, and\nhow to generate cookies that are tamper proof.<\/p>\n<h2 id=\"what-are-we-trying-to-accomplish\">What are we trying to accomplish?<\/h2>\n<p>Ultimately we want to be able to have X amount pieces of data that are tied to\na particular user. Unfortunately due the fact that HTTP is a stateless protocol\nwe have to use cookies. Cookies are small little pieces of data that are\ntransmitted from the server to the client (generally done once), and then upon\nthe user coming back to the website they are transmitted from the client to the\nserver. This allows us to uniquely track a single user across connections to\nour website.<\/p>\n<p>If the website allows a user to authenticate and the fact that they are\nauthenticated is stored in the session, we also want to make sure that we can\naggressively expire a session, if this is possible depends on our session\nstorage.<\/p>\n<h2 id=\"session-storage\">Session storage<\/h2>\n<p>There are a multitude of ways to store the session data, but it ultimately\nboils down to server-side or client-side. Server-side can be done in Cassandra,\nMemcache, Redis or even in a SQL database.<\/p>\n<h3 id=\"server-side-storage\">Server-side storage<\/h3>\n<p>The main one that has\nbeen used for years is to use server side storage. Storing a small file on the\nservers hard drive that contains the data, and the client is sent a cookie that\ncontains a unique identifier that is linked to the on-disk storage.<\/p>\n<p>For example:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>1 =&gt; \/tmp\/session_1<\/span><\/span>\n<span class=\"giallo-l\"><span>2 =&gt; \/tmp\/session_2<\/span><\/span>\n<span class=\"giallo-l\"><span>...<\/span><\/span>\n<span class=\"giallo-l\"><span>N =&gt; \/tmp\/session_N<\/span><\/span><\/code><\/pre><h4 id=\"easily-expire-sessions\">Easily expire sessions<\/h4>\n<p>The upside to server-side storage is that it is possible for us to very easily\nexpire a session, simply remove the associated file\/data that is stored and the\nusers session has now become invalid.<\/p>\n<h3 id=\"client-side-storage\">Client-side storage<\/h3>\n<p>The other method that has recently started being used more to make it easier to\nscale the server side is to store session data encoded in base64 in the cookie\nitself. In this case there is no unique session ID, and no data is stored\nserver side.<\/p>\n<h4 id=\"expiration-is-more-difficult\">Expiration is more difficult<\/h4>\n<p>The downside to using client-side storage is that there is no way, short of the\nexpiration on the cookie itself for the website to expire a session. There are\nwork-arounds, but they all require storing state server-side. A hybrid approach\nfor example is possible, store a unique ID along with the session data, and\nstore that unique ID server side, but none of the extra data. Remove the unique\nID server side and if we receive a session that contains a unique ID we don't\nrecognise, we simply clear the session.<\/p>\n<h2 id=\"expiration-why-do-i-care\">Expiration, why do I care?<\/h2>\n<p>Being able to easily expire a users sessions allows for extra security\nmeasures. For example in Google Mail it is possible to sign out all other\nlocations, this forces those other locations to re-authenticate before gaining\naccess to your account.<\/p>\n<p>This is a good security measure to have, so that if a users cookie is stolen,\nor their credentials are compromised upon changing their password all their\nsessions are invalidated and an attacker using an old cookie\/session ID can't\ncontinue to wreak havoc on the users account.<\/p>\n<h2 id=\"cookie-format\">Cookie format<\/h2>\n<p>If we are just storing a session ID, or the full session the cookie should be\nhardened so that it can not be tampered with by a client. Even if you are\nprotecting the cookie using SSL, we still don't want to allow a malicious user\nto modify the cookie to change the session ID or the session itself.<\/p>\n<h3 id=\"signing-your-cookie\">Signing your cookie<\/h3>\n<p>The single best way to make sure your cookie has not been tampered with is to\ncryptographically sign your cookie, and upon receiving the cookie from the\nclient verifying that the signature matches what you are expecting. This is\nespecially important if you are using client-side storage, because you don't\nwant someone to be able to change the user ID from 950 to 1 and suddenly\nimpersonate a different user.<\/p>\n<h4 id=\"use-an-hmac\">Use an HMAC<\/h4>\n<p>HMAC (Hash-based message authentication code) is an cryptographic construct\nthat uses a hashing algorithm (SHA-1, SHA-256, SHA-3) to create a MAC (message\nauthentication code) with a secret key. It is very easy given the secret key\nand the original data to create the MAC, but it is very difficult if not\nimpossible to take the original data, and MAC and get the secret key.<\/p>\n<p>This allows us to do the following:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>data = &quot;Hello World&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span>mac = HMAC(data, sha256, &quot;SEEKRIT&quot;)<\/span><\/span><\/code><\/pre>\n<p>Our <code>mac<\/code> would now be equal to:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>e655f98cb9b3c02f45576f7906d64b0b7f8731f25a5319c42ca666917aca45a4<\/span><\/span><\/code><\/pre>\n<p>If we now create our cookie as follows:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>cookie = mac + &quot; &quot; + data<\/span><\/span><\/code><\/pre>\n<p>It would look as follows:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>cookie = e655f98cb9b3c02f45576f7906d64b0b7f8731f25a5319c42ca666917aca45a4 Hello World<\/span><\/span><\/code><\/pre>\n<p>We can then send that to the client that requested the page. Once the client\nvisits the next page, their browser will send that same cookie back to use. If\nwe split the mac from the data, we can then do the following operation:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>cookie = e655f98cb9b3c02f45576f7906d64b0b7f8731f25a5319c42ca666917aca45a4 Hello World<\/span><\/span>\n<span class=\"giallo-l\"><span>data = &quot;Hello World&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span>mac = e655f98cb9b3c02f45576f7906d64b0b7f8731f25a5319c42ca666917aca45a4<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>mac_verify = HMAC(data, sha256, &quot;SEEKRIT&quot;)<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>mac_verify == mac<\/span><\/span><\/code><\/pre>\n<p>If and only if <code>mac_verify<\/code> and <code>mac<\/code> are the same can we be sure that the\ncookie has not been tampered with.<\/p>\n<p>This requires that the client is NEVER aware of what we are using as our secret\nkey. In the above exmaples that is \"SEEKRIT\". In your web application you will\nbe required to make this a configuration variable, and you will have to take\ncare not to commit that configuration variable to a git repository and upload\nit to github (for example).<\/p>\n<h4 id=\"do-not-use-a-bare-hash-algorithm\">Do not use a bare hash algorithm<\/h4>\n<p>Using a bare hash algorithm allows for <a rel=\"external\" href=\"http:\/\/en.wikipedia.org\/wiki\/Length_extension_attack\">length extension attacks<\/a> if used\nincorrectly, this would allow an attacker to concatenate extra data to the end\nof our existing data, modify the \"MAC\" and the server would accept it.<\/p>\n<p>This construct is thus very dangerous:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>data = &quot;Hello World&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span>key = &quot;SEEKRIT&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span>mac = SHA1(key + data)<\/span><\/span><\/code><\/pre>\n<p>The following construct is still not recommended, but is not nearly as\ndangerous:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>mac = SHA1(data + key)<\/span><\/span><\/code><\/pre>\n<p>Due to the key being last, this is not vulnerable to a length extension attack,\nhowever please don't do this, instead stick to using an HMAC instead.<\/p>\n<h2 id=\"encrypting-session-data\">Encrypting session data<\/h2>\n<p>When using client-side storage, it may be beneficial to encrypt the data to add\nan extra layer of security. Even if encrypting the data you need to continue\nusing a MAC.<\/p>\n<p>Using just encryption will not protect you against decrypting bad data because\nan attacker decided to provide invalid data. Signing the cookie data with a MAC\nmakes sure that the attacker is not able to mess with the ciphertext.<\/p>\n<h2 id=\"what-are-web-frameworks-languages-doing-by-default\">What are web frameworks\/languages doing by default?<\/h2>\n<p>I am most familiar with the <a rel=\"external\" href=\"http:\/\/www.pylonsproject.org\/projects\/pyramid\/about\">Pylons Project's Pyramid Web Framework<\/a>, the\ndefault session implementation that is provided by the project is named\n<code>SignedCookieSessionFactory<\/code>, as the name implies this uses a client-side\ncookie to store the session data, which is signed using a secret key that is\nprovided upon instantiation of the factory.<\/p>\n<p><a rel=\"external\" href=\"http:\/\/flask.pocoo.org\/docs\/quickstart\/#sessions\">Flask sessions<\/a> also uses a signed cookie for client-side session storage.<\/p>\n<p><a rel=\"external\" href=\"http:\/\/api.rubyonrails.org\/classes\/ActionDispatch\/Session\/CookieStore.html\">Ruby on Rails<\/a> uses a signed\/encrypted cookie for client-side session\nstorage by default.<\/p>\n<p><a rel=\"external\" href=\"http:\/\/php.net\">PHP<\/a> does not by default sign the session cookie, it does however use\nserver-side storage for session data by default. However extra security can be\nadded by installing <a rel=\"external\" href=\"http:\/\/www.hardened-php.net\/suhosin.127.html\">PHP SuHoSin<\/a> which adds session cookie\nencryption\/signing.<\/p>\n"},{"title":"User sessions, what data should be stored where?","published":"2013-08-25T13:05:00+00:00","updated":"2013-08-25T13:05:00+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2013\/user-sessions-and-the-data-to-be-stored\/"}},"id":"https:\/\/funcptr.net\/2013\/user-sessions-and-the-data-to-be-stored\/","content":"<p><em>This article has been updated. For the old version please check\n<a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20130826031727\/http:\/\/funcptr.net\/2013\/08\/25\/user-sessions,-what-data-should-be-stored-where-\/\">Archive.org<\/a><\/em><\/p>\n<p>A couple of days ago on <a rel=\"external\" href=\"http:\/\/reddit.com\/\">reddit.com<\/a>'s <a rel=\"external\" href=\"http:\/\/reddit.com\/r\/netsec\/\">\/r\/netsec<\/a> a poster by the name\nof <a rel=\"external\" href=\"http:\/\/danweber.blogspot.com\/\">Dan Weber<\/a> posted what he believed to be an attack on PHP sessions:\n<a rel=\"external\" href=\"http:\/\/danweber.blogspot.com\/2013\/08\/hacking-php-sessions-by-running-out-of.html\">Hacking PHP sessions by running out of memory<\/a> <a rel=\"external\" href=\"http:\/\/www.reddit.com\/r\/netsec\/comments\/1k7khy\/i_know_its_not_that_hard_but_i_think_i_found_a\/\">(reddit link)<\/a>. The way\nthe \"attack\" works is as follows:<\/p>\n<ol>\n<li>Create a new session<\/li>\n<li>Assign some new data to the session, in this case a username which is used\nto signify that the user is logged in.<\/li>\n<li>Check to verify that the user should be logged in<\/li>\n<li>If the user should not be logged in, destroy the session.<\/li>\n<\/ol>\n<p>The \"attack\" would be to run the <a rel=\"external\" href=\"http:\/\/php.net\/\">PHP<\/a> script out of memory on number 3, since\nonce something is set on the session it is immediately stored, so even if the\nuser is not supposed to be logged in, they are now logged in since their\nsession says they are.<\/p>\n<p>I wouldn't necessarily call this a PHP hack, this is really just bad practice\nin terms of programming, the logic should be reversed.<\/p>\n<ol>\n<li>Create a new session<\/li>\n<li>Verify the user should be logged in<\/li>\n<li>If so, set the session data<\/li>\n<li>If the user should not be logged in, don't set the session data<\/li>\n<\/ol>\n<p>That would solve the problem at hand, and now there is no way for the user to\ntrick the PHP script into believing she is logged in when that is not the case.<\/p>\n<p>However as the discussion went on on Reddit, it became even more clear that\nthere are no good resources on what you should store in the user session, and\nwhat you shouldn't store in the user session. Some of these things may seem\nlike common knowledge, but sadly this is something every single new person to\nprogramming has to learn on their own.<\/p>\n<h2 id=\"some-assumptions\">Some assumptions<\/h2>\n<p>Let's get this out of the way, this is in no way limited to PHP, but it is the\none I will be using as an example. This can all apply to Ruby (<a rel=\"external\" href=\"http:\/\/rubyonrails.org\">Ruby on\nRails<\/a>), Python (<a rel=\"external\" href=\"http:\/\/www.pylonsproject.org\">Pyramid<\/a>) or many other frameworks.<\/p>\n<p>The basic problem is that generally writing to the session is not an atomic\ntransaction based on the page accessed, so the assumption made in this article\nis that when you write to the session it is instantly committed, and there is\nno way to roll it back upon failure. If there was, our first example listed\nwouldn't be able to occur since upon running out of memory at step 3, the\nsession would have been rolled back and cleaned up.<\/p>\n<h2 id=\"what-should-you-store-in-the-users-session\">What should you store in the users session?<\/h2>\n<p>You should only really store anything in the session that if it were made\npublic it would do very little harm.<\/p>\n<p>Really the list of items to be stored in a session are as follows:<\/p>\n<ol>\n<li>The users unique id (The ID that allows you to retrieve the users\ninformation from storage)<\/li>\n<li>Temporary state (i.e. Flash messages)<\/li>\n<li>CSRF token<\/li>\n<\/ol>\n<p>More importantly, don't store permission bits, or group memberships, or\nanything that is used to allow\/deny access to particular resources. You want to\nstore just enough information that upon a user accessing your site you are able\nto retrieve the users information from storage, and based upon that information\nfrom storage you then make decisions such as permissions\/group memberships.<\/p>\n<h3 id=\"why-is-this-important\">Why is this important?<\/h3>\n<p>One of the things that Dan Weber brought up in the Reddit post was storing the\nusers permission level and group membership in the session. If your code is\nthen relying on the session to always contain the right permission level, then\nthere is no way to expire someones access to the data.<\/p>\n<ol>\n<li>A user logs in<\/li>\n<li>A user gets various permissions, and they are set in the session<\/li>\n<li>The user has been fired from his job, and an administrator removes the\nusers permissions<\/li>\n<li>Since the user is still logged in, the users permission bits are still\nset, and he continues to have access to parts of the site\/information he\nshouldn't have access to.<\/li>\n<\/ol>\n<p>If instead on every page visit we simply pull out the users unique id and\nverify the permissions upon access, as soon as the permissions are revoked by\nthe administrator the user no longer has access to the various resources.<\/p>\n<h3 id=\"temporary-state\">Temporary state<\/h3>\n<p>There has to be an easy way to remember something from page visit to page visit\nthat isn't considered detrimental if the information gets lost. One of those\nthings is flash messages. Flash messages are generally used to provide the user\nindication that something has changed, they are shown once and then never\nagain.<\/p>\n<p>Storing these as session data makes sense. If the flash message gets set,\ngreat, if it doesn't get set, it doesn't matter. Flash messages are simply a\nnotification tool, if the user misses them it isn't important.<\/p>\n<h2 id=\"what-you-shouldn-t-store-in-the-session\">What you shouldn't store in the session<\/h2>\n<p>Definitely don't store any kind of permission bits, groups a user is a part of\nor anything that would allow the user access that they normally would not be\nable to access.<\/p>\n<p>On each page access check what permissions the user has. While it may mean a\nlittle more heavy lifting server side it provides extra security, and the means\nto enforce changes in permissions instantly.<\/p>\n<h2 id=\"good-secure-programming-practices\">Good secure programming practices<\/h2>\n<p>Keep secure programming practices in mind at all times, always consider how the\ninformation you are storing\/processing is accessed\/viewed\/administered. More\nimportantly think about the access controls that are in place, and how one\ncould expire access to a particular resource without requiring a co-operative\nclient.<\/p>\n<p>The ordering of how variables are set, and when they are set are very\nimportant.  <code>$_SESSION['isadmin'] = True<\/code> at the top of a PHP script, and then\nremoving it by checking to see if the user is actually an administrator later\non in the script is a bad idea.<\/p>\n<p>Always order your code so that if a failure does occur there is no chance that\na critical section of your code is executed by accident, or that information is\nstored in a half-verified state. This is especially important for access\ncontrol.<\/p>\n"}]}