LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2018, 08:31 PM   #1
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Rep: Reputation: Disabled
Using iptables to block ports


I am trying to block 80/443 on my server using iptables (prevent anyone from attempting to access the ports on the outside). The first rule I have for input is this:
Code:
-A INPUT -i eth0 (this is my wan interface) -p tcp -m tcp --dport 80:443 -j DROP
I go to do a scan, and it returns, showing that ports 80 and 443 are open (TCP). Why are they still showing as open?
 
Old 02-27-2018, 12:48 AM   #2
ilesterg
Member
 
Registered: Jul 2012
Location: München
Distribution: Debian, CentOS/RHEL
Posts: 587

Rep: Reputation: 72
Paste the complete output of iptables -L, and are you sure the scan you're doing goes out of eth0?
 
Old 02-27-2018, 06:10 AM   #3
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Original Poster
Rep: Reputation: Disabled
Code:
-A INPUT -i eth0 -p tcp -m multiport --dports 80,443 -j DROP
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED
Default policy is set to DROP.

Last edited by sniper8752; 02-27-2018 at 06:26 AM.
 
Old 02-27-2018, 06:45 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Where and how do you do that scan. On the same server? The ports are not closed, the processes listening on the ports are still running.

You must run the test from the outside, going into Eth0.

For debugging, you can add a log entry. Note that log is non-terminating, so the traffic will continue after being logged.

Code:
iptables -A INPUT <matching rules here> -j LOG --log-prefix "INPUT:DROP:" --log-level 6
jlinkels
 
Old 02-27-2018, 02:20 PM   #5
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Original Poster
Rep: Reputation: Disabled
I scan from the outside (the Internet) with nmap.
 
Old 02-27-2018, 04:47 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
That is good. Then I am not sure IPTables will show the port as open while still denying a TCP connection. To be sure you can telnet into the port and see if you get "connection refused". Then you know the connection is denied.

And if you can connect, you should create that log entry to see if your rule matches.

Quote:
-A INPUT -i eth0 (this is my wan interface) -p tcp -m tcp --dport 80:443 -j DROP
You have a type here: -m tcp. In your second post it was corrected.

Since multiport is a module which is not installed by default (depending on your installation) try to skip the multiport and try a single port first. You can also create 2 rules. It is only 2 ports. Depending on how you enter the IPTables commands you might or might not see an error message on the multiport module.

jlinkels
 
Old 02-27-2018, 05:50 PM   #7
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Original Poster
Rep: Reputation: Disabled
I added this rule: -A INPUT -i eth0 -p tcp -m tcp --dport 80 -j DROP, but nmap still shows 80/tcp http.
 
Old 02-27-2018, 06:17 PM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by sniper8752 View Post
I added this rule: -A INPUT -i eth0 -p tcp -m tcp --dport 80 -j DROP, but nmap still shows 80/tcp http.
-m tcp is not really needed. Although I believe it is correct, it is hardly specified explicitly.
You really should try this and see if something is logged:
Code:
iptables -A INPUT -p tcp --dport 80 -j LOG --log-prefix "dropped access to port 80"
iptables -A INPUT -p tcp --dport 80 -j DROP
Did you try the telnet connection as I suggested?

If the log does not show anything and telnet does connect, you make wrong assumptions somewhere. So install tcpdump and run:
Code:
tcpdump -i eth0 port 80
while you telnet into your host.

It is plain impossible you see traffic on port 80 in tcpdump and you don't see anything in the log of IPTables.

jlinkels
 
Old 02-27-2018, 06:33 PM   #9
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Original Poster
Rep: Reputation: Disabled
So I've been thinking it might be better to just go to the root cause of the issue, and prevent apache2 from listening on the wan interface. I've modified ports.conf and sites-available/000-default.conf and default-ssl.conf, but a scan still shows the two ports. I'd rather not have to block them, and just have the product configured properly. Is there a configuration I'm missing somewhere, where the listening interface needs to be updated?
 
Old 02-27-2018, 06:46 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,678

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
Have you changed the apache listen address to just your LAN?

Rules operate in order so while you just posted the one for port 80 you could have a something misplaced. In addition I prefer to set the default polices to drop then add rules to allow incoming traffic.
 
Old 02-27-2018, 07:42 PM   #11
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by sniper8752 View Post
So I've been thinking it might be better to just go to the root cause of the issue, and prevent apache2 from listening on the wan interface. I've modified ports.conf and sites-available/000-default.conf and default-ssl.conf, but a scan still shows the two ports. I'd rather not have to block them, and just have the product configured properly. Is there a configuration I'm missing somewhere, where the listening interface needs to be updated?
Now I asked you 3 times to add a log target to IPtables and to try a telnet connection from the outside to your box. And run tcpdump dependent on the previous outcome.

See, if you don't ever issue a test command you can keep trying things until you see blue in the face.

jlinkels
 
Old 02-27-2018, 07:42 PM   #12
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Original Poster
Rep: Reputation: Disabled
Yes.
My default policies are set to drop.
 
Old 02-27-2018, 07:44 PM   #13
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jlinkels View Post
Now I asked you 3 times to add a log target to IPtables and to try a telnet connection from the outside to your box. And run tcpdump dependent on the previous outcome.

See, if you don't ever issue a test command you can keep trying things until you see blue in the face.

jlinkels
I will try this.
 
Old 02-27-2018, 07:52 PM   #14
sniper8752
Member
 
Registered: Oct 2012
Posts: 564

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jlinkels View Post
-m tcp is not really needed. Although I believe it is correct, it is hardly specified explicitly.
You really should try this and see if something is logged:
Code:
iptables -A INPUT -p tcp --dport 80 -j LOG --log-prefix "dropped access to port 80"
iptables -A INPUT -p tcp --dport 80 -j DROP
Did you try the telnet connection as I suggested?

If the log does not show anything and telnet does connect, you make wrong assumptions somewhere. So install tcpdump and run:
Code:
tcpdump -i eth0 port 80
while you telnet into your host.

It is plain impossible you see traffic on port 80 in tcpdump and you don't see anything in the log of IPTables.

jlinkels
Added rules. It says it connected, and I saw a few results while running tcpdump.
 
Old 02-28-2018, 04:11 AM   #15
ilesterg
Member
 
Registered: Jul 2012
Location: München
Distribution: Debian, CentOS/RHEL
Posts: 587

Rep: Reputation: 72
Quote:
Originally Posted by sniper8752 View Post
Added rules. It says it connected, and I saw a few results while running tcpdump.
Where are the logs?
 
  


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
Attempting to block all non-used ports with iptables. jddancks Linux - Security 12 10-19-2013 04:24 PM
Block all the ports excpet 80 on iptables (eth2) webboy105 Linux - Security 11 02-04-2010 03:17 PM
How to block high ports in iptables? sparc86 Linux - Security 1 12-01-2008 07:55 PM
block m$ related ports using iptables carboncopy Linux - Security 8 01-28-2005 12:30 PM
iptables: block ports and RELATED, ESTABLISHED Klaus Pforte Linux - Security 6 07-17-2003 10:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

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