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

<channel>
	<title>usefulfor.com/security &#187; Networking</title>
	<atom:link href="http://usefulfor.com/security/category/networking/feed/" rel="self" type="application/rss+xml" />
	<link>http://usefulfor.com/security</link>
	<description>security dojo</description>
	<lastBuildDate>Wed, 16 Jun 2010 15:47:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Grabbing LM challenges with an ettercap filter</title>
		<link>http://usefulfor.com/security/2008/06/24/lm-challenge-ettercap-filter/</link>
		<comments>http://usefulfor.com/security/2008/06/24/lm-challenge-ettercap-filter/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 17:15:58 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[hack-fu]]></category>

		<guid isPermaLink="false">http://usefulfor.com/security/?p=71</guid>
		<description><![CDATA[hack-fu by: bob So... this is an amalgamtion of several ideas and bits of work I've found floating around that I put together. The result is a handy pentesting/pwnage technique. The following blog that kicked this idea off discusses a modified Metasploit exploit module called smb_server.pm by Kurt Grutzmacher: NTLMv1, Metasploit and You.  Around [...]]]></description>
			<content:encoded><![CDATA[<p>hack-fu by: <strong>bob</strong> So... this is an amalgamtion of several ideas and bits of work I've found floating around that I put together. The result is a handy pentesting/pwnage technique. The following blog that kicked this idea off discusses a modified Metasploit exploit module called smb_server.pm by Kurt Grutzmacher: <a href="http://grutztopia.jingojango.net/2007/04/ntlmv1-metasploit-and-you.html">NTLMv1, Metasploit and You</a>.  Around the same time I'd been playing with some ettercap filters, based on the below work at Irongeek: <a href="http://www.irongeek.com/i.php?page=security/ettercapfilter">Fun with Ettercap Filters</a>.  These two sparked something and gave me a use of those huge LMHALFCHALLENGE rainbow tables I'd downloaded. <span id="more-71"></span></p>
<h3>Goal:</h3>
<p>To retreive a client's currently logged in credentials through manipulation of their HTTP stream (via a MITM attack).  Most of the clients seen during various internal tests were pretty well firewalled and patched. However by manipulating a client's HTTP stream it was possible to have them send their username and the majority of their password hash.</p>
<h3>Conditions:</h3>
<p>So as with most things there are some conditions that must be inplace for this to be successful and also requirements that the credential snarfer must have sorted.  Client Conditions:-:</p>
<ul>
<li>The target client must be using a version of IE for their browsing. During testing IE appears to be the only browser that by default sends through the resonse to the challenge of the locally logged in user that we're after.</li>
<li>Must be on same netowrk segment, for the MITM attack to work of course.</li>
<li>Only LANMAN hashes can be obtained this way. If the client has been configured to support NTLM only then we lose. Most default installs will not be set up this way, you <strong>may</strong> encounter more secure setups in some corporate environments.</li>
<li>Only the first 7 characters are in the tables. Youll have to brute force the rest of the password.</li>
</ul>
<h3>Attacker's Rig:</h3>
<ul>
<li>Some LMHALFCHALL rainbow tables: <a href="http://www.freerainbowtables.com/tables/halflmchall/halflmchall_all-space_1-7.torrent">halflmchall_all-space_1-7.torrent</a> (54gig) -or- <a href="http://www.freerainbowtables.com/tables/halflmchall/halflmchall_alpha-numeric_1-7.torrent">halflmchall_alpha-numeric_1-7.torrent</a> (5gig)</li>
<li>Metasploit, plus Kurt Grutzmacher's exploit module smb_server.pm from here (this does not work on Windows):
<ul>
<li><a href="http://metasploit.com/tools/">http://metasploit.com/tools/</a>: Looking for Framework v.2.7</li>
<li><a href="http://grutz.jingojango.net/exploits/smb_sniffer.pm">smb_sniffer.pm</a></li>
</ul>
</li>
<li><a href="http://ettercap.sourceforge.net/download.php">Ettercap</a> plus an ettercap filter to rewrite the victims web pages <a href="/security/files/2008/07/LMHC_etter.filter">LMHC_etter.filter</a>.</li>
<li><a href="http://www.oxid.it/downloads/ca_setup.exe">Cain &amp; Abel</a> to perform the crypt analysis of the LMHALFCHALLENGE hashes recovered. As far as I know these rainbow tables for LMHC were created with the Cain &amp; Abel utility winrtgen and therefore are incompatible with the patched version of rcrack.</li>
</ul>
<h3>Attacker Process:</h3>
<ul>
<li>Fix up the IP address in the <code>LMHC_etter.filter</code> file to represent the host you'll be running Metasploit 2.7 &amp; the smbsniffer exploit module from.</li>
<li>Compile that with:
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>etterfilter.exe -o LMHC_etter.ef LMHC_etter.filter</pre></div></div>
</li>
<li>Unpack Metasploit 2.7 from tha above link, place <code>smb_sniffer.pm</code> under the exploits/ directory.</li>
<li>Install and run as root</li>
<li>Metasploit commands :
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>framework-2.7 $ sudo ./msfconsole msf &amp;gt; use smb_sniffer msf smb_sniffer &amp;gt; set PWFILE=/tmp/smb_sniffed.pw3</pre></div></div>
<p> So the SMBsniffer is waiting listening for connections fro the victims IE. So we need to rewrite some of their web requests to ensure they pass us their LMHALFCHALLENGE hashes.</li>
<li>Run up ettercap using our compiled filter from earlier, to perfom the arp poisoning attack with the following:
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>ettercap -Tq -M arp:remote -F /mnt/LMHC_etter.ef /GATEWAY ADDRESS/ /VICTIM ADDRESS/</pre></div></div>
</li>
<li>When the page gets rewritten correctly IE will attempt to make the connection and send the logged in user credentials to your waiting server.</li>
<li>Resulting in a Pwdump compliant output file called <code>smb_sniffed.pw3</code></li>
<li>Switch to Cain &amp; Abel for the hash cracking</li>
<li>Select the hash you want to crack, right click and select Cryptanalysis Attack-&gt;HALFLM Hashes + challenge-&gt;via RainbowTables, add the tables, and crack it.</li>
</ul>
<h3>Some Other Things To Play With In The Same Eenvironment:</h3>
<p><a href="http://str0ke213.tradebit.com/pub/8/67.swf">LSO: MSFweb 3.0 part 2</a> (via Milw0rm videos)</p>
<img src="http://usefulfor.com/security/?ak_action=api_record_view&id=71&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://usefulfor.com/security/2008/06/24/lm-challenge-ettercap-filter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>xmitm: xml man in the middle</title>
		<link>http://usefulfor.com/security/2007/12/16/xml-man-in-the-middle-xmitm/</link>
		<comments>http://usefulfor.com/security/2007/12/16/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[Tools]]></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="/security/files/2008/06/xmitm-slides_2008-02-07.pdf">here</a><br />
<span id="more-39"></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 class="aligncenter"><img src="http://usefulfor.com/security/files/2008/06/xmitm_01.jpg" alt="Diagram showing the XML dialog between client and server" width="329" height="320" class="aligncenter size-full wp-image-61" /></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 class="aligncenter"><img src="http://usefulfor.com/security/files/2008/06/xmitm_02.jpg" alt="Diagram that shows how the ruby script stands between client and server for intercepting the XML dialog" width="393" height="250" class="aligncenter size-full wp-image-62" /></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 class="aligncenter"><img src="http://usefulfor.com/security/files/2008/06/xmitm_03.jpg" alt="" width="469" height="293" class="aligncenter size-full wp-image-63" /></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="/security/files/2008/06/xmitm.rb">code</a>):-</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># create a server that accepts connections from the client</span>
server = TCPServer.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>$local_host, <span style="color:#ff6633; font-weight:bold;">$local_port</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span>local = server.<span style="color:#9900CC;">accept</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#008000; font-style:italic;"># everytime we accept a connection for the client, we open a connection</span>
  <span style="color:#008000; font-style:italic;"># with the server to stablish the dialog.</span>
  remote = TCPSocket.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>$remote_host, <span style="color:#ff6633; font-weight:bold;">$remote_port</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># if one of the ends of the communication closes the socket, we</span>
  <span style="color:#008000; font-style:italic;"># toggle this flag</span>
  alive = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">while</span> alive <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#008000; font-style:italic;"># see the explanation below</span>
    result = <span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>local, remote<span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> result != <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">then</span>
      <span style="color:#9966CC; font-weight:bold;">for</span> socket <span style="color:#9966CC; font-weight:bold;">in</span> result<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># detect if one end of the connection is closed and</span>
        <span style="color:#008000; font-style:italic;"># close the other end</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>socket.<span style="color:#9900CC;">eof</span>?<span style="color:#006600; font-weight:bold;">&#41;</span>
          local.<span style="color:#9900CC;">close</span>
          remote.<span style="color:#9900CC;">close</span>
          alive = <span style="color:#0000FF; font-weight:bold;">false</span>
          <span style="color:#9966CC; font-weight:bold;">break</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># read the information that one peer wants to send to the other</span>
        data = socket.<span style="color:#CC0066; font-weight:bold;">gets</span><span style="color:#006600; font-weight:bold;">&#40;</span>$eom<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># encapsulate the data into an HTTP proxy request</span>
        res = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>$proxy_host, <span style="color:#ff6633; font-weight:bold;">$proxy_port</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">start</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>http<span style="color:#006600; font-weight:bold;">|</span>
          req = <span style="color:#6666ff; font-weight:bold;">Net::HTTP::Post</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;http://#{$dummyhttp_host}:#{$dummyhttp_port}/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          req.<span style="color:#9900CC;">body</span>= data
          http.<span style="color:#9900CC;">request</span><span style="color:#006600; font-weight:bold;">&#40;</span>req<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        modified_data = res.<span style="color:#9900CC;">body</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># send the modified data to the other end of the connection</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>socket == local<span style="color:#006600; font-weight:bold;">&#41;</span>
          remote.<span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span>modified_data<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          local.<span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span>modified_data<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        socket.<span style="color:#9900CC;">flush</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">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="/security/files/2008/06/webrick.rb">ruby version</a> and a <a href="http://usefulfor.com/security/files/2008/06/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="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'webrick'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">include</span> WEBrick
&nbsp;
<span style="color:#008000; font-style:italic;"># create the server, no output, disable logging</span>
s = HTTPServer.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>
  <span style="color:#ff3333; font-weight:bold;">:Port</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2000</span>,
  <span style="color:#ff3333; font-weight:bold;">:Logger</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Log.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#6666ff; font-weight:bold;">BasicLog::FATAL</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
  <span style="color:#ff3333; font-weight:bold;">:AccessLog</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>  <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># the *echo* functionality</span>
s.<span style="color:#9900CC;">mount_proc</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>req, res<span style="color:#006600; font-weight:bold;">|</span>
  res.<span style="color:#9900CC;">body</span> = req.<span style="color:#9900CC;">body</span>
  res<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Content-Type'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = req<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Content-Type'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># clean tear down</span>
<span style="color:#CC0066; font-weight:bold;">trap</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'INT'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> s.<span style="color:#9900CC;">shutdown</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
s.<span style="color:#9900CC;">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://usefulfor.com/security/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<img src="http://usefulfor.com/security/?ak_action=api_record_view&id=39&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://usefulfor.com/security/2007/12/16/xml-man-in-the-middle-xmitm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>check for robots.txt</title>
		<link>http://usefulfor.com/security/2007/10/23/check-for-robotstxt/</link>
		<comments>http://usefulfor.com/security/2007/10/23/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[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://usefulfor.com/security/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<span id="more-32"></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://usefulfor.com/security/files/2008/06/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   ...</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]   ...</pre></div></div>
<p>Let'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 "-p" 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, $HTTP 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://usefulfor.com/security/?ak_action=api_record_view&id=32&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://usefulfor.com/security/2007/10/23/check-for-robotstxt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ninja iptables for your server</title>
		<link>http://usefulfor.com/security/2007/09/14/ninja-iptables-for-your-server/</link>
		<comments>http://usefulfor.com/security/2007/09/14/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[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... And one of these layers is usually an iptables firewall installed in your server. Let'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's [...]]]></description>
			<content:encoded><![CDATA[<p>Security is often about layers on top of layers on top of layers... And one of these layers is usually an <code>iptables</code> firewall installed in your server. Let's create a small script to provide our server with the <a href="/security/files/2008/06/mastering_kung-fu.jpg">kung-fu fighting techniques</a> needed to defeat the black hats!!<br />
<span id="more-27"></span><br />
You can download the script from <a href="http://usefulfor.com/security/files/2008/06/firewall.sh">here</a>. But let'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
### 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://usefulfor.com/security/files/2008/06/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://usefulfor.com/security/?ak_action=api_record_view&id=27&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://usefulfor.com/security/2007/09/14/ninja-iptables-for-your-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>runningserver: hello? anybody out there?</title>
		<link>http://usefulfor.com/security/2006/12/20/runningserver-hello-is-there-anybody-out-there/</link>
		<comments>http://usefulfor.com/security/2006/12/20/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[Shell Script]]></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'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's begin with the script. Then we can talk about the work it does.<br />
<span id="more-18"></span><br />
Here is the <a href="http://usefulfor.com/security/files/2008/06/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 ]  [  ...]

Options:
-h/-help/--help:  This help message
-p :  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 8080nn&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 compact 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'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't that difficult, was it? <img src='http://usefulfor.com/security/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://usefulfor.com/security/?ak_action=api_record_view&id=18&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://usefulfor.com/security/2006/12/20/runningserver-hello-is-there-anybody-out-there/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>icmp timestamps</title>
		<link>http://usefulfor.com/security/2006/12/14/icmp-timestamps/</link>
		<comments>http://usefulfor.com/security/2006/12/14/icmp-timestamps/#comments</comments>
		<pubDate>Thu, 14 Dec 2006 18:53:12 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Shell Script]]></category>

		<guid isPermaLink="false">http://weblog.nomejortu.com/?p=14</guid>
		<description><![CDATA[The Timestamp is an ICMP (rfc792) message which is used for time synchronization.  The Timestamp Reply  message consists of the originating timestamp sent by the sender of the Timestamp as well as a receive timestamp and a transmit timestamp.
If your machine answers  ICMP Timestamp messages an attacker can learn the date which [...]]]></description>
			<content:encoded><![CDATA[<p>The <em>Timestamp</em> is an <strong>ICMP</strong> (<a href="http://www.faqs.org/rfcs/rfc792.html">rfc792</a>) message which is used for time synchronization.  The <em>Timestamp Reply</em>  message consists of the originating timestamp sent by the sender of the Timestamp as well as a receive timestamp and a transmit timestamp.</p>
<p>If your machine answers  <strong>ICMP</strong> <em>Timestamp</em> messages an attacker can learn the date which is set on your machine. This may help him to defeat all your time based authentication protocols.</p>
<p><span id="more-16"></span><br />
Here is the <a href="http://usefulfor.com/security/files/2008/06/icmp-ts.sh">code</a> of a script that can be used to check if a remote host listens <em>Timestamp</em> requests:</p>
<div class="hl-surround" ><div class="hl-main"><pre># Check if the script is being run as root exit if it is not.
if [ &quot;$UID&quot; -ne &quot;0&quot; ]
then
  echo &quot;[ERROR] This script must be run as root&quot;
  exit 1
fi

for foo in $*; do
  echo -n &quot;$foo &quot;
  output=`hping3 -c 3 --icmp-ts $foo 2&gt;/dev/null | grep &quot;ICMP timestamp&quot; | wc -l`
  if (( output &gt; 0  ))
  then
    echo &quot;reacts to ICMP timestamp.&quot;
  else
    echo &quot;doesn't react.&quot;
  fi
done</pre></div></div>
<p>First we need to check that <code>root</code> is the one running the script because otherwise we won't be able to craft <strong>ICMP</strong> packages. For this task we will be using <a href="http://www.hping.org/">hping</a> (i.e: <strong>hping3</strong> package in Debian GNU/Linux).</p>
<p>The script just sends three (<code>-c 3</code>) <strong>ICMP</strong> Timestamps (<code>--icmp-ts</code>) to each of the hosts feeded in the command line. We grep the output of <code>hping3</code> looking for the <em>magic</em> string "<code>ICMP timestamp</code>", and if found, we print a success message.</p>
<img src="http://usefulfor.com/security/?ak_action=api_record_view&id=16&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://usefulfor.com/security/2006/12/14/icmp-timestamps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
