SlideShare a Scribd company logo
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
IP Tables Basics
June, 2014
By Steve Mushero
Copyright 2015 ChinaNetCloud
ChinaNetCloud
1
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 2
Introduction
● iptables is main server firewall
● Layer 4 – all IP, Port, protocol-based
● Software-based
● Built-into kernel
● Powerful & fast
● But difficult to use
● We have a script :)
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 3
Basic Parts
● Kernel Module - netfilter
● Kernel Module – conntrack
● Creates sysctrl items like conntrack_max
● Tool – iptables command
● Run as root
● Save files – simple save file
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 4
Filtering Basics
● Filter on:
● IP Address – Source or Destination
● Ports – Source or Destination
● Protocol – ICMP, UDP, TCP, etc.
● Status – SYN, Established, Related
● Two main results – Allow or Block (drop)
● Special functions
● Logging
● Statistics
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 5
Tables
● Three Tables are built into kernel
● Filter – Real firewall, always used
● NAT – For NAT by Linux, rarely used
● Mangle – Special use
● Filter is the default table, the one you will use
● It’s the filter iptables shows/changes without -t
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 6
Chains
● Each Table has Chains
● Three built-in Chains in Filter Table
● INPUT – For traffic coming INTO server
● OUTPUT – For traffic LEAVING server
● FORWARD – For routing, rarely used
● You can add more chains for ease of use
● Such as logging, special protocols
● The Chains have the Rules
● You will usually edit these
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 7
Chains
● That Chain can call other Chains
● RedHat always includes a special RH chain
● You can add more chains, such as for logging
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 8
Chains
● Iptables –vnL
Chain INPUT (policy ACCEPT )
Chain OUPPUT (policy ACCEPT)
Chain FORWARD (policy ACCEPT)
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 9
Tables & Chains & Rules
● Filter, NAT, Mangle Tables
● Input and Output Chains in Filter Table
● Rules in Input Chain to protect server
● Firewall is a set of Tables, Chains, and Rules
● Rules are most important
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 10
Basic Packet Flow
● Each input packet hits Filter Table, Input Chain
● Packet is checked rule by rule, from top
● If a rule is true, results happens
● Usually ACCEPT, DROP, or REJECT
● Process ends (except for LOG result)
● Statistic counters tell you which rules are hit/true
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 11
Basic Packet Flow
# Target prot in out source destination
1 ACCEPT all lo lo 0.0.0.0/0 0.0.0.0/0
2 ACCEPT TCP * * 1.2.3.4./32 0.0.0.0/0
3 DROP all * * 0.0.0.0/0 0.0.0.0/0
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 12
Basic Rule Structure
iptables -A INPUT -p tcp –i eth0 –s 0.0.0.0/0 -j ACCEPT
● Basic rule
● Chain - INPUT
● Protocol – TCP, UDP, IDCMP, ALL
● Interface - * or lo or eth0, etc.
● Action – ACCEPT, DROP, or REJECT
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 13
Basic Rule Options
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
● Ports and States
● Destination Port – 22 (ssh)
● Very often used for services
● Module – state (needed for next option)
● Module Option – State NEW
● Always used for normal rules
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 14
Other Common Rule Options
● Logging – like -j LOG --log-prefix 'bad port: ’
● Will log to syslog
● Used to log bad or illegal packets
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 15
Accept Established / Related
iptables -I INPUT -m state --state ESTABLISHED, RELATED -j ACCEPT
● All systems have a rule like this
● To pass ESTAB connections, always save
● Managed by conntrack module
● RELATED is for TCP like FTP or DNS UDP
● For DNS UDP it remembers out / in
● Put this rule first in rule list, for better performance
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 16
Last Rule always Drop
● Always add -j DROP rule at end
● So if we don't allow traffic, it's dropped
● Even if Chain Policy is also DROP
● Best practice is both DROP policy & Drop rule
● This ensures we drop everything we don’t want
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 17
Chain Policy
Chain INPUT (policy ACCEPT 7091K packets, 4852M bytes)
● Each Chain has a default action
● Very important
● Done automatically at end of Chain
● Should be DROP on all major Chains
● Should be ACCEPT for middle partial Chains
● To allow packets to continue to other chains
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 18
Using iptables command
● Can show, add, insert, delete rules
● Easiest to show rules with numbers:
● iptables –vnL –line-numbers [Note L for list]
● Will show current rules with numbers
● Other options to Add, Delete, Insert
● Delete / Insert use line numbers
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 19
Iptables-save / restore
● Dump iptables in memory to file
● Loaded by init when server starts
● Any changes not in file are LOST on reboot !!
● File usually in /etc/sysconfig:
/etc/sysconfig/iptables
● Can be monitored by Zabbix, Nagios, etc.
● Can run manually
● iptables-save > file
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 20
Iptables as a Service
● It's NOT a service, but looks like a service
● Has init script to load save file on boot
● Script just changes options
● Stop – Deletes all rules and allows all traffic
● Start – Load iptables-save file /etc/sysconfig/iptables
● If you 'stop' iptables to test, don't forget to start
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 21
Advanced Use
● NAT
● Used for ssh and Zabbix forwarding
● Used as gateway for private LAN (DB, etc.)
● Port Changes
● Can move port 80 traffic to 8080
● Routing between NIC
● Xen Dom0 Use – Control VMs
● Change packet data
● Quite Rare
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 22
Packet Flow
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 23
Summary
● Iptables very important
● Used on every server
● A bit complicated
● Use a script to manage
● Be careful
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
Copyright 2015 ChinaNetCloud 24
About ChinaNetCloud
ChinaNetCloudRunning All the World's Internet Servers
管理全世界的网络服务器
ChinaNetCloud
Sales@ChinaNetCloud.com
www.ChinaNetCloud.com
Beijing Office:
Lee World Business Building #305
57 Happiness Village Road,
Chaoyang District
Beijing, 100027 China
Silicon Valley Office:
California Avenue
Palo Alto, 94123 USA
Shanghai Headquarters:
X2 Space 1-601, 1238 Xietu Lu
Shanghai, 200032 China
T: +86-21-6422-1946 F: +86-21-
6422-4911

More Related Content

PDF
Connectivity Troubleshooting - pfSense Hangout June 2016
PDF
Linux-wpan: IEEE 802.15.4 and 6LoWPAN in the Linux Kernel - BUD17-120
PDF
pfSense 2.2 Preview - pfSense Hangout November 2014
PDF
Remote Access VPNs Part 2 - pfSense Hangout October 2015
PDF
pfSense 2.4.4 Short Topic Miscellany - pfSense Hangout August 2018
PDF
BKK16-210 Migrating to the new dispatcher
PDF
Intro to Multi-WAN - pfSense Hangout April 2014
PDF
IPv6 Basics - pfSense Hangout July 2015
Connectivity Troubleshooting - pfSense Hangout June 2016
Linux-wpan: IEEE 802.15.4 and 6LoWPAN in the Linux Kernel - BUD17-120
pfSense 2.2 Preview - pfSense Hangout November 2014
Remote Access VPNs Part 2 - pfSense Hangout October 2015
pfSense 2.4.4 Short Topic Miscellany - pfSense Hangout August 2018
BKK16-210 Migrating to the new dispatcher
Intro to Multi-WAN - pfSense Hangout April 2014
IPv6 Basics - pfSense Hangout July 2015

What's hot (20)

PDF
Tomcat next
PDF
Console Menu - pfSense Hangout December 2016
PDF
HTTP/2 and SSL/TLS state of art in ASF servers
PDF
Remote Access VPNs - pfSense Hangout September 2015
PDF
OSMC 2019 | Monitoring your Logs with Fluent by Toshaan Bharvani
PDF
Site-to-Site VPNs - pfSense Hangout November 2015
PDF
BKK16-207 VLANd in LAVA
PDF
Capture gigabytes from FPGA at speed
PDF
Squid, SquidGuard, and Lightsquid on pfSense 2.3 & 2.4 - pfSense Hangout Janu...
PDF
Firewall Best Practices for VoIP on pfSense - pfSense Hangout October 2017
ODP
Moscow erlang users meeetup 2013 01-12 erlrtpproxy
PDF
Achieving the Ultimate Performance with KVM
PPTX
Vigor 3910 docker firmware quick start
PDF
BUD17-310: Introducing LLDB for linux on Arm and AArch64
PPT
Ip6 tables in linux
PDF
Routed IPsec on pfSense 2.4.4 - pfSense Hangout June 2018
PDF
BKK16-111 Tunables: The Future of Platform Optimization Selection in glibc
PPTX
Salt for Network Engineers
ODP
Cassandra drivers
PDF
Tips for Monitoring and Maintaining FME Server
Tomcat next
Console Menu - pfSense Hangout December 2016
HTTP/2 and SSL/TLS state of art in ASF servers
Remote Access VPNs - pfSense Hangout September 2015
OSMC 2019 | Monitoring your Logs with Fluent by Toshaan Bharvani
Site-to-Site VPNs - pfSense Hangout November 2015
BKK16-207 VLANd in LAVA
Capture gigabytes from FPGA at speed
Squid, SquidGuard, and Lightsquid on pfSense 2.3 & 2.4 - pfSense Hangout Janu...
Firewall Best Practices for VoIP on pfSense - pfSense Hangout October 2017
Moscow erlang users meeetup 2013 01-12 erlrtpproxy
Achieving the Ultimate Performance with KVM
Vigor 3910 docker firmware quick start
BUD17-310: Introducing LLDB for linux on Arm and AArch64
Ip6 tables in linux
Routed IPsec on pfSense 2.4.4 - pfSense Hangout June 2018
BKK16-111 Tunables: The Future of Platform Optimization Selection in glibc
Salt for Network Engineers
Cassandra drivers
Tips for Monitoring and Maintaining FME Server
Ad

Viewers also liked (14)

PPTX
Big Data Security (ChinaNetCloud - Guiyang Conference)
PDF
Programa convencion ahpr (final)
PPTX
Imagen Sales Kit 2014 Casiano Communications
PPT
How to find the perfect venue for valentine party
PDF
Wedding suggestions 2014,1
PPTX
ChinaNetCloud - Company & Services Overview
PDF
Running Internet Systems in China - The Details You Need to Succeed in Chines...
PPT
ChinaNetCloud - China Internet Infrastructure Summary
PPS
Aerolineas mundiales power point
PPTX
ChinaNetCloud - The Zabbix Database - Zabbix Conference 2014
PPT
Barras y unidades
PPTX
FARHAM ESTATE HEALTH SPA
PDF
My Resume
Big Data Security (ChinaNetCloud - Guiyang Conference)
Programa convencion ahpr (final)
Imagen Sales Kit 2014 Casiano Communications
How to find the perfect venue for valentine party
Wedding suggestions 2014,1
ChinaNetCloud - Company & Services Overview
Running Internet Systems in China - The Details You Need to Succeed in Chines...
ChinaNetCloud - China Internet Infrastructure Summary
Aerolineas mundiales power point
ChinaNetCloud - The Zabbix Database - Zabbix Conference 2014
Barras y unidades
FARHAM ESTATE HEALTH SPA
My Resume
Ad

Similar to ChinaNetCloud Training - iptables Intro (20)

PDF
Pdf c1t tlawaxb
ODP
Optimizing Linux Servers
ODP
Nagios Conference 2014 - Eric Mislivec - Getting Started With Nagios Core
PDF
Measuring a 25 and 40Gb/s Data Plane
PPTX
Comprehensive lecture on IO Systems version 2.pptx
ODP
ChinaNetCloud Training - HAProxy Intro
PPT
Networking Layer Basics - ChinaNetCloud Training
PDF
Bandwidth Monitoring - pfSense Hangout March 2015
PPT
Linux Memory Basics for SysAdmins - ChinaNetCloud Training
PDF
LCE13: Test and Validation Summit: The future of testing at Linaro
PDF
LCE13: Test and Validation Mini-Summit: Review Current Linaro Engineering Pro...
DOCX
How to Install iptable on Debian 12.docx
PDF
Suricata: A Decade Under the Influence (of packet sniffing)
PDF
Building a Small DC
PDF
Building a Small Datacenter
PDF
HKNOG 6.0 Next Generation Networks - will automation put us out of jobs?
PPTX
P99 Publish Performance in a Multi-Cloud NATS.io System
PDF
Mikrotik Fastpath vs Fasttrack
PDF
Approaching hyperconvergedopenstack
PPTX
Free5 gc installation
Pdf c1t tlawaxb
Optimizing Linux Servers
Nagios Conference 2014 - Eric Mislivec - Getting Started With Nagios Core
Measuring a 25 and 40Gb/s Data Plane
Comprehensive lecture on IO Systems version 2.pptx
ChinaNetCloud Training - HAProxy Intro
Networking Layer Basics - ChinaNetCloud Training
Bandwidth Monitoring - pfSense Hangout March 2015
Linux Memory Basics for SysAdmins - ChinaNetCloud Training
LCE13: Test and Validation Summit: The future of testing at Linaro
LCE13: Test and Validation Mini-Summit: Review Current Linaro Engineering Pro...
How to Install iptable on Debian 12.docx
Suricata: A Decade Under the Influence (of packet sniffing)
Building a Small DC
Building a Small Datacenter
HKNOG 6.0 Next Generation Networks - will automation put us out of jobs?
P99 Publish Performance in a Multi-Cloud NATS.io System
Mikrotik Fastpath vs Fasttrack
Approaching hyperconvergedopenstack
Free5 gc installation

More from ChinaNetCloud (20)

PPTX
AWS ELB Tips & Best Practices
PPTX
OpsStack--Integrated Operation Platform
PPTX
ChinaNetCloud Online Lecture:Something About Tshark
PPTX
ChinaNetCloud Online Lecture: Fight Against External Attacks From Different L...
PPTX
Steve Mushero on Entrepreneurship - 创业 - 崔牛会
PPTX
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
PPTX
云中漫步 颠覆创新_创业邦春季创新峰会主题演讲 Cloud Innovation in China
PDF
运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享
PPTX
AWS Summit OaaS Talk by ChinaNetCloud
PDF
Making Internet Operations Easier
PPTX
Internet Cloud Operations - ChinaNetcloud & AWS Event Beijing
PPTX
Internet System Security Overview
PPTX
Why Work at ChinaNetCloud
PPTX
Cloud Operations Challenges - Talk by ChinaNetCloud at Joint Cisco event
PPTX
Automatically Managing Internet Operations In The Cloud - 云计算平台的自动化运维
PPTX
ChinaNetCloud - Aliyun Joint Event on Cloud Operations
PDF
Clouds in China
PPTX
ChinaNetCloud - Public Clouds in China Overview
PPTX
ChinaNetCloud Chase Future Event - Lessons Learned Running a Chinese Startup
PPTX
ChinaNetCloud Magento Operations - Magentocom Conference - Nov 2014
AWS ELB Tips & Best Practices
OpsStack--Integrated Operation Platform
ChinaNetCloud Online Lecture:Something About Tshark
ChinaNetCloud Online Lecture: Fight Against External Attacks From Different L...
Steve Mushero on Entrepreneurship - 创业 - 崔牛会
Dev-Ops与Docker的最佳实践 QCon2016 北京站演讲
云中漫步 颠覆创新_创业邦春季创新峰会主题演讲 Cloud Innovation in China
运维安全 抵抗黑客攻击_云络安全沙龙4月上海站主题分享
AWS Summit OaaS Talk by ChinaNetCloud
Making Internet Operations Easier
Internet Cloud Operations - ChinaNetcloud & AWS Event Beijing
Internet System Security Overview
Why Work at ChinaNetCloud
Cloud Operations Challenges - Talk by ChinaNetCloud at Joint Cisco event
Automatically Managing Internet Operations In The Cloud - 云计算平台的自动化运维
ChinaNetCloud - Aliyun Joint Event on Cloud Operations
Clouds in China
ChinaNetCloud - Public Clouds in China Overview
ChinaNetCloud Chase Future Event - Lessons Learned Running a Chinese Startup
ChinaNetCloud Magento Operations - Magentocom Conference - Nov 2014

Recently uploaded (20)

PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPTX
Internet___Basics___Styled_ presentation
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PPTX
international classification of diseases ICD-10 review PPT.pptx
PDF
Testing WebRTC applications at scale.pdf
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
DOCX
Unit-3 cyber security network security of internet system
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPTX
artificial intelligence overview of it and more
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
The Internet -By the Numbers, Sri Lanka Edition
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
introduction about ICD -10 & ICD-11 ppt.pptx
Slides PDF The World Game (s) Eco Economic Epochs.pdf
SASE Traffic Flow - ZTNA Connector-1.pdf
Paper PDF World Game (s) Great Redesign.pdf
Internet___Basics___Styled_ presentation
presentation_pfe-universite-molay-seltan.pptx
Design_with_Watersergyerge45hrbgre4top (1).ppt
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
international classification of diseases ICD-10 review PPT.pptx
Testing WebRTC applications at scale.pdf
An introduction to the IFRS (ISSB) Stndards.pdf
Unit-3 cyber security network security of internet system
QR Codes Qr codecodecodecodecocodedecodecode
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
artificial intelligence overview of it and more
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Introuction about ICD -10 and ICD-11 PPT.pptx
Slides PPTX World Game (s) Eco Economic Epochs.pptx
The Internet -By the Numbers, Sri Lanka Edition
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰

ChinaNetCloud Training - iptables Intro

  • 1. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 IP Tables Basics June, 2014 By Steve Mushero Copyright 2015 ChinaNetCloud ChinaNetCloud 1
  • 2. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 2 Introduction ● iptables is main server firewall ● Layer 4 – all IP, Port, protocol-based ● Software-based ● Built-into kernel ● Powerful & fast ● But difficult to use ● We have a script :)
  • 3. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 3 Basic Parts ● Kernel Module - netfilter ● Kernel Module – conntrack ● Creates sysctrl items like conntrack_max ● Tool – iptables command ● Run as root ● Save files – simple save file
  • 4. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 4 Filtering Basics ● Filter on: ● IP Address – Source or Destination ● Ports – Source or Destination ● Protocol – ICMP, UDP, TCP, etc. ● Status – SYN, Established, Related ● Two main results – Allow or Block (drop) ● Special functions ● Logging ● Statistics
  • 5. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 5 Tables ● Three Tables are built into kernel ● Filter – Real firewall, always used ● NAT – For NAT by Linux, rarely used ● Mangle – Special use ● Filter is the default table, the one you will use ● It’s the filter iptables shows/changes without -t
  • 6. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 6 Chains ● Each Table has Chains ● Three built-in Chains in Filter Table ● INPUT – For traffic coming INTO server ● OUTPUT – For traffic LEAVING server ● FORWARD – For routing, rarely used ● You can add more chains for ease of use ● Such as logging, special protocols ● The Chains have the Rules ● You will usually edit these
  • 7. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 7 Chains ● That Chain can call other Chains ● RedHat always includes a special RH chain ● You can add more chains, such as for logging
  • 8. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 8 Chains ● Iptables –vnL Chain INPUT (policy ACCEPT ) Chain OUPPUT (policy ACCEPT) Chain FORWARD (policy ACCEPT)
  • 9. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 9 Tables & Chains & Rules ● Filter, NAT, Mangle Tables ● Input and Output Chains in Filter Table ● Rules in Input Chain to protect server ● Firewall is a set of Tables, Chains, and Rules ● Rules are most important
  • 10. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 10 Basic Packet Flow ● Each input packet hits Filter Table, Input Chain ● Packet is checked rule by rule, from top ● If a rule is true, results happens ● Usually ACCEPT, DROP, or REJECT ● Process ends (except for LOG result) ● Statistic counters tell you which rules are hit/true
  • 11. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 11 Basic Packet Flow # Target prot in out source destination 1 ACCEPT all lo lo 0.0.0.0/0 0.0.0.0/0 2 ACCEPT TCP * * 1.2.3.4./32 0.0.0.0/0 3 DROP all * * 0.0.0.0/0 0.0.0.0/0
  • 12. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 12 Basic Rule Structure iptables -A INPUT -p tcp –i eth0 –s 0.0.0.0/0 -j ACCEPT ● Basic rule ● Chain - INPUT ● Protocol – TCP, UDP, IDCMP, ALL ● Interface - * or lo or eth0, etc. ● Action – ACCEPT, DROP, or REJECT
  • 13. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 13 Basic Rule Options iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT ● Ports and States ● Destination Port – 22 (ssh) ● Very often used for services ● Module – state (needed for next option) ● Module Option – State NEW ● Always used for normal rules
  • 14. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 14 Other Common Rule Options ● Logging – like -j LOG --log-prefix 'bad port: ’ ● Will log to syslog ● Used to log bad or illegal packets
  • 15. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 15 Accept Established / Related iptables -I INPUT -m state --state ESTABLISHED, RELATED -j ACCEPT ● All systems have a rule like this ● To pass ESTAB connections, always save ● Managed by conntrack module ● RELATED is for TCP like FTP or DNS UDP ● For DNS UDP it remembers out / in ● Put this rule first in rule list, for better performance
  • 16. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 16 Last Rule always Drop ● Always add -j DROP rule at end ● So if we don't allow traffic, it's dropped ● Even if Chain Policy is also DROP ● Best practice is both DROP policy & Drop rule ● This ensures we drop everything we don’t want
  • 17. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 17 Chain Policy Chain INPUT (policy ACCEPT 7091K packets, 4852M bytes) ● Each Chain has a default action ● Very important ● Done automatically at end of Chain ● Should be DROP on all major Chains ● Should be ACCEPT for middle partial Chains ● To allow packets to continue to other chains
  • 18. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 18 Using iptables command ● Can show, add, insert, delete rules ● Easiest to show rules with numbers: ● iptables –vnL –line-numbers [Note L for list] ● Will show current rules with numbers ● Other options to Add, Delete, Insert ● Delete / Insert use line numbers
  • 19. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 19 Iptables-save / restore ● Dump iptables in memory to file ● Loaded by init when server starts ● Any changes not in file are LOST on reboot !! ● File usually in /etc/sysconfig: /etc/sysconfig/iptables ● Can be monitored by Zabbix, Nagios, etc. ● Can run manually ● iptables-save > file
  • 20. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 20 Iptables as a Service ● It's NOT a service, but looks like a service ● Has init script to load save file on boot ● Script just changes options ● Stop – Deletes all rules and allows all traffic ● Start – Load iptables-save file /etc/sysconfig/iptables ● If you 'stop' iptables to test, don't forget to start
  • 21. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 21 Advanced Use ● NAT ● Used for ssh and Zabbix forwarding ● Used as gateway for private LAN (DB, etc.) ● Port Changes ● Can move port 80 traffic to 8080 ● Routing between NIC ● Xen Dom0 Use – Control VMs ● Change packet data ● Quite Rare
  • 22. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 22 Packet Flow
  • 23. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 23 Summary ● Iptables very important ● Used on every server ● A bit complicated ● Use a script to manage ● Be careful
  • 24. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 Copyright 2015 ChinaNetCloud 24 About ChinaNetCloud
  • 25. ChinaNetCloudRunning All the World's Internet Servers 管理全世界的网络服务器 ChinaNetCloud Sales@ChinaNetCloud.com www.ChinaNetCloud.com Beijing Office: Lee World Business Building #305 57 Happiness Village Road, Chaoyang District Beijing, 100027 China Silicon Valley Office: California Avenue Palo Alto, 94123 USA Shanghai Headquarters: X2 Space 1-601, 1238 Xietu Lu Shanghai, 200032 China T: +86-21-6422-1946 F: +86-21- 6422-4911