IP Tables not allowing ports


 
Thread Tools Search this Thread
Special Forums Cybersecurity IP Tables not allowing ports
# 1  
Old 12-08-2013
IP Tables not allowing ports

Hi guys, I'm trying to configure iptables to only allow certain ports access.
I set the first set of rules to block everything and then subsequently open ports as needed, but everything still seems to be blocked.

I have read that the order matters (new to iptables), perhaps this is an issue. Google has not been very helpful.

What am I doing wrong here?

EDIT: OS is Debian and Iceweasel browser. If that's relevant.

Code:
#!/bin/bash

#Drop all
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

#Outbound
iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 7465 -j ACCEPT

#Inbound
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 7465 -j ACCEPT

#Forwarded
iptables -A FORWARD -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A FORWARD -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A FORWARD -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A FORWARD -p tcp -m tcp --dport 7465 -j ACCEPT


Last edited by 3therk1ll; 12-08-2013 at 12:55 AM..
# 2  
Old 12-08-2013
Think of the direction of the packet in the INPUT section. dport describes what?
Say your client software grabs a random local port, 68123, and starts the TCP handshake with an SSH service on 22 elsewhere. The reply comes back to you on 68123 and is ... DROP.

Generally the first rule looks like this:
Code:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

And should fix that
# 3  
Old 12-08-2013
Solved: As below works just fine.
Some sites were using other ports such as 8443 as an https alternative and 587 (submission) in the same role.

Code:
#Drop all
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

#Accept IN associated ports
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT


#Inbound
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 7465 -j ACCEPT

#Outbound
iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 8080 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 7465 -j ACCEPT

#Forwarded
iptables -A FORWARD -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A FORWARD -p tcp -m tcp --dport 8080 -j ACCEPT
iptables -A FORWARD -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A FORWARD -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A FORWARD -p tcp -m tcp --dport 7465 -j ACCEPT


Last edited by 3therk1ll; 12-08-2013 at 11:41 PM.. Reason: Solved
# 4  
Old 12-08-2013
Are you running services you want to allow access to, or are all these ports for outgoing traffic? Is this a server or desktop or router?

This would allow OUTGOING web traffic:
Code:
iptables -A OUTPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

ah i see you edited it. I'm guessing your individual INPUT rules are mostly useless, and you'd see 0 under them in iptables -L -v -n since your OUTPUT allows the connection request, and the "-m state" rule does the rest.

Last edited by neutronscott; 12-09-2013 at 12:17 AM..
# 5  
Old 12-09-2013
This is just for my laptop but it is something I'd like to implement on my desktop too at some point, just testing it here..
Is the 'FORWARDED' flag more for use with routers?

What I'm looking to do is drop all traffic and then only allow IN/OUT certain ports and services as I need them.

The problem I had before was websites I was connecting to had the default ports changed, ie 587 for example rather than 80.

I want the rules to block anything coming in that hasn't already been established by me initiating a connection.

Example:
I initiate to connect to a webserver on local 80
Webserver responds on 587 and the connection is allowed because I initiated it in the first place.
Ideally to happen without explicitly specifying each and every port/service individually.
If being explicit is the best or safest way, so be it, just want to get it done effectively.

Would it be best to only have
Code:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Rather than the same for
Code:
INPUT,OUTPUT,FORWARD

?

If that is the case, what should it look like based on my previous posts.

Much appreciate your help by the way buddy, sorry if I'm a little slow on the uptake!
# 6  
Old 12-09-2013
Basically you want to deny incoming packets that you didn't initiate? This is usually done in the router, but there are so many tutorials and howtos..

Having just that rule and a default policy of ALLOW on OUTPUT should do it though.

And yes, FORWARD should only matter if you're routing packets. It's likely not even enabled (it's a separate sysctl option)
# 7  
Old 12-09-2013
Yeah that is basically what I want to do.
Thanks for the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

One user to su to another without allowing root access and password

Hello Gurus, I want One user to su to another without allowing root access and password. I want to run a specific command as below from user am663: --------------------------------------------------------- sudo -u appsprj4 /home/appsrj4/scripts/start_apache.sh ------------------- But... (6 Replies)
Discussion started by: pokhraj_d
6 Replies

2. AIX

Ssh not allowing NIS user to login

As I do a ssh <nis_user>@server1 from server2, ssh prompts for certificates (as expected the first time), then it prompts for the users password, as soon as I enter the password, I get a Connection to server1 closed by remote host, and connection to server1 closed. and I disconnect back to the... (3 Replies)
Discussion started by: mrmurdock
3 Replies

3. UNIX for Dummies Questions & Answers

Allowing External Scans

Hello! I run an HP Unix system which I host oracle databases on, as well as oracle based apps used by my company. My IA department needs to scan my files to ensure I am following IA procedures and check for vulnerabilities in scripts etc. The scan is coming from corporate, and they asked for... (2 Replies)
Discussion started by: hpuxguy
2 Replies

4. Red Hat

Samba for anonymouse setup but not allowing me to write

Hi Friends, samba for annonymouse setup but not allowing me to write when i tried to browse from windows 7 box conf as below #testparm Load smb config files from /etc/samba/smb.conf Processing section "" Processing section "" Processing section "" Loaded services file OK. Server... (0 Replies)
Discussion started by: heman96
0 Replies

5. Homework & Coursework Questions

Need help in allowing symmetric cryptography[2]

The server that provides the time synchronization must be configured to allow its clients to verify its authenticity using symmetric cryptography. 4. Singapore Polytechnic, Dover, Singapore,Mr Kam, and Computer Engineering I don't think there is any coding since it is just configuring... (3 Replies)
Discussion started by: wilsonljx
3 Replies

6. Red Hat

Need help in allowing symmetric cryptography[2]

I have encountered some problems in my school work. Here is the question: The server that provides the time synchronization must be configured to allow its clients to verify its authenticity using symmetric cryptography. Much Appreciated!:) (1 Reply)
Discussion started by: wilsonljx
1 Replies

7. Programming

allowing members of a group to kill a process

I've written a python program where I want to allow members of a specific group the ability to kill it, and I'm not sure how to do it. I've been looking at the setuid() and setgid() and similar functions in the os module, but haven't been able to get them to work. I can't seem to change the uid or... (1 Reply)
Discussion started by: vastcharade
1 Replies

8. Shell Programming and Scripting

Converting tables of row data into columns of tables

I am trying to transpose tables listed in the format into format. Any help would be greatly appreciated. Input: test_data_1 1 2 90% 4 3 91% 5 4 90% 6 5 90% 9 6 90% test_data_2 3 5 92% 5 4 92% 7 3 93% 9 2 92% 1 1 92% ... Output:... (7 Replies)
Discussion started by: justthisguy
7 Replies

9. Cybersecurity

Allowing access to ports < 1024 w/o root

I need to set up an application to run in a script which will be running as a web server but is a database. I need to allow users to use the web server but the app must be run as root in order for the ports to be accessible. This is not a very secure environment would like to know how this could... (2 Replies)
Discussion started by: rpollard
2 Replies
Login or Register to Ask a Question