Skriptimiskeeled aine aruanded 2010 sügis/SigmarMuuga/Bash: Difference between revisions

From ICO wiki
Jump to navigationJump to search
(Created page with '== Sigmar Muuga Bashi asjad == ===Kodutöö=== Skript, mis vaatab /var/log/auth.log faili ja võtab sealt enim ebaõnnestunud IP aadressid, mis üritasid teha sisse logimist SSH …')
 
 
Line 61: Line 61:
fi
fi
done
done
</source>
===Tunnitöö - failijagaja===
<source lang="bash">
#!/bin/bash
# Skript, mis jagaks etteantud grupile uue kausta
#
# Autor: Sigmar Muuga DK31
#
# Kasutamine: jaga.sh KAUST GRUPP <JAGATUD KAUST>
#
# Skript teeb järgnevat
# paigaldab samba (see osa pole kohustuslik)
# loob kausta KAUST (kui vaja)
# loob grupi GRUPP (kui vaja)
# lisab grupile sobivad read smb.conf faili ja teeb failiserveri teenusele #reload'i
usage="Usage: jaga.sh <yourfolder> <yourgroup>"
smb_conf="smb.conf"
smb_conf_new=$smb_conf".new"
# kaust peab olema m22ratud
if [ -z "$1" ]; then
echo "No folder specified."
echo $usage
exit 0
fi
# grupp peab olema m22ratud
if [ -z "$2" ]; then
echo "No group specified."
echo $usage
exit 0
fi
folder=$1
group=$2
# kontrollime, kas kaust on olemas ja kui pole, siis teeme selle
if [ ! -e $folder ]
then
echo "Folder does not exists, creating it now.."
mkdir $folder
echo "Folder created"
fi
group_count=$(grep -c --word-regexp $group "/etc/group")
# kontrollime, kas grupp on olemas, kui pole, loob selle
if [ $group_count -lt 1 ]
then
echo "Group "$group" does not exist, creating it..."
groupadd $group
echo "Group created"
fi
echo "So far so good, lets smash the smb.conf...\n"
cp $smb_conf $smb_conf_new
echo "["$folder"]" >> $smb_conf_new
echo " comment = "$folder" access share" >> $smb_conf_new
echo " path="$folder >> $smb_conf_new
echo " browseable = yes" >> $smb_conf_new
echo " valid users = @"$group >> $smb_conf_new
echo "Share added to smb.conf, now testing the conf..."
testparm -s $smb_conf_new
mv $smb_conf_new $smb_conf
echo "Restarting samba"
/etc.init.d/samba restart
echo "Folder shared!"
</source>
</source>

Latest revision as of 13:58, 28 January 2011

Sigmar Muuga Bashi asjad

Kodutöö

Skript, mis vaatab /var/log/auth.log faili ja võtab sealt enim ebaõnnestunud IP aadressid, mis üritasid teha sisse logimist SSH kaudu(lävend võiks näiteks olla 10 ebaõnnestunud katset) ning saadab need kas e-mailile või lisab tulemüüri reeglitesse(IPTABLES näiteks). Jah, ma tean, et on ka paremaid viise hoste blokeerida, näiteks kasutada hosts-allow parameetrit ssh konfiguratsioonis.

#!/bin/bash

# Skript, mis loeb läbi UNIX-i auth.log faili ja koostab IPTables block reeglid IP aadressidele,
# mille pealt on autentimine ebaõnnestunud vähemalt etteantud arv kordi

# käivitamise näide:
# sh check_ip.sh /var/log/auth.log /etc/iptables_blocked.txt

# Autor: Sigmar Muuga, DK31

# mitme ebaõnnestumise korral me IP blokeerime
BLOCK_LEVEL=5
# auth.log faili asukoht
AUTH_LOG_FILE=$1
# tüüpilised veasituatsioonid
ERROR_PATTERNS="error | illegal | not\ allowed"
# väljundfaili nimi (või asukoht)
IP_TABLES_FILE=$2

# kontrollime etteantud argumenti
if [ -z "$1" ]; then
	echo "No command-line arguments."
	exit 0
fi

if [ -z "$2" ]; then
	echo "No IPTABLES file specified."
	exit 0
fi

# kontrollime, et faili saaks kirjutada
touch "$IP_TABLES_FILE"
if [ -e $IP_TABLES_FILE ]; then
	echo "IPTABLES file: "$IP_TABLES_FILE
else
	echo "Cannot write IPTABLES file!"
	exit 0
fi

# itereerime yle logiridade
more $AUTH_LOG_FILE | egrep "$ERROR_PATTERNS" | while read line; do
	address=$(echo $line | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}')
	# kas on valiidne IP
	if [ ${#address} -gt 0 ]; then
		# loeme esinevuskorrad
		occurences=$(grep -c $address $AUTH_LOG_FILE)
		if [ $occurences -ge $BLOCK_LEVEL ]; then # kui on juba piisavalt feilinud IP
			added_occurences=$(grep -c $address $IP_TABLES_FILE)
			#kontrollime, et poleks juba lisatud
			if [ $added_occurences -lt 1 ]; then
				echo "Blacklisting "$address" with "$occurences" occurences"
				#kirjutame IPTABLES reegli
				echo "iptables -A INPUT -s "$address" -j DROP" >> $IP_TABLES_FILE
			fi
		fi
	fi
done