<?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>Ex nihilo nihil fit &#187; Expert</title>
	<atom:link href="http://victorhurdugaci.com/category/tutorial/tut-expert/feed/" rel="self" type="application/rss+xml" />
	<link>http://victorhurdugaci.com</link>
	<description>Victor Hurdugaci on software development</description>
	<lastBuildDate>Wed, 21 Jul 2010 11:39:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Modifying .NET binaries – Part 2</title>
		<link>http://victorhurdugaci.com/modifying-net-binaries-part-2/</link>
		<comments>http://victorhurdugaci.com/modifying-net-binaries-part-2/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 11:16:39 +0000</pubDate>
		<dc:creator>Victor</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Expert]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[CFF Explorer]]></category>
		<category><![CDATA[Cracking]]></category>
		<category><![CDATA[IL]]></category>
		<category><![CDATA[WinDbg]]></category>

		<guid isPermaLink="false">http://victorhurdugaci.com/?p=2122</guid>
		<description><![CDATA[Description Objective Remove the &#8216;Trial protection&#8217; from an obfuscated .NET application Tools WinDbg CFF Explorer Target audience Advanced users The second part of the article discusses how to modify binaries that are obfuscated. For simplicity and clarity, I will not use obfuscated binaries. Doing this, allows the reader to understand what is actually happening. In [...]]]></description>
			<content:encoded><![CDATA[<table class="tutorial-description" border="1" cellspacing="0">
<tbody>
<tr>
<th colspan="2">Description</th>
</tr>
<tr>
<td class="header-column">Objective</td>
<td>Remove the &#8216;Trial protection&#8217; from an obfuscated .NET application</td>
</tr>
<tr>
<td class="header-column">Tools</td>
<td>
<ul>
<li>WinDbg</li>
<li>CFF Explorer</li>
</ul>
</td>
</tr>
<tr>
<td class="header-column">Target audience</td>
<td>Advanced users</td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">The second part of the article discusses how to modify binaries that are obfuscated. For simplicity and clarity, I will not use obfuscated binaries. Doing this, allows the reader to understand what is actually happening. In the demo I will completely ignore the name of the methods or the actual, non-obfuscated, code.</p>
<p style="text-align: justify;">I recommend reading the <a href="http://victorhurdugaci.com/modifiying-net-binaries-part-1/" target="_blank">first part</a>, if you didn&#8217;t already. It provides some information that might be needed to understand theis second part.</p>
<p style="text-align: justify;">The same &#8216;TrialApp.exe&#8217; binary is used. The current approach, as opposed the the former one, is:</p>
<ol style="text-align: justify;">
<li>Load the application in debugger and break the execution when the trial message is displayed.</li>
<li>Get the call stack</li>
<li>Find the address of the trial check method</li>
<li>Remove the call</li>
</ol>
<h4 style="text-align: justify;">1. Load the application in debugger and break the execution when the trial message is displayed</h4>
<p style="text-align: justify;">WinDbg can be obtained for free from Windows SDK (see the Microsoft Downloads website). If you are running a 64 bit OS, make sure you start the 32bit version of WinDbg (should be in Program Files (x86)).</p>
<p style="text-align: justify;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg01.jpg" rel="lightbox[2122]"><img class="alignright size-thumbnail wp-image-2130" title="WinDbg01" src="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg01-150x101.jpg" alt="" width="150" height="101" /></a>Load &#8216;TrialApp.exe&#8217; in WinDbg by clicking File -&gt; Load Executable. In order to run it you have 3 options:</p>
<ol style="text-align: justify;">
<li>Type &#8216;g&#8217; and press ENTER</li>
<li>Press F5</li>
<li>Click Debug -&gt; Go</li>
</ol>
<p style="text-align: justify;">The application will start and the execution will stop when the message box is displayed. Is actually waiting for the user to click OK. At this point break the execution by pressing Debug -&gt; Break.</p>
<p style="text-align: center;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg02.jpg" rel="lightbox[2122]"><img class="aligncenter size-large wp-image-2131" title="WinDbg02" src="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg02-1024x689.jpg" alt="" width="717" height="482" /></a></p>
<p style="text-align: justify;">Before being able to debug the .NET application, 2 DLLs needs to be loaded. They help the debugger &#8216;understand&#8217; the .NET internals. The actual paths might differ on your configuration. Anyway, make sure you load the 32 bit version of these files (the 64 bit version are in the <em>Framework64 </em>folder). The <em>.load</em> command loads external libraries.</p>
<pre class="brush: plain; light: true;">.load c:\Windows\Microsoft.NET\Framework\v4.0.30319\SOS.dll
.load c:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll</pre>
<h4><span id="more-2122"></span>2. Get the call stack</h4>
<p style="text-align: justify;">A call stack is associated with a thread. Before getting the stack we need to figure out which is the thread for which we want it. Execute the following command and inspect the output&#8230;</p>
<pre class="brush: plain; light: true;">!threads</pre>
<p style="text-align: center;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg03.jpg" rel="lightbox[2122]"><img class="size-large wp-image-2132  aligncenter" title="WinDbg03" src="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg03-1024x689.jpg" alt="" width="717" height="482" /></a></p>
<p style="text-align: justify;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg03.jpg" rel="lightbox[2122]"></a>There are two thread having IDs 0 and 2. Is quite easy to decide which is the main thread since just one of them is Single Thread Apartment (STA). Switch to the main thread and display the CLR stack using the following commands:</p>
<pre class="brush: plain; light: true;">~0s
!clrstack</pre>
<p style="text-align: center;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg04.jpg" rel="lightbox[2122]"><img class="aligncenter size-large wp-image-2133" title="WinDbg04" src="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg04-1024x689.jpg" alt="" width="717" height="482" /></a></p>
<h4>3. Find the address of the trial check method</h4>
<p style="text-align: justify;">OK! You&#8217;re still with me? If yes, then take a look at the result of the last command. It displays the call stack of the main thread. Notice that OnCreateControl calls OnLoad, OnLoad calls From1_Load, etc. In the case of obfuscated code, the name would probably be strange and you would have to analyze each method in depth. Because the code was JIT compiled the call to the trial check was inlined.</p>
<p style="text-align: justify;">Let&#8217;s take a look at the IL code for Form1_Load. To do this, first we need the address description of the MethodDesc structure of method. The <em>ip2md</em> command returns the structure. The argument is the IP address of the method. After this, just dump the IL for the address specified in MethodDesc. I want to make on observation here: if you look at the MethodDesc structure you can see the <em>mdToken</em> field. This field specified the table and the row in the table for the this method (the row corresponding to this method is the 6<sup>th</sup>, because the index starts at 0).</p>
<pre class="brush: plain; light: true;">!ip2md 003f01f9
!dumpil 00176304</pre>
<p style="text-align: center;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg05_details.jpg" rel="lightbox[2122]"><img class="aligncenter size-large wp-image-2135" title="WinDbg05_details" src="http://victorhurdugaci.com/wp-content/uploads/2010/07/WinDbg05_details-1024x689.jpg" alt="" width="717" height="482" /></a></p>
<p style="text-align: justify;">In case of obfuscated code, you would probably see just a call instruction to some cryptic method. It makes no difference. We can see that at IL_0001 (relative to the start of the method) we have a call and this instruction uses 5 bytes in the file (0006-0001 = 0005; in hex)).</p>
<p style="text-align: justify;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/07/CFF01.jpg" rel="lightbox[2122]"><img class="alignright size-thumbnail wp-image-2126" title="CFF01" src="http://victorhurdugaci.com/wp-content/uploads/2010/07/CFF01-150x101.jpg" alt="" width="150" height="101" /></a>Having the size of the instruction, its position and the row of the method in the <a href="http://msdn.microsoft.com/en-us/magazine/cc163791.aspx#S8" target="blank">methods table</a> we can proceed further. Open CFF Explorer and load the assembly.</p>
<p style="text-align: justify;">Navigate to .NET Directory -&gt; MetaData Streams -&gt; #~ -&gt; Tables. Look for the Method table in the new tree and select the entry with number 5. Copy its RVA value.</p>
<h4>4. Remove the call</h4>
<p style="text-align: justify;">With the RVA in hand (on clipboard :-) ), remove the call just like in the first part of the article. Replace the call bytes with zeros. One observation: we must also remove the instruction before the call (ldarg_0; opcode 02; no arguments). So, zero 6 bytes starting at the first in the method.</p>
<p style="text-align: center;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/07/CFF02.jpg" rel="lightbox[2122]"><img class="aligncenter size-large wp-image-2127" title="CFF02" src="http://victorhurdugaci.com/wp-content/uploads/2010/07/CFF02-1024x692.jpg" alt="" width="717" height="484" /></a></p>
<p style="text-align: justify;">In other words, replace:</p>
<pre class="brush: plain; light: true;">00 00 0A 02 28 08 00 00 06 2A 1E 02 28 06 00 00
06 2A 66 02 7B 02 00 00 04 2C 10 72 01 00 00 00</pre>
<p>with</p>
<pre class="brush: plain; light: true;">00 00 0A 02 28 08 00 00 06 2A 1E 00 00 00 00 00
00 2A 66 02 7B 02 00 00 04 2C 10 72 01 00 00 00</pre>
<p style="text-align: justify;">Run the application. The trial check is gone.</p>
]]></content:encoded>
			<wfw:commentRss>http://victorhurdugaci.com/modifying-net-binaries-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Modifying .NET binaries &#8211; Part 1</title>
		<link>http://victorhurdugaci.com/modifiying-net-binaries-part-1/</link>
		<comments>http://victorhurdugaci.com/modifiying-net-binaries-part-1/#comments</comments>
		<pubDate>Sun, 30 May 2010 17:11:00 +0000</pubDate>
		<dc:creator>Victor</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Expert]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[CFF Explorer]]></category>
		<category><![CDATA[Cracking]]></category>
		<category><![CDATA[IL]]></category>

		<guid isPermaLink="false">http://victorhurdugaci.com/?p=2076</guid>
		<description><![CDATA[The content of this post can be used for good and bad purposes. Modifying the source code to bypass trial/license checks is what crackers do in order to get paid software for free. Be advised that the purpose of this article is not to teach you how to steal. My target for this article are [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The content of this post can be used for good and bad purposes. Modifying the source code to bypass trial/license checks is what crackers do in order to get paid software for free. Be advised that the purpose of this article is not to teach you how to steal. My target for this article are the .NET developers who should understand what a cracker will (try to) do in order to get access to paid features.</p>
<p style="text-align: justify;">Before reading any further you should understand that each protection measure (as long as the cracker can access the source code) is useless. Is just a matter of time, for a motivated person, before she will bypass any protection.</p>
<p style="text-align: justify;">For the demo, we are going to use a very simple Windows Forms Application that will display a message box with a trial message and will exit after that. The goal is to show a few techniques that will prevent the application from exiting (and will remove the trial message).</p>
<p style="text-align: justify;">The code for the &#8216;trial&#8217; application is kept in just one class. There is just one variable for checking the trial and we&#8217;ll consider that is always true &#8211; it makes no difference if there was a function call to determine if the trial has expired.</p>
<pre class="brush: csharp;">
public partial class Form1 : Form
{
    bool hasExpired = true;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        CheckTrialApp();
    }

    private void CheckTrialApp()
    {
        if (hasExpired)
        {
            MessageBox.Show(&quot;Trial has expired&quot;);
            Application.Exit();
        }
    }
}
</pre>
<p style="text-align: justify;">The binary used was compiled on the x86 Release configuration with VS2010 having .NET 4.0 as target framework. The IL Disassembler from VS2010 and a free application called <a href="http://www.ntcore.com/exsuite.php" target="blank">CFF Explorer</a> are used to view and edit the binary.</p>
<p style="text-align: justify;"><span id="more-2076"></span></p>
<p style="text-align: justify;">Opening the &#8216;TrialApp.exe&#8217; file (the target binary) in IL Dissasembler will reveal all the statements from each method. This is important but, more important is the <a href="http://stackoverflow.com/questions/2170843/va-virutual-adress-rva-relative-virtual-address" target="blank">RVA</a> of the method containing the trial check, the bytes for each statement and their position relative to the RVA.</p>
<p style="text-align: center;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/05/BytesExplained.jpg" rel="lightbox[2076]"><img class="aligncenter size-full wp-image-2087" title="BytesExplained" src="http://victorhurdugaci.com/wp-content/uploads/2010/05/BytesExplained.jpg" alt="" width="708" height="408" /></a></p>
<p style="text-align: justify;">By knowing the RVA you are able to navigate to that address using CFF explorer and locate the bytes for the calls. Even without seeing the actual bytes, one is able to locate the calls (and their length) by looking at the offsets (ie: the byte 2C is located 0006 bytes from the beginning of the implementation) &#8211; more on this in Part2.</p>
<p style="text-align: center;"><a href="http://victorhurdugaci.com/wp-content/uploads/2010/05/ILMapping.jpg" rel="lightbox[2076]"><img class="aligncenter size-large wp-image-2090" title="ILMapping" src="http://victorhurdugaci.com/wp-content/uploads/2010/05/ILMapping-1024x433.jpg" alt="" width="717" height="303" /></a></p>
<p style="text-align: justify;">Having access to all this information gives not one but many possibilities of bypassing the trial check:</p>
<ol style="text-align: justify;">
<li>Remove the two calls to <em>Application.Exit</em> and <em>MessageBox.Show.</em></li>
<li>Change the <em>if</em> check.</li>
<li>Remove the &#8216;CheckTrialApp&#8217; call from &#8216;Form1_Load&#8217;.</li>
</ol>
<p style="text-align: justify;">This post will cover just the first two possibilities, since the third is similar to the first.</p>
<h3>1. Remove the calls to <em>Exit </em>and <em>Show</em></h3>
<p style="text-align: justify;">The bytes from the method implementation:</p>
<pre class="brush: csharp; light: true;">
         02 7B 02 00 00 04 2C 10 72 01 00 00 70
28 16 00 00 0A 26 28 17 00 00 0A 2A
</pre>
<p style="text-align: justify;">A call to a method has the opcode 28. The next 4 bytes following the opcode represent the location of the method in the methods table (you can see this table using CFF explorer).</p>
<p style="text-align: justify;">Now here comes the magic: in order to remove the calls to <em>Exit </em>and <em>Show, </em>one must  replace with NOP, all the bytes associated with these methods. Basically we are going the introduce a NOP byte (00) for each byte in the call.</p>
<pre class="brush: csharp; light: true;">
         02 7B 02 00 00 04 2C 10 72 01 00 00 70
00 00 00 00 00 26 00 00 00 00 00 2A
</pre>
<p>That&#8217;s all. Save the file and the trial is bypassed.</p>
<h3>2. Change the <em>if</em> check</h3>
<p style="text-align: justify;">If you look in the disassembled IL you can see that at offset 0&#215;6 we have a <em>brfalse.s</em> opcode. This is a branch instruction that will branch to offset 0&#215;18 (IL_0018) if false. However, in the case of &#8216;TrialApp&#8217;, since <em>hasExpired</em> is always true, the branch will never take place and the code following it will be executed.</p>
<p style="text-align: justify;">In order to change the meaning of the code &#8211; in other words &#8220;give the trial message if the application has NOT expired&#8221; &#8211; the check will be changed. Currently, is checking against <em>false</em> using the instruction <em>brfalse.s</em>, having the opcode 2C. By looking on MSDN, the opcode for <em>brtrue.s</em> can be found: 2D. Replacing 2C with 2D will make the branch happen always.</p>
<p style="text-align: justify;">The method inside the binary, after replacing the <em>brfalse.s</em> opcode:</p>
<pre class="brush: csharp; light: true;">
         02 7B 02 00 00 04 2D 10 72 01 00 00 70
28 16 00 00 0A 26 28 17 00 00 0A 2A
</pre>
<p style="text-align: justify;">That&#8217;s all. The message box will not be displayed since the body of the <em>if</em> statement is no longer executed.</p>
<p style="text-align: justify;">There are some techniques that will make cracking difficult. Obfuscating the code is one of them. However, part 2 of this article will cover the modification of obfuscated binaries.</p>
]]></content:encoded>
			<wfw:commentRss>http://victorhurdugaci.com/modifiying-net-binaries-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Install custom firmware on Asus WL500G Premium</title>
		<link>http://victorhurdugaci.com/install-ddwrt-on-asus-wl500g-premium/</link>
		<comments>http://victorhurdugaci.com/install-ddwrt-on-asus-wl500g-premium/#comments</comments>
		<pubDate>Fri, 23 May 2008 08:25:25 +0000</pubDate>
		<dc:creator>Victor</dc:creator>
				<category><![CDATA[Expert]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Asus WL500G Premium]]></category>
		<category><![CDATA[Custom Firmware]]></category>
		<category><![CDATA[DD-WRT]]></category>
		<category><![CDATA[Mod]]></category>
		<category><![CDATA[Router]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://victorhurdugaci.wordpress.com/?p=50</guid>
		<description><![CDATA[If you are one of the Asus WL500G Premium owners than it is possible to have problems with an USB drive connected the the unit: the HDD won&#8217;t respond after a few MB written on disk and the unit restores it&#8217;s factory settings. Maybe you don&#8217;t have problems like the one above but I am [...]]]></description>
			<content:encoded><![CDATA[<p>If you are one of the <a href="http://www.asus.com/products.aspx?l1=12&amp;l2=43&amp;l3=0&amp;model=1121&amp;modelmenu=1">Asus WL500G Premium</a> owners than it is possible to have problems with an USB drive connected the the unit: the HDD won&#8217;t respond after a few MB written on disk and the unit restores it&#8217;s factory settings.</p>
<p>Maybe you don&#8217;t have problems like the one above but I am having problems with an Western Digital 500GB Essential Edition connected to the router so I decided to upgrade it&#8217;s firmware to a <a href="http://www.dd-wrt.com/dd-wrtv3/index.php">DD-WRT</a> version (v24 std final). It&#8217;s a small Linux distribution that can be installed on many routers.</p>
<p>It brings a lot of new features:</p>
<ul>
<li>Advanced banwidth management</li>
<li>DDNS support</li>
<li>Ad network support</li>
<li>Hotspot support</li>
<li>MAC Address Clone</li>
<li>Advanced DHCP options</li>
<li>Advanced Wireless configuration (mode, frequency, encryption, mac filter, etc)</li>
<li>XBOX and PSP connection</li>
<li>System log, traffic log, network map</li>
<li>SSH connection</li>
<li>A lot of filters for MAC, hostname, IP, etc.</li>
<li>Overclocking (!)</li>
<li>New web UI</li>
<li>Wake On Lan</li>
<li>etc.</li>
</ul>
<p>See the difference between the classic Asus firmware and DDWRT:</p>
<p><a href="http://www.phoronix.net/image.php?id=542&amp;image=asus_wl500g_s01_lrg"><img style="vertical-align:middle;" src="http://www.phoronix.net/image.php?id=542&amp;image=asus_wl500g_s01_lrg" alt="Default Asus firmware" width="200" height="150" /></a></p>
<p><a href="http://victorhurdugaci.com/wp-content/uploads/2008/05/ddwrt.jpg" rel="lightbox[20]"><img class="alignnone size-thumbnail wp-image-60" src="http://victorhurdugaci.com/wp-content/uploads/2008/05/ddwrt.jpg?w=128" alt="DD-WRT Firware" width="202" height="148" /></a></p>
<p>This guide is for Asus but a similar process can be applied to any router from the <a href="http://www.dd-wrt.com/dd-wrtv3/dd-wrt/hardware.html">Supported Hardware List</a>. Step 1 and 2 are different but the others are the same.</p>
<p><strong>Very important: </strong>Before you read any further and/or try a firmware upgrade note that I am not responsible for any hardware/software/human (hope not) damages/injuries. Try to be sure that you have a backup supply for the router and PC because a power shortage in the middle of the upgrade process makes your router useless!</p>
<p><span id="more-20"></span></p>
<p>I recommend to read the entire guide before you try anything.</p>
<p>Before you begin make sure that you have the following:</p>
<ol>
<li>Asus WL500G Premium :)</li>
<li>Windows Xp (I was not able to upgrade from Vista &#8211; Asus utilities are quite stupid)</li>
<li>A pencil or pin because the reset button is almost impossible to be pressed with the finger.</li>
<li>The original firmware from <a href="http://support.asus.com/download/download.aspx?SLanguage=en-us&amp;model=WL-500gP">Asus Download</a> (in case of upgrade failure)</li>
<li>DD-WRT Firmware. Today, when I&#8217;m writing this post, the latest version is v24 final. Download it from <a href="http://www.dd-wrt.com/dd-wrtv3/dd-wrt/downloads.html">here</a>. I recommend &#8220;std generic&#8221; version.</li>
<li>Asus Utility from the router CD or Asus download center.</li>
<li><a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/">Putty</a> or some other telnet client.</li>
<li>Network cable (upgrade cannot be done by wireless!)</li>
<li>Administrative rights on the workstation connected to the router.</li>
<li>Some UPS for router and PC.</li>
<li>Internet connection.</li>
<li>Optional: external USB Drive (HDD, memory stick or card reader with card)</li>
</ol>
<p><strong>Step 1 (preparing the router):</strong></p>
<p>Reset the router to it&#8217;s default settings by holding the reset button &#8211; on the back of the unit &#8211; pressed for aprox. 5 seconds until the power led flashes.</p>
<p>Connect the network cable to router (one of the LAN ports) and PC. Set your PC IP address to 192.168.1.1 . The last &#8220;1&#8243; (one) can be replaced with any number between 1 and 254.</p>
<p><strong>Step 2 (installing the ddwrt firmware):</strong></p>
<p>Disable the firewall on the PC from which you are upgrading and start Asus Firmware Restoration with administrative rights.</p>
<p>Now we need the router in Restore Mode. Unplug the power cord; press and hold (!!) the reset button while plugging the power cord. The power led should blink slowly.</p>
<p>In the firmware restoration utility select the bin file downloaded from ddwrt, choose upload and hope for success. <strong>Do not</strong> interrupt the process!!</p>
<p>After the process is complete the unit reboots. Start a web browser and type http://192.168.1.1 . If you see a popup window asking for credentials than you have done it. If not&#8230; you are quite unlucky :(</p>
<p>The default credentials for DDWRT are root/admin (user/password).</p>
<p><strong>Step 3 (web interface configuration from DDWRT):</strong></p>
<p>Configure whatever you need in the web interface and set the internet connection.</p>
<p>Asus WL500G Premium has 8 MB of ROM memory and the std version of the firmware uses ~3.6. We need to be able to use the rest of the space for the installation of drivers and other needed application.</p>
<p>The remaining space from ROM is called JFFS (Journalling Flash File System) and needs to be cleared before you can write on it.</p>
<p>Log in to the web interface of the router, go to Administration tab and choose on JFFS2: Enable and on Clean JFFS2: Enable. Apply the settings and reboot the router.</p>
<p>You should see, after reboot, that JFFS2 has some free space (the value might differ).</p>
<p><img src="http://www.croccobiscotto.it/wp-content/uploads/2007/05/jffs2.jpg" alt="JFFS Free Space" width="377" height="122" /></p>
<p><strong>Step 4 (installing software &#8211; telnet)</strong></p>
<p>Install and start Putty. On protocol choose Telnet and type the IP of the router (default is 192.168.1.1). When asked for credentials, the user name is &#8220;root&#8221; whether you changed it or not and the password is the one that you have set from web interface or &#8220;admin&#8221; by default.</p>
<p>ipkg is the application used for software installation and upgrade. The first thing that must be done is to update this application. For this type &#8220;<em>ipkg update</em>&#8221; in the console. If errors appear make sure that you are connected to Internet and restart the router.</p>
<p><strong>Step 4.1 (USB and ext3 drivers):</strong></p>
<p>Because ddwrt does not include USB drivers we need to manually install them. For this type the following command &#8220;<em>ipkg -force-depends install kmod-usb-core kmod-usb2 kmod-usb-storage</em>&#8220;. If this command fails try it again &#8211; I had problems with it the first time.</p>
<p>If you are planning to add an USB drive that this one must be formated as ext2, ext3 or fat32. I recommend ext3 and if the drive is an HDD partition it like this: first partition 1 GB for optware (you&#8217;ll see in a few moments what is this) and the rest as one big partition. Let&#8217;s install the ext3 drivers &#8220;<em>ipkg install kmod-ext3&#8243;</em>. Replace ext3 with &#8220;ext2&#8243; or &#8220;vfat&#8221; for other file systems.</p>
<p><strong>Step 4.2 (prerequisites):</strong></p>
<p>Not needed if no USB storage drive is available or don&#8217;t want Optware.</p>
<p>We are going to use the first partition from the HDD to store optware. For this, mount the partition in the opt folder: &#8220;<em>mount -t ext3 -o noatime /dev/discs/disc0/part1 /opt</em>&#8220;.</p>
<p>Let&#8217;s install Optware: &#8220;<em>wget <span class="external text">http://pastebin.ca/raw/876251</span> -O &#8211; | tr -d &#8216;\r&#8217; &gt; /tmp/optware-install.sh</em>&#8221; and then &#8220;<em>sh /tmp/optware-install.sh</em>&#8220;.</p>
<p>Output should be like this:</p>
<pre>Checking system config ...
Using 192.168.1.1 as default gateway.
Using the following nameserver(s):
nameserver 192.168.1.30
Warning: local nameserver is different than gateway!
Check config or enter:
  sed -i s/192.168.*/192.168.1.1/ /tmp/resolv.conf
to correct this.
Installing package uclibc-opt_0.9.28-13_mipsel.ipk ...
Connecting to ipkg.nslu2-linux.org[140.211.166.82]:80
uclibc-opt_0.9.28-12 100% |***********************************************|   832 KB 00:00:00 ETA
Updating /opt/etc/ld.so.cache
/opt/sbin/ldconfig: can't create /opt/etc/ld.so.cache~ (No such file or directory)
Installing package ipkg-opt_0.99.163-9_mipsel.ipk ...
Connecting to ipkg.nslu2-linux.org[140.211.166.82]:80
ipkg-opt_0.99.163-9_ 100% |***********************************************| 75896    00:00:00 ETA
Downloading <a class="external free" title="http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/Packages.gz" rel="nofollow" href="http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/Packages.gz">http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/Packages.gz</a>
Inflating <a class="external free" title="http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/Packages.gz" rel="nofollow" href="http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/Packages.gz">http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/Packages.gz</a>
Updated list of available packages in /opt/lib/ipkg/lists/optware
Successfully terminated.
Installing uclibc-opt (0.9.28-12) to /opt/...
Downloading <a class="external free" title="http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/uclibc-opt_0.9.28-12_mipsel.ipk" rel="nofollow" href="http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/uclibc-opt_0.9.28-12_mipsel.ipk">http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/uclibc-opt_0.9.28-12_mipsel.ipk</a>
package uclibc-opt suggests installing ipkg-opt
Configuring uclibc-opt
Updating /opt/etc/ld.so.cache
Successfully terminated.
Installing ipkg-opt (0.99.163-9) to /opt/...
Downloading <a class="external free" title="http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/ipkg-opt_0.99.163-9_mipsel.ipk" rel="nofollow" href="http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/ipkg-opt_0.99.163-9_mipsel.ipk">http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable/ipkg-opt_0.99.163-9_mipsel.ipk</a>
Configuring ipkg-opt
Successfully terminated.</pre>
<p><em></em></p>
<p><strong>Step 4.3 (create a startup script that loads the modules):</strong></p>
<p>This step is not necessarily if you don&#8217;t want to share a printer and/or usb storage drive from router.</p>
<p>Now we have installed the modules but we want them to load when the router starts. Also we are going to mount the partitions and load any optware application from the /opt folder.</p>
<blockquote><p><em>Originally created for the Linksys NSLU2 Unslung firmware, Optware is the name of the additional software packages available.</em></p></blockquote>
<p>Start a telnet session. We are going to use &#8220;vi&#8221; text editor. A few vi commands that we are going to use are:</p>
<ul>
<li>press &#8220;i&#8221; once to enter the edit mode.</li>
<li>press &#8220;esc&#8221; once to exit de edit mode.</li>
<li>press &#8220;d&#8221; + &#8220;d&#8221; while not in edit mode to delete a line.</li>
<li>type &#8220;:wq&#8221; while not in edit mode to exit and save.</li>
</ul>
<p>For additional commands see <a href="http://www.cs.colostate.edu/helpdocs/vi.html">http://www.cs.colostate.edu/helpdocs/vi.html</a>.</p>
<p>First create a folder named &#8220;config&#8221; in /jffs/etc with the command &#8220;<em>mkdir /jffs/etc/config</em>&#8220;.</p>
<p>Now in this folder we are going to place our startup script (named &#8220;usb.startup&#8221;). Let&#8217;s create a new file with vi: &#8220;<em>vi /jffs/etc/config/usb.startup</em>&#8220;<em>. </em>Please note that anything that is after a &#8220;#&#8221; until the end of the line is comment and will be ignored; you can also not write that lines. <em></em>Press &#8220;i&#8221; to enter the edit mode and type the following:</p>
<pre>(
unset LD_PRELOAD
export PATH=/bin:/usr/bin:/sbin:/usr/sbin
export LD_LIBRARY_PATH=/lib:/usr/lib
echo "Inserting modules for USB disk support..."
insmod  /jffs/lib/modules/2.4.30/usbcore.o
insmod  /jffs/lib/modules/2.4.30/ehci-hcd.o
insmod  /jffs/lib/modules/2.4.30/scsi_mod.o
insmod  /jffs/lib/modules/2.4.30/usb-storage.o
insmod  /jffs/lib/modules/2.4.30/sd_mod.o
insmod  /jffs/lib/modules/2.4.30/jbd.o
#Change the following line is using another filesystem or remove it if you don't have external drive
insmod  /jffs/lib/modules/2.4.30/ext3.o
echo "Waiting for modules to initialize disk access..."
sleep 20
export LD_LIBRARY_PATH=/jffs/lib:/jffs/usr/lib
export PATH=/jffs/bin:/jffs/sbin:/jffs/usr/sbin:/jffs/usr/bin:/bin:/sbin
echo "Mountint disk partitions..."
export LD_LIBRARY_PATH=/lib:/usr/lib
mount -t ext3 -o noatime /dev/discs/disc0/part1 /opt
mount -t ext3 -o noatime /dev/discs/disc0/part2 /mmc
# provide Optware search paths
unset LD_LIBRARY_PATH
export PATH=/opt/bin:/opt/sbin:/bin:/sbin:/usr/sbin:/usr/bin
echo "Starting Optware programs..."
if [ -d /opt/etc/init.d ]; then
    for f in /opt/etc/init.d/S* ; do
        [ -x $f ] &amp;&amp; $f start
    done
fi
) &gt; /tmp/optware.log 2&gt;&amp;1</pre>
<p>Press &#8220;esc&#8221; and type &#8220;:wq&#8221; to save and exit.</p>
<p>The result of the script can be seen, after reboot in &#8220;/tmp/optware.log&#8221;. Just open this file with vi.</p>
<p>Just making this file will not load it at startup. We need to mark it as executable: &#8220;<em>chmod +x /jffs/etc/config/usb.startup</em>&#8220;.</p>
<p>After making this, go to the web interface in Administration -&gt; Commands and type:</p>
<pre>for I in `/bin/ls /jffs/etc/config/*.startup`
do
    sh $I &amp;
done</pre>
<p>Choose &#8220;Save startup&#8221;. Now the router will load any file ending with &#8220;.startup&#8221; from /jffs/etc/config.</p>
<p><strong>Step 5 (almost done &#8211; installing Samba):</strong></p>
<p>Samba is an application that will allow you to share the USB drive connected to the router as a network drive. (cool, huh? :) )</p>
<p>Reboot your router and then after this start a telnet session. Type the following commands, one at a time:</p>
<pre>/opt/bin/ipkg update
/opt/bin/ipkg install samba2
/opt/bin/ipkg install xinetd
/opt/etc/init.d/S10xinetd start
/opt/etc/init.d/S80samba start</pre>
<p>Reboot.</p>
<p><strong>Step 6 (configure Samba):</strong></p>
<p>Samba&#8217;s default port is 901 so type in a browser &#8220;http://<em>your.router.ip</em>:901&#8243; (default is http://192.168.1.1:901&#8243;) and the credentials are &#8220;root&#8221; for user, whether you changed it or not, and the password is the one you provided in ddwrt web interface (default is &#8220;admin&#8221;).</p>
<p><a href="http://www.dd-wrt.com/wiki/images/9/9f/Swat-globals.png" rel="lightbox[20]"><img src="http://www.dd-wrt.com/wiki/images/9/9f/Swat-globals.png" alt="Samba" width="160" height="200" /></a></p>
<p>In the globals tab choose &#8220;root&#8221; for the guest account and press &#8220;Commit changes&#8221;. Not doing this will not allow you to write to the network drive.</p>
<p>Now make folders and share them :)</p>
<p>Done.</p>
<p><strong>Step 7 (troubleshooting):</strong></p>
<p>If you have any problems please feel free to contact me or write on <a href="http://www.dd-wrt.com/phpBB2/">ddwrt forum</a>. Some more detailed guides and advanced topics can be found <a href="http://www.dd-wrt.com/wiki/index.php/Main_Page">here</a>.</p>
<p><a href="http://www.dd-wrt.com/wiki/index.php/Installation">DD-WRT Installation on other devices</a></p>
<p><a href="http://www.dd-wrt.com/wiki/index.php/USB_storage">DD-WRT USB Storage Guide</a></p>
<p><a href="http://www.dd-wrt.com/wiki/index.php/Optware">Optware Installation Guide</a></p>
]]></content:encoded>
			<wfw:commentRss>http://victorhurdugaci.com/install-ddwrt-on-asus-wl500g-premium/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
