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 01-24-2014, 02:44 PM   #1
sandilovely24
LQ Newbie
 
Registered: Jan 2014
Posts: 12

Rep: Reputation: Disabled
firewall iptables rules (arch-user)


Can someone tell me about my iptables rules? It is important to understand my progress... Thanks a lot.

sudo iptables -L

Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere
DROP all -- anywhere anywhere ctstate INVALID
ACCEPT icmp -- anywhere anywhere icmp echo-request ctstate NEW
UDP udp -- anywhere anywhere ctstate NEW
TCP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN ctstate NEW
REJECT udp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- anywhere anywhere reject-with tcp-reset
REJECT all -- anywhere anywhere reject-with icmp-proto-unreachable
ACCEPT icmp -- anywhere anywhere icmp echo-request limit: avg 30/min burst 8
DROP icmp -- anywhere anywhere icmp echo-request
sshguard tcp -- anywhere anywhere tcp dpt:ssh
IN_SSH tcp -- anywhere anywhere tcp dpt:ssh ctstate NEW
IN_SSH tcp -- anywhere anywhere tcp dpt:ssh ctstate NEW
ACCEPT icmp -- anywhere anywhere icmp echo-request ctstate NEW
IN_SSH tcp -- anywhere anywhere tcp dpt:ssh ctstate NEW
UDP udp -- anywhere anywhere ctstate NEW

Chain FORWARD (policy DROP)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain IN_SSH (3 references)
target prot opt source destination
DROP all -- anywhere anywhere recent: CHECK seconds: 10 hit_count: 3 TTL-Match name: sshbf side: source mask: 255.255.255.255
DROP all -- anywhere anywhere recent: CHECK seconds: 1800 hit_count: 4 TTL-Match name: sshbf side: source mask: 255.255.255.255
ACCEPT all -- anywhere anywhere recent: SET name: sshbf side: source mask: 255.255.255.255

Chain TCP (1 references)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh

Chain UDP (2 references)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain

Chain sshguard (1 references)
target prot opt source destination


my "/etc/iptables/iptables.rules"

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:IN_SSH - [0:0]
:TCP - [0:0]
:UDP - [0:0]
:sshguard - [0:0]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NE$
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
-A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 30/min --limit-burst 8 -$
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
-A INPUT -p tcp -m tcp --dport 22 -j sshguard
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A IN_SSH -m recent --rcheck --seconds 10 --hitcount 3 --rttl --name sshbf --mas$
-A IN_SSH -m recent --rcheck --seconds 1800 --hitcount 4 --rttl --name sshbf --m$
-A IN_SSH -m recent --set --name sshbf --mask 255.255.255.255 --rsource -j ACCEPT
-A TCP -p tcp -m tcp --dport 22 -j ACCEPT
-A TCP -p tcp -m tcp --dport 80 -j ACCEPT
-A TCP -p tcp -m tcp --dport 443 -j ACCEPT
-A TCP -p tcp -m tcp --dport 22 -j ACCEPT
-A UDP -p udp -m udp --dport 53 -j ACCEPT
COMMIT
 
Old 01-24-2014, 03:27 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
What would you like to know? What every rule does? How a specific rule works?

Did you create the rules yourself, or are these the default rules on your system?

(Also, could you put lists like that in [code] tags? Makes it a lot easier to read.)
 
1 members found this post helpful.
Old 01-24-2014, 04:56 PM   #3
sandilovely24
LQ Newbie
 
Registered: Jan 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
I created this rules helped by internet. Can i do more to improve my safety? My setting does respect good rules to give me security? where can i change?

Last edited by sandilovely24; 01-24-2014 at 04:58 PM.
 
Old 01-24-2014, 05:12 PM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
There are some peculiar entries in that list of rules. How much do you know about iptables? That is, how detailed would you like the explanation to be?

Some of the lines from /etc/iptables/iptables.rules were truncated with a "$" character in your first post, I assume because you copied the text from a terminal window. Could you try reposting, preferably inside [code] tags?
 
Old 01-24-2014, 05:27 PM   #5
sandilovely24
LQ Newbie
 
Registered: Jan 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
Code:
$ sudo iptables -L

Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere
DROP all -- anywhere anywhere ctstate INVALID
ACCEPT icmp -- anywhere anywhere icmp echo-request ctstate NEW
UDP udp -- anywhere anywhere ctstate NEW
TCP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN ctstate NEW
REJECT udp -- anywhere anywhere reject-with icmp-port-unreachable
REJECT tcp -- anywhere anywhere reject-with tcp-reset
REJECT all -- anywhere anywhere reject-with icmp-proto-unreachable
ACCEPT icmp -- anywhere anywhere icmp echo-request limit: avg 30/min burst 8
DROP icmp -- anywhere anywhere icmp echo-request
sshguard tcp -- anywhere anywhere tcp dpt:ssh
IN_SSH tcp -- anywhere anywhere tcp dpt:ssh ctstate NEW
IN_SSH tcp -- anywhere anywhere tcp dpt:ssh ctstate NEW
ACCEPT icmp -- anywhere anywhere icmp echo-request ctstate NEW
IN_SSH tcp -- anywhere anywhere tcp dpt:ssh ctstate NEW
UDP udp -- anywhere anywhere ctstate NEW

Chain FORWARD (policy DROP)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain IN_SSH (3 references)
target prot opt source destination
DROP all -- anywhere anywhere recent: CHECK seconds: 10 hit_count: 3 TTL-Match name: sshbf side: source mask: 255.255.255.255
DROP all -- anywhere anywhere recent: CHECK seconds: 1800 hit_count: 4 TTL-Match name: sshbf side: source mask: 255.255.255.255
ACCEPT all -- anywhere anywhere recent: SET name: sshbf side: source mask: 255.255.255.255

Chain TCP (1 references)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh

Chain UDP (2 references)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain

Chain sshguard (1 references)
target prot opt source destination
And then ...

Code:
 $ sudo nano /etc/iptables/iptables.rules

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:IN_SSH - [0:0]
:TCP - [0:0]
:UDP - [0:0]
:sshguard - [0:0]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NE$
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
-A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 30/min --limit-burst 8 -$
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
-A INPUT -p tcp -m tcp --dport 22 -j sshguard
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A IN_SSH -m recent --rcheck --seconds 10 --hitcount 3 --rttl --name sshbf --mas$
-A IN_SSH -m recent --rcheck --seconds 1800 --hitcount 4 --rttl --name sshbf --m$
-A IN_SSH -m recent --set --name sshbf --mask 255.255.255.255 --rsource -j ACCEPT
-A TCP -p tcp -m tcp --dport 22 -j ACCEPT
-A TCP -p tcp -m tcp --dport 80 -j ACCEPT
-A TCP -p tcp -m tcp --dport 443 -j ACCEPT
-A TCP -p tcp -m tcp --dport 22 -j ACCEPT
-A UDP -p udp -m udp --dport 53 -j ACCEPT
COMMIT

Last edited by sandilovely24; 01-24-2014 at 05:41 PM.
 
Old 01-24-2014, 06:17 PM   #6
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
The tags helped, thanks. The same lines in /etc/iptables/iptables.rules are still truncated, though. I'll try to make sense of them by looking at the output from iptables -L.

You didn't say anything about how much you know about iptables, or if you're at all familiar with the way firewalls work in general, so I'll just comment and explain everything. (You had your chance... )

I don't know anything about how your Linux distribution works, but it seems /etc/iptables/iptables.rules is just a dump of the entire iptables ruleset, generated by iptables-save (just run iptables-save from the command line and you'll see). Evidently, your distribution uses this file to store the rules, and loads them using iptables-restore at bootup.

I will concentrate on the contents of this file as it appeared in your first post, as it contains the exact same rules as the list produced by iptables -L, unless you've added or removed rules since you last booted the system. (Well, actually iptables -L leaves out certain match criteria, so it's always best to dump the ruleset with iptables-save.)
Quote:
Originally Posted by sandilovely24 View Post
*filter
This says that the rules following this statement are to be loaded into the "filter" table. There are three tables: filter, which blocks or allows traffic; nat, which manipulates the addresses and/or port numbers in packets ("Network Address Translation"), and mangle, which alters or marks packets in different ways.

There are no "*nat" or "*mangle" statements anywhere, so it seems your system doesn't use the nat or mangle tables as all. This is not unusual for a standalone system.
Quote:
Originally Posted by sandilovely24 View Post
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:IN_SSH - [0:0]
:TCP - [0:0]
:UDP - [0:0]
:sshguard - [0:0]
Lines starting with a : sets the policy for a chain in a table. The policy is how the system deals with traffic that doesn't match any specific rule. The line
Code:
:INPUT DROP
sets the policy for the INPUT chain to "DROP", which means "drop any packets not explicitly allowed by a rule". It's the same as entering the command iptables -t filter -P INPUT DROP at the command line.

The filter table has three built-in chains: INPUT, FORWARD and OUTPUT. Any inbound traffic to the system itself is always processed by the rules in the INPUT chain, while traffic generated by the system is checked against the rules in the OUTPUT chain. The FORWARD chain is for inbound packets with a destination address that doesn't match any of the addresses assigned to the system, and thus need to be forwarded (routed). Only routers need rules in this chain.

In addition, your system has four user defined chains: "IN_SSH", "TCP", "UDP" and "sshguard". Such chains can contain rules just like the predefined chains, but will do nothing by themselves. They can be called from rules in any of the predefined chains. To understand the purpose of such chains, one has to look at how they are called and what rules they contain.
Quote:
Originally Posted by sandilovely24 View Post
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
These first and last of these rules in the INPUT chain deals with packets matching a certain "state". The Linux firewall keeps track of packets going in and out, and is able to determine whether a packet is a reply to a previous outbound packet, if it's a new request, or perhaps neither.

The first rule says "packets matching the Connection Tracker status RELATED or ESTABLISHED should be handled by the ACCEPT target". ESTABLISHED means a packet is a reply to a previous packet, while RELATED means the firewall considers it to be related to some other traffic, like an error message or a data stream opened by some allowed protocol. The "ACCEPT" target simply allows the packets in question.

The third rule sends packets matching the INVALID state (meaning the firewall can't make heads or tails of it) to the DROP target, which simply discards the packet.

The second rule simply allows internal traffic using the "lo" network interface. Traffic to this interface should never be blocked.
Quote:
Originally Posted by sandilovely24 View Post
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
This allows ICMP type 8 packets matching the NEW state. ICMP type 8 is "Echo" (better known as "ping"), and NEW means an incoming "ping" request from another host. In other words, others are allowed to "ping" your system. (Ping replies are not handled by this rule, but would be covered by the very first rule for ESTABLISHED packets.)
Quote:
Originally Posted by sandilovely24 View Post
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NE$
The latter of these rules has been truncated, as you can see. What is says, is "all TCP packets with the TCP flags FIN, SYN, RST, or ACK which are matching the state NEW [probably] should be sent [somewhere]". From the output from iptables -L I can see that "somewhere" is in fact the user-defined chain "TCP". Apparently, any rules pertaining to TCP traffic can be found in this chain.

This makes sense in the context of the previous rule, which sends all UDP packets matching the NEW status to the user-defined chain called "UDP".

Quote:
Originally Posted by sandilovely24 View Post
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
The first of these rejects all UDP packets. To reach this rule, a UDP packet would have to match neither of the above rules, or be returned from the "UDP" chain, either manually through the RETURN target or because there were no matching rules in that chain.

The next rule does the same for TCP traffic, and the last handles any non-TCP/UDP traffic in much the same way. This is probably a mistake, as it means the remaining rules in this chain are useless.
Quote:
Originally Posted by sandilovely24 View Post
-A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 30/min --limit-burst 8 -$
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
Another truncated rule here, one which handles ICMP packets. Not many such packets would get this far (only incoming pings, error messages and replies to outgoing pings, I believe), but this rules throttles how many such packets the system will allow through.

The first rule sends up to 30 packets/minute to the ACCEPT chain (again, the output from iptables -L provided the clue), and then ICMP type 8 packets are dropped by the second rule. As type 8 packets were explicitly allowed by an earlier rule, no traffic will ever match the second rule.
Quote:
Originally Posted by sandilovely24 View Post
-A INPUT -p tcp -m tcp --dport 22 -j sshguard
Any incoming TCP packets to port 22 is sent to the "sshguard" user-defined chain. TCP port 22 is used by the SSH daemon, and presumably the "sshguard" chain is supposed to limit incoming SSH traffic in some way, but the chain is actually empty. As a result, any SSH packet will simply hit the end of that chain and return here to be processed by the rules below.
Quote:
Originally Posted by sandilovely24 View Post
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
OK, more SSH rules. More than needed, actually; the first rule sends all SSH packets matching the NEW state to the "IN_SSH" user-defined chain. The next does exactky the same, which makes little sense.

Then follows three equally redundant rules; one allowing incoming "ping" packets (already allowed by an earlier rule), one sending SSH packets to the "IN_SSH" chain once again (could be problematic; see the IN_SSH chain below), and one sending UDP packets to the "UDP" chain (already done).
Quote:
Originally Posted by sandilovely24 View Post
-A IN_SSH -m recent --rcheck --seconds 10 --hitcount 3 --rttl --name sshbf --mas$
-A IN_SSH -m recent --rcheck --seconds 1800 --hitcount 4 --rttl --name sshbf --m$
-A IN_SSH -m recent --set --name sshbf --mask 255.255.255.255 --rsource -j ACCEPT
These three lines add three rules to the "IN_SSH" user-defined chain. The first two are truncated, but it's clear that in combination, these rules are meant to limit the amount of inbound SSH requests. The first rule keeps track of SSH packets and will only allow 3 such packets inside any 10 second window. The second allows up to 4 packets inside any 30 minute window. The last rule just creates/increments the counter.

Note that only SSH packets not matching the ESTABLISHED state will get far enough down the INPUT chain to reach the rule sending them to the "IN_SSH" chaion, so established SSH sessions will not be throttled or time out due to these rules. They probably exist to prevent brute force or dictionary password cracking attempts.

Unfortunately, there are THREE rules in the INPUT chain sending SSH packets to "IN_SSH". Each time, the packet is counted and control returned to the INPUT chain. As a result, even a single SSH packet is enough to fill the "max 3 packets per 10 second interval" quota, and a second attempt will also trigger the "max 4 packets per 30 minute interval" rule.
Quote:
Originally Posted by sandilovely24 View Post
-A TCP -p tcp -m tcp --dport 22 -j ACCEPT
-A TCP -p tcp -m tcp --dport 80 -j ACCEPT
-A TCP -p tcp -m tcp --dport 443 -j ACCEPT
-A TCP -p tcp -m tcp --dport 22 -j ACCEPT
The "TCP" chain accepts TCP traffic to ports 80, 443 and 22. Port 22 is SSH, but the first two ports are for web traffic. Are you running a web server on this system?

These rules are invoked when a packet reaches the corresponding rule in the INPUT chain, sending them to this chain for further processing.
Quote:
Originally Posted by sandilovely24 View Post
-A UDP -p udp -m udp --dport 53 -j ACCEPT
This rule allows inbound traffic to UDP port 53, which is the default port for DNS servers. If you're not running a publicly available DNS server on your system, the rule makes little sense and could possibly represent a security problem.

So, to sum it all up:
  • Your INPUT chain allows "ping" and replies to outbound traffic (the ESTABLISHED and RELATED states)
  • The INPUT chain sends SSH packets through the "sshguard" chain, then to the IN_SSH chain (both user-defined)
  • TCP and UDP traffic is sent from the INPUT chain to the "TCP" and "UDP" user-defined chains respectively
  • The "sshguard" chain does nothing
  • The "IN_SSH" chain limits incoming SSH login attempts (returns non-blocked traffic to the calling chain)
  • The "TCP" chain allows incoming HTTP, HTTPS and SSH packets (returns non-allowed traffic to the calling chain)
  • The "UDP" chain allows DNS requests (returns non-allowed traffic to the calling chain)
  • Unmatched packets reaching the bottom of the INPUT chain is dropped by the DROP policy
  • The OUTPUT chain has no rules and an ALLOW policy
  • The FORWARD chain has no rules and a DROP policy
Possible issues with this setup are:
  1. There's a blanket REJECT rule in the middle of the INPUT chain, blocking potentially useful traffic that's clearly meant to be let through (like SSH)
  2. SSH connection attempts are counted three times, which will trigger the blocking rules much earlier than intended
  3. Inbound traffic to the local web server is allowed, as well as DNS traffic. Unless you're running a web server (could be) and a public DNS server (highly unlikely), these rules should be removed.
And then a few observations (which are not errors):

<pedantic value="nitpick">

The "IN_SSH" chain does not really behave as one would expect. There's a rule in the INPUT chain sending SSH traffic to this chain, and I for one would expect the chain to handle anything and everything related to SSH. Instead, it blocks some SSH traffic and returns the rest, so there has to be an additional rule in the INPUT chain allowing SSH. I'd probably just stick the ACCEPT rule at the bottom of the SSH chain instead.

Same for the "TCP" and "UDP" chains, but with regards to blocked traffic. Once a packet is sent to the "TCP" or "UDP" chain for processing, I'd expect that to be the last I saw of it. Instead, the chains simply allow some traffic and return the rest. Why not block the rest instead, as that's clearly the intention anyway?

</pedantic>

Last edited by Ser Olmy; 01-24-2014 at 06:25 PM.
 
3 members found this post helpful.
Old 01-25-2014, 03:43 AM   #7
sandilovely24
LQ Newbie
 
Registered: Jan 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanks a lot!
 
Old 01-25-2014, 04:13 AM   #8
sandilovely24
LQ Newbie
 
Registered: Jan 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
Truncated rule

Code:
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j TCP
The mistake

Code:
-A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 30/min --limit-burst 8 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
Removing one

Code:
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
"IN_SSH" truncated rules

Code:
-A IN_SSH -m recent --rcheck --seconds 10 --hitcount 3 --rttl --name sshbf --mask 255.255.255.255 --rsource -j DROP
-A IN_SSH -m recent --rcheck --seconds 1800 --hitcount 4 --rttl --name sshbf --mask 255.255.255.255 --rsource -j DROP
-A IN_SSH -m recent --set --name sshbf --mask 255.255.255.255 --rsource -j ACCEPT
Removing

Code:
-A UDP -p udp -m udp --dport 53 -j ACCEPT
-A TCP -p tcp --dport 80 -j ACCEPT
-A TCP -p tcp --dport 443 -j ACCEPT

Last edited by sandilovely24; 01-25-2014 at 04:25 AM.
 
Old 01-25-2014, 04:15 AM   #9
sandilovely24
LQ Newbie
 
Registered: Jan 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
So...

Code:
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:IN_SSH - [0:0]
:TCP - [0:0]
:UDP - [0:0]
:sshguard - [0:0]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j TCP
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
-A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 30/min --limit-burst 8 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
-A INPUT -p tcp -m tcp --dport 22 -j sshguard
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A IN_SSH -m recent --rcheck --seconds 10 --hitcount 3 --rttl --name sshbf --mask 255.255.255.255 --rsource -j DROP
-A IN_SSH -m recent --rcheck --seconds 1800 --hitcount 4 --rttl --name sshbf --mask 255.255.255.255 --rsource -j DROP
-A IN_SSH -m recent --set --name sshbf --mask 255.255.255.255 --rsource -j ACCEPT
-A TCP -p tcp -m tcp --dport 22 -j ACCEPT
COMMIT

Last edited by sandilovely24; 01-25-2014 at 04:24 AM.
 
Old 01-25-2014, 05:19 AM   #10
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 sandilovely24 View Post
Code:
-A INPUT -i lo -j ACCEPT
Netfilter checks rules in order they're loaded, so it's better to have this one as the first rule as it gets all the loopback traffic out of the way.


Quote:
Originally Posted by sandilovely24 View Post
Code:
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
Duplicate rules.
No rules in "UDP" chain.


Quote:
Originally Posted by sandilovely24 View Post
Code:
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
Doesn't make sense to have reject rules here if your filter table INPUT chain default policy is DROP already.


Quote:
Originally Posted by sandilovely24 View Post
Code:
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
More duplicate rules.


Quote:
Originally Posted by sandilovely24 View Post
Code:
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j TCP
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
No rules in "TCP" chain.
filter table INPUT chain default policy is DROP already.


Quote:
Originally Posted by sandilovely24 View Post
Code:
-A INPUT -p tcp -m tcp --dport 22 -j sshguard
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
Unless the "sshguard" chain last rule does a "-j RETURN" the second rule makes no sense as its already jumped to the "sshguard" chain?..
Its also suggested the first rule has a "--ctstate NEW" added.


Quote:
Originally Posted by sandilovely24 View Post
Code:
-A IN_SSH -m recent --rcheck --seconds 10 --hitcount 3 --rttl --name sshbf --mask 255.255.255.255 --rsource -j DROP
-A IN_SSH -m recent --rcheck --seconds 1800 --hitcount 4 --rttl --name sshbf --mask 255.255.255.255 --rsource -j DROP
-A IN_SSH -m recent --set --name sshbf --mask 255.255.255.255 --rsource -j ACCEPT
You may have particular ideas about doing it this way but to me three "-m recent" checks seems overkill?


*BTW when working with iptables rule sets it's often more efficient, better to post
Code:
iptables-save
output instead as it lists all the actual rules across all tables.
 
Old 01-25-2014, 12:12 PM   #11
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
No rules in "UDP" chain.
Quote:
Originally Posted by unSpawn View Post
No rules in "TCP" chain.
But there are rules in both those chains.
Quote:
Originally Posted by unSpawn View Post
filter table INPUT chain default policy is DROP already.
DROP is not the same as REJECT. For the system to return an ICMP error message, REJECT must be used (and it can't be uses as a policy target).
Quote:
Originally Posted by unSpawn View Post
Unless the "sshguard" chain last rule does a "-j RETURN" the second rule makes no sense as its already jumped to the "sshguard" chain?..
If you reach the end of a user-defined chain, RETURN is implied.
Quote:
Originally Posted by unSpawn View Post
You may have particular ideas about doing it this way but to me three "-m recent" checks seems overkill?
The three rules do very different things. Look closer.
Quote:
Originally Posted by unSpawn View Post
*BTW when working with iptables rule sets it's often more efficient, better to post
Code:
iptables-save
output instead as it lists all the actual rules across all tables.
Agreed. It seems /etc/iptables/iptables.rules on this distribution is just such a dump, however.
 
Old 01-26-2014, 02:29 PM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Thanks for the corrections, I shouldn't post in haste.
 
Old 01-29-2014, 07:34 AM   #13
sandilovely24
LQ Newbie
 
Registered: Jan 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
Code:
$ sudo iptables-save

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [722:103734]
:IN_SSH - [0:0]
:TCP - [0:0]
:UDP - [0:0]
:sshguard - [0:0]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j TCP
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable
-A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 30/min --limit-burst 8 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -m recent --set --name ping_limiter --mask 255.255.255.255 --rsource
-A INPUT -p icmp -m icmp --icmp-type 8 -m recent --update --seconds 4 --hitcount 6 --name ping_limiter --mask 255.255.255.255 --rsource -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j IN_SSH
-A INPUT -p tcp -m tcp --dport 22 -j sshguard
-A IN_SSH -m recent --rcheck --seconds 10 --hitcount 3 --rttl --name sshbf --mask 255.255.255.255 --rsource -j DROP
-A IN_SSH -m recent --rcheck --seconds 1800 --hitcount 4 --rttl --name sshbf --mask 255.255.255.255 --rsource -j DROP
-A IN_SSH -m recent --set --name sshbf --mask 255.255.255.255 --rsource -j ACCEPT
-A TCP -p tcp -m tcp --dport 22 -j ACCEPT
Code:
$sudo iptables -nvL

Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 2887 2598K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    7   420 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
   15   600 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    0     0 UDP        udp  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate NEW
    0     0 TCP        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:0x17/0x02 ctstate NEW
    0     0 REJECT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with tcp-reset
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-proto-unreachable
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8 limit: avg 30/min burst 8
    0     0 DROP       icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8
    0     0            icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8 recent: SET name: ping_limiter side: source mask: 255.255.255.255
    0     0 DROP       icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8 recent: UPDATE seconds: 4 hit_count: 6 name: ping_limiter side: source mask: 255.255.255.255
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8
    0     0 IN_SSH     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW
    0     0 sshguard   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 2516 packets, 397K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_SSH (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            recent: CHECK seconds: 10 hit_count: 3 TTL-Match name: sshbf side: source mask: 255.255.255.255
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            recent: CHECK seconds: 1800 hit_count: 4 TTL-Match name: sshbf side: source mask: 255.255.255.255
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            recent: SET name: sshbf side: source mask: 255.255.255.255

Chain TCP (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain UDP (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain sshguard (1 references)
 pkts bytes target     prot opt in     out     source               destination
Added 3 new rules and removed 1 line:

Code:
$ sudo iptables -A INPUT -p icmp --icmp-type echo-request -m recent --name ping_limiter --set
$ sudo iptables -A INPUT -p icmp --icmp-type echo-request -m recent --name ping_limiter --update --hitcount 6 --seconds 4 -j DROP
$ sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT

$ sudo iptables -D INPUT -p icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT

Last edited by sandilovely24; 01-29-2014 at 01:39 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
wrong iptables rules on firewall beliya Linux - Software 4 11-03-2013 02:53 PM
Writing firewall rules with iptables bzlaskar Linux - Security 1 02-27-2007 07:41 AM
help with firewall rules .. ipfilter<-->iptables playahater Linux - Security 1 03-05-2006 04:00 AM
Question about IPtables/firewall rules ilan1 Linux - Security 3 02-20-2006 11:58 PM
Firewall Rules for daemons (Iptables) robeb Linux - Security 5 05-31-2002 04:27 PM

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

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