自动安装squid+iptables上网代理及上网行为管理脚本(二)。

#本节内容为配置iptables 与 squid ,并以文件来获取规则,以方便管理规则。
#接上一个脚本内容,此部分内容需根据需求更改部分内容。
#停止squid服务
systemctl enable squid &> /dev/null
systemctl stop squid
#编辑squid配置文件
squid_conf_file=/etc/squid/squid.conf
mv $squid_conf_file $squid_conf_file.bk
cat > $squid_conf_file << EOF
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
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
#visible_hostname
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
acl allow_all src "/etc/squid/rules/allow_all.squid"
http_access allow allow_all
acl files urlpath_regex -i \.mp3$ \.avi$ \.exe$ \.rar$ \.zip$ \.mp4$ \.7z$ \.rm$ \.rmvb$ \.qsv$ \.mov$ \.msi$ \.wav$ \.torrent$ \.cab$ \.com$ \.bat$ \.gz$ \.bz2$ \.sys$ \.swf$
http_access deny files
acl only_web src "/etc/squid/rules/allow_only_web.squid"
http_access allow only_web
http_access allow localhost
http_access deny all
#http_port 3128 transparent
http_port 3129 
cache_dir aufs /var/spool/squid 10240 16 256
cache_mem 1024 MB
maximum_object_size 8 MB
minimum_object_size 0 kB
maximum_object_size_in_memory 4096 kB
acl nocache urlpath_regex -i \.asp$ \.jsp$
no_cache deny nocache
acl nogov urlpath_regex -i \.gov\.cn
no_cache deny nogov 
ipcache_size 65535
fqdncache_size 65535
coredump_dir /var/spool/squid
cache_log /var/log/squid/cache.log
access_log /var/log/squid/access.log
cache_store_log /var/log/squid/store.log 
cache_store_log none
logfile_rotate 7
cache_swap_low 85
cache_swap_high 95
error_directory /usr/share/squid/errors/zh-cn
cache_mgr [email protected]
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i \.(jpg|jpeg|gif|png|xml|html|htm|css|js|ico) 1440    90%     2880
refresh_pattern -i \.(mp3|mp4|swf|rar|zip)      1440    20%     10080
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern .               0       20%     4320
EOF
sed -i ‘s/#visible_hostname/visible_hostname ‘${host_name}‘/g‘ ${squid_conf_file}
sed -i ‘s/#http_port 3128 transparent/http_port ‘${lan_ip}‘:3128 transparent/g‘ ${squid_conf_file}
#configrue iptables 
#将以下内容复制出来,重新编写变量,用来日常网络权限控制!
#日常管理脚本策略根据自身要求自行定义。
#-------copy begin---------
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ip_conntrack
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo "1" > /proc/sys/net/ipv4/ip_forward
read -p "input manager host ip address : "  manager_ip  #初始脚本时所定义变量,日常管理脚本不需要。
#------管理脚本,自定义变量-------
#lan=
#lan_ip=
#lan_dns1=
#lan_dns2=
#lan_net=
#manager_ip=
#wan=
#wan_ip=
iptables -F
iptables -X
iptables -Z
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -d ${lan_ip} -p tcp --dport 3128 -j ACCEPT
iptables -A INPUT -s ${manager_ip} -i ${lan} -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
AICMP="0 3 3/4 4 11 12 18"
for TYICMP in ${AICMP} ; do
 iptables -A INPUT -i ${lan} -p icmp --icmp-type ${TYICMP} -j ACCEPT
done
 
iptables -N syn-flood
iptables -A INPUT -p tcp --syn -j syn-flood
iptables -I syn-flood -p tcp -m limit --limit 1/s --limit-burst 3 -j RETURN
iptables -A syn-flood -j DROP
iptables -I OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -s ${wan_ip} -p tcp -m multiport --dports 80,443 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -s ${lan_dns1} -p udp --dport 53 -j ACCEPT
iptables -A FORWARD -s ${lan_dns2} -p udp --dport 53 -j ACCEPT
#  redirect and SNAT
iptables -t nat -A PREROUTING -i ${lan} -p tcp --dport 80 -j REDIRECT --to-ports 3128

read -p "input super subnet! exap 192.168.0.0/22 "  lan_sup_subnet
iptables -t nat -A POSTROUTING -s ${lan_sup_subnet} -o ${wan} -j SNAT --to-source ${wan_ip}
#自定义规则,默认规则为拒绝,80端口的权限由 squid 管理,不需要添加。
#本通过读取文件来配置防火墙规则,如,规则文件放在/usr/local/iptabes中。 
#规则文件以IP开头,中间以空格分开,后接备注名称,格式如:IP地址  使用人
#exap: 允许所有端口通过。
if [ ! -d /usr/local/iptables ] ; then
 mkdir /usr/local/iptables
fi
if [ ! -d /etc/squid/rules ] ; then
 mkdir /etc/squid/rules
fi

echo "#允许所有端口通过" > /usr/local/iptables/allow_all.rule   #管理脚本需自定义文件,不需要此行
echo "$manager_ip it" >> /usr/local/iptables/allow_all.rule #添加IT
ALLOW_ALL_RULE=`grep -v "#" /usr/local/iptables/allow_all.rule | awk ‘{print $1}‘`
for ALLOW_ALL in ${ALLOW_ALL_RULE} ; do
 iptables -A FORWARD -s ${ALLOW_ALL} -o ${wan} -j ACCEPT
done
#同步 squid 配置文件
echo "用户没有限制" > /etc/squid/rules/allow_all.squid 
echo $ALLOW_ALL_RULE | awk -v RS=" " ‘{print $0}‘ >> /etc/squid/rules/allow_all.squid
#exap: 只允许443,80 通过,但限制 mail关键字,如限制 QQ邮箱。但允许服务器8000端口中的mail关键字。
echo "#只允许443,80端口通过。并限制“Mmail”,但允许服务器8000端口中的mail关键字。 " >> /usr/local/iptables/allow_only_web.rule   #管理脚本不需要此行
echo "$manager_ip it" >> /usr/local/iptables/allow_only_web.rule
ALLOW_ONLY_WEB_RULE=`grep -v "#" /usr/local/iptables/allow_only_web.rule | awk ‘{print $1}‘`
for ALLOW_ONLY_WEB in ${ALLOW_ONLY_WEB_RULE} ; do
 iptables -A FORWARD -s ${ALLOW_ONLY_WEB} -o ${wan} -j ACCEPT
 iptables -I FORWARD -d ${ALLOW_ONLY_WEB} -i ${wan} -m string --algo bm --from 40 --to 450 --hex-string "mail" -p tcp ! --sport 8000 -j DROP
done
#同步 squid 配置文件
echo "允许443,80 通过,但限制 mail关键字" > /etc/squid/rules/allow_only_web.squid
echo $ALLOW_ONLY_WEB_RULE | awk -v RS=" " ‘{print $0}‘ >> /etc/squid/rules/allow_only_web.squid

#初始化squid。
sleep 1  
squid -z #管理脚本不需要此行
sleep 1
systemctl restart squid.service
sleep 5
/sbin/squid -k reconfigure
#--------copy end -------------
#日常管理中只需更改/usr/local/iptabes中的规则文件,再运行管理脚本退可!
#修改规则时,需在管理脚本与squid配置文件中加入相应规则退可。
#编写自启动脚本。
#cat > /usr/lib/systemd/system/ipt.rule.service << EOF
#[Unit]
#Description=iptables and squid manage script
#After=syslog.target network.target nss-lookup.target
#[Service]
#Type=oneshot
#ExecStart=/usr/local/iptables/ipt.rule.sh
#[Install]
#WantedBy=multi-user.target
#EOF
#systemctl enable ipt.rule.service
时间: 2024-07-31 14:34:50

自动安装squid+iptables上网代理及上网行为管理脚本(二)。的相关文章

自动安装squid+iptables上网代理及上网行为管理脚本(一)。

我是一个linux初学者,为了更好的学习linux,自己试着写了一些脚本,只为学习,和爱好.英语不好,为了在终端上运行,所以勉强写了几句.本脚本是为了实现自动安装squid,iptables,并实现上网行为管理的第一份脚本.后续再将iptables的规则写出来.如有错误,或更好的实现方法,请大家一起讨论,研究. script: #!/bin/bash#This script auto configure ip address , hostanem , local yum ,#and change

centos7.3安装squid的透明代理

关于squid这个强大的代理软件,已不须多言,自己也测试了一把,不过,在centos7.3安装测试的过程,出现了问题, 在做透明代理的时候,用iptables是可以成功的,而用firewall默认防火墙怎么也不成功,但真正知道了firwall的原理后,其实firewall已经帮我们做好了!请看 安装 #yum install squid -y 禁用selinux #vi /etc/sysconfig/selinux SELINUX=disabled #setenforce 0 临时关闭 打开路由

西部开源学习笔记BOOK2《自动安装系统》

############################# #####  unit1自动安装系统 ##### ############################# ################################### #### Network Install(HTTP,FTP,NFS) #### ################################### #######1.kickstart脚本###### kickstart脚本是自动应答系统在安装过程中一切

使用PowerShell 自动安装.NET4.6.1离线包

自动安装脚本在文章末尾 自动安装脚本在文章末尾 自动安装脚本在文章末尾 运行环境:Windows Server 2012 R2 开发环境:Win10 Microsoft .NET Framework 4.6.1离线安装包:下载地址 需要注意的是win10能运行的脚本在2012上不一定能运行,所以要把写好的脚本在2012上确保运行. 为了确保安装,需要提前做一些判断 一.获取已安装软件列表 这些判断借鉴了 示例(传送门),可以下载运行试试,但在我的2012不能直接使用 这里  Get-WmiObj

使用squid配置透明代理并对上网行为进行控制

使用Squid配置透明代理 环境:CentOS 6.4 + squid-3.1.10-20.el6_5.3.x86_64 1.检查squid是否默认安装,没有安装先安装 rpm -qa squid 假如查找不到,就安装squid: yum install squid 2.配置虚拟机的网络,一个网卡是内网,另一个是外网 内网:eth0 ip:192.168.10.209 netmask:255.255.255.0 gateway:192.168.10.1 外网:eth1 ip:10.10.10.1

Squid显式代理实现内网上网

一.Squid显式代理上网 Squid显式代理,监听某地址的3128活着8080端口,需要内网用户在浏览器上手动设置代理配置.内网pc只需要保证能路由可达Squid监听的地址即可,无需配置dns,内网pc的所有访问流量都通过浏览器出去(包括DNS解析). 二.配置 拓扑: (该模式下squid单臂部署,路由模式也是没问题的,但需要保证squid能正常上网) ①安装squid  [[email protected] ~]#   yum  install  -y  squid ②配置squid的配置

使用本脚本可以自动批量完成中间节点环境的部署工作,包括:Nginx编译安装、添加程序管理脚本、设置开机启动、反向代理配置、证书分发、添加iptables规则等

使用本脚本可以自动批量完成中间节点环境的部署工作,包括:Nginx编译安装.添加程序管理脚本.设置开机启动.反向代理配置.证书分发.添加iptables规则等.脚本支持自定义nginx安装版本.设置编译模块.配置监听端口等. 1. Nginx Role规则说明 本脚本用于中间节点(Nginx反向代理)环境的自动化配置,主要内容包括: 安装基础依赖环境: 创建nginx启动用户(支持自定义用户): 下载nginx安装文件(可自定义nginx版本): 解压安装文件: 执行编译安装(可自定义编译参数和

centos7安装squid代理

局域网只有一台服务器可以上互联网,其他机器需要使用代理上网,windows下可以用ccproxy,linux建议使用squid(dns解析需要配合iptables) 1.安装squid yum install squid.x86_64 2.配置squid配置文件. vi /etc/squid/squid.conf # # Recommended minimum configuration: # # Example rule allowing access from your local netw

Linux如何安装PHP环境 linux下如何上网

下面介绍的是,linux下,如何 配置php环境,然后实现上网! ####3.安装PHP环境 安装支持库 yum install libxml2 libxml2-devel 下载软件包 mkdir /tmp/php cd /tmp/php wget ftp://192.168.2.231/lamp/php-5.4.13.tar.gz 解压php安装包 tar -xf php-5.4.13.tar.gz cd php-5.4.13 开始安装php ./configure --prefix=/usr