You are not logged in.
r3s
Last edited by Jebususu (2013-02-04 16:43:58)
Offline
It might help if you said what you actually wanted it to do.
Without knowing the goals, the main thing that jumps out at me is a long awkward series of greps, seds, and awks followed by a loop through each matching element that does some counting and prints out some information. *ALL* that should be replaced by a single awk invocation.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
Using the builtin test [[ rather than [ which is an executable will may be speed the script up. Also, have you thought about using fail2ban instead? It is a python script that runs a daemon and does the same thing.
Offline
Arch hasn't used tcp_wrappers for a long time, so this script is redundant. Adapt it to use iptables instead.
Offline
echo '
# /etc/hosts.deny
# See "man tcpd" and "man 5 hosts_access" as well as /etc/hosts.allow
# for a detailed description.
#Ian Morris
ALL: xx.xx.xx.xx' > /etc/hosts.deny
Use cat with here docs instead of multi-line echos
cat > /etc/hosts.deny <<EOT
# /etc/hosts.deny
# See "man tcpd" and "man 5 hosts_access" as well as /etc/hosts.allow
# for a detailed description.
#Ian Morris
ALL: xx.xx.xx.xx
EOT
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
echo '
# /etc/hosts.deny
# See "man tcpd" and "man 5 hosts_access" as well as /etc/hosts.allow
# for a detailed description.
#Ian Morris
ALL: xx.xx.xx.xx' > /etc/hosts.deny
Use cat with here docs instead of multi-line echos
cat > /etc/hosts.deny <<EOT
# /etc/hosts.deny
# See "man tcpd" and "man 5 hosts_access" as well as /etc/hosts.allow
# for a detailed description.
#Ian Morris
ALL: xx.xx.xx.xx
EOT
for IP in `/bin/grep sshd /var/log/secure|/bin/grep "Failed password"|awk -F" port" '{print $1}'|awk -F"from " '{print $2}'` 0.0.0.0; do
Either use a proper subshell or a while loop instead.
for IP in $(/bin/grep sshd /var/log/secure|/bin/grep "Failed password"|awk -F" port" '{print $1}'|awk -F"from " '{print $2}') 0.0.0.0; do
or
/bin/grep sshd /var/log/secure|/bin/grep "Failed password"|awk -F" port" '{print $1}'|awk -F"from " '{print $2}') | while read IP ; do
(Not sure why you have 0.0.0.0 in there too though, the second method can't use that)
Lastly, there's no sorting in there, so if an IP is seen multiple times, but with other IP's in between, you won't count them more than once.
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
You can replace the for-loop with something like this:
awk -v max_count="$MAXCOUNT" '
$6=="Failed" && $7=="password" && $NF=="ssh2" {
array[$11]++
}
END {
for(ip in array) {
if(array[ip] >= max_count) {
print "ALL:", ip
}
}
}' /var/log/secure.log >> /etc/hosts.deny
Offline