<?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 Dhanuka Perera on Medium]]></title>
        <description><![CDATA[Stories by Dhanuka Perera on Medium]]></description>
        <link>https://medium.com/@dhanukaperera?source=rss-7b73c8ef60bc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PO_Hf-MXeWp7qaSFs5SluQ.jpeg</url>
            <title>Stories by Dhanuka Perera on Medium</title>
            <link>https://medium.com/@dhanukaperera?source=rss-7b73c8ef60bc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 02:50:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhanukaperera/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[Why Rapid SaaS Launches Fail: A Case Study in Shipping Eleven Products — And Earning Almost Nothing]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://dhanukaperera.medium.com/why-rapid-saas-launches-fail-a-case-study-in-shipping-eleven-products-and-earning-almost-nothing-b134e70ac64d?source=rss-7b73c8ef60bc------2"><img src="https://cdn-images-1.medium.com/max/1024/1*ooQVkXMV8kWp60fKzMUo5A.png" width="1024"></a></p><p class="medium-feed-snippet">What happens when a founder launches eleven SaaS products in just six months&#x2014;only to see nearly all of them fail to gain traction, let&#x2026;</p><p class="medium-feed-link"><a href="https://dhanukaperera.medium.com/why-rapid-saas-launches-fail-a-case-study-in-shipping-eleven-products-and-earning-almost-nothing-b134e70ac64d?source=rss-7b73c8ef60bc------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://dhanukaperera.medium.com/why-rapid-saas-launches-fail-a-case-study-in-shipping-eleven-products-and-earning-almost-nothing-b134e70ac64d?source=rss-7b73c8ef60bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/b134e70ac64d</guid>
            <category><![CDATA[entrepreneurship]]></category>
            <category><![CDATA[startup-lessons]]></category>
            <category><![CDATA[case-study]]></category>
            <category><![CDATA[solopreneur]]></category>
            <category><![CDATA[saas]]></category>
            <dc:creator><![CDATA[Dhanuka Perera]]></dc:creator>
            <pubDate>Sat, 04 Oct 2025 12:44:41 GMT</pubDate>
            <atom:updated>2025-10-04T12:44:41.196Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Understanding OAuth 2.0 with an Example]]></title>
            <link>https://dhanukaperera.medium.com/understanding-oauth-2-0-with-an-example-e20cef11f549?source=rss-7b73c8ef60bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/e20cef11f549</guid>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[web-application-security]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[web-applications]]></category>
            <dc:creator><![CDATA[Dhanuka Perera]]></dc:creator>
            <pubDate>Tue, 09 Oct 2018 14:12:33 GMT</pubDate>
            <atom:updated>2018-10-27T01:45:38.786Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gro9rFBAfpInyNn0rMoA5Q.jpeg" /></figure><p>This post is related OAuth 2.0 (Open Authorization). You will be able to get a clear understanding about what is OAuth and how it works. After that we will be build a simple OAuth web application to use the GitHub login and retrieve uses profile information and repositories.</p><h3>What is OAuth?</h3><p>OAuth is a framework that use to give access to protected user resources to 3rd parties in a controlled manner. What is that mean? You may have experienced logging in to some website using your Google Login or Facebook or using some other login provider and when you logged in to your the web site some of your information are already display on the webpage, like your name, profile picture. That is where OAuth is coming in picture. Simply OAuth provide access to your protected resources in 3rd party websites. OAuth don’t expose the users credentials 3rd party logins when users login from the OAuth providers like Facebook, Google. So theses 3rd party websites doesn’t need consider or even don’t know about your login credentials. And also these 3rd party sites and applications can only access the data that you allow them to access. So its always users have the choice of controlling what information to be allow and what to not allow. OAuth is only handles the authorization. OAuth is more secure and fast. The latest version (according to 2018-Oct) of this Framework is OAuth 2.0.</p><p>As I mentioned OAuth is handle only the authorization, let’s try to understand what is it mean. There are two terms authorization and authentication.</p><p><strong>Authentication : </strong>Check the if the user is who he is.<br><strong>Authorization</strong> : The actions user is allowed to do when the user has been authenticated.</p><p>Some example of OAuth is accessing the contact list in Google, accessing the friends list in facebook, accessing the play list from sportfy.</p><h3>Main Roles in OAuth</h3><p>Following are the main roles of OAuth.</p><ol><li>Client</li><li>Resource Owner</li><li>Authorization Server</li><li>Resource Server</li></ol><p>Let’s understand the these roles with an example. Let’s see I developed a web application to display user private GitHub repositories. So in this case the application I developed is the <strong>client</strong>. Now I deploy this web application to some web server and give it to my friend Bob. In my application I want to access Bob’s repositories, which is Bob’s git resources. So the <strong>resource owner</strong> in this case is Bob, in other words the user. The <strong>Authorization server</strong> is what give the access token to my web application. And finally the <strong>Resource Server </strong>is what where the all the resources are stored, in this case information about user’s GitHub repositories.</p><p>I hope the above example will help you to understand the main roles of OAuth. Now let’s look at the flow of it and how it will work in deatils.</p><h3>Main Endpoits</h3><p>OAuth use several end points in it’s workflow process.</p><ol><li>Authorization End Point — Use to initialize the user authentication.</li><li>Redirection End Point — Location where the user should redirect in the client application after successfully authenticated.</li><li>Token End Point — Use by the client application to get the access token.</li><li>Resource End Point — Use by the client to application the retrieve the projected resources.</li></ol><h3>OAuth Tokens</h3><p>OAuth Specify two different types of tokens.</p><p><strong>Access Token</strong> — Access token use to get access to the protected resources. It is issued by the authorization server and has a limited life time.</p><p><strong>Refresh Token</strong> — Refresh token is used to obtain a new access token when the existing access token has expired.</p><h3>OAuth Grant Types</h3><p>There are four main Grant Types in OAuth. Grant types are how client will get the access token. Different grant types are used in different applications. Let’s see what are those grant types and their work flows.</p><h4>1. Authorization Code Grant Type</h4><p>This grant type used in most of the client server web applications that uses Java, .NET, PHP, Python.. as their backend systems.</p><p>1)Client Application request send a Authorization request to the Authorization Sever.</p><p>2)OAuth server request the permissions to access users resources.</p><p>3)If the user allowed and grant the permissions, OAuth server will provide a Authorization code and redirect the user to the client web application redirection end point.</p><p>4)The client application provide the Authorization code and get the access Token from the Token End Point.</p><p>5)The Access Token send back to the client.</p><p>6)Now the client application can use the Access Token to access the user’s protected resources by sending the access token to Resource End Point.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nM9754WjA1s22z5OvvtEkg.jpeg" /><figcaption>Authorization Code Grant Type Flow</figcaption></figure><h4>2. Implicit Grant Type</h4><p>This grant type used single page web applications. The flow is much similar to Authorization Code Grant Type but after the user grant the permissions, the Authorization code send with the request.</p><p>1)Client Application request send a Authorization request to the Authorization Sever.</p><p>2)OAuth server request the permissions to access users resources.</p><p>3)If the user allowed and grant the permissions, OAuth server will provide a Authorization Token and redirect the user to the client web application redirection end point.</p><p>4) Using the Authorization token client can access the user’s protected resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wtkk9SQNPbju4d-teOVbMw.jpeg" /><figcaption>Implicit Grant Type Flow</figcaption></figure><h4>3. Password Protect Grant Type</h4><p>This grant type used when developing trusted applications from the same vender. Examples can take as Facebook Mobile App by Facebook. Gmail Mobile App by Google.</p><p>1)Send the resource owner credentials to the client.</p><p>2)Client send the resource owner credentials + client credentials to the Authorization End Point.</p><p>3)Authorization Send the Access Token back to the client.</p><p>4)Client can use the Access Token to access the user’s protected resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q2dHnNPraYRoHIHhHGiTQA.jpeg" /><figcaption>Password Protect Grant Type Flow</figcaption></figure><h4>4. Client Credentials Grant Type</h4><p>This grant type used when the protected dose not belong to the user. This grant type used API based applications, like weather applications. User don’t have to provide any credentionals.</p><p>1)Client send the client credentials to the Authorization End Point</p><p>2)Authorization server respond with an access token to the client.</p><p>3)Client can provide the access token and access the protected resources from the Resource Server End point</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P9J8l-ZCY1T3fOXQdw2a-w.jpeg" /><figcaption>Client Credentials Grant Type Flow</figcaption></figure><h3>Basic OAuth Flow</h3><p>Now let’s understand the basic OAuth with an example. To make you understand this clear I will take the previous example that I used to explain the the roles of OAuth.</p><p>I am creating a the GitHub Profile viewer web application. First I want to register my my application with then GitHub. After that they will provide me a client Id and a secret. Client Id is like an username and secret is like a password, which we should not share with anyone. Now when a user going to going to log in to my application, my application will provide the client ID and secret and the GitHub Backend server will provide a token back to my client. Now using the provided token, client id and secret send a request to GitHub authorization server, and it will respond back with another token. This token is used to access the resource in the GitHub resource server. Now when the client provide the resource access token to GitHub resource server it will respond with the particular resource that was allowed, in this example the users GitHub repositories.</p><h3>Let’s Build OAuth Web Application</h3><p>Now you have the basic understand of what is OAuth and how it works. So aren’t you excited enough to build a your own OAuth Web application? Yes, let’s get started.</p><p>I am going to build a web application that for the scenario which I have explained above. What our application does is allow users to login with their GitHub account and the application will display user’s profile information and list of all the repositories they have including private ones.</p><p>The technologies we are using to build this web application is <a href="https://www.python.org/">Python</a> using the <a href="http://flask.pocoo.org/">Flask</a> Framework. We are using <a href="http://jinja.pocoo.org/">Jinja</a> as our template engine for python and <a href="https://bulma.io/">Bulma</a> as CSS framework. You can find the complete finished project in below GitHub link → <a href="https://github.com/dhanukaperera/OAuth-2.0-App">https://github.com/dhanukaperera/OAuth-2.0-App</a></p><p>First of all we need to register our application in GitHub. In order to do first login to your GitHub account and go to <strong>Settings</strong>. In the left side in the every bottom you will see button called ‘<strong>Developer Settings</strong>’. Click that button and you will see a similer view like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7rT_e1VmhjXc4QfCgaZ0EA.png" /></figure><p>Click <strong>Register a new application</strong> button. You will a view like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mIoMzBFRzBlIWzCZ72fhmw.png" /></figure><p>Fill the from. You can put any name to your application name. I run my app on localhost port 5000, so I put that as my home page URL <a href="http://localhost:5000.">http://localhost:5000.</a> The Authorization callback URL is where the user should redirected to after the authorization. In this application its <a href="http://localhost:5000/github_login/github/authorized">http://localhost:5000/github_login/github/authorized</a>. After filling the from click <strong>Register Application </strong>Button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J7-mbpIjsanypS11fU8VKA.png" /></figure><p>Form the above view you can view the <strong>Client ID</strong> and <strong>Client Secret</strong> that you need to use in the application. This both should not share with anyone can keep as a secret.</p><p><strong>The flow of the web application as follow:</strong></p><ol><li>Users are redirected to request their GitHub identity.</li><li>Users are redirected back to your site by GitHub.</li><li>Your app accesses the API with the user’s access token.</li></ol><p>Now let’s understand flow of each step.</p><h4><strong>1. Users are redirected to request their GitHub identity.</strong></h4><pre>GET https://github.com/login/oauth/authorize</pre><p>Following parameters are used.</p><p>client_id<strong><em> </em></strong><em>: </em>[<strong>Required</strong>]. we get this after registering the application.</p><p>redirect_uri: The url where users should redirect after the authorization.</p><p>scope: scope is which components which request the access of the user, in this example <strong>user</strong>, <strong>repos</strong> are scopes used in by GitHub. User can always decide what to scope to allow and give access to the client application.</p><p>state: A additional random string to protect cross site request forgery attacts.</p><p>allow_signup: This is allow new user to signup Github if they don’t have any account.</p><h4>2. Users are redirected back to our web application by GitHub</h4><pre>POST https://github.com/login/oauth/access_token</pre><p>If the user accepts the request, GitHub will redirects back the redirection url we have set with a code. Now we exchange this code with an access token to access the resources we need.</p><p>Following parameters are used.</p><p>client_id<strong><em> : </em></strong>Required. The client ID you received from GitHub for your GitHub App.</p><p>client_secret: Required. The client secret you received from GitHub for your GitHub App.</p><p>code: Required. The code you received as a response to Step 1.</p><p>redirect_url: The URL in your application where users are sent after authorization.</p><p>state: The unguessable random string you provided in Step 1.</p><p>The response will look like below.</p><pre>Accept: application/json <br>{&quot;access_token&quot;:&quot;e72e16c7e42f292c6912e7710c838347ae178b4a&quot;, &quot;scope&quot;:&quot;repo,gist&quot;, &quot;token_type&quot;:&quot;bearer&quot;}  <br>Accept: application/xml <br>&lt;OAuth&gt;   <br>&lt;token_type&gt;bearer&lt;/token_type&gt;  <br>&lt;scope&gt;repo,gist&lt;/scope&gt;   &lt;access_token&gt;e72e16c7e42f292c6912e7710c838347ae178b4a&lt;/access_toke&gt; <br>&lt;/OAuth&gt;</pre><h4>3. Use the access token to access the API</h4><p>Now we can use the access token to call the GitHub API and get the resources web want. We can pass the token in the query parameter but best practice is will be to include it in the Authorization header. Below curl command show how to get the user deatils.</p><pre>curl -H &quot;Authorization: token OAUTH-TOKEN&quot; <a href="https://api.github.com/user">https://api.github.com/user</a></pre><p>The following code shows the backend implementation of our client application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a221f65dc75c3edd742fd2c4f5d3ce93/href">https://medium.com/media/a221f65dc75c3edd742fd2c4f5d3ce93/href</a></iframe><p>The Login From looks below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6wyPaOU2TtZ3kUqmEi8KpQ.png" /><figcaption>Login.html</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2954beb73820cc2a107ca544deabdd72/href">https://medium.com/media/2954beb73820cc2a107ca544deabdd72/href</a></iframe><p>When user click the <strong>Login </strong>button user will redirect to GitHub login page to login.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zP2zH5vdJgVRqerCbn8SLg.png" /><figcaption>GitHub Login Page</figcaption></figure><p>After successfully logged in user will redirected to page where user need to allow persimmons to access that in the client app. Also it shows what kind of data is going to access in the web application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*niCnGo9l5Rm3ShUJOp9glQ.png" /><figcaption>User Consent Page</figcaption></figure><p>When user clicks the <strong>Authorize</strong> button and your profile information and repositry information will display in the web application as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jaarh7pvuRomcNl7ZKlG-w.png" /><figcaption>Display user information and repositories</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/855c261f9db07715c68f187448e30dc0/href">https://medium.com/media/855c261f9db07715c68f187448e30dc0/href</a></iframe><h3>Additional Reading</h3><ul><li><a href="https://tools.ietf.org/html/rfc6749">RFC 6749: The OAuth 2.0 Authorization Framework</a></li><li><a href="http://www.securityinternal.com/2017/04/retrieving-user-resources-from-facebook.html">Retrieving User Resources from Facebook over the OAuth 2.0 Authorization Code Grant Type</a></li><li><a href="http://www.securityinternal.com/2016/04/retrieving-user-profile-information.html">Retrieving User Profile Information from LinkedIn using OAuth 2.0 Authorization Code Grant Type</a></li><li><a href="https://auth0.com/blog/oauth2-the-complete-guide/">OAuth 2.0: The Complete Guide</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e20cef11f549" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Preventing CSRF Attacks with Double Submit Cookies Pattern]]></title>
            <link>https://dhanukaperera.medium.com/csrf-with-double-submit-cookies-pattern-fba1a7a99e27?source=rss-7b73c8ef60bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/fba1a7a99e27</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[csrf]]></category>
            <category><![CDATA[web-application-security]]></category>
            <category><![CDATA[information-security]]></category>
            <dc:creator><![CDATA[Dhanuka Perera]]></dc:creator>
            <pubDate>Thu, 04 Oct 2018 16:15:34 GMT</pubDate>
            <atom:updated>2018-10-26T23:53:53.866Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LmBD9OaRAJPnBYBoZwyZMw.jpeg" /></figure><p>This article is related to information security and web application security. In the article we are focusing on how to prevent Cross Site Request Forgery (CSRF) Attacks using Double Submit Cookie Pattern (DSCP). If you are not familiar with what is CSRF, please check this post I wrote previously.</p><p><a href="https://medium.com/@dhanukaperera/csrf-with-synchronizer-token-pattern-a4af534d1764">CSRF with Synchronizer Token Pattern</a></p><p>In this previous blog post you will find what is CSRF attacks and how to prevent CSRF Attacks using Synchronizer Token Pattern (STP). There is also demo I created how to secure web application using STP. Even you are familiar with CSRF I encourage you to check that my previous blog post. This blog post is like it’s Continuation.</p><p>I hope now you read my previous post and about CSRF and now let’s see how to prevent CSRF attacks using DSCP. I also make a demo web application how to demonstrate how to prevent these attack.</p><h3>How to prevent CSRF attacks using DSCP</h3><p>In DSCP we don’t store the CSRF token in the client as we did in STP. Instead of that we create a cookie with the token and send back to the client site. Hidden from field is set the extracted and set the value of the CSRF token. Now in our web from we have hidden field with the token and also a cookie also store the CSRF token. When the user submit a secure from data in the from + hidden value in the input field and the cookie stored the CSRF token submit to the server. In the server will extract both the cookie value and the hidden from filed value and compare. If both match then the action is execute else it will reject by the sever.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vYVjMCK30CwXyVN-6XhwtA.jpeg" /></figure><h3>About the Demo Web Application</h3><p>To demonstrate the scenario in the above, I have made a simple web application using Python using the Flask Framework. You can find the link to this project in this Github URL →<a href="https://github.com/dhanukaperera/CSRF-Double-Submit-Cookies-Pattern"> CSRF-Double-Submit-Cookies-Pattern</a></p><p>Please check the Readme file in the GitHub Repository to setup the development and run the project. The application contain the login page and password change page. User’s are need to be logged in to change the password. All the user login and backend functionalities are handled by <em>server.py</em> file. The application dosn’t use any datebase servies. Other than that project contains a <em>hack.html </em>file that use to trick the user.</p><p>The Login Page Looks like this. Use the <strong>Email</strong>: <em>admin@email.com</em> and <strong>Password</strong>: <em>admin123 </em>to log in to web application. Now there is no cookies created on the web application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HoFzdmgvNRHrg8XEgBZLNg.png" /><figcaption>Login Page</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ebc67cdfcfc73ea4adbfc4c839694cf/href">https://medium.com/media/9ebc67cdfcfc73ea4adbfc4c839694cf/href</a></iframe><p>After login to the web application we can see the there are two cookies created in by the application. One is session cookie and the other one is cookie store the CSRF token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RoB3ykkIHeQz_zjs8VO0Kg.png" /><figcaption>Update Email Page</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d79b3f29950ec50a49bd71dcc4c8e4f/href">https://medium.com/media/2d79b3f29950ec50a49bd71dcc4c8e4f/href</a></iframe><p>Now let’s update the user email address to ‘<strong>bob@email.com’</strong> and password to ‘<strong>new pass</strong>’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0qPHbLRE5UL8yQ0kOgl1zg.png" /></figure><p>Web application display a success message and update the user login. Let’s look the network request send by application. It have the email, password and the csrf fields. Take a closer look of the CSRF value ‘<strong>dtsvvngipwxpqnulmhog’ </strong>and the CSRF Cookie value. They both identical, that’s why server performed the action sucessfully.</p><p>Now let’s change value of the CSRF Cookie to “123” and try to update the user login details again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GcfqOHDcW3oMLfYmvVcuOA.png" /><figcaption>changing the csrf_token value to “123”</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oWgw8fuo0tFg43gat65XpQ.png" /><figcaption>Login Details Update Failed</figcaption></figure><p>Now the action failed. We couldn’t update the user. Take a close look at the csrf_token value send to the server. It send the preivously genarated token for that current users session, which was the same value in CSRF cookie. But the CSRF cookie value and when sever compare the request and cookie value it failed to validate the values, and reject the request. The CSRF attack is protected using the DSCP.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e8b657c76f83629d694631150d4fe1c/href">https://medium.com/media/0e8b657c76f83629d694631150d4fe1c/href</a></iframe><h3>Conclusion</h3><p>Now we learned about the CSRF attacks and how to prevent these attacks using DSCP. Main differences between STP and DSCP are STP store the CSRF token in the server and DSCP don’t store the CSRF token in server, instead of that, it sets the CSRF token as cookie. Both STP and DSCP embed the CSRF token in the hidden from field.</p><p>A good developer should consider about these attacks and make the web applications secure. In the user’s perspective, users should avoid visiting and clicking unwanted website and links.</p><p>GitHub Link for the Project →<a href="https://github.com/dhanukaperera/CSRF-Double-Submit-Cookies-Pattern"> CSRF-Double-Submit-Cookies-Pattern</a></p><p>Check post on Preventing CSRF attacks using STP → <a href="https://medium.com/@dhanukaperera/csrf-with-synchronizer-token-pattern-a4af534d1764">https://medium.com/@dhanukaperera/csrf-with-synchronizer-token-pattern-a4af534d1764</a></p><h3>Additional Reading</h3><ul><li><a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet">Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</a></li><li><a href="https://security.stackexchange.com/questions/61110/why-does-double-submit-cookies-require-a-separate-cookie">Why does Double Submit Cookies require a separate cookie?</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fba1a7a99e27" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Preventing CSRF Attacks with Synchronizer Token Pattern]]></title>
            <link>https://dhanukaperera.medium.com/csrf-with-synchronizer-token-pattern-a4af534d1764?source=rss-7b73c8ef60bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4af534d1764</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[csrf]]></category>
            <category><![CDATA[web-application-security]]></category>
            <category><![CDATA[information-security]]></category>
            <dc:creator><![CDATA[Dhanuka Perera]]></dc:creator>
            <pubDate>Thu, 04 Oct 2018 16:13:44 GMT</pubDate>
            <atom:updated>2018-10-26T23:55:22.068Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h84_OXDECybyzwsmdXX4dg.jpeg" /></figure><p>This article is related to information security and web application security. This article is structured as follow: First we a looking at what is CSRF is and understanding it through a simple example, after that let’s perform a CSRF attack using a demo application. After that let’s see how to prevent CSRF attacks and the secure the web application. There are several ways to prevent the CSRF attacks, in this article we are taking about prevent CSRF attacks using Synchronizer Token Patterns (STP). The objectives of this article as follows.</p><p><strong>1.) Give you clear understanding what is Cross Site Request Forgery.</strong></p><p><strong>2.) How Cross Site Request Forgery works.</strong></p><p><strong>3.) How to prevent CSRF attacks.</strong></p><h3>What is CSRF?</h3><p>CRSF stands for Cross Site Request Forgery. This is an attack where force the end users to execute an unwanted action where the end users are currently authenticated. Sounds confusing? Ok let’s try to understand this with a simple example and later demonstrate the scenario using a demo web application.</p><h3>Understanding CSRF with an Example</h3><p>Bob is user that use an CSRF Demo Web Application, and there is a hacker that want to get access to Bob’s account. So, the hacker plan to give a CSRF attack to Bob’s account. So, what does hacker need to know before give such an attack? The Hacker need to know an authenticated end point of the web site Bob uses. The website allows any user to create accounts, so the hacker can create an account and check the endpoints of the website. The hacker is interested in the password change endpoint in the web site. How hacker can know such an endpoint? It’s easy using the most modern web browsers. If we take the chrome, we can right click on the web page and go the inspect and clicking the ‘network’ tab and view all the network calls of that particular web site.</p><p>Now the hacker creates a sample web page that have the same POST request with the hidden input fields setting hacker’s details and send the link of the web page to Bob. Somehow in order to work the CSRF attack, Bob needed to log in and authenticated in the web site. Bob clicks the link of the hacker page and the password change request is made from hacker’s page and the change the Bob’s user name and password to hacker’s email and password. Now the hacker and log in to Bob’s account with the username and password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7abF2yZcJb4BD6Ue6cM8Yw.png" /></figure><h3>How to Prevent this using a STP</h3><p>In order to prevent CSRF attack using a STP, we need to generate Token when user is authenticated to the site. This token is store in the server site. In the client site this token is request using the current Session Id and store in the hidden input filed. When user try to update the login details the generated token also send with the request. In the server site compare the both tokens and perform the action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6xeY516kY4PVKxaQuzCEag.png" /></figure><h3>About the Demo Web Application</h3><p>To demonstrate the scenario in the above, I have made a simple web application using Python using the Flask Framework. You can find the link to this project in this Github URL → <a href="https://github.com/dhanukaperera/CSRF-Synchronizer-Token-Pattern">https://github.com/dhanukaperera/CSRF-Synchronizer-Token-Pattern</a></p><p>Please check the Readme file in the GitHub Repository to setup the development and run the project. The application contain the login page and password change page. User’s are need to be logged in to change the password. All the user login and backend functionalities are handled by <em>server.py</em> file. The application dosn’t use any datebase servies. Other than that project contains a <em>hack.html </em>file that use to trick the user.</p><p>The Login Page Looks like this. Use the <strong>Email</strong>: <em>admin@email.com</em> and <strong>Password</strong>: <em>admin123 </em>to log in to web application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DbvMvKzzQ7kGXP0IsCiLPw.png" /><figcaption>Login Page</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ebc67cdfcfc73ea4adbfc4c839694cf/href">https://medium.com/media/9ebc67cdfcfc73ea4adbfc4c839694cf/href</a></iframe><p>Ones the user logged in user will navigate to the password change page. Notice when the user logged in to application a cookie is created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*noqQU5eloD6EWkGx6GxqPw.png" /><figcaption>Update Email Page</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d79b3f29950ec50a49bd71dcc4c8e4f/href">https://medium.com/media/2d79b3f29950ec50a49bd71dcc4c8e4f/href</a></iframe><p>Now let’s update user’s Email as <em>dhanuka@email.com</em> and password as <em>123 </em>and see how the request goes through the network. We check the form data send on the network from the network tab in the inspect in Chrome. Notice we can see there is another filed called ‘<strong>csrf</strong>’ submitted with the form data. This is the random generated string for the session id of the user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-zguiJB7Q_08lKaizJu2Rg.png" /><figcaption>Successfully Update the login details</figcaption></figure><p>Now let’s look at the hacker page. The hack page have hidden input from fields to update the username and password of the user. Ones the user click on the button the request is send to the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u9VBr-18PdhjX4kLtdu69w.png" /><figcaption>Hacker Page</figcaption></figure><p>When user clicks on the ‘Get $100 Credit Free’ Button a POST request is send to the server but the hacker page don’t have the token of user. Due that when server compare the tokens, it don’t match. So the action is not sucessful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OBPPLMO49dC28K6JGC5QsQ.png" /><figcaption>CSRF token validation fail</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c74f6cf7d905b1309ef18d7721729691/href">https://medium.com/media/c74f6cf7d905b1309ef18d7721729691/href</a></iframe><h3>Conclusion</h3><p>Now we learned about the CSRF attacks and how to prevent these attacks using STP. There are more ways attacks can be performed in the web.To Learn how to protect the CSRF attacks using a double submit cookie pattern check out me next article.</p><p>Check the GitHub Project →<a href="https://github.com/dhanukaperera/CSRF-Synchronizer-Token-Pattern"> CSRF-Synchronizer-Token-Pattern</a></p><p>Check this → <a href="https://medium.com/p/fba1a7a99e27?source=your_stories_page---------------------------">CSRF with Double Submit Cookies Pattern</a></p><p>A good developer should consider about these attacks and make the web applications secure. In the user’s perspective, users should avoid visiting and clicking unwanted website and links.</p><h3>Additional Reading</h3><p><a href="https://www.incapsula.com/web-application-security/csrf-cross-site-request-forgery.html">https://www.incapsula.com/web-application-security/csrf-cross-site-request-forgery.html</a></p><p><a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=3&amp;ved=2ahUKEwjA8czi6IfeAhXGLI8KHUGYBAkQFjACegQICxAL&amp;url=https%3A%2F%2Fwww.owasp.org%2Findex.php%2FCross-Site_Request_Forgery_(CSRF)&amp;usg=AOvVaw27ZaRYljzQK4hEnUxhqvG0">Cross-Site Request Forgery (CSRF) OWASPhttps://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)</a>.</p><p><a href="https://prettyprinted.com/blog/1088267/understanding-csrf-attacks">https://prettyprinted.com/blog/1088267/understanding-csrf-attacks</a></p><p><a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet">https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet</a></p><p><a href="https://blog.codinghorror.com/preventing-csrf-and-xsrf-attacks/">https://blog.codinghorror.com/preventing-csrf-and-xsrf-attacks/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4af534d1764" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>