-1.5 C
New York
Thursday, February 2, 2023

How to extract artifacts from OpenAPI docs to help attack APIs – Security Boulevard

The Home of the Security Bloggers Network
Home » Security Bloggers Network » How to extract artifacts from OpenAPI docs to help attack APIs
Don’t you just love OpenAPI docs? They have so much useful information in them. When written well, you can really get a good understanding of how an API endpoint functions.
Heck, you don’t even have to have well-written API docs. You can always generate your own rogue API docs if they don’t exist.
The thing is, those API docs get cumbersome quickly. There is so much information in them; you can wade through them for hours, trying to extract the information you need as you go about your API security testing.
In this article, I’m gonna show you some clever ways to extract the most vital information you need when hacking APIs.
Let’s go!
So these API docs are nothing more than complex JSON formatted files that follow the OpenAPI specification (OAS). We can use the popular jq tool to extract the information we want.
jq is a lightweight and flexible command-line JSON processor. It’s like sed for JSON data – you can slice, filter, map, and transform structured data with the same ease that sed, awk, grep, and friends let you play with text.
In our case, we will use it to extract the more important bits we want to use when API hacking.
So you will need to make sure you have installed jq on your system.
jq is written in C and has no runtime dependencies, so it should be possible to build it for nearly any platform. Prebuilt binaries are available for Linux, OS X, and Windows. You can find the download page here.
Check your package manager. Something like this might work:
Use brew:
Now that you have jq installed, you will need an OpenAPI document that we can work with for the article. I am going to use the OpenAPI document from OWASP crAPI. You can download it here.
Feel free to use any swagger/OpenAPI doc you want. For this article, I am going to be parsing out an OpenAPI 3.0 document.
One of the first things we want to do after securing the API doc is to extract the endpoint routes. I like to always produce a custom wordlist so I can understand the form and function of the API layout. You can learn a lot from the behavior of developers by understanding how they construct the paths to their endpoints.
In fact, at times it’s possible to identify the development framework being used by understanding the paths. However, that is an article for another day.
So, to extract the paths to be used in a wordlist, try something like this:
What you will find in the wordlist file is a clean list of the API routes for crAPI:
You can immediately notice a few things.
This is all useful. We could use this custom wordlist and jam it into kiterunner to see if there are any other interesting endpoints. But we will cover that sort of thing another day.
For now, you have a clean wordlist representing all the key API routes to get started and an understanding of the different modules of the API.
Side Note: If you haven’t worked with OWASP crAPI before, here’s an interesting tidbit. The web application modules are all written in different programming languages. The community module is written in Go. The identity module is written in Java. And the workshop module is written in Python.
This demonstrates that while the API document shows it as a heterogenous API, the fact is that the app has several different tech stacks to attack. Being able to identify this during your recon phase will allow you to better craft your payloads by understanding the programming languages being used. If the concept of fingerprinting the target programming language is new to you, I suggest you check out my article on How to Detect the Programming Language of an API.
One of the more interesting things we can understand when looking through an OpenAPI document is how the developers use HTTP status codes. It’s quite common to see patterns in how they use them to represent authentication and authorization errors, success statuses, and whatnot.
This query is a bit more complex. I will walk you through it.
So let’s break down this query and discuss the jq filters I used:
The end result will look something like this:
So how is this valuable? We get an idea of what response codes are used. Immediately interesting are things like HTTP 500 codes being (mis)used. You can check through the API document and quickly see that the API returns 500 response codes even when it ISN’T an “internal server error”. And you can see that only one place uses 401 error codes, and it’s NOT in the login endpoint. It too returns a 500 code on a failed login. All this helps you understand how mature a developer is in their coding practices and how you should think about how the API will react to you sending over tainted data.
OK, let me show you one more creative way to quickly understand the OpenAPI document you are interrogating. While having a wordlist of all endpoints is useful, and understanding what status codes are used is enlightening, getting a quick human-readable summary is even more valuable.
This query is gonna look ugly. But it’s really helpful. Credit to Arnaud Lauret (aka the API handyman) for this one. I learned it from him a few years back.
Brace yourself.
The end results are really nice though:
It’s at this point that I highly recommend you start looking at using a somewhat unknown feature of jq. When more complex queries like this start cluttering your command line, it might make more sense to move the query into a file and then use the -f parameter to load it.
You can download your own copy here from my GitHub gist: dump-endpoints.jq.
Here’s how to use it:
I’m not going to break down this query. You can always look at the jq manual if something doesn’t quite make sense. The idea though is it will reach in, parse out the API spec documentation and return the HTTP method, route, and description of every endpoint defined in the API.
It’s through these results we can quickly see a mapping of all the endpoints we might be interested in, allowing us to cross-reference with the API doc later to get even more details as required.
Time for one last query. Once you have the ability to quickly dump your endpoint wordlist, determine how HTTP status codes are used and have good visibility into the different endpoints, now you want to be able to extract the full details of a specific endpoint.
You can do this by querying the paths array and extracting individual endpoints by their HTTP method.
Try something like this:
Notice what I did there? By quickly passing in the endpoint route and the detected HTTP method, you can dump the detailed JSON output for the exact API endpoint you wanna attack and know everything from the schema used in the body to the expected response codes and response structure.
I picked this specific route because it supports more than one HTTP method. Let’s query for the GET and see how it differs:
Nothing to it. Easy to move around the API document with a bit of jq query magic.
This article has shown you how to use jq, a powerful JSON processing tool, to quickly extract useful information from the OpenAPI documents you may be using while hacking APIs. You can use this information to better understand the API and determine which endpoints may be of interest to you to attack. Additionally, by using jq you can extract full details of a specific endpoint, including the schema and response codes expected, so you don’t have to waste time wading through tons of structured data in an overly complex JSON file.
You can build out more complex jq queries and store them in your own files for easy reuse, just like I did to dump details for all the endpoints using dump-endpoints.jq. In the end, building out this library of jq filters lets you move around your target’s API quickly, hone in on what is interesting, and get right to work.
I hope you found this creative approach helpful!
Like what you saw? Then check out my free ebook of The Ultimate Guide to API Hacking Resources where you can find tons more online resources focused on this sort of thing.
The post How to extract artifacts from OpenAPI docs to help attack APIs appeared first on Dana Epp's Blog.
*** This is a Security Bloggers Network syndicated blog from Dana Epp's Blog authored by Dana Epp. Read the original post at: https://danaepp.com/how-to-extract-artifacts-from-openapi-docs-to-help-attack-apis
More Webinars
Security Boulevard Logo White
DMCA

source

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles