{"id":188,"date":"2020-05-23T08:36:10","date_gmt":"2020-05-23T08:36:10","guid":{"rendered":"https:\/\/shreyapohekar.com\/blogs\/?p=188"},"modified":"2022-02-09T18:54:08","modified_gmt":"2022-02-09T18:54:08","slug":"privilege-escalation-with-jwt","status":"publish","type":"post","link":"https:\/\/shreyapohekar.com\/blogs\/privilege-escalation-with-jwt\/","title":{"rendered":"How to do Privilege Escalation with JWT"},"content":{"rendered":"\n<p>Hello everyone!! In the previous <a href=\"https:\/\/shreyapohekar.com\/blogs\/the-concepts-of-jwt\/\">[post]<\/a>, we discussed about what exactly are JSON Web Tokens, its components and how it works.<\/p>\n\n\n\n<p>In this blog post, I am gonna talk about how keeping weak secret keys  could lead to privileged access rights.<\/p>\n\n\n\n<p>With all that said, Lets get started.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">RECAP<\/h4>\n\n\n\n<p>We learned in the previous <a href=\"https:\/\/shreyapohekar.com\/blogs\/the-concepts-of-jwt\/\">[post]<\/a> that JWT is of the form&nbsp;<\/p>\n\n\n\n<p>Header.payload.signature<\/p>\n\n\n\n<p>Where header and payload are base64 encoded and signature is calculated based upon the header,payload, a secret and the signing algorithm(in the header)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use a strong secret??<\/h2>\n\n\n\n<p>The funcion by which the signature is created determines the overall security of the token. A secret key(that is only known to the server)&nbsp; is an essential part of signing the signature. And thats the only parameter that is hidden from any normal user. Therefore, if the server uses a weak secret, an attacker can potentially crack that secret and recreate the jwt with the malformed payload to gain privileged access.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Let\u2019s consider a scenario!<\/h4>\n\n\n\n<p>There is a user john on an application with normal user privileges. The application also has the admin user who can perform all the administrative tasks. So, just by knowing the secret key, privileges of john can be elevated by re-signing the jwt with an obfuscated payload.&nbsp;<\/p>\n\n\n\n<p><strong>Lets walk in step by step.<\/strong><\/p>\n\n\n\n<p>Here is the original token components of John. As a part of payload, Its clearly visible that the key \u201cuser\u201d has the value<strong> john<\/strong>.&nbsp;<\/p>\n\n\n\n<p class=\"wpf-blue-background pad\">{ <br>\n  &#8220;alg&#8221;: &#8220;HS256&#8221;,<br>\n  &#8220;typ&#8221;: &#8220;JWT&#8221;<br>\n}<br>\n<br>\n{<br>\n&#8220;user&#8221;: &#8220;john&#8221;<br>\n}<br>\nHMACSHA256(<br>\n  base64UrlEncode(header) + &#8220;.&#8221; +<br>\n  base64UrlEncode(payload),<br>\n  password<br>\n)<br>\n<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">And the token generated is:<\/h5>\n\n\n\n<p>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiam9obiJ9.kkxgkYx4559EWetAD2phOxqgSe2B-nTSRXqEmoP0nXY<\/p>\n\n\n\n<p>The goal is to generate a token, where the key \u201cuser\u201d has value <strong>admin<\/strong>.<\/p>\n\n\n\n<p>&nbsp;Here, I have used secret as password which is easily crackable. The secret key of jwt can be cracked using <strong>hashcat<\/strong>(as it has the module available)&nbsp;<\/p>\n\n\n\n<p>Google for hashcat example hashes, and search for jwt.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/W-WV62CDL4mYWyVrPndN32Ch3rldWxFkUsDzIGKH6hqynSzGZs6tbwFG0dsMT5c4kGsfkK6hCBK0iMQiqZBAZEuI7KRCBfPDthTc9ZsREjLPbXdxG54llpx7DS_jUCZJ3tau6Jji\" alt=\"\"\/><\/figure>\n\n\n\n<p>&nbsp;There is only one mode available for jwt i.e.<strong>16500<\/strong>.&nbsp;<\/p>\n\n\n\n<p>So let&#8217;s save the above-generated jwt to a file and name it jwt.hash. Rockyou.txt is a huge dictionary of common passwords and can be used as a wordlist to crack passwords.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted wpf-blue-background\"># hashcat -m 16500 jwt.hash \/usr\/share\/wordlists\/rockyou.txt --force&nbsp;&nbsp;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/s9Gs6s4mph2-Kdr4EWw5j7WfohpEM9vA8fpBPGPbZ3sCdr3ZIryBLAlxG2D9qyidhgFLBNezhhbAKVF4Nqmy0c0cUhRXuZsSc3TSjChyvI1_lVu8J1vdVoqA-lXqWinmXm7-uKEf\" alt=\"\"\/><\/figure>\n\n\n\n<p>Within no time, the password got cracked.&nbsp;<\/p>\n\n\n\n<p>So now that we know the secret, we can re-create the jwt, with user: admin<\/p>\n\n\n\n<p class=\"wpf-blue-background pad\">{<br> &#8220;alg&#8221;: &#8220;HS256&#8221;,<br>   &#8220;typ&#8221;: &#8220;JWT&#8221;<br> }<br> <br> {<br> &#8220;user&#8221;: &#8220;admin&#8221;<br> }<br> HMACSHA256(<br>   base64UrlEncode(header) + &#8220;.&#8221; +<br>   base64UrlEncode(payload),<br>   password<br> )<br> <\/p>\n\n\n\n<p>And the newly created token is:<\/p>\n\n\n\n<p>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYWRtaW4ifQ.is8m3EBkfw5RqrUtrXG5mwTjjAOHhPMuxnjbpD9Fugk<\/p>\n\n\n\n<p>Now this token can be used as a part of authorization header to gain access to application with admin privilege.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/V1W_uiYHBS1DsOco7MUh5zfwVgDk5mPdtUydP5ETjd3v2m3KpBZC27zskr9zyh7HS_LJsS92GNJUjvyQGtneOoS4WbT9YwCtOL-rpokF5b6iQrN2a4yx59FjZCHq6P6yfO55BNYs\" alt=\"\"\/><\/figure>\n\n\n\n<p>Remediations??<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Use a strong secret while signing the signature.<\/li><li>&nbsp;The JWT can be encrypted, so that any normal user cant even view or transform the contents of the payload.<\/li><\/ol>\n\n\n\n<p>However, it was a primitive use-case to exploit still it should not be overlooked. <br>Hope you enjoyed reading the post. I\u2019ll be back with few more attack scenarios on JWT.<\/p>\n\n\n\n<p>Until then, Happy Hunting!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using a weak password can always be problematic. Lets see how selecting a weak secret key can lead to privilege escalation in JWTs<\/p>\n","protected":false},"author":1,"featured_media":182,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[2,94],"tags":[92,91,90,79,89,87,93,88,82],"class_list":["post-188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-information-security","category-jwt","tag-encryption","tag-jwe","tag-jws","tag-jwt","tag-jwt-to","tag-priviege-escalation","tag-remediations","tag-secret","tag-signature","entry","has-media"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/posts\/188"}],"collection":[{"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":9,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":928,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/posts\/188\/revisions\/928"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/media\/182"}],"wp:attachment":[{"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}