Skip to main content

Testing API security using Python and Postman part 1 (day 18)



API stands for Application Programming Interface, mainly used as a service to serve its user and it can provide wide range variety of utility depends on how the developer design it. It is not surprise that many application including web and mobile have adapted this technology into their backend architecture. Nevertheless, every developer and hacker should pay attention to its security to ensure it is not abuse by malicious actor.

In this blog i will show you how to test an API by using Python and Postman. To test the API i used Tireful API. It is a web app intentionally developed to be insecure. The purpose of the app to teach developers, QA or security professionals about flaws present in webservices (REST API) due to insecure coding practice. Following are the scenarios implemented.

  • Information Disclosure
  • Insecure Direct Object Reference
  • Access Control
  • Throttling
  • SQL Injection
  • Cross Site Scripting

Setup The API:

i used python environment utility to isolate the installation of the prerequisite library of Tiredful API so it is not interfere with other package in my original python package.

to use virtual environment in python, use this command:

~# sudo apt install virtualenv

create a virtualenv:

~# virtualenv -p python env

the following command will create a virtual environment in Python 2 with the environment name of env

run the following command to make sure it is setup properly:

~# ls env/lib

activate the virtual environment with:

~# source env/bin/activate

Now that we have activate our virtual environment, next step is to install all the necessary package to run the Tiredful-API:


After the setup is complete, you can run it with the following command:


It will host the API at localhost:8000

 

Challenge: Information Disclosure


Postman:

using postman, you can send any request to the API, basically postman have create a very nice GUI that let user have a nice experience to do API testing. You can just put the URL api and press send.



The top bar is the url target and the lower part is the result. As you can see we got a json like response from the API. The challenge in this section is to find an information disclosure from the API this means we have to trigger some error that will cause the API to spill unwanted information from its backend (stacktrace).

I try several string such as special character and it give me some error regarding the map application. This happen due to the debug settings in django, in order to prevent this to happen you should make the debug to False.


and i found out if you put a capital letter to the ISBN it will show a stacktrace log to your response.

 

 Python:

in order to send API request with python you can use python library "requests". The library is pretty easy to use you can customize the packet and also create your own payload.


The script is super simple is just send a GET request and get the response.


Challenge: Insecure Direct Object Reference

Postman:

in order to send a request to the api you need to have an authentication token to platform. To generate it, go to the user token page and enter a listed credential (username and password) to get the token.


to use it you need to include "Authentication" tag to the request and the value must follow the format (Bearer <token>)


in the section description the exam-id is in format of:
  1. MQ== => 1
  2. Mg==  => 2
you can recognize that this in base64 format. Thus we can enumerate other object by doing loop and combine it with a base64 encoder. Postman have what is called as test script (pre request script and test script) that programmed in javascript, you can use this feature to create automation to test API. In this section i'm going to create a script that will enumerate all exam id.



to send a request you can use the "pm" object with sendRequest method and of course because that in order to interact the api you need to provide an authorization in the header once you done the response will log into the postman console (crtl+alt+c)

the final script going to look like this:


the script is simply loop to 100 and convert the integer to a base64 encode and send it to the backend API and only show to the console if the HTTP response is 200. After the script is run we got two more exam result.


Python:

using python is going to just add couple of line in our script. The following is the python script:

we use headers parameter in our script so the authentication tag is send along with the GET request






and the result is going to be like this:


Thank you have a nice day :)



Comments

Popular posts from this blog

Having fun analyzing nginx log to find malicious attacker in the net (ง'̀-'́)ง (day 37)

  What makes you sleepless at night? is it because of a ghost or scary stories? is it because you have an important meeting tomorrow? or is it because you have an exam? For me, what keeps me up all night is that I keep thinking about what happens to a website that I just created, is it safe from an attacker (certainly not) or did I missing some security adjustments that lead to vulnerability? well I'm not the best secure programmer in the world, I'm still learning and there is a big possibility that I can make a mistake but for me, a mistake can be a valuable investment to myself or yourself to be better so from this idea, I want to know more about what attackers casually do when attacking a website. Here in this post, I'm going to show you how I analyzed attack to the website that I have permission to design and also some interesting findings that I could get from the analysis Background: All of this analysis comes from the traffic that is targeted to th

Utilize Pwntools for crafting ROP chain :') (day 69)

who doesn't like pwntools? it is a very versatile tool and can be customized according to our need using the python script but did you need to know that pwntools itself can help us to automatically craft a rop chain for us? so in this post, I will show you how to make rop chain less painful and make pwntools do all the heavy lifting. To demonstrate this I will use the binary challenge callme 64 bit from ropemporium link: https://ropemporium.com/challenge/callme.html Crashing the app: Like any other exploitation process, we need to crash the program by generating a long string pattern to determine the offset. based on the information from the above figure we can see that we required to provide 40 bytes of offset Fun stuff: now this where the fun stuff began write the following python script: as in the guideline of the challenged said we need to chain the function call by first to call the callme_one function, callme_two function and then callme_three funct

WriteUp PWN tarzan ROP UNICTF ಠ_ಠ (day 61)

So in this post, I'm going to talk about how to solve the Tarzan pwn challenge from UNICTF 2019. Back in the day when the competition is still going I couldn't finish it and don't have any clue to solve this but this time I was able to finish it :) Also in this post, we will be going to be heavily focused on how to utilize pwntools to construct a ROP chain. If you kinda confused about my explanation in this post you can refer to this following youtube video, link: https://www.youtube.com/watch?v=gWU2yOu0COk I build the python script based on this video Ok, let's get started! In this challenge, you will get two binary first go with tarzan and libc-2.29.so by providing .so file it tell us what version library that the target machine is using this could help us to do ROP chain. first, we run the Tarzan binary to get the basic idea of the program work and as you can see it just show you some text, newline and when you try to input something it doesn't gi