debian做软路由的笔记和策略
原地址:http://linux.chinaunix.net/bbs/archiver/?tid-1150404.html
采用debian 4.0r8-adm64-netinst
采用以下源代码:
linux-2.6.21.7.tar.bz2
iptables-1.3.8.tar.bz2
patch-o-matic-ng-20091205
ipp2p.0.99.15.tar.gz
hashspeed-0.1.tar.gz
l7-protocols-2008-12-18.tar.gz
netfilter-layer7-v2.20.tar.gz
________________________________________
安装后debian基本操做系统后:配置网络
————————————————————————————
1:连上外网
设ip 例如: 二张网卡。eth0 为内网(121.9.XX) eth0 为外网(192.168.0.1)
# The primary network interface
auto eth0
allow-hotplug eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.254.0
network 192.168.0.0
auto eth1
allow-hotplug eth1
iface eth1 inet static
address 121.9.XX
netmask 255.255.XX.XX
network 121.9.XX.XX
oadcast 121.9.XX.XX
gateway 121.9.XX.XX
----------------------------
2:设DNS
/etc/resolv.conf
nameserver 202.96.128.86
nameserver 202.96.128.166
(如果只是做个简单的路由无防火无限止的话,此时已经可以了只要二条命令)
#:echo 1 > /proc/sys/net/ipv4/ip_forward (开启转发)
#:iptables -t nat -A POSTROUTING -s $eth0 -o $eth1 -j MASQUERADE (eth0 转向 eth1)
——————————————————————————
3:设置软件包源
/etc/apt/sources.list
#cn99的国内最快的
deb http://debian.cn99.com/debian stable main
deb-src http://debian.cn99.com/debian stable main
stables 意思就是稳定版的
设好后apt-get update 更新成功!可以安装软件了
————————————————————————————
4:安装后面工作需要的工具包:
#:apt-get install kernel-package ncurses-dev fakeroot wget bzip2 make patch
#:apt-get install ncurses-bin (不安装这个就不能用iptables的补丁)
#:apt-get install openssh-server (安装ssh远程)
记得修改ssh的配置文件:比如换个端口(必要的)不允许root登陆
5:解压 放至/usr/src/ 目录下方便管理
linux-2.6.21.7.tar.bz2
iptables-1.3.8.tar.bz2
patch-o-matic-ng-20091205
ipp2p.0.99.15.tar.gz
hashspeed-0.1.tar.gz
l7-protocols-2008-12-18.tar.gz
netfilter-layer7-v2.20.tar.gz
解压:bz2 的tar jxvf
gz 的tar zxvf
这后新建二个连接方便管理
#:ln -s linux-XXXXX linux
#:ln -s iptables-XXXX iptables
---------------------------
6:进入解压出来的内核源码中,复制当中内核的配置文件到新内核源码上。
#:cp /boot/conXXXXXXXXXX ./.config (命名为.config)
7:先给内核打iptables的补丁。
#:cd /usr/src/patch-o-matic-ng-20091205/
/usr/src/patch-o-matic-ng-20091205#export KERNEL_DIR=/usr/src/linux
/usr/src/patch-o-matic-ng-20091205#export IPTABLES_DIR=/usr/src/iptables
/usr/src/patch-o-matic-ng-20091205#./runme --download
之后就会显示:
——————————————————————————
Successfully downloaded external patch geoip
Successfully downloaded external patch condition
Successfully downloaded external patch IPMARK
Successfully downloaded external patch ROUTE
Successfully downloaded external patch connlimit
Successfully downloaded external patch ipp2p
Successfully downloaded external patch time
Successfully downloaded external patch ipv4options
Successfully downloaded external patch TARPIT
Successfully downloaded external patch ACCOUNT
Successfully downloaded external patch pknock
Hey! KERNEL_DIR is not set.
Where is your kernel source directory? [/usr/src/linux]
Hey! IPTABLES_DIR is not set.
Where is your iptables source code directory? [/usr/src/iptables]
Loading patchlet definitions........... done
Excellent! Source trees are ready for compilation.
————————————————————————————
然后:
/usr/src/patch-o-matic-ng-20091205#./runme connlimit (按示就按Y确定)
8:再给内核打L7补丁。
#:cd /usr/src/linux
/usr/src/linux#patch -p1 <../netfilter-layer7-v2.21/for_older_kernels/kernel-2.6.20-2.6.21-layer7-2.16.1.patch
9:给iptables 打补丁
#:cd /usr/src/iptables
/usr/src/iptables#patch -p1
Networking options --->
Network packet filtering framework (Netfilter) --->
Core Netfilter Configuration --->
IP: Netfilter Configuration --->
可以把Core Netfilter Configuration IP: Netfilter Configuratio 下的所有都加入
慢慢看咯,最后退出时保存,
#:make-kpkg clean
#:make-kpkg --initrd kernel_image modules_image (这是生成deb包的内核)
不出意外的话在内核源文件的上级目录下会出现linux-image-XXXXXXXX.deb
直接dpkg -i 安装
/usr/src/linux#dpkg -i linux-image-2.6.21.7_2.6.21.7-10.00.Custom_amd64.deb
因为是2.6的内核,所以不用修改引导参数,直接重启进入新内核
-----------------------------
11:安装iptables
#:cd /usr/src/iptables
/usr/src/iptables#make KERNEL_DIR=/usr/src/linux
/usr/src/iptables#make install KERNEL_DIR=/usr/src/linux
重启。
#:iptalbes -V 查看是不是更新了
---------------------------------
12:安装L7
#:cd /usr/src/l7-protocols-2008-12-18
/usr/src/l7-protocols-2008-12-18#make install
mkdir -p /etc/l7-protocols
cp -R * /etc/l7-protocols
现在就已经可以用L7过滤了,
#:iptables -m layer7 --help
13:编译ipp2p模块并安装
#:cd /usr/src/ipp2p:0.99.15
修改
/usr/src/ipp2p:0.99.15#vim Makefile
修改:KERNEL_SRC = /usr/src/linux IPTABLES_SRC = /usr/src/iptables
/usr/src/ipp2p:0.99.15#make
后会显示如下才是通过 因为不是原作者所以编译模块是因内核和各软件包源版本而定,能通过
就能通过,不能通过,除非改源代码。- -
make -C /usr/src/linux-2.6.21.7 M=/usr/src/ipp2p-0.99.15 modules
make[1]: Entering directory `/usr/src/linux-2.6.21.7'
CC [M] /usr/src/ipp2p-0.99.15/ipt_ipp2p.o
Building modules, stage 2.
MODPOST 1 modules
CC /usr/src/ipp2p-0.99.15/ipt_ipp2p.mod.o
LD [M] /usr/src/ipp2p-0.99.15/ipt_ipp2p.ko
make[1]: Leaving directory `/usr/src/linux-2.6.21.7'
gcc -O3 -Wall -DIPTABLES_VERSION=\"1.3.8\" -I/usr/src/iptables-1.3.8/include -fPIC -c libipt_ipp2p.c
gcc -shared -o libipt_ipp2p.so libipt_ipp2p.o
已经编译成功了现在只要安装就行了,手动吧
/usr/src/ipp2p:0.99.15#cp ipt_ipp2p.ko /lib/modules/2.6.21.7/kernel/net/ipv4/netfilter/
/usr/src/ipp2p:0.99.15#cp libipt_ipp2p.so /usr/local/lib/iptables/
然后:depmod -a 重新加载内核模块就可以用了。
测试:iptables -I FORWARD -m ipp2p --xunlei -j DROP (会显示ipp2p的版本号)
#:iptables -L -n 查看结果
target prot opt source destination
DROP all -- 0.0.0.0/0 0.0.0.0/0 ipp2p v0.99.15 --xunlei
如何运行ipp2p内核模块 查看iptables -m ipp2p --help
-------------------------------------------------------------
14
:编译hashspeed.0.1 些模块功能是进行包或字节的匹配,也就是限速- -
#:cd /usr/src/hashspeed.0.1
/usr/src/hashspeed.0.1#vim Makefile
修改:KERNEL_SRC = /usr/src/linux和IPTABLES_SRC = /usr/src/iptables
/usr/src/hashspeed.0.1#make
make -C /usr/src/linux M=/usr/src/hashspeed-0.1 modules
make[1]: Entering directory `/usr/src/linux-2.6.21.7'
CC [M] /usr/src/hashspeed-0.1/ipt_hashspeed.o
Building modules, stage 2.
MODPOST 1 modules
CC /usr/src/hashspeed-0.1/ipt_hashspeed.mod.o
LD [M] /usr/src/hashspeed-0.1/ipt_hashspeed.ko
make[1]: Leaving directory `/usr/src/linux-2.6.21.7'
cc -O2 -Wall -DIPTABLES_VERSION=\"1.3.8\" -I/usr/src/iptables/include -I/usr/src/linux/include -fPIC -c libipt_hashspeed.c
cc -shared -o libipt_hashspeed.so libipt_hashspeed.o
和IPP2P一样,手动cp文件
/usr/src/hashspeed.0.1#:cp ipt_hashspeed.ko /lib/modules/2.6.21.7/kernel/net/ipv4/netfilter/
/usr/src/hashspeed.0.1#:cp libipt_hashspeed.so /usr/local/lib/iptables/
然后:depmod -a 重新加载内核模块就可以用了。
/usr/src/hashspeed.0.1#iptables -m hashspeed --help 查看帮助
测试:
#:iptables -N QOS
#:iptables -A FORWARD -i eth0 -p ALL -m hashspeed \! --hashspeed-bytes 56K --hashspeed-mode srcip --hashspeed-name slimit -j QOS
#:iptables -A FORWARD -o eth0 -p ALL -m hashspeed \! --hashspeed-bytes 128K --hashspeed-mode dstip --hashspeed-name dlimit -j QOS
#:iptables -L -n 查看结果
QOS all -- 0.0.0.0/0 0.0.0.0/0 ! limit: bytes avg 64K mode srcip
QOS all -- 0.0.0.0/0 0.0.0.0/0 ! limit: bytes avg 128K mode dstip
如何运行hashspeed内核模块 查看iptables -m hashspeed --help
---------------------------------------------------------
15:安装FTP
#:apt-get install vsftpd
配置:#:/etc/vsftpd.conf
anonymous_enable=NO 禁止匿名登陆
local_enable=YES 允许本地用户
write_enable=YES 允许回写
首先创建用户组ftp和arp用户目录
#:groupadd ftp (组名是随便起的,只是便于管理以后可以随时cat /etc/group |grep ftp)
#:mkdir /down/arp/ /down这目录是分区的时候设置的 也可以弄成/home/arp/ 总之就是新建arp目录
然后创建用户
#:useradd -g ftp -d /down/arp/ arp(arp是用户名)
注:G:用户所在的组 d:指定创建用户的自己目录
接着改变文件夹属于的组和用户
chown 用户.组 路径
#:chown arp.ftp /down/arp
#:chmod 711 /down/arp 给予权限
/etc/init.d/vsftpd restart
测试
然后在FTP目录下新建个文件
#:touch arp
建立FTP就是为了方便管理:
如将内网所有客户机的IP和MAC写入些表中,服务器可以设置每60秒自动绑定一次此表中的IP对应的MAC。
写法:
IP MAC
127.0.0.1 00:11:22:33:44:55
16:设置每60秒自动绑定一次ARP
#:touch arp.sh (新建一个脚本内容为下)
arp -f /down/arp/arp
#:chmod 755 /down/arp/arp 执行权限
修改:
#:vim /etc/crontab
分 时 日 月 星期 执行脚本 那么就如下添加
** * * * * root /root/arp.sh
________________________________________________________________
_________________________________________________________
以下是iptables.sh脚本 防火策略。
#!/bin/bash
##########分配设定##########
LAN="eth0"
WAN="eth1"
LANIP0="192.168.0.0/23"
WANIP="121.9.XX.XX"
WGIP="192.168.0.1"
WGMAC="00:01:02:03:04:05"
######清除所有策略#######
iptables -F
iptables -X
iptables -Z
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -F -t mangle
iptables -X -t mangle
iptables -Z -t mangle
#######重置策略######
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
###### ARP绑定 #######
arp -f /dow/arp/arp
arp -s $WGIP $WGMAC -i $LAN
###抵御小量SYN###
sysctl -w net.ipv4.tcp_max_syn_backlog=2048
sysctl -w net.ipv4.tcp_synack_retries=3
sysctl -w net.ipv4.tcp_syn_retries=3
echo 10 > /proc/sys/net/ipv4/tcp_fin_timeout
echo 600 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established
echo "105500" > /proc/sys/net/ipv4/netfilter/ip_conntrack_max
#####加载所需模块#####
#modprobe
#####开启路由转发(IP伪装)#####
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s $LANIP -o $WAN -j MASQUERADE
#:用下行参数的话,可以在254的掩码中,二个IP段。0和1
#iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE
###########端口映射###########
####vnc####
#iptables -t nat -A PREROUTING -d $WANIP -p tcp --dport 5900 -j DNAT --to-destination 192.168.0.2:5900
####临控####
iptables -t nat -A PREROUTING -d $WANIP -p tcp --dport 8001 -j DNAT --to-destination 192.168.0.5:8001
iptables -t nat -A PREROUTING -d $WANIP -p tcp --dport 39777 -j DNAT --to-destination 192.168.0.5:39777
iptables -t nat -A PREROUTING -d $WANIP -p tcp --dport 37777 -j DNAT --to-destination 192.168.0.5:37777
iptables -t nat -A PREROUTING -d $WANIP -p tcp --dport 37778 -j DNAT --to-destination 192.168.0.5:37778
iptables -t nat -A PREROUTING -d $WANIP -p tcp --dport 47777 -j DNAT --to-destination 192.168.0.5:47777
#######端口映射结束#########
###################安全策略开始####################
###限止PING数量 禁止外网PING###
iptables -N PING
iptables -A PING -p icmp --icmp-type echo-request -m limit --limit 2/second -j RETURN
iptables -A PING -p icmp -j DROP
iptables -I INPUT -p icmp --icmp-type echo-request -m state --state NEW -j PING
iptables -A INPUT -p icmp --icmp-type echo-request -i $WAN -j DROP
#### 限制tcp连接数 ###
iptables -N CONNLIMIT
iptables -A FORWARD -p tcp ! --dport 80 --syn -i $LAN -m connlimit --connlimit-above 15 -j CONNLIMIT
iptables -A CONNLIMIT -j REJECT
#iptables -I CONNLIMIT -s 192.168.1.203 -j RETURN
iptables -N P2P
iptables -A FORWARD -p ALL -m ipp2p --ipp2p -j P2P
iptables -A P2P -j DROP
iptables -I P2P -p tcp -i $LAN -m connlimit --connlimit-above 2 -j RETURN
####以下端口INPUT 丢弃####
iptables -I INPUT -p tcp --dport 135 -j DROP
iptables -A INPUT -p udp --dport 135 -j DROP
iptables -I INPUT -p tcp --dport 136 -j DROP
iptables -A INPUT -p udp --dport 136 -j DROP
iptables -I INPUT -p tcp --dport 137 -j DROP
iptables -A INPUT -p udp --dport 137 -j DROP
iptables -I INPUT -p tcp --dport 138 -j DROP
iptables -A INPUT -p udp --dport 138 -j DROP
iptables -I INPUT -p tcp --dport 139 -j DROP
iptables -A INPUT -p udp --dport 139 -j DROP
iptables -I INPUT -p tcp --dport 445 -j DROP
iptables -A INPUT -p udp --dport 445 -j DROP
iptables -I INPUT -p tcp --dport 4444 -j DROP
iptables -A INPUT -p udp --dport 4444 -j DROP
iptables -I INPUT -p tcp --dport 5554 -j DROP
iptables -I INPUT -p tcp --dport 1434 -j DROP
iptables -A INPUT -p udp --dport 1434 -j DROP
iptables -I INPUT -p tcp --dport 2500 -j DROP
iptables -I INPUT -p tcp --dport 5900 -j DROP
iptables -I INPUT -p tcp --dport 5800 -j DROP
iptables -I INPUT -p tcp --dport 6346 -j DROP
iptables -I INPUT -p tcp --dport 6667 -j DROP
iptables -I INPUT -p tcp --dport 9393 -j DROP
iptables -I INPUT -p tcp --dport 593 -j DROP
iptables -A INPUT -p udp --dport 593 -j DROP
iptables -A INPUT -p udp --dport 69 -j DROP
####FORWArD链的端口丢弃###
iptables -I FORWARD -p tcp --dport 135 -j DROP
iptables -A FORWARD -p udp --dport 135 -j DROP
iptables -I FORWARD -p tcp --dport 136 -j DROP
iptables -A FORWARD -p udp --dport 136 -j DROP
iptables -I FORWARD -p tcp --dport 137 -j DROP
iptables -A FORWARD -p udp --dport 137 -j DROP
iptables -I FORWARD -p tcp --dport 138 -j DROP
iptables -A FORWARD -p udp --dport 138 -j DROP
iptables -I FORWARD -p tcp --dport 139 -j DROP
iptables -A FORWARD -p udp --dport 139 -j DROP
iptables -I FORWARD -p tcp --dport 445 -j DROP
iptables -A FORWARD -p udp --dport 445 -j DROP
#### 防止非法描路由密码同减轻小量SYN攻击 ###
iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit 1/s -j ACCEPT
iptables -N SYNFLOOD
iptables -A INPUT -p tcp -m state --state NEW -j SYNFLOOD
iptables -A SYNFLOOD -p tcp -j REJECT --reject-with tcp-reset
iptables -I SYNFLOOD -p tcp --syn -m limit --limit 1/s --limit-burst 10 -j RETURN
iptables -I SYNFLOOD -p tcp --dport 21 -s $LANIP -j RETURN
iptables -I SYNFLOOD -p tcp --dport 8989 -j RETURN
iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
####指定IP或段不能访问/网址/端口####
#iptables -I FORWARD -s $LANIP -d 域名 -j DROP
#iptables -I FORWARD -s $LANIP -p tcp --dport 端口 -j DROP
#iptables -I FORWARD -s 192.168.0.0/24 -d 域名 -j DROP
#iptables -I FORWARD -s 192.168.0.0/24 -p tcp --dport 端口 -j DROP
#####没用的包丢弃#####
iptables -I FORWARD -m conntrack --ctstate INVALID -j DROP
iptables -t mangle -A PREROUTING -i $LAN ! -s $LANIP -j DROP
iptables -t mangle -A PREROUTING -p tcp -i $LAN -d $WANIP -j DROP
iptables -t mangle -A PREROUTING -p icmp --icmp-type redirect -j DROP
iptables -t mangle -A FORWARD -s $LANIP -d $LANIP -j DROP
################安全策略全部结束###############
######## 禁止唔听话的收银八婆玩收银机 #######
iptables -N SHOUYING -t nat
iptables -A PREROUTING -t nat -s 192.168.0.248 -j SHOUYING
iptables -A SHOUYING -t nat -p tcp --dport 80 -j DROP
iptables -I SHOUYING -t nat -d 61.145.117.155 -j RETURN
iptables -I SHOUYING -t nat -d 61.145.117.154 -j RETURN
iptables -I SHOUYING -t nat -d 202.96.128.86 -j RETURN
iptables -I SHOUYING -t nat -d 202.96.128.166 -j RETURN
iptables -I SHOUYING -t nat -d 61.152.101.129 -j RETURN
iptables -I SHOUYING -t nat -d 61.152.101.128 -j RETURN
iptables -I SHOUYING -t nat -d 61.152.101.117 -j RETURN
iptables -I SHOUYING -t nat -d 61.152.101.74 -j RETURN
iptables -I SHOUYING -t nat -d 61.152.101.49 -j RETURN
iptables -I SHOUYING -t nat -d 202.105.12.231 -j RETURN
iptables -I SHOUYING -t nat -d 218.18.95.176 -j RETURN
iptables -I SHOUYING -t nat -d 61.152.98.64 -j RETURN
iptables -I SHOUYING -t nat -d $WGIP -j RETURN
iptables -I SHOUYING -t nat -d 219.136.249.226 -j RETURN
iptables -I SHOUYING -t nat -d 218.30.64.194 -j RETURN
iptables -I SHOUYING -t nat -d 125.64.7.176 -j RETURN
iptables -I SHOUYING -t nat -d 125.95.190.220 -j RETURN
iptables -I SHOUYING -t nat -d 119.145.141.75 -j RETURN
iptables -I SHOUYING -t nat -d www.sicent.com -j RETURN
iptables -I SHOUYING -t nat -d 61.152.101.113 -j RETURN
###########保护游戏服务器#########
iptables -N YOUXI -t nat
iptables -A PREROUTING -t nat -s 192.168.0.3 -j YOUXI
iptables -A PREROUTING -t nat -s 192.168.0.2 -j YOUXI
iptables -A YOUXI -t nat -p tcp --dport 80 -j DROP
iptables -I YOUXI -t nat -d www.icafe8.com -j RETURN
iptables -I YOUXI -t nat -d 211.103.157.34 -j RETURN
iptables -I YOUXI -t nat -d 211.103.157.39 -j RETURN
iptables -I YOUXI -t nat -d 211.103.157.38 -j RETURN
iptables -I YOUXI -t nat -d 211.103.157.36 -j RETURN
############ 限速 ###########
iptables -N QOS
iptables -A FORWARD -i eth0 -p ALL -m hashspeed \! --hashspeed-bytes 64K --hashspeed-mode srcip --hashspeed-name slimit -j QOS
iptables -A FORWARD -o eth0 -p ALL -m hashspeed \! --hashspeed-bytes 350K --hashspeed-mode dstip --hashspeed-name dlimit -j QOS
iptables -A QOS -j DROP
#iptables -I QOS -s 192.168.0.2 -j RETURN
iptables -I QOS -d 192.168.0.2 -j RETURN
#iptables -I FORWARD -m ipp2p --xunlei -j DROP
exit 0
加入启动后如果出现错误:
#:cp -r /usr/local/sbin/iptables /sbin/iptables