QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 49062|回复: 89

透明代理 (Iptables 与 Squid)

  [复制链接]
发表于 2002-11-18 13:30:16 | 显示全部楼层 |阅读模式

一、 /etc/sysconfig/iptables 文件


#  /etc/sysconfig/iptables 文件
#

#=======================  古公  =======================

#
# mangle 段
*mangle
REROUTING ACCEPT [0]
:OUTPUT ACCEPT [0]
COMMIT
#

#
# nat 段
*nat
REROUTING ACCEPT [0]
OSTROUTING ACCEPT [0]
:OUTPUT ACCEPT [0]
#

# 为使用 SQUID 作“透明代理”而设定!
#
# 没有指定 网卡、地址:
#[0] -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
#
# 指定 网卡、地址:
[0] -A PREROUTING -s 192.168.20.0/255.255.255.0 -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
[0] -A PREROUTING -s 192.168.20.0/255.255.255.0 -i eth0 -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 3128
# 将 对于 80、443 端口的访问 重定向到 3128 端口。
#
#
#  这些机器可以走这个机器做网关上 Internet 网。
# 需要在 /etc/sysctl.conf 文件里面修改成 net.ipv4.ip_forward = 1
# 或者 echo 1 > /proc/sys/net/ipv4/ip_forward
# 由于利用 SQUID 实现了“透明代理”,Masq 取消相应的客户地址。
# 这里,只剩下几个需要利用“IP伪装”来上网的机器(可以上 QQ、雅虎通、msn 之类的):
#
[0] -A POSTROUTING -s 192.168.20.3 -j MASQUERADE
[0] -A POSTROUTING -s 192.168.20.10 -j MASQUERADE
[0] -A POSTROUTING -s 192.168.20.32/255.255.255.240 -j MASQUERADE  

# 若你的 公网的 IP 地址是固定的,使用这个语句似乎更好些:
#[0] -A POSTROUTING -s 192.168.20.32/255.255.255.240 -j SNAT --to 211.148.130.133  

COMMIT
#

#
# filter 段
*filter
:INPUT ACCEPT [0]
:FORWARD ACCEPT [0]
:OUTPUT ACCEPT [0]

#
# 屏蔽 来自 microsoft 的站点:
[0] -A INPUT -s 207.46.0.0/255.255.0.0 -j DROP
[0] -A INPUT -d 207.46.0.0/255.255.0.0 -j DROP
#

# 防止IP欺骗:
# 所谓的IP欺骗就是指在IP包中存在着不可能的IP源地址或目标地址。
# eth1是一个与外部Internet相连,而192.168.20.0则是内部网的网络号,
# 也就是说,如果有一个包从eth1进入主机,而说自己的源地址是属于
# 192.168.20.0网络,或者说它的目标地址是属于这个网络的,那么这显
# 然是一种IP欺骗,所以我们使用DROP将这个包丢弃。
[0] -A INPUT -d 192.168.20.0/255.255.255.0 -i eth1 -j DROP
[0] -A INPUT -s 192.168.20.0/255.255.255.0 -i eth1 -j DROP
#
# 同样的,如果有包要通过eth1向Internet,而且它的源地址或目标地址是属于
# 网络192.168.20.0,那么显然也是不可能的。我们仍然使用DROP将它丢弃。
[0] -A OUTPUT -d 192.168.20.0/255.255.255.0 -o eth1 -j DROP
[0] -A OUTPUT -s 192.168.20.0/255.255.255.0 -o eth1 -j DROP
#

# 防止广播包从IP代理服务器进入局域网:
[0] -A INPUT -s 255.255.255.255 -i eth0 -j DROP
[0] -A INPUT -s 224.0.0.0/224.0.0.0 -i eth0 -j DROP
[0] -A INPUT -d 0.0.0.0 -i eth0 -j DROP
#    当包的源地址是255.255.255.255或目标地址是0.0.0.0,则说明它是一个
# 广播包,当广播包想进入eth0时,我们就应该DENY,丢弃它。而240.0.0.0/3
# 则是国际标准的多目广播地址,当有一个源地址是属于多目广播地址的包,
# 我们将用DROP策略,丢弃它。

#
# 屏蔽 windows xp 的 5000 端口(这个端口是莫名其妙的 !)
[0] -A INPUT -p tcp -m tcp --sport 5000 -j DROP
[0] -A INPUT -p udp -m udp --sport 5000 -j DROP
[0] -A OUTPUT -p tcp -m tcp --dport 5000 -j DROP
[0] -A OUTPUT -p udp -m udp --dport 5000 -j DROP
# 原来是用来跑 vpn 的,呵呵,我误解了。
#

#
# 防止 Internet 网的用户访问 SAMBA 服务器:
[0] -A INPUT -s 211.148.130.129 -i eth1 -p tcp -m tcp --dport 137:139 -j DROP
[0] -A INPUT -s 211.148.130.129 -i eth1 -p udp -m udp --dport 137:139 -j DROP
[0] -A INPUT -s 192.168.20.0/255.255.255.0 -i eth0 -p tcp -m tcp --dport 137:139 -j ACCEPT
[0] -A INPUT -s 192.168.20.0/255.255.255.0 -i eth0 -p udp -m udp --dport 137:139 -j ACCEPT
[0] -A INPUT -s 211.148.130.128/255.255.255.240 -i eth1 -p tcp -m tcp --dport 137:139 -j ACCEPT
[0] -A INPUT -s 211.148.130.128/255.255.255.240 -i eth1 -p udp -m udp --dport 137:139 -j ACCEPT
[0] -A INPUT -p tcp -m tcp --dport 137:139 -j DROP
[0] -A INPUT -p udp -m udp --dport 137:139 -j DROP
#

#
# 对于本局域网用户不拒绝访问:
[0] -A INPUT -s 192.168.20.0/255.255.255.0 -i eth0 -p tcp -j ACCEPT
[0] -A INPUT -s 192.168.20.0/255.255.255.0 -i eth0 -p udp -j ACCEPT
#

#
[0] -A INPUT -i eth1 -p udp -m udp --dport 3 -j DROP
[0] -A INPUT -i eth1 -p tcp -m tcp --dport 3 -j DROP
[0] -A INPUT -i eth1 -p tcp -m tcp --dport 111 -j DROP
[0] -A INPUT -i eth1 -p udp -m udp --dport 111 -j DROP
#

#
[0] -A INPUT -i eth1 -p udp -m udp --dport 587 -j DROP
[0] -A INPUT -i eth1 -p tcp -m tcp --dport 587 -j DROP
#

# 防止 Internet 用户访问 SQUID 的 3128 端口:
[0] -A INPUT -s 211.148.130.129 -i eth1 -p tcp -m tcp --dport 3128 -j DROP
[0] -A INPUT -s 192.168.20.0/255.255.255.0 -i eth0 -p tcp -m tcp --dport 3128 -j ACCEPT
[0] -A INPUT -s 211.148.130.128/255.255.255.240 -i eth1 -p tcp -m tcp --dport 3128 -j ACCEPT
[0] -A INPUT -p tcp -m tcp --dport 3128 -j DROP
#

# 让人家 ping 不通我 !
[0] -A INPUT -i eth1 -s 192.168.30.0/24 -p icmp -m icmp --icmp-type 8 -j ACCEPT
[0] -A INPUT -i eth1 -s 211.148.130.128/28 -p icmp -m icmp --icmp-type 8 -j ACCEPT
[0] -A INPUT -i eth1 -p icmp -m icmp --icmp-type 8 -j DROP
#

COMMIT
# =======================  结束 =======================
# =======================  古公 =======================




二、再看看 /etc/squid/squid.conf 文件


# /etc/squid/squid.conf 文件
#

# http_port 3128
http_port 192.168.20.8:3128

hierarchy_stoplist cgi-bin ?
acl QUERY urlpath_regex cgi-bin \?
no_cache deny QUERY

# cache_mem 8 MB
cache_mem   48 MB

# emulate_httpd_log off
# ============================================================================
emulate_httpd_log on
# ============================================================================

# redirect_rewrites_host_header on
# ============================================================================
redirect_rewrites_host_header off
# ============================================================================

#Recommended minimum configuration:
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl SSL_ports port 443 563
acl Safe_ports port 80                # http
acl Safe_ports port 21                # ftp
acl Safe_ports port 443 563        # https, snews
acl Safe_ports port 70                # gopher
acl Safe_ports port 210                # wais
acl Safe_ports port 1025-65535        # unregistered ports
acl Safe_ports port 280                # http-mgmt
acl Safe_ports port 488                # gss-http
acl Safe_ports port 591                # filemaker
acl Safe_ports port 777                # multiling http
acl CONNECT method CONNECT

# ============================================================================
#
acl allow_domain        dstdomain        "/etc/squid/allow_domain"

# 下面是只允许每天上三个小时的:
acl no_allow_time_0_1        time                "/etc/squid/no_allow_time_0_1"
acl no_allow_time_0_2        time                "/etc/squid/no_allow_time_0_2"
acl no_allow_time_0_3        time                "/etc/squid/no_allow_time_0_3"
acl no_allow_time_0_4        time                "/etc/squid/no_allow_time_0_4"
acl no_allow_time_0_5        time                "/etc/squid/no_allow_time_0_5"
# 完

# 下面是只允许每天上八个小时的:
acl no_allow_time_1_1   time            "/etc/squid/no_allow_time_1_1"
acl no_allow_time_1_2   time            "/etc/squid/no_allow_time_1_2"
acl no_allow_time_1_3   time            "/etc/squid/no_allow_time_1_3"
acl no_allow_time_1_4   time            "/etc/squid/no_allow_time_1_4"
acl no_allow_time_1_5   time            "/etc/squid/no_allow_time_1_5"
# 完

acl no_allow_web        dst                "/etc/squid/no_allow_web"
acl no_allow_domain        dstdomain        "/etc/squid/no_allow_domain"
acl no_allow_client        src                "/etc/squid/no_allow_client"
#acl allow_time                time                "/etc/squid/allow_time"
#
acl allow_client_inf        src                "/etc/squid/allow_client_inf"
acl allow_client_fore        src                "/etc/squid/allow_client_fore"
acl allow_client_8h        src                "/etc/squid/allow_client_8h"
acl allow_client_3h        src                "/etc/squid/allow_client_3h"
#
#
#
acl Uncachable url_regex cgi \?
#

# Only allow cachemgr access from localhost
http_access allow manager localhost
http_access deny manager

#  ============================================================================

# Deny requests to unknown ports
http_access deny !Safe_ports

# ============================================================================
no_cache        deny        Uncachable
http_access        allow        allow_domain
http_access        allow        allow_client_inf
http_access        deny        no_allow_web
http_access        deny        no_allow_domain
http_access        deny        no_allow_client
http_access        allow        allow_client_fore
#
# 下面是只允许每天上八个小时的:
http_access     deny    no_allow_time_1_1 allow_client_8h
http_access     deny    no_allow_time_1_2 allow_client_8h
http_access     deny    no_allow_time_1_3 allow_client_8h
http_access     deny    no_allow_time_1_4 allow_client_8h
http_access     deny    no_allow_time_1_5 allow_client_8h
http_access     allow   allow_client_8h
# 完
#
# 下面是只允许每天上三个小时的:
http_access        deny        no_allow_time_0_1 allow_client_3h
http_access        deny        no_allow_time_0_2 allow_client_3h
http_access        deny        no_allow_time_0_3 allow_client_3h
http_access        deny        no_allow_time_0_4 allow_client_3h
http_access        deny        no_allow_time_0_5 allow_client_3h
http_access        allow        allow_client_3h
# 完
#http_access deny        no_allow_time
# ============================================================================


# Deny CONNECT to other than SSL ports
http_access deny CONNECT !SSL_ports

# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
# And finally deny all other access to this proxy
http_access allow localhost
http_access deny all

#Allow ICP queries from eveyone
icp_access allow all

cache_mgr [email protected]

# httpd_accel_port 80
# +++++++++++++++++++++++++++++++++++ 古公 ++++++++++++透明代理的设定+++++++++
httpd_accel_host virtual
#httpd_accel_port 80
# +++++++++++++++++++++++++++++++++++ 古公 ++++++++++++透明代理的设定+++++++++

# httpd_accel_with_proxy on
# +++++++++++++++++++++++++++++++++++ 古公 ++++++++++++透明代理的设定+++++++++
httpd_accel_with_proxy off
# +++++++++++++++++++++++++++++++++++ 古公 ++++++++++++透明代理的设定+++++++++

# httpd_accel_uses_host_header off
# +++++++++++++++++++++++++++++++++++ 古公 ++++++++++++透明代理的设定+++++++++
httpd_accel_uses_host_header on
# +++++++++++++++++++++++++++++++++++ 古公 ++++++++++++透明代理的设定+++++++++


append_domain .fruitron.com.cn


# ============================================================================
error_directory /usr/lib/squid/errors/Simplify_Chinese
# ============================================================================


# ============================================================================

delay_pools 1      # 1 delay pools
delay_class 1 3    # pool 1 is a class 3 pool

# ============================================================================

#delay_access 1 deny all

delay_access 1 allow allow_client_3h allow_client_8h allow_client_fore allow_client_inf
delay_access 1 deny all
# ============================================================================


# ============================================================================
delay_parameters        1        8000/8000 2000/4000 4000/8000
#delay_parameters        2        8000/8000 4000/8000 4000/8000
#delay_parameters        3        8000/8000 4000/8000 4000/8000
# ============================================================================

# ie_refresh off
# ============================================================================
#ie_refresh on
# ============================================================================

 楼主| 发表于 2002-11-18 13:51:50 | 显示全部楼层


参照一下 ifconfig 的输出,可能效果更好一些:



2002年11月18日下午13时41分28秒[root@squid gugong]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:FE:B7:7A:8D
          inet addr:192.168.20.8  Bcast:192.168.20.255  Mask:255.255.255.0
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
          RX packets:11020015 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11436178 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:2195933623 (2094.2 Mb)  TX bytes:3942280622 (3759.6 Mb)
          Interrupt:10 Base address:0xd000

eth0:0    Link encap:Ethernet  HWaddr 00:50:FE:B7:7A:8D
          inet addr:192.168.10.8  Bcast:192.168.20.255  Mask:255.255.255.0
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
          Interrupt:10 Base address:0xd000

eth1      Link encap:Ethernet  HWaddr 00:05:5D:0D:BE:8A
          inet addr:211.148.130.133  Bcast:211.148.130.135  Mask:255.255.255.248
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1710331 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1135502 errors:3 dropped:0 overruns:0 carrier:0
          collisions:21006 txqueuelen:100
          RX bytes:1075231971 (1025.4 Mb)  TX bytes:153524789 (146.4 Mb)
          Interrupt:11 Base address:0xb800

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:7094 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7094 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:835017 (815.4 Kb)  TX bytes:835017 (815.4 Kb)

回复

使用道具 举报

发表于 2002-11-20 12:29:55 | 显示全部楼层
very good!
你都是哪里搞的?!

佩服!
回复

使用道具 举报

 楼主| 发表于 2002-11-20 14:11:43 | 显示全部楼层
外为己用。
回复

使用道具 举报

发表于 2002-12-3 12:53:37 | 显示全部楼层
没有/etc/sysconfig/iptables 文件怎么办?我是RH8.0
回复

使用道具 举报

发表于 2002-12-8 12:18:14 | 显示全部楼层
宿舍有4部电脑,一个ip
以前是用winxp用双网卡,共享网卡做nat的
现在我想用redhat8.0做代理,可以怎么样做,是不是做一个透明代理或者ipchains就行,不用两个都弄把
假如我的ip是192.168.54.28:255.255.252.0(从学校获得的ip)
谢谢!
回复

使用道具 举报

发表于 2003-1-11 12:42:29 | 显示全部楼层
i fu le.
我正需要。
回复

使用道具 举报

发表于 2003-1-18 08:38:14 | 显示全部楼层

如何配置squid的用户认证,我按照好多资料上的配置做,可是老提示

很急的,请大人关照一下
回复

使用道具 举报

发表于 2003-1-23 17:06:57 | 显示全部楼层

老大,你原来在这里呀

.古老大,pchome里多靠你帮助,搞定了公司里的服务器,现在你好久不去了,大家都很想你呀
回复

使用道具 举报

发表于 2003-3-1 10:53:30 | 显示全部楼层
其中的
# 让人家 ping 不通我 !
[0] -A INPUT -i eth1 -s 192.168.30/24 -p icmp -m icmp --icmp-type 8 -j ACCEPT
[0] -A INPUT -i eth1 -s 211.148.130.128/28 -p icmp -m icmp --icmp-type 8 -j ACCEPT
[0] -A INPUT -i eth1 -p icmp -m icmp --icmp-type 8 -j DROP
#

这段的第一句中的"..... -s 192.168.30/24 -p  ......"似乎缺一点什么?应该家什么?
回复

使用道具 举报

 楼主| 发表于 2003-3-1 15:14:49 | 显示全部楼层
不明白你的意思 ,你的意思是 ... ?
回复

使用道具 举报

发表于 2003-3-5 06:22:26 | 显示全部楼层
gugong,我按这文章配置了局域网,网内可以ping到192.168.0.1和主机的ip地址,就是在局域网中上不了网,是什么原因可以指点下吗?
我已经找了好多资料了。
回复

使用道具 举报

 楼主| 发表于 2003-3-5 13:07:06 | 显示全部楼层
你可以详细说明你的情况吗 ?
回复

使用道具 举报

发表于 2003-3-5 19:48:35 | 显示全部楼层
不好意思,太匆忙了。我又装了一遍redhat,不是修改/etc/sysconfig/iptables 文件出问题,而是我的第一步配置出问题。
但是还是需要你的帮助和关怀,
1.我的基础:
我的adsl线是直接可以接到internet的,不需要拨号,不管是windows还是linux,ip也是固定的80.25.203.188.
现在我装了redhat8.1beta3,作为居域网的主机,共享网络资源。故,配置了2张网卡,eth1的ip设为80.25.203.188,eth0的ip设为192.168.0.1.
2,我的共享的实现
根据网络上的文件,修改了/etc/rc.d/rc.local,增加了
etho 1 > /proc/sys/net/ipv4/ip_forward
modprobe iptable_nat
modprobe ip_table
modprobe ip_nat_table
modprobe ip_conntrack
modprobe ip_conntrack_ftp
iptables -t nat -A POSTROUNTING -s 192.168.0.0/24 -o eth1 -j SNAT --to 80.25.203.188
然后我重新启动,局域网可以连通外网了。
3,问题接着就来了。我看了资料
http://www.linuxforum.net/forum/showflat.php?Cat=&Board=proxy&Number=374971&page=1&view=collapsed&sb=5&o=&fpart=3&vc=1
我想,把上面的写到nat.sh也不错啊,所以就根据这个接上的第一步写了nat.sh.
#!/bin/sh
PATH=$PATH:/usr/sbin:/sbin
echo 1 > /porc/sys/net/ipv4/ip_forward
iptables -F INPUT
iptables -F FORWARD
iptables -F POSTROUTING -t nat
iptables -P FORWARD DROP
insmod ip_nat_ftp
insmod ip_conntrack_ftp
iptables -A FORWARD -s 192.168.0.0/24 -j ACCEPT
iptables -A FORWARD -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -o eth1 -s 192.168.0.0/24 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth1 -j SNAT --to 80.25.203.188
接着chmod +x nat.sh,加入/etc.rc.d/rc.local.但我没去修改
/etc/sysconfig/network
/etc/sysconfig/network-scripts/ifcfg-eth0和eth1

4.然后重新启动,我的局域网就不能上网了。于是我又重新修改回到第2步,还是不能上网,局域网中的机子可以ping通192.168.0.1和我的ip80.25.203.188,也可以打开80.25.203.188地址的网页,但是就是ping不到外网的ip.

你能帮我解决吗?我想在实现局域网后还要实现网络过滤,却在这首先的一步上挂了,呵呵。先谢谢你。
回复

使用道具 举报

 楼主| 发表于 2003-3-6 08:53:07 | 显示全部楼层
iptables -t nat -A POSTROUTING -o eth1 -s 192.168.0.0/24 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth1 -j SNAT --to 80.25.203.188

只需要一行,注释掉其中一行。

第3步运行后,你没有清除 iptables 的规则,就运行了第二步。是导致错误的原因。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-5-9 05:38 , Processed in 0.083392 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

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