<?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; Networking</title>
	<atom:link href="http://weblog.nomejortu.com/category/networking/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>howto create an intermediate Certificate Authority (CA) using openssl</title>
		<link>http://weblog.nomejortu.com/networking/howto-create-an-intermediate-certifica-authority-ca-using-openssl</link>
		<comments>http://weblog.nomejortu.com/networking/howto-create-an-intermediate-certifica-authority-ca-using-openssl#comments</comments>
		<pubDate>Thu, 20 Mar 2008 03:05:27 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=44</guid>
		<description><![CDATA[What is an Intermediate Certificate Authority (CA) and why do I need one? An Intermediate CA is an authority that you use to create your own SSL certificates in a PKI environment. An Intermediate CA depends on a Root CA that is the origin of the chain of trust. The idea is that if your [...]]]></description>
			<content:encoded><![CDATA[<p>What is an Intermediate Certificate Authority (CA) and why do I need one? An Intermediate CA is an authority that you use to create your own <acronym title="Secure Sockets Layer">SSL</acronym> certificates in a <acronym title="Public Key Infrastructure">PKI</acronym> environment. An Intermediate CA depends on a Root CA that is the origin of the chain of trust. The idea is that if your Intermediate CA gets compromised or you decide to revocate all the certificates issued by it, you can still use your Root CA without further inconvenience for your users (the users only need to have installed the certificate of the Root CA in their browsers).</p>
<p>As for the second question, the sort answer is that chances are that you really do not need one <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  but for the shake of the experiment lets get our hands dirty!<br />
<span id="more-44"></span><br />
First of all, I need to clarify that my interest in this topic was also risen by the fact that <a href="http://www.verisign.com/support/verisign-intermediate-ca/index.html">Verisign</a> has switched to a two-tier hierarchy of Certificate Authorities, and this has some implications specially in the configuration of web server software:</p>
<blockquote><p>
&#8220;As of April 2006, all SSL certificates issued by VeriSign require the installation of an Intermediate CA Certificate.  The SSL certificates are signed by an Intermediate CA using a two-tier hierarchy (also known as trust chain) which enhances the security of your SSL Certificate. If the proper Intermediate CA is not installed <strong>on the server</strong>, your customers will see browser errors and may choose not to proceed further and close their browser.&#8221; (<em>boldface is mine</em>)
</p></blockquote>
<p>This means that while the users do not need to modify anything (if their browser already has Verisigns Root CA certificate) the server owners need to ensure that the server is able to provide the so called <em>trust chain</em> to the users&#8217; browser when the SSL handshake is performed.</p>
<p>Never mind, lets get back to it. In order to get your Intermediate CA working, first you need a Root CA (if you already have a CA, feel free to skip the next section). Remember that in order to get this working you need to have a copy of the <a href="http://www.openssl.org/">openssl</a> toolkit installed in your system.</p>
<h3>Configure the Root CA</h3>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">mkdir</span><span class="hl-quotes"> /</span><span class="hl-string">var</span><span class="hl-quotes">/c</span><span class="hl-identifier">a
</span><span class="hl-reserved">cd</span><span class="hl-quotes"> /</span><span class="hl-string">var</span><span class="hl-quotes">/c</span><span class="hl-identifier">a</span><span class="hl-default">/
</span><span class="hl-identifier">mkdir certs crl newcerts private
</span><span class="hl-reserved">echo </span><span class="hl-quotes">&quot;</span><span class="hl-string">01</span><span class="hl-quotes">&quot;</span><span class="hl-default"> &gt; </span><span class="hl-identifier">serial
</span><span class="hl-reserved">cp</span><span class="hl-quotes"> /</span><span class="hl-string">dev</span><span class="hl-quotes">/</span><span class="hl-identifier">null index</span><span class="hl-default">.</span><span class="hl-identifier">txt
</span><span class="hl-comment"># beware that the location of the sample file is dependent on your environment
</span><span class="hl-reserved">cp</span><span class="hl-quotes"> /</span><span class="hl-string">usr</span><span class="hl-quotes">/</span><span class="hl-identifier">lib</span><span class="hl-default">/</span><span class="hl-identifier">ssl</span><span class="hl-default">/</span><span class="hl-identifier">openssl</span><span class="hl-default">.</span><span class="hl-identifier">cnf</span><span class="hl-default"> .</span></pre></div></div>
<p>You may want to modify some of the settings in the configuration file to save you some time in the future when creating the certificates: <em>default_bits</em>, <em>countryName</em>, <em>stateOrProvinceName</em>, <em>0.organizationName_default</em>, <em>organizationalUnitName</em> and <em>emailAddress</em>.</p>
<p>Now you are ready to create the CA:</p>
<div class="hl-surround" ><div class="hl-main"><pre># generate a private key
openssl genrsa -des3 -out private/cakey.key 4096
# create a self-signed certificate valid for 5 years
openssl req -new -x509 -nodes -sha1 -days 1825 -key private/cakey.pem -out cacert.pem
# go for the default values if you adapted the settings in the openssl.cnf file or enter the values you desire</pre></div></div>
<p>Now you have everything you need to run a successful CA. </p>
<h3>Configure an Intermediate CA</h3>
<p>The idea is simple, we will create a new CA following the same template that we used  in the previous section, but this time instead of generating a self-signed certificate we will generate a certificate sign request that we will sign using the Root CA.</p>
<p>First we create the folder structure:</p>
<div class="hl-surround" ><div class="hl-main"><pre>cd /var/ca/
mkdir ca2008
cd ca2008
cp ../openssl.cnf .
mkdir certs crl newcerts private
echo &quot;01&quot; &gt; serial
cp /dev/null index.txt</pre></div></div>
<p>Then the Intermediate CA private key:</p>
<div class="hl-surround" ><div class="hl-main"><pre>#generate the key
openssl genrsa -des3 -out private/cakey.pem 4096
#generate a signing request (valid for 1year)
openssl req -new -sha1 -key private/cakey.pem -out ca2008.csr
# go for the default values if you adapted the settings in the openssl.cnf file or enter the values you desire</pre></div></div>
<p>Move the sign request to the Root CA directory and sign it:</p>
<div class="hl-surround" ><div class="hl-main"><pre>mv ca2008.csr ..
cd ..
openssl ca -extensions v3_ca -days 365 -out ca2008.crt -in ca2008.csr -config openssl.cnf
mv ca2008.* ca2008/
cd ca2008/
mv ca2008.crt cacert.pem</pre></div></div>
<p>And that was it. The next thing to do is start using your Intermediate CA to sign your new certificates. But just before that, remember that<br />
to verify a certificate signed by an Intermediate CA the web browser has to verify both the certificate against the Intermediate CA and the certificate of the Intermediate CA against a Root CA.</p>
<p>In order to allow the browser to do this, a certificate chain file needs to be installed in the server. A certificate chain is a plaintext file that contains all the certificates from the Authority issuing a given certificate up to the Root of the certificate tree. In this case our chain has only two levels and the chain file is created like this:-</p>
<div class="hl-surround" ><div class="hl-main"><pre># first the intermediate CA certificate
cat cacert.pem &gt; chain.crt
# then the Root CA cert
cat ../cacert.pem &gt;&gt; chain.crt</pre></div></div>
<p>This file is the one you need to specify in the <strong>SSLCertificateChainFile</strong> of your server.</p>
<h3>Create a new server certificate</h3>
<div class="hl-surround" ><div class="hl-main"><pre># make sure you are in the Intermediate CA folder and not in the Root CA one
cd /var/ca/ca2008/
# create the private key
openssl genrsa -des3 -out {server_name}.key 4096
# generate a certificate sign request
openssl req -new -key {server_name}.key -out {server_name}.csr  
# sign the request with the Intermediate CA
openssl ca -config openssl.cnf -policy policy_anything -out {server_name}.crt -infiles {server_name}.csr
# and store the server files in the certs/ directory
mkdir certs/{server_name}
mv {server_name}.key {server_name}.csr {server_name}.crt certs/</pre></div></div>
<p>Then you should securely copy the .key and .crt files to the server and configure it to use them.</p>
<h3>Apache server configuration</h3>
<p>Just in case you are using Apache server and for the shake of completeness, these are the settings that you need to modify (possibly in your <tt>extra/http-ssl.conf</tt>):-</p>
<div class="hl-surround" ><div class="hl-main"><pre>SSLCertificateFile /var/ca/ca2008/certs/{server_name}.crt
SSLCertificateKeyFile /var/ca/ca2008/certs/{server_name}.key
SSLCertificateChainFile /var/ca/ca2008/chain.crt</pre></div></div>
<h3>References</h3>
<ul>
<li><a href="http://httpd.apache.org/docs/2.0/ssl/ssl_faq.html">SSL/TLS Strong Encryption: FAQ</a></li>
<li><a href="http://www.onlamp.com/pub/a/onlamp/2003/02/06/linuxhacks.html">Creating Your Own CA</a></li>
<li><a href="http://www.g-loaded.eu/2005/11/10/be-your-own-ca/">Be your own Certificate Authority</a></li>
<li><a href="http://www.sendmail.org/~ca/email/other/cagreg.html">Very brief introduction to create a CA and a CERT</a></li>
</ul>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=44&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/networking/howto-create-an-intermediate-certifica-authority-ca-using-openssl/feed</wfw:commentRss>
		<slash:comments>7</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>
		<item>
		<title>Net::DHCP</title>
		<link>http://weblog.nomejortu.com/ruby/netdhcp</link>
		<comments>http://weblog.nomejortu.com/ruby/netdhcp#comments</comments>
		<pubDate>Mon, 05 Nov 2007 17:59:05 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=29</guid>
		<description><![CDATA[The aim of Net::DHCP (hosted at RubyForge)  is to provide a set of classes to low level handle the specifics of DHCP (rfc2131, rfc2132, etc.) in ruby.
With Net::DHCP you will be able to craft custom DHCP packages and have access to all the fields defined for the protocol.


The Dynamic Host Configuration Protocol (DHCP) provides [...]]]></description>
			<content:encoded><![CDATA[<p>The aim of <a href="http://rubyforge.org/projects/netdhcp/">Net::DHCP</a> (hosted at <a href="http://rubyforge.org/">RubyForge</a>)  is to provide a set of classes to low level handle the specifics of <acronym title="Dynamic Host Connection Protocol">DHCP</acronym> (<a href="http://www.ietf.org/rfc/rfc2131.txt">rfc2131</a>, <a href="http://www.ietf.org/rfc/rfc2132.txt">rfc2132</a>, etc.) in ruby.</p>
<p>With <a href="http://rubyforge.org/projects/netdhcp/">Net::DHCP</a> you will be able to craft custom <acronym title="Dynamic Host Connection Protocol">DHCP</acronym> packages and have access to all the fields defined for the protocol.<br />
<span id="more-29"></span></p>
<blockquote><p>
The Dynamic Host Configuration Protocol (DHCP) provides a framework for passing configuration information to hosts on a TCPIP network.
</p></blockquote>
<p><acronym title="User Datagram Protocol">UDP</acronym> is used as transport protocol, all packets sent by the client have a source port of 68 and a destination port of 67. Likewise, packets originated in the server will have source port 67 and destination port 68.</p>
<p>You can create messages, attach options and pack them as the payload of a <acronym title="User Datagram Protocol">UDP</acronym> packet. In the same way, you can use a capturing library such as Ruby/pcap to get packages from the network and parse their contents into well formed and comprehensible ruby objects.</p>
<p>This project&#8217;s SVN repository can be checked out through anonymous access with the following command(s):-<br />
<code><br />
svn checkout http://netdhcp.rubyforge.org/svn/<br />
or<br />
svn checkout svn://rubyforge.org/var/svn/netdhcp<br />
</code></p>
<p>I hope you find the library useful. Let me know if you are using it for something! <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>References</strong></p>
<ul>
<li><a href="http://www.ietf.org/rfc/rfc2131.txt">rfc2131</a>: Dynamic Host Configuration Protocol</li>
<li><a href="http://www.ietf.org/rfc/rfc2132.txt">rfc2132</a>: DHCP Options and BOOTP Vendor Extensions</li>
<li><a href="http://www.ietf.org/rfc/rfc2563.txt">rfc2563</a>: DHCP Option to Disable Stateless Auto-Configuration in IPv4 Clients</li>
<li><a href="http://www.ietf.org/rfc/rfc4578.txt">rfc4578</a>: DHCP Options for the Intel Preboot eXecution Environment (PXE)</li>
<li><a href="http://www.ietf.org/rfc/rfc4702.txt">rfc4702</a>: The DHCP Client Fully Qualified Domain Name (FQDN) Option</li>
</ul>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=29&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/netdhcp/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>check for robots.txt</title>
		<link>http://weblog.nomejortu.com/shell-script/check-for-robotstxt</link>
		<comments>http://weblog.nomejortu.com/shell-script/check-for-robotstxt#comments</comments>
		<pubDate>Tue, 23 Oct 2007 15:54:04 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Shell Script]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=12</guid>
		<description><![CDATA[Some times it is useful to check if a given HTTP server has a robots.txt file in it. If it exist it may disclose interesting information, useful for a pentest 

 From the Wikipedia:

The robots exclusion standard or robots.txt protocol is a convention to prevent cooperating web spiders and other web robots from accessing all [...]]]></description>
			<content:encoded><![CDATA[<p>Some times it is useful to check if a given HTTP server has a <code>robots.txt</code> file in it. If it exist it may disclose interesting information, useful for a pentest <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<span id="more-12"></span><br />
 From the <a href="http://en.wikipedia.org/wiki/Robots.txt">Wikipedia</a>:</p>
<blockquote><p>
The robots exclusion standard or robots.txt protocol is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a website. The information specifying the parts that should not be accessed is specified in a file called robots.txt in the top-level directory of the website.
</p></blockquote>
<p>Here is a script that checks for the presence of the file in a list of hosts (you can download the <a href="http://weblog.nomejortu.com/data/code/bash/robots.sh">source code</a>). Two main parts can be distinguished: command line parsing and file download. </p>
<p>You can call the script in two different ways. Either you do not specify the protocol (and HTTP will be used):-</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>./robots.sh &lt;host1&gt; &lt;host2&gt; ...</pre></div></div>
<p>Or you specify the protocol with:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>./robots.sh  -p [http|https] &lt;host1&gt; &lt;host2&gt; ...</pre></div></div>
<p>Let&#8217;s see how this is done:</p>
<div class="hl-surround" ><div class="hl-main"><pre>PROTO=( http https )
HTTP=${PROTO[0]}
FILE=/tmp/robots.txt

# command line parsing
if [ &quot;-p&quot; == $1 ]
then
  for bar in ${PROTO[*]}
  do
    if [ $bar == $2 ];
    then
      HTTP=$2
      HOSTS=${*:3}
    fi
  done
else
  HOSTS=$*
fi</pre></div></div>
<p>We check if the first argument is &#8220;-p&#8221; in which case, the next argument should be one of the allowed values (those in <code>$PROTO</code> array). If that is the case, we strip the first two parameters and put everything else in the <code>$HOSTS</code> variable. At the end of the code above, <cod>$HTTP</cod> will contain either <strong>http</strong> or <strong>https</strong> and <code>$HOSTS</code> will consist of a list of hosts whose robots.txt file existance we want to verify. </p>
<p>Once we know what protocol are we using and the list of targets, the only thing left is to try to download the <code>robots.txt</code> file of each server:-</p>
<div class="hl-surround" ><div class="hl-main"><pre>for foo in $HOSTS; do
  echo &quot;================&quot;
  echo &quot;Server: $foo ($HTTP)&quot;
  CODE=`wget -O $FILE $HTTP://$foo/robots.txt 2&gt;&amp;1 | grep HTTP | head -1 | awk '{print $6}'`
  echo &quot;Code: $CODE&quot;
  if [ &quot;200&quot; == $CODE ]
  then
    echo &quot;Contents:&quot;
    echo &quot;----------------&quot;
    cat $FILE
    rm $FILE
    echo &quot;----------------&quot;
  fi
done</pre></div></div>
<p>If the response code is <code>200 OK</code> we <strong>cat</strong> the file to standard output. Otherwise we just move on to the next target of the list. The only tricky bit of the previous code is:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>wget -O $FILE $HTTP://$foo/robots.txt 2&gt;&amp;1 | grep HTTP | head -1 | awk '{print $6}'</pre></div></div>
<p>Where we try to download the file saving it to the location specified by <code>$FILE</code>. In order to get the HTTP error code we redirect standard error to standard output using <code>2&gt;&amp;1</code>.</p>
<p>One last word, it is acknowledged that the script does not follow HTTP redirects, but if the server replies with a redirect this means that effectively, no <code>robots.txt</code> file is present.</p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=12&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/shell-script/check-for-robotstxt/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ninja iptables for your server</title>
		<link>http://weblog.nomejortu.com/shell-script/ninja-iptables-for-your-server</link>
		<comments>http://weblog.nomejortu.com/shell-script/ninja-iptables-for-your-server#comments</comments>
		<pubDate>Fri, 14 Sep 2007 11:15:37 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Shell Script]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=24</guid>
		<description><![CDATA[Security is often about layers on top of layers on top of layers&#8230; And one of these layers is usually an iptables firewall installed in your server. Let&#8217;s create a small script to provide our server with the kung-fu fighting techniques needed to defeat the black hats!!

You can download the script from here. But let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Security is often about layers on top of layers on top of layers&#8230; And one of these layers is usually an <code>iptables</code> firewall installed in your server. Let&#8217;s create a small script to provide our server with the <a href="/data/img/mastering_kung-fu.jpg">kung-fu fighting techniques</a> needed to defeat the black hats!!<br />
<span id="more-24"></span><br />
You can download the script from <a href="/data/code/bash/firewall.sh">here</a>. But let&#8217;s have it on the screen so we can walk through the rules:-</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre>#!/bin/bash

###
### IPTables config file
### Based on the rules compiled by Ranjit San aka 'the grasshopper'
### Created 2007-09-14 by Daniel Martin Gomez &lt;etd[-at-]nomejortu.com&gt;
### Revision 1
###

###
### define variables
###

### path to iptables 
IPT=/sbin/iptables

### This contains a list of approved Debian sites to get software updates.
DEBIAN_SITES=('194.109.137.218' '212.219.56.139' '212.219.56.133' '212.219.56.134' '212.219.56.135' '212.219.56.138')

### This contains the authorised DNS servers configured in /etc/resolv.conf. 
DNS_SERVERS=('') 

### This is a list of external IPs that you want to allow ssh access from.
OTHER_GATEWAYS=('') 

### This is a list of hosts authorised to try ICMP probes to check if the
### server is running. This could be your ISP's IPs
CONTROL_GATEWAYS=('')

### Types of ICMP probes to allow from the previous servers
ICMP_TYPES=('echo-reply' 'destination-unreachable' 'echo-request' 'ttl-exceeded')


#### NTP servers for time synch
NTP_SERVERS=('')

### ------------------------------------------------- do not change below this line

###
### INPUT
###

### will flush the chains or all rules one by one. Therefore all new rules will be created. 
$IPT -F 

### allows inbound packets to be processed
$IPT -P INPUT ACCEPT

### drops packets so that they can not come through one interface and flow out of another. 
$IPT -P FORWARD DROP 

### This allows outbound packets to be processed
$IPT -P OUTPUT ACCEPT


### allows ICMP types (defined above) for hosts in the control list 
for IP in ${CONTROL_GATEWAYS[@]}; do
	for ICMP in ${ICMP_TYPES[@]}; do
		$IPT -A INPUT -s $IP -p icmp --icmp-type $ICMP -j ACCEPT 
	done
done

### this accepts connections for http and https access from anywhere
$IPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT

### this allows remote administration using ssh from your other gateways.
for IP in ${OTHER_GATEWAYS[@]}; do 
    $IPT -A INPUT -s $IP -p tcp -m tcp --dport 22 -j ACCEPT
done


### this allows packets to start a new connection or allows packets that are
### already associated with a connection, required for stateful inspection.
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

### this allows NTP traffic from NTP server
for NTP in ${NTP_SERVERS[@]}; do 
    $IPT -A INPUT -s $NTP -p udp -m udp --sport 123 -j ACCEPT
done

### we are about to drop everything else, so first log the discarded traffic
### just in case we want to know what *they* are trying.
$IPT -A INPUT -j LOG

### this drops any traffic that does not match to the INPUT rules
$IPT -A INPUT -j DROP 



###
### OUTPUT
###

### Allows traffic to authorised DNS servers
for IP in ${DNS_SERVERS[@]}; do 
    $IPT -A OUTPUT -d $IP -p udp -m udp --dport 53 -j ACCEPT
done

### Allows http traffic to debain sites for software updates. 
### Initial config rule
for IP in ${DEBIAN_SITES[@]}; do 
    $IPT -A OUTPUT -d $IP -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
done

### this allows packets to start a new connection or allows packets that are
### already associated with a connection, required for stateful inspection. 
$IPT -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 

### this allows NTP traffic to the NTP servers
for NTP in ${NTP_SERVERS[@]}; do 
    $IPT -A OUTPUT -d $NTP -p udp -m udp --dport 123 -j ACCEPT
done

### this logs all OUTPUT traffic that does not match the rules before it beign
### dropped.
$IPT -A OUTPUT -j LOG 

### this drops any traffic that does not match to the OUTPUT rules
$IPT -A OUTPUT -j DROP</pre></div></div>
<p>Just two things to add: First, do not forget to set your own values for the variables <code>DNS_SERVERS</code>, <code>OTHER_GATEWAYS</code>, <code>CONTROL_GATEWAYS</code> and <code>NTP_SERVERS</code>. And second, if you want your kung-fu up and ready after boot you may need to issue the following:-</p>
<div class="hl-surround" ><div class="hl-main"><pre>cd /etc/init.d/
wget http://weblog.nomejortu.com/data/code/bash/firewall.sh
chmod +x firewall.sh
update-rc.d firewall.sh defaults</pre></div></div>
<p>If you ever want to remove it from the boot sequence just issue:-</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>update-rc.d -f firewall.sh remove</pre></div></div>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=24&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/shell-script/ninja-iptables-for-your-server/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>harden your apache+php installation</title>
		<link>http://weblog.nomejortu.com/networking/harden-your-apachephp-installation</link>
		<comments>http://weblog.nomejortu.com/networking/harden-your-apachephp-installation#comments</comments>
		<pubDate>Fri, 14 Sep 2007 10:05:31 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=22</guid>
		<description><![CDATA[Instructions follow  on how to build and harden one of the most common configurations out there.

We are going for a DIY installation were everything is compiled from source, so some development tools are required. Let&#8217;s get our hands dirty!
Download an uncompress
cd /usr/local/src/
wget http://mirror.public-internet.co.uk/apache/httpd/httpd-2.2.4.tar.gz
tar -xvvzf httpd-2.2.4.tar.gz
wget http://uk2.php.net/get/php-5.2.3.tar.gz/from/this/mirror
tar -xvvzf php-5.2.3.tar.gz
Install software
Required by Apache:
apt-get install gcc make [...]]]></description>
			<content:encoded><![CDATA[<p>Instructions follow  on how to build and harden one of the most common configurations out there.<br />
<span id="more-22"></span><br />
We are going for a <acronym title="Do It Yourself">DIY</acronym> installation were everything is compiled from source, so some development tools are required. Let&#8217;s get our hands dirty!</p>
<p><strong>Download an uncompress</strong></p>
<div class="hl-surround" ><div class="hl-main"><pre>cd /usr/local/src/
wget http://mirror.public-internet.co.uk/apache/httpd/httpd-2.2.4.tar.gz
tar -xvvzf httpd-2.2.4.tar.gz
wget http://uk2.php.net/get/php-5.2.3.tar.gz/from/this/mirror
tar -xvvzf php-5.2.3.tar.gz</pre></div></div>
<p><strong>Install software</strong><br />
Required by Apache:</p>
<div class="hl-surround" ><div class="hl-main"><pre>apt-get install gcc make libc6-dev libc-dev \
linux-kernel-headers libssl-dev zlib1g-dev</pre></div></div>
<p>Required by PHP:</p>
<div class="hl-surround" ><div class="hl-main"><pre>apt-get install g++ g++-4.1 libfreetype6 \
libfreetype6-dev libgd2-noxpm libgd2-noxpm-dev \
libjpeg62 libjpeg62-dev libmysqlclient15-dev \
libpng12-0 libpng12-dev libstdc++6-4.1-dev \
libxml2 libxml2-dev</pre></div></div>
<p><strong>Tweak Apache</strong><br />
Get rid of the server banner, edit <code>/usr/local/src/httpd-2.2.4/include/ap_release.h</code>:</p>
<div class="hl-surround" ><div class="hl-main"><pre>define AP_SERVER_BASEVENDOR &quot;nomejortu&quot;
define AP_SERVER_BASEPROJECT &quot;nmt server&quot;
define AP_SERVER_BASEPRODUCT &quot;server&quot;</pre></div></div>
<p><strong>Configure, compile and install</strong></p>
<div class="hl-surround" ><div class="hl-main"><pre>cd /usr/local/src/httpd-2.2.4/
./configure --disable-info --disable-autoindex \
--disable-include  --disable-userdir --disable-status \
--disable-imagemap --disable-cgid --disable-cgi \
--disable-proxy --enable-ssl=static \
--enable-rewrite=static --enable-dir=static \
--enable-unique_id=static --enable-so
make
make install</pre></div></div>
<p>With the previous configure line we are removing modules that either disclose too much information or we do not need (wach out! you may need some of them). All inluded modules are statically linked to the binary. The only dynamic <code>modules</code> that we will be using are the <strong>mod_php</strong> and <strong>mod_security</strong>.</p>
<ul>
<li>&#8211;<strong>disable-info</strong>,  &#8211;<strong>disable-status</strong>: we don&#8217;t need server info or status at all.</li>
<li> &#8211;<strong>disable-autoindex</strong>, &#8211;<strong>disable-userdir</strong>:  no automatic directory listings, no username enumeration through the /~ technique.</li>
<li> &#8211;<strong>enable-dir</strong>: redirect malformed urls (requests to directories without trailing slash) and the <code>DirectoryIndex</code> directive.</li>
<li> &#8211;<strong>disable-include</strong>, &#8211;<strong>disable-imagemap</strong> : no server side includes or image maps handled by the server.</li>
<li> &#8211;<strong>disable-cgid</strong>, &#8211;<strong>disable-cgi</strong> : no cgi interfaces.</li>
<li> &#8211;<strong>disable-proxy</strong>, &#8211;<strong>enable-ssl</strong>, &#8211;<strong>enable-rewrite</strong>: disable the proxy capanility, enable <acronym title="Secure Sockets Layer">SSL</acronym> and the rewrite engine.</li>
<li> &#8211;<strong>enable-unique_id</strong>: needed for <strong>mod_security</strong> (see below).</li>
<li> &#8211;<strong>enable-so</strong>: </li>
</ul>
<p><strong>Configure apache</strong><br />
In apache2&#8217;s configuration file (<code>/usr/local/apache2/conf/httpd.conf</code>) append:</p>
<div class="hl-surround" ><div class="hl-main"><pre># server banner
ServerSignature  Off
ServerTokens  Prod
# disable TRACE requests
TraceEnable off</pre></div></div>
<p>If needed, add the <code>index.php</code><code> as a default file to </code><code>DirectoryIndex</code> directive on Line 165:</p>
<div class="hl-surround" ><div class="hl-main"><pre>&lt;IfModule dir_module&gt;
   DirectoryIndex index.php index.html
&lt;/IfModule&gt;</pre></div></div>
<p>In the same way, if you need virtual hosts enabled, uncomment the line 386 (or equivalent):</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>Include conf/extra/httpd-vhosts.conf</pre></div></div>
<p>Add your options to that file. And if you need SSL support, uncomment the line 398 (or equivalent) of the same file:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>Include conf/extra/httpd-ssl.conf</pre></div></div>
<p>Change ownership of the htdocs and remove unnecessary files and folders:-</p>
<div class="hl-surround" ><div class="hl-main"><pre>chown daemon.daemon /usr/local/apache2/htdocs/ -R
rm -rf /usr/local/apache2/htdocs/*
rm -rf /usr/local/apache2/cgi-bin/*
rm -rf /usr/local/apache2/icons</pre></div></div>
<p>If you want your server to start at boot time, issue the following commands:-</p>
<div class="hl-surround" ><div class="hl-main"><pre>rm /etc/init.d/apache2
ln -s /usr/local/apache2/bin/apachectl /etc/init.d/apache2
update-rc.d apache2 defaults</pre></div></div>
<p>Be careful because if you have configured SSL with a certificate whose private key requires a pass phrase, the system will request the pass phrase and wait upon restart.</p>
<p><strong>PHP</strong><br />
Not much on the PHP side. Download and compile:</p>
<div class="hl-surround" ><div class="hl-main"><pre>cd /usr/local/src/php-5.2.3
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/include/mysql --with-config-file-path=/etc --with-gd --with-zlib-dir=/usr/lib/

make
make install</pre></div></div>
<ul>
<li>&#8211;<strong>with-apxs2</strong>: compile a module for apache2 in the specified location.</li>
<li>&#8211;<strong>with-mysql</strong>: .enable mysql support.</li>
<li>&#8211;<strong>with-config-file-path</strong>: .specify where you want the php.ini config file.</li>
<li>&#8211;<strong>with-gd</strong>: .the graphical library if you need it.</li>
<li>&#8211;<strong>with-zlib-dir</strong>: .use system&#8217;s zlib (downloaded from packages).</li>
</ul>
<p>Although the php installation adds the <code>LoadModule</code> line, but you still need to edit apache configuration file (<code>httpd.conf</code>) and add the following:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>AddType application/x-httpd-php .php .phtml</pre></div></div>
<p>Modify the <code>DirectoryIndex</code> directive if you want the server to default to <code>index.php</code> when a directory is requested.</p>
<p><strong>mod_security</strong><br />
Download:</p>
<div class="hl-surround" ><div class="hl-main"><pre>cd /usr/local/src/
wget http://www.modsecurity.org/download/modsecurity-apache_2.1.2.tar.gz
tar -xvvzf modsecurity-apache_2.1.2.tar.gz
cd modsecurity-apache_2.1.2/apache2/</pre></div></div>
<p>Edit the Makefile to adjust the following lines (compile <strong>mod_security</strong> with Apache&#8217;s version of the <code>pcre</code> library):</p>
<div class="hl-surround" ><div class="hl-main"><pre>top_dir      = /usr/local/apache2
INCLUDES = -I /usr/include/libxml2 -I /usr/local/src/httpd-2.2.4/srclib/pcre/</pre></div></div>
<p>Compile and install:</p>
<div class="hl-surround" ><div class="hl-main"><pre>make
make install</pre></div></div>
<p>Copy the default rule set to apache directory and include them in the main apache configuration file:</p>
<div class="hl-surround" ><div class="hl-main"><pre>cp -r /usr/local/src/modsecurity-apache_2.1.2/rules/ \
/usr/local/apache2/conf/modsecurity</pre></div></div>
<p>In /usr/local/apache2/conf/httpd.conf add the following lines:</p>
<div class="hl-surround" ><div class="hl-main"><pre>LoadModule security2_module modules/mod_security2.so
Include conf/modsecurity/*.conf</pre></div></div>
<p>In order to enforce the rules (by default <strong>mod_security</strong> would simply log requests that matched the rules), go to each and single file and change the SecDefaultAction to:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>SecDefaultAction &quot;phase:2,log,deny,status:400&quot;</pre></div></div>
<p><strong>The End: up and running</strong><br />
Last but not least do not forget to remove software that you no longer need! No compilers or development libraries should remain in the sever.</p>
<p>First software needed to compile Apache:</p>
<div class="hl-surround" ><div class="hl-main"><pre>apt-get remove --purge binutils cpp cpp-4.1 gcc-4.1 \
libssp0 make gcc libc6-dev libc-dev \
linux-kernel-headers libssl-dev zlib1g-dev</pre></div></div>
<p>And also the one needed for PHP:</p>
<div class="hl-surround" ><div class="hl-main"><pre>apt-get remove --purge libxml2-dev libfreetype6-dev \
libgd2-noxpm-dev libjpeg62-dev libpng12-dev libgd2-dev \
libmysqlclient15-dev g++ g++-4.1 libstdc++6-4.1-dev</pre></div></div>
<p>Remove all the sources that we have used:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>rm -rf /usr/local/src/*</pre></div></div>
<p>And of course:-</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>/usr/local/apache2/bin/apachectl start</pre></div></div>
<p><strong>References</strong></p>
<ul>
<li><a href="http://xianshield.org/guides/apache2.0guide.html">Apache 2.0 Hardening Guide</a></li>
</ul>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=22&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/networking/harden-your-apachephp-installation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rails environment with lighttpd</title>
		<link>http://weblog.nomejortu.com/ruby/rails-environment-with-lighttpd</link>
		<comments>http://weblog.nomejortu.com/ruby/rails-environment-with-lighttpd#comments</comments>
		<pubDate>Tue, 04 Sep 2007 12:49:24 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=23</guid>
		<description><![CDATA[We want an automated deployment enviroment. The server will automatically (upon restart) detect all the rails applications under a given folder and create virtual hosts for them. No further changes in the server configuration will be required. never. 

We also want a different virtual host in our domain for each application. For instance if we [...]]]></description>
			<content:encoded><![CDATA[<p>We want an automated deployment enviroment. The server will automatically (upon restart) detect all the rails applications under a given folder and create virtual hosts for them. No further changes in the server configuration will be required. never. <img src='http://weblog.nomejortu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<span id="more-23"></span><br />
We also want a different virtual host in our domain for each application. For instance if we have a <strong>expenses</strong> and a <strong>blog</strong> applications and our domain name is <code>nomejortu.com</code>. We are setting an environment were we will be able to access out applications using <em>http://expenses.nomejortu.com/</em> and <em>http://blog.nomejortu.com/</em>. </p>
<p>We are using a standard package based installation of <a href="http://www.lighttpd.net/">lighttpd</a> and ruby:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre># apt-get install gcc</pre></div></div>
<p><strong>gcc</strong> is needed for compiling <code>fcgi</code> (fast cgi) for Ruby.</p>
<div class="hl-surround" ><div class="hl-main"><pre># apt-get install ruby ruby1.8-dev rubygems libfcgi-ruby1.8
# gem install rails
# gem install fcgi</pre></div></div>
<p>And the web server:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>apt-get install lighttpd</pre></div></div>
<p>The Rails applications will be located under <code>/var/www/lighttpd/rails/</code> directory and we will have two folders in <code>/etc/lighttpd</code>: <strong>railsapp-available</strong> and <strong>railsapp-enabled</strong>. The first will contain the list of available Rails applications, the second &#8211; surprisingly &#8211; a list of those that we want to enable. With this configuration we will be able to have lots of applications in the server but just enabling (or disabling) them as we need it. Lets prepare this structure:-</p>
<div class="hl-surround" ><div class="hl-main"><pre># mkdir /var/www/lighttpd/rails/
# ln -s /var/www/lighttpd/rails/ /etc/lighttpd/railsapp-available
# mkdir /etc/lighttpd/railsapp-enabled</pre></div></div>
<p>The next step is to <em>enable</em> those applications we want to deploy. Say for instance you have <strong>app #1</strong>, <strong>app #2</strong> and <strong>app #3</strong> under <code>/var/www/lighttpd/rails/</code> and right now we only want to deploy <strong>app #1</strong> and <strong>app #3</strong>. We will need to issue the following commands:-</p>
<div class="hl-surround" ><div class="hl-main"><pre># cd /etc/lighttpd/railsapp-enabled/
# ln -s ../railsapp-available/app1
# ln -s ../railsapp-available/app3</pre></div></div>
<p>Only applications linked inside <code>railsapp-enabled</code> will be deployed when the server is restarted. To add a new application create a link as shown above, to <em>undeploy</em> an application just remove the link.</p>
<p>No we need the server to be able to understand this settings and only deploy the applications we want. For doing it we will include a script in the main config file that creates the necessary bits of lighttpd configuration required for them to work. Add this line at the end of <code>/etc/lighttpd/lighttpd.conf</code>:-</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>include_shell &quot;/usr/share/lighttpd/include-app-vhost.rb&quot;</pre></div></div>
<p>Download the  <a href="/data/code/ruby/include-app-vhost.rb">source code</a> for the script and place it in the above location. The code looks like this:-</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre>#!/usr/bin/ruby
 $VIRTUAL_HOST=&lt;&lt;EOV
    $HTTP[&quot;host&quot;] =~ &quot;%RAILS_APP%($|\.nomejortu\.loc)&quot; {
        server.document-root = &quot;/var/www/lighttpd/rails/%RAILS_APP%/public/&quot;
        server.error-handler-404 = &quot;/dispatch.fcgi&quot;
        server.indexfiles = (&quot;dispatch.fcgi&quot;)
        fastcgi.server = (&quot;.fcgi&quot; =&gt;
           (&quot;localhost&quot; =&gt;
                (&quot;socket&quot; =&gt; &quot;/tmp/railsapp-%RAILS_APP%.socket&quot;,
                 &quot;min-procs&quot; =&gt; 2,
                 &quot;max-procs&quot; =&gt; 2,
                 &quot;bin-path&quot; =&gt; &quot;/var/www/lighttpd/rails/%RAILS_APP%/public/dispatch.fcgi&quot;,
                 &quot;bin-environment&quot; =&gt; (&quot;RAILS_ENV&quot; =&gt; &quot;production&quot;)
                )
           )
        )
    }
 EOV
 Dir[&quot;/etc/lighttpd/railsapp-enabled/*&quot;].each do |railsapp|
   puts $VIRTUAL_HOST.gsub(/%RAILS_APP%/,  File.basename(railsapp))
 end</pre></div></div>
<p>The main loop of the script goes trough the <code>/etc/lighttpd/railsapp-enabled/</code> directory and for each file it finds there, creates a new block of configuration directives. It does this by replacing the <strong>%RAILS_APP%</strong> string in the <code>$VIRTUAL_HOST</code> variable with the current name of the application under that directory.</p>
<p>Let&#8217;s have a closer look to the generated configuration for each application:</p>
<div class="hl-surround" ><div class="hl-main"><pre>$HTTP[&quot;host&quot;] =~ &quot;%RAILS_APP%($|\.nomejortu\.com)&quot; {
  server.document-root = &quot;/var/www/lighttpd/rails/%RAILS_APP%/public/&quot;
  server.error-handler-404 = &quot;/dispatch.fcgi&quot;
  server.indexfiles = (&quot;dispatch.fcgi&quot;)
  fastcgi.server = (&quot;.fcgi&quot; =&gt;
    (&quot;localhost&quot; =&gt;
      (&quot;socket&quot; =&gt; &quot;/tmp/railsapp-%RAILS_APP%.socket&quot;,
      &quot;min-procs&quot; =&gt; 2,
      &quot;max-procs&quot; =&gt; 2,
      &quot;bin-path&quot; =&gt; &quot;/var/www/lighttpd/rails/%RAILS_APP%/public/dispatch.fcgi&quot;,
      &quot;bin-environment&quot; =&gt; (&quot;RAILS_ENV&quot; =&gt; &quot;production&quot;)
      )
    )
  )
}</pre></div></div>
<p>The first line matches the virtual hostname with the application, then we need to customize a few parameters: <code>server.document-root</code>, <code>socket</code> and <code>bin-path</code> that will point to the Rails directory structure of out application.</p>
<p>Don&#8217;t forget to make it executable:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre># chmod +x /usr/share/lighttpd/include-app-vhost.rb</pre></div></div>
<p>Remember that adding an application into /var/www/lighttpd/rails will not make it readily available. You still need to add the correct links in /etc/lighttpd/railsapp-enabled and restart lighttpd!</p>
<div class="hl-surround" ><div class="hl-main"><pre># cd /etc/lighttpd/railsapp-enabled
# ln -s ../railsapp-available/&lt;app_name&gt;
# /etc/init.d/lighttpd restart</pre></div></div>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=23&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/rails-environment-with-lighttpd/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>send files through email from the command line</title>
		<link>http://weblog.nomejortu.com/shell-script/send-files-through-email-from-the-command-line</link>
		<comments>http://weblog.nomejortu.com/shell-script/send-files-through-email-from-the-command-line#comments</comments>
		<pubDate>Wed, 20 Dec 2006 13:09:15 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Shell Script]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=16</guid>
		<description><![CDATA[Every now and then I need to send by email some file to a friend or coworker or even myself. I have found that the easiest way to do this is just having a shell script that do the hard work for you.
After some research I found a set of scripts that actually do what [...]]]></description>
			<content:encoded><![CDATA[<p>Every now and then I need to send by email some file to a friend or coworker or even myself. I have found that the easiest way to do this is just having a shell script that do the hard work for you.</p>
<p>After some research I found a set of scripts that actually do what I want (credit goes to Heiner Steven). The bad news is that this is not a <emph>full-bash</emph> solution. The scripts use the <code>metasend</code> command to send files as MIME atachments.</p>
<p><span id="more-16"></span></p>
<p>This is a easy two-step process. First, you need to install the <strong>metamail</strong> (this is the name of the Debian GNU/Linux package) in your box. Then grab this two scripts (sendfile,  getmimetype). The first one does the call to <code>metasend</code>. From it&#8217;s usage information:</p>
<div class="hl-surround" ><div class="hl-main"><pre>usage: sendfile [-f] [-s subject] [-m mimetype] recipient file ...
    -f:  force sending of mail even for invalid recipients
    -s:  subject of the mail message
    -m:  mime-type (i.e. &quot;application/octet-stream&quot;)

Multiple files may be specified. If no mimetype was given,
it is determined via a call to &quot;getmimetype&quot;.</pre></div></div>
<p>And you are ready to go.</p>
<img src="http://weblog.nomejortu.com/?ak_action=api_record_view&id=16&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/shell-script/send-files-through-email-from-the-command-line/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>runningserver: hello? anybody out there?</title>
		<link>http://weblog.nomejortu.com/ruby/runningserver-hello-is-there-anybody-out-there</link>
		<comments>http://weblog.nomejortu.com/ruby/runningserver-hello-is-there-anybody-out-there#comments</comments>
		<pubDate>Wed, 20 Dec 2006 12:45:52 +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=13</guid>
		<description><![CDATA[I have created a small ruby script to check if there are running servers on a given port number. The script is able to check a list of hosts and will output an informational message on the port status for each host.
Let&#8217;s begin with the script. Then we can talk about the work it does.

Here [...]]]></description>
			<content:encoded><![CDATA[<p>I have created a small <a href="http://www.ruby-lang.org/">ruby</a> script to check if there are running servers on a given port number. The script is able to check a list of hosts and will output an informational message on the port status for each host.</p>
<p>Let&#8217;s begin with the script. Then we can talk about the work it does.<br />
<span id="more-13"></span><br />
Here is the <a href="http://weblog.nomejortu.com/data/code/ruby/runningserver.rb">code</a>:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre>#!/usr/bin/ruby
##################################################################################################################
#
# runningserver.rb
# 12/DEC/2006
# etd [etd__at__nomejortu.com]
#
# Desc:
#   Script to create a connection on the specified port to check if a server is
#   listening on it.
#
# Version:
#   v1.0 [12/Dec/2006]: first released
#
###################################################################################################################
$help =&lt; &lt;EOH
#{$0}:
   Script to create a connection on the specified port to check if a server is
   listening on it.

Usage:
  #{$0} [-h|-help|--help] [-p &lt;portnumber&gt;] &lt;host1&gt; [&lt;host2&gt; &lt;host3&gt; ...]

Options:
  -h/-help/--help:  This help message
  -p &lt;portnumber&gt;:  Specify which TCP port should be tested.
Arguments:
  host(s):  The different hosts to test.
EOH

#------------------------------ Input argument parsing
if ( 
    ARGV.include?('--help')  || 
    ARGV.include?('-help') || 
    ARGV.include?('-h') || 
    (ARGV.size==0) 
  ) then
  puts
  puts $help
  exit
end

# set the port number
if ARGV.include?('-p') then
  p_position = ARGV.index('-p')
  # read value after the -p
  if ARGV[p_position+1] != nil then
    $port = ARGV[p_position+1].to_i
  else
    puts &quot;ERROR: you kind of need to specify the port if you use the -p parameter&quot;
    puts &quot;\t e.g: -p 8080\n\n&quot; 
    exit
  end
  # clear these values in the array
  ARGV[p_position] = nil
  ARGV[p_position+1] = nil
end

# clear the ARGV of already parsed ARGs
hostlist = ARGV.compact

#------------------------------ Interesting stuff starts here
require 'socket'
require 'net/http'
require 'timeout'

if $port == nil then
  $port=18264
end
#for each host in the command line
hostlist.each() do |host|
  puts
  serverstr = &quot;No server was found in port #{$port}&quot;
  begin
    Timeout::timeout(3) do
      client = TCPSocket.open(host, $port)
      client.close
      serverstr = &quot;Server is running on port #{$port}&quot;
    end
  rescue Exception =&gt; e
    serverstr = &quot;No server was found in port #{$port}&quot;
  end
  puts &quot;#{host}: #{serverstr}&quot;
end
puts</pre></div></div>
<p>Because this is the first ruby script in this blog I will explain it step by step. The first thing that comes in the script is the input argument parsing. Input arguments are passed as a string array, in the variable <code>ARGV</code>. To check if the user is requesting help or usage information we can issue:</p>
<div class="hl-surround" ><div class="hl-main"><pre>if ( 
    ARGV.include?('--help')  || 
    ARGV.include?('-help') || 
    ARGV.include?('-h') || 
    (ARGV.size==0) 
  ) then
  puts
  puts $help
  exit
end</pre></div></div>
<p>In the same way the script checks if the user has supplied a port number. If this is not the case, we use a default port. If the input argument <code>-p</code> is present, we asume the next argument (<code>p_position+1</code>) to be the TCP port number. It is important to get the port number as an interger. We accomplish this by issuing the <code>.to_i</code> call.</p>
<div class="hl-surround" ><div class="hl-main"><pre># clear these values in the array
  ARGV[p_position] = nil
  ARGV[p_position+1] = nil</pre></div></div>
<p>With the code above we clear the elements we are not using any more. Doing so we can <emph>compact</emph> the <code>ARGV</code> array in order to remove all the <code>nil</code> elements of it just by calling the <code>.compact</code> function of the array.</p>
<p>The body of the script is pretty straight forward. For each host given as input to the script we try to open a socket on the specified port. If successful, we close the socket and that&#8217;s it. To avoid hanging for ever on closed ports we are using <strong>timeout</strong> library (<code>require 'timeout'</code>). The timeout mechanism is very simple. You create a block and give the timeout in seconds (can be a float point value). If the block terminates before the timeout, <code>timeout</code> returns the value of the block. Otherwise, the exception (<code>Timeout::Error</code>) is raised.</p>
<div class="hl-surround" ><div class="hl-main"><pre>begin
    Timeout::timeout(3) do
      #code
    end
  rescue Exception =&gt; e
    #exception handling
  end</pre></div></div>
<p>You see? It wasn&#8217;t that difficult, was it? <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=13&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://weblog.nomejortu.com/ruby/runningserver-hello-is-there-anybody-out-there/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
