<?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 Vipul Jain on Medium]]></title>
        <description><![CDATA[Stories by Vipul Jain on Medium]]></description>
        <link>https://medium.com/@vipulparveenjain?source=rss-f18e96136dd4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Tbhd1yw99udrxK6T</url>
            <title>Stories by Vipul Jain on Medium</title>
            <link>https://medium.com/@vipulparveenjain?source=rss-f18e96136dd4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 07:51:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vipulparveenjain/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[CORS Implementation & Various Headers, CORS series (Part 2)]]></title>
            <link>https://medium.com/@vipulparveenjain/cors-implementation-various-headers-cors-series-part-2-895a04ef481d?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/895a04ef481d</guid>
            <category><![CDATA[bug-bounty]]></category>
            <category><![CDATA[cors-vulnerability]]></category>
            <category><![CDATA[cross-origin-resource]]></category>
            <category><![CDATA[web-security]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Wed, 04 Dec 2024 15:48:17 GMT</pubDate>
            <atom:updated>2024-12-04T15:48:17.260Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Access-Control-Allow-Origin?</h3><p>It’s an <strong>HTTP response header</strong> which is sent by the server to tell the browser whether it allows a specific origin to allow access its resources or not.</p><p>The purpose is to control cross-origin requests and securely allow only those which the server permits.</p><h3>CORS Implementation in detail</h3><p><strong>Let us assume a request is made from one origin to another (cross-origin)</strong>:<br><strong>Consider that your browser is on </strong><a href="https://example.com./"><strong>https://example.com</strong></a><strong>.</strong></p><ul><li>A JavaScript script on example.com tries to fetch a resource from <a href="https://api.anotherwebsite.com/data.">https://api.anotherwebsite.com/data</a>.</li><li><strong>The browser sends a request to the target server (</strong><strong>https://api.anotherwebsite.com)</strong>:</li><li>This includes an <strong>Origin header</strong>, which specifies the origin of the request (e.g., <a href="https://example.com)./">https://example.com).</a></li></ul><pre>GET /data HTTP/1.1<br>Host: api.anotherwebsite.com<br>Origin: https://example.com</pre><p><strong>The server responds with an </strong><strong>Access-Control-Allow-Origin header</strong>:</p><ul><li>If the server allows access to the origin (https://example.com), it includes the matching Access-Control-Allow-Origin: https://example.com header in its response.</li><li>If the origin isn’t allowed, the browser blocks the response due to the <strong>Same-Origin Policy (SOP)</strong>.</li></ul><pre>HTTP/1.1 200 OK<br>Content-Type: application/json<br>Access-Control-Allow-Origin: https://example.com</pre><p>Browser’s action:</p><ul><li>The browser compares the origin of the request (https://example.com) with the value of Access-Control-Allow-Origin in the server&#39;s response.</li><li>If they match, the browser allows the resource to be used by the requesting page.</li><li>If they don’t match (or the header is absent), the browser blocks the response for security reasons.</li></ul><h3>Special cases — CORS implementation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*NAalShRMJ0dUm3XH.jpeg" /></figure><h4>1. Wildcard (*)</h4><p>The server can use Access-Control-Allow-Origin: * to allow any origin to access the resource.</p><pre>Access-Control-Allow-Origin: *</pre><p>This is mainly used for public APIs or resources with no sensitive data.</p><p><strong>Limitations of wildcard:</strong></p><ul><li>It is allowed only for unauthenticated requests e.g., a public API.</li><li>BUT If the request includes <strong>credentials</strong> (e.g., cookies, HTTP authentication, or client-side SSL certificates) then in that case,<br>if the browser sees credentials: true in the request and Access-Control-Allow-Origin: *, it will block the response because exposing credentials to any origin is a significant security risk.</li><li>Further wildcards can not be used with some other value for e.g. <strong>Access-Control-Allow-Origin: https://*.normal-website.com</strong></li></ul><h4>2. Null Origin:</h4><p>A null origin occurs in specific scenarios where the request <strong>does not have a valid origin</strong>. Consider the example:</p><p><strong>Request Originating from </strong><strong>file:// URLs:</strong></p><ul><li>If we open an HTML file directly from our computer (e.g., file:///C:/index.html), it uses the file:// protocol.</li><li>Browsers consider file:// URLs as having a null origin because they aren’t related to a specific domain.</li></ul><h3>Handling cross-origin resource requests with credentials — <strong>Access-Control-Allow-Credentials</strong> header</h3><p><strong>The default behavior:<br></strong>The default behavior of cross-origin resource requests is for requests to be passed without credentials like cookies and the Authorization header.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*9DcT2TmEMoM0rf2KdWSz4Q.jpeg" /></figure><p>If the requested server uses <strong>Access-Control-Allow-Credentials</strong> header then the requesting site can send the request along with the credentials also. Now it is up-to the server to accept those credentials or not and if accepted the requesting site can access the response in that case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*JJLNJ5mBVotMJ1GB.gif" /></figure><p>Consider this scenario:</p><p>You have an active session on the main website which has the domain, <strong>https://main-website.com.</strong></p><p>This means that the browser has stored a cookie for this session e.g.<br><strong>JSESSIONID=&lt;your_session_cookie&gt;.</strong></p><p>Now you visit another website which has the domain <strong>https://malicious-website.com. </strong>Mistakenly you click a link on this website which sends a request to the main website (which has your session running) with java-script as:</p><pre>fetch(&quot;https://main-website.com/secure-data&quot;, {<br>  credentials: &quot;include&quot;,<br>});</pre><p>This “include credentials” directive tells the browser to include the stored credentials (in this case the session cookie) while sending the cross-origin request.</p><p>The browser eventually sends the following request:</p><pre>GET /secure-data HTTP/1.1<br>Host: main-website.com<br>Origin: https://malicious-website.com<br>Cookie: JSESSIONID=&lt;your_session_cookie&gt;</pre><p><strong>Now comes the role of the server and the CORS headers.</strong></p><p>If the server of the main website responds as:</p><pre>HTTP/1.1 200 OK<br>Access-Control-Allow-Origin: https://malicious-website.com<br>Access-Control-Allow-Credentials: true</pre><p>In this case the browser will check two things:<br>1) Whether the origin from where the request is initiated, is it allowed or not as per the <strong>Access-Control-Allow-Origin </strong>header value this means if this header contains same value as the <strong>Origin </strong>header or not.<br>2) The browser checks whether inclusion of credentials is allowed or not in the cross origin request.</p><p>If the value for both the headers is same in the first check and the inclusion of credentials in the cross-origin request is allowed then the malicious website will be able to access the response to its request using the credentials of the user.</p><p><strong>IMPACT:</strong></p><p>This will allow the malicious website to read sensitive data from the main website’s response. Consider if this main website is a banking site then in that case the malicious website has access to the user’s critical financial details.</p><p>BUT if the server responds as:</p><pre>HTTP/1.1 200 OK<br>Access-Control-Allow-Origin: https://malicious-website.com<br>Access-Control-Allow-Credentials: false</pre><p>Since the value of Access-Control-Allow-Credentials header is false thus even though that malicious origin is allowed, the credentials are not included and in this case the browser blocks access to the response even though the request itself was sent. Even if one of the conditions is not met, the browser will block access to the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/0*7ACd1zFm5kHsAjnO.jpg" /></figure><h3>OPTIONS Method — Pre-flight Checks</h3><p>In cases when the cross-origin request contains some:</p><ul><li><strong>Non-standard HTTP methods</strong> e.g., PUT, DELETE, PATCH.</li><li><strong>Custom request headers</strong> like Special-Request-Header.</li><li>The request uses Content-Type values other than the standard ones e.g. application/x-www-form-urlencoded, multipart/form-data, or text/plain.</li></ul><p>This indicates some sort of risky request, hence to handle this even before the actual request is sent, the browser sends a request with OPTIONS header to the target server. The aim is to ask the server for permission about various methods and headers that are being requested in the cross-origin request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*0v5d3caS5ftPRRHG.png" /></figure><p>Consider this example:</p><p>Suppose the cross-origin request is as shown:</p><pre>OPTIONS /data HTTP/1.1<br>Host: example.com<br>Origin: https://normal-website.com<br>Access-Control-Request-Method: PUT<br>Access-Control-Request-Headers: Special-Request-Header</pre><ul><li>&quot;OPTIONS” indicates that this is a pre-flight request and not the actual data request.</li><li>&quot;Origin” tells about the domain making the request.</li><li>&quot;Access-Control-Request-Method” tells the server which HTTP method is being asked for in the request e.g. in this case is PUT.</li><li>&quot;Access-Control-Request-Headers&quot; lists any custom headers that are being requested e.g. in this case is Special-Request-Header.</li></ul><p>Now consider the response of the server as:</p><pre>HTTP/1.1 204 No Content<br>Access-Control-Allow-Origin: https://normal-website.com<br>Access-Control-Allow-Methods: PUT, POST, OPTIONS<br>Access-Control-Allow-Headers: Special-Request-Header<br>Access-Control-Allow-Credentials: true<br>Access-Control-Max-Age: 240</pre><p>204 No Content means that is a pre-flight request and not the actual data request.</p><p>The response contains all the headers which gives information about which all origins, methods, headers are allowed and whether inclusion of credentials in the request is allowed or not.</p><p>Access-Control-Max-Age header explains about how long this response can be cached, in this case 240 seconds, this means subsequent requests within 240 seconds won’t trigger a new pre-flight check.</p><p>After this the browser either proceeds with the request if the server allows all the requested methods, headers and the origin but if any of the conditions fail then the request is blocked.</p><p>Therefore pre-flight checks helps the browser handle potentially risky requests or those with non standard methods and headers.</p><p>In the next blog we will discuss about exploiting CORS vulnerability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*Y_MnsqPTCsdNVpya.jpg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=895a04ef481d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to CORS-Cross-origin resource sharing & SOP-Same Origin Policy, CORS series (Part 1)]]></title>
            <link>https://medium.com/@vipulparveenjain/introduction-to-cors-cross-origin-resource-sharing-sop-same-origin-policy-cors-series-part-1-149ad6f77cf6?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/149ad6f77cf6</guid>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[bug-bounty]]></category>
            <category><![CDATA[web-security-testing]]></category>
            <category><![CDATA[cors-vulnerability]]></category>
            <category><![CDATA[cross-origin-resource]]></category>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Thu, 21 Nov 2024 16:50:54 GMT</pubDate>
            <atom:updated>2024-11-21T16:50:54.866Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Di2Zduw7LxrXNZoo.jpg" /></figure><h3>What is CORS</h3><p>CORS (Cross-Origin Resource Sharing) is a security mechanism used by web browsers to <strong>control and allow specific types of cross-origin requests</strong>. It helps bypass the restrictive Same-Origin Policy (SOP) but in a controlled manner.</p><p><strong>Example — Imagine a scenario where:</strong></p><ul><li>A web application hosted at https://example.com wants to fetch data from a REST API hosted at <a href="https://api.someotherdomain.com./">https://api.someotherdomain.com</a>.</li><li>Due to SOP, the browser will block this request because the two URLs have <strong>different origins.</strong></li></ul><p>But with CORS:</p><ul><li>The server at https://api.someotherdomain.com can include specific HTTP headers (like Access-Control-Allow-Origin) to tell the browser which domains are <strong>allowed to access its resources</strong>.</li><li>If the headers are present and configured to permit https://example.com, the browser allows the request to proceed.</li></ul><p>This means that in one place where SOP does not allow any interaction between different origin requests, CORS does allow that interaction but in a controlled manner.</p><h3>Same-origin Policy</h3><p>The same-origin policy is a restrictive cross-origin specification that completely limits the ability for a website to interact with resources outside of the same origin.</p><p>The same-origin policy restricts scripts on one origin from accessing data from another origin. An origin consists of a URI scheme, domain and port number. For example, consider the following URL:</p><pre>http://normal-website.com/example/example.html</pre><p>This uses the scheme http, the domain normal-website.com, and the port number 80.</p><p>The following image shows how the same-origin policy will be applied if content at the above URL tries to access other origins:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*TDumPZQI4by5kuHFXfrlQQ.png" /><figcaption>same-origin policy</figcaption></figure><p>Hence two URLs are considered to be of same origin if they have same value of these three factors:</p><ul><li>Scheme</li><li>Port number</li><li>Domain</li></ul><blockquote>This means if the origins of two resources match, JavaScript code on one resource can access and manipulate the other resource. However, if the origins differ, the SOP restricts access.</blockquote><h3>Why is the same-origin policy necessary?</h3><p><strong>Consider this scenario:</strong></p><p>Imagine you’re browsing the internet. You visit your favorite online shopping website, <em>example.com</em>. To log in, you enter your credentials, and the website sets a session cookie on your computer. This cookie contains information about your login session, such as your user ID and authentication token.</p><p>Now, let’s say you visit a malicious website, <em>malicious.com</em>. If the malicious website can trick you into clicking on a link or loading content from <em>example.com</em>, it can send an HTTP request to <em>example.com</em>.</p><p><strong>Without SOP:</strong></p><p>If the SOP didn’t exist, the browser would include the <em>example.com</em> session cookie in its request. When <em>example.com</em> receives the request, it would recognize the cookie and process it as if it came from your browser directly. This could allow the malicious website to:</p><ul><li><strong>Access your account:</strong> The malicious website could use the session cookie to log in to your account on <em>example.com</em> without your knowledge.</li><li><strong>Steal sensitive information:</strong> The malicious website could access your personal information, order history, or payment details.</li><li><strong>Perform unauthorized actions:</strong> The malicious website could place orders, change your password, or perform other actions on your behalf.</li></ul><p><strong>With SOP:</strong></p><p>However, due to the SOP, the browser would consider this request of different origin and hence would not include the <em>example.com</em> session cookie in its request. This is because the request is coming from a different origin (<em>malicious.com</em>). The browser will prevent the cookie from being sent, protecting your account from unauthorized access.</p><h3>How is the same-origin policy implemented?</h3><p>The SOP controls <strong>what JavaScript code is allowed to do</strong> when it tries to interact with content loaded from a different origin (e.g., a different domain, port, or protocol).</p><p>Although SOP restricts JavaScript from directly interacting with cross-origin content, <strong>loading</strong> some types of external resources (e.g., images, videos, or scripts) from another origin is allowed. However, <strong>reading</strong> the contents of those resources is blocked.</p><p>This means that although the content like video or an image will be loaded cross-domain and displayed but the JS code running on that page will not be able to directly interact with the embedded content eg- modifying the data or interacting with the metadata etc.</p><p>Therefore SOP allows embedding of images via the &lt;img&gt; tag, media via the &lt;video&gt; tag and JavaScript includes with the &lt;script&gt; tag but does not let the java script code to directly interact with the embedded data. So this means that JS will not be able to read the pixel data of the embedded image, it will not be able to access the metadata of the embedded video etc.</p><h3>Exceptions to SOP</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*N1UOqGQrc803B-mO.gif" /></figure><p>There are some exceptions to Same Origin Policy:</p><ul><li>Some objects are writable but not readable cross-domain, such as the location object or the location.href property from iframes or new windows.</li><li>Some objects are readable but not writable cross-domain, such as the length property of the window object (which stores the number of frames being used on the page) and the closed property.</li></ul><p>Further due to legacy requirements, the same-origin policy is more relaxed when dealing with cookies, so they are often accessible from all subdomains of a site even though each subdomain is technically a different origin. To mitigate the risk we can use the HttpOnly flag.</p><blockquote>The <strong>HttpOnly flag</strong> is a setting that can be applied to cookies to restrict access to them. When a cookie is marked with this flag:</blockquote><blockquote>It can only be sent with HTTP requests (like those made when navigating to a new page or making API calls).</blockquote><blockquote>It <strong>cannot be accessed or modified by JavaScript</strong> running in the browser.</blockquote><h3>How does HttpOnly mitigate risk</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_l2sCEo3neVpDZLb.png" /></figure><p><strong>Risk without HttpOnly:</strong></p><p>If cookies are accessible via JavaScript, malicious scripts (like those injected through cross-site scripting, or XSS) can steal cookies, including sensitive ones like session tokens.</p><p>Example of an attack:</p><ol><li>A website sets a cookie to manage the login session.</li><li>An attacker injects a malicious script into the webpage that reads document.cookie.</li><li>The attacker sends the session cookie to their server, gaining access to the account.</li></ol><p><strong>Benefit of HttpOnly:</strong></p><p>Marking a cookie as <strong>HttpOnly</strong> ensures that:</p><p>1. Even if an attacker successfully injects a script, it <strong>cannot read the cookie</strong> using document.cookie.</p><p>2. The cookie is only sent in HTTP headers during legitimate requests, reducing the risk of theft.</p><h3>Relaxing SOP norms</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/0*jZkaYR9opdiAzHQe.gif" /></figure><ul><li><strong>Limited interaction due to SOP:</strong></li></ul><p>By default, <strong>subdomains are treated as separate origins</strong>. For example:marketing.example.com and example.com have different origins due to their subdomain differences.</p><p>This means they cannot directly interact with each other under SOP.</p><ul><li><strong>Relaxing SOP using </strong><strong>document.domain</strong>:</li></ul><p>If we have control over both the domains (e.g., marketing.example.com and example.com), we can use the document.domain property of the document object in java script to make them <strong>appear as if they have the same origin</strong>. Both domains need to set document.domain = &quot;example.com&quot;; in their JavaScript code.</p><p>This makes their origin <strong>common</strong>, effectively bypassing the SOP restriction. Now, these domains can freely share content, cookies, and other resources as though they are part of the same origin.</p><blockquote>In the past it was possible to set document.domain to a TLD such as com, which allowed access between any domains on the same TLD, but now modern browsers prevent this.</blockquote><h4>What is FQDN (Fully Qualified Domain Name)</h4><p>It is complete address of the domain on the internet and specifies exact location of a resource in the hierarchy of the Domain Name System (DNS).</p><p><strong>Structure of an FQDN:</strong></p><p>An FQDN consists of several parts:</p><ol><li><strong>Subdomain (optional)</strong>:</li></ol><ul><li>This is a specific section of a website. For example, marketing or blog in marketing.example.com.</li></ul><p><strong>2. Domain name</strong>:</p><ul><li>The main identifier, such as example in example.com.</li></ul><p><strong>3. Top-Level Domain (TLD)</strong>:</p><ul><li>The suffix, such as .com, .org, or .net.</li></ul><p><strong>4. Root domain (hidden)</strong>:</p><ul><li>Represented as a dot (.) at the end of an FQDN, signifying the topmost level in DNS (e.g., example.com.).</li></ul><p><strong>Example of FQDN:</strong></p><p>For the domain marketing.example.com:</p><ul><li><strong>Subdomain</strong>: marketing</li><li><strong>Domain</strong>: example</li><li><strong>TLD</strong>: com</li><li><strong>FQDN</strong>: marketing.example.com. (though the trailing . is often implied and hidden in browsers).</li></ul><blockquote>Note that SOP can only be relaxed if the string in “document.domain” property of the document object in java script is a part of FQDN (fully qualified domain name) of both the origins. Here in this case “example.com” is part of both the FQDNs.</blockquote><p>In the next blog we will talk about CORS implementation and various headers related to CORS.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=149ad6f77cf6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Circumventing common SSRF defenses, SSRF series (Part 2b)]]></title>
            <link>https://medium.com/@vipulparveenjain/circumventing-common-ssrf-defenses-ssrf-series-part-2b-2b717f25364e?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b717f25364e</guid>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Tue, 17 Sep 2024 15:47:40 GMT</pubDate>
            <atom:updated>2024-09-17T15:47:40.333Z</atom:updated>
            <content:encoded><![CDATA[<h3>Bypassing SSRF filters via open redirection</h3><h4>Introduction</h4><ul><li>It is sometimes possible to bypass filter-based defenses by exploiting an open redirection vulnerability.</li><li>Open redirection vulnerability means that the user/attacker has control over the URL where the redirection takes place this means that the URL can be easily tampered with and sometimes made to access even unauthorized locations if the URL is not validated properly.</li><li>It also might be possible that the attacker redirects the user to their own phishing website, impersonating as a valid website and thus stealing the user’s critical information.</li></ul><h4>Security issue</h4><ul><li>In the previous example, imagine the user-submitted URL is strictly validated to prevent malicious exploitation of the SSRF behavior. However, the application whose URLs are allowed contains an open redirection vulnerability.</li><li>This depends on the API used to make the back-end HTTP request, if it supports redirections then in that case attacker can construct a URL that satisfies the filter and results in a redirected request to the desired back-end target.</li></ul><h4>Example</h4><p>The application contains an open redirection vulnerability in which the following URL:</p><pre>/product/nextProduct?currentProductId=6&amp;path=http://evil-user.net</pre><p>returns a redirection to:</p><pre>http://evil-user.net</pre><p>This can be leveraged, the open redirection vulnerability can be used to bypass the URL filter, and exploit the SSRF vulnerability as follows:</p><pre>POST /product/stock HTTP/1.0<br>Content-Type: application/x-www-form-urlencoded<br>Content-Length: 118<br><br>stockApi=http://weliketoshop.net/product/nextProduct?currentProductId=6&amp;path=http://192.168.0.68/admin</pre><p>This SSRF exploit works because the application first validates that the supplied stockAPI URL is on an allowed domain, which it is. The application then requests the supplied URL, which triggers the open redirection. It follows the redirection, and makes a request to the internal URL of the attacker&#39;s choosing.</p><h3>Lab: SSRF with filter bypass via open redirection vulnerability</h3><h4>Lab Description</h4><p>This lab has a stock check feature which fetches data from an internal system.</p><p>To solve the lab, change the stock check URL to access the admin interface at http://192.168.0.12:8080/admin and delete the user carlos.</p><p>The stock checker has been restricted to only access the local application, so you will need to find an open redirect affecting the application first.</p><h4>Lab Solution</h4><p>Click on any product and then access the check stock feature as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P53kfp4XxrF6vYK7" /></figure><p>Now check burp suite for the intercepted request and send it to repeater.</p><p>Now try accessing the admin functionality as done in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v58crV7cLo0VPPFt" /></figure><p>The response to the above request is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XD8KD5912kutM8_n" /></figure><p>This means the request to this internal resource is being blocked, hence we need to find an open redirection that can help us access this restricted resource.</p><p>On further analysis we get the next product button as shown below and clicking it captures the request for the same in burp suite.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v-LWrLrmbJyDFPWW" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*k0k3AeiW_8IU8gRy" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QI1rA0tjqP0pkdUA" /></figure><p>As shown in the screenshot above, the request for the next product contains the query parameter path which provides the potential of a possible open redirection.</p><p>Open redirection means a security flaw in a web application that allows an attacker to manipulate where the application redirects a user.</p><p>Hence we try redirecting this to “google.com” as demonstrated below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D_sSC70iEM1zDuXF" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pYwQ4fWvLcSwWQNH" /></figure><p>As can be seen, there is indeed an open redirection vulnerability that allows the attacker to control the redirection path.</p><p>Since the stockApi has access to the internal resources thus we can not redirect from this request itself but we copy the path of this request and then paste it in the stockApi parameter, further we append the internal path in the query parameter path. This is demonstrated in the screenshots below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L_TVB9Oc6JNU55yD" /><figcaption>copy the highlighted path</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cmM4KCujccilM7V4" /><figcaption>paste it in the stockApi parameter and also append the internal path in the path query parameter</figcaption></figure><p>Now the response for the above request is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BPaMsV2-8rgi3fu7" /></figure><p>Since the original value of the stockApi was URL encoded hence we encode this value also by selecting it and pressing ctrl+U.</p><p>The request is modified as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rnIbuV3THQ3BtK-r" /></figure><p>The response for the above request is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4f4GWBWsHbDgrlNQ" /></figure><p>This shows that we have successfully got access to the admin panel.</p><p>Now we look for the request that deletes user Carlos and modify the stockApi value accordingly as demonstrated in the screenshots below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*h_y5SeXP1VvQ0mp3" /><figcaption>modified request</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZxNQpxX8NJfCuAZY" /></figure><p>This finally solves the lab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9lBN8EdxbR2TnwSu" /></figure><blockquote>The logic behind the working of this attack is that, the stockApi parameter had access to the internal resources of the server but it did not allow direct access to them due to strict validation of the request. Thus we look for the request that has an open redirection vulnerability which means that the user has control over the URL where redirection takes place.</blockquote><blockquote>In this case the redirection was there in the “next product” request. We copy the path of this request and replace it with the value of stockApi since stock checking feature has access to internal resources and thus redirection to internal resources was not possible from the “next product” request itself.</blockquote><blockquote>Finally we replace the path parameter with the value of the path to the internal resource that is admin in this case. This finally gives us the access to the admin panel and the lab is further solved as per the steps explained above.</blockquote><h3>Blind SSRF vulnerabilities</h3><h4>What is blind SSRF?</h4><p>Blind SSRF vulnerabilities occur if you can cause an application to issue a back-end HTTP request to a supplied URL, but the response from the back-end request is not returned in the application’s front-end response.</p><h4>What is the impact of blind SSRF vulnerabilities?</h4><p>The impact of blind SSRF vulnerabilities is often lower than fully informed SSRF vulnerabilities because of their one-way nature. They cannot be trivially exploited to retrieve sensitive data from back-end systems, although in some situations they can be exploited to achieve full remote code execution.</p><h4>How to find and exploit blind SSRF vulnerabilities</h4><p>The most reliable way to detect blind SSRF vulnerabilities is using out-of-band (OAST) techniques. This involves attempting to trigger an HTTP request to an external system that you control, and monitoring for network interactions with that system.</p><h3>Finding hidden attack surface for SSRF vulnerabilities</h3><p><strong>1. Partial URLs in Requests:</strong></p><ul><li>The application might accept user input for URLs, but only a portion of the URL (like the hostname or part of the path) is stored in a request parameter. The server then constructs the complete URL by combining this user input with a pre-defined base URL.</li></ul><p><strong>2. URLs within Data Formats:</strong></p><ul><li>The application might accept and process data in formats like XML that allow embedding URLs. When the server parses this data, it might also unintentionally request the embedded URLs.</li></ul><p><strong>3. SSRF via the Referer Header:</strong></p><ul><li>The application might use analytics software that logs the “Referer” header in requests. This header indicates the webpage that linked to the current request. Some analytics software might even visit the URLs found in the Referer header.</li><li>Attackers can potentially craft a webpage containing a malicious URL and trick a user into visiting it. The Referer header would then contain the attacker’s URL, which might get unintentionally requested by the analytics software if it’ not properly validated.</li></ul><p>This finally marks the end of this series, stay tuned for more blogs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b717f25364e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Circumventing common SSRF defenses, SSRF series (Part 2a)]]></title>
            <link>https://medium.com/@vipulparveenjain/circumventing-common-ssrf-defenses-ssrf-series-part-2a-016614fc956a?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/016614fc956a</guid>
            <category><![CDATA[ssrf-attack]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[ssrf-walkthrough]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[payload-obfuscation]]></category>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Mon, 12 Aug 2024 10:33:42 GMT</pubDate>
            <atom:updated>2024-08-12T10:33:42.759Z</atom:updated>
            <content:encoded><![CDATA[<p>It is common for websites to have deployed several defenses against the SSRF attacks but in many cases these can be easily by-passed as explained further.</p><h3>SSRF with blacklist-based input filters</h3><p>Some applications block input containing hostnames like 127.0.0.1 and localhost, or sensitive URLs like /admin. In this situation, you can often circumvent the filter using the following techniques:</p><ul><li>Use an alternative IP representation of 127.0.0.1, such as 2130706433, 017700000001, or 127.1.</li><li>Obfuscate blocked strings using URL encoding and other encoding techniques.</li><li><strong>Case Variation:</strong> Trying variations of blacklisted keywords (e.g., “LoCaLhOsT”) might occasionally bypass case-sensitive filters.</li><li>Provide a URL that you control, which redirects to the target URL. Try using different redirect codes, as well as different protocols for the target URL. For example, switching from an http: to https: URL during the redirect has been shown to bypass some anti-SSRF filters.</li></ul><blockquote>Redirect codes are three-digit HTTP status codes used by web servers to inform the client (usually a web browser) that the requested resource has been moved to a different location.</blockquote><h3>Lab: SSRF with blacklist-based input filter</h3><h4>Lab Description</h4><p>This lab has a stock check feature which fetches data from an internal system.</p><p>To solve the lab, change the stock check URL to access the admin interface at http://localhost/admin and delete the user carlos.</p><p>The developer has deployed two weak anti-SSRF defenses that you will need to bypass.</p><h4>Lab Solution</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*c0mkomJp8nSARYxY" /></figure><p>Access the check stock feature by visiting any product and clicking the check stock button as shown in the screenshot below.</p><p>Now check burp suite for the request captured.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fuUPxlHeuoxcB_K3" /></figure><p>Send this request to burp suite repeater and replace the current value of stockApi with the back-end system URL as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TRcSUISOUFm4EIzC" /></figure><p>Now send the request and analyse the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1JoaGyI6ahbgl9gb" /></figure><p>As can be seen, the request has been blocked, this means there is some anti-SSRF defense mechanism which is blocking the request.</p><p>Now try URL encoding “/” and replacing it with its URL encoded counterpart and also try case variation in case of “localhost”. Finally send the request and analyse the response as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mjM754x5wjSOYoR5" /></figure><p>Now try to replace “localhost” with “127.0.0.1” and also try case variation in “admin” leaving “/” URL encoded only. Send the request and analyse the response as shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mbyb0aneQSYMBUua" /></figure><p>Now try to replace URL encoded “/” with “/” itself and also try some other case variation in “admin” and “localhost” as shown in the screenshot below. Finally send the request and analyse the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0vu2ngT8-0VZHtg1" /></figure><p>This means that the SSRF attack was successful and that we have got the access to admin panel.</p><p>Look for the path that will delete the user carlos as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I_XhhNW4VS52WFjA" /></figure><p>Modify the value of stockApi as shown in the screenshot below and send the request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oR2vzY5OKfr4HVAV" /></figure><p>This finally solves the lab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HdeRAnq58rDe9BJR" /></figure><p>The logic behind the lab was using an SSRF payload and by-passing the anti-SSRF security mechanism using payload obfuscation.</p><p>In the next part of this series I will explain in detail about by-passing SSRF filters via open redirection.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=016614fc956a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to server-side request forgery and common attacks, SSRF series (Part 1b)]]></title>
            <link>https://medium.com/@vipulparveenjain/introduction-to-server-side-request-forgery-and-common-attacks-ssrf-series-part-1b-0f40fd831845?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/0f40fd831845</guid>
            <category><![CDATA[ssrf-attack]]></category>
            <category><![CDATA[ssrf-walkthrough]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[owasp-top-10]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Thu, 08 Aug 2024 07:40:42 GMT</pubDate>
            <atom:updated>2024-08-08T07:40:42.259Z</atom:updated>
            <content:encoded><![CDATA[<h3>SSRF attack against other back-end systems, SSRF series (Part 1b)</h3><blockquote>In continuation of the last blog of this series, in this one I will explain about SSRF attacks against other back-end systems.</blockquote><h3>SSRF attacks against other back-end systems</h3><ul><li>In some cases, the application server is able to interact with back-end systems that are not directly reachable by users.</li><li>These systems often have non-routable private IP addresses. The back-end systems are normally protected by the network topology, so they often have a weaker security posture.</li><li>In many cases, internal back-end systems contain sensitive functionality that can be accessed without authentication by anyone who is able to interact with the systems.</li><li>In the previous example (<a href="https://medium.com/@vipulparveenjain/introduction-to-server-side-request-forgery-and-common-attacks-ssrf-series-part-1a-99b985310895">https://medium.com/@vipulparveenjain/introduction-to-server-side-request-forgery-and-common-attacks-ssrf-series-part-1a-99b985310895</a>), imagine there is an administrative interface at the back-end URL https://192.168.0.68/admin. An attacker can submit the following request to exploit the SSRF vulnerability, and access the administrative interface:</li></ul><pre>POST /product/stock HTTP/1.0 <br>Content-Type: application/x-www-form-urlencoded <br>Content-Length: 118 <br><br>stockApi=http://192.168.0.68/admin</pre><blockquote>Non-routable private IP addresses, also known as private IPs, are reserved address blocks that devices use for internal network communication. They cannot be directly accessed from the public internet.</blockquote><blockquote><strong>Purpose:</strong> Private IPs help create private networks within organizations or home environments. Devices on the same network can communicate with each other using these addresses, but they are not visible to the broader internet. This promotes network security and efficient address management.</blockquote><blockquote><strong>Security:</strong> Since private IPs are not directly routable from the internet, they offer an added layer of security for devices on a private network. However, other security measures like firewalls and proper network configuration are still crucial.</blockquote><p>Now consider this lab from Web Security Academy.</p><h3>Lab: Basic SSRF against another back-end system</h3><h4>Lab Description</h4><p>This lab has a stock check feature which fetches data from an internal system.</p><p>To solve the lab, use the stock check functionality to scan the internal 192.168.0.X range for an admin interface on port 8080, then use it to delete the user carlos.</p><h4>Lab Solution</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OF6DmMTxqfvXSTfJ" /></figure><p>Access the check stock feature by clicking any product and using the check stock functionality as shown in the above screenshot.</p><p>Now check burp suite for the requests captured.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L2RfoHjy31KCiVMe" /></figure><p>As shown in the screenshot above, the request selected contains the check stock feature.</p><p>Carefully note that as mentioned in the lab description, the address provided to us is not complete. To know the complete address where the admin interface is hosted, we will send this request to burp suite intruder and brute force for the correct address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*e7Pgg4orAjHHEyvF" /></figure><p>As shown in the above screenshot we replace the stockApi value with the URL to the IP given in the lab description which points to port 8080 to access the admin panel.</p><p>We replace the “X” with “1” and add the payload position over there as this will be replaced each time to check for the correct IP address.</p><p>In next step we set the payload as the numbers from 1 to 255 with step value as 1 as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AXCx18w4WjSZkEcw" /></figure><p>The results from burp intruder are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*StqZO9QPliHziLe-" /></figure><p>As can be seen in the results above, there is one request with status code 200 which means this is the successful request. The highlighted part is the correct IP address which hosts the admin panel over port 8080.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D8Q0N53bZNtRm8QV" /></figure><p>Now send this request to repeater.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1aNw5vxChVSDTAav" /></figure><p>Now in repeater, send the request and in the response look for the URL that will delete the user carlos.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aperi-6Zcn3X5oMm" /></figure><p>Update the current value of stockApi with this URL and send the request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-UsKT-lWH_1AMS9G" /></figure><p>This finally solves the lab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dNUv4y6RC9HFI6_F" /></figure><p>In the next part of this series I will explain in detail about some of the techniques to circumvent common SSRF defenses.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0f40fd831845" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to server-side request forgery and common attacks, SSRF series (Part 1a)]]></title>
            <link>https://medium.com/@vipulparveenjain/introduction-to-server-side-request-forgery-and-common-attacks-ssrf-series-part-1a-99b985310895?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/99b985310895</guid>
            <category><![CDATA[ssrf-walkthrough]]></category>
            <category><![CDATA[ssrf-attack]]></category>
            <category><![CDATA[owasp]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Mon, 05 Aug 2024 11:14:35 GMT</pubDate>
            <atom:updated>2024-08-05T11:18:32.672Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><ul><li>Server-side request forgery is a web security vulnerability that allows an attacker to cause the server-side application to make requests to an unintended location.</li><li>In a typical SSRF attack, the attacker might cause the server to make a connection to internal-only services within the organization’s infrastructure.</li><li>In other cases, the attacker may be able to force the server to connect to arbitrary external systems.</li><li>This could leak sensitive data, such as authorization credentials.</li></ul><h4>Impact of SSRF attacks</h4><ul><li>A successful SSRF attack can often result in unauthorized actions or access to data within the organization.</li><li>This can be in the vulnerable application, or on other back-end systems that the application can communicate with.</li><li>In some situations, the SSRF vulnerability might allow an attacker to perform arbitrary command execution.</li><li>An SSRF exploit that causes connections to external third-party systems might result in malicious onward attacks, these can appear to originate from the organization hosting the vulnerable application.</li></ul><h3>Common SSRF attacks</h3><ul><li>SSRF attacks often exploit trust relationships to escalate an attack from the vulnerable application and perform unauthorized actions.</li><li>These trust relationships might exist in relation to the server, or in relation to other back-end systems within the same organization.</li></ul><h4>Imagine a network with multiple systems:</h4><ul><li>A web application (the vulnerable application) is accessible from the internet.</li><li>Behind the scenes, this web application might interact with other systems within the organization’s network, such as databases, internal APIs, or other services.</li></ul><h4>Types of Trust Relationships:</h4><p>1.Trust Between Web Application and Server:</p><ul><li>The web application might be designed to fetch data from internal databases or APIs.</li><li>This implies a trust relationship where the server hosting the web application trusts the requests it receives from the application itself.</li><li>In an SSRF attack, the attacker exploits this trust by tricking the application into making unintended requests to unauthorized locations.</li></ul><p>2.Trust Between Servers:</p><ul><li>Different servers within the network might have established trust relationships to enable them to communicate and exchange data.</li><li>An attacker could exploit a vulnerability in one server to forge a request that appears to come from another trusted server. This forged request might trick the target server into performing unauthorized actions.</li></ul><h4>Exploiting trust for malicious gain:</h4><ul><li>Normally, these trust relationships allow legitimate communication and data exchange within the network.</li><li>An SSRF attack disrupts this by manipulating the trusted communication channels for malicious purposes.</li></ul><p>Examples:</p><ul><li><strong>Example 1 (Web App and Server):</strong> The web application might trust user input for searching a product catalog. The attacker injects a URL pointing to an internal database instead of a product name. The server, trusting the application’s request, retrieves data from the unauthorized database.</li><li><strong>Example 2 (Server to Server):</strong> A server responsible for managing user accounts trusts requests from another server that handles order processing. The attacker exploits a vulnerability in the order processing server to forge a request that appears to come from the trusted server. This forged request might trick the user account management server into granting unauthorized access.</li></ul><p><strong>In essence, SSRF attacks take advantage of existing trust relationships between systems to bypass security restrictions and gain unauthorized access to data or functionalities.</strong></p><h3>SSRF attacks against the server</h3><ul><li>In an SSRF attack against the server, the attacker causes the application to make an HTTP request back to the server that is hosting the application, via its loop-back network interface.</li><li>This typically involves supplying a URL with a host-name like 127.0.0.1 (a reserved IP address that points to the loop-back adapter) or localhost (a commonly used name for the same adapter).</li><li>The URL is supplied so as to access the resources on a server or a system that is outside of the application itself.</li><li>Consider a shopping application that lets the user view whether an item is in stock in a particular store.</li><li>To provide the stock information, the application must query various back-end REST APIs. It does this by passing the URL to the relevant back-end API endpoint via a front-end HTTP request.</li><li>When a user views the stock status for an item, their browser makes the following request:</li></ul><pre>POST /product/stock HTTP/1.0 <br>Content-Type: application/x-www-form-urlencoded <br>Content-Length: 118 <br><br>stockApi=http://stock.weliketoshop.net:8080/product/stock/check%3FproductId%3D6%26storeId%3D1</pre><p>This causes the server to make a request to the specified URL, retrieve the stock status, and return this to the user.</p><h4>Exploiting the vulnerability</h4><p>In this example, an attacker can modify the request to specify a URL local to the server:</p><pre>POST /product/stock HTTP/1.0 <br>Content-Type: application/x-www-form-urlencoded <br>Content-Length: 118 <br><br>stockApi=http://localhost/admin</pre><ul><li>The server fetches the contents of the /admin URL and returns it to the user.</li><li>An attacker can visit the /admin URL, but the administrative functionality is normally only accessible to authenticated users.</li><li>Thus it might be the case that attacker won’t see anything of interest. However, since the request to the /admin URL comes from the local machine, the normal access controls are bypassed.</li><li>The application grants full access to the administrative functionality, because the request appears to originate from a trusted location that is the application itself which the server trusts.</li></ul><h4>Potential reasons for implicitly trusting the request</h4><ul><li>The access control check might be implemented in a different component that sits in front of the application server. When a connection is made back to the server, the check is bypassed.</li><li>For disaster recovery purposes, the application might allow administrative access without logging in, to any user coming from the local machine. This provides a way for an administrator to recover the system if they lose their credentials. This assumes that only a fully trusted user would come directly from the server.</li><li>The administrative interface might listen on a different port number to the main application, and might not be reachable directly by users. This is a security best practice as it separates user traffic from administrative functionalities. Thus the server trusts that a request to admin account is not possible from the main application itself thus ignoring the port number and trusting the request from the application.</li></ul><p>These kind of trust relationships, where requests originating from the local machine are handled differently than ordinary requests, often make SSRF into a critical vulnerability.</p><p>Now consider this lab from Web Security Academy.</p><h3>Lab: Basic SSRF against the local server</h3><h4>Lab Description</h4><p>This lab has a stock check feature which fetches data from an internal system.</p><p>To solve the lab, change the stock check URL to access the admin interface at http://localhost/admin and delete the user carlos.</p><h4>Lab Solution</h4><p>To access the &quot;check stock&quot; feature we click on any of the products as in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*J5viIMBMiR6ZxiYM" /></figure><p>Now check for this captured request in burp suite.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zLGX4zoKEfF17XQF" /></figure><p>Now send the request to repeater.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*w6ztha4es-RgUHC4" /></figure><p>Now change the URL of the stockApi to the one given in the lab description as shown in the screenshot below. This modification sends the request back to the server, to a location that is local to the server which is hosting the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NzpQErDi8eeTPP7L" /></figure><p>Finally send this request and the response for the same is as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_7leUehIVane3YSF" /></figure><p>Now look for the path that will delete user Carlos and replace the stockApi URL with that as shown in the images below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Zd9PwGva-UzCvoxE" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aV-6bULEQyw6iUcm" /></figure><p>Finally send this request and as can be seen below the user has been deleted successfully and lab has been solved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*niGAT0JUbmU2NxZf" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*j0EzWcS4vWWtyqoD" /></figure><p>In the next part of this series I will explain about SSRF attacks against other back-end systems. Further I will solve lab from Web Security Academy based on this concept.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99b985310895" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Insufficient blacklisting of dangerous file types, File Upload Vulnerability series (Part 3b-II)]]></title>
            <link>https://medium.com/@vipulparveenjain/insufficient-blacklisting-of-dangerous-file-types-file-upload-vulnerability-series-part-3b-ii-de4f6ed6695c?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/de4f6ed6695c</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[remote-code-execution]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[rce-vulnerability]]></category>
            <category><![CDATA[file-upload-vulnerability]]></category>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Wed, 31 Jul 2024 02:41:01 GMT</pubDate>
            <atom:updated>2024-07-31T02:41:01.896Z</atom:updated>
            <content:encoded><![CDATA[<p>In continuation of the last blog, in this one I will explain in detail about by-passing the blacklist of file types by obfuscating file extensions.</p><h3>Obfuscating file extensions</h3><p>Imagine the validation code <strong>is case sensitive</strong> and fails to recognize that exploit.pHp is in fact a .php file. If the code that subsequently maps the file extension to a MIME type <strong>is not case sensitive</strong>, this discrepancy allows the attacker to sneak malicious PHP files past validation that may eventually be executed by the server.</p><p>Since the validation code is case sensitive thus it only blocks .php files and is not able to stop files with extensions such as .pHp. Now in the next step as the code that maps the MIME type to extension in NOT case sensitive thus it considers .pHp and .php both as same extension. This ultimately allows us to by-pass the black list and get the malicious PHP file executed.</p><p>Some of the obfuscation techniques are:</p><ul><li>Provide multiple extensions. Depending on the algorithm used to parse the filename, the following file may be interpreted as either a PHP file or JPG image: exploit.php.jpg</li><li>Add trailing characters. Some components will strip or ignore trailing whitespaces, dots, and suchlike: exploit.php.</li><li>Try using the URL encoding (or double URL encoding) for dots, forward slashes, and backward slashes. If the value isn’t decoded when validating the file extension, but is later decoded server-side, this can also allow you to upload malicious files that would otherwise be blocked: exploit%2Ephp</li><li>Add semicolons or URL-encoded null byte characters before the file extension. If validation is written in a high-level language like PHP or Java, but the server processes the file using lower-level functions in C/C++, for example, this can cause discrepancies in what is treated as the end of the filename: exploit.asp;.jpg or exploit.asp%00.jpg</li><li>Try using multibyte unicode characters, which may be converted to null bytes and dots after unicode conversion or normalization. Sequences like xC0 x2E, xC4 xAE or xC0 xAE may be translated to x2E if the filename parsed as a UTF-8 string, but then converted to ASCII characters before being used in a path.</li><li>There might be a defensive technique to remove the dangerous extensions to prevent the file from being executed. If this transformation isn’t applied recursively, you can position the prohibited string in such a way that removing it still leaves behind a valid file extension. For example, consider what happens if you strip .php from the following filename:exploit.p.phphp. This way the removal of the extension be by-passed and get the file executed.</li></ul><h3>Lab: Web shell upload via obfuscated file extension</h3><h4>Lab Description</h4><p>This lab contains a vulnerable image upload function. Certain file extensions are blacklisted, but this defense can be bypassed using a classic obfuscation technique.</p><p>To solve the lab, upload a basic PHP web shell, then use it to exfiltrate the contents of the file /home/carlos/secret. Submit this secret using the button provided in the lab banner.</p><p>You can log in to your own account using the following credentials: wiener:peter</p><p>Begin the lab by logging in using the given credentials.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dXDtrMrbv1ggpOoY" /></figure><p>Now upload a PHP file with the following contents:</p><pre>&lt;?php echo file_get_contents(&#39;/home/carlos/secret&#39;); ?&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Pof9Q6GGdS7SG4e2" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tPxnkh0d0ZHP3WAf" /></figure><p>This shows that the application has black listed files with .php extension.</p><p>Check burp suite for the requests captured for more details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tcaqbewMihoRd5Tj" /></figure><p>Send the above request to repeater for further analysis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vYbpCdgpxWFyvbjb" /></figure><p>Now change the name of the file as shown in the image above and add another extension.</p><p>Now send this request and analyse the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D2C6l1adBPDz6HvL" /></figure><p>The response shows that the filename has not changed as it should have been .php but contains 2 extensions which means the file might not work.</p><p>Now try adding a URL encoded null byte after the .php extension as shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9y0tZaLxT-M0Nm2M" /></figure><p>Now send this request and analyse the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PAMzAARk6CfKlYfz" /></figure><p>As can be seen in the screenshot above the .jpg extension and the null byte have been removed by the server and ultimately leaves the .php extension intact.</p><p>Now refresh the browser and check for the requests in burp suite.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7qj-mS6rPEc7iD7W" /></figure><p>The image above shows the GET request for the file we uploaded but the response is not correct this is because the file name itself is not correct. Hence we send this request to repeater and then change the filename to webshell.php and finally send this request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bZtxIG9LM_2moWow" /></figure><p>The response contains the secret code, copy this and submit it as solution to solve the lab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*V4wC2qGvdE8DFrYL" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nWY8AzjNefGbhzW6" /></figure><p>The logic behind the lab is, the server allows only JPG or PNG hence to upload our malicious file we append the .jpg extension after the .php extension in the filename to confuse the server. The server chips off the extension which ultimately makes the file non-executable. To work around this we add a URL encoded null byte after the .php extension.</p><p>When finally analysed the response, the server had chipped off the .jpg extension and the null byte leaving the php extension intact which earlier got deleted during the chipping off process by the server. Now finally this file is executed and the secret code is revealed.</p><p>This finally marks the end of this series, stay tuned for more blogs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de4f6ed6695c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Insufficient blacklisting of dangerous file types, File Upload Vulnerability series (Part 3b-I)]]></title>
            <link>https://medium.com/@vipulparveenjain/insufficient-blacklisting-of-dangerous-file-types-file-upload-vulnerability-series-part-3b-i-e1d4cb897570?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1d4cb897570</guid>
            <category><![CDATA[remote-code-execution]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[file-upload-vulnerability]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[rce-vulnerability]]></category>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Mon, 29 Jul 2024 06:47:57 GMT</pubDate>
            <atom:updated>2024-07-29T06:47:57.178Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>One of the more obvious ways of preventing users from uploading malicious scripts is to blacklist potentially dangerous file extensions like .php. The practice of blacklisting is inherently flawed as it&#39;s difficult to explicitly block every possible file extension that could be used to execute code. Such blacklists can sometimes be bypassed by using lesser known, alternative file extensions that may still be executable, such as .php5, .shtml, and so on.</blockquote><p>In this blog we will discuss two techniques to by-pass these flawed blacklists as mentioned below:-</p><ul><li>Overriding the server configuration</li><li>Obfuscating file extensions</li></ul><h3>Overriding the server configuration</h3><h4>Normal config files</h4><p>As explained earlier, servers typically won’t execute files unless they have been configured to do so. Consider the following example:</p><ul><li>For Apache server to execute PHP files requested by a client, developers might have to add the following directives to their /etc/apache2/apache2.conf file:</li></ul><pre>LoadModule php_module /usr/lib/apache2/modules/libphp.so<br>    AddType application/x-httpd-php .php</pre><ul><li>When a client requests a file with the .php extension, the request reaches the Apache server.</li><li>Apache checks its configuration to see how to handle .php files.</li><li>It finds the AddType directive and recognizes the file as a PHP script (based on the MIME type).</li><li>Apache then loads the PHP module (if it wasn’t already loaded) to understand the script’s code.</li><li>Apache executes the PHP script and generates a response based on the script’s output.</li></ul><blockquote>On Debian-based systems like Ubuntu, /etc/apache2/apache2.conf is the <strong>default location</strong> for the main Apache configuration file. This is a standard convention for system administrators, but it can vary depending on the Linux distribution or specific server setup.</blockquote><h4>Special config files</h4><p>There is one more case of configuration files, it is when the servers allow developers to create special configuration files within individual directories in order to override or add to one or more of the global settings. Consider the following examples:</p><ul><li>Apache servers, for example, will load a directory-specific configuration from a file called .htaccess if one is present.</li><li>You can use .htaccess file to modify various settings like access control, directory listings, and MIME type mappings within a specific directory.</li><li>Similarly, developers can make directory-specific configuration on IIS servers using a web.config file. This might include directives such as the following, which in this case allows JSON files to be served to users:</li></ul><pre>&lt;staticContent&gt;<br>    &lt;mimeMap fileExtension=&quot;.json&quot; mimeType=&quot;application/json&quot; /&gt;<br>    &lt;/staticContent&gt;</pre><blockquote>NOTE that .htaccess is the file name itself and not a file extension.</blockquote><blockquote>MIME (Multipurpose Internet Mail Extensions) defines a standard for <strong>identifying the format and nature of data</strong> exchanged on the internet.</blockquote><blockquote>In the context of web servers, MIME types play a crucial role in telling the server and the client’s browser how to handle a specific file.</blockquote><blockquote>By knowing the MIME type, the server can choose the appropriate way to process the data (e.g., display an image, execute a script, download a file).</blockquote><blockquote>The client’s browser can also use the MIME type to determine the correct way to render the content (e.g., display an image viewer for a JPEG image).</blockquote><blockquote>IIS stands for <strong>Internet Information Services</strong>. It’s a web server software developed by Microsoft and primarily used on Windows Server operating systems.</blockquote><blockquote><strong>Function:</strong> IIS acts as a software program that allows your computer to function as a web server. It receives requests from web browsers and delivers content (like web pages, images, or files) in response.</blockquote><blockquote><strong>Role:</strong> When you visit a website hosted on a Windows server, there’s a high chance the website is running on IIS in the background. It handles the communication between your browser and the website’s files.</blockquote><blockquote><strong>Comparison:</strong> Similar to Apache, a popular web server for Linux, IIS provides core web server functionalities.</blockquote><blockquote><strong>In essence, IIS is a software program that empowers Windows machines to act as web servers, delivering content and functionality to users who access websites.</strong></blockquote><h4>The vulnerability</h4><ul><li>Web servers use these kinds of configuration files when present, but users are not normally allowed to access them using HTTP requests.</li><li>However, you may occasionally find servers that fail to stop you from uploading your own malicious configuration file.</li><li>In this case, even if the file extension you need is blacklisted, you may be able to trick the server into mapping an arbitrary, custom file extension to an executable MIME type.</li></ul><p>Hence the attacker can upload a config file made by themselves for the server, the server will be tricked to consider that config file and give access to those file types that were earlier not allowed to be executed.</p><p>Consider this lab from Web Security Academy based on this concept.</p><h3>Lab: Web shell upload via extension blacklist bypass</h3><h4>Lab Description</h4><p>This lab contains a vulnerable image upload function. Certain file extensions are blacklisted, but this defense can be bypassed due to a fundamental flaw in the configuration of this blacklist.</p><p>To solve the lab, upload a basic PHP web shell, then use it to exfiltrate the contents of the file /home/carlos/secret. Submit this secret using the button provided in the lab banner.</p><p>You can log in to your own account using the following credentials: wiener:peter</p><h4>Solution</h4><p>Begin the lab by logging in with the given credentials.</p><p>Now try uploading exploit.php file with the following contents:</p><pre>&lt;?php echo system($_GET[&#39;command&#39;]); ?&gt;</pre><p>The response that we get is as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1H1rJ8iRx99ow2aW" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tfzYv7OsTN3CbXW9" /></figure><p>Analyse this request in burp suite and check the response for more details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vCLOw1-Yp8YcCmQl" /></figure><p>As can be seen in the screenshot above the server is an Apache server which has blacklisted PHP files. To by-pass this blacklist we create a special configuration file named “.htaccess” with the following contents.</p><pre>AddType application/x-httpd-php .shell</pre><p>This plain text will map the extension “.shell” to the MIME type “application/x-httpd-php”. This means that any file with the extension “.shell” will be treated as a PHP file.</p><p>Now upload this file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*a2ZJ49-Ku8Vwvh74" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*E1EVlAYT8c2i5WLl" /></figure><p>Now create another file with the extension “.shell’ and the following contents:</p><pre>&lt;?php echo file_get_contents(&#39;/home/carlos/secret&#39;); ?&gt;</pre><p>This will fetch contents of the secret file at the specified location when executed.</p><p>Now upload this file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*q2n2F9BPlcYETEGU" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WRRApXuznBsK0stL" /></figure><p>Now check burp suite for all the requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BY8XjaSumJWe7xS5" /></figure><p>This screenshot above for the special config file shows the contents of the same and the file type also.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uiEJ__rt1bTl6Sp4" /></figure><p>Similarly the screenshot above for the webshell shows the contents of the webshell and the extension of the filename too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aBV2JhB-7A5d3RPn" /></figure><p>The GET request for the uploaded webshell contains some secret code in the response. This means that the webshell file was successfully executed as a PHP file in the back-end even with the blacklist.</p><p>Copy this code and submit it as the solution to solve the lab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_2PDmFITG9yZ8D1f" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*toJXeLw-norFUtWX" /></figure><p>The logic behind this lab is, the server does not check for the contents of the file but only checks for the type of the file being uploaded and blocks all those files which are in the blacklist. The attacker is able to by-pass this blacklist by creating a special configuration file which basically allows the attacker to submit a file with some other extension but actually that file extension has been mapped to the blacklisted extension only. Hence the file gets executed and we are able to extract out the secret code.</p><p>The next part of this series will be the last blog in which I will continue the topic-insufficient blacklisting of dangerous file types and explain about obfuscating file extensions to by-pass the defense mechanism.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1d4cb897570" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploiting flawed validation of file uploads, File Upload Vulnerability series (Part 3a)]]></title>
            <link>https://medium.com/@vipulparveenjain/exploiting-flawed-validation-of-file-uploads-file-upload-vulnerability-series-part-3a-7d12678de1d6?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d12678de1d6</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[file-upload-vulnerability]]></category>
            <category><![CDATA[remote-code-execution]]></category>
            <category><![CDATA[rce-vulnerability]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Thu, 25 Jul 2024 10:59:51 GMT</pubDate>
            <atom:updated>2024-07-25T10:59:51.496Z</atom:updated>
            <content:encoded><![CDATA[<p>In the wild, it’s unlikely to find a website that has no protection against file upload attacks but just because defenses are in place, that doesn’t mean that they’re robust. We can still exploit flaws in these mechanisms to obtain a web shell for remote code execution.</p><h3>Flawed file type validation</h3><p>When submitting HTML forms, the browser typically sends the provided data in a POST request with the content type application/x-www-form-url-encoded.</p><p>This is fine for sending simple text like your name or address. However, it isn’t suitable for sending large amounts of binary data, such as an entire image file or a PDF document. In this case, the content type multipart/form-data is preferred.</p><p>Consider a form containing fields for uploading an image, providing a description of it, and entering your username. Submitting such a form might result in a request that looks something like this:</p><pre>POST /images HTTP/1.1<br>    Host: normal-website.com<br>    Content-Length: 12345<br>    Content-Type: multipart/form-data; boundary=---------------------------012345678901234567890123456<br><br>    ---------------------------012345678901234567890123456<br>    Content-Disposition: form-data; name=&quot;image&quot;; filename=&quot;example.jpg&quot;<br>    Content-Type: image/jpeg<br><br>    [...binary content of example.jpg...]<br><br>    ---------------------------012345678901234567890123456<br>    Content-Disposition: form-data; name=&quot;description&quot;<br><br>    This is an interesting description of my image.<br><br>    ---------------------------012345678901234567890123456<br>    Content-Disposition: form-data; name=&quot;username&quot;<br><br>    username<br>    ---------------------------012345678901234567890123456--</pre><p>As can be seen, the message body is split into separate parts for each of the form’s inputs. This means one part contains the data about the image itself and the other two parts contain the text data one is username and the other one is description of the data.</p><p>Each part contains a Content-Disposition header, which provides some basic information about the input field it relates to. These individual parts may also contain their own Content-Type header, which tells the server the MIME type of the data that was submitted using this input. In this case it is the image data type part which contains this header to tell the server about the type of data.</p><h4>Flawed security</h4><p>One way that websites may attempt to validate file uploads is to check that this input-specific Content-Type header matches an expected MIME type. If the server is only expecting image files, for example, it may only allow types like image/jpeg and image/png. This way a malicious PHP file will not be accepted.</p><h4>Vulnerability</h4><p>Problems can arise when the value of this header is implicitly trusted by the server. If no further validation is performed to check whether the contents of the file actually match the supposed MIME type, this defense can be easily bypassed using tools like Burp Repeater.</p><h3>Preventing file execution in user-accessible directories</h3><p>This is another type of defense mechanism used by websites to prevent file upload vulnerability. This forms the second line of defense and stops the server from executing any scripts that do slip through the net.</p><p>As a precaution, servers generally only run scripts whose MIME type they have been explicitly configured to execute. Otherwise, they may just return some kind of error message or, in some cases, serve the contents of the file as plain text instead. Consider this below:</p><pre>GET /static/exploit.php?command=id HTTP/1.1<br>    Host: normal-website.com<br><br><br>    HTTP/1.1 200 OK<br>    Content-Type: text/plain<br>    Content-Length: 39<br><br>    &lt;?php echo system($_GET[&#39;command&#39;]); ?&gt;</pre><p>Here the command in the file is returned as it is which means the server did not execute it.</p><p>This way the server prevents the execution of any files that it is not configured to do so. This behaviour is potentially interesting in its own right, as it may provide a way to leak source code, but it nullifies any attempt to create a web shell.</p><h4>Vulnerability</h4><p>This kind of configuration often differs between directories. A directory to which user-supplied files are uploaded will likely have much stricter controls than other locations on the filesystem that are assumed to be out of reach for end users. If the attacker can find a way to upload a script to a different directory that’s not supposed to contain user-supplied files, the server may execute the script after all.</p><p>We can do this using the filename field of the content disposition header in the multi-part /form data. This is usually used to name the file and specify the location where the file will be stored. Hence using path traversal technique we can navigate to those directories that do not contain any user supplied file and try saving our file over there. This will help us execute the web shell and compromise the server.</p><p>Now consider this lab below from Web Security Academy based on this concept.</p><h3>Lab: Web shell upload via path traversal</h3><h4>Lab Description</h4><p>This lab contains a vulnerable image upload function. The server is configured to prevent execution of user-supplied files, but this restriction can be bypassed by exploiting a secondary vulnerability.</p><p>To solve the lab, upload a basic PHP web shell and use it to ex-filtrate the contents of the file /home/carlos/secret. Submit this secret using the button provided in the lab banner.</p><p>You can log in to your own account using the following credentials: wiener:peter</p><p>Begin the lab by logging in using the given credentials.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VhwxlUPfnkCmYIcn" /></figure><p>Now upload the exploit as shown below using the upload functionality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rYkZzMbq6EyaBIuU" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Y1uGBG8hMET6OPgx" /></figure><p>The exploit used in this case is:</p><pre>&lt;?php echo system($_GET[&#39;command&#39;]); ?&gt;</pre><p>This exploit can execute any command given by the attacker as the value of “command” query parameter in the GET request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QHNixFB8iHZ2jGHb" /></figure><p>Now as shown in the screenshot above try to open the exploit in the new tab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P1HukPxmElLbz7GI" /></figure><p>Since the result in the browser is blank thus check burp suite for the request captured.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*t3if-esApvwdvSym" /></figure><p>As can be seen above, the result is the PHP code itself, hence the server has not executed the shell due to the restriction of the MIME type which is allowed to be executed in this directory.</p><p>To by pass this we try to traverse one directory up the system and upload our web shell over there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PiagM8Fd80pYo10Y" /></figure><p>As can be seen in the screenshot above we change the filename field in the content-disposition header to include a path traversal sequence. If implemented successfully, the exploit will be uploaded in one directory up the current directory which ultimately may help us by pass the restriction that might be just for this current directory.</p><p>The result of this request is as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*j-gpF6Yj2n3bZaJ8" /></figure><p>Since there is no change in the filename as seen in the response therefore the path traversal sequence did not work well. In order to make it work, try to URL-encode the traversal sequence and then add it to the filename as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2qfpk9XgelAVjJhI" /></figure><p>Now send this request and analyse the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KDY18R1u9qpcbiIB" /></figure><p>As can be seen above, the path traversal technique has worked successfully and that now the file has been uploaded in the directory one up than the current one.</p><p>Now again try opening the file uploaded in a new tab as done previously after refreshing the web page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2WMMibNEUHT-_at-" /></figure><p>This error comes up because the name of the file still contains the path traversal sequence, since the exploit has been uploaded in an upper directory thus delete the sequence along with the “avatar” from the file location and add value for the command parameter as query string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EIV7vSRwqMz5Ph0A" /></figure><p>The response is as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FaPSOOgM4MW94TMd" /></figure><p>This shows that the web shell has been executed successfully.</p><p>Now try to read the contents of the file mentioned in the lab description.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mdXnJoQB5ns9NSvx" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BTKUnm4VmTZZazLK" /></figure><p>As shown in the screenshot above, the secret code is repeated twice in the output thus simply copy the code and submit it as the solution to solve the lab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wWCLjBlmtbRzMQ7G" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ei4uxr3XezQI_ZhX" /></figure><p>The logic behind this lab is that directory to upload the user avatar has restriction on the file type that can be executed hence the web shell does not work well in that directory.</p><p>Thus to by pass this we simply travel one directory up and upload the web shell.</p><p>This is done using the path traversal sequence in the filename field of the content disposition header.</p><p>Since the sequence was not directly parsed correctly thus we URL-encoded it for it to be successful.</p><p>The sequence is decoded and it results the file being uploaded in one directory up than the current one.</p><p>The server took the filename itself and appended it to the “avatar” directory, this became the location of the uploaded file. We add a sequence of path traversal in the filename and that filename is appended after the “avatar” directory to form the final location path. This path eventually contains the path traversal sequence thus it results in the file being uploaded to one directory up in the filesystem.</p><p>Since in the new directory there is no restriction about the type of the file being uploaded and that there is no restriction in execution also thus we execute out web shell to know the secret.</p><p>In next part of this series I will explain in detail about insufficient blacklisting of dangerous file types which is again a defense mechanism to prevent file upload vulnerability. Finally there will be solution to Web Security Academy labs based on this concept.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d12678de1d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploiting unrestricted file uploads to deploy a web shell, File Upload Vulnerability series (Part…]]></title>
            <link>https://medium.com/@vipulparveenjain/exploiting-unrestricted-file-uploads-to-deploy-a-web-shell-file-upload-vulnerability-series-part-979b4df0e657?source=rss-f18e96136dd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/979b4df0e657</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[php-web-shell]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[remote-code-execution]]></category>
            <category><![CDATA[rce-vulnerability]]></category>
            <dc:creator><![CDATA[Vipul Jain]]></dc:creator>
            <pubDate>Mon, 22 Jul 2024 09:40:18 GMT</pubDate>
            <atom:updated>2024-07-22T09:40:18.903Z</atom:updated>
            <content:encoded><![CDATA[<h3>Exploiting unrestricted file uploads to deploy a web shell, File Upload Vulnerability series (Part 2)</h3><p>From a security perspective, the worst possible scenario is when a website allows you to upload server-side scripts, such as PHP, Java, or Python files, and is also configured to execute them as code. This makes it trivial to create your own web shell on the server.</p><blockquote><strong>Web shell</strong></blockquote><blockquote>A web shell is a malicious script that enables an attacker to execute arbitrary commands on a remote web server simply by sending HTTP requests to the right endpoint. For more information refer to the part 1 of this series.</blockquote><p>If you’re able to successfully upload a web shell, you effectively have full control over the server. This means you can read and write arbitrary files, exfiltrate sensitive data, even use the server to pivot attacks against both internal infrastructure and other servers outside the network.</p><p>For example, the following PHP one-liner could be used to read arbitrary files from the server’s filesystem:</p><pre>&lt;?php echo file_get_contents(&#39;/path/to/target/file&#39;); ?&gt;</pre><p>Once uploaded, sending a request for this malicious file will return the target file’s contents in the response.</p><p>A more versatile web shell may look something like this:</p><pre>&lt;?php echo system($_GET[&#39;command&#39;]); ?&gt;</pre><p>This script enables you to pass an arbitrary system command via a query parameter as follows:</p><pre>GET /example/exploit.php?command=id HTTP/1.1</pre><p>Now consider this lab from Web Security Academy.</p><h3>Lab: Remote code execution via web shell upload</h3><h4>Lab Description</h4><p>This lab contains a vulnerable image upload function. It doesn’t perform any validation on the files users upload before storing them on the server’s filesystem.</p><p>To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file /home/carlos/secret. Submit this secret using the button provided in the lab banner.</p><p>You can log in to your own account using the following credentials: wiener:peter</p><h4>Solution</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kEdioysrA5AkfSC9" /></figure><p>Begin the lab by logging in using the given credentials.</p><p>Now interact with the image upload functionality to display the avatar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I-MVc6y5__9YZHYp" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6sDxO4RugX-GIfZU" /></figure><p>This finally uploads our file onto the server and going back to the homepage we see the avatar has been updated as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8RsLRqY9PamGR5hG" /></figure><p>This means the file uploaded onto the server has been executed and since the file type was an image thus the server simply returns the contents of the file as response to the user and displays the avatar on the user page.</p><p>Now check burp suite for the requests captured.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nqLWga59K3mCAUWA" /></figure><p>The request shown above is the one used for uploading the file onto the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yEbuk5RCE9rvyw3I" /></figure><p>The request shown above is used to execute the file that was uploaded onto the server. Since in this case it was an image file thus the server simply returns the content of this file in the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CTg7gXz1dY2d9vNt" /></figure><p>Now as shown in the screenshot above create a web shell using PHP and change the name of the file to “exploit.php” or anything else but with “.php” extension. Further remove the image file content since now image is not being uploaded onto the server. Now send this request and analyse the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RS44JTKcPQbthde5" /></figure><p>As can be seen in the response above the web shell has been uploaded onto the web server successfully. This finally gives us access to the server. Now we simply execute the command by changing the name of the file in the GET method from the image file to the exploit file. This makes the server to visit the mentioned location where the exploit file is stored and execute it.</p><p>Since the server does not perform any validation on the file being uploaded and that it is also configured to execute the “.php” files not checking the contents of the file before executing. The server executes the file and tries to read the file mentioned in the web shell.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u_76y6z1QO0fdUO9" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5zc1gCpRO9vWTYJS" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pbk22R_brwPvbTTR" /></figure><p>This finally reveals the secret to us. Finally copy this and submit it as solution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*74vgcs49L-eq_wnx" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DkUhFBRJ_wf_fMmx" /></figure><p>This finally solves the lab.</p><p>The logic behind the lab is, the server does not validate any of the files that are being uploaded onto the system. This allows us to replace the image file with our web shell. Further there is no defense against executing potentially malicious PHP files thus when we try executing the file (the exploit uploaded onto the server), the server executes the file and reveals the secret code.</p><p>Note that there are two crucial steps:</p><ul><li>First is to upload a web shell and this is done using the file upload vulnerability present in the system</li><li>The next step is to execute the web shell in order to perform desired function. Though in many cases we might not have to execute the file explicitly and thus only uploading the file might just be enough for compromising the server.</li></ul><p>In the next part I will explain in detail about exploiting flawed validation of file uploads and then solve Web Security Academy labs based on this concept.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=979b4df0e657" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>