文章列表
 
您正在查看 "Linux Ubuntu Arch Debian" 分类下的文章

2009年09月20日 19:19

While it may be hard to believe, until Hardy Heron, Ubuntu did not ship with a firewall in it’s base install. You’d never dream of putting a Windows machine out on the Internet naked, for all to poke and prod. Things are different on Ubuntu for a few reasons:

  • Fewer services are running by default on an Ubuntu install, as compared to Windows.
  • Ubuntu (and really Linux) have always had security as a priority, while Microsoft has only recently began to focus on the problems in Vista. Windows is getting better in this regard.
  • Lastly, and probably the biggest reason, the overwhelming install base of Windows makes for an easier target. Looking to do some evil? You’d likely focus your sights on something that can cause the most virtual damage – Windows and it’s 90% desktop market share.

Irregardless, running behind a software firewall is a good practice to adopt. Ubuntu 8.04 has introduced the Uncomplicated Firewall or ufw with the goal of integrating the power and flexibilty of iptables without the overcomplicated nature that usually comes along for the ride. Here’s an overview to get you up and running quickly.

Enable ufw:

sudo ufw enable

When you initially enable ufw it’s defaulted into ACCEPT mode – basically allowing all incoming and outgoing traffic – until you establish a ruleset as below.

Allowing an incoming/outgoing connect on port 22:

sudo ufw allow 22

By port and protocal:

sudo ufw allow 22/tcp

Or by service name (list of service names from /etc/services):

sudo ufw allow ssh

Deny all ssh traffic from 172.16.1.0/24 to 10.123.123.100:

sudo ufw deny proto tcp from 172.16.1.0/24 to 10.123.123.100 port 22

Deleting the rule just created above:

sudo ufw delete deny proto tcp from 172.16.1.0/24 to 10.123.123.100 port 22

Block a single IP:

sudo ufw deny from 192.16.1.245

Deny a single IP behind your firewall from ssh-ing out:

sudo ufw deny proto tcp from 192.16.1.242 to any port 22

Enable/Disable logging:

sudo ufw logging on or sudo ufw logging off

Overview of the configured ports in ufw:

sudo ufw status

And to disable ufw and the running ruleset:

sudo ufw disable

As you can see, the syntax is quite logical once you’ve spent just a few minutes with it. I believe this is enough to get anyone up and running. More information can be found at the Ubuntu Wiki.

 
2009年09月20日 12:24
#============================================================#
# /etc/smb.conf
#------------------------------------------------------------------------------------------------------------#
# SAMBA主配置文件
# 配置文件的骨架,根据你的需求选择其中的参数。
#------------------------------------------------------------------------------------------------------------#
# 测试通过的系统: Solaris系统和 Linux各个发布版本
# RedHat 6.0, 7.0 和 7.1
# Solaris 7
# Slackware 7.x
# Mandrake 6.1, 7.0 和 8.1
# SuSe 7.2
#------------------------------------------------------------------------------------------------------------#
# 最近一次更改时间: 08/12/2001
# 作者:Sebastian Sasias - sasias@Linuxmail.org
#============================================================#
#
# 这个文件按照SAMBA规范来开发的,可以参见smb.conf(5)手册。
#
# OBS: 更改本文件以后,使用"testparm"命令来测试。
#
#======================== Global Options =======================#
#
# 总配置
#
[global]
#......................................................................................................................................#
# workgroup = NT-Domain-Name o Workgroup-Name,比如: THEDOMAIN
# PDC域
workgroup = THEDOMAIN
#......................................................................................................................................#
# 在其他的机器中声明的本机器的名称
netbios name = SMBServer
#......................................................................................................................................#
# 这个声明会出现在Windows的“网络邻居”中
server string = Samba Server de este lugar
#......................................................................................................................................#
# 这一行由于安全的原因很关键,只许在局域网中特定的计算机的连接。
# 在这个例子中,是192.168.8.0(C级网络)的网络
# 和“环路”(loopback)的接口是可以连接的。
# 更多的细节,请阅读smb.conf man手册。
# 比如:只有从规定开始的ip以后的地址才能共享资源。
# 192.168.8 和 127 (以后的注释)
; hosts allow = 192.168.8. 127.
#......................................................................................................................................#
# 如果你想自动载入一个打印机的清单,这样就不必一个一个手动录入,可以使用:
; load printers = yes
#......................................................................................................................................#
# 覆盖printcap的路径是可能的。
; printcap name = /etc/printcap
#......................................................................................................................................#
# 在SystemV系统中 printcap对lpstat名称属性一定允许
# 自动地从SystemV(这么个词!:-))的spool系统中取得打印机列表。
; printcap name = lpstat
#......................................................................................................................................#
# 如果打印机系统是非标准的,就需要指定是什么打印系统。
# 现在支持的打印系统有:
# bsd, sysv, plp, lprng, aix, hpux, qnx
; printing = bsd
#......................................................................................................................................#
# 如果你需要一个guest帐户,你不要注释掉下面这行。
# 你一定么加入这条到/etc/passwd里,否则这个用户无“人”可用。
; guest account = pcguest
#......................................................................................................................................#
# 下面这条就是使得每台计算机都有一个不同的log文件,
# 通过这个文件和SAMBA服务器相连。
log file = /var/log/samba/log.%m
#......................................................................................................................................#
# 设置log文件长度的限制(单位 Kb)。
max log size = 50
#......................................................................................................................................#
# 阅读security_level.txt for得到更多的细节
# 指定验证密码的方式
# 用户级的安全策略=每个用户都有自己的密码 (SAMBA密码)
security = user
#......................................................................................................................................#
# 如果使用服务器级的安全策略,验证过程在另一个机器上进行。
# 只有在使用服务器级的安全策略时,才使用值"password server"
# 密码服务器等于【认证服务器地址】。
; password server = <NT-Server-Name>
#......................................................................................................................................#
# 如果你想使用加密的密码,请阅读Samba文档中的ENCRYPTION.TXT,
# Win95.txt和WinNT.txt。
# 你只有清楚的了解这个属性的足够多的信息才能使用它。
# 信息:Win95,Win98和 WinNT 可以发送加密的密码。
encrypt passwords = yes
#......................................................................................................................................#
# 使用下列的行来定制你的配置。
# 在网络中的每个计算机,%m 取代了自身netbios的名字。
; include = /usr/local/samba/lib/smb.conf.%m
#......................................................................................................................................#
# 在你会发现文档和一些流行的“提示”会告诉你下面的选项可以得到更好的性能。
# 试试!
# 阅读speed.txt和手册来知道更多的细节。
socket options = TCP_NODELAY
#......................................................................................................................................#
# Samba 可以配置多种的网络接口。
# 如果你使用多种网络接口,你一定要在下面列出来。
# 阅读手册来知道更多的细节。
; interfaces = 192.168.8.2/24 192.168.12.2/24
#......................................................................................................................................#
# Browser 控制选项:
# 如果你不想让samba做为网络中的主browser, 设置"local master = no"。
local master = yes
#......................................................................................................................................#
# 在OS 水平上,本服务器当选主browser优先权的设置。
# 一般地,缺省值可能就可以了。
; os level = 33
#......................................................................................................................................#
# 域主机指定Samba成为域中主Browser。
# 这样允许Samba运行域控制器和在不同的TCP/IP子网中能被“看成”一台机器。
# 如果你使用了 Windows NT/2000域控制器,你就不应该使用它。
domain master = yes
#......................................................................................................................................#
# 更高级的域主在启动中使得Samba成为一个局域的Browser,
# 这使得它有更多的机会(选举成为域主)。
# 如果我们有2个以上的服务器,级别高的服务器会更受“欢迎”,
# 客户机会在一个列表中搜寻到一台服务器的。
preferred master = yes
#......................................................................................................................................#
# 只有你使用NT/2000 服务器在以一个主域控制器(PDC)在运行,你才能使用下条。
; domain controller = <NT-Domain-Controller-SMBName>
#......................................................................................................................................#
# 如果你想把SAMBA当成Windows 9x/Me 工作站的“域登陆服务器”,你要使用下条。
domain logons = yes
#......................................................................................................................................#
# 如果你使用了“域登陆”,你一定要使用一个登陆脚本,
# 在Windows网络中的每台机器或者每个用户。

# 每个工作站的特定登陆批处理是
; logon script = %m.bat

# 每个用户的特定登陆批处理是
; logon script = %U.bat
#......................................................................................................................................#
# 那里存放零星的profiles文件 (只对Win95和 WinNT有效)
# %L 取代这个服务器的NetBIOS名字, %U 取代用户名
# 如果你使用它,一定不要注释掉下面的Profiles共享
; logon path = \\%L\Profiles\%U
#......................................................................................................................................#
# Windows互联网解析服务器:
# WINS支持 — 告知NMBD使能它的WINS 服务器。
# WINS协议 把机器名转换成IP地址,
# 它象TCP/IP中DNS那样工作。
; wins support = yes
#......................................................................................................................................#
# WINS服务器-告知Samba的NMBD部件成为WINS的一个客户。
# SAMBA服务器可以成为其中的一个:WINS 服务器或 WINS客户机,
# 但是不可以同时2者皆是。
# 这里WINS IP服务器一定要指定。
; wins server = 192.168.8.1
#......................................................................................................................................#
# WINS代理-告知Samba回应那些没有WINS能力的客户的名字解析的请求,
# 这个情况只有在网络中至少有一台WINS服务器时才有效。
# 缺省是不。
; wins proxy = yes
#......................................................................................................................................#
# DNS代理-告知Samba是否解析NetBIOS名字
# 版本1.9.17 内建的缺省是“是”, 从版本1.9.18 变成了“否”
# 这里我们可以告知SAMBA名字解析使用DNS或者不。
# dns proxy = yes
# dns proxy = no (name resolution will be made by using the file lmhosts )
#......................................................................................................................................#
# 如果登陆的驱动盘没有被指定,Z:单元会自动登陆的。
logon drive = P:
#......................................................................................................................................#
# 当一个登陆出现时,这个脚本被执行: /etc/samba/netlogon/SAMBA.BAT
# 并且使用"netuse"来登陆磁盘单元
logon script = SAMBA.BAT

#====================== Share Definitions ========================#

# 每个用户的私人目录
# 单位 P:

[homes]
comment = Home Directories
browseable = no
writable = yes
readonly = no
force create mode = 0700
create mode = 0700
force directory mode = 0700
directory mode = 700

#------------------------------------------------------------------------------------------------------------#
# 临时文件目录
# 单位 T:

[tmp]
comment = Tempora Files
path = /tmp
readonly = no
public = yes
writable = yes
force create mode = 0777
create mode = 0777
force directory mode = 0777
directory mode = 0777

#------------------------------------------------------------------------------------------------------------#
# 服务器的CD-ROM
# 单位 L:

[cdrom]
comment = CD-ROM
path = /mnt/cdrom
public = yes
writable = no

#------------------------------------------------------------------------------------------------------------#
# 组,根据/home/grp.name_group来的
# /home/user/group 是 /home/grp.name_group一个链接
# grp.name_group 使用权限770
# 单位 G:

[group]
comment = Directory of Group
path = /home/%u/group
writable = yes
readonly = no
force create mode = 0770
create mode = 0770
force directory mode = 0770
directory mode = 0770

#------------------------------------------------------------------------------------------------------------#
# 这个单元存放应用软件,安装软件,专用软件等。
# /net 和 /net/install的权限是 755, 比如这里, root是它的拥有者
# 单位 N:

[net]
comment = Directory Net
path = /net
writable = yes
readonly = no
force create mode = 0750
create mode = 0750
force directory mode = 0750
directory mode = 0750

#------------------------------------------------------------------------------------------------------------#
[netlogon]
comment = Logon Services in the Network
path = /etc/samba/netlogon
guest ok = yes
writable = no
locking = no
public = no
browseable = yes
share modes = no

#------------------------------------------------------------------------------------------------------------#
 
2009年08月26日 23:12

所有用到到包有:

cmake-2.6.4.tar.gz (编nlpbamboo用)

CRF++-0.53.tar.gz(同上)

nlpbamboo-1.1.1.tar.bz2(分词用)

postgreSQL-8.3.3.tar.gz(索引用)

安装pgsql

tar -zxvf postgreSQL-8.3.3.tar.gz

cd postgre-8.3.3

./configure –prefix=/opt/pgsql

make
make install

useradd postgre

chown -R postgre.postgre /opt/pgsql
su – postgre
vi ~postgre/.bash_profile
添加
export PATH
PGLIB=/opt/pgsql/lib
PGDATA=/data/PGSearch
PATH=$PATH:/opt/pgsql/bin
MANPATH=$MANPATH:/opt/pgsql/man
export PGLIB PGDATA PATH MANPATH

# mkdir -p /data/PGSearch

# chown -R postgre.postgre /data/PGSearch

# chown -R postgre.postgre /opt/pgsql

#sudo -u postgre /opt/pgsql/bin/initdb –locale=zh_CN.UTF-8 –encoding=utf8 -D /data/PGSearch

#sudo -u postgre /opt/pgsql/bin/postmaster -i -D /data/PGSearch & //允许网络访问

#sudo -u postgre /opt/pgsql/bin/createdb kxgroup
# vim /data/PGSearch/pg_hba.conf 如下增加可访问的机器:

host all all 10.2.19.178 255.255.255.0 trust

#su – postgre

$pg_ctl stop

$postmaster -i -D /data/PGSearch &
安装中文分词(Cmake CRF++ bamboo)
Cmake是为了编译bamboo,CRF++是bamboo依赖的。

tar -zxvf cmake-2.6.4.tar.gz

cd cmake-2.6.4
./configure
gmake
make install

tar -zxvf CRF++-0.53.tar.gz
cd CRF++-0.53
./configure
make
make install

tar -jxvf nlpbamboo-1.1.1.tar.bz2
cd nlpbamboo-1.1.1
mkdir build
cd build/
cmake .. -DCMAKE_BUILD_TYPE=release
make all
make install

cp index.tar.bz2 /opt/bamboo/
cd /opt/bamboo/
tar -jxvf index.tar.bz2

#/opt/bamboo/bin/bamboo

如果出现:

ERROR: libcrfpp.so.0: cannot open shared object file: No such file or directory

就执行:

ln -s /usr/local/lib/libcrfpp.so.* /usr/lib/
ldconfig

增加上中文分词扩展到pgsql

#vim /root/.bash_profile 也增加:

PGLIB=/opt/pgsql/lib
PGDATA=/data/PGSearch
PATH=$PATH:/opt/pgsql/bin
MANPATH=$MANPATH:/opt/pgsql/man
export PGLIB PGDATA PATH MANPATH

#source ~/.bash_profile

cd /opt/bamboo/exts/postgres/chinese_parser/
make
make install

su – postgre
cd /opt/pgsql/share/contrib/
touch /opt/pgsql/share/tsearch_data/chinese_utf8.stop
psql kxgroup
\i chinese_parser.sql 导入

再执行下面的sql,已经可以将一段话分词了:

SELECT to_tsvector(’chinesecfg’, ‘结果在命令行下执行bamboo才知道’);

先到这里,下一部分讲述对TEXT字段进行索引和查询,完整构造一整个搜索引擎。

一、基础篇

本回从一条sql开始:

select * from dbname where field_name @@ ‘aa|bb’ order by rank(field_name, ‘aa|bb’);

从这个sql字面意思讲解:从 dbname这个表中查field_name匹配aa或者是bb的词,并且按照他们的匹配的RANK排序。

基本上明白上面这段话后,来学习四个概念:tsvector、tsquery、@@ 、gin。

1.tsvector:

在postgreSQL 8.3自带支持全文检索功能,在之前的版本中需要安装配置tsearch2才能使用。它提供两个数据类型(tsvector,tsquery),并且通过 动态检索自然语言文档的集合,定位到最匹配的查询结果,tsvector正是其中之一。

一个tsvector的值是唯一分词的分类列表,把一话一句词格式化为不同的词条,在进行分词处理的时候,tsvector会自动去掉分词中重复的词条,按照一定的顺序装入。例如

SELECT ‘a fat cat sat on a mat and ate a fat rat’::tsvector;
tsvector
—————————————————-
‘a’ ‘on’ ‘and’ ‘ate’ ‘cat’ ‘fat’ ‘mat’ ‘rat’ ’sat’

通过tsvector把一个字符串按照空格进行分词,这可以把分词后的词按照出现的次数排成一排(还会按词长度)。

对于英文和中文的全文检索我们还要看下面这条sql:

SELECT to_tsvector(’english’, ‘The Fat Rats’);
to_tsvector
—————–
‘fat’:2 ‘rat’:3

to_tsvector函数来是tsvector规格化的,在其中可指定所使用的分词。

2.tsquery:

顾名思义,tsquery,表示的应该是查询相关的.tsquery是存储用于检索的词条.并且可以联合使用boolean 操作符来连接, & (AND), | (OR), and ! (NOT). 使用括号(),可以强制分为一组.

同时,tsquery 在做搜索的时候,也可以使用权重,并且每个词都可以使用一个或者多个权重标记,这样在检索的时候,会匹配相同权重的信息.跟上面的tsvector相同,tsquery也有一个to_tsquery函数.

3.@@:

在postgresql中全文检索匹配操作使用@@ 操作符,如果一个
tsvector(document) 匹配到 tsquery(query)则返回true.

看一个简单的例子:

SELECT ‘a fat cat sat on a mat and ate a fat rat’::tsvector @@ ‘cat & rat’::tsquery;
?column?
———-
t
我们在处理索引的时候还是要使用他们的函数如下:
SELECT to_tsvector(’fat cats ate fat rats’) @@ to_tsquery(’fat & rat’);
?column?
———-
t
并且操作符 @@ 可以使用text作为tsvector和tsquery.如下操作符可以使使用的方法

tsvector @@ tsquery
tsquery  @@ tsvector
text @@ tsquery
text @@ text
上面的前两种我们已经使用过了,但是后两种,
text @@ tsquery 等同于 to_tsvector(x) @@ y.
text @@ text 等同于 to_tsvector(x) @@ plainto_tsquery(y).(~)plainto_tsquery在后面讲。。。

4.gin:

gin是一种索引的名称,全文索引用的。

我们可以通过创建gin索引来加速检索速度.例如

CREATE INDEX pgweb_idx ON pgweb USING gin(to_tsvector(’english’, body));

创建索引可以有多种方式.索引的创建甚至可以连接两个列:
CREATE INDEX pgweb_idx ON pgweb USING gin(to_tsvector(’english’, title || body));

二、提高篇

基础知识学完了,应该上阵了,为了实现全文检索,我们需要把一个文档创建一个tsvector 格式,并且通过tsquery实现用户的查询,在查询中我们返回一个按照重要性排序的查询结果。

先看一个to_tsquery的sql:

SELECT to_tsquery(’english’, ‘Fat | Rats:AB’);
to_tsquery
——————
‘fat’ | ‘rat’:AB

可以看出,to_tsquery函数在处理查询文本的时候,查询文本的单个词之间要使用逻辑操作符(& (AND), | (OR) and ! (NOT))连接(或者使用括号)。

如果执行下面这条sql就会出错:

SELECT to_tsquery(’english’, ‘Fat  Rats’);

plainto_tsquery函数却可以提供一个标准的tsquery,如上面的例子,plainto_tsquery会自动加上逻辑&操作符。
SELECT plainto_tsquery(’english’, ‘Fat  Rats’);

plainto_tsquery
—————–
‘fat’ & ‘rat’
但是plainto_tsquery函数不能够识别逻辑操作符和权重标记。
SELECT plainto_tsquery(’english’, ‘The Fat & Rats:C’);
plainto_tsquery
———————
‘fat’ & ‘rat’ & ‘c’

三、终结篇

看完上面的一堆后,千言万语汇成一句话,本文主要讲的是一条sql,在加了第一部分里所讲述的扩展后,使用下面的sql,从一个字段中搜一句话,还要排序出来:

select * from tabname where to_tsvector(’chinesecfg’,textname) @@ plainto_tsquery(’搜点啥’) order by ts_rank(to_tsvector(’chinesecfg’,textname),plainto_tsquery(’搜点啥’)) limit 10;

之前的create table create index就不写了。授人以渔才是关键。

 
2009年08月26日 22:45

1: iptables -A INPUT -p tcp -syn -j DROP

This is a desktop-centric rule that will do two things: First it will allow you to actually work normally on your desktop. All network traffic going out of your machine will be allowed out, but all TCP/IP traffic coming into your machine will simply be dropped. This makes for a solid Linux desktop that does not need any incoming traffic. What if you want to allow specific networking traffic in — for example, ssh for remote management? To do this, you’ll need to add an iptables rule for the service and make sure that service rule is run before rule to drop all incoming traffic.

2: iptables -A INPUT -p tcp –syn –destination-port 22 -j ACCEPT

Let’s build on our first command. To allow traffic to reach port 22 (secure shell), you will add this line. Understand that this line will allow any incoming traffic into port 22. This is not the most secure setup alone. To make it more secure, you’ll want to limit which machines can actually connect to port 22 on the machine. Fortunately, you can do this with iptables as well. If you know the IP address of the source machine, you can add the -s SOURCE_ADDRESS option (Where SOURCE_ADDRESS is the actual address of the source machine) before the –destination-port portion of the line.

3: /sbin/iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT

This will allow all previously initiated and accepted exchanges to bypass rule checking. The ESTABLISHED and RELATED arguments belong to the –state switch. The ESTABLISHED argument says, “Any packet that belongs to an existing connection,” and the RELATED argument says, “Any packet that does not belong to an already existing connection but is related to an existing connection.” The “state machine” of iptables is a means for iptables to track connections with the help of the kernel level “conntrack” module. By tracking connections, iptables knows what connections can be allowed and what can’t. This reduces the amount of work the administrator has to do.

Here’s how state works. If the local user initiates a connection, that packet (to that connection) is set as NEW in the prerouting chain. When the local user gets a return packet, the state is changed to ESTABLISHED in the prerouting chain. So when a state is set as ESTABLISHED, it can be allowed with the right iptables rule.

4: iptables -N LOGDROP

With this handy chain, iptables will log all dropped packets. Of course, this is only part of the chain. To complete it, you need to add the follow two rules: iptables -A logdrop -J LOG and iptables -A logdrop -J DROP. Now all matching packets (in this case, anything that has been dropped) will be added to the logdrop chain which will log them and then drop them.

5: iptables -t nat -A PREROUTING -i WLAN_INTERFACE -p tcp –dportPORTNUMBERS -j DNAT –to-destination DESTINATION_IP

When you need to route packets from external sources to specific ports on specific internal machines, this is what you want to do. This rule takes advantage of network address translation to route packets properly. To suit your needs, the WLAN_INTERFACE must be changed to the WLAN interface that bridges the external network to the internal network, the PORTNUMBERS must be changed, and DESTINATION_IP must be changed to match the IP address of the destination machine.

6: iptables -A INPUT -p tcp –syn –dport 25 -j ACCEPT

This is the beginning of a SYN flood protection rule. This portion of the rule blocks DoS attacks on a mail server port. (You can change this to suit your mail server needs.) There are three more portions of this rule set. The first is to add the same rule but modify the port to whatever is being served up by whatever ports you have open. The next portion is iptables -A INPUT -p tcp –syn -m limit –limit 1/s –limit-burst 4 -j ACCEPT, which is the actual SYN flood protection. Finally, iptables -A INPUT -p tcp –syn -j DROP will drop all SYN flood packets.

7: iptables -A INPUT -p tcp -m tcp -s MALICIOUS_ADDRESS -j DROP

This is where you can take care of malicious source IP addresses. For this to work properly, you must make sure you know the offending source IP address and that, in fact, it’s one you want to block. The biggest problem with this occurs when the offending address has been spoofed. If that’s the case, you can wind up blocking legitimate traffic from reaching your network. Do your research on this address.

8: iptables -N port-scan

This is the beginning of a rule to block furtive port scanning. A furtive port scan is a scan that detects closed ports to deduce open ports. Two more lines are needed to complete this rule:

iptables -A port-scan -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j RETURN
iptables -A port-scan -j DROP

Notice that the above rule set is adding a new chain called “port-scan”. You don’t have to name it such; it’s just easier to keep things organized. You can also add timeouts to the above rule set like so:

iptables -A specific-rule-set -p tcp --syn -j syn-flood
iptables -A specific-rule-set -p tcp --tcp-flags SYN,ACK,FIN,RST RST -j port-scan

9: iptables -A INPUT -i eth0 -p tcp -m state –state NEW -m multiport –dports ssh,smtp,http,https -j ACCEPT

What you see here is a chain making use of the multiport argument, which will allow you to set up multiple ports. Using the multiport argument lets you write one chain instead of multiple chains. This single rule saves you from writing out four separate rules, one each for ssh, smtp, http, and https. Naturally, you can apply this to ACCEPT, DENY, REJECT.

10: iptables -A PREROUTING -i eth0 -p tcp –dport 80 -m state –state NEW -m nth –counter 0 –every 4 –packet 0 -j DNAT –to-destination 192.168.1.10:80

If you’re looking to load balance between multiple mirrored servers (in the example case, load balancing a Web server at 192.168.1.10), this rule is what you want. At the heart of this rule is the nth extension, which tells iptables to act on every “nth” packet. In the example, iptables uses counter 0 and acts upon every 4th packet. You can extend this to balance out your mirrored sites this way. Say you have four mirrored servers up and you want to balance the load between them. You could have one line for each server like so:

iptables -A PREROUTING -i eth0 -p tcp --dport 80 -m state --state NEW -m nth --counter 0 --every 4 --packet 0 -j DNAT --to-destination 192.168.1.10:80
iptables -A PREROUTING -i eth0 -p tcp --dport 80 -m state --state NEW -m nth --counter 0 --every 4 --packet 1 -j DNAT --to-destination 192.168.1.20:80
iptables -A PREROUTING -i eth0 -p tcp --dport 80 -m state --state NEW -m nth --counter 0 --every 4 --packet 2 -j DNAT --to-destination 192.168.1.30:80
iptables -A PREROUTING -i eth0 -p tcp --dport 80 -m state --state NEW -m nth --counter 0 --every 4 --packet 3 -j DNAT --to-destination 192.168.1.40:80

As you can see the server on .10 will be routed every 0 packet, the server on .20 will be routed every 1st packet, the server on .30 will be routed every 2nd packet, and the server on .40 will be routed every 3rd packet.

================================================

iptables--静态防火墙实例教程 follow me



介绍:
这篇文章是本人原创,向读者展示了如何一步一步建立静态防火墙来保护您的计算机,同时在每一步中,我力图向读者讲述清楚原理。在这篇教程之后,你将能理解到防火墙内在过滤机制,同时也能自己动手创建符合自己要求的防火墙。

版权所有,转载请注明来自www.linuxsir.org 并写明作者


1、iptables介绍

iptables是复杂的,它集成到linux内核中。用户通过iptables,可以对进出你的计算机的数据包进行过滤。通过iptables命令设置 你的规则,来把守你的计算机网络──哪些数据允许通过,哪些不能通过,哪些通过的数据进行记录(log)。接下来,我将告诉你如何设置自己的规则,从现在 就开始吧。



2、初始化工作

在shell提示符 # 下打入

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

以上每一个命令都有它确切的含义。一般设置你的iptables之前,首先要清除所有以前设置的规则,我们就把它叫做初始化好了。虽然很多情况下它什么也不做,但是保险起见,不妨小心一点吧! 如果你用的是redhat 或fedora,那么你有更简单的办法

service iptables stop



3、开始设置规则:

接下下开始设置你的规则了

iptables -P INPUT DROP

这一条命令将会为你构建一个非常“安全”的防火墙,我很难想象有哪个hacker能攻破这样的机器,因为它将所有从网络进入你机器的数据丢弃(drop) 了。这当然是安全过头了,此时你的机器将相当于没有网络。如果你ping localhost,你就会发现屏幕一直停在那里,因为ping收不到任何回应。


4 、添加规则

接着上文继续输入命令:

iptables -A INPUT -i ! ppp0 -j ACCEPT

这条规则的意思是:接受所有的,来源不是网络接口ppp0的数据。

我们假设你有两个网络接口,eth0连接局域网,loop是回环网(localhost)。ppp0是一般的adsl上网的internet网络接口,如果你不是这种上网方式,那则有可能是eth1。在此我假设你是adsl上网,你的internet接口是ppp0

此时你即允许了局域网的访问,你也可以访问localhost

此时再输入命令 ping localhost,结果还会和刚才一样吗?

到此我们还不能访问www,也不能mail,接着看吧。



5、我想访问www

iptables -A INPUT -i ppp0 -p tcp --sport 80 -j ACCEPT

允许来自网络接口ppp0(internet接口),并且来源端口是80的数据进入你的计算机。
80端口正是www服务所使用的端口。

好了,现在可以看网页了。但是,你能看到吗?


如果你在浏览器的地址中输入www.baidu.com,能看到网页吗?

你得到的结果一定是:找不到主机www.baidu.com

但是,如果你再输入220.181.27.5,你仍然能够访问baidu的网页。

为什么?如果你了解dns的话就一定知道原因了。

因为如果你打入www.baidu.com,你的电脑无法取得www.baidu.com这个名称所能应的ip地址220.181.27.5。如果你确实 记得这个ip,那么你仍然能够访问www,你当然可以只用ip来访问www,如果你想挑战你的记忆的话^ _ ^,当然,我们要打开DNS。


6、打开dns端口

打开你的dns端口,输入如下命令:

iptables -A INPUT -i ppp0 -p udp -sport 53 -j ACCEPT

这条命令的含义是,接受所有来自网络接口ppp0,upd协议的53端口的数据。53也就是著名的dns端口。

此时测试一下,你能通过主机名称访问www吗?你能通过ip访问www吗?

当然,都可以!



7、查看防火墙

此时可以查看你的防火墙了

iptables -L

如果你只想访问www,那么就可以到此为止,你将只能访问www了。 不过先别急,将上面讲的内容总结一下,写成一个脚本。



#!/bin/bash

# This is a script

# Edit by liwei

# establish static firewall

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

iptables -P INPUT DROP

iptables -A INPUT -i ! ppp0 -j ACCEPT

iptables -A INPUT -i ppp0 -p tcp --sport 80 -j ACCEPT

iptables -A INPUT -i ppp0 -p udp --sport 53 -j ACCEPT



8、复杂吗?到此iptables可以按你的要求进行包过滤了。你可以再设定一些端口,允许你的机器访问这些端口。这样有可能,你不能访问QQ,也可能不 能打网络游戏,是好是坏,还是要看你自己而定了。顺便说一下,QQ这个东西还真是不好控制,用户与服务器连接使用的好像是8888端口,而QQ上好友互发 消息使用的又是udp的4444端口(具体是不是4444还不太清楚)。而且QQ还可以使用www的80端口进行登录并发消息,看来学无止境,你真的想把 这个家伙控制住还不容易呢?还是进入我们的正题吧。

如果你的机器是服务器,怎么办?


9、如果不巧你的机器是服务器,并且要提供www服务。显然,以上的脚本就不能符合我们的要求了。但只要你撑握了规则,稍作修改同样也能很好的工作。在最后面加上一句

iptables -A INPUT -i ppp0 -p tcp --dport 80 -j ACCEPT

这一句也就是将自己机器上的80端口对外开放了,这样internet上的其他人就能访问你的www了。当然,你的www服务器得工作才行。如果你的机器 同时是smtp和pop3服务器,同样的再加上两条语句,将--dport后面的80改成25和110就行了。如果你还有一个ftp服务器,呵呵,如果你 要打开100个端口呢……

我们的工作好像是重复性的打入类似的语句,你可能自己也想到了,我可以用一个循环语句来完成,对,此处可以有效的利用shell脚本的功能,也让你体验到了shell脚本语言的威力。看下文:


10、用脚本简化你的工作,阅读下面的脚本

#!/bin/bash

# This is a script

# Edit by liwei

# establish a static firewall



# define const here

Open_ports="80 25 110 10" # 自己机器对外开放的端口

Allow_ports="53 80 20 21" # internet的数据可以进入自己机器的端口



#init

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

iptables -P INPUT DROP #we can use another method to instead it

iptables -A INPUT -i ! ppp0 -j ACCEPT



# define ruler so that some data can come in.

for Port in "$Allow_ports" ; do

iptables -A INPUT -i ppp0 -p tcp --sport $Port -j ACCEPT

iptables -A INPUT -i ppp0 -p udp --sport $Port -j ACCEPT

done

for Port in "$Open_ports" ; do

iptables -A INPUT -i ppp0 -p tcp --dport $Port -j ACCEPT

iptables -A INPUT -i ppp0 -p udp --dport $Port -j ACCEPT

done



这个脚本有三个部分(最前面的一段是注释,不算在这三部分中)

第一部分是定义一些端口:访问你的机器"Open_ports"端口的数据,允许进入;来源是"Allow_ports"端口的数据,也能够进入。

第二部分是iptables的初始化,第三部分是对定义的端口具体的操作。

如果以后我们的要求发生了一些变化,比如,你给自己的机器加上了一个ftp服务器,那么只要在第一部分"Open_ports"的定义中,将ftp对应的20与21端口加上去就行了。呵呵,到此你也一定体会到了脚本功能的强大的伸缩性,但脚本的能力还远不止这些呢!



11、使你的防火墙更加完善

看上面的脚本init部分的倒数第二句

iptables -P INPUT DROP

这是给防火墙设置默认规则。当进入我们计算机的数据,不匹配我们的任何一个条件时,那么就由默认规则来处理这个数据----drop掉,不给发送方任何应答。

也就是说,如果你从internet另外的一台计算机上ping你的主机的话,ping会一直停在那里,没有回应。

如果黑客用namp工具对你的电脑进行端口扫描,那么它会提示黑客,你的计算机处于防火墙的保护之中。我可不想让黑客对我的计算机了解太多,怎么办,如果我们把drop改成其他的动作,或许能够骗过这位刚出道的黑客呢。

怎么改呢?将刚才的那一句( iptables -P INPUT DROP )去掉,在脚本的最后面加上

iptables -A INPUT -i ppp0 -p tcp -j REJECT --reject-with tcp-reset

iptables -A INPUT -i ppp0 -p udp -j REJECT --reject-with icmp-port-unreachable

这样就好多了,黑客虽然能扫描出我们所开放的端口,但是他却很难知道,我们的机器处在防火墙的保护之中。如果你只运行了ftp并且仅仅对局域网内部访问, 他很难知道你是否运行了ftp。在此我们给不应该进入我们机器的数据,一个欺骗性的回答,而不是丢弃(drop)后就不再理会。这一个功能,在我们设计有 状态的防火墙中(我这里讲的是静态的防火墙)特别有用。

你可以亲自操作一下,看一看修改前后用namp扫描得到的结果会有什么不同?



12、这个教程我想到此就结束了,其中有很多东西在这里没有提到,如ip伪装,端口转发,对数据包的记录功能。还有一个很重要的东西就 是:iptables处理数据包的流程.在这里我想告诉你,你设置的过滤规则的顺序很重要,在此不宜详细介绍,因为这样一来,这个教程就会拘泥于细节。

iptables是复杂的,我在linuxsir上看过很多教程,它们往往多而全,反而让人望而生畏,希望我的这个教程,能够指导你入门。加油!

最后,我把完整的脚本写出来如下,你只要修改常量定义部分,就能表现出较大的伸缩性^_^

#!/bin/bash

# This is a script

# Edit by liwei

# establish a static firewall



# define const here

Open_ports="80 25 110 10" # 自己机器对外开放的端口

Allow_ports="53 80 20 21" # internet的数据可以进入自己机器的端口



#init

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

# The follow is comment , for make it better
# iptables -P INPUT DROP

iptables -A INPUT -i ! ppp0 -j ACCEPT



# define ruler so that some data can come in.

for Port in "$Allow_ports" ; do
ptables -A INPUT -i ppp0 -p tcp --sport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport $Port -j ACCEPT
done

for Port in "$Open_ports" ; do
iptables -A INPUT -i ppp0 -p tcp --dport $Port -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --dport $Port -j ACCEPT
done

# This is the last ruler , it can make you firewall better
iptables -A INPUT -i ppp0 -p tcp -j REJECT --reject-with tcp-reset
iptables -A INPUT -i ppp0 -p udp -j REJECT --reject-with icmp-port-unreachable

 
2009年08月09日 12:15
安装netcfg工具
pacman -S netcfg

设置netcfg的配置文件
cp /etc/network.d/examples/wireless-wpa-config  /etc/network.d/wireless-home

修改/etc/rc.conf
NETWORKS = (wireless-home)
DEAMON = (...  net-profiles)
如此便实现了开机自动查找网络

手工获取
netcfg wireless-home 或
/etc/rc.d/net-profiles start
 
   
 
 
文章存档
 
     
 
最新文章评论
  

很及时,谢谢!
 

回复leader20:没有
 

这个跟django版本有关系吗?
 

装了n回,才找到这个
 

你爱的我也爱
   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu