iptables默认安全规则脚本

什么是iptables

iptables或netfilter(网络过滤器) 是一个用户态的防火墙应用软件,允许系统管理员可以调整设定X表(Xtables)提供相关的系统表格(目前主要是在 iptables/netfilter 底下)以及相关的“链”与“规则”来管理网络封包的流动与转送的动作。因为相关动作上的需要,所以 iptables 的操作需要用到系统管理员的权限,不然就无法运作下去。在大部份的 Linux 系统上面, iptables 是使用/usr/sbin/iptables 来操作,文件则放置在手册页(Man page)(参考: [1])底下,也可以透过 man iptables 的方式来取得这份文件。通常 iptables 都需要核心层级(kernel)的模组来配合运作,Xtables 是主要在核心层级里面 iptables API 运作功能的模组。

目前 iptables 系在 2.4 及 2.6 的核心底下运作,旧版的 Linux 核心 (2.2) 使用 ipchains 及 ipwadm (linux 2.0) 来达成类似的功能。[来自:维基百科]

保存在这的目的

上次把iptables弄坏了,然后就内网都无法连接了.把这个脚本保存在这里,备用.

14
image-1888

具体操作[转载自网络]

下面的命令以root用户在终端执行:

1
vim default_firewall.sh

下面是脚本内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
#########################################################################
#
# File:         default_firewall.sh
# Description:
# Language:     GNU Bourne-Again SHell
# Version: 1.0
# Date: 2010-6-23
# Corp.: c1gstudio.com
# Author: c1g
# WWW: http://blog.c1gstudio.com
### END INIT INFO
###############################################################################
 
IPTABLES=/sbin/iptables
 
# start by flushing the rules
$IPTABLES -P INPUT DROP
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
$IPTABLES -t nat -P OUTPUT ACCEPT
$IPTABLES -t mangle -P PREROUTING ACCEPT
$IPTABLES -t mangle -P OUTPUT ACCEPT
 
$IPTABLES -F
$IPTABLES -X
$IPTABLES -Z
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
$IPTABLES -t nat -X
$IPTABLES -t mangle -X
$IPTABLES -t nat -Z
 
## allow packets coming from the machine
$IPTABLES -A INPUT -i lo -j ACCEPT
$IPTABLES -A OUTPUT -o lo -j ACCEPT
 
# allow outgoing traffic
$IPTABLES -A OUTPUT -o eth0 -j ACCEPT
 
# block spoofing
$IPTABLES -A INPUT -s 127.0.0.0/8 -i ! lo -j DROP
 
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPTABLES -A INPUT -p icmp -j ACCEPT
 
 
# stop bad packets
$IPTABLES -A INPUT -m state --state INVALID -j DROP
 
# NMAP FIN/URG/PSH
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
# stop Xmas Tree type scanning
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags ALL ALL -j DROP
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
# stop null scanning
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags ALL NONE -j DROP
# SYN/RST
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
# SYN/FIN
$IPTABLES -A INPUT -i eth0 -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
# stop sync flood
$IPTABLES -N SYNFLOOD
$IPTABLES -A SYNFLOOD -p tcp --syn -m limit --limit 1/s -j RETURN
$IPTABLES -A SYNFLOOD -p tcp -j REJECT --reject-with tcp-reset
$IPTABLES -A INPUT -p tcp -m state --state NEW -j SYNFLOOD
# stop ping flood attack
$IPTABLES -N PING
$IPTABLES -A PING -p icmp --icmp-type echo-request -m limit --limit 1/second -j RETURN
$IPTABLES -A PING -p icmp -j REJECT
$IPTABLES -I INPUT -p icmp --icmp-type echo-request -m state --state NEW -j PING
 
 
#################################
## What we allow
#################################
 
# tcp ports
 
# smtp
$IPTABLES -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
# http
$IPTABLES -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
# pop3
$IPTABLES -A INPUT -p tcp -m tcp --dport 110 -j ACCEPT
# imap
$IPTABLES -A INPUT -p tcp -m tcp --dport 143 -j ACCEPT
# ldap
$IPTABLES -A INPUT -p tcp -m tcp --dport 389 -j ACCEPT
# https
$IPTABLES -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
# smtp over SSL
$IPTABLES -A INPUT -p tcp -m tcp --dport 465 -j ACCEPT
# line printer spooler
$IPTABLES -A INPUT -p tcp -m tcp --dport 515 -j ACCEPT
# cups
$IPTABLES -A INPUT -p tcp -m tcp --dport 631 -j ACCEPT
# mysql
$IPTABLES -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
# tomcat
$IPTABLES -A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
# squid
$IPTABLES -A INPUT -p tcp -m tcp --dport 81 -j ACCEPT
# nrpe
$IPTABLES -A INPUT -p tcp -m tcp --dport 15666 -j ACCEPT
 
## restrict some tcp things ##
 
# ssh
$IPTABLES -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
$IPTABLES -A INPUT -p tcp -m tcp --dport 6022 -j ACCEPT
# samba (netbios)
$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 --dport 137:139 -j ACCEPT
# ntop
$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 --dport 3000  -j ACCEPT
# Hylafax
$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 --dport 4558:4559 -j ACCEPT
# webmin
$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 --dport 10000  -j ACCEPT
 
# udp ports
# DNS
$IPTABLES -A INPUT -p udp -m udp --dport 53 -j ACCEPT
# DHCP
$IPTABLES -A INPUT -p udp -m udp --dport 67:68 -j ACCEPT
# NTP
$IPTABLES -A INPUT -p udp -m udp --dport 123 -j ACCEPT
# SNMP
$IPTABLES -A INPUT -p udp -m udp --dport 161:162 -j ACCEPT
 
## restrict some udp things ##
 
# Samba (Netbios)

$IPTABLES -A INPUT -p udp -m udp -s 192.168.0.0/16 --dport 137:139  -j ACCEPT
$IPTABLES -A INPUT -p udp -m udp --sport 137:138 -j ACCEPT
 
# finally - drop the rest
 
$IPTABLES -A INPUT -p tcp --syn -j DROP

//-end
//:wq! 保存退出

下面在Linux终端[使用root用户]进行操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//设置权限
chmod u+x ./default_firewall.sh

//运行脚本
./default_firewall.sh

//查看iptables
/sbin/iptables -nL

//保存iptables
 /sbin/iptables-save > /etc/sysconfig/iptables

//重启iptables
/etc/init.d/iptables restart

Fedora18安装tomcat

tomcat
image-1704

最近在Fedora18上配置了tomcat+Eclipse
大致配置过程如下:

安装jdk

1
2
//卸载系统自带的jdk
rpm -e --nodeps jdk* //强制卸载jdk,包括其依赖的软件.

其他安装步骤,请参考:http://www.cnblogs.com/beceo/archive/2012/08/19/2646706.html

安装Tomcat

  1. 下载apache-tomcat-7.0.35.tar.gz
  2. [以root用户登录]将上述文件移动到/usr/local/
  3. 使用:tar   -zxvf    apache-tomcat-7.0.35.tar.gz
  4. 重命名(可以忽略这一步):mv      apache-tomcat-7.0.35     tomcat-7.0.35
  5. 增加环境变量:vi  /etc/profile
  6. CATALINA_HOME=/usr/local/tomcat-7.0.35
    export CATALINA_HOME
  7. 用:source /etc/profile 使其立即生效.
  8. 打印一下:echo $CATALINA_HOME
  9. 启动tomcat:/usr/local/tomcat-7.0.35/bin/startup.sh
  10. 打开浏览器输入:http://localhost:8080,如果看到tomcat的界面则说明安装成功
  11. 停止tomcat:/usr/local/tomcat-7.0.35/bin/shutdown.sh

安装Eclipse

  1. 下载:Eclipse IDE for Java EE Developers, 220 MB
  2. 使用:tar -zxvf eclipse-jee-juno-SR1-linux-gtk.tar.gz
  3. 然后进入文件夹:点开 eclipse就可以用了

MySQL:数据库的导出和导入

这几天fedora17一部分配置出问题了(懒得修复),正好赶上fedora18发布。
于是直接就赶了fedora18的潮流,直接换系统,然后把旧系统的一些东西复制过去(很费精力啊啊啊啊啊)

首先将mysql的文件从fedora17导出来[在fedora终端执行下面的命令,数据库文件将导入当前目录]:
*:Test作为mysql里面的数据库名称.

MySQL数据库图标
image-1700

1
2
3
mysqldump -u 数据库用户名 -p Test > Test
password://键入你的密码,密码不显示,但实际已经录入
//回车.即可将数据库导出.

然后将文件复制到fedora18,再打开终端执行下面的命令:

1
2
3
4
5
6
    mysql -u 数据库用户名 -p
    password://键入你的密码,密码不显示,但实际已经录入
    mysql> CREATE DATABASE TestDB;
        mysql> use TestDB;
        mysql> source Test;
        mysql> SELECT * FROM Test; //验证数据是否导入.

完成.

为Linux更换yum源

*:此方法与大部分Linux桌面系统兼容,不兼容方面请在评论中指明。

1.Redhat使用CentOS国内源

首先以root用户登录终端,执行以下命令:

1
[root@localhost Desktop]# cd /etc/yum.repos.d/

切换到yum源所在目录.

执行:

1
2
3
[root@localhost yum.repos.d]# ls
CentOS6-Base-163.repo  google.repo  rhel-source.repo
google-chrome.repo     redhat.repo

//执行ls命令,即可查看所有内容
对于这些原本存在的文件,为了避免出现错误,我们可以对其进行重命名:

1
[root@localhost yum.repos.d]# mv redhat.repo redhat.repoOLD

//为避免错误,对所有源执行重命名.
*:也可以将其中的文件全部删除,但这样会对操作有风险.

下面就可以进行新创建的源文件了:
使用命令: vim 163.repo
然后将下面的内容粘贴到里面,请确认粗体部分的链接可以访问!!!(如:http://mirrors.163.com/centos/6/os/)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#########################################################################
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-6 - Base - 163.com
<strong>baseurl=http://mirrors.163.com/centos/6/os/$basearch/</strong>
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#released updates
[updates]
name=CentOS-6 - Updates - 163.com
<strong>baseurl=http://mirrors.163.com/centos/6/updates/$basearch/</strong>
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-6 - Extras - 163.com
<strong>baseurl=http://mirrors.163.com/centos/6/extras/$basearch/</strong>
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-6 - Plus - 163.com
<strong>baseurl=http://mirrors.163.com/centos/6/centosplus/$basearch/</strong>
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users
[contrib]
name=CentOS-6 - Contrib - 163.com
<strong>baseurl=http://mirrors.163.com/centos/6/contrib/$basearch/</strong>
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#########################################################################

然后执行命令: yum clean
yum makecache

就可以用了,配置完成后可以使用 : yum install mysql* 测试.

2.Fedora使用国内源

虽然Fedora自身提供了软件源,但速度还是不如人意,最近我就对Fedora的软件源进行了改造,将其原本的国外的源换成了网易和搜狐的源。

其方法基本与上面相同,下面这两个repo的文件是:Fedora 17 /32 的,可以直接放到/etc/yum.repos.d/文件夹下面:

搜狐:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[releases-mirror.sohu.com]
name=fedora-sohu-releases
baseurl=http://mirrors.sohu.com/fedora/releases/17/Fedora/i386/os/
enabled=1
gpgcheck=0

[everything-mirror.sohu.com]
name=fedora-sohu-Everything
baseurl=http://mirrors.sohu.com/fedora/releases/17/Everything/i386/os/
enabled=1
gpgcheck=0

[updates-mirror.sohu.com]
name=Fedora-sohu-updates
baseurl=http://mirrors.sohu.com/fedora/updates/17/i386/
enabled=1
gpgcheck=0

网易的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Fedora-mirrors.163.com]
name=Fedora 17 – i386
baseurl=http://mirrors.163.com/fedora/updates/17/i386/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Fedora file:///etc/pki/rpm-gpg/RPM-GPG-KEY
[Everything-mirrors.163.com]
name=Everything 17 – i386
baseurl=http://mirrors.163.com/fedora/updates/17/i386/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Fedora file:///etc/pki/rpm-gpg/RPM-GPG-KEY
[updates-mirrors.163.com]
name=Fedora updates
baseurl=http://mirrors.163.com/fedora/updates/17/i386/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Fedora file:///etc/pki/rpm-gpg/RPM-GPG-KEY

然后执行命令: yum clean
yum makecache
就可以使用了,配置了两个国内源速度刷刷的啊。。。