<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Samson Endale on Medium]]></title>
        <description><![CDATA[Stories by Samson Endale on Medium]]></description>
        <link>https://medium.com/@SamAsEnd?source=rss-7a1efb7582a5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*17NFUCuoJa3FkmZZmh7ysQ.jpeg</url>
            <title>Stories by Samson Endale on Medium</title>
            <link>https://medium.com/@SamAsEnd?source=rss-7a1efb7582a5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 01:08:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@SamAsEnd/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to store a password]]></title>
            <link>https://medium.com/@SamAsEnd/how-to-store-a-password-62c032439b6b?source=rss-7a1efb7582a5------2</link>
            <guid isPermaLink="false">https://medium.com/p/62c032439b6b</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[bcrypt]]></category>
            <category><![CDATA[cryptography]]></category>
            <dc:creator><![CDATA[Samson Endale]]></dc:creator>
            <pubDate>Tue, 12 Sep 2017 00:00:00 GMT</pubDate>
            <atom:updated>2023-01-29T18:59:31.559Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I was pen-testing some boring web apps and I noticed how people <strong>REALLY</strong> don’t know how to store passwords in a database. I couldn’t judge them because no one thought them how they should store passwords securely in a database.</p><p>For the average Joe storing a password in a database is the same as storing email and other stuff … <strong>AS PLAIN TEXT</strong>. That’s really bad, I mean <strong>REALLY REALLY REALLY</strong> horrible. This is the worst thing you can do when you store a user&#39;s password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/0*m-te_7JPDSSIMgI_.jpg" /></figure><p>So I thought I should write something about it. I don’t wanna make my blog a tutorial site where you can copy and paste code but I thought I should at least give them a highlight.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/0*UvW7-KO7VNOYD35v.png" /></figure><p>More pawned sites at <a href="https://haveibeenpwned.com/PwnedWebsites">https://haveibeenpwned.com/PwnedWebsites</a></p><p>Imagine if someone used the same password in their PayPal account. In a matter of fact, You don’t need to imagine, as <a href="https://nakedsecurity.sophos.com/2013/04/23/users-same-password-most-websites/">research by Sophos in 2013</a> found out 55% of net users use the same password for most, if not all, websites.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*94w1L8_wkXM7aeXy.jpg" /></figure><p>Plus my email was found in three data breaches. If I have been using the same password as the same as my Facebook, Google or GitHub accounts it’s a disaster, right?</p><blockquote><em>In a side note, check out if u have been pawned by clicking </em><a href="https://haveibeenpwned.com/"><em>here</em></a><em>.</em></blockquote><p>Now I think you understood why you should store a user&#39;s password securely. Not doing so is letting your client and customer down with you when you got hacked.</p><h3>How to store a password in a database?</h3><p>I will first list how you should <strong>NOT</strong> store passwords then I will show you the right way to store a password.</p><h3>1. As a plain text</h3><blockquote><em>Storing plain text passwords in the database is a sin. </em><a href="http://www.geeksforgeeks.org/store-password-database/"><em>*</em></a></blockquote><p>A password is not some other field you just pass to the database. It’s the most confidential non-financial property I could think of. So please don’t store our password in plain text for God&#39;s sake.</p><p><strong><em>PLEASE NEVER DO THIS AGAIN</em></strong>.</p><h3>2. As Base64 encoded</h3><p>You might be surprised but believe me or not, one of the sites I was testing was using base64 as password storage.</p><p>This is just plain stupid. Base64 encoding is an <strong>ENCODING</strong>, with no security at all. Even Caesar cipher is better than base64 encoding. Your adversary can just decode it and boom … a password in a clear.</p><h3>3. Encrypt them with a single key</h3><p>A lot of people do this (<a href="https://nakedsecurity.sophos.com/2013/11/04/anatomy-of-a-password-disaster-adobes-giant-sized-cryptographic-blunder/">Including Adobe</a>), they have a <strong>“SUPER SECRET”</strong> key in the backend to encrypt every password in the database.</p><p>Well, this is definitely better than the previous one. Even if an adversary compromises your database he/she will get only the cipher of the password and they don’t know the key.</p><p>What’s wrong with it?</p><h3>First: The password is still recoverable</h3><p>If the attacker finds a vulnerability in the system and learns the key he can just decipher them and all the passwords are now clear.</p><h3>Second: It’s easy to brute force</h3><p>The attacker only needs to try one key for every user. Let me rephrase it, The attacker only needs to go through the key space once to compromise every password. If they found a key for deciphering one password they can use it to decipher every password.</p><h3>Third: An adversary can have a rainbow table for every key you could possibly have as a password</h3><p>Plus they don’t need to encrypt every possible password. They can encrypt one chosen password with every possible key once and sign up for your service with that password and look up the cipher in the rainbow table to learn the key.</p><p>I can list more but this should be enough to convince you not to use it</p><h3>4. Encrypt it by itself as a key</h3><p>When I was at <a href="http://bdu.edu.et/">Bahir Dar University</a> defending a project of mine I was suggested by one of the examiners to encrypt the password by itself as a key to store as a password. I know it’s not secure but I didn’t judge her because that’s how she learned about hashing/one-way function.</p><p>The problem is when lecturers thought their students about hash functions they use this analogy. Let’s say I have a user&#39;s password in a registration form and I encrypt it with itself and store the cipher in the database. Now I can throw away the password. If a user login later I can encrypt the provided password with itself and if the cipher text matches whatever is stored in the database I will let them in.</p><p>Plus even if an attacker compromises the database or even the whole system he can’t decipher the ciphertext to get the password because the key for the decryption is the password itself and we don’t have that either. Sound secure?</p><p>In fact, this is again better than the previous one but let’s see what’s wrong with it.</p><h3>First: Similar passwords have similar cipher</h3><p>Let’s say Facebook was using this and 1 million out of the billion users use the same password ‘123456’. Now an attacker can brute force one cipher to learn a million users&#39; password</p><p>Or simply, if I saw someone else’s password cipher and it’s the same as mine. Mean we have the same password!</p><h3>Second: Rainbow table</h3><p>An adversary can have a rainbow table for every password just by going into the key space once just like the previous one. Every password has the same corresponding key.</p><p>We can also generate a rainbow table for the most used passwords which is economic.</p><h3>Third: Depending on the cipher you are using it can leak the length of the password.</h3><h3>5. Using cryptographic hashing functions</h3><p>If you don’t know what cryptographic hash functions are</p><blockquote><em>A cryptographic hash function is a special class of hash function that has certain properties which make it suitable for use in cryptography. It is a mathematical algorithm that maps data of arbitrary size to a bit string of a fixed size (a hash function) which is designed to also be a one-way function, that is, a function which is infeasible to invert. The only way to recreate the input data from an ideal cryptographic hash function’s output is to attempt a brute-force search of possible inputs to see if they produce a match, or use a rainbow table of matched hashes. Bruce Schneier has called one-way hash functions “the workhorses of modern cryptography”.</em></blockquote><p>This is way better than any of the above but still not secure enough to store passwords. Let’s see why.</p><h3>First: Google search / Rainbow table</h3><p>I better demonstrate this one. Google for 5d7845ac6ee7cfffafc5fe5f35cf666d.</p><p>Awesome right?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/311/0*232MNY5c137U3kdg.jpg" /></figure><p>Do you think md5 is the only one? Google for f2b14f68eb995facb3a1c35287b778d5bd785511 and fcf730b6d95236ecd3c9fc2d92d7b6b2bb061514961aec041d6c7a7192f592e4</p><p>and perhaps for 348735696e74c45e7fbf9c6839d87f891486d19e5059db7e397d5086e486dc0051a533752805dc9288463673f0a6fcbf2a655548738a85305b2d571bae44a71e too.</p><p>You are screwed if your adversary knows how to Google. lol</p><p>I will add more on this but let’s address this issue and we can talk about the rest later</p><h3>6. Using a cryptographic hash function with a salt</h3><p>If you don’t know what salt mean</p><blockquote><em>In cryptography, a salt is random data that is used as an additional input to a one-way function that “hashes” a password or passphrase. The primary function of salts is to defend against dictionary attacks or against its hashed equivalent, a pre-computed rainbow table attack.</em></blockquote><blockquote><em>A new salt is randomly generated for each password. In a typical setting, the salt and the password (or its version after Key stretching) are concatenated and processed with a cryptographic hash function, and the resulting output (but not the original password) is stored with the salt in a database. Hashing allows for later authentication without keeping and therefore risking the plaintext password in the event that the authentication data store is compromised.</em></blockquote><p>So lets Google for a hash e37ad635765263fc3efa63bf0142801a</p><p>No result was found, right? That’s because I added 16 bytes long random nonsense(AKA salt) before/after it.</p><p>Is it secure now?</p><p>Not really!</p><p>Let’s see what’s wrong with the cryptographic hash function(with salt) for password storage</p><h3>First: The cryptographic hash function is really really fast</h3><p>One of the specifications of a cryptographic hash function is to be fast but we don’t want it to be fast to be cracked.</p><p>Thanks to the down of cryptocurrency there are ASICs and GPUs which and can hash millions and billions of hash per second.</p><p>My PC can crunch 10 million hashes in a second. Imagine what the NSA could do.</p><h3>Second: Even with your salt in place a gigantic rainbow table is feasible for an adversary with great computational power.</h3><h3>Third: Length extension attack</h3><p>I will let you Google that too</p><p>Right now I hope you realized that none of the above is secure.</p><h3>Now let’s see how you SHOULD store your password.</h3><p>Believe it or not, this is the hardest part. I can just recommend how you should store a user&#39;s password but it might be not secure next year so it’s hard if people don’t find an updated state-of-the-art password storage technique.</p><p>I hope I will remove or recommend a better one if this information became irrelevant and misleading but please look for a recent blog post if you are reading this in the distant future.</p><p>As of today(Sept 12, 2017), the recommended way of storing passwords is by using</p><h3>Adaptive hash function</h3><blockquote><em>Adaptive one-way functions compute a one-way (irreversible) transform. Each function allows configuration of ‘work factor’. Underlying mechanisms used to achieve irreversibility and govern work factors (such as time, space, and parallelism) vary between functions and remain unimportant to this discussion.</em></blockquote><blockquote><em>As it turns out, just hashing a password using md5() or even sha512() isn’t good enough. Cryptographic hash functions are designed to be fast. This is good for cryptographic needs such as signing. But for password hashing, that’s a problem since it allows an attacker to brute force a lot of passwords very quickly. Adding a salt makes it resistant to rainbow tables, but not resistant to brute forcing where that salt is known.</em></blockquote><blockquote><em>By using either a stretched algorithm (Such as PBKDF2) or an algorithm designed to be slow (Such as bcrypt), a much better defense against brute forcing will be had.</em></blockquote><p>The four <a href="https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet">OWASP</a>-recommended adaptive hash functions are</p><ul><li><strong>Argon2</strong> is the winner of the <a href="https://password-hashing.net/">password hashing competition</a> and should be considered your first choice for new applications;</li><li><strong>PBKDF2</strong> when FIPS certification or enterprise support on many platforms is required;</li><li><strong>scrypt</strong> where resisting any/all hardware accelerated attacks is necessary but support isn’t.</li><li><strong>bcrypt</strong> where scrypt support is not available.</li></ul><p>That being said I usually use <strong>bcrypt</strong> because it’s already built into PHP as a default but that doesn’t mean you can’t use the others in PHP but they require you to install an extension.</p><p>And in the bright side, PHP has started built-in support for Argon2 in <a href="https://wiki.php.net/rfc/argon2_password_hash">PHP 7.2</a>.</p><p>Now I highlight how you can corporate bcrypt into your PHP project.</p><blockquote><em>Make sure you are using PHP &gt;= 5.5</em></blockquote><p>Now let’s say the user is registering and we have their password, we can bcrypt it as</p><pre>$hashed = password_hash($password, PASSWORD_BCRYPT);</pre><p>now go ahead and store the password.</p><ul><li>Make sure the password field is at least 100 characters long</li><li>No need to generate salt by yourself PHP got your back</li><li>There is a third parameter that specifies options but please leaves it as it is, it has a pretty good default</li></ul><p>When the user login we can authenticate them like this</p><pre>$stmt = $pdo-&gt;prepare(&#39;SELECT * FROM users WHERE email=:email LIMIT 1;&#39;);</pre><pre>$stmt-&gt;execute([&#39;:email&#39; =&gt; $email]);</pre><pre>if ($stmt-&gt;rowCount() &gt; 0 &amp;&amp; ($user = $stmt-&gt;fetchObject()) &amp;&amp;<br>    password_verify($_POST[&#39;password&#39;], $user-&gt;password)) {<br>    // login successed<br>} else {<br>    // login failed<br>}</pre><p>That’s how easy it is. But please don’t deal with passwords yourself, use a framework.</p><p>That’s all for today folks. Please give some feedback, it will help me in my future writing.</p><p>Happy new year and thanks for reading.</p><p><em>Originally published at </em><a href="http://sam.1gna.com/2017/09/12/how-to-store-a-password.html"><em>sam.1gna.com</em></a><em> on September 12, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62c032439b6b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Contributing to Laravel]]></title>
            <link>https://medium.com/@SamAsEnd/contributing-to-laravel-fd06cafff19f?source=rss-7a1efb7582a5------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd06cafff19f</guid>
            <category><![CDATA[contribution]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[open-source]]></category>
            <dc:creator><![CDATA[Samson Endale]]></dc:creator>
            <pubDate>Sun, 27 Aug 2017 00:00:00 GMT</pubDate>
            <atom:updated>2023-01-29T19:02:03.558Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of days ago I started to build a small web app for fun and it was a <a href="https://www.dropbox.com/"><em>dropbox</em></a> clone. It’s called <strong>Yet Another DropBox</strong> and as you guess, it’s an <a href="https://en.wikipedia.org/wiki/File_hosting_service">online file hosting and sharing service</a>. At the same time, I was also planning to try out <a href="https://laravel.com/docs/5.5/">Laravel 5.5</a> which is not been officially released yet. So I thought I could try out the cutting edge of what Laravel got to offer.</p><h3>What is DropBox?</h3><p><a href="https://www.dropbox.com/"><em>Dropbox</em></a> is a file hosting service that offers cloud storage, file synchronization, personal cloud, and client software.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/0*3h2fox2n4hgm5Ega.png" /></figure><h3>What about Laravel?</h3><p><a href="https://laravel.com/">Laravel</a> is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern. And by the way, Laravel is third on GitHub in the web application frameworks category next to meteor and rails.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/0*tuFqcRfPUIHhczgN.png" /></figure><h3>Then what?</h3><p>After setting up the necessary things for the web app I changed the namespace of my app to YetAnother using the app:name artisan command which is one of Laravel&#39;s awesome helper commands. The app:name command lets you set the application namespace from the default App namespace to the one you specify.</p><h3>How can working on your own project make you a laravel contributor?</h3><p>Yesterday I was generating a model for representing <strong>“file”</strong> and I saw -a an option to the make:model command and the description said</p><blockquote><em>-a, –all Generate a migration, factory, and resource controller for the model”</em></blockquote><p>I thought it was awesome because before 5.5 I use to pass -cmr to the make:model command to generate a resource controller and migration for a given model. I tried it out and it generated the model with migration and a resource controller but it also creates a file FileFactory.php in database/factories. I open it up and it was a model factory for the File model which we use to write in ModelFactory.php on 5.4. Then I saw a file called a UserFactory.php next to it and it was the model factory for the User model which use to be shipped in ModelFactory.php before 5.4.</p><p>Then I realize it was a new feature on laravel 5.5. We store all our model factories in one file which can be messy and get huge if you have a lot of models. Splitting it into multiple files was a cool thing and getting rid of ModelFactory.php was a smart move … I appreciated Taylor as always and continue.</p><h3>So what?</h3><p>But I notice that the application namespace is not updated on UserFactory, It was still saying App\User. So I executed the command Again as</p><pre>php artisan app:name YetAnother</pre><p>but it was still not changing. I know for sure this command uses to change the namespace in ModelFactory Laravel 5.4 but now it’s not working on laravel 5.5-dev so I start poking around why it doesn’t work.</p><p>First, I search for AppName and I found out that the app:name command resides on Illuminate\Foundation\Console\AppNameCommand class and I started skimming through the code. Since every command implements the handle method, I jump to the handle method and saw $this-&gt;setDatabaseFactoryNamespaces(); it looks convenient that the database factory is handled there so I jump to the setDatabaseFactoryNamespaces method.</p><p>On line 224 I found the method and I was shocked. it read as</p><pre>$this-&gt;replaceIn( $this-&gt;laravel-&gt;databasePath().&#39;/factories/ModelFactory.php&#39;, $this-&gt;currentRoot, $this-&gt;argument(&#39;name&#39;) );</pre><p>I thought why is it trying to change the namespace in ModelFactory. I asked Isn’t ModelFactory gone for good? Then tried the command a couple of times with a debugger and finally, I realized it was a bug.</p><p>Since my installation was a week old I said it might be fixed and I should update to get the latest commit. Therefore I update my composer dependencies and nothing changed. So I log in to GitHub and check it on the Laravel latest branch and I found out the bug was there too.</p><h3>How could this happen?</h3><p>I started digging when the regression happened and I found out it was 5 months ago when Taylor himself renamed the ModelFactory.php file to UserFactory.php with a commit message saying <a href="https://github.com/laravel/laravel/commit/67a8a1157004c4373663ec4a9398780feb6d6fa4">“rename ModelFactory to UserFactory to encourage separate files”</a>. But the app:name command was not updated accordingly of the change. That poor little artisan command was still looking for ModelFactory.php a file.</p><p>Finally, I fixed the bug gladly. I searched all the files inside the database/factories and update the namespace on all of them iteratively.</p><p>After that, I send a <a href="https://github.com/laravel/framework/pull/20766">pull request</a> to the laravel/framework repo which got merged after a couple of hours.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/0*ef7ysVjfC-Zr9Z0c.png" /></figure><h3>Got some advice?</h3><p>I want to give some pointers for those of you who want to contribute to big open-source projects</p><ol><li>First, read the contribution guideline before doing anything</li><li>Try to find some useful improvements or bugs</li><li>Don’t be afraid to send a pull request, they won’t bite</li><li>Don’t get offended if they didn’t accept your pull request</li><li>Most importantly be nice</li></ol><p>Thanks for reading</p><p>PS. give some feedback</p><p><em>Originally published at </em><a href="http://sam.1gna.com/2017/08/27/contributing-to-laravel.html"><em>sam.1gna.com</em></a><em> on August 27, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd06cafff19f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>