DNS

域名系统(Domain Name System, DNS)是一种分布式目录服务系统,负责域名和IP地址的相互映射,是整个互联网的核心服务之一。

域名(Domain Name)

IP地址能够唯一地标记网络内的设备,但IP地址是一长串数字,不直观难记忆。域名就是为了解决这个问题而发明的。

互联网名称与数字地址分配机构(Internet Corporation for Assigned Names and Numbers,ICANN)负责管理域名空间。 ICANN自身难以管理庞大的域名空间,所以他们会对域名空间进行划分,然后交给不同的代理机构进行管理。如Verisign ICANN主要负责顶级域名的管理。

# 域名 www.example.com 和对应的 IPV4/IPV6 地址
www.example.com -> 93.184.216.34/2606:2800:220:1:248:1893:25c8:1946
# 使用 dig 命令查询DNS服务器
# dig 是使用最广泛的DNS服务器系统-Berkeley Internet Name Domain (BIND)提供的工具
▶ dig www.feedpanda.cn

; <<>> DiG 9.11.3-1ubuntu1.12-Ubuntu <<>> www.feedpanda.cn
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 14592
;; flags: qr rd ad; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;www.feedpanda.cn.              IN      A

;; ANSWER SECTION:
www.feedpanda.cn.       0       IN      CNAME   xenodochial-wright-9df02f.netlify.app.
xenodochial-wright-9df02f.netlify.app. 0 IN A   178.128.17.49

;; Query time: 115 msec
;; SERVER: 172.19.224.1#53(172.19.224.1)
;; WHEN: Tue May 26 18:17:49 CST 2020
;; MSG SIZE  rcvd: 154

# 使用 nslookup 命令查询DNS服务器
▶ nslookup www.google.com
Server:         172.19.224.1
Address:        172.19.224.1#53

Non-authoritative answer:
Name:   www.google.com
Address: 66.220.147.47
Name:   www.google.com
Address: 2001::6ca0:ad01

域名构成(Domain Anatomy)

域名是由点号分隔的一串字符串

domain name structure

以gmail域名为例

mail.google.com
  • The right-most label conveys the top-level domain; for example, the domain name www.example.com belongs to the top-level domain com.
  • The hierarchy of domains descends from the right to the left label in the name; each label to the left specifies a subdivision, or subdomain of the domain to the right. For example: the label example specifies a node example.com as a subdomain of the com domain, and www is a label to create www.example.com, a subdomain of example.com. Each label may contain from 1 to 63 octets. The empty label is reserved for the root node and when fully qualified is expressed as the empty label terminated by a dot. The full domain name may not exceed a total length of 253 ASCII characters in its textual representation.[6] Thus, when using a single character per label, the limit is 127 levels: 127 characters plus 126 dots have a total length of 253. In practice, some domain registries may have shorter limits.
  • A hostname is a domain name that has at least one associated IP address. For example, the domain names www.example.com and example.com are also hostnames, whereas the com domain is not. However, other top-level domains, particularly country code top-level domains, may indeed have an IP address, and if so, they are also hostnames. Hostnames impose restrictions on the characters allowed in the corresponding domain name. A valid hostname is also a valid domain name, but a valid domain name may not necessarily be valid as a hostname.

域名层级(Domain Hierarchy)

Domain-Hierachy

域名解析(Domain Name Resolve)

通过域名获取对应服务器地址的过程称为域名解析

Domain Name Resolve

DNS记录(DNS Record)

DNS相关信息以DNS记录的形式保存在DNS服务器。

常见DNS记录类型

  • A记录(A Record,A = Address) 记录域名对应的32位 IPv4 地址

  • AAAA记录(AAAA Record) 128位 IPv6 地址

  • CNAME记录(Canonical Name Record) 关联域名别名和真实域名

CNAME记录保存了域名别名和真实域名的映射关系,通过CNAME记录可以得到真实域名。比如部署到github pages 或 netlify等平台的网站, 配置自定义域名时就需要在DNS解析记录里添加CNAME记录,将自定义域名映射到github pages或netlify默认提供的域名。

  • MX记录(Mail exchange record) 配合SMTP(Simple Mail Transfer Protocol)负责邮件消息的路由

  • NS记录(Name Server record) 记录权威域名服务器地址

  • PTR记录(Pointer record) 记录IP地址对应的域名(和A记录相反),DNS逆向解析(通过IP查域名)会用到PTR记录

  • SRV记录(Service Location Record) 记录某些服务(如VoIP)的主机地址和端口

  • TXT记录(Text Record)记录一些备注信息用于验证域名所有人身份等

  • SOA记录(Start of [a zone of] Authority Record) 记录域名管理员的邮件地址、域名更新时间等信息

完整列表参见 https://en.wikipedia.org/wiki/List_of_DNS_record_types

分区文件(Zone File)

域名空间被划分成多个区域(Zone),每个区域是一部分连续的域名空间。ICAAN将不同区域代理给不同的机构进行管理。

分区文件用于描述域名分区,域名信息以DNS记录的形式保存在分区文件中,分区文件是一个文本文件。

分区文件示例:

$ORIGIN example.com.     ; designates the start of this zone file in the namespace
$TTL 1h                  ; default expiration time of all resource records without their own TTL value
example.com.  IN  SOA   ns.example.com. username.example.com. ( 2007120710 1d 2h 4w 1h )
example.com.  IN  NS    ns                    ; ns.example.com is a nameserver for example.com
@             IN  MX    20 mail2.example.com. ; equivalent to above line, "@" represents zone origin
@             IN  MX    50 mail3              ; equivalent to above line, but using a relative host name
example.com.  IN  A     192.0.2.1             ; IPv4 address for example.com
              IN  AAAA  2001:db8:10::1        ; IPv6 address for example.com
ns            IN  A     192.0.2.2             ; IPv4 address for ns.example.com
              IN  AAAA  2001:db8:10::2        ; IPv6 address for ns.example.com
www           IN  CNAME example.com.          ; www.example.com is an alias for example.com
mail          IN  A     192.0.2.3             ; IPv4 address for mail.example.com

处理分区文件形式,域名信息也可以保存在数据库系统中。

详情参见: https://en.wikipedia.org/wiki/DNS_zone

域名服务器类型

  • 递归DNS解析服务器(Recursive DNS Resolver)

通常由网络服务商(ISP)提供,谷歌、Cloudfare、阿里云等公司也提供公共DNS解析服务器。 递归解析服务器负责直接处理客户端(如浏览器)的DNS查询请求,DNS 递归解析服务器会根据实际情况再去请求其他类型的域名服务器获取信息来完成客户端的DNS查询请求。

  • 根域名服务器(Root Name Server)

管理根域名信息的服务器,根域名服务器由ICANN负责管理。全球目前共13个根域名服务(数百台服务器)。

  • 顶级域名服务器(Top Level Domain Name Server)

管理顶级域名信息的服务器,顶级域名服务器由ICANN的分支机构IANA(Internet Assigned Numbers Authority)管理。

  • 权威域名服务器(Authorative Name Server)

通常是域名注册商所设置的 DNS 服务器,用于管理具体域名(例如 google.com、mail.google.com)的记录。

域名解析过程

在不考虑缓存的情况下域名解析包含以下几个步骤:

  • 终端用户在浏览器地址栏访问某个网站如 example.com 触发HTTP请求,浏览器需要知道域名对应的IP地址才能完成HTTP请求,所以浏览器先向递归解析服务器发起请求获取DNS记录。
  • 递归解析服务器收到浏览器请求后向根域名服务器发起请求,获取到顶级域名(此处为.com)服务器地址
  • 递归解析服务器接着请求顶级域名服务器,获取到权威服务器地址
  • 最后,递归服务器请求权威域名服务器获取到DNS记录并返回给浏览器

DNS-Lookup

DNS请求分为3种:

  • 递归查询 客户端向递归解析服务器请求DNS信息是一个递归查询的过程
  • 迭代查询 递归服务器向根域名服务器、顶级域名服务器、权威服务器发起DNS查询请求,是一个迭代查询的过程。
  • 非递归查询 直接请求权威服务器获取DNS记录或者请求递归解析服务器时递归解析服务器存在DNS记录缓存直接返回缓存的DNS记录

为了减少不必要的请求、减轻服务器压力以及节省网络带宽通常浏览器、操作系统和DNS服务器都会对进行DNS记录进行缓存, 缓存时长遵循DNS记录的TTL(time to live)

DNS安全问题

DNS请求主要使用UDP协议(不需要像TCP协议那样进行3次握手、无连接、头部开销小),某些情况下也会使用TCP协议。 但使用UDP协议的请求容易受到中间人攻击和DDoS攻击,所以DNS也就存在一些安全问题。

DNS劫持(DNS hijacking)

将DNS请求重定向到目标服务器以外的其他服务器,如运营商劫持

dnsD

DNS欺骗(DNS Spoofing)

也叫DNS 缓存投毒(DNS Cache Poisoning)

因为DNS服务器会对DNS记录进行缓存,所以通过伪造DNS请求响应来污染缓存。这样用户就会请求到伪造的DNS记录,访问到假的服务器。

DNS amplification

DNS amplification attack

DNSSEC

DNS安全扩展(Domain Name System Security Extensions, DNSSEC),通过给DNS记录添加数字签名来确保安全。

https://www.cloudflare.com/dns/dnssec/how-dnssec-works/

DNS over HTTPS

HTTPS协议可以保证用户的请求内容不被窥视,但是通过DNS请求还是可以知道用户请求了什么网站。 为了更好的保护用户隐私,产生了DNS Over HTTPS。当然通过HTTPS协议进行DNS解析也可以起到和使用DNSSEC一样的增强安全性的效果。

参考资料