LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Security (https://www.linuxquestions.org/questions/linux-security-4/)
-   -   Failed SSH login attempts (https://www.linuxquestions.org/questions/linux-security-4/failed-ssh-login-attempts-340366/)

Bjorkli 11-01-2004 08:06 AM

This is a script that I found in another tread (hopefully not this one). Not sure who to credit for this script. I have not tried it on my machine yet, but it seems to be a good script.

Seems to add offending IP (hosts) to the iptables (firewall) rules.
Code:

#!/bin/sh

# Settings:
iptables="/sbin/iptables"
blockchain="blocking"
blocktarget="blacklist"

# This program will match lines:
# Illegal user (userid) from (host)
# Failed password for (userid) from (host) (...)
# and adds (host) to the iptables blacklist chain
# $blockchain.
#
#This chain is cleared regularly by a separate
# script to let entries expire after a while.

while read mm dd hms localhostname sshd word1 word2 word3 word4 host1 host2 rest; do
if [ "$word1 $word2 $word4" = "Illegal user from" ]; then
$iptables -A $blockchain -s ${host1}/32 -j $blocktarget
elif [ "$word1 $word2 $word3 $host1" = "Failed password for from" ]; then
$iptables -A $blockchain -s ${host2}/32 -j $blocktarget
fi
done

Hope it helps...

BenODen 11-01-2004 09:48 AM

Personally, I think that script is to much of a heavy hammer. If I understand it correctly, if you fumble your own password once, the Ip
you're logging in from gets blocked.. It needs the other script (wherever it is) to back off a bit, and some counting of numbers of failures
is also in order it seems...

-Ben

dmoorhouse 11-01-2004 06:20 PM

I agree. That script looks promising. But counting the number of attempts would be great. I can see my deny chains becomming rather large :)

thanks for the script though. I'm going to look into it.

Hamsjael 11-11-2004 03:32 AM

How about this
 
It seems to me that these attacks try different usernames depending on what machine the attack is coming from.
I suspect that infected machines attacks with all users from the passwd file, although some user like "test" and "admin" seems constant.

Maybe this is naive, but how about this for a countermeasure:

Every time the sshd deamon get these attempts from illegal users the host should start pinging the address the attacks are coming from. This way a DoS situation will pretty soon affect the infected site, and the admins out there will be forced to take action, and clean up. ;-)

dont no if it would be illegal, and maybe it could be done in a more elegant way. but these attacks are really starting to annoy me.

An ssh server coming on the network will almost immedatly get its logs filled wwith this crap, and it has been goinig on for a LOT longer than the two weeks stated here.

Capt_Caveman 11-11-2004 09:57 AM

Actually there are several versions of this malware. The first one used a limited password dictionary while newer versions have a substantially larger one like this example:

http://www.k-otik.com/exploits/08202004.brutessh2.c.php

You can see that the usernames and passwords are hard-coded, including the users test, guest, and admin. It would also be fairly trivial to add more passwords and usernames to the dictionary it's using, which likely accounts for some of the variability. Also note from the code that this is not a worm and doesn't "infect" systems by itself. If you find this on your system, it was likely uploaded manually or by an automated attack script.

Also I would highly recommend against using countermeasures like that against machines attempting these attacks. You're more likely to get into trouble with your ISP than to have an effect on the attacker. If you take preventative measures like disabling root logins, using good passwords, or switching to key-based authentication, you should be secure.

Also note that the post containing the "last two weeks" comment was posted at the beginning of August.

----
Btw, Happy Birthday.

emetib 12-04-2004 01:20 PM

i haven't read this whole thread, but i'm curious about the hosts.allow.

can you specify a mac address to let through, instead of just an ip?
since i'll be using a laptop to access my server in varying locations.

thanks.

adjman 12-05-2004 02:52 AM

MAC Address
 
Yes, you can indeed specify MAC or Hardware addresses on the firewall for rules :

and would be done something like this

iptables -A INPUT -s any -m mac -mac-source 00:C7:8F:72:14 -j ACCEPT

You need to have compiled mac address matching into your kernel, or loaded the appropriate module.

Please correct me if I'm wrong chaps out there

Capt_Caveman 12-05-2004 04:25 AM

The problem with using mac addresses is that the machine connecting has to be on the local network due to the fact that mac addresses aren't remotely transmited (in OSI terminology they're utilized on the link layer not the network layer). So if you were connecting remotely over the internet, you couldn't filter based on the remote systems mac address.

jymbo 12-15-2004 07:33 PM

Portsentry will add the offender to hosts.deny AND add a KILLROUTE. 3 caveats though:

1.) You need to run sshd on a differnet port
2.) You need to configure portsentry to monitor port 22
3.) To prevent your routing table from getting flooded, set-up a cronjob to flush it every 2 days or so

whoisdevnull 12-20-2004 06:36 PM

Tried to read through all the posts here but couldn't make it ;-)

Don't know if this has already been posted, but there is some good information in a few places here.

A good summary: http://dev.gentoo.org/~krispykringle/sshnotes.txt

A blacklisting script: http://www.pettingers.org/code/SSHBlack.html

A look at what they do once they get in: http://www.security.org.sg/gtec/hone...diary=20041102

Sorry if these are dupes of other posts, we're six pages in here!

whoisdevnull 12-27-2004 05:55 PM

Here is another one. Haven't looked at it too closely, the script from a few posts above is working fine.


http://linux.newald.de/new_design/login_check.html



-Mike

IchBin 12-28-2004 11:15 PM

well there are a couple of IP's that I would like to give access. Is that possible?

flipcode 12-28-2004 11:57 PM

Yes, a sample iptables script would be:

Code:

IPT=/sbin/iptables

echo "Start of INPUT DROP"
        $IPT -N input-drop
        $IPT -t filter -A input-drop -j LOG --log-level info --log-prefix input-drop:
        $IPT -t filter -A input-drop -j DROP
echo "End of INPUT DROP"

echo "Start of INPUT ACCEPT"
        $IPT -N input-accept
        $IPT -t filter -A input-accept -j LOG --log-level info --log-prefix input-accept:
        $IPT -t filter -A input-accept -j ACCEPT
echo "End of INPUT ACCEPT"

echo "Start of INPUT Chain"
        $IPT -t filter -p tcp --dport 22 -s 203.215.112.231 -j input-accept
        $IPT -t filter -p tcp --dport 22 -s 203.215.112.232 -j input-accept
        $IPT -t filter -p tcp --dport 22 -s 203.215.112.233 -j input-accept
        $IPT -t filter -p tcp --dport 22 -j input-drop
        $IPT -t filter -p udp --dport 22 -j input-drop
echo "End of INPUT Chain"

Obviously you would modify the IPs as shown above. I also like to log connection acceptance and dropping.

Hope this helps.

flipcode 12-29-2004 04:43 AM

By the way, those log file entries are located in /var/log/messages just in case you wondered.

emetib 12-30-2004 06:26 PM

just read a nice little article and then did some more reading on this subject.

/etc/ssh/sshd_config
AllowGroups
This keyword can be followed by a list of group name patterns, separated by spaces. If specified, login is
allowed only for users whose primary group or supplementary group list matches one of the patterns. '*'
and '?' can be used as wildcards in the patterns. Only group names are valid; a numerical group ID is not
recognized. By default, login is allowed for all groups.

AllowUsers
This keyword can be followed by a list of user name patterns, separated by spaces. If specified, login is
allowed only for user names that match one of the patterns. '*' and '?' can be used as wildcards in the
patterns. Only user names are valid; a numerical user ID is not recognized. By default, login is allowed
for all users. If the pattern takes the form USER@HOST then USER and HOST are separately checked,
restricting logins to particular users from particular hosts.

that should help alot of people out.


All times are GMT -5. The time now is 10:46 PM.