# Finding Sql Servers

## Unauthenticated

To find SQL servers from an unauthenticated user, we can use SQLCMD:

```
sqlcmd -L 
```

We can do the same with metasploit

```
msf > use auxiliary/scanner/mssql/mssql_ping
msf auxiliary(mssql_ping) > set RHOSTS Target_IP_or_CIDR_identifier
msf auxiliary(mssql_ping) > run
```

&#x20;PowerUpSQL

```
>> import-module .\PowerUpSQL.psd1
>> Get-SQLInstanceScanUDP
```

Other tools are&#x20;

* [OSQL](https://docs.microsoft.com/en-us/sql/tools/osql-utility?view=sql-server-2017).
* [SQLPing3](http://www.sqlsecurity.com/downloads).
* Nmap
* Nessus

## Local User

As a local user SQL Server instances can be identified by checking system services and registry settings.

```
>> Get-SQLInstanceLocal
```

## Domain User

SQL servers are automatically registered in AD with an associated service account. This is done to support Kerberos authentication. We can use SPN scanning like so:&#x20;

```
>> setspn -T domain -Q MSSQLSvc/*
```

or just use powerupsql again

```
>> Get-SQLInstanceDomain
```

Tools are:

* [setspn.exe](https://social.technet.microsoft.com/wiki/contents/articles/717.service-principal-names-spns-setspn-syntax-setspn-exe.aspx).
* [adfind.exe](http://www.joeware.net/freetools/tools/adfind/index.htm).
* [Get-Spn.psm1](https://github.com/nullbind/Powershellery/blob/master/Stable-ish/Get-SPN/Get-SPN.psm1).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kwcsec.gitbook.io/the-red-team-handbook/infrastructure/sql/ms-sql/finding-sql-servers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
