{"id":397,"date":"2024-03-24T01:41:24","date_gmt":"2024-03-24T01:41:24","guid":{"rendered":"https:\/\/jsec-rt.com\/?p=397"},"modified":"2024-07-08T22:53:51","modified_gmt":"2024-07-09T02:53:51","slug":"rt-lateral-movement-through-mssql-part-1","status":"publish","type":"post","link":"https:\/\/jsec-rt.com\/index.php\/en\/2024\/03\/24\/rt-lateral-movement-through-mssql-part-1\/","title":{"rendered":"RT &#8211; Lateral movement through MSSQL &#8211; Part 1"},"content":{"rendered":"\n<p>It is quite common to identify MSSQL instances in Active Directory environments, due to the easy integration with LDAP and respective authentication mechanisms, so these deployed instances can provide an attacker with different ways to obtain information or some way to compromise internal servers.<\/p>\n\n\n\n<p>In this post I will explain some ways to extract information from MSSQL databases and abuse certain configurations to perform lateral movement.<\/p>\n\n\n\n<p>The scenarios we will explore will be the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Listing internal directories of a server through MSSQL, identifying sensitive files.<\/li>\n\n\n\n<li>Use of public functions to force authentication of the server and a domain user to the attacker&#8217;s machine for NTLM Relay attacks.<\/li>\n\n\n\n<li>Remote code execution through MSSQL&#8217;s own functions.<\/li>\n<\/ul>\n\n\n\n<p>To emulate these attack vectors, we will need to deploy a couple of MSSQL databases on the test domain used in previous posts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MSSQL installation on internal servers.<\/h2>\n\n\n\n<p>For this lab, SQL Express will be enough, which can be downloaded from the following link:<\/p>\n\n\n\n<p><a href=\"https:\/\/download.microsoft.com\/download\/5\/1\/4\/5145fe04-4d30-4b85-b0d1-39533663a2f1\/SQL2022-SSEI-Expr.exe\">https:\/\/download.microsoft.com\/download\/5\/1\/4\/5145fe04-4d30-4b85-b0d1-39533663a2f1\/SQL2022-SSEI-Expr.exe<\/a><\/p>\n\n\n\n<p>Now I&#8217;ll be using a different lab, deployed recently on a NUC.<\/p>\n\n\n\n<p>This is the new infrastructure:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"734\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/jsec.drawio-2-1024x734.png\" alt=\"\" class=\"wp-image-380\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/jsec.drawio-2-1024x734.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/jsec.drawio-2-300x215.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/jsec.drawio-2-768x551.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/jsec.drawio-2.png 1131w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For this exercise, we will deploy two MSSQL instances in the child domain <strong>internal.jsec.rt<\/strong> and the parent domain <strong>jsec.rt<\/strong>, however, the configuration of links between the two databases will be for a next post.<\/p>\n\n\n\n<p>The steps are repeated for both machines, as this is a lab.<\/p>\n\n\n\n<p>*Note: Make sure you have installed .NET Framework 4.7.2.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"804\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-1024x804.png\" alt=\"\" class=\"wp-image-321\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-1024x804.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-300x235.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-768x603.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-1536x1205.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image.png 1858w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Choosing the <strong>Basic<\/strong> option is enough for our purposes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"790\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-1-1024x790.png\" alt=\"\" class=\"wp-image-322\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-1-1024x790.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-1-300x232.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-1-768x593.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-1-1536x1185.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-1.png 1858w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Accept the terms and conditions.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"795\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-2-1024x795.png\" alt=\"\" class=\"wp-image-323\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-2-1024x795.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-2-300x233.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-2-768x596.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-2-1536x1193.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-2.png 1852w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We choose the installation route and then click on <strong>Install<\/strong>.<\/p>\n\n\n\n<p>Once the installer finishes downloading and copying files, we&#8217;ll have a working MSSQL instance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing a Database management suite.<\/h2>\n\n\n\n<p>We&#8217;ll use the recommended Microsoft tool to interact with the database.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"778\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-5-1024x778.png\" alt=\"\" class=\"wp-image-326\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-5-1024x778.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-5-300x228.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-5-768x584.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-5-1536x1168.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-5.png 1860w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Installation can be done with the default values for the purpose of this lab.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Remote access configuration.<\/h2>\n\n\n\n<p>By default, the database is accessible only locally, to allow network access, we&#8217;ll follow these steps.<\/p>\n\n\n\n<p>Start the Database configuration app.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"824\" height=\"1024\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-6-824x1024.png\" alt=\"\" class=\"wp-image-327\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-6-824x1024.png 824w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-6-241x300.png 241w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-6-768x955.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-6-1236x1536.png 1236w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-6.png 1276w\" sizes=\"auto, (max-width: 824px) 100vw, 824px\" \/><\/figure>\n\n\n\n<p>We expand the <strong>SQL Server Network Configuration<\/strong> option and click on the <strong>Protocols<\/strong> tab, where we&#8217;ll modify the <strong>TCP\/IP<\/strong> Protocol.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"500\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-7-1024x500.png\" alt=\"\" class=\"wp-image-328\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-7-1024x500.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-7-300x146.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-7-768x375.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-7.png 1192w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We change the <strong>Enabled<\/strong> dropbox value to <strong>Yes<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-8-1024x683.png\" alt=\"\" class=\"wp-image-329\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-8-1024x683.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-8-300x200.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-8-768x513.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-8-1536x1025.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-8.png 1804w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Then, we switch tabs to <strong>IP Addresses<\/strong> and add the port where the database will listen for connections by modifying the value of <strong>TCP Port<\/strong> of the <strong>IPAll<\/strong> section.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"688\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-10-1024x688.png\" alt=\"\" class=\"wp-image-331\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-10-1024x688.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-10-300x202.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-10-768x516.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-10-1536x1033.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-10.png 1800w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Then, to restart the service and apply the changes, we access the <strong>SQL Server Services<\/strong> menu, right click on the <strong>SQL Server<\/strong> object and then click on <strong>Restart.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"327\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-11-1024x327.png\" alt=\"\" class=\"wp-image-332\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-11-1024x327.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-11-300x96.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-11-768x245.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-11-1536x490.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-11.png 1942w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, to confirm the service is listening on all interfaces, we can run <strong>netstat<\/strong> and <strong>findstr<\/strong> to filter the results, showing matches of the configured port.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"308\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-12-1024x308.png\" alt=\"\" class=\"wp-image-333\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-12-1024x308.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-12-300x90.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-12-768x231.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-12.png 1224w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>From the Kali host, we can confirm the service is remotely accesible using <strong>Impacket-mssqlclient<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"423\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-13-1024x423.png\" alt=\"\" class=\"wp-image-334\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-13-1024x423.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-13-300x124.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-13-768x317.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-13.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We get the hostname and the configured database as a reply, so now we can configure the vulnerabilities.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing the vulnerable environment.<\/h2>\n\n\n\n<p>Recapping the mentioned vulnerabilities.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Listing internal directories of a server through MSSQL, identifying sensitive files.<\/li>\n\n\n\n<li>Use of public functions to force authentication of the server and a domain user to the attacker&#8217;s computer for NTLM Relay attacks.<\/li>\n\n\n\n<li>Remote code execution through MSSQL stored procedures.<\/li>\n<\/ul>\n\n\n\n<p>For the first two functions, we do not need to do much, since it was installed on a Windows domain, regular users have very reduced query privileges, but any credential gives us access to public functions.<\/p>\n\n\n\n<p>But to replicate the behavior in corporate networks, we will register SPN tickets in the respective domains to allow authentication by Kerberos to the respective service.<\/p>\n\n\n\n<p>For this task, on our domain controller we will use the following command:<\/p>\n\n\n\n<p>setspn -S MSSQLSvc\/coreint.internal.jsec.rt internal.jsec.rt\\serveradmin<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"321\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-36-1024x321.png\" alt=\"\" class=\"wp-image-360\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-36-1024x321.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-36-300x94.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-36-768x241.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-36-1536x481.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-36.png 1698w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This way, the instance can be identified through Active Directory enumeration.<\/p>\n\n\n\n<p>For the third point, we will create a local database user with <strong>Sysadmin<\/strong> privileges, which is one of the requirements to enable the &#8220;<strong>xp_cmdshell<\/strong>&#8221; function.<\/p>\n\n\n\n<p>In the respective databases, we access the database manager.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"980\" height=\"1024\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-15-980x1024.png\" alt=\"\" class=\"wp-image-336\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-15-980x1024.png 980w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-15-287x300.png 287w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-15-768x802.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-15-1470x1536.png 1470w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-15.png 1516w\" sizes=\"auto, (max-width: 980px) 100vw, 980px\" \/><\/figure>\n\n\n\n<p>We access with the domain user that installed the database engine.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"787\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-18-1024x787.png\" alt=\"\" class=\"wp-image-339\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-18-1024x787.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-18-300x231.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-18-768x590.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-18-1536x1180.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-18.png 1580w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Once inside, we access the <strong>Security<\/strong> submenu and then <strong>Logins<\/strong>, right clicking it and accessing the <strong>New Login<\/strong> option.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1004\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-22-1024x1004.png\" alt=\"\" class=\"wp-image-343\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-22-1024x1004.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-22-300x294.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-22-768x753.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-22.png 1244w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>To add a domain user as a valid user with privileges in the database, click on <strong>Search<\/strong>, then click on the <strong>Locations<\/strong> button and in order to search for internal domain users, expand the parent domain <strong>jsec.rt<\/strong> and select the subdomain <strong>internal.jsec.rt<\/strong> where our test user resides.<\/p>\n\n\n\n<p>We write the user name to which we want to give privileges, in my case <strong>Sqluser<\/strong>, and we click on Check Names, to make sure that the user is being identified correctly.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"841\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-20-1024x841.png\" alt=\"\" class=\"wp-image-341\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-20-1024x841.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-20-300x246.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-20-768x631.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-20-1536x1262.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-20.png 1702w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We click on <strong>OK<\/strong> and move to the <strong>Server Roles<\/strong> tab, where we&#8217;ll give this user <strong>Sysadmin<\/strong> privileges by checking the corresponding box.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"806\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-21-1024x806.png\" alt=\"\" class=\"wp-image-342\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-21-1024x806.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-21-300x236.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-21-768x604.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-21-1536x1209.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-21.png 1756w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We click OK and our user is ready.<\/p>\n\n\n\n<p>From within our Kali host, we confirm the remote access using <strong>impacket-mssqlclient<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"317\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-23-1024x317.png\" alt=\"\" class=\"wp-image-344\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-23-1024x317.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-23-300x93.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-23-768x238.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-23.png 1512w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Its necessary to use the <strong>-windows-auth<\/strong> flag to be able to authenticate as operating system or domain users.<\/p>\n\n\n\n<p>To finish the configuration, we&#8217;ll add the machine account of the <strong>Coreint<\/strong> server as a local administrator on the <strong>DBInt<\/strong> server for the relay attack.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"617\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-24-1024x617.png\" alt=\"\" class=\"wp-image-346\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-24-1024x617.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-24-300x181.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-24-768x462.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-24-1536x925.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-24.png 2036w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>To exploit these scenarios, we&#8217;ll use the tool <strong>PowerUpSQL<\/strong> and a domain joined Windows host with a low privileged user (<strong>jsec<\/strong>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Attack path 1: Directory listing through MSSQL.<\/h2>\n\n\n\n<p>There are many cases where it is possible to identify MSSQL instances configured in the domain, for internal applications and others, for the case of Web applications hosted on a server with MSSQL, it is possible to discover the exposed directories and in some cases, identify sensitive files that could be downloaded.<\/p>\n\n\n\n<p>Simulating a real scenario, on a server with MSSQL I installed XAMPP to have a web server, creating a backup folder with a name that would be a bit complicated to find without bruteforcing on directories.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"345\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-26-1024x345.png\" alt=\"\" class=\"wp-image-349\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-26-1024x345.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-26-300x101.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-26-768x259.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-26.png 1334w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, we can use <strong>PowerUpSQL<\/strong> to discover MSSQL instances in the domain and use the public function &#8220;xp_dirtree&#8221; to list internal paths.<\/p>\n\n\n\n<p>Using a non domain joined host that has visibility towards the internal network, we confirm reachability to the domain controller.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"525\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-27-1024x525.png\" alt=\"\" class=\"wp-image-351\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-27-1024x525.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-27-300x154.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-27-768x393.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-27.png 1214w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Then, we import the script after setting the execution policy value to <strong>Bypass<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"264\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-28-1024x264.png\" alt=\"\" class=\"wp-image-352\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-28-1024x264.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-28-300x77.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-28-768x198.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-28.png 1286w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, to be able to consult the information to the domain, we need that the imported functions are executed from the context of a domain user, to make this, some of the simplest options are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a Credential object in Powershell, which is accepted by the tool.<\/li>\n\n\n\n<li>Execute a Powershell window using runas, to force the context of a different user.<\/li>\n\n\n\n<li>Add the respective machine to the domain, assuming that the MachineAccountQuota parameter is set to default values and that no restrictions on domain users have been implemented.<\/li>\n<\/ul>\n\n\n\n<p>The first two options are possible to validate quickly, so in this case, we will use runes to raise a shell in the context of a domain user.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"305\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-30-1024x305.png\" alt=\"\" class=\"wp-image-354\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-30-1024x305.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-30-300x89.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-30-768x229.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-30.png 1384w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Once the respective shell has been obtained and the access to the internal domain has been validated, we proceed to list the MSSQL instances registered in the domain.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"824\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-37-1024x824.png\" alt=\"\" class=\"wp-image-361\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-37-1024x824.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-37-300x241.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-37-768x618.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-37.png 1528w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We find two registered instances, where we&#8217;ll use a new function to test our connectivity.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"714\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-38-1024x714.png\" alt=\"\" class=\"wp-image-362\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-38-1024x714.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-38-300x209.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-38-768x535.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-38-1536x1071.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-38.png 1790w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Confirming the accessibility to these using any domain user, we proceed to list the internal directories using the public function &#8220;<strong>xp_dirtree<\/strong>&#8220;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"724\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-39-1024x724.png\" alt=\"\" class=\"wp-image-363\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-39-1024x724.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-39-300x212.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-39-768x543.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-39-1536x1086.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-39.png 1842w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We can read more about this function in the following link:<\/p>\n\n\n\n<p><blockquote class=\"wp-embedded-content\" data-secret=\"UTLelQaN3t\"><a href=\"https:\/\/www.sqlops.com\/what-is-xp_dirtree\/\">What is XP_DIRTREE?\u00a0 What are the alternatives to XP_Dirtree<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;What is XP_DIRTREE?\u00a0 What are the alternatives to XP_Dirtree&#8221; &#8212; SQLOPS\" src=\"https:\/\/www.sqlops.com\/what-is-xp_dirtree\/embed\/#?secret=Ti8WLj0TRB#?secret=UTLelQaN3t\" data-secret=\"UTLelQaN3t\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n\n\n\n<p>Now, it happened to me more than once to come across a web application that had an instance of MSSQL running, so I was able to use this feature to discover hidden directories and files.<\/p>\n\n\n\n<p>In this case, we will use XAMPP for simplicity.<\/p>\n\n\n\n<p>We can list the contents of the xampp folder to explore a bit, but the known path where web applications are hosted is in the htdocs folder.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"379\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-40-1024x379.png\" alt=\"\" class=\"wp-image-364\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-40-1024x379.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-40-300x111.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-40-768x284.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-40-1536x568.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-40.png 1958w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We find two folders that are not part of the default routes, so we&#8217;ll explore the <strong>backups<\/strong> folder.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"319\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-41-1024x319.png\" alt=\"\" class=\"wp-image-365\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-41-1024x319.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-41-300x94.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-41-768x239.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-41-1536x479.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-41.png 1982w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We observe the existence of the <strong>backups-old-2023-migracion<\/strong> folder, which would be very difficult to find using traditional methods of directory discovery in web applications.<\/p>\n\n\n\n<p>As there are no more folders inside this folder, we obtain an empty result with the query used previously, however, we can add a new value as a parameter in xp_dirtree, to show the files.<\/p>\n\n\n\n<p>In this case, the query executed would be: <strong>exec xp_dirtree &#8216;C:\\xampp\\htdocs\\backups\\backups-old-2023-migracion&#8217;,1,1&#8242;<\/strong>.<\/p>\n\n\n\n<p>Where the two parameters after the defined path refer to the depth of the file listing in the function and a flag to enable file listing, which is disabled by default.<\/p>\n\n\n\n<p>We can download the file that would not have been identified quickly with traditional methods using a low privilege user and taking advantage of a valid function in the MSSQL instance.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"624\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-42-1024x624.png\" alt=\"\" class=\"wp-image-366\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-42-1024x624.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-42-300x183.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-42-768x468.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-42-1536x936.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-42.png 1976w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Attack path 2: Public function usage to Coerce authentication to an attacker host for NTLM Relay attacks.<\/h2>\n\n\n\n<p>For this scenario, we can take advantage of the same function as above, or we can also use another function called xp_fileexist.<\/p>\n\n\n\n<p>This second function will simply validate that a specific file exists in the specified path or not.<\/p>\n\n\n\n<p>As an attacker, we can take advantage of the fact that both functions allow working with remote SMB paths, so it is possible to capture hashes or perform relay attacks.<\/p>\n\n\n\n<p>Validating IP addresses and SMB message signing is not required.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"212\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-43-1024x212.png\" alt=\"\" class=\"wp-image-367\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-43-1024x212.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-43-300x62.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-43-768x159.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-43-1536x318.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-43-2048x425.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We can do the visibility test, observing what kind of hashes and from which user could be captured using the <strong>xp_dirtree<\/strong> or <strong>xp_fileexist<\/strong> function.<\/p>\n\n\n\n<p>Deploying an smb server through Impacket.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"960\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-44-1024x960.png\" alt=\"\" class=\"wp-image-368\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-44-1024x960.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-44-300x281.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-44-768x720.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-44.png 1288w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We execute the public function <strong>xp_dirtree<\/strong> in <strong>Coreint<\/strong> towards the attacker host to capture the NetNTLMv2 Machine account hash.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"99\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-45-1024x99.png\" alt=\"\" class=\"wp-image-369\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-45-1024x99.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-45-300x29.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-45-768x74.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-45-1536x149.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-45.png 2000w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We note that the hash obtained belongs to the <strong>coreint$<\/strong> user, since the MSSQL instance is running under a service user.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"505\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-46-1024x505.png\" alt=\"\" class=\"wp-image-370\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-46-1024x505.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-46-300x148.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-46-768x379.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-46-1536x758.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-46.png 1622w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Confirming the possibility of capturing the <strong>CoreInt<\/strong> machine account hash, we can deploy the relay server pointing to <strong>DBINT<\/strong>, where we add the <strong>CoreInt<\/strong> team as local administrator.<\/p>\n\n\n\n<p>We run the query again and see that the local user hashes (SAM) are obtained.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"536\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-48-1024x536.png\" alt=\"\" class=\"wp-image-372\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-48-1024x536.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-48-300x157.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-48-768x402.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-48-1536x804.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-48-2048x1071.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We run the query again to obtain the local user&#8217;s NTLM hashes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"480\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-49-1024x480.png\" alt=\"\" class=\"wp-image-373\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-49-1024x480.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-49-300x140.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-49-768x360.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-49-1536x719.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-49-2048x959.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>As it was seen on previous posts, we could also execute commands or deploy a socks proxy.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"508\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-50-1024x508.png\" alt=\"\" class=\"wp-image-374\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-50-1024x508.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-50-300x149.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-50-768x381.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-50-1536x762.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-50-2048x1015.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Attack path 3: Remote code execution through MSSQL stored procedures.<\/h2>\n\n\n\n<p>The command execution is done through <strong>xp_cmdshell<\/strong>, a well known function, so we will show the exploitation using <strong>PowerUpSQL<\/strong>.<\/p>\n\n\n\n<p>In this case, to exploit this scenario, we need a privileged user in the database to be able to enable the respective function, in our case, it will be the domain user <strong>Sqluser<\/strong>, which we previously added as <strong>Sysadmin<\/strong>.<\/p>\n\n\n\n<p>We start the Powershell window under the context of this user, whose pass we could have found in scenario 1 (downloading a backup file in hidden web directories), validating that the credentials are correct when querying SPN tickets related to MSSQL databases.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"826\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-51-1024x826.png\" alt=\"\" class=\"wp-image-375\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-51-1024x826.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-51-300x242.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-51-768x620.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-51-1536x1240.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-51.png 1586w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Next, we need to validate if the user we have is really privileged, for which we can use the <strong>Get-SQLServerLinkCrawl<\/strong> function, which will query linked databases and if we have Sysadmin privileges.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"756\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-52-1024x756.png\" alt=\"\" class=\"wp-image-376\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-52-1024x756.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-52-300x221.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-52-768x567.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-52.png 1520w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The next post will talk about linked databases, so now we are only interested in the Sysadmin value which is set to 1, confirming that we are a privileged user.<\/p>\n\n\n\n<p>We could enable the xp_cmdshell function manually, for which there is plenty of documentation, but in summary, these would be the queries that we would have to run on the database using the privileged user.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>EXECUTE sp_configure &#8216;show advanced options&#8217;, 1;<\/li>\n\n\n\n<li>RECONFIGURE;<\/li>\n\n\n\n<li>EXECUTE sp_configure &#8216;xp_cmdshell&#8217;, 1;<\/li>\n\n\n\n<li>RECONFIGURE;<\/li>\n<\/ul>\n\n\n\n<p>Fortunately, PowerUpSQL has a function called Invoke-SQLOSCmd, which enables xp_cmdshell automatically and allows us to execute respective commands.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"404\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-53-1024x404.png\" alt=\"\" class=\"wp-image-377\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-53-1024x404.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-53-300x118.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-53-768x303.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-53-1536x605.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-53.png 1766w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>While it is possible to execute commands, it is not an interactive shell, so getting results or command information becomes complicated to understand.<\/p>\n\n\n\n<p>For example, if we run a simple ipconfig, we will see that the answer is automatically trimmed to fit on the screen, we could use the <strong>fl<\/strong> function to get the rest of the information, but as seen in the screenshot, it is still complicated to understand.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"554\" src=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-54-1024x554.png\" alt=\"\" class=\"wp-image-378\" srcset=\"https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-54-1024x554.png 1024w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-54-300x162.png 300w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-54-768x415.png 768w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-54-1536x830.png 1536w, https:\/\/jsec-rt.com\/wp-content\/uploads\/2023\/11\/image-54.png 1994w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>So in this case, the most recommended would be to deploy a reverse shell to be able to interact with the operating system, or add new users to the local administrator group, but this can be a bit complicated due to antivirus and EDR issues.<\/p>\n\n\n\n<p>In the following post I will show a little trick I developed to deploy a &#8220;portable&#8221; SSH server using a MSSQL database and a service account, which can be used later as a SSH tunnel to gain access to segmented networks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is quite common to identify MSSQL instances in Active Directory environments, due to the easy integration with LDAP and respective authentication mechanisms, so these deployed instances can provide an attacker with different ways to obtain information or some way to compromise internal servers. In this post I will explain <a href=\"https:\/\/jsec-rt.com\/index.php\/en\/2024\/03\/24\/rt-lateral-movement-through-mssql-part-1\/\" class=\"btn-link\">Continue Reading<i class=\"ion-ios-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[28,30],"class_list":["post-397","post","type-post","status-publish","format-standard","hentry","category-p-rt-en","tag-english","tag-rt"],"_links":{"self":[{"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/posts\/397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/comments?post=397"}],"version-history":[{"count":1,"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/posts\/397\/revisions"}],"predecessor-version":[{"id":428,"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/posts\/397\/revisions\/428"}],"wp:attachment":[{"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/media?parent=397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/categories?post=397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jsec-rt.com\/index.php\/wp-json\/wp\/v2\/tags?post=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}