Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow ssh incoming/outgoing and blocking all outgoing besides specific ports

I am trying to create iptable rules that will allow incoming and outgoing ssh connections, and then allow outbound connections to specific ports, then finally drop anything that doesnt match.

These are the rules I have come up with, the SSH rules work, but when I tunnel into the box I cant seem to access http (port 80) even though i've allowed it. Can anyone spot the mistake?

#!/bin/bash
#clear iptables
iptables -F
iptables -X

#set default policy to drop
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

#accept everything no matter port on localhost
iptables -A INPUT -i lo -j ACCEPT

#allow established connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#allow input on port 22, (established connections auto accepted)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

#allow traffic going to specific outbound ports
iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 6667 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 6697 -j ACCEPT
#...

#drop anything that doesnt match the rules above
iptables -A INPUT -j DROP
iptables -A OUTPUT -j DROP

Thanks for your time.

like image 318
randy newfield Avatar asked Nov 04 '13 20:11

randy newfield


People also ask

How do I block outgoing SSH?

@BitsOfNix "outgoing SSH connection from server" means block the whole connection! To block specific users. You can edit ssh_d config and add any user name you want to block under DenyUsers. Just do sudo vi /etc/ssh/sshd_config , then under DenyUsers add the user name you want to block.

How do I specify a port using SSH?

Specifying SSH port number on the command lineThe -p <port> option can be used to specify the port number to connect to when using the ssh command on Linux. The -P <port> (note: capital P) option can be used with SFTP and scp .

Should you block port 22?

Aspera recommends disabling TCP/22 to prevent security breaches of your SSH server. Once your client users have been notified of the port change (from TCP/22 to TCP/33001), you can disable Port 22 in your sshd_config file. To disable TCP/22 and use only TCP/33001, comment-out Port 22 in your sshd_config file.


1 Answers

You might want to add the DNS ports, otherwise you may not be able to resolve any hostnames.

Allowing OUTPUT for TCP and UDP Port 53 should help.

like image 135
MeyerRJ Avatar answered Sep 30 '22 04:09

MeyerRJ