środa, 11 sierpnia 2021

Bounty CORSare

Few days ago someone asked me about CORS-related vulnerabilities. I decided it will be a good idea o try to create a small tool. Below you'll find the whole story. Here we go...

Today we'll start here:

As the misconfiguration bug is already pretty good explained online[1, 2, 3] my main goal was to focus only on the script code. For a 'quick poc' I decided to stay with python3.

In my 'ideal scenario' what I was looking for was:
- http(s) service available on remote host
- modifying 'Origin' header
- checking for mentioned header echoe'd back in the response...
- ...as well as existence of "other headers" (ex. "Access-Control-Allow-Credentials: true").

So far - we should be somewhere here:

As you'll see (in the code) - script is 'very basic' and can be easily extended. I know. ;) But I'll leave it to you as an exercise. ;)

Continuing - I was wondering how to try CORSare.py with something 'more automated'. And that's how I landed on my previous post about the DNS enumeration:

So my next move was to recreate similar steps and prepare some "semi-automated CORS-scanning" tool ;)

Quick results you'll find below - preparing our 'target list':

Next, some grep's to grab only IP(s):

For now we can continue with the CORSare.py (in a simple bash-for-loop), like this:


After a while - we should be somewhere here:


If you'd like to check your server(s) using this code - below you'll find the current "full version":

---<cut>---

c@box:~/tools/CORSar$ base64 CORSare.py
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwojIENPUlNhcmUucHkgLSBtaXNjb25maWcgQ09SUyBmaW5k
ZXIKIyAKIyAxMC4wOC4yMDIxIEAgMTg6MzAgCiMgICAnTHVkemllLCBzdG9qYWN5IGphayBqYSAt
IG5hIGtyYXdlZHppIC0gbmllIGJvamEgc2llLCB6ZSBtb2dhIHNwYXNjLiAKIyAgICBUeWxrbywg
emUgbW9nYSBza29jenljLicKIyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgTWFyZ2luIENhbGwsIDIwMTEKIyAKCmltcG9ydCByZXF1ZXN0cwppbXBvcnQgc3lz
CmZyb20gdXJsbGliLnBhcnNlIGltcG9ydCB1cmxwYXJzZQpwb3J0cyA9IFs4MCwgNDQzLCA4NDQz
LCA4MDgwLCA4MTAwLCA4MTgwLCA5MTAwIF0KdGFyZ2V0ID0g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b3IgcG9ydCBpbiBwb3J0czoKICAgICAgICBwcmVwYXJpbmdfdGFyZ2V0
ID0gcGFyc2VkLnNjaGVtZSArICc6Ly8nICsgdGFyZ2V0ICsgJzonICsgc3RyKHBvcnQpCiAgICAg
ICAgcHJpbnQoIiAgWytdIHByZXBhcmVkIHBhcnNlZCB0YXJnZXQ6ICVzIiAlIHByZXBhcmluZ190
YXJnZXQpCiAgICAgICAgc2VuZF9tZShwYXJzZWQuc2NoZW1lLCB0YXJnZXQsIHN0cihwb3J0KSkK
CgplbHNlOgogICAgIyBwcmludCgicHJvYmFibHkgbm8gc2NoZW1lIGZvdW5kLCBzbyBjaGVja2lu
ZyBodHRwIGFzIGEgZGVmYXVsdCIpCgogICAgIyBwcmVwYXJpbmcgdGFyZ2V0IGZvciBwb3J0czoK
ICAgIGZvciBwb3J0IGluIHBvcnRzOgogICAgICAgIHByZXBhcmluZ190YXJnZXQgPSAnaHR0cHM6
Ly8nICsgdGFyZ2V0ICsgJzonICsgc3RyKHBvcnQpCiAgICAgICAgcHJpbnQoIiAgWytdIHByZXBh
cmVkIHBhcnNlZCAobm8gc2NoZW1lKSB0YXJnZXQ6ICVzIiAlIHByZXBhcmluZ190YXJnZXQpCiAg
ICAgICAgcGFyc2VkX3NjaGVtZSA9ICdodHRwcycKICAgICAgICBzZW5kX21lKHBhcnNlZF9zY2hl
bWUsIHRhcmdldCwgc3RyKHBvcnQpKQoKCgoKCgo=
c@box:~/tools/CORSar$

---</cut>---

Maybe you'll find it useful. ;)

In case of any questions - feel free to ask.

See you next time!

Cheers




Brak komentarzy:

Prześlij komentarz