`
Anddy
  • 浏览: 191470 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

iptables 那点小知识

阅读更多

了解服务器防火墙 应该是玩服务器必须上的一课 。

linux的防火墙安全 的好坏 主要是 iptables 配置得如何,我认为。

于是乎,进入主题iptables的配置。(环境是centos 5.5) 

 

iptables 需了解的几个基本概念:  TARGET , CHAINS(user-defined and  built-in chains), TABLE  

 

 

1. TABLE 

描述如下:

Iptables is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel.  Several different tables may be defined.  Each table contains a number of built-in chains and may also contain user-defined chains. 


TABLE 里存放的就是各种内置的或者用户定义的链。 iptables存在着多种不同table ,比如:filter,nat,mangle,raw 。 

 

2. CHAINS 

描述如下:

Each chain is a list of rules which can match a set of packets.  Each rule specifies what to do with a packet that matches.

This  is  called a ‘target’, which may be a jump to a user-defined chain in the same table.

 

chain 中包含各种规则(rule), 规则用来匹配数据包(packet), 同时了规则指定了如果匹配成功则对数据包(packet)做什么处理, 这就是所谓的target 。 也就是如果匹配成功,就交给taget 处理。 

 

3. TARGETS 

描述如下 :

A firewall rule specifies criteria for a packet, and a target.  (可以忽略)

If the packet does not match, the next rule in the chain is the examined; 

if it does match, then the next rule is specified by the value of the target, which can be the name of a user-defined chain  or one of the special values ACCEPT, DROP, QUEUE, or RETURN. (这句话得认真看)

 

如果匹配成功,则数据包交给 由Target所决定的 下一条rule处理。 而Target 指定了什么?  Target 可以是 用户定义的链,也可以是 ACCEPT ,DROP ,QUEUE,RETURN 。 

 

接下来看看 这个四个单词是神马? 

 

ACCEPT  means  to  let the packet through.  

DROP means to drop the packet on the floor.  

QUEUE means to pass the packet to userspace.  (How the packet can be received by a userspace process differs by the particular queue handler.  2.4.x and 2.6.x kernels up to 2.6.13 include the ip_queue queue handler.  Kernels 2.6.14 and later additionally include the nfnetlink_queue queue handler.  Packets with a  target of QUEUE will be sent to queue number ’0’ in this case. Please also see the NFQUEUE target as  described  later  in  this  man page.)  (这个在此忽略!!!)

RETURN means stop traversing this chain and resume at the next rule in the previous (calling) chain.  If the end of a built-in chain is reached or a rule in a built-in chain with target RETURN is matched, the target specified by the chain policy  determines the fate of the packet.

 

其他三个不用解释了。 

 

基本概念清理完毕,开始写脚本 

 

#!/bin/bash
#
# iptables example configuration script
#
#
# If connecting remotely we must first temporarily set the default policy on the INPUT chain to ACCEPT 
# otherwise once we flush the current rules we will be locked out of our server.
#
iptables -P INPUT ACCEPT 
#
# Flush all current rules from iptables
#
iptables -F
#
# Allow SSH connections on tcp port 22
# This is essential when working on remote servers via SSH to prevent locking yourself out of the system
#
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#
# Allow Httpd on tcp port 22 
#
iptables -A INPUT -p tcp --dport 80 -j ACCEPT 
#
# Set default policies for INPUT, FORWARD and OUTPUT chains
#
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
#
# Set access for localhost
#
iptables -A INPUT -i lo -j ACCEPT
#
# Accept packets belonging to established and related connections
#
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#
# Create a new CHAIN called LOGNDROP
#
iptables -N LOGNDROP
#
# the standard DROP at the bottom of the INPUT chain is replaceed with LOGNDROP
# 
iptables -A INPUT -j LOGNDROP 
#
#
# add protocol descriptions so it makes sense looking at the log 
#
iptables -A LOGNDROP -p tcp -m limit --limit 5/min -j LOG --log-prefix "Denied TCP: " --log-level 4
iptables -A LOGNDROP -p udp -m limit --limit 5/min -j LOG --log-prefix "Denied UDP: " --log-level 4
iptables -A LOGNDROP -p icmp -m limit --limit 5/min -j LOG --log-prefix "Denied ICMP: " --log-level 4


#
# drop the traffic at the end of the LOGNDROP chain. 
#
iptables -A LOGNDROP -j DROP

# Save settings
#
/sbin/service iptables save
#
# List rules
#
iptables -L -v
 

 

注释应该很详细了,提醒一点,上面定义了一个LOGNDROP 链, 并为该链添加了3条rules 。 

 

附加修改syslog.conf的配置文件

 

#
#config syslog 
#

iptableslog=`cat /etc/syslog.conf | awk '{print $2}' | grep iptables.log`

if [ "$iptableslog"="/var/log/iptables.log" ]; then 
	echo "setup for iptables log Have already exists" 
else 
	echo "# Save Denied messages  to  iptalbes.log" > /etc/syslog.conf
	echo "kern.warning                                            /var/log/iptables.log" > /etc/syslog.conf
fi
 

然后tail -f /var/log/iptables.log 查看被Denied的Packet, 可以看到来源的IP 。 

 

这些都是iptables鸡毛蒜皮的小事情, iptables还大有文章,继续学习。 

 

 

参考: 

http://wiki.ubuntu.org.cn/IptablesHowTo#More_detailed_Logging_.E5.85.B3.E4.BA.8E.E6.97.A5.E5.BF.97.E8.AE.B0.E5.BD.95.E7.9A.84.E6.9B.B4.E5.A4.9A.E7.BB.86.E8.8A.82

Basic Iptables How to for Ubuntu Server Edition Ubuntu 服务器版 Iptables 基本设置指南


http://wiki.centos.org/HowTos/Network/IPTables  

  • HowTos>
  • Network>
  • IPTables (centos)

  • http://hi.baidu.com/duxf/blog/item/1ec6b00e14ce3dcd7acbe14e.html Iptables配置+访问日志记录


  • 1
    1
    分享到:
    评论

    相关推荐

      iptables指南1.1.19电子书

      必备知识 本文约定 1. 序言 1.1. 为什么要写这个指南 1.2. 指南是如何写的 1.3. 文中出现的术语 2. 准备阶段 2.1. 哪里能取得iptables 2.2. 内核配置 2.3. 编译与安装 2.3.1. 编译 2.3.2. 在Red Hat 7.1上...

      LINUX IPTABLES防火墙的基本知识和应用

      一份说明IPTABLES基础知识的文档,含有详细使用说明和示例,内容较全面丰富,是一个学习LINUX 防火墙应用知识的材料。

      iptables详解

      安全体系概览,预备知识:OSI七层模型,Firewalls: netfilter/iptables,数据包的流向图(iptables语法,标准流程)

      Iptables详细介绍

      Iptables详细介绍秒速了Iptables的基本概念知识,运行环境,运行原理等内容。对Iptables的结构构成和各部分协同工作做了介艄。并对常用Iptables的操作代码做了解释并举例。看了此文档后,读者即可以学会对iptables的...

      【小知识】20分钟掌握iptables指令

      【小知识】20分钟掌握iptables指令

      iptables命令实例

      在介绍iptables之前,我们先来了解一下防火墙的相关知识: 防火墙存在于网络边界,通过预设规则对进出口路由进行检查的处理组件;防火墙分为包过滤防火墙和procy防火墙,(第二种称谓可能不太合适,欢迎大家来指点哦...

      iptables手册详解

      iptables手册详解,静态网页版,对netfile网络基础知识做全面介绍,详细讲解iptables命令以及应用场景。

      iptables.ppt

      iptables 马哥ppt 关于防火墙安全的知识

      iptables 入门篇

      本篇文章整理了 iptables 的基本概念及入门知识,旨在帮助大家快速了解和使用 iptables。 关于更多 iptables 的扩展和实践部分将在后续篇章推出,敬请期待。

      Iptables Tutorial 1.2.2 英文原版带书签

      Iptables Tutorial 1.2.2 PDF,英文原版带书签,详细的讲述了iptable的原理、使用技巧,还补充了必要的tip/ip协议知识以便于理解。

      linux下防火墙iptables

      linux下防火墙iptables 一、基本知识 二、iptable的安装与配置 禁止端口的实例 强制访问指定的站点 发布内部网络服务器 通过NAT上网 iptables实例

      Linux netfilter/iptables知识点详解

      在本篇文章里小编给大家整理的是关于Linux netfilter/iptables知识点详解,有兴趣的朋友们可以参考下。

      ebtables/iptables interaction on a Linux−based bridge

      ebtables/iptables interaction on a Linux−based bridge

      iptables指南

      iptables指南,iptables命令基础大全。是网络学习者必备知识。

      iptables实例

      iptables的一些配置实例,基础知识!!

      操作系统安全:iptables工具.pptx

      iptables;iptables工具;iptables工具;iptables工具;iptables工具;FilterTable的操作;FilterTable的操作;FilterTable的操作;FilterTable的操作;FilterTable的操作;FilterTable的操作;FilterTable的操作;NATTable的...

      iptables:尝试对iptables进行理智的介绍

      在知识共享( )下发布。 旨在为从未接触过iptables的中高级用户(或更高级别的用户)提供iptables简介。 这是iptables的粗略概述,并提供了一些基本示例。 传奇: 常用或提及不常用或此处未记录 iptables (和ip6...

      Linux使用iptables限制多个IP访问你的服务器

      iptables是一个管理netfilter的工具。这篇文章主要介绍了Linux使用iptables限制多个IP访问你的服务器的相关知识,需要的朋友可以参考下

      Linux下IPTABLES配置详解

      如果你的IPTABLES基础知识还不了解,建议先去看看.  开始配置  我们来配置一个filter表的防火墙.  (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L -n Chain INPUT (policy ACCEPT) target ...

    Global site tag (gtag.js) - Google Analytics