LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 08-16-2005, 06:16 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
SSH and IPTABLES


I am constantly being attacked by scripted kiddies trying to gain access through SSH. I would like to add a statement in my firewall via IPTABLES to restricting SSH access from untrusted hosts via iptables. I would like a statement that would only allow the IP address that I specify are legitiment or a certain range of IPs that can access my linux box via SSH. Any recommendations?
 
Old 08-16-2005, 06:32 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Sure - read the sticky thread about those at the top
of this very forum ;}

http://www.linuxquestions.org/questi...hreadid=340366



Cheers,
Tink

Last edited by Tinkster; 08-16-2005 at 06:38 PM.
 
Old 08-16-2005, 07:36 PM   #3
int0x80
Member
 
Registered: Sep 2002
Posts: 310

Rep: Reputation: Disabled

#!/bin/sh

face="eth0"
allowed_ssh="1.1.1.1 2.2.2.2 3.3.3.3"
# set default policy to drop
iptables -P INPUT DROP

# white list allowed_ssh
for host in $allowed_ssh; do
iptables -A INPUT -i $face -p tcp -s $host --dport 22 -j ACCEPT
done
 
Old 08-26-2005, 05:22 PM   #4
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
I an tired of seeing these script kiddie attempts in my var/log/messages. By adding this firewall script or just that statement will it end the var/log/messages entries of these attempts or will I always see these attempts from these idiots?
 
Old 08-26-2005, 05:38 PM   #5
danimalz
Member
 
Registered: Jul 2005
Location: West Coast South, USA
Distribution: debian 3.1
Posts: 267

Rep: Reputation: 36
That would depend upon rules further down the table.

The rules stated in prev. post simply accept the ip addresses that
you specify as being 'white' or known and okay.

If you don't what logging of every other IP going to ssh, you need
another rule that will drop them at the end of your INPUT table.
 
Old 08-29-2005, 06:19 PM   #6
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
how about if I just change the port number from 22 to another?
 
Old 08-29-2005, 07:06 PM   #7
major.tom
Member
 
Registered: Jun 2003
Location: Canada
Distribution: Slackware (current); Gentoo (newbie)
Posts: 142

Rep: Reputation: 15
If your key authentication is working correctly (and I hope it is) this isn't 100% necessary. But it never hurts to be a little more safe...

You could just setup sshd to allow only connections from specific ip addresses/blocks. That could be achieved by adding something like the following to /etc/hosts.allow:

Code:
sshd: <ip1> <ip2> : deny
Or you could incorporate some dynamic blacklisting. There's a script called SSHblack that (which a little tweaking) should accomplish this. I would read the sticky at the top of this group if you haven't already. The link is in there, along with alot of good info.

Garry
 
Old 08-29-2005, 07:26 PM   #8
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
Can you post your iptables script as well, just so we can see how it's setup.
 
Old 08-30-2005, 03:17 PM   #9
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Here is my my iptables rules:


# Generated by iptables-save v1.2.8 on Thu Jun 2 23:32:55 2005
*nat
:PREROUTING ACCEPT [31:3208]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [48:2880]
-A POSTROUTING -o ppp0 -j MASQUERADE
COMMIT
# Completed on Thu Jun 2 23:32:55 2005
# Generated by iptables-save v1.2.8 on Thu Jun 2 23:32:55 2005
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [744:319548]
:block - [0:0]
-A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -j ACCEPT
-A INPUT -j block
-A INPUT -i ppp0 -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j DROP
-A INPUT -i ppp0 -m state --state INVALID,NEW -j DROP
-A FORWARD -j block
-A FORWARD -i ppp0 -m state --state INVALID,NEW -j DROP
-A FORWARD -m limit --limit 3/hour -j LOG
-A FORWARD -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 1/sec -j ACCEPT
-A FORWARD -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK RST -m limit --limit 1/sec -j ACCEPT
-A FORWARD -p icmp -m limit --limit 1/sec -m icmp --icmp-type 8 -j ACCEPT
-A FORWARD -i ppp0 -m state --state INVALID,RELATED,ESTABLISHED -j DROP
-A block -m state --state RELATED,ESTABLISHED -j ACCEPT
-A block -i ! ppp0 -m state --state NEW -j ACCEPT
-A block -j DROP
COMMIT
# Completed on Thu Jun 2 23:32:55 2005


give me some feedback!
 
Old 08-30-2005, 11:02 PM   #10
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
Well I'm only new to iptables/ipchains myself, I have never used the iptables-save command before, some of these rules don't look right to me. Have you create your own script with all the iptables rules in it? Most distros will have a script usually in the /etc/rc.d directory that runs when the pc boots up.

Most of the time it will be called rc.firewall or something similiar. I notice you have INPUT, FORWARD and OUTPUT rules set to ACCEPT, that's not a good idea very insecure, atleast have your INPUT and FORWARD rules set to DROP, you can have the OUTPUT rules set to ACCEPT.

Some of your INPUT rules are not specifying what input device they are comming in on. Also the FORWARD can say what device they are coming in on, but you are better to specify what device you are forwarding too.

The -p tcp I understand that, but not -m tcp but as I said I'm only new to this so I might be missing something.

I'm currently working on my own iptables script that I'm testing on my test router at home, would be glad to post it to give some ideas on how the rules should look.
 
Old 08-31-2005, 04:54 PM   #11
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
I would like to see you iptables rules. Can you please post them thanks.
 
Old 08-31-2005, 05:34 PM   #12
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
Here it is, it is only experimental at the moment but seems to work fine, I haven't had anyone look at it and let me know I'm on the right track with the rules. There is a lot of logging in the script, maybe too much of it for some people, half the script was sourced from different places on the net, the rest I wrote myself.

My beleif is you can never have too much logging, have to know what's coming in or out, one thing I like about the script is it filters tcp coming into and out of the network, which will help to reduce malicious activity in both directions. If you need me to explain any part of the script if you have trouble with it just ask.

Ok i tried to post it but the scripts to long to go on the forum so I link it from my website, just get it from here:

http://www.linuxclan.com/downloads/l...ty/rc.firewall
 
Old 08-31-2005, 05:52 PM   #13
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
holy smokes, that make my script look like a 2 year old tried to write it.cool let me look at it and I will give some feedback.
 
Old 08-31-2005, 07:17 PM   #14
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
Glad you like it
 
Old 09-05-2005, 02:51 AM   #15
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
I've made some adjustments to the script, it seems I did not have the rules exactly right.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
iptables ssh ruleman Linux - Security 3 05-07-2005 03:46 PM
iptables and ssh iomari Linux - Networking 6 10-01-2004 07:44 PM
ssh / iptables bruj3w Linux - Networking 17 08-05-2004 12:00 PM
iptables ssh priority? Emanon Linux - Networking 2 04-15-2004 02:22 AM
Ipchains/iptables/ssh RockerSWE Linux - Networking 6 03-18-2003 03:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 09:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration