LinuxQuestions.org
Help answer threads with 0 replies.
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-25-2018, 07:29 PM   #1
DevGuy
LQ Newbie
 
Registered: May 2018
Location: London
Distribution: CentOS 7.5
Posts: 25

Rep: Reputation: Disabled
Need evaluation of my firewall rules


I need an expert eye on my FW rules to see if everything is OK. I have been using these for a very long time and don't have any problems. I want to have these evaluated once and for all so that I don't ever have to come back to them.

I don't know what some of the rules are for and I am not sure if the rules are ordered correctly. Can anyone see any problems/weaknesses in them? Are there any rules I don't need and can be removed? The server handles HTTP, incoming-only SMTP delivery, and DNS requests. Thanks.


# Generated by iptables-save v1.4.21 on Sat May 26 00:03:57 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [716:124495]
-A INPUT -s xxx.xxx.xxx.xxx/32 -m comment --comment "all access from own IP" -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p udp -m udp --dport 53 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 53 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Sat May 26 00:03:57 2018
 
Old 05-25-2018, 08:59 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,317
Blog Entries: 28

Rep: Reputation: 6140Reputation: 6140Reputation: 6140Reputation: 6140Reputation: 6140Reputation: 6140Reputation: 6140Reputation: 6140Reputation: 6140Reputation: 6140Reputation: 6140
I'm certainly not an expert on firewall rules, but all the ports listed as open are ports that legitimately could be open for tcp, http, https, and email.
 
Old 05-26-2018, 04:15 AM   #3
DevGuy
LQ Newbie
 
Registered: May 2018
Location: London
Distribution: CentOS 7.5
Posts: 25

Original Poster
Rep: Reputation: Disabled
I don't have strong OUTPUT rules. Should I?
 
Old 05-26-2018, 08:55 AM   #4
luizlmarins
LQ Newbie
 
Registered: Nov 2012
Location: São Paulo
Distribution: Debian
Posts: 10
Blog Entries: 1

Rep: Reputation: Disabled
Simple Firewall Iptables for One Desktop Machine.

(POP, SMTP, and IMAP are commented)



#!/bin/bash
### Delete previous rules
iptables -F
### Accepting loopback
iptables -A OUTPUT -o lo -j ACCEPT
### Creating default policies
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
### Allow previously established connections to continue uninterupted
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
### Allow outbound connections on the ports we previously decided
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
### DNS
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 51413 -j ACCEPT
### HTTP
iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
### HTTPS
iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT
### POP
#iptables -A OUTPUT -p tcp --dport 995 -j ACCEPT
### IMAP
#iptables -A OUTPUT -p tcp --dport 993 -j ACCEPT
### SMTP
#iptables -A OUTPUT -p tcp --dport 587 -j ACCEPT
### BTtracker
iptables -A OUTPUT -p tcp --dport 51413 -j ACCEPT
### DHCP
iptables -A OUTPUT -p tcp --dport 6969 -j ACCEPT
iptables -A OUTPUT -p UDP --dport 67:68 -j ACCEPT
### Drop others input connections
iptables -A INPUT -p tcp --syn -j DROP


If you want to do it more simple yet, you can use only this two rules.
They give permission to access internet with security, without complications:



#!/bin/bash
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -p tcp --syn -j DROP



Credits:
Carlos Morimoto
Servidores Linux, guia prático
Editora Meridional, Porto Alegre
2008, p. 191

Last edited by luizlmarins; 05-26-2018 at 09:11 AM.
 
Old 05-26-2018, 10:43 AM   #5
DevGuy
LQ Newbie
 
Registered: May 2018
Location: London
Distribution: CentOS 7.5
Posts: 25

Original Poster
Rep: Reputation: Disabled
Your defaults are all drops, and mine are all accepts. Which is better? My rules are for a server. At the moment all outgoing traffic are allowed. This seems too relaxed.
 
Old 05-27-2018, 05:49 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by DevGuy View Post
I have been using these for a very long time and don't have any problems.
You may encounter problems on different levels. For now remember that continuous systems hardening and monitoring are a given, and that compromises most likely take place at OSI layer 7 (application) and 8 (wetware ;-p).


Quote:
Originally Posted by DevGuy View Post
I want to have these evaluated once and for all so that I don't ever have to come back to them.
You use of the machine should include regular review of security posture. On ounce of prevention and all that.


Quote:
Originally Posted by DevGuy View Post
I don't know what some of the rules are for and I am not sure if the rules are ordered correctly.
If you're a Dev guy doing Ops then your rudimentary knowledge should include basic Linux admin knowledge I'd say?


Quote:
Originally Posted by DevGuy View Post
Can anyone see any problems/weaknesses in them? Are there any rules I don't need and can be removed? The server handles HTTP, incoming-only SMTP delivery, and DNS requests.
Looks OK. Slight tweaks:

Code:
*filter
:INPUT ACCEPT [0:0]
# You're not forwarding so explicitly deny it.
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [716:124495]
# Get loopback device out of the way, also keeps you from having to use Ethernet device names if you have just one.
-A INPUT -i lo -j ACCEPT
# The next rules should be ordered "first match wins", meaning since it's a server you want to prioritize what you serve, yes?
# I've taken the liberty to make this an Internet-facing server so prioritize DNS over the rest and remote over local req's.
# Tweak or remove limits as you see fit. Also spot a new port for SSH:
-A INPUT -p udp -m conntrack --ctstate NEW -m udp --dport 53 -m limit --limit 10/s -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -m tcp --dport 53 -m limit --limit 5/s -j ACCEPT
-A INPUT -p tcp -m conntrack --ctstate NEW -m tcp -m multiport --dports 80,443 -m limit --limit 120/s -j ACCEPT
-A INPUT -p tcp -m conntrack --ctstate NEW -m tcp -m multiport --dports 22,25 -m limit --limit 1/s -j ACCEPT
-A INPUT -m conntrack --ctstate NEW RELATED,ESTABLISHED -j ACCEPT
-A INPUT -s xxx.xxx.xxx.xxx/32 -m comment --comment "all access from own IP" -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
Indeed your egress rule set is empty. You could populate it with a list of bogon ranges (https://www.team-cymru.org/Services/...ogons-ipv4.txt), common ports for say IRC and Bitcoin miners, but instead of that I'd invest time in basic hardening & monitoring.


HTH
 
Old 05-27-2018, 10:07 AM   #7
DevGuy
LQ Newbie
 
Registered: May 2018
Location: London
Distribution: CentOS 7.5
Posts: 25

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
If you're a Dev guy doing Ops then your rudimentary knowledge should include basic Linux admin knowledge I'd say?
Too much knowledge makes for too little time. I am happy to outsource to experts. Gives them a chance to use their skills before becoming rusty .

Based on your evaluation, I have modified my FW rules accordingly:

Quote:
# Generated by iptables-save v1.4.21 on Sun May 27 14:49:37 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [485:169191]
-A INPUT -s xxx.xxx.xxx.xxx/32 -m comment --comment "all access from own IP" -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport xxxxx -m comment --comment Apps -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -m udp --dport 53 -m limit --limit 10/sec -j ACCEPT
-A INPUT -p tcp -m conntrack --ctstate NEW -m tcp --dport 53 -m limit --limit 5/sec -j ACCEPT
-A INPUT -p tcp -m conntrack --ctstate NEW -m tcp -m multiport --dports 80,443 -m limit --limit 120/sec -j ACCEPT
-A INPUT -p tcp -m conntrack --ctstate NEW -m tcp --dport 25 -m limit --limit 10/sec -j ACCEPT
-A INPUT -s xxx.xxx.xxx.xxx/32 -p tcp -m tcp --dport xxxxxx -m comment --comment "backup access from secondary server" -j ACCEPT
-A INPUT -m conntrack --ctstate NEW,RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A OUTPUT -j LOG
COMMIT
# Completed on Sun May 27 14:49:37 2018

The "all access from own IP" rule remains at the top because that's a script generated entry (done on demand), and I don't know a way to generate it at an arbitrary line towards the bottom.

I have no idea what the appropriate limits should be. So, I am using the ones you indicated.

Port 25 SMTP limit is set to 10. No idea if this is good but seems more forgiving than 1.

Public access to SSH port 22 is not required. All SSH access will be private and granted by the "all access from own IP" rule.

I have decided not to add any OUTPUT rules but to set up log for observation. At the moment, most of the log is about port 22 which I don't need. How do I exclude port 22 from logging?
 
Old 05-27-2018, 04:22 PM   #8
DevGuy
LQ Newbie
 
Registered: May 2018
Location: London
Distribution: CentOS 7.5
Posts: 25

Original Poster
Rep: Reputation: Disabled
Had a fatal flaw in the following rule which let everything through:

Quote:
-A INPUT -m conntrack --ctstate NEW,RELATED,ESTABLISHED -j ACCEPT
It should be:

Quote:
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
 
Old 05-29-2018, 12:36 PM   #9
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
I don't use open rules to initialize. I set the filter tables to drop. I specifically block all private address spaces except what I'm using, and about half of ipv4 address space. But if you initialize with a drop policy, you have to specifically allow everything.
 
Old 05-29-2018, 04:19 PM   #10
DevGuy
LQ Newbie
 
Registered: May 2018
Location: London
Distribution: CentOS 7.5
Posts: 25

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
I don't use open rules to initialize. I set the filter tables to drop. I specifically block all private address spaces except what I'm using, and about half of ipv4 address space. But if you initialize with a drop policy, you have to specifically allow everything.
Can I see a short sample. If it makes sense to me, I'll make use of what you have.

My INPUT rules should be OK.

Not sure if it's because I don't know how to read the OUTPUT log, my server appears to be contacting IP's from all over the place. Not sure why. I could do with some control over that.
 
Old 05-30-2018, 12:19 PM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by DevGuy View Post
Had a fatal flaw in the following rule which let everything through
Good one! Thanks for posting back, missed that one myself.
 
Old 05-31-2018, 07:43 PM   #12
DevGuy
LQ Newbie
 
Registered: May 2018
Location: London
Distribution: CentOS 7.5
Posts: 25

Original Poster
Rep: Reputation: Disabled
Based on stats, the rule

Quote:
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
really needs to be at the top for best server efficiency, which may translate into better server response.


Stats:
Quote:
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1424 173K ACCEPT all -- * * xx.x.xxx.xx 0.0.0.0/0 /* all access from own IP */
0 0 DROP all -- * * xx.xx.xxx.x/24 0.0.0.0/0 /* email spammer */
2825 1608K ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
13 923 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 ctstate NEW udp dpt:53 /* DNS */ limit: avg 10/sec burst 5
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 ctstate NEW tcp dpt:53 /* DNS */ limit: avg 5/sec burst 5
17 948 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 ctstate NEW tcp multiport dports 80,443 limit: avg 120/sec burst 5
5 260 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 ctstate NEW tcp dpt:25 limit: avg 5/sec burst 5
0 0 ACCEPT tcp -- * * xxx.xxx.xxx.xxx 0.0.0.0/0 tcp dpt:xxxxx /* backup access from secondary server */
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:xxxxx /* Legacy user apps */
132K 417M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
1 28 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
3288 197K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy DROP 0 packets, 0 bytes)

My final rules are
Quote:
# Generated by iptables-save v1.4.21 on Fri Jun 1 00:30:42 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [29:4312]
-A INPUT -s xx.x.xxx.xx/32 -m comment --comment "all access from own IP" -j ACCEPT
-A INPUT -s xx.xx.xxx.x/24 -m comment --comment "email spammer" -j DROP
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -m udp --dport 53 -m comment --comment DNS -m limit --limit 10/sec -j ACCEPT
-A INPUT -p tcp -m conntrack --ctstate NEW -m tcp --dport 53 -m comment --comment DNS -m limit --limit 5/sec -j ACCEPT
-A INPUT -p tcp -m conntrack --ctstate NEW -m tcp -m multiport --dports 80,443 -m limit --limit 120/sec -j ACCEPT
-A INPUT -p tcp -m conntrack --ctstate NEW -m tcp --dport 25 -m limit --limit 5/sec -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -s xxx.xxx.xxx.xxx/32 -p tcp -m tcp --dport xxxxx -m comment --comment "backup access from secondary server" -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport xxxxx -m comment --comment "Legacy user apps" -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Fri Jun 1 00:30:42 2018

Last edited by DevGuy; 05-31-2018 at 07:48 PM.
 
  


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
Evaluation of firewall rules, simple AP on RPI pi_newb Linux - Security 2 10-05-2017 09:55 AM
Rules for Firewall kamrinjacobs Linux - Networking 1 02-24-2010 08:31 AM
firewall rules sulekha Linux - Networking 2 10-09-2008 02:30 AM
Firewall Rules studpenguin Linux - Security 0 07-01-2004 03:14 AM
help with firewall rules please deuce868 Linux - Security 1 06-14-2004 03:18 PM

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

All times are GMT -5. The time now is 08:55 PM.

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