RatSec

RatSec Blog

SSRF Vulnerability

- Posted in Uncategorized by

This PHP code defines a simple web page that is vulnerable to Server-Side Request Forgery (SSRF). Let's break down the code:

  1. PHP Configuration:

    ini_set('allow_url_fopen', '1');
    

    This line sets the configuration option allow_url_fopen to 1, allowing the use of URLs with the fopen function. This configuration setting determines whether the file_get_contents function can be used to open URLs.

  2. Checking for URL Parameter:

    if (isset($_GET['url'])) {
       $url = $_GET['url'];
       $response = file_get_contents("https://www.google.com");
       echo $response;
    } else {
    

    This block checks if a URL parameter named 'url' is present in the query string. If the parameter is present, it retrieves the value and uses file_get_contents to fetch the content from the specified URL ("https://www.google.com" in this case) and then echoes the content.

  3. HTML Form for User Input:

    <form action="ssrf_vulnerable.php" method="get">
       <label for="url">Enter a URL to fetch:</label>
       <input type="text" name="url" id="url" placeholder="http://example.com">
       <button type="submit">Fetch URL</button>
    </form>
    

    If the 'url' parameter is not present in the query string, the code displays an HTML form where the user can input a URL. Upon submitting the form, the page will reload with the specified URL in the query string.

  4. HTML Page Content: The rest of the code includes HTML markup for the webpage, including a title, a heading, and some explanatory text about the SSRF vulnerability. It also includes a link to a file called "solutions.txt" and some JavaScript code related to Google Ads and Google Analytics.

The main issue with this code is the lack of proper validation and filtering of the user-provided URL ($_GET['url']). This makes the application vulnerable to SSRF attacks, where an attacker can manipulate the URL parameter to make the server perform unintended requests to internal resources.

It's important to note that in a real-world scenario, allowing arbitrary URLs to be fetched by the server without proper validation poses a serious security risk and should be avoided. Security measures such as input validation and using a whitelist of allowed URLs are recommended to mitigate SSRF vulnerabilities.

You can try this out on our labs here