LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3579|回复: 24

请问如何禁止BT下载

[复制链接]
发表于 2004-5-18 11:38:26 | 显示全部楼层 |阅读模式
公司用的是1Madsl拨号,代理服务器是redhat9,用iptables+squid做的透明代理,最近公司局域网内有人利用BT下载,搞的网速奇慢无比,内存占用率经常是60%,cpu占用率99.9%都是家常便饭,请问怎么样才能让他们无法用bt下载。谢谢。
发表于 2004-5-18 18:42:15 | 显示全部楼层
bt可以自定义端口吧, ?
发表于 2004-5-20 11:22:56 | 显示全部楼层
用squid控制不让其下载.torrent不就成了。
 楼主| 发表于 2004-5-20 13:12:12 | 显示全部楼层
禁止不让下载.torrent,但是bt的种子文件也可以通过别的途径传播呀,我看有人说先关掉所有端口,然后在开放需要的端口,这种途径可以,请问谁能告诉我用iptables怎么写这段代码?谢谢
发表于 2004-5-20 13:29:55 | 显示全部楼层
那要看你公司的这台服务器要提供什么服务了。如果不提供服务的话,把所有端口连进来的方向(tcp/udp)都封闭掉,只允许出,这样应该也可以。
发表于 2004-5-20 13:48:14 | 显示全部楼层
老生长谈了
反关端口啊
这不更简单啊
 楼主| 发表于 2004-5-20 14:14:33 | 显示全部楼层
所谓会者不难,难者不会,我就是,我对iptables不是很熟悉,还望哪位大哥能告诉我怎么写这段代码。谢谢。
发表于 2004-5-20 15:02:09 | 显示全部楼层
找到一个很好的脚本,你可以略加修改,运行之:

http://www.linux-firewall-tools. ... andalone.firewall.1


  1. #!/bin/bash

  2. modprobe ip_conntrack_ftp

  3. CONNECTION_TRACKING="1"
  4. ACCEPT_AUTH="0"
  5. SSH_SERVER="0"
  6. FTP_SERVER="0"
  7. WEB_SERVER="0"
  8. SSL_SERVER="0"
  9. DHCP_CLIENT="1"

  10. INTERNET="eth0"                      # Internet-connected interface
  11. LOOPBACK_INTERFACE="lo"              # however your system names it
  12. IPADDR="my.ip.address"               # your IP address
  13. SUBNET_BASE="network.address"        # ISP network segment base address
  14. SUBNET_BROADCAST="directed.broadcast" # network segment broadcast address
  15. MY_ISP="my.isp.address.range"        # ISP server & NOC address range

  16. NAMESERVER="isp.name.server.1"       # address of a remote name server
  17. POP_SERVER="isp.pop.server"          # address of a remote pop server
  18. MAIL_SERVER="isp.mail.server"        # address of a remote mail gateway
  19. NEWS_SERVER="isp.news.server"        # address of a remote news server
  20. TIME_SERVER="some.timne.server"      # address of a remote time server
  21. DHCP_SERVER="isp.dhcp.server"        # address of your ISP dhcp server

  22. LOOPBACK="127.0.0.0/8"               # reserved loopback address range
  23. CLASS_A="10.0.0.0/8"                 # class A private networks
  24. CLASS_B="172.16.0.0/12"              # class B private networks
  25. CLASS_C="192.168.0.0/16"             # class C private networks
  26. CLASS_D_MULTICAST="224.0.0.0/4"      # class D multicast addresses
  27. CLASS_E_RESERVED_NET="240.0.0.0/5"   # class E reserved addresses
  28. BROADCAST_SRC="0.0.0.0"              # broadcast source address
  29. BROADCAST_DEST="255.255.255.255"     # broadcast destination address

  30. PRIVPORTS="0:1023"                   # well-known, privileged port range
  31. UNPRIVPORTS="1024:65535"             # unprivileged port range

  32. SSH_PORTS="1024:65535"

  33. NFS_PORT="2049"
  34. LOCKD_PORT="4045"
  35. SOCKS_PORT="1080"
  36. OPENWINDOWS_PORT="2000"
  37. XWINDOW_PORTS="6000:6063"
  38. SQUID_PORT="3128"

  39. ###############################################################

  40. # Enable broadcast echo Protection
  41. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

  42. # Disable Source Routed Packets
  43. for f in /proc/sys/net/ipv4/conf/*/accept_source_route; do
  44.     echo 0 > $f
  45. done

  46. # Enable TCP SYN Cookie Protection
  47. echo 1 > /proc/sys/net/ipv4/tcp_syncookies

  48. # Disable ICMP Redirect Acceptance
  49. for f in /proc/sys/net/ipv4/conf/*/accept_redirects; do
  50.     echo 0 > $f
  51. done

  52. # Don¹t send Redirect Messages
  53. for f in /proc/sys/net/ipv4/conf/*/send_redirects; do
  54.     echo 0 > $f
  55. done

  56. # Drop Spoofed Packets coming in on an interface, which if replied to,
  57. # would result in the reply going out a different interface.
  58. for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
  59.     echo 1 > $f
  60. done

  61. # Log packets with impossible addresses.
  62. for f in /proc/sys/net/ipv4/conf/*/log_martians; do
  63.     echo 1 > $f
  64. done

  65. ###############################################################

  66. # Remove any existing rules from all chains
  67. iptables --flush
  68. iptables -t nat --flush
  69. iptables -t mangle --flush

  70. # Unlimited traffic on the loopback interface
  71. iptables -A INPUT  -i lo -j ACCEPT
  72. iptables -A OUTPUT -o lo -j ACCEPT

  73. # Set the default policy to drop
  74. iptables --policy INPUT   DROP
  75. iptables --policy OUTPUT  DROP
  76. iptables --policy FORWARD DROP

  77. # A bug that showed up as of the Red Hat 7.2 release results
  78. # in the following 5 default policies breaking the firewall
  79. # initialization:

  80. # iptables -t nat --policy PREROUTING  DROP
  81. # iptables -t nat --policy OUTPUT DROP
  82. # iptables -t nat --policy POSTROUTING DROP

  83. # iptables -t mangle --policy PREROUTING DROP
  84. # iptables -t mangle --policy OUTPUT DROP

  85. # Remove any pre-existing user-defined chains
  86. iptables --delete-chain
  87. iptables -t nat --delete-chain
  88. iptables -t mangle --delete-chain

  89. ###############################################################
  90. # Stealth Scans and TCP State Flags

  91. # All of the bits are cleared
  92. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP

  93. # SYN and FIN are both set
  94. iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

  95. # SYN and RST are both set
  96. iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP

  97. # FIN and RST are both set
  98. iptables -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP

  99. # FIN is the only bit set, without the expected accompanying ACK
  100. iptables -A INPUT -p tcp --tcp-flags ACK,FIN FIN -j DROP

  101. # PSH is the only bit set, without the expected accompanying ACK
  102. iptables -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP

  103. # URG is the only bit set, without the expected accompanying ACK
  104. iptables -A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP

  105. ###############################################################
  106. # Using Connection State to By-pass Rule Checking

  107. if [ "$CONNECTION_TRACKING" = "1" ]; then
  108.     iptables -A INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT
  109.     iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

  110.     # Using the state module alone, INVALID will break protocols that use
  111.     # bi-directional connections or multiple connections or exchanges,
  112.     # unless an ALG is provided for the protocol. At this time, FTP and is
  113.     # IRC are the only protocols with ALG support.

  114.     iptables -A INPUT -m state --state INVALID -j LOG \
  115.              --log-prefix "INVALID input: "
  116.     iptables -A INPUT -m state --state INVALID -j DROP

  117.     iptables -A OUTPUT -m state --state INVALID -j LOG \
  118.              --log-prefix "INVALID ouput: "
  119.     iptables -A OUTPUT -m state --state INVALID -j DROP
  120. fi

  121. ###############################################################
  122. # Source Address Spoofing and Other Bad Addresses

  123. # Refuse spoofed packets pretending to be from
  124. # the external interface's IP address
  125. iptables -A INPUT  -i $INTERNET -s $IPADDR -j DROP

  126. # Refuse packets claiming to be from a Class A private network
  127. iptables -A INPUT  -i $INTERNET -s $CLASS_A -j DROP

  128. # Refuse packets claiming to be from a Class B private network
  129. iptables -A INPUT  -i $INTERNET -s $CLASS_B -j DROP

  130. # Refuse packets claiming to be from a Class C private network
  131. iptables -A INPUT  -i $INTERNET -s $CLASS_C -j DROP

  132. # Refuse packets claiming to be from the loopback interface
  133. iptables -A INPUT  -i $INTERNET -s $LOOPBACK -j DROP

  134. # Refuse malformed broadcast packets
  135. iptables -A INPUT  -i $INTERNET -s $BROADCAST_DEST -j LOG
  136. iptables -A INPUT  -i $INTERNET -s $BROADCAST_DEST -j DROP

  137. iptables -A INPUT  -i $INTERNET -d $BROADCAST_SRC  -j LOG
  138. iptables -A INPUT  -i $INTERNET -d $BROADCAST_SRC  -j DROP  

  139. if [ "$DHCP_CLIENT" = "0" ]; then
  140.     # Refuse directed broadcasts
  141.     # Used to map networks and in Denial of Service attacks
  142.     iptables -A INPUT -i $INTERNET -d $SUBNET_BASE -j DROP
  143.     iptables -A INPUT -i $INTERNET -d $SUBNET_BROADCAST -j DROP

  144.     # Refuse limited broadcasts
  145.     iptables -A INPUT -i $INTERNET -d $BROADCAST_DEST -j DROP
  146. fi

  147. # Refuse Class D multicast addresses
  148. # illegal as a source address
  149. iptables -A INPUT -i $INTERNET -s $CLASS_D_MULTICAST -j DROP

  150. iptables -A INPUT -i $INTERNET -p ! udp -d $CLASS_D_MULTICAST -j DROP

  151. iptables -A INPUT  -i $INTERNET -p udp -d $CLASS_D_MULTICAST -j ACCEPT

  152. # Refuse Class E reserved IP addresses
  153. iptables -A INPUT  -i $INTERNET -s $CLASS_E_RESERVED_NET -j DROP

  154. # refuse addresses defined as reserved by the IANA
  155. # 0.*.*.*          - Can¹t be blocked unilaterally with DHCP
  156. # 169.254.0.0/16   - Link Local Networks
  157. # 192.0.2.0/24     - TEST-NET

  158. if [ "$DHCP_CLIENT" = "1" ]; then
  159.     iptables -A INPUT  -i $INTERNET -p udp \
  160.              -s $BROADCAST_SRC --sport 67 \
  161.              -d $BROADCAST_DEST --dport 68 -j ACCEPT
  162. fi

  163. iptables -A INPUT -i $INTERNET -s 0.0.0.0/8 -j DROP
  164. iptables -A INPUT -i $INTERNET -s 169.254.0.0/16 -j DROP
  165. iptables -A INPUT -i $INTERNET -s 192.0.2.0/24 -j DROP

  166. ###############################################################
  167. # Disallowing Connections to Common TCP Unprivileged Server Ports

  168. # X Window connection establishment
  169. iptables -A OUTPUT -o $INTERNET -p tcp --syn \
  170.          --destination-port $XWINDOW_PORTS -j REJECT

  171. # X Window: incoming connection attempt
  172. iptables -A INPUT -i $INTERNET -p tcp --syn \
  173.          --destination-port $XWINDOW_PORTS -j DROP

  174. # Establishing a connection over TCP to NFS, OpenWindows, SOCKS or squid
  175. iptables -A OUTPUT -o $INTERNET -p tcp \
  176.          -m multiport --destination-port \
  177.          $NFS_PORT,$OPENWINDOWS_PORT,$SOCKS_PORT,$SQUID_PORT \
  178.          --syn -j REJECT

  179. iptables -A INPUT -i $INTERNET -p tcp \
  180.          -m multiport --destination-port \
  181.          $NFS_PORT,$OPENWINDOWS_PORT,$SOCKS_PORT,$SQUID_PORT \
  182.          --syn -j DROP

  183. ###############################################################
  184. # Disallowing Connections to Common UDP Unprivileged Server Ports

  185. # NFS and lockd
  186. if [ "$CONNECTION_TRACKING" = "1" ]; then
  187.     iptables -A OUTPUT -o $INTERNET -p udp \
  188.              -m multiport --destination-port $NFS_PORT,$LOCKD_PORT \
  189.              -m state --state NEW -j REJECT

  190.     iptables -A INPUT -i $INTERNET -p udp \
  191.              -m multiport --destination-port $NFS_PORT,$LOCKD_PORT \
  192.              -m state --state NEW -j DROP
  193. else
  194.     iptables -A OUTPUT -o $INTERNET -p udp \
  195.              -m multiport --destination-port $NFS_PORT,$LOCKD_PORT \
  196.              -j REJECT

  197.     iptables -A INPUT -i $INTERNET -p udp \
  198.              -m multiport --destination-port $NFS_PORT,$LOCKD_PORT \
  199.              -j DROP
  200. fi

  201. ###############################################################
  202. # DNS Name Server

  203. # DNS Fowarding Name Server or client requests

  204. if [ "$CONNECTION_TRACKING" = "1" ]; then
  205.     iptables -A OUTPUT -o $INTERNET -p udp \
  206.              -s $IPADDR --sport $UNPRIVPORTS \
  207.              -d $NAMESERVER --dport 53 \
  208.              -m state --state NEW -j ACCEPT
  209. fi

  210. iptables -A OUTPUT -o $INTERNET -p udp \
  211.          -s $IPADDR --sport $UNPRIVPORTS \
  212.          -d $NAMESERVER --dport 53 -j ACCEPT

  213. iptables -A INPUT  -i $INTERNET -p udp \
  214.          -s $NAMESERVER --sport 53 \
  215.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  216. #...............................................................
  217. # TCP is used for large responses

  218. if [ "$CONNECTION_TRACKING" = "1" ]; then
  219.     iptables -A OUTPUT -o $INTERNET -p tcp \
  220.              -s $IPADDR --sport $UNPRIVPORTS \
  221.              -d $NAMESERVER --dport 53 \
  222.              -m state --state NEW -j ACCEPT
  223. fi

  224. iptables -A OUTPUT -o $INTERNET -p tcp \
  225.          -s $IPADDR --sport $UNPRIVPORTS \
  226.          -d $NAMESERVER --dport 53 -j ACCEPT

  227. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  228.          -s $NAMESERVER --sport 53 \
  229.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  230. #...............................................................
  231. # DNS Caching Name Server (local server to primary server)

  232. if [ "$CONNECTION_TRACKING" = "1" ]; then
  233.     iptables -A OUTPUT -o $INTERNET -p udp \
  234.              -s $IPADDR --sport 53 \
  235.              -d $NAMESERVER --dport 53 \
  236.              -m state --state NEW -j ACCEPT
  237. fi

  238. iptables -A OUTPUT -o $INTERNET -p udp \
  239.          -s $IPADDR --sport 53 \
  240.          -d $NAMESERVER --dport 53 -j ACCEPT

  241. iptables -A INPUT  -i $INTERNET -p udp \
  242.          -s $NAMESERVER --sport 53 \
  243.          -d $IPADDR --dport 53 -j ACCEPT

  244. ###############################################################
  245. # Filtering the AUTH User Identification Service (TCP Port 113)

  246. # Outgoing Local Client Requests to Remote Servers

  247. if [ "$CONNECTION_TRACKING" = "1" ]; then
  248.     iptables -A OUTPUT -o $INTERNET -p tcp \
  249.              -s $IPADDR --sport $UNPRIVPORTS \
  250.              --dport 113 -m state --state NEW -j ACCEPT
  251. fi

  252. iptables -A OUTPUT -o $INTERNET -p tcp \
  253.          -s $IPADDR --sport $UNPRIVPORTS \
  254.          --dport 113 -j ACCEPT

  255. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  256.          --sport 113 \
  257.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  258. #...............................................................
  259. # Incoming Remote Client Requests to Local Servers

  260. if [ "$CONNECTION_TRACKING" = "1" ]; then
  261.     iptables -A INPUT  -i $INTERNET -p tcp \
  262.              --sport $UNPRIVPORTS \
  263.              -d $IPADDR --dport 113 \
  264.              -m state --state NEW -j ACCEPT
  265. fi

  266. if [ "$ACCEPT_AUTH" = "1" ]; then
  267.     if [ "$CONNECTION_TRACKING" = "1" ]; then
  268.         iptables -A INPUT  -i $INTERNET -p tcp \
  269.                  --sport $UNPRIVPORTS \
  270.                  -d $IPADDR --dport 113 \
  271.                  -m state --state NEW -j ACCEPT
  272.     fi

  273.     iptables -A INPUT  -i $INTERNET -p tcp \
  274.              --sport $UNPRIVPORTS \
  275.              -d $IPADDR --dport 113 -j ACCEPT

  276.     iptables -A OUTPUT -o $INTERNET -p tcp ! --syn \
  277.              -s $IPADDR --sport 113 \
  278.              --dport $UNPRIVPORTS -j ACCEPT
  279. else
  280.     iptables -A INPUT -i $INTERNET -p tcp \
  281.              --sport $UNPRIVPORTS \
  282.              -d $IPADDR --dport 113 -j REJECT --reject-with tcp-reset
  283. fi

  284. ###############################################################
  285. # Sending Mail to Any External Mail Server
  286. # Use "-d $MAIL_SERVER" if an ISP mail gateway is used instead

  287. if [ "$CONNECTION_TRACKING" = "1" ]; then
  288.     iptables -A OUTPUT -o $INTERNET -p tcp \
  289.              -s $IPADDR --sport $UNPRIVPORTS \
  290.              --dport 25 -m state --state NEW -j ACCEPT
  291. fi

  292. iptables -A OUTPUT -o $INTERNET -p tcp \
  293.          -s $IPADDR --sport $UNPRIVPORTS \
  294.          --dport 25 -j ACCEPT

  295. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  296.          --sport 25 \
  297.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  298. ###############################################################
  299. # Retrieving Mail as a POP Client (TCP Port 110)

  300. if [ "$CONNECTION_TRACKING" = "1" ]; then
  301.     iptables -A OUTPUT -o $INTERNET -p tcp \
  302.              -s $IPADDR --sport $UNPRIVPORTS \
  303.              -d $POP_SERVER --dport 110 -m state --state NEW -j ACCEPT
  304. fi

  305. iptables -A OUTPUT -o $INTERNET -p tcp \
  306.          -s $IPADDR --sport $UNPRIVPORTS \
  307.          -d $POP_SERVER --dport 110 -j ACCEPT

  308. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  309.          -s $POP_SERVER --sport 110 \
  310.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  311. ###############################################################
  312. # Accessing Usenet News Services (TCP NNTP Port 119)

  313. if [ "$CONNECTION_TRACKING" = "1" ]; then
  314.     iptables -A OUTPUT -o $INTERNET -p tcp \
  315.              -s $IPADDR --sport $UNPRIVPORTS \
  316.              -d $NEWS_SERVER --dport 119 -m state --state NEW -j ACCEPT
  317. fi

  318. iptables -A OUTPUT -o $INTERNET -p tcp \
  319.          -s $IPADDR --sport $UNPRIVPORTS \
  320.          -d $NEWS_SERVER --dport 119 -j ACCEPT

  321. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  322.          -s $NEWS_SERVER --sport 119 \
  323.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  324. ###############################################################
  325. # ssh (TCP Port 22)

  326. # Outgoing Local  Client Requests to Remote Servers

  327. if [ "$CONNECTION_TRACKING" = "1" ]; then
  328.     iptables -A OUTPUT -o $INTERNET -p tcp \
  329.              -s $IPADDR --sport $SSH_PORTS \
  330.              --dport 22 -m state --state NEW -j ACCEPT
  331. fi

  332. iptables -A OUTPUT -o $INTERNET -p tcp \
  333.          -s $IPADDR --sport $SSH_PORTS \
  334.          --dport 22 -j ACCEPT

  335. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  336.          --source-port 22 \
  337.          -d $IPADDR --dport $SSH_PORTS -j ACCEPT

  338. #...............................................................
  339. # Incoming Remote Client Requests to Local Servers

  340. if [ "$SSH_SERVER" = "1" ]; then
  341.     if [ "$CONNECTION_TRACKING" = "1" ]; then
  342.         iptables -A INPUT  -i $INTERNET -p tcp \
  343.                  --sport $SSH_PORTS \
  344.                  -d $IPADDR --dport 22 \
  345.                  -m state --state NEW -j ACCEPT
  346.     fi

  347.     iptables -A INPUT  -i $INTERNET -p tcp \
  348.              --sport $SSH_PORTS \
  349.              -d $IPADDR --dport 22 -j ACCEPT

  350.     iptables -A OUTPUT -o $INTERNET -p tcp ! --syn \
  351.              -s $IPADDR --sport 22 \
  352.              --dport $SSH_PORTS -j ACCEPT
  353. fi

  354. ###############################################################
  355. # ftp (TCP Ports 21, 20)

  356. # Outgoing Local Client Requests to Remote Servers

  357. # Outgoing Control Connection to Port 21
  358. if [ "$CONNECTION_TRACKING" = "1" ]; then
  359.     iptables -A OUTPUT -o $INTERNET -p tcp \
  360.              -s $IPADDR --sport $UNPRIVPORTS \
  361.              --dport 21 -m state --state NEW -j ACCEPT
  362. fi

  363. iptables -A OUTPUT -o $INTERNET -p tcp \
  364.          -s $IPADDR --sport $UNPRIVPORTS \
  365.          --dport 21 -j ACCEPT

  366. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  367.          --sport 21 \
  368.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  369. # Incoming Port Mode Data Channel Connection from Port 20
  370. if [ "$CONNECTION_TRACKING" = "1" ]; then
  371.     # This rule is not necessary if the ip_conntrack_ftp
  372.     # module is used.
  373.     iptables -A INPUT  -i $INTERNET -p tcp \
  374.              --sport 20 \
  375.              -d $IPADDR --dport $UNPRIVPORTS \
  376.              -m state --state NEW -j ACCEPT
  377. fi

  378. iptables -A INPUT  -i $INTERNET -p tcp \
  379.          --sport 20 \
  380.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  381. iptables -A OUTPUT -o $INTERNET -p tcp ! --syn \
  382.          -s $IPADDR --sport $UNPRIVPORTS \
  383.          --dport 20 -j ACCEPT

  384. # Outgoing Passive Mode Data Channel Connection Between Unprivileveg Ports
  385. if [ "$CONNECTION_TRACKING" = "1" ]; then
  386.     # This rule is not necessary if the ip_conntrack_ftp
  387.     # module is used.
  388.     iptables -A OUTPUT -o $INTERNET -p tcp \
  389.              -s $IPADDR --sport $UNPRIVPORTS \
  390.              --dport $UNPRIVPORTS -m state --state NEW -j ACCEPT
  391. fi

  392. iptables -A OUTPUT -o $INTERNET -p tcp \
  393.          -s $IPADDR --sport $UNPRIVPORTS \
  394.          --dport $UNPRIVPORTS -j ACCEPT

  395. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  396.          --sport $UNPRIVPORTS \
  397.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  398. #...............................................................
  399. # Incoming Remote Client Requests to Local Servers

  400. if [ "$FTP_SERVER" = "1" ]; then

  401.     # Incoming Control Connection to Port 21
  402.     if [ "$CONNECTION_TRACKING" = "1" ]; then
  403.         iptables -A INPUT  -i $INTERNET -p tcp \
  404.                  --sport $UNPRIVPORTS \
  405.                  -d $IPADDR --dport 21 \
  406.                  -m state --state NEW -j ACCEPT
  407.     fi

  408.     iptables -A INPUT  -i $INTERNET -p tcp \
  409.              --sport $UNPRIVPORTS \
  410.              -d $IPADDR --dport 21 -j ACCEPT

  411.     iptables -A OUTPUT -o $INTERNET -p tcp ! --syn \
  412.              -s $IPADDR --sport 21 \
  413.              --dport $UNPRIVPORTS -j ACCEPT

  414.     # Outgoing Port Mode Data Channel Connection to Port 20
  415.     if [ "$CONNECTION_TRACKING" = "1" ]; then
  416.         iptables -A OUTPUT -o $INTERNET -p tcp \
  417.                  -s $IPADDR --sport 20\
  418.                  --dport $UNPRIVPORTS -m state --state NEW -j ACCEPT
  419.     fi

  420.     iptables -A OUTPUT -o $INTERNET -p tcp \
  421.              -s $IPADDR --sport 20 \
  422.              --dport $UNPRIVPORTS -j ACCEPT

  423.     iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  424.              --sport $UNPRIVPORTS \
  425.              -d $IPADDR --dport 20 -j ACCEPT

  426.     # Incoming Passive Mode Data Channel Connection Between Unprivileved Ports
  427.     if [ "$CONNECTION_TRACKING" = "1" ]; then
  428.         iptables -A INPUT  -i $INTERNET -p tcp \
  429.                  --sport $UNPRIVPORTS \
  430.                  -d $IPADDR --dport $UNPRIVPORTS \
  431.                  -m state --state NEW -j ACCEPT
  432.     fi

  433.     iptables -A INPUT  -i $INTERNET -p tcp \
  434.              --sport $UNPRIVPORTS \
  435.              -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  436.     iptables -A OUTPUT -o $INTERNET -p tcp ! --syn \
  437.              -s $IPADDR --sport $UNPRIVPORTS \
  438.              --dport $UNPRIVPORTS -j ACCEPT
  439. fi
  440. ###############################################################
  441. # HTTP Web Traffic (TCP Port 80)

  442. # Outgoing Local Client Requests to Remote Servers

  443. if [ "$CONNECTION_TRACKING" = "1" ]; then
  444.     iptables -A OUTPUT -o $INTERNET -p tcp \
  445.              -s $IPADDR --sport $UNPRIVPORTS \
  446.              --dport 80 -m state --state NEW -j ACCEPT
  447. fi

  448. iptables -A OUTPUT -o $INTERNET -p tcp \
  449.          -s $IPADDR --sport $UNPRIVPORTS \
  450.          --dport 80 -j ACCEPT

  451. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  452.          --sport 80 \
  453.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  454. #...............................................................
  455. # Incoming Remote Client Requests to Local Servers

  456. if [ "$WEB_SERVER" = "1" ]; then
  457.     if [ "$CONNECTION_TRACKING" = "1" ]; then
  458.         iptables -A INPUT  -i $INTERNET -p tcp \
  459.                  --sport $UNPRIVPORTS \
  460.                  -d $IPADDR --dport 80 \
  461.                  -m state --state NEW -j ACCEPT
  462.     fi

  463.     iptables -A INPUT  -i $INTERNET -p tcp \
  464.              --sport $UNPRIVPORTS \
  465.              -d $IPADDR --dport 80 -j ACCEPT

  466.     iptables -A OUTPUT -o $INTERNET -p tcp ! --syn \
  467.              -s $IPADDR --sport 80 \
  468.              --dport $UNPRIVPORTS -j ACCEPT
  469. fi

  470. ###############################################################
  471. # SSL Web Traffic (TCP Port 443)

  472. # Outgoing Local  Client Requests to Remote Servers

  473. if [ "$CONNECTION_TRACKING" = "1" ]; then
  474.     iptables -A OUTPUT -o $INTERNET -p tcp \
  475.              -s $IPADDR --sport $UNPRIVPORTS \
  476.              --dport 443 -m state --state NEW -j ACCEPT
  477. fi

  478. iptables -A OUTPUT -o $INTERNET -p tcp \
  479.          -s $IPADDR --sport $UNPRIVPORTS \
  480.          --dport 443 -j ACCEPT

  481. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  482.          --sport 443 \
  483.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  484. #...............................................................
  485. # Incoming Remote Client Requests to Local Servers

  486. if [ "$SSL_SERVER" = "1" ]; then
  487.     if [ "$CONNECTION_TRACKING" = "1" ]; then
  488.         iptables -A INPUT  -i $INTERNET -p tcp \
  489.                  --sport $UNPRIVPORTS \
  490.                  -d $IPADDR --dport 443 \
  491.                  -m state --state NEW -j ACCEPT
  492.     fi

  493.     iptables -A INPUT  -i $INTERNET -p tcp \
  494.              --sport $UNPRIVPORTS \
  495.              -d $IPADDR --dport 443 -j ACCEPT

  496.     iptables -A OUTPUT -o $INTERNET -p tcp ! --syn \
  497.              -s $IPADDR --sport 443 \
  498.              --dport $UNPRIVPORTS -j ACCEPT
  499. fi

  500. ###############################################################
  501. # whois (TCP Port 43)

  502. # Outgoing Local  Client Requests to Remote Servers

  503. if [ "$CONNECTION_TRACKING" = "1" ]; then
  504.     iptables -A OUTPUT -o $INTERNET -p tcp \
  505.              -s $IPADDR --sport $UNPRIVPORTS \
  506.              --dport 43 -m state --state NEW -j ACCEPT
  507. fi

  508. iptables -A OUTPUT -o $INTERNET -p tcp \
  509.          -s $IPADDR --sport $UNPRIVPORTS \
  510.          --dport 43 -j ACCEPT

  511. iptables -A INPUT -i $INTERNET -p tcp ! --syn \
  512.          --sport 43 \
  513.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  514. ###############################################################
  515. # Accessing Remote Network Time Servers (UDP 123)
  516. # Note: some client and servers use source port 123
  517. # when querying a remote server on destination port 123.

  518. if [ "$CONNECTION_TRACKING" = "1" ]; then
  519.     iptables -A OUTPUT -o $INTERNET -p udp \
  520.              -s $IPADDR --sport $UNPRIVPORTS \
  521.              -d $TIME_SERVER --dport 123 \
  522.              -m state --state NEW -j ACCEPT
  523. fi

  524. iptables -A OUTPUT -o $INTERNET -p udp \
  525.          -s $IPADDR --sport $UNPRIVPORTS \
  526.          -d $TIME_SERVER --dport 123 -j ACCEPT

  527. iptables -A INPUT  -i $INTERNET -p udp \
  528.          -s $TIME_SERVER --sport 123 \
  529.          -d $IPADDR --dport $UNPRIVPORTS -j ACCEPT

  530. ###############################################################
  531. # Accessing Your ISP's DHCP Server (UDP Ports 67, 68)

  532. # Some broadcast packets are explicitly ignored by the firewall.
  533. # Others are dopped by the default policy.
  534. # DHCP tests must precede broadcast-related rules, as DHCP relies
  535. # on broadcast traffic initially.

  536. if [ "$DHCP_CLIENT" = "1" ]; then
  537.     # Initialization or rebinding: No lease or Lease time expired.

  538.     iptables -A OUTPUT -o $INTERNET -p udp \
  539.              -s $BROADCAST_SRC --sport 68 \
  540.              -d $BROADCAST_DEST --dport 67 -j ACCEPT

  541.     # Incoming DHCPOFFER from available DHCP servers

  542.     iptables -A INPUT  -i $INTERNET -p udp \
  543.              -s $BROADCAST_SRC --sport 67 \
  544.              -d $BROADCAST_DEST --dport 68 -j ACCEPT

  545.     # Fall back to initialization
  546.     # The client knows its server, but has either lost its lease,
  547.     # or else needs to reconfirm the IP address after rebooting.

  548.     iptables -A OUTPUT -o $INTERNET -p udp \
  549.              -s $BROADCAST_SRC --sport 68 \
  550.              -d $DHCP_SERVER --dport 67 -j ACCEPT

  551.     iptables -A INPUT  -i $INTERNET -p udp \
  552.              -s $DHCP_SERVER --sport 67 \
  553.              -d $BROADCAST_DEST --dport 68 -j ACCEPT

  554.     # As a result of the above, we're supposed to change our IP
  555.     # address with this message, which is addressed to our new
  556.     # address before the dhcp client has received the update.
  557.     # Depending on the server implementation, the destination address
  558.     # can be the new IP address, the subnet address, or the limited
  559.     # broadcast address.

  560.     # If the network subnet address is used as the destination,
  561.     # the next rule must allow incoming packets destined to the
  562.     # subnet address, and the rule must preceed any general rules
  563.     # that block such incoming broadcast packets.

  564.     iptables -A INPUT  -i $INTERNET -p udp \
  565.              -s $DHCP_SERVER --sport 67 \
  566.              --dport 68 -j ACCEPT

  567.     # Lease renewal

  568.     iptables -A OUTPUT -o $INTERNET -p udp \
  569.              -s $IPADDR --sport 68 \
  570.              -d $DHCP_SERVER --dport 67 -j ACCEPT

  571.     iptables -A INPUT  -i $INTERNET -p udp \
  572.              -s $DHCP_SERVER --sport 67 \
  573.              -d $IPADDR --dport 68 -j ACCEPT

  574.     # Refuse directed broadcasts
  575.     # Used to map networks and in Denial of Service attacks
  576.     iptables -A INPUT -i $INTERNET -d $SUBNET_BASE -j DROP
  577.     iptables -A INPUT -i $INTERNET -d $SUBNET_BROADCAST -j DROP

  578.     # Refuse limited broadcasts
  579.     iptables -A INPUT -i $INTERNET -d $BROADCAST_DEST -j DROP

  580. fi
  581. ###############################################################
  582. # ICMP Control and Status Messages

  583. # Log and drop initial ICMP fragments
  584. iptables -A INPUT  -i $INTERNET --fragment -p icmp -j LOG \
  585.          --log-prefix "Fragmented incoming ICMP: "
  586. iptables -A INPUT  -i $INTERNET --fragment -p icmp -j DROP

  587. iptables -A OUTPUT  -o $INTERNET --fragment -p icmp -j LOG \
  588.          --log-prefix "Fragmented outgoing ICMP: "
  589. iptables -A OUTPUT  -o $INTERNET --fragment -p icmp -j DROP

  590. iptables -A INPUT  -i $INTERNET -p icmp \
  591.          --icmp-type source-quench -d $IPADDR -j ACCEPT

  592. iptables -A OUTPUT -o $INTERNET -p icmp \
  593.          -s $IPADDR --icmp-type source-quench -j ACCEPT

  594. iptables -A INPUT  -i $INTERNET -p icmp \
  595.          --icmp-type parameter-problem -d $IPADDR -j ACCEPT

  596. iptables -A OUTPUT -o $INTERNET -p icmp \
  597.          -s $IPADDR --icmp-type parameter-problem -j ACCEPT

  598. iptables -A INPUT  -i $INTERNET -p icmp \
  599.          --icmp-type destination-unreachable -d $IPADDR -j ACCEPT

  600. iptables -A OUTPUT -o $INTERNET -p icmp \
  601.          -s $IPADDR --icmp-type fragmentation-needed -j ACCEPT

  602. # Don¹t log dropped outgoing ICMP error messages
  603. iptables -A OUTPUT -o $INTERNET -p icmp \
  604.          -s $IPADDR --icmp-type destination-unreachable -j DROP

  605. # Intermediate traceroute responses
  606. iptables -A INPUT  -i $INTERNET -p icmp \
  607.          --icmp-type time-exceeded -d $IPADDR -j ACCEPT

  608. # allow outgoing pings to anywhere
  609. if [ "$CONNECTION_TRACKING" = "1" ]; then
  610.     iptables -A OUTPUT -o $INTERNET -p icmp \
  611.              -s $IPADDR --icmp-type echo-request \
  612.              -m state --state NEW -j ACCEPT
  613. fi

  614. iptables -A OUTPUT -o $INTERNET -p icmp \
  615.          -s $IPADDR --icmp-type echo-request -j ACCEPT

  616. iptables -A INPUT  -i $INTERNET -p icmp \
  617.          --icmp-type echo-reply -d $IPADDR -j ACCEPT

  618. # allow incoming pings from trusted hosts
  619. if [ "$CONNECTION_TRACKING" = "1" ]; then
  620.     iptables -A INPUT  -i $INTERNET -p icmp \
  621.              -s $MY_ISP --icmp-type echo-request -d $IPADDR \
  622.              -m state --state NEW -j ACCEPT
  623. fi

  624. iptables -A INPUT  -i $INTERNET -p icmp \
  625.          -s $MY_ISP --icmp-type echo-request -d $IPADDR -j ACCEPT

  626. iptables -A OUTPUT -o $INTERNET -p icmp \
  627.          -s $IPADDR --icmp-type echo-reply -d $MY_ISP -j ACCEPT

  628. ###############################################################
  629. # Logging Dropped Packets

  630. # Don't log dropped incoming echo-requests
  631. iptables -A INPUT -i $INTERNET -p icmp \
  632.          --icmp-type ! 8 -d $IPADDR -j LOG

  633. iptables -A INPUT -i $INTERNET -p tcp \
  634.          -d $IPADDR -j LOG

  635. iptables -A OUTPUT -o $INTERNET -j LOG

  636. exit 0
复制代码
发表于 2004-5-20 15:28:15 | 显示全部楼层
最初由 sbrd 发表
禁止不让下载.torrent,但是bt的种子文件也可以通过别的途径传播呀,我看有人说先关掉所有端口,然后在开放需要的端口,这种途径可以,请问谁能告诉我用iptables怎么写这段代码?谢谢


你说先关端口再开需要的端口,行不通的
发表于 2004-5-20 15:42:11 | 显示全部楼层
首先iptable drop all,然后需要用到哪个就开哪个。
除非下BT的能自定义到80,21这样的端口上去。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表