将BIND配置为Debian上的仅捕获DNS服务器

时间:2020-03-21 11:42:27  来源:igfitidea点击:

DNS服务器解析查询时,会将答案返回给客户端。
DNS服务器还会在记录的TTL值所允许的时间段内,将答案存储在其缓存中。
这样,当要求名称服务器再次解析相同的名称时,可以更快地处理所有后续请求。
仅缓存服务器不转发任何区域。

软件

本文使用的软件:

  • Debian Wheezy
  • Bind 9.8.4

在我们开始之前

我们将要建立一个通用的DNS服务器,该服务器:

  • 充当两个内部区域的主节点,以及
  • 充当所有其他请求的缓存服务器。

BIND服务器的信息:

  • 主机名:dns。
  • IP:10.10.1.2.
  • 局域网:10.10.1.0/24.

将设置两个内部DNS区域:

  • lan.local –前向区域,可将域名转换为IP地址。
  • 1.10.10 –反向区域,可将IP地址转换为域名。

DNS服务器将通过以下方式保护:

  • 以较少的权限运行BIND。
  • 将查询限制为仅LAN。
  • 限制区域仅转移到LAN。
  • 隐藏公开的BIND版本号和主机名。
  • 配置iptables以仅允许从LAN访问TCP/UDP端口53.

安装

安装BIND和resolvconf软件包:

# apt-get update && apt-get install bind9 bind9utils resolvconf

在启动时启动BIND:

# update-rc.d bind9 defaults
update-rc.d: using dependency based boot sequencing

dnsutils软件包包含dig和nslookup:

# apt-get install dnsutils

我们服务器的网络配置如下:

# cat /etc/network/interfaces
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet static
 address 10.10.1.2
 netmask 255.255.255.0
 gateway 10.10.1.1
 broadcast 10.10.1.255
 dns-namesevers 127.0.0.1

如果进行了任何更改,请重新启动网络配置:

# nohup sh -c "ifdown eth0 && ifup eth0"

更新“ /etc/resolv.conf”文件:

# resolvconf -u

配置

首先要做的是确保BIND不会以root特权运行。
打开“/etc/default/bind9”并添加以下行(如果尚不存在):

OPTIONS="-u bind"

这告诉BIND服务在完成特权操作(例如创建侦听特权端口的套接字)之后将setuid设置为“绑定”用户。

/etc/bind/named.conf

提供了一些注释。
建议我们查看BIND v9.8文档以获取更多信息。

include "/etc/bind/rndc.key";
include "/etc/bind/named.conf.default-zones";
# Declaring control channels to be used by the rndc utility.
controls {
	inet 127.0.0.1 port 953
	allow { 127.0.0.1; } keys { "rndc-key"; };
};
# Limiting access to local networks only.
acl "clients" {
        127.0.0.0/8;
        10.10.1.0/24;
};
# Global server configuration.
options {
	directory "/var/cache/bind";
        pid-file "/var/run/named/named.pid";
        dump-file "/var/cache/bind/named.dump";
        statistics-file "/var/cache/bind/named.stats";
	# no forwarding, plus listen on IPv4 only
	forwarders {};
	listen-on-v6 { none; };
	# Maximum number of simultaneous client TCP connections to accept.
	tcp-clients 50;
	# Disable built-in server information zones.
	version none;
	hostname none;
	server-id none;
	# Attempt to do all the work required to answer the query.
	recursion yes;
	recursive-clients 500;
        allow-recursion { clients; };
        allow-query { clients; };
	# Only LAN users are allowed to receive zone transfers from the server.
        allow-transfer { clients; };
	auth-nxdomain no;
        notify no;
	dnssec-enable yes;
	dnssec-validation auto;
};
# Specifications of what to log, and where the log messages are sent.
logging {
        channel "common_log" {
                file "/var/log/bind/named.log" versions 10 size 5m;
		severity error;
                print-category yes;
                print-severity yes;
                print-time yes;
        };
        category default { "common_log"; };
        category general { "common_log"; };
        category queries { "common_log"; };
	category client { "common_log"; };
	category security { "common_log"; };
	category query-errors { "common_log"; };
	category lame-servers { null; };
};
# Internal zone definitions.
zone "lan.local" {
        type master;
        file "/etc/bind/db.lan.local";
};
zone "1.10.10.in-addr.arpa" {
        type master;
        file "/etc/bind/db.1.10.10";
};

/etc/bind/db.lan.local

$TTL    86400
@               IN      SOA     localhost. root.localhost. (
                                        2014082401      ; Serial
                                        86400           ; Refresh
                                        3600            ; Retry
                                        604800          ; Expire
                                        7200 )          ; Negative Cache TTL
@               IN      NS      localhost.
@		IN	A	10.10.1.2
dns             IN      A       10.10.1.2
gw		IN	A	10.10.1.1
www             IN      A       10.10.1.17

/etc/bind/db.1.10.10

$TTL    86400
@               IN      SOA     localhost. root.localhost. (
                                        2014082401      ; Serial
                                        86400           ; Refresh
                                        3600            ; Retry
                                        604800          ; Expire
                                        7200 )          ; Negative Cache TTL
@               IN      NS      localhost.
1		IN	PTR	gw.lan.local.		;10.10.1.1
2		IN	PTR	dns.lan.local.		;10.10.1.2
17		IN	PTR	www.lan.local.		;10.10.1.17

验证BIND配置

# named-checkconf /etc/bind/named.conf

重新启动服务

# service bind9 restart

验证服务正在侦听TCP/UDP端口53:

# netstat -nltup | grep :53
tcp        0      0 10.10.1.2:53     0.0.0.0:*       LISTEN   10905/named     
tcp        0      0 127.0.0.1:53     0.0.0.0:*       LISTEN   10905/named     
udp        0      0 10.10.1.2:53     0.0.0.0:*                10905/named     
udp        0      0 127.0.0.1:53     0.0.0.0:*                10905/named

在BIND服务器上配置iptables以允许LAN访问

# iptables -A INPUT -s 10.10.1.0/24 -p tcp --dport 53 -j ACCEPT
# iptables -A INPUT -s 10.10.1.0/24 -p udp --dport 53 -j ACCEPT

从客户端PC查询DNS服务器

缓存DNS服务器

验证BIND服务器正在缓存查询:

$dig kernel.org | grep time
;; Query time: 237 msec
$dig kernel.org | grep time
;; Query time: 1 msec

请注意,重新启动BIND时,缓存数据将被丢弃。

DNS区域转移

如果“允许转移{无; };”在“ named.conf”中指定。

$host -l lan.local
lan.local name server localhost.
lan.local has address 10.10.1.2
dns.lan.local has address 10.10.1.2
gw.lan.local has address 10.10.1.1
www.lan.local has address 10.10.1.17

为了使挖掘输出更具可读性:

$alias dig='dig +noquestion +nocomments +nostats'

我们还可以使用“挖+ noall +答案”之类的方法。

$dig @lan.local lan.local axfr
; <<>> DiG 9.8.4-rpz2+rl005.12-P1 <<>> +noquestion +nocomments +nostats @lan.local lan.local axfr
; (1 server found)
;; global options: +cmd
lan.local.		3600	IN	SOA	localhost. root.localhost. 2014082401 86400 3600 604800 7200
lan.local.		3600	IN	NS	localhost.
lan.local.		3600	IN	A	10.10.1.2
dns.lan.local.		3600	IN	A	10.10.1.2
gw.lan.local.		3600	IN	A	10.10.1.1
www.lan.local.		3600	IN	A	10.10.1.17
lan.local.		3600	IN	SOA	localhost. root.localhost. 2014082401 86400 3600 604800 7200

反向DNS查找

$dig -x 10.10.1.2
; <<>> DiG 9.8.4-rpz2+rl005.12-P1 <<>> +noquestion +nocomments +nostats -x 10.10.1.2
;; global options: +cmd
2.1.10.10.in-addr.arpa.	3600	IN	PTR	dns.lan.local.
1.10.10.in-addr.arpa.	3600	IN	NS	localhost.
localhost.		604800	IN	A	127.0.0.1
localhost.		604800	IN	AAAA	::1

查询BIND服务器的版本和主机名

获取BIND版本。
如果在“ named.conf”中指定了“ version none;”,则此方法不起作用。

$dig @lan.local version.bind chaos txt
; <<>> DiG 9.8.4-rpz2+rl005.12-P1 <<>> +noquestion +nocomments +nostats @lan.local version.bind chaos txt
; (1 server found)
;; global options: +cmd
version.bind.		0	CH	TXT	"9.8.4-rpz2+rl005.12-P1"
version.bind.		0	CH	NS	version.bind.

获取BIND主机名。
如果在“ named.conf”中指定了“ hostname none;”,则此方法不起作用。

$dig @lan.local hostname.bind chaos txt
; <<>> DiG 9.8.4-rpz2+rl005.12-P1 <<>> +noquestion +nocomments +nostats @lan.local hostname.bind chaos txt
; (1 server found)
;; global options: +cmd
hostname.bind.		0	CH	TXT	"dns"
hostname.bind.		0	CH	NS	hostname.bind.

故障排除

检查系统日志:

# grep named /var/log/syslog | less

检查BIND日志:

# tail /var/log/bind/named.log