# Google dorking to SQL injection

#### **Greetings Hackers, In this write-up I'll declare how I found boolean based SQL injection on a Microsoft Server web app from google dorking, So let's start.**

The program I'm talking about is a private program so I will refer to it as `vuln.com` .

### Getting The Sub-domain

After using tools like `amass` and `subfinder` I got some decent results, however I wanted to use google dorking, more specific **Bing Dorking.** I used the following dork: `domainName+inurl:admin` in my case: `vuln+inurl:admin` notice that I didn't use the tld to get more and non exclusive results. The purpose of this dork is to search for admin keyword in any URL related to the domain so I could get any exposed admin panel.

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2FzGkP83TXmbA8Lj1ImOiZ%2Fimage.png?alt=media&#x26;token=ec4347b1-c93e-4547-b8e0-1084f5917fd5" alt=""><figcaption></figcaption></figure>

In the first page I found the following:

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2F2kim23UTwUL4hdTngEkK%2Fimage.png?alt=media&#x26;token=52773123-66ba-4185-b877-43b376dc1396" alt=""><figcaption></figcaption></figure>

I've found this subdomain before by using the tools I mentioned, but I didn't get anything except the main page, even after performing directory search I didn't reach this path. And here lies the power of dorking.

### Mapping The  Site

When I entered the page it was a normal HTML page with no functionalities. Before trying anything I thought of accessing the parent directory which is `/arcgis` and I got redirected to the following page :&#x20;

&#x20;

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2Ffc1WJEatiASwjaFYwpZI%2Fimage.png?alt=media&#x26;token=d7f7f3a3-1525-459f-a81f-4020c7fbe73c" alt=""><figcaption></figcaption></figure>

Directory indexing that reveals most of the services + the current version of `ArcGIS REST` , Nothing sensitive here so I decided to search with the current version for any CVEs and I found that It's actually vulnerable to `CVE 2012-4949` that says that this version is vulnerable to SQLi in the `where` parameter in the following URL:&#x20;

`/arcgis/rest/services/{Service-Name}/query?f=json&where=featured=true&returnGeometry=true&spatialRel=esriSpatialRelIntersects`

&#x20;The main problem here is that there were a lot of services and each one has other branching links and it was frustrating to search in every section. So I used `hakrawler` which is a web crawler to get me most of the links hoping to find any service that makes use of the `query` endpoint

`echo "http://sub.vuln.com/arcgis/rest/" | hakrawler -d 4 -subs -u` and it got me this&#x20;

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2F4SCkEvWCxayoDZdGEhqe%2Fimage.png?alt=media&#x26;token=2775a00f-d15c-4f91-9b51-78c7204ba4cd" alt=""><figcaption><p>Bingo</p></figcaption></figure>

### Analysing The Parameter

When I went to the URL I found this page :

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2FDh47Pk2LjQrrXRqmI9Gb%2Fimage.png?alt=media&#x26;token=c7b386af-de35-4183-b90a-40b43b3f92a8" alt=""><figcaption></figcaption></figure>

I can see input field for the vulnerable parameter which is a good sign, The CVE didn't contain any info nor exploits, It only said the the parameter was vulnerable.&#x20;

So I tried the very basic payload to analyse It's behavior .... a single quote `'`&#x20;

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2FOMSE7herTQMQa6KoSwn7%2Fimage.png?alt=media&#x26;token=ad808d27-974e-4e34-b867-364b96bac71d" alt=""><figcaption></figcaption></figure>

Seems a promising result, Now let's try to cope with the query logic. We all know that `where` statement usually deals with boolean values , for example `where user='admin'` right ? what if we add this to our query&#x20;

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2FDhe2h3JSsSpSCiWrplPa%2Fimage.png?alt=media&#x26;token=faaf2ecd-4c84-4941-a814-8f7a641a3590" alt=""><figcaption></figcaption></figure>

From this detailed error we can notice that we are dealing with SQL Server and also no column called user. There was a parameter called `f` which specifies the format of the response , so let's set it to json and use burp for the ease of illustrating and exploiting

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2F79IC3NuIsZvr4xoKY0Qk%2Fimage.png?alt=media&#x26;token=b33b065f-b733-4aa0-ad12-535dff7bfea1" alt=""><figcaption><p>Here we go ...</p></figcaption></figure>

Now .... since we don't know any column name, what about entering a true value like `1=1` so the whole query is translated to `where 1=1` :&#x20;

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2FIAig7rxSznasDrDmANga%2Fimage.png?alt=media&#x26;token=5bd62185-229b-43b6-8b76-1e55ec8685b9" alt=""><figcaption><p>Response of 1=1</p></figcaption></figure>

Let's change the condition to a false one like `1=2` :&#x20;

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2FnSDHVd1qQaUX4SF4NvHw%2Fimage.png?alt=media&#x26;token=0d75a337-897d-48ee-91cd-4a7115d47c48" alt=""><figcaption><p>Response of 1=2</p></figcaption></figure>

This is confirmation of Boolean based SQLi as the response changes with changing the condition.

I didn't want to report it this way as I wanted to extract actual data.

### Exploiting The Parameter

Now we know that we are dealing with `MSSQL` and I'm not really good with it so I used\
[PayloadsAllTheThings](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/MSSQL%20Injection.md) and [Portswigger CheatSheat](https://portswigger.net/web-security/sql-injection/cheat-sheet) to conduct my payload.

I found that `user_name()` is quite good enough to be a POC , the main syntax is as follow:

```
SELECT user_name()
```

First I need to determine it's length, since we are dealing with boolean based we will depend on the response to know the length

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2F09ncnMLsLDBBfTDSd5Ru%2Fimage.png?alt=media&#x26;token=3773bfb6-9ce1-4284-858b-a8a90accec60" alt=""><figcaption><p>Request</p></figcaption></figure>

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2FZofJNz9Vu47WO4yZJ6iq%2Fimage.png?alt=media&#x26;token=c5304cf9-877b-4575-a8cf-25c1b8913390" alt=""><figcaption><p>Response</p></figcaption></figure>

After not so long tries I found that the length is 8.

Now to get the actual value of it, I used `SUBSTRING()` function as follow:

```
query?where=SUBSTRING((select+user_name()),1,1)='CHAR'
```

This will select the `user_name()` and then gets the first character of it and compares it with the provided character. To get the values I would send the request to the intruder and brute-force it 8 times **BUT** I'm kind of person that always wants to mess with things and break them down,  so I thought of providing a digit instead of character to see what happens:&#x20;

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2Fcy64hnr7Dj5QUl1YxNHh%2Fimage.png?alt=media&#x26;token=063bde1d-9e58-4eb5-94a3-5caacf8904e5" alt=""><figcaption><p>Response</p></figcaption></figure>

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2FkFzBBTw8CavKOxz3GgTt%2Fimage.png?alt=media&#x26;token=22005019-edb1-4471-8b6d-e52b45fa014c" alt=""><figcaption><p>Request</p></figcaption></figure>

This was really surprising for me, The DBMS actually responded with the character value due to conversion error, so instead of brute-forcing the rest of the characters I changed the payload to be :

```
query?where=SUBSTRING((select+user_name()),1,8)=0
```

This will get the first 8 characters because it's the length of the username and the result was:&#x20;

<figure><img src="https://49018334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQa5lZLJzKij2hNo1ER5S%2Fuploads%2FiRQfY9QD7RvIO8Y9ZThg%2Fimage.png?alt=media&#x26;token=e62d540f-5b77-4bb5-9d3c-4d624e64af21" alt=""><figcaption></figcaption></figure>

Got the username of the database, other things could be extracted like `version` , `tables names` , etc ... but I stopped here, I reported it as high severity but the triage team changed it to critical.

### Final

shout out to [@GodfatherOrwa](https://twitter.com/GodfatherOrwa?t=Vv7isnHeC7jJ4ZFmi1OAug\&s=09) for his tip about using bing dorking.

As a final tip from me, Don't always rely on tools. I've passed the request the first time to SQLMap and it said that the parameter wasn't exploitable.

### &#x20;
