Yami

メモとか、やったこととか。

Squid設定メモ(通信元・通信先・User Agentで制限)

検証環境を作りインターネットへの通信を制御するとき、よくプロキシサーバ(Squid)を構築し利用しているのですが、構築する度に設定方法を調べている気がするのでその設定メモです。

※インターネット公開したり、社内でも本番環境で利用する際は入念な確認をお願いします。何等か問題が発生しても責任は負いかねます。

環境

# uname -a
Linux step 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

導入

# yum update -y
# yum install squid -y
# squid -v
Squid Cache: Version 3.5.20
Service Name: squid
configure options:  '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--sharedstatedir=/var/lib' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-strict-error-checking' '--exec_prefix=/usr' '--libexecdir=/usr/lib64/squid' '--localstatedir=/var' '--datadir=/usr/share/squid' '--sysconfdir=/etc/squid' '--with-logdir=$(localstatedir)/log/squid' '--with-pidfile=$(localstatedir)/run/squid.pid' '--disable-dependency-tracking' '--enable-eui' '--enable-follow-x-forwarded-for' '--enable-auth' '--enable-auth-basic=DB,LDAP,MSNT-multi-domain,NCSA,NIS,PAM,POP3,RADIUS,SASL,SMB,SMB_LM,getpwnam' '--enable-auth-ntlm=smb_lm,fake' '--enable-auth-digest=file,LDAP,eDirectory' '--enable-auth-negotiate=kerberos' '--enable-external-acl-helpers=file_userip,LDAP_group,time_quota,session,unix_group,wbinfo_group,kerberos_ldap_group' '--enable-cache-digests' '--enable-cachemgr-hostname=localhost' '--enable-delay-pools' '--enable-epoll' '--enable-ident-lookups' '--enable-linux-netfilter' '--enable-removal-policies=heap,lru' '--enable-snmp' '--enable-ssl-crtd' '--enable-storeio=aufs,diskd,rock,ufs' '--enable-wccpv2' '--enable-esi' '--enable-ecap' '--with-aio' '--with-default-user=squid' '--with-dl' '--with-openssl' '--with-pthreads' '--disable-arch-native' 'build_alias=x86_64-redhat-linux-gnu' 'host_alias=x86_64-redhat-linux-gnu' 'CFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic -fpie' 'LDFLAGS=-Wl,-z,relro  -pie -Wl,-z,relro -Wl,-z,now' 'CXXFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic -fpie' 'PKG_CONFIG_PATH=:/usr/lib64/pkgconfig:/usr/share/pkgconfig'

設定

/etc/squid/squid.conf

設定したいことは以下です。

  • クライアント(アクセス元)で制限
  • アクセス先(ドメイン名)で制限
  • User Agentで制限
  • SSLHTTPS)でも上記を適用可能 ※ただしSSLの復号は行わない

実際の設定は以下の通りです。なおデフォルト設定から、説明のコメントは削除、デフォルト設定からの変更はコメントアウトのうえ次行に追加、という記載をしています。

#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

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow localhost manager
http_access deny manager

#http_access allow localnet  # クライアント端末のアクセス制限は以下で新たに追加
http_access allow localhost


# ここから、独自の許可設定を追加。
# 「http_access deny all」までの間に許可するルールを書く。
# ルールは最初にヒットしたものが適用され、1行に複数のルールを記載するとAnd条件で判定される。

# 追加ルール例1:クライアントの許可
acl allowsrc1 src 192.168.100.50/32
http_access allow allowsrc1

# 追加ルール例2:クライアントの許可 And 通信先(ドメイン名)の許可
acl allowsrc2 src 192.168.100.51/32
acl allowdomain dstdomain "/etc/squid/allowdomain"
http_access allow allowsrc2 allowdomain

# 追加ルール例3:クライアントの許可 And 指定したブラウザからのみ許可
# クライアントルールは別のファイルに記載
acl allowsrc3 src "/etc/squid/allowsrc3"
acl allowua browser regex "/etc/squid/allowua"
http_access allow allowsrc3 allowua

# 上記の条件に一致しなかった通信はすべて拒否
http_access deny all


#http_port 3128
http_port 8080

coredump_dir /var/spool/squid

refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern .               0       20%     4320

/etc/squid/allowdomain

許可する通信先(ドメイン名)を設定します。

先頭にドットを付けないと完全一致、ドットを付けるとサブドメインも含まれます。

# google.co.jpのみ
google.co.jp
# google.comのほかに、www.google.comなど、サブドメインも含む
.google.com

/etc/squid/allowsrc3

許可するクライアントのIPアドレスを設定します。

CIDR形式(xxx.xxx.xxx.xxx/yy)の書式に対応しています。

192.168.100.60/32

/etc/squid/allowua

許可するUser Agentを設定します。

正規表現で指定します。

Mozilla/5\.0 \(Windows NT 10\.0; Win64; x64\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/95\.0.4638\.54 Safari/537.36

参考:ログに記録される内容

参考に、HTTP、HTTPSそれぞれでアクセスした際のログの内容を記載します。

HTTP通信はURLが、HTTPS通信はドメイン名のみログに記録されます。

1635859328.122    121 192.168.xxx.xxx TCP_MISS/200 15100 GET http://www.google.co.jp/ - HIER_DIRECT/172.217.25.99 text/html
1635859475.556    276 192.168.xxx.xxx TCP_TUNNEL/200 19994 CONNECT www.google.co.jp:443 - HIER_DIRECT/172.217.25.99 -