LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-06-2013, 07:10 PM   #1
Pringle
LQ Newbie
 
Registered: May 2013
Posts: 17

Rep: Reputation: Disabled
Is my firewall preventing me from logging in via ssh?


Hello,

I have created a bash script for my firewall and I thought everything was fine until I logged out and tried to log back in again using ssh.

I only started learning about this a couple of days ago when I noticed many ip addresses in my /var/log/auth.log either attempting to brute force or single attacks.

Here is the script I am using and I thought it would allow all connection attempts apart from the ones I blacklisted.

Code:
#!/bin/bash

IPT=/sbin/iptables

#FLUSH
$IPT -F
$IPT -X

#SET DEFAULTS
$IPT -P OUTPUT ACCEPT
$IPT -P INPUT DROP
$IPT -P FORWARD DROP

#CREATE USER CHAINS
$IPT -N SERVICE

#ALLOWED INPUTS
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A INPUT -j SERVICE

#ALLOW RESPONSES
$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#ALLOWED SERVICES
$IPT -A SERVICE -p tcp --dport 22 -j ACCEPT

#SPECIFIC ATTACKS
#prevent local ip spoofing
$IPT -A INPUT -i !lo -s 127.0.0.0/8 -j DROP
#prevent smurf attack
$IPT -I INPUT -p icmp --icmp-type echo-request -m recent --set
$IPT -I INPUT -p icmp --icmp-type echo-request -m recent --update --seconds 2 --hitcount 1 -j DROP
#drop invalid tcp flags
$IPT -A INPUT -m state --state INVALID -j DROP
$IPT -A OUTPUT -m state --state INVALID -j DROP
$IPT -A FORWARD -m state --state INVALID -j DROP
#drop tcp flags that don't make sense
$IPT -t filter -A INPUT -p tcp --tcp-flags FIN,ACK FIN -j DROP
$IPT -t filter -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP
$IPT -t filter -A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP
$IPT -t filter -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
$IPT -t filter -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
$IPT -t filter -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP

#BLACKLIST
#My attackers
$IPT -A SERVICE -s 94.242.252.0/24 -p tcp -j DROP
$IPT -A SERVICE -s 178.172.235.0/24 -p tcp -j DROP
$IPT -A SERVICE -s 203.255.252.0/24 -p tcp -j DROP
$IPT -A SERVICE -s 221.12.174.0/24 -p tcp -j DROP
$IPT -A SERVICE -s 202.112.112.0/24 -p tcp -j DROP
#Leks' attackers
$IPT -A SERVICE -s 62.217.127.0/24 -p tcp -j DROP
$IPT -A SERVICE -s 106.3.242.0/24 -p tcp -j DROP
$IPT -A SERVICE -s 61.156.238.0/24 -p tcp -j DROP

#Allow Me
$IPT -A SERVICE -s 192.168.1.0/24 -p tcp -j ACCEPT

#SAVE AND DISPLAY
iptables-save > /etc/iptables.rules
$IPT --list
 
Old 05-06-2013, 08:46 PM   #2
Pringle
LQ Newbie
 
Registered: May 2013
Posts: 17

Original Poster
Rep: Reputation: Disabled
Further progress

I had been using ssh to access this machine on my local network and I was using the hostname

for example

Code:
ssh user@hostname
and it wasn't working. It was working before I ran my firewall script. I decided to try with ip

for example

Code:
ssh user@192.168.XXX.XXX
and this worked

Can anyone tell me what has caused the difference?
 
Old 05-07-2013, 01:37 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
- either use the firewall tools your system provides or use the default 'iptables-save' and 'iptables-restore' tools for displaying, saving and loading rule sets.
- start with the default rule set and modify that.
- if you're not well-versed in writing rules and like automatic blocking and reporting then best use as much available tools like fail2ban.

Here's what your filter table, simplified and re-ordered, could look like:
Code:
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -s 192.168.1.0/24 -m state --state NEW -j ACCEPT
-A INPUT -s 127.0.0.0/8 -j LOG --log-prefix "IN_lo-range "
-A INPUT -s 127.0.0.0/8 -j DROP
-A INPUT -p icmp --icmp-type echo-request -m recent --set
-A INPUT -p icmp --icmp-type echo-request -m recent --update --seconds 2 --hitcount 1 -j LOG --log-prefix "IN_icmp "
-A INPUT -p icmp --icmp-type echo-request -m recent --update --seconds 2 --hitcount 1 -j DROP
-A INPUT -m state --state INVALID -j LOG --log-prefix "IN_inv "
-A INPUT -m state --state INVALID -j DROP
-A INPUT -p tcp --tcp-flags FIN,ACK FIN -j DROP
-A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP
-A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP
-A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
-A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
-A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
-A OUTPUT -m state --state INVALID -j  --log-prefix "OUT_inv "
-A OUTPUT -m state --state INVALID -j DROP
COMMIT
Ponder why that could be.

*BTW your hostname vs IP address question probably points to faulty or non-existent DNS resolution (if local try /etc/hosts entries or a caching name server like Pdnsd, Dnsmasq or even ISC BINDs caching-nameserver).
 
1 members found this post helpful.
Old 05-07-2013, 07:55 PM   #4
Pringle
LQ Newbie
 
Registered: May 2013
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
*BTW your hostname vs IP address question probably points to faulty or non-existent DNS resolution (if local try /etc/hosts entries or a caching name server like Pdnsd, Dnsmasq or even ISC BINDs caching-nameserver).
Thank you, I added rules to accept packets from my router dns and this solved this problem for me.

I am still finding people trying to access my ssh port from unrecognised ip addresses. I am aware that I could just accept packets from specific ip addresses but I never know where I will log in from next so I don't want to end up blocking myself from getting in for example if I went to France for a few days. I have looked into using a DSA key which I could stick on my mobile phone's SD card and copy it into the authorized_keys file on any machine that I am going to use to connect to my server and then I can delete it once I am done. This seems like the best way forward.
 
Old 05-08-2013, 01:26 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
See http://www.linuxquestions.org/questi...tempts-340366/
 
Old 05-08-2013, 05:45 PM   #6
Pringle
LQ Newbie
 
Registered: May 2013
Posts: 17

Original Poster
Rep: Reputation: Disabled
Further progress

I checked out that link you sent me and I feel like this next question is more relevant to that one but that thread was closed. I have created a public key on my server and I can now login using this key. This makes me feel so much more secure now.

However, I have also found that when I tried to log in to another server that I use I couldn't get there. It told me that it too needed a public key even though there is no authorized_key file on there. Does my client machine now have to always use a public key? I also didn't edit the config file on there to disable password access.

The only thing I can think of is that the ssh-keygen tool did something.

Last edited by Pringle; 05-08-2013 at 05:48 PM. Reason: Added more info
 
Old 05-08-2013, 05:56 PM   #7
Pringle
LQ Newbie
 
Registered: May 2013
Posts: 17

Original Poster
Rep: Reputation: Disabled
I was wrong

Quote:
Originally Posted by pringle View Post
I have looked into using a DSA key which I could stick on my mobile phone's SD card and copy it into the authorized_keys file on any machine that I am going to use to connect to my server and then I can delete it once I am done. This seems like the best way forward.
After some research I realised that actually RSA is a better way forward and you actually copy the contents of the public key into the authorized_keys file on the server and then make sure you have the public key in ~/.ssh on any client you want to log in from.
 
Old 05-09-2013, 12:22 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Reading SSH documentation or generic instructions about enabling pubkey auth should show copying over the public key to the remote servers ~/.ssh/authorized_keys file is an integral part of getting pubkey auth to work, regardless of the chosen key type. Also see 'man ssh-copy-id'.
 
  


Reply



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
Preventing ssh service from starting at boot moxieman99 Ubuntu 1 09-22-2010 09:29 PM
Preventing internal network traffic with linux firewall pauldg123 Linux - Networking 3 07-03-2010 07:42 AM
Preventing SSH timeouts . Some clarification needed bzlaskar Linux - Server 1 06-08-2010 07:27 AM
LXer: Preventing SSH Dictionary Attacks With DenyHosts LXer Syndicated Linux News 0 02-19-2006 11:01 AM
Firewall is preventing gamespy from showing my quake server kidicarus Linux - Networking 0 11-25-2004 11:58 AM

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

All times are GMT -5. The time now is 12:13 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