LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-17-2024, 05:53 AM   #1
Jason.nix
Member
 
Registered: Feb 2023
Posts: 563

Rep: Reputation: 10
Post iptables and OUTPUT policy


Hello,
Are the following iptables rules wrong?
Code:
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -s "IP" -p tcp -m tcp --dport 20 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 20 -j DROP
-A INPUT -p tcp -m tcp --dport 30 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 30 -j DROP
-A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 2/sec -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
-A INPUT -p icmp -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -m state --state INVALID -j DROP
-A OUTPUT -d "IP" -p tcp -m tcp --dport 40 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 40 -j DROP
Thank you.
 
Old 02-17-2024, 12:36 PM   #2
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 786

Rep: Reputation: 250Reputation: 250Reputation: 250
Wrong for what? With a default accept output, you only are blocking tcp/40 outbound which I can't see a practical application for.
 
Old 02-18-2024, 12:05 AM   #3
Jason.nix
Member
 
Registered: Feb 2023
Posts: 563

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by jayjwa View Post
Wrong for what? With a default accept output, you only are blocking tcp/40 outbound which I can't see a practical application for.
Hello,
Thank you so much for your reply.
Not really. I have only allowed exit to port 40 and other ports are blocked.
 
Old 02-19-2024, 12:38 PM   #4
Jason.nix
Member
 
Registered: Feb 2023
Posts: 563

Original Poster
Rep: Reputation: 10
Hello,
No idea?

Thanks.
 
Old 02-19-2024, 02:17 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by Jason.nix View Post
Hello,
Thank you so much for your reply.
Not really. I have only allowed exit to port 40 and other ports are blocked.
No, you are doing just the opposite:

Quote:
Originally Posted by Jason.nix View Post
Hello,
Are the following iptables rules wrong?
Code:
...
-P OUTPUT ACCEPT
...
-A OUTPUT -d "IP" -p tcp -m tcp --dport 40 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 40 -j DROP
...Policy ACCEPT applies to everything else not handled above...
You are blocking tcp/40 except to "IP" (assuming that to be redacted) and accepting everything else.

Last edited by astrogeek; 02-19-2024 at 02:21 PM.
 
Old 02-20-2024, 12:31 AM   #6
Jason.nix
Member
 
Registered: Feb 2023
Posts: 563

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by astrogeek View Post
No, you are doing just the opposite:



You are blocking tcp/40 except to "IP" (assuming that to be redacted) and accepting everything else.
Hello,
Thank you so much for your reply.
1- How can I solve it? I want the server to be able to send data only to port number 40.

2- Is the following rule also wrong? I just want a specific IP address to be able to connect to port 20.
Code:
-A INPUT -s "IP" -p tcp -m tcp --dport 20 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 20 -j DROP
 
Old 02-20-2024, 08:09 AM   #7
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 786

Rep: Reputation: 250Reputation: 250Reputation: 250
If you want to block by default, you should set the default policy to block.
Code:
iptables -P OUTPUT DROP
iptables -A OUTPUT -d $IP -p tcp -m tcp --dport 40 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d $IP -p tcp -m tcp --dport 40 -j ACCEPT
You'll have to allow for DNS, if needed, and also mind ipv6 (if in use). Your second example does similar: it takes packets on the INPUT chain and then drops tcp/20 packets. If you want that type of rule, look into negation (! --dport 20). Unfortunately, I can't easily test these rules right now but I think you can see what I'm getting at.
 
Old 02-21-2024, 01:26 AM   #8
Jason.nix
Member
 
Registered: Feb 2023
Posts: 563

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by jayjwa View Post
If you want to block by default, you should set the default policy to block.
Code:
iptables -P OUTPUT DROP
iptables -A OUTPUT -d $IP -p tcp -m tcp --dport 40 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d $IP -p tcp -m tcp --dport 40 -j ACCEPT
You'll have to allow for DNS, if needed, and also mind ipv6 (if in use). Your second example does similar: it takes packets on the INPUT chain and then drops tcp/20 packets. If you want that type of rule, look into negation (! --dport 20). Unfortunately, I can't easily test these rules right now but I think you can see what I'm getting at.
Hello,
Thank you so much for your reply.
Why you changed DROP to ACCEPT in the second rule?
 
Old 02-21-2024, 12:16 PM   #9
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 786

Rep: Reputation: 250Reputation: 250Reputation: 250
Because the default policy is already DROP and everything is dropped by default. If you don't make any exception, nothing is allowed and you wanted tcp/40.

Quote:
-P, --policy chain target
Set the policy for the built‐in (non‐user‐defined) chain to the
given target. The policy target must be either ACCEPT or DROP.
 
  


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
How to solve Content-Security-Policy and Permissions-Policy? n00b_noob Linux - Server 3 02-27-2021 04:21 AM
Configure my Redhat directory server password policy and account lockout policy arunplanet Linux - Newbie 4 10-06-2012 08:59 AM
How to set the password policy and lockout policy bin_shell Linux - Security 4 03-24-2010 03:30 PM

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

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