Jan 25
kdig 是 knot-dns 中的类似 dig 的工具,使用 TLSv1.3 DoT 查询比起 dig 来要方便许多。静态编译它花了很多功夫,最终还是在 Tomatoware 作者的指导下才成功。
kdig 依赖:libedit, libunistring, gmp, nettle, gnutls,其中后三者 Tomatoware 都已自带,前两个需要下载源码编译。(注:Tomatoware 带的 gnutls 编译时未带证书,所以静态编译出来的 kdig 在运行时要指定 ca-file=/path/to/certfile,我个人为了偷懒是重新编译了带证书的 gnutls)
#libedit
./configure --prefix=/mmc/s --enable-shared=no
#libunistring
./configure --prefix=/mmc/s --enable-shared=no
#gnutls:
#opt
./configure --prefix=/mmc --enable-local-libopts --without-p11-kit --with-included-libtasn1 --enable-static --with-included-unistring --disable-doc --with-system-priority-file=/opt/etc/gnutls/default-priorities --with-default-trust-store-file=/opt/etc/ssl/certs/ca-certificates.crt --with-default-trust-store-dir=/opt/etc/ssl/certs --with-unbound-root-key-file=/opt/etc/unbound/root.key
/opt/etc/ssl/certs/ca-certificates.crt 这个证书文件可以通过 Entware 的 opkg install ca-bundle 来安装。
然后开始编译 knot-dns,我们只要其中的工具 kdig:
#knot-dns:
./configure --prefix=/opt --disable-daemon --disable-modules --disable-documentation --disable-fastparser --without-libidn
make V=99 LDFLAGS="-zmuldefs -all-static"
make V=99 用来查看编译中的详细信息,-zmuldefs 处理编译中出现的重复定义错误。
这个编译过程会出错停在开始编译 kdig 的时候,注意最后一句 gcc 相关的编译出错:
libtool: link: gcc -g -O2 -Wall -Werror=format-security -Werror=implicit -Wstrict-prototypes -zmuldefs -static -o kdig utils/kdig/kdig-kdig_exec.o utils/kdig/kdig-kdig_main.o utils/kdig/kdig-kdig_params.o ./.libs/libknotus.a /mmc/src/knot/knot-2.7.5/src/.libs/libknot.a -L/mmc/lib /mmc/src/knot/knot-2.7.5/src/.libs/libdnssec.a /mmc/lib/libedit.a -lncurses /mmc/lib/libgnutls.a -lnettle -lhogweed /mmc/lib/libgmp.a /mmc/lib/libintl.a /mmc/lib/libiconv.a -lc
复制下来,然后把外部调用的库用 -Wl,--whole-archive 和 -Wl,--no-whole-archive 包围起来,进入 src 目录手动编译:
cd src
gcc -g -O2 -Wall -Werror=format-security -Werror=implicit -Wstrict-prototypes -zmuldefs -static -o kdig utils/kdig/kdig-kdig_exec.o utils/kdig/kdig-kdig_main.o utils/kdig/kdig-kdig_params.o ./.libs/libknotus.a /mmc/src/knot/knot-2.7.5/src/.libs/libknot.a -L/mmc/lib /mmc/src/knot/knot-2.7.5/src/.libs/libdnssec.a -Wl,--whole-archive /mmc/lib/libedit.a -lncurses /mmc/lib/libgnutls.a -lnettle -lhogweed /mmc/lib/libgmp.a /mmc/lib/libintl.a /mmc/lib/libiconv.a -lc -Wl,--no-whole-archive
这样就可以编译出静态版本的 kdig 了:
file /opt/bin/kdig
/opt/bin/kdig: ELF 32-bit LSB executable, ARM, EABI5 version 1 (GNU/Linux), statically linked, no section header
测试:
kdig +tls -d @1.1.1.1 quakemachinex.com
;; DEBUG: Querying for owner(quakemachinex.com.), class(1), type(1), server(1.1.1.1), port(853), protocol(TCP)
;; DEBUG: TLS, received certificate hierarchy:
;; DEBUG: #1, C=US,ST=California,L=San Francisco,O=Cloudflare\, Inc.,CN=cloudflare-dns.com
;; DEBUG: SHA-256 PIN: TdBczz+YjD3Q/taSfHXL5n4LnRxzJk0WG0JAX7nRu6s=
;; DEBUG: #2, C=US,O=DigiCert Inc,CN=DigiCert ECC Secure Server CA
;; DEBUG: SHA-256 PIN: PZXN3lRAy+8tBKk2Ox6F7jIlnzr2Yzmwqc3JnyfXoCw=
;; DEBUG: TLS, skipping certificate PIN check
;; DEBUG: TLS, skipping certificate verification
;; TLS session (TLS1.3)-(ECDHE-SECP256R1)-(ECDSA-SECP256R1-SHA256)-(AES-256-GCM)
;; ->>HEADER<<- opcode: QUERY; status: NOERROR; id: 50261
;; Flags: qr rd ra; QUERY: 1; ANSWER: 1; AUTHORITY: 0; ADDITIONAL: 1
;; EDNS PSEUDOSECTION:
;; Version: 0; flags: ; UDP size: 1452 B; ext-rcode: NOERROR
;; PADDING: 402 B
;; QUESTION SECTION:
;; quakemachinex.com. IN A
;; ANSWER SECTION:
quakemachinex.com. 4 IN A 113.67.73.162
;; Received 468 B
;; Time 2019-01-25 08:57:44 CST
;; From 1.1.1.1@853(TCP) in 66.2 ms