Linux Firewall Wizardry. By Nemus



Similar documents
Firewalls. Chien-Chung Shen

Intro to Linux Kernel Firewall

+ iptables. packet filtering && firewall

Track 2 Workshop PacNOG 7 American Samoa. Firewalling and NAT

Linux: 20 Iptables Examples For New SysAdmins

Firewall. IPTables and its use in a realistic scenario. José Bateira ei10133 Pedro Cunha ei05064 Pedro Grilo ei09137 FEUP MIEIC SSIN

Linux firewall. Need of firewall Single connection between network Allows restricted traffic between networks Denies un authorized users

Linux Networking: IP Packet Filter Firewalling

1:1 NAT in ZeroShell. Requirements. Overview. Network Setup

How To Set Up An Ip Firewall On Linux With Iptables (For Ubuntu) And Iptable (For Windows)

Main functions of Linux Netfilter

Linux Routers and Community Networks

Chapter 7. Firewalls

ipchains and iptables for Firewalling and Routing

Optimisacion del ancho de banda (Introduccion al Firewall de Linux)

Linux Firewall. Linux workshop #2.

TECHNICAL NOTES. Security Firewall IP Tables

Network security Exercise 9 How to build a wall of fire Linux Netfilter

Network Security Exercise 10 How to build a wall of fire

Netfilter / IPtables

CSC574 - Computer and Network Security Module: Firewalls

Matthew Rossmiller 11/25/03

Linux Firewalls (Ubuntu IPTables) II

Building a Home Gateway/Firewall with Linux (aka Firewalling and NAT with iptables )

How to Turn a Unix Computer into a Router and Firewall Using IPTables

Worksheet 9. Linux as a router, packet filtering, traffic shaping

CS Computer and Network Security: Firewalls

Protecting and controlling Virtual LANs by Linux router-firewall

Firewalls. Firewall types. Packet filter. Proxy server. linux, iptables-based Windows XP s built-in router device built-ins single TCP conversation

Packet Filtering Firewall

Computer Firewalls. The term firewall was originally used with forest fires, as a means to describe the

Packet filtering with Linux

CS Computer and Network Security: Firewalls

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering

How To Understand A Firewall

Focus on Security. Keeping the bad guys out

iptables: The Linux Firewall Administration Program

Linux Administrator (Advance)

Firewalls. Pehr Söderman KTH-CSC

Network Security. Routing and Firewalls. Radboud University Nijmegen, The Netherlands. Autumn 2014

Assignment 3 Firewalls

Netfilter. GNU/Linux Kernel version 2.4+ Setting up firewall to allow NIS and NFS traffic. January 2008

Firewall implementation and testing

How To Set Up A Network Map In Linux On A Ubuntu 2.5 (Amd64) On A Raspberry Mobi) On An Ubuntu (Amd66) On Ubuntu 4.5 On A Windows Box

Lecture 18: Packet Filtering Firewalls (Linux) Lecture Notes on Computer and Network Security. by Avi Kak

CSE543 - Computer and Network Security Module: Firewalls

Linux Cluster Security Neil Gorsuch NCSA, University of Illinois, Urbana, Illinois.

Lab Objectives & Turn In

Firewalls, NAT and Intrusion Detection and Prevention Systems (IDS)

Module: Firewalls. Professor Patrick McDaniel Spring CMPSC443 - Introduction to Computer and Network Security

Firewalls. October 23, 2015

How to Create, Setup, and Configure an Ubuntu Router with a Transparent Proxy.

Definition of firewall

CIT 480: Securing Computer Systems. Firewalls

CIS 433/533 - Computer and Network Security Firewalls

Firewall Configuration and Assessment

Linux Home Networking II Websites At Home

Linux 2.4 stateful firewall design

Lecture 18: Packet Filtering Firewalls (Linux) Lecture Notes on Computer and Network Security. by Avi Kak

IP Address: the per-network unique identifier used to find you on a network

netkit lab load balancer web switch 1.1 Giuseppe Di Battista, Massimo Rimondini Version Author(s)

VENKATAMOHAN, BALAJI. Automated Implementation of Stateful Firewalls in Linux. (Under the direction of Ting Yu.)

How to Secure RHEL 6.2 Part 2

Secure use of iptables and connection tracking helpers

Home Networking In Linux

CIT 480: Securing Computer Systems. Firewalls

IP Firewalls. an overview of the principles

Guardian Digital WebTool Firewall HOWTO. by Pete O Hara

1. Firewall Configuration

Load Balancing Trend Micro InterScan Web Gateway

How to protect your home/office network?

Linux as an IPv6 dual stack Firewall

10.4. Multiple Connections to the Internet

Vuurmuur - iptables manager

Load Balancing McAfee Web Gateway. Deployment Guide

Firewall Firewall August, 2003

Load Balancing Sophos Web Gateway. Deployment Guide

Load Balancing Bloxx Web Filter. Deployment Guide

Firewalld, netfilter and nftables

Linux MDS Firewall Supplement

ClusterLoad ESX Virtual Appliance quick start guide v6.3

Linux Networking Basics

Manuale Turtle Firewall

ΕΠΛ 674: Εργαστήριο 5 Firewalls

Load Balancing Web Proxies Load Balancing Web Filters Load Balancing Web Gateways. Deployment Guide

Fault tolerant stateful firewalling with GNU/Linux. Pablo Neira Ayuso Proyecto Netfilter University of Sevilla

Linux Network Security

Install and configure a Debian based UniFi controller

Network Security Management

Load Balancing Clearswift Secure Web Gateway

Firewalls with IPTables. Jason Healy, Director of Networks and Systems

Netfilter s connection tracking system

IPv6 Network Security.

The SpeedTouch and Firewalling

Stateful Firewalls. Hank and Foo

Firewalls N E T W O R K ( A N D D ATA ) S E C U R I T Y / P E D R O B R A N D Ã O M A N U E L E D U A R D O C O R R E I A

Transcription:

Linux Firewall Wizardry By Nemus

The internet and your server So then what do you protect your server with if you don't have a firewall in place?

NetFilter / Iptables http://www.netfilter.org Iptables define by netfiler Netfilter is a set of hooks inside the Linux kernel that allows kernel modules to register callback functions with the network stack. A registered callback function is then called back for every packet that traverses the respective hook within the network stack. Iptables is a generic table structure for the definition of rulesets. Each rule within an IP table consists of a number of classifiers.

Tables and Chains iptables contains multiple tables (which are netfilter modules). Think of tables as a place to put chains. Tables contain multiple chains. Chains can be built-in or user-defined. Chains contain rules. Chains contain rules. Rules are defined for packets. iptables -> Tables -> Chains -> Rules. iptables has the following 4 built-in tables.

Filter Table Filter Table (DEFAULT TABLE!) Filter is default table for iptables and the most commonly used table. (iptables -t filer -list or just iptables -L) The filter table has the following chains INPUT chain Incoming to firewall. For packets who's destination is the server. OUTPUT chain Outgoing from firewall. For packets generated locally and going out of the local server. FORWARD chain Packet's that pass through the server. For packets routed through the local server.

Filter Table Example iptables -L -n -x -v Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 4714 1147392 ACCEPT all -- eth0 * 0.0.0.0/0 0.0.0.0/0 9998 3016856 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 12060 packets, 3876912 bytes) pkts bytes target prot opt in out source destination

NAT Table Iptable s NAT table has the following built-in chains. PREROUTING chain Alters packets before routing. Packet translation happens immediately after the packet enter the system and is done before being processed by the routing table. Helps to translate the destination ip address of the packets to something that matches the routing on the local server or subnet. used for DNAT (destination NAT). POSTROUTING chain Alters packets after routing. Packet translation happens when the packets are leaving the system Used to translate the source ip address of the packets to something that matches the routing on the destination server. used by SNAT (source NAT). OUTPUT chain NAT for locally generated packets on the firewall.

Nat Table Example iptables -t nat -L -n -x -v Chain PREROUTING (policy ACCEPT 678523 packets, 74334701 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 43265 packets, 4868594 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 1185036 packets, 139614154 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 1015193 packets, 86055073 bytes) pkts bytes target prot opt in out source destination 62114 4441616 SNAT all -- * * 172.x.x.0/24 0.0.0.0/0 to:x.x.x.135

Mangle Table Iptables s Mangle table is for specialized packet alteration. Mainly used to set QOS bits in the TCP header. Can be used to set pick me first in packet header. Mangle table has the following built-in chains. PREROUTING chain OUTPUT chain FORWARD chain INPUT chain POSTROUTING chain

Mangle Table Example iptables -t mangle -L -n -x -v Chain PREROUTING (policy ACCEPT 131 packets, 14768 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 131 packets, 14768 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 92 packets, 18196 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 91 packets, 17868 bytes) pkts bytes target prot opt in out source destination

Raw table Iptable s Raw table is for configuration exceptions Raw table has the following built-in chains. PREROUTING chain OUTPUT chain

Raw Table Example iptables -t raw -L -n -x -v Chain PREROUTING (policy ACCEPT 108 packets, 11590 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 72 packets, 11284 bytes) pkts bytes target prot opt in out source destination

http://www.linuxhomenetworking.com/wiki/index.php/ Quick_HOWTO_:_Ch14_:_Linux_Firewalls_Using_iptables#.UOZp9nd1imk

Target Values Following are the possible target values ACCEPT DROP Firewall will accept the packet. Log Firewall will drop the packet. REJECT RETURN Logs packet to syslog Drops packet and sends response back to sender Firewall will stop executing the next set of rules in the current chain for this packet. The control will be returned to the calling chain.

Matches Every rule in iptables has a criteria and a target you can match packet data using the following matches --source (-s) Match on a source IP address or network --destination (-d) Match on a destination IP address or network --protocol (-p) Match on an IP value --in-interface (-i) Input interface (eth0,eth1,etc..) --out-interface (-o) Output interface (eth0,eth1,etc..) --state Match on a set of ip packect connection states --string Match on a sequence of application layer data bytes --comment Associate up to 256 bytes of comment data

Key Points Rules contain a criteria and a target. If the criteria is matched, it goes to the rules specified in the target (or) executes the special values mentioned in the target. If the criteria is not matached, it moves on to the next rule.

THOU SHALL NOT PASS Now an example of iptables wizardry!

Example http://www.cipherdyne.org/linuxfirewalls/ch01/ #!/bin/sh IPTABLES=/sbin/iptables (path to iptables) IP6TABLES=/sbin/ip6tables (path to ipables6) MODPROBE=/sbin/modprobe (path to modprobe) INT_NET=192.168.10.0/24 (nated internal network) INT_INTF=eth1 (input interface bash variable lan) EXT_INTF=eth0 (exit interface bash variable wan)

Example ### flush existing rules and set chain policy setting to DROP echo "[+] Flushing existing iptables rules..." $IPTABLES -F (remove rules form chains in filter table) $IPTABLES -F -t nat #(remove rules from chains in nat table) $IPTABLES -X #(delete user define chains ) $IPTABLES -P INPUT DROP #(set default policy to drop input) $IPTABLES -P OUTPUT DROP #(set default policy to drop ouput) $IPTABLES -P FORWARD DROP #(set default policy to drop all packets passing through firewall)

Drop IPV6 Packets echo "[+] Disabling IPv6 traffic..." $IP6TABLES -P INPUT DROP $IP6TABLES -P OUTPUT DROP $IP6TABLES -P FORWARD DROP

Example rules 2 Input Chain ### state tracking rules $IPTABLES -A INPUT -m conntrack --ctstate INVALID -j LOG --log-prefix "DROP INVALID " --log-ip-options log-tcp-options #(log invalid state packets) $IPTABLES -A INPUT -m conntrack --ctstate INVALID -j DROP #(drop invalid packets) $IPTABLES -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ### anti-spoofing rules (remember constant from above) $IPTABLES -A INPUT -i $INT_INTF! -s $INT_NET -j LOG --log-prefix "SPOOFED PKT " $IPTABLES -A INPUT -i $INT_INTF! -s $INT_NET -j DROP ### ACCEPT rules $IPTABLES -A INPUT -i $INT_INTF -p tcp -s $INT_NET --dport 22 -m conntrack --ctstate NEW -j ACCEPT $IPTABLES -A INPUT -p icmp --icmp-type echo-request -j ACCEPT ### default INPUT LOG rule $IPTABLES -A INPUT! -i lo -j LOG --log-prefix "DROP " --log-ip-options --log-tcp-options ### make sure that loopback traffic is accepted $IPTABLES -A INPUT -i lo -j ACCEPT

Example rules 3 Output Chain ###### OUTPUT chain ###### ### state tracking rules $IPTABLES -A OUTPUT -m conntrack --ctstate INVALID -j LOG --log-prefix "DROP INVALID " --log-ip-options --log-tcp-options $IPTABLES -A OUTPUT -m conntrack --ctstate INVALID -j DROP $IPTABLES -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ### ACCEPT rules for allowing connections out $IPTABLES -A OUTPUT -p tcp --dport 21 -m conntrack --ctstate NEW -j ACCEPT # allow ftp out $IPTABLES -A OUTPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT # allow ssh out bound $IPTABLES -A OUTPUT -p tcp --dport 25 -m conntrack --ctstate NEW -j ACCEPT # allow telnet out $IPTABLES -A OUTPUT -p tcp --dport 43 -m conntrack --ctstate NEW -j ACCEPT # allow whois out $IPTABLES -A OUTPUT -p tcp --dport 80 -m conntrack --ctstate NEW -j ACCEPT # allow http out $IPTABLES -A OUTPUT -p tcp --dport 443 -m conntrack --ctstate NEW -j ACCEPT # allow https out $IPTABLES -A OUTPUT -p tcp --dport 53 -m conntrack --ctstate NEW -j ACCEPT #allow dns out for tcp $IPTABLES -A OUTPUT -p udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT #allow dns out for tcp $IPTABLES -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT

Example rules 4 Forward Chain ### state tracking rules $IPTABLES -A FORWARD -m conntrack --ctstate INVALID -j LOG --log-prefix "DROP INVALID " --log-ip-options --log-tcp-options $IPTABLES -A FORWARD -m conntrack --ctstate INVALID -j DROP $IPTABLES -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ### anti-spoofing rules $IPTABLES -A FORWARD -i $INT_INTF! -s $INT_NET -j LOG --log-prefix "SPOOFED PKT " $IPTABLES -A FORWARD -i $INT_INTF! -s $INT_NET -j DROP

Forward Chain Continued ### ACCEPT rules $IPTABLES -A FORWARD -p tcp -i $INT_INTF -s $INT_NET --dport 21 -m conntrack --ctstate NEW -j ACCEPT $IPTABLES -A FORWARD -p tcp -i $INT_INTF -s $INT_NET --dport 22 -m conntrack --ctstate NEW -j ACCEPT $IPTABLES -A FORWARD -p tcp -i $INT_INTF -s $INT_NET --dport 25 -m conntrack --ctstate NEW -j ACCEPT $IPTABLES -A FORWARD -p tcp -i $INT_INTF -s $INT_NET --dport 43 -m conntrack --ctstate NEW -j ACCEPT $IPTABLES -A FORWARD -p tcp --dport 80 -m conntrack --ctstate NEW -j ACCEPT $IPTABLES -A FORWARD -p tcp --dport 443 -m conntrack --ctstate NEW -j ACCEPT $IPTABLES -A FORWARD -p tcp --dport 53 -m conntrack --ctstate NEW -j ACCEPT $IPTABLES -A FORWARD -p udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT $IPTABLES -A FORWARD -p icmp --icmp-type echo-request -j ACCEPT ### default LOG rule $IPTABLES -A FORWARD! -i lo -j LOG --log-prefix "DROP " --log-ip-options --log-tcp-options

Example Rules 5 Nat Table ###### NAT rules ###### echo "[+] Setting up NAT rules..." $IPTABLES -t nat -A PREROUTING -p tcp --dport 80 -i $EXT_INTF -j DNAT --to 192.168.10.3:80 $IPTABLES -t nat -A PREROUTING -p tcp --dport 443 -i $EXT_INTF -j DNAT --to 192.168.10.3:443 $IPTABLES -t nat -A PREROUTING -p udp --dport 53 -i $EXT_INTF -j DNAT --to 192.168.10.4:53 $IPTABLES -t nat -A POSTROUTING -s $INT_NET -o $EXT_INTF -j MASQUERADE

Enabling Fowarding echo "[+] Enabling IP forwarding..." echo 1 > /proc/sys/net/ipv4/ip_forward

Command Over View iptables-save > /etc/sysconfig/iptables iptables -D INPUT 10 Save iptables setup iptables-restore -c < /etc/sysconfig/iptables Restores iptables Iptables -L -n -v -x List filter table with out doing dns lookup (quick view) V for verbose () X show packet count Should be your default iptables command iptables -L -n line-number Shows Line Numbers Delete rule with line number 10 in input chain iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT Append to input chain Adds to the bottom iptables -I INPUT 1 -i lo -j ACCEPT Inserts above chain rule set (opposite of append) Add to the top of the rule chain iptables -P INPUT DROP Sets default policy for chain Iptables -F Flushes filter table

Operating System Defense # Disable routing triangulation. Respond to queries out # the same interface, not another. Helps to maintain state # Also protects against IP spoofing net/ipv4/conf/all/rp_filter = 1 # Enable logging of packets with malformed IP addresses net/ipv4/conf/all/log_martians = 1 # Disable redirects # Disable acceptance of ICMP redirects net/ipv4/conf/all/accept_redirects = 0 # Turn on protection from Denial of Service (DOS) attacks net/ipv4/tcp_syncookies = 1 # Disable responding to ping broadcasts net/ipv4/icmp_echo_ignore_broadcasts = 1 net/ipv4/conf/all/send_redirects = 0 # Disable source routed packets net/ipv4/conf/all/accept_source_route = 0 http://www.linuxhomenetworking.com/wiki/index.php/ Quick_HOWTO_:_Ch14_:_Linux_Firewalls_Using_iptables#.UOZ6lHd1imk

Great Resources http://www.cyberciti.biz/tips/linux-iptables-examples.html http://www.linuxhomenetworking.com/wiki/index.php/ Quick_HOWTO_:_Ch14_:_Linux_Firewalls_Using_iptables#.UOZ6lHd1imk http://www.cipherdyne.org/linuxfirewalls/ch01/ http://www.thegeekstuff.com/2011/06/iptables-rules-examples/ Linux Firewalls: Attack Detection and Response with iptables, psad, and fwsnort http://www.cipherdyne.org/linuxfirewalls/

Windows People Iptables on Windows? netsh firewall (deprecated after XP and 2003) netsh advfirewall (Vista, 7, and 2008) http://serverfault.com/questions/207620/windows-equivalent-of-iptables http://www.activexperts.com/network-monitor/windowsmanagement/ scripts/networking/windowsfirewall/ http://technet.microsoft.com/en-us/library/cc771920%28ws.10%29.aspx http://support.microsoft.com/kb/947709 http://itblog.gr/213/configuring-windows-firewall-from-the-command-line/

Windows Block Subnet netsh advfirewall firewall add rule name="http" protocol=tcp localport=80 action=block dir=in remoteip=x.x.x.x/24 netsh advfirewall firewall add rule name="http" protocol=tcp localport=80 action=allow dir=in remoteip=x.x.x.x netsh advfirewall set domainprofile state on Windows 7 netsh advfirewall set domainprofile state off Windows 7

Level Up