现有生产环境是centos6+bind9.8.2+namedmanager解决方案,该方案使用多年都十分稳定,性能优良,但由于centos平台无法再升级bind版本了,导致漏扫一直能扫出各种dns漏洞,必须寻找替代方案。

  新架构:4台debian服务器安装最新版bind,官方版本到9.16.15了,而且持续更新,生产环境两两用keepalived起虚IP(本笔记只使用了192.168.118.250这个IP作为实验),管理端为192.168.118.25,管理端安装dnscontrol。管理端使用scp传输和bind提供的rndc工具远程控制4台服务器。   安装准备

1. https://www.debian.org/ 下载网络安装镜像(查了bind官方文档推荐使用debian发行版,可直接用包管理器安装最新版bind)2. 安装4台服务器操作系统为debian10 IP地址分配3. 根据主机基线对4台服务器进行主机安全加固

4. 安装bind9 1)添加isc官方源

vi /etc/apt/sources.list 添加一行 deb http://ftp.cn.debian.org/debian sid main

2)sudo apt-get install bind9 就会安装最新版本的bind9,以后升级也十分方便,只需执行apt-get upgrade bind9即可。 5. 安装keepalived

sudo apt-get install keepalived

6. 更改 dns地址为自己

vi /etc/resolv.conf nameserver 127.0.0.1

 

一、named(bind9) 配置

vi /etc/bind/named.conf

// Thi is the primary configuration file for the BIND DNS server named.
//
// Please read /usr/share/doc/bind9/README.Debian.gz for information on the
// structure of BIND configuration files in Debian, *BEFORE* you customize
// this configuration file.
//
// If you are just adding zones, please do that in /etc/bind/named.conf.local

//include "/etc/bind/named.conf.options";
//include "/etc/bind/named.conf.local";

options {
listen-on port 53 { any; };
listen-on-v6 { any; };
directory "/var/cache/bind";
dump-file "/var/cache/bind/xcache_dump.db";
statistics-file "/var/cache/bind/named_stats.txt";
memstatistics-file "/var/cache/bind/named_mem_stats.txt";
allow-query { any; };
allow-new-zones yes;
allow-query-cache { any; };
forwarders { 202.103.224.68; 202.103.225.68; 119.29.29.29; 223.6.6.6;};
recursion yes;

dnssec-enable no;
dnssec-validation no;

};

logging {
channel query-errors_log {
file "query-errors" versions 5 size 20m;
print-time yes;
print-category yes;
print-severity yes;
severity dynamic;
};

channel default_debug {
file "named.run" versions 5 size 20m;
print-time yes;
print-category yes;
print-severity yes;
severity dynamic;
};

category queries { null; };
category query-errors {query-errors_log; };
category default { default_debug; };

};

include "/etc/bind/named.conf.default-zones";

配置开机启动

sudo systemctl enable named sudo systemctl enable keepalived

  此时单台DNS服务器已安装完成,windows客户端可以用nslookup www.qq.com 192.168.118.250 检查解析是否正常。linux客户端使用dig @192.168.118.250 www.qq.com

 

二、dnscontrol安装

直接下载对应安装包安装即可,十分方便。

​ ​​https://github.com/StackExchange/dnscontrol/releases/​

三、dnscontrol基本使用

这里有两个官方配置手册 入门手册 ​https://stackexchange.github.io/dnscontrol/getting-started

迁移手册 ​https://stackexchange.github.io/dnscontrol/migrating

我们先简单学习一下基本使用

cd /opt/ mkdir dnscontrol cd dnscontrol mkdir zones

1)配置授信文件(dnscontrol支持各种各样的DNS服务商,支持同时推送,bind是其中之一)

vi creds.json  指定生成文件为zone文件格式
{
"bind": {
"directory": "zones",
"filenameformat": "%U.zone"
}
}

vi dnsconfig.js 这是主配置文件,所有自定义DNS记录都在里面添加、配置、修改、删除,可以结合svn或者git进行版本管理。

// Providers:

var REG_NONE = NewRegistrar('none', 'NONE'); // No registrar.
var DNS_BIND = NewDnsProvider('bind', 'BIND', {
'default_soa': {
'master': 'ns1.example.com.',
'mbox': 'ns1.example.com.',
'refresh': 3600,
'retry': 600,
'expire': 604800,
'minttl': 1440,
},
'default_ns': [
'ns1.example.com.',
'ns2.example.com.'
]
})

// Domains:

D('example.com', REG_NONE, DnsProvider(DNS_BIND),
A('ns1', '192.168.118.118'),
A('ns2', '192.168.117.117'),
A('test1', '192.168.118.15'),
A('www', '192.168.117.5'),
A('www2', '192.168.117.5'),
A('ntp', '192.168.118.5')
);

推送,可以看到目录多了个zone文件。

root@tmpl-debian10:~/dnscontrol# dnscontrol push
******************** Domain: example.com
----- Getting nameservers from: bind
----- DNS Provider: bind...
File does not yet exist: "/opt/dnscontrol/zones/example.com.zone"
1 correction
#1: GENERATE_ZONEFILE: 'example.com' (new file with 9 records)

WRITING ZONEFILE: /opt/dnscontrol/zones/example.com.zone
SUCCESS!
----- Registrar: none...
0 corrections
Done. 1 corrections.


好了,顺利生成bind的zone配置文件,下一步使用scp和rndc工具进行推送,推送之前要先配置好rndc组件以实现dns服务器允许控制端控制。  

四、配置rndc通讯

需要先在DNS管理机上安装好组件

apt-get install bind9utils

生成密钥

rndc-confgen -r /dev/urandom   会自动生成一段配置内容,注意添加修改dns服务器ip地址

# Start of rndc.conf
key "rndc-key" {
algorithm hmac-md5;
secret "Jb8UIcC9WJAWeRQaKZ3E+Q==";
};

options {
default-key "rndc-key";
default-server DNS服务器IP地址;
default-port 953;
};
# End of rndc.conf

将以上这段复制到控制机 /etc/bind/rndc.conf,以下内容追加复制到DNS服务器named.conf,注意放通防火墙rndc TCP953端口,并加入管理机ip地址,重启服务

# Use with the following in named.conf, adjusting the allow list as needed:
key "rndc-key" {
algorithm hmac-md5;
secret "Jb8UIcC9WJAWeRQaKZ3E+Q==";
};

controls {
inet 0.0.0.0 port 953
allow { 管理机ip; } keys { "rndc-key"; };
};
# End of named.conf

配置完成后在控制端进行联通性测试

ncvtadm@ops:~$ rndc -s 192.168.118.250 status
version: BIND 9.16.15-Debian (Stable Release) <id:4469e3e>
running on tmpl-debian10: Linux x86_64 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21)
boot time: Sun, 22 Aug 2021 11:28:34 GMT
last configured: Sun, 22 Aug 2021 11:28:34 GMT
configuration file: /etc/bind/named.conf
CPUs found: 16
worker threads: 16
UDP listeners per interface: 16
number of zones: 102 (97 automatic)
debug level: 0
xfers running: 0
xfers deferred: 0
soa queries in progress: 0
query logging is ON
recursive clients: 0/900/1000
tcp clients: 0/150
TCP high-water: 0
server is up and running
ncvtadm@ops:~$


五、推送配置

下面以在线添加第三步生成的一个测试域名为例example.com

  配置推送机制,将该文件传送到各台DNS服务器的/var/cache/bind/目录上,管理机上执行

配置免密登录,便于使用scp命令传输文件,可写成一个脚本
cd /opt/dnscontrol

ssh-keygen
ssh-copy-id root@dns服务器ip -p ssh的端口

scp -P 22 zones/*.zone root@192.168.118.250:/var/cache/bind/

第一次传输是新增域名,故管理机上执行

rndc -s 192.168.118.250 addzone example.com '{type master; file "example.com.zone"; };'
rndc -s 192.168.118.250 reload example.com

后续都是修改解析记录场景,只需传输新zone文件后在管理机上执行

rndc -s 192.168.118.250 reload example.com

域名停止解析或者想删除,只需在管理机上执行

rndc -s 192.168.118.250 delzone example.com