<?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; Shell Script</title>
	<atom:link href="http://usefulfor.com/security/category/shell-script/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>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>
