Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5707940
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: LINUX

2006-09-04 21:57:42


首先感谢CU,感谢白金...感谢所有fans们!
这就是俺 三个月来的百分之六十精华**,全放在下面!

忠告(如果有朋友要做的,请下源码,不要用系统自带的,不管是rh fc cos如果是系统自带的源码,都 通不过,我在学习中曾经有大错误,以前是照着别人的文章 ,然后复制粘贴他们的命令,结果输得好惨!那怕你花多十倍的时间 ,也要用心去理解别人的文章)


插曲:我曾经在照着别人的文章 做到失败时,你们知道我最想做什么吗? ?我最想上去把那个写文章 的人编一顿,我在想,那个"鸟"人是不是把一些特别的参数隐藏了,其实就是我们没有理解)

正题:

下面的第一个引用是转贴(摘錄自旗標「Linux iptables 技術實務 - 防火牆、頻寬管理、連線管制」一書)
引用:使用 l7-filter 來過濾 P2P 或即時通訊軟體

因為 l7-filter 只是 iptables 的子系統, 僅在 iptables 的功能上作延伸, 因此封包過濾的方法和原本的 iptables 並沒有太大的不同。

7-filter 的語法格式

l7-filter 的語法與 iptables 類似, 在使用上差不多, 其格式大致如下:


iptables -t mangle -I POSTROUTING -m layer7 --l7proto http -j DROP
          ─┬──     ───┬────  ──┬──   ───┬───
使用 mangle 表格             │     符合 Layer 7 的封包     │
                   使用 POSTROUTING 鏈            Layer 7 封包的通訊協定
      


其中比較需要注意的只有 --l7proto 參數後面所使用的 Layer 7 封包通訊協定, 它就是我們所要阻擋的封包類型。至於如何知道 該填哪些名稱, 可由我們所安裝的 l7-filter 樣式檔得知。請檢視 /etc/l7-protocols 目錄下的 file_types、  protocols、weakpatterns、extra 及 malware 子目錄下附檔名為 pat 的檔案, 它們就是 l7- filter 的樣式檔。裡面有簡單的說明, 告訴您這個樣式檔可以過濾哪些類型網路軟體的封包, 以及它的過濾規則。而 --l7proto 參數後面 僅需填樣式檔的名稱即可 (不含附檔名)。

以下是筆者所檢視的 msnmessenger.pat 檔, 您只要將 --l7proto http 換成 -- l7proto msnmessenger 即可阻擋 MSN。筆者檢視 /etc/l7- protocols/protocols/msnmessenger.pat 檔的內容如下:


# MSN Messenger - Microsoft Network chat client
                     ↑由說明可知是阻擋 MSN 的樣式檔
# Pattern quality: good  Messenger 
#
# Usually uses port 1863

#
# This pattern has been tested and is believed to work well.  
# If it does not work for you, or you believe it could be      
#    improved, please 
# post to l7-filter-developers@lists.sf.net .  This list may be
# subscribed to at

# developers

msnmessenger
# ver: allow versions up to 99.
# usr (in case ver didn't work):
^(ver [0-9]+ msnp[1-9][0-9]? [\x09-\0d -~]* cvr|usr md5 i [ -~]*)
   ↑符合的封包字串
      


過濾常見的即時通訊軟體

常見的即時通訊軟體有 MSN Messenger、Yahoo Messenger 和 ICQ, 若要擋下這些軟體, 所需要用到的 l7- filter 樣式檔有 /etc/l7-protocols/protocols/ 目錄下的 msnmessenger.pat、 yahoo.pat 與 aim.pat。其中因為 ICQ 已經賣給 AOL (American online), 所以亦叫 AIM  (AOL instant messenger)。

知道要使用哪些 l7-filter 樣式檔之後, iptables 的寫法如下:


[root@free ~]# iptables -t mangle -I POSTROUTING -m layer7
         --l7proto msnmessenger -j DROP
       ↑阻擋 MSN Messenger
[root@free ~]# iptables -t mangle -I POSTROUTING -m layer7
         --l7proto yahoo -j DROP
        ↑阻擋 Yahoo Messenger
[root@free ~]# iptables -t mangle -I POSTROUTING -m layer7
         --l7proto aim -j DROP
              ↑阻擋 ICQ
      


設定好後, 可如下檢視:


[root@free ~]# iptables -t mangle -L POSTROUTING
         ↑檢視 mangle 表格的 POSTROUTING 鏈
Chain POSTROUTING (policy ACCEPT)
target prot opt source    destination
DROP   all  --  anywhere  anywhere LAYER7 l7proto aim         
DROP   all  --  anywhere  anywhere LAYER7 l7proto yahoo       
DROP   all  --  anywhere  anywhere LAYER7 l7proto msnmessenger
              ↑阻擋常見的即時通訊軟體 
      


此後,只要使用者開啟即時通訊軟體時, 就會發現無法連上網路:

過濾常見的 P2P 軟體

常見的 P2P 軟體有 eMule、eDonkey、Kazaa 與 Bittorrent...等, 要擋這些軟體需要用到的 l7- filter 樣式檔有 /etc/l7-protocols/protocols 目錄下的 bittorrent.pat 與  fasttrack.pat 和 /etc/l7-protocols/weakpatterns 目錄下的 edonkey.pat 檔。

其中 bittorrent.pat 可以阻擋所有使用 bittorrent 通訊協定的軟體, 如 ABC、Bitcomet ...等。 fasttrack.pat 可以阻擋 kazaa。edonkey.pat 可以阻擋所有使用 eDonkey 通訊協定的軟體, 如  eDonkey 與 eMule... 等。

設定方式如下:


[root@free ~]# iptables -t mangle -I POSTROUTING -m layer7
    --l7proto bittorrent -j DROP ←阻擋 bittorrent 通訊協定
[root@free ~]# iptables -t mangle -I POSTROUTING -m layer7
       --l7proto fasttrack -j DROP  ←阻擋 kazaa
[root@free ~]# iptables -t mangle -I POSTROUTING -m layer7
       --l7proto edonkey -j DROP    ←阻擋 eDonkey
      


檢視 mangle 表格的 POSTROUTING 鏈:


[root@free ~]# iptables -t mangle -L POSTROUTING
Chain POSTROUTING (policy ACCEPT)
target prot opt source    destination
DROP   all  --  anywhere  anywhere  LAYER7 l7proto edonkey
DROP   all  --  anywhere  anywhere  LAYER7 l7proto fasttrack
DROP   all  --  anywhere  anywhere  LAYER7 l7proto bittorrent 
DROP   all  --  anywhere  anywhere  LAYER7 l7proto aim
DROP   all  --  anywhere  anywhere  LAYER7 l7proto yahoo 
DROP   all  --  anywhere  anywhere  LAYER7 l7proto msnmessenger

      


這樣 P2P 軟體使用時會發生連線錯誤,日後也不能使用了。

l7-filter 還可以過濾很多的封包, 比方說可以限制傳輸 jpg 或 gif 格式的圖檔。當然還有其他更多的功能, 您可以自行閱讀 /etc/l7-protocols/ 目錄下的 l7-filter 樣式檔的說明以取得相關資訊



中间的引用我先秀一下我的机器模块
引用:[root@localhost ~]# lsmod
Module                  Size  Used by
ipt_layer7             17576  6
iptable_mangle          7168  1
ipt_ipp2p              12672  3
ipt_REJECT             10240  1
ipt_LOG                11648  1
ipt_limit               6784  3
ipt_connlimit           7680  3
ipt_multiport           6912  3
ipt_state               6144  3
ip_nat_ftp              7680  0
ip_conntrack_ftp       77200  1 ip_nat_ftp
ipt_MASQUERADE          7936  1
iptable_nat            27700  3 ip_nat_ftp,ipt_MASQUERADE
ip_conntrack           49032  6 ipt_connlimit,ipt_state,ip_nat_ftp,ip_conntrack_ftp,ipt_MASQUERADE,iptable_nat
arc4                    6272  0
ppp_mppe_mppc          19972  0
ppp_synctty            15488  0
ppp_async              16896  1
crc_ccitt               6400  1 ppp_async
ppp_generic            35476  7 ppp_mppe_mppc,ppp_synctty,ppp_async
slhc                   11264  1 ppp_generic
iptable_filter          7296  1
ip_tables              26368  12 ipt_layer7,iptable_mangle,ipt_ipp2p,ipt_REJECT,ipt_LOG,ipt_limit,ipt_connlimit,ipt_multiport,ipt_state,ipt_MASQUERADE,iptable_nat,iptable_filter
vfat                   18176  1
fat                    57244  1 vfat
dm_mod                 64156  0
video                  19972  0
button                 10896  0
battery                13572  0
ac                      9092  0
uhci_hcd               38928  0
ehci_hcd               39688  0
hw_random               9748  0
i2c_i801               13068  0
i2c_core               25728  1 i2c_i801
snd_intel8x0           37184  0
snd_ac97_codec         84988  1 snd_intel8x0
snd_pcm_oss            57504  0
snd_mixer_oss          23168  1 snd_pcm_oss
snd_pcm                97924  3 snd_intel8x0,snd_ac97_codec,snd_pcm_oss
snd_timer              30212  1 snd_pcm
snd                    61924  6 snd_intel8x0,snd_ac97_codec,snd_pcm_oss,snd_mixer_oss,snd_pcm,snd_timer
soundcore              14048  1 snd
snd_page_alloc         15240  2 snd_intel8x0,snd_pcm
sk98lin               176096  0
8139too                32640  0
mii                     9984  1 8139too
floppy                 64212  0
ext3                  139656  2
jbd                    65048  1 ext3
scsi_mod               97508  0
[root@localhost ~]#



再下来我贴一下我现在的iptables脚本:
引用:
[color=blue]#!/bin/sh
#
modprobe ppp_mppe_mppc
modprobe ipt_MASQUERADE
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
iptables -F
iptables -t nat -F
iptables -X
iptables -t nat -X
iptables -t mangle -F
###########################INPUT键###################################
iptables -P INPUT DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp -m multiport --dports 110,80,25 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 139 -j ACCEPT
#允许内网samba,smtp,pop3,连接
iptables -A INPUT -i eth1 -p udp -m multiport --dports 53 -j ACCEPT
#允许dns连接
iptables -A INPUT -p tcp --dport 1723 -j ACCEPT
iptables -A INPUT -p gre -j ACCEPT
#允许外网vpn连接
iptables -A INPUT -s 192.186.0.0/24 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp --syn -m connlimit --connlimit-above 15 -j DROP
#为了防止DOS太多连接进来,那么可以允许最多15个初始连接,超过的丢弃
iptables -A INPUT -s 192.186.0.0/24 -p tcp --syn -m connlimit --connlimit-above 15 -j DROP
#为了防止DOS太多连接进来,那么可以允许最多15个初始连接,超过的丢弃
iptables -A INPUT -p icmp -m limit --limit 3/s -j LOG --log-level INFO --log-prefix "ICMP packet IN: "
iptables -A INPUT -p icmp -m limit --limit 6/m -j ACCEPT
iptables -A INPUT -p icmp -j DROP
#设置icmp阔值 ,并对攻击者记录在案
iptables -t nat -A POSTROUTING -o ppp0 -s 192.168.0.0/24 -j MASQUERADE
#内网转发
iptables -t mangle -I POSTROUTING -m layer7 --l7proto bittorrent -j DROP
iptables -t mangle -I POSTROUTING -m layer7 --l7proto fasttrack -j DROP
iptables -t mangle -I POSTROUTING -m layer7 --l7proto edonkey -j DROP
iptables -t mangle -I POSTROUTING -m layer7 --l7proto skypeout -j DROP
iptables -t mangle -I POSTROUTING -m layer7 --l7proto skypetoskype -j DROP
#上面几句你用心看第一个转贴你会看得懂,我希望有大牛可以在这里玩出更多的花#样,比如可以禁止某种格式的档案通过,比如可以像string一样,比如你开发的..比如你#想像的..比如
iptables -N syn-flood
iptables -A INPUT -p tcp --syn -j syn-flood
iptables -I syn-flood -p tcp -m limit --limit 3/s --limit-burst 6 -j RETURN
iptables -A syn-flood -j REJECT
#防止SYN攻击 轻量
#######################FORWARD链###########################
iptables -P FORWARD DROP
iptables -A FORWARD -m layer7 --l7proto qq  -m time --timestart 8:15 --timestop 12:30 --days Mon,Tue,Wed,Thu,Fri,Sat  -j DROP
iptables -A FORWARD -m layer7 --l7proto qq  -m time --timestart 13:00 --timestop 20:30 --days Mon,Tue,Wed,Thu,Fri,Sat  -j DROP
#按时间放行qq
iptables -A FORWARD -p tcp -s 192.168.0.0/24 -m multiport --dports 80,110,21,25,1723 -j ACCEPT
iptables -A FORWARD -p udp -s 192.168.0.0/24 --dport 53 -j ACCEPT
iptables -A FORWARD -p gre -s 192.168.0.0/24 -j ACCEPT
iptables -A FORWARD -p icmp -s 192.168.0.0/24 -j ACCEPT
#允许 vpn客户走vpn网络连接外网
iptables -I FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m ipp2p --edk --kazaa --bit -j DROP
iptables -A FORWARD -p tcp -m ipp2p --ares -j DROP
iptables -A FORWARD -p udp -m ipp2p --kazaa -j DROP
#禁止BT连接(有点重复,因为上面的layer7已经可以很好的禁止了现在不管什么bt都 #完蛋了
iptables -A FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 15 --connlimit-mask 24 -j DROP
#只允许每组ip同时15个80端口转发
#######################################################################
sysctl -w net.ipv4.ip_forward=1 &>;/dev/null
#打开转发
#######################################################################
sysctl -w net.ipv4.tcp_syncookies=1 &>;/dev/null
#打开 syncookie (轻量级预防 DOS 攻击)
sysctl -w net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=3800 &>;/dev/null
#设置默认 TCP 连接痴呆时长为 3800 秒(此选项可以大大降低连接数)
sysctl -w net.ipv4.ip_conntrack_max=300000 &>;/dev/null
#设置支持最大连接树为 30W(这个根据你的内存和 iptables 版本来,每个 connection 需要 300 多个字节)
#######################################################################
iptables -I INPUT -s 192.168.0.50 -j ACCEPT
iptables -I FORWARD -s 192.168.0.50 -j ACCEPT
#192.168.0.50是我的机子,全部放行!
############################完#########################################
###########################枫影-乡下猫#################################[/color]



系统是cos4.1
引用:[root@localhost etc]# uname -a
Linux localhost.localdomain 2.6.13 #1 SMP Mon Sep 26 18:17:45 CST 2005 i686 i686 i386 GNU/Linux
[root@localhost etc]#
阅读(1224) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~