# API9:2023 Improper Inventory Management

It's nothing new, but I liked the idea of ​​this lab during study Api Penetration Testing Cource from Api University Platform,  , so I will try to explain it step by step in a simple way and i will&#x20;

* Vulnerability in OWASP API  : [https://owasp.org/API-Security/editions/2023/en/0xa9-improper-inventory-management](https://owasp.org/API-Security/editions/2023/en/0xa9-improper-inventory-management/)
* API University Course : <https://university.apisec.ai/products/api-penetration-testing>
* Vulnerable Application : you can use it online <http://crapi.apisec.ai/>  or Download it local <https://github.com/OWASP/crAPI>

## API9:2023 Improper Inventory Management  <a href="#api92023-improper-inventory-management" id="api92023-improper-inventory-management"></a>

* it's a Vulnerability found when application still using API out of date/old versions of APIs. it happen when you upgrade from version 3 to version 4 but leave the old version&#x20;
* &#x20; attacker `discover unsupported` and non-production `versions` of an API  during Reconnaissance  ( all of the APIs that are running, all the endpoints, versions, older versions, who's accessing them )

## **Common endpoints you can find During Recon  :**&#x20;

* like our API has update to `Version 3` and some end points still use `Version 2` like :
  * api.target.com/v3
  * /api/`v2`/accounts
  * /api/`v3`/accounts
  * /`v2`/accounts
* API versioning could also be maintained as a header:
  * *Accept: `version=2.0`*
  * *Accept `API-version=3`*
* `Non-production versions` of an API include any version of the API that wasn’t meant for end-user consumption مش متخصص للاستخدام للمستخدم العادي (نسخه تجريبيه)
  * api.test.target.com
  * api.uat.target.com
  * beta.api.com
  * /api/private
  * /api/partner
  * /api/test

### Scenario ( Use Old Version of Api to Brute Force OTP Code )

1. fuzzing requests in our Postman collection to sort version we have&#x20;

   <figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FNaSe8IMAoK5S2wX59wle%2FUntitled.png?alt=media&#x26;token=176cd155-4bd1-4904-8226-057be005cbac" alt="" width="151"><figcaption></figcaption></figure>
2. configure a test using the Collection Editor
   1. select Collection and choose edit
   2. add this script to know which request has been successfully received   :  &#x20;
      1. `pm.test("Status code is 200", function () { pm.response.to.have.status(200); })`
   3. choose run collection option ⇒ remove requests that `doesn’t have version number`
   4. remove the token to Run an unauthenticated baseline scan of the crAPI collection

<figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2Fy5Y3XS7mdP2MAeFok7iR%2FUntitled.png?alt=media&#x26;token=8c0e9324-c4c0-43ae-92bf-7427a78abc78" alt="" width="563"><figcaption></figcaption></figure>

3. when we make a test with non authentication we get failed in all requests&#x20;

<figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FkDgTHxz5peCSCh3Rvr8a%2FUntitled.png?alt=media&#x26;token=6ace7413-8764-4adb-88b3-369100c94a91" alt="" width="563"><figcaption></figcaption></figure>

4. create a new environment
   1. `create a variable give it value v1` in initial value and current value&#x20;

      <figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FEjya4FmtKQQlif8jE4oy%2FUntitled.png?alt=media&#x26;token=0b1074fd-66db-477a-9ea0-6dfa5872395f" alt=""><figcaption></figcaption></figure>
5. `"Find and Replace"` all requests with `v2` and replace the all to our variable `{{ver}}` ( v1 )

   1. to turn the collection's current versions into a variable
   2. put variable name between`{{ }}` like `{{ver}}` and choose select all to replace it in all requests&#x20;

   <figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2F3VexA5V7WaxXUSPWgVLQ%2FUntitled.png?alt=media&#x26;token=68eb0eef-c135-41ea-9426-7eec286a5429" alt="" width="375"><figcaption></figcaption></figure>
6. after run i found that the v3 get error 404

<figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FyCgBUs5RJyKVozaCTElV%2FUntitled.png?alt=media&#x26;token=88a87e68-781f-4014-9899-b31454648f17" alt="" width="563"><figcaption></figcaption></figure>

7. let’s change the current value to `V2` and run again `OTP request` with replace version to 1 get ⇒ internal server error `500`&#x20;

<div align="center"><figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2F7tmIzBSKzKjjI5LV9XD9%2FUntitled.png?alt=media&#x26;token=b69569d7-3d66-431a-9acc-3bff2e37adef" alt="" width="563"><figcaption></figcaption></figure></div>

8. let’s try 2 times by give the real version with 2 request ⇒ first with v2 and second with v3&#x20;

<figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2F9V6fzwuRPHRwMDDtgJoj%2FUntitled.png?alt=media&#x26;token=6523e52b-7f25-4ebb-93f8-2e65bd44f287" alt="" width="563"><figcaption><p>first one with v2 </p></figcaption></figure>

<figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FRMkr5PshiCchXsnzLy5t%2F2.png?alt=media&#x26;token=8431f0ce-bd73-4f0d-b43c-97948f36e506" alt="" width="563"><figcaption><p>second with v3 </p></figcaption></figure>

9. this is the worst thing because we can use `Unsupported Version` ⇒ we get success
10. when we try to many time to enter `OTP in v3` account locked then we can’t do `brute force`&#x20;

    <figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FIPEt4qzBR9Yh2O4aLo0m%2FUntitled.png?alt=media&#x26;token=77e15b83-1dda-4b97-bb83-6786c54e08f3" alt="" width="563"><figcaption></figcaption></figure>
11. but in `V2 Brute force is available` and account didn’t lock ⇒ this is the vulnerability
12. let’s enable the burp proxy from postman by ⇒ by enter setting then ⇒ add custom proxy and put same ip and port for burp
13. grep “`Invalid`” word because the correct OTP give same response 500 but different error&#x20;

    <figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FoiMFlDsrjMr0SbRSVPuO%2FUntitled.png?alt=media&#x26;token=09933a24-e65c-44db-b748-52428c74c98b" alt="" width="563"><figcaption></figcaption></figure>
14. let’s check our mail of CRAPI platform\
    &#x20;

    <figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FwWAlIN3jkudnSDbdVZeI%2FUntitled.png?alt=media&#x26;token=e075e6e2-e212-42f2-9e49-9668e5743e07" alt="" width="563"><figcaption></figcaption></figure>
15. it’s the same and here we success let’s try it with Wfuzz tool to make a brute Force Then send Post request with the new OTP & new Password :&#x20;
    1. `wfuzz -d '{"[k](mailto:email%22:%22hapihacker@email.com)ero@kero.com", "otp":"FUZZ","password":"NewPassword1"}' -H 'Content-Type: application/json' -z file,/usr/share/wordlists/SecLists-master/Fuzzing/4-digits-0000-9999.txt -u [http://crapi.apisec.ai/identity/api/auth/v2/check-otp](http://crapi.apisec.ai/identity/api/auth/v2/check-otp) --hc 500` &#x20;

       <figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FUF0MtVabW7nBmFMbO9z8%2FUntitled.png?alt=media&#x26;token=8ef1fc8a-bdfc-4dd4-b2ef-30a4eae370ab" alt="" width="563"><figcaption></figcaption></figure>
16. here we Success in login&#x20;

<figure><img src="https://2140186435-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpoPpsF6DyQtOrXy70rxC%2Fuploads%2FmL5XnTZUV5yCtsbczzT7%2FUntitled.png?alt=media&#x26;token=5241da3d-3b71-48e3-9811-87e949a3721b" alt="" width="375"><figcaption></figcaption></figure>
