1. 程式人生 > >nslookup在靜態編譯的busybox上如何正常解析域名

nslookup在靜態編譯的busybox上如何正常解析域名

busybox是自制簡易系統必備的工具集了,包含了coreutils等各種系統小工具。在自己編譯的核心上執行busybox就要靜態編譯它。以前一直遇到busybox靜態編譯後放在自制系統執行nslookup ping wget解析不了域名的問題,也一直懶,沒有去解決。這次又遇到了這個問題,於是花點時間把它解決吧。

busybox靜態編譯後的nslookup

下載了glibc-static後對busybox編譯:make menuconfig,勾選Static Linkmake。其中的build log當然不會注意,樂呵呵使用busybox nslookup blog.csdn.net

,輸出Can't resolve "blog.csdn.net"。這個問題困擾很久了,今天終於有時間來看一看究竟。

難道是因為系統網路配置的不對?ping 172.16.65.1閘道器是通路。開始抓瞎亂操作,比如開busybox dnsd,編譯ip_relay172.16.65.1:53對映到本機127.0.0.1:53;全都沒有效果。於是Google這個問題,基本都是在問docker的busybox。在編譯Busybox的Linux機器上執行busybox nslookup blog.csdn.net,竟然是work的!

下載strace原始碼,靜態編譯。先在編譯機上試執行strace busybox nslookup blog.csdn.net

,把log看一看,它先讀了/etc/resolv.conf,又讀了/etc/nsswitch.conf,還有/etc/host.conf;照搬把這些檔案全在目標機器上建立好,nslookup顯示server倒是隨著resolve.conf的nameserver改變而改變了,可是不管設定什麼server,都不能解析域名,114.114.114.114也如此。

再回到strace的log,發現它一樣載入了很多庫,比如libdl.so libnss.so。那busybox執行在目標機器上會做什麼?隨意把靜態編譯的strace複製到目標機器,strace busybox nslookup blog.csdn.net

,好了,問題來了:目標機器上一樣要載入libdl.so等庫,而且在目標機器上都是No such file的錯誤,所以最後輸出了不能解析域名。

網上搜索了libnss是什麼樣的庫,給出的答案是設計就是動態庫,不建議靜態編譯。要靜態編譯,請先重新編譯glibc,把–enable-libnss-static放上。這就比較煩了,用busybox就是衝著它dependency少,glibc又是一堆依賴,關鍵是編譯出來佔蠻大空間的。有沒有其他方案?

域名解析

因為編譯了node,就想看看有沒有純javascript的包支援域名解析?結果沒有,還發現了npm也要用getaddrinfo進行域名解析。這個函式是libnss裡的一個API。能繞過它麼?既然網路連得通,那為什麼不可以直接發包給dns伺服器呢?於是下載了Wireshark抓包,網上找找DNS協議的說明。發現了:
http://www.binarytides.com/dns-query-code-in-c-with-linux-sockets/
這篇用C程式碼手動發DNS請求得到ip的文章。先用JavaScript寫了一遍,準備編譯到node裡讓npm工作起來,發現node靜態編譯後執行npm會segment falut,暫時擺一邊。於是先還是解決busybox的域名解析。grep getaddrinfo,發現三個檔案在用nslookup.c xconnect.c inet_common.c,其中inet_common.c用它解析ipv6,這個就暫時不管了。對於nslookup和xcoonect(ping wget都呼叫這裡),應該把系統的getaddrinfo函式覆蓋掉就好了吧,所以準備了一個getaddrinfo.h 把getaddrinfo和freeaddrinfo重寫一遍,用直接傳送UDP包的形式。make編譯busybox,然後在目標機器上執行:busybox nslookup blog.csdn.net

這裡寫圖片描述

好了,編譯完成。順便把node那個fully-static編譯後的DNS問題也解決了吧 :)

Translation:

Busybox is a power tool set including coreutils; it is very useful for self compiled Linux distribution. For using it on a Linux kernel without glibc support, it is required static link to compile Busybox. One issue always occurring is that nslookup ping wget applets do not work for url whose host name is not ip after static link. This time my build is stuck again for the problem. And also this time, I decide to totally resolve it.

nslookup in static linked Busybox

Installing glibc static support bundles, it is easy to compile Busybox with static link option checked: make menuconfig; make. Actually there is some static link warning in build process and I noticed after resolve the problem. After compiling it is happy to run busybox nslookup www.google.com, however the output is sad - Can't resolve "www.google.com". What a bug!

Is there any problem on the system network configuration? Let me check. ping 172.16.65.1 the gateway is available; meanwhile my target machine has ip at 172.16.65.101. I am lost in the sea and do trials and errors: run busybox dnsd; compile ip_relay and map 127.0.0.1:53 to 172,16.65.1:53; etc. No luck at all. I also tried to Google the problem and no exact answer, most people reported busybox nslookup problem is for busybox running by docker in container. Wait! Let me try static linked Busybox on build machine: busybox nslookup www.google.com, it works!!!

I believe the program trigger some thing in backend and connect to DNS server. I download and compile static linked (it is possible to move strace to my target machine conveniently in future) strace to look insight. Read the trace log and find that the program touched some files like /etc/resolv.conf /etc/nsswitch.conf /etc/host.conf. I set up them on the target machine as the same on the build machine. nslookup changes DNS server when resolve.conf changes. Somehow it cannot resolve domain name even using 8.8.8.8.

Back to the strace log and I find there are many shared libraries loaded, for example libdl.so libnss.so. What Busybox do internally on the target machine? For static linked strace, it is easy to move it to the target machine and run: strace busybox nslookup www.google.com. Good, hit the problem: there are many shared libraries needed to be loaded; however, they does not exist on the target machine so that the log shows many No such file error. Naturally it print out Can't resolve "xxx".

Searching for libnss on Internet, I got the answer that it is designed to be a shared library set; it is not recommended to link statically. If really want, recompile glibc with --enable-libnss-static. How terrible! Lazy people want to sleep. I do not want glibc. Any idea?

DNS resolver

Actually I compile NodeJS and link it fully statically and want to use pure JavaScript package to support DNS resolving. Nothing found and get another problem: npm install uses getaddrinfo to resolve domain name and the function is dynamically linked to libnss API. Let’s find a way to bypass the dynamic link. Then an idea occurs. All we can get network available, why not send DNS request to servers directly? To understand DNS process in network, I download and install Wireshark to sniff network traffic and read a nice introduction at:
http://www.binarytides.com/dns-query-code-in-c-with-linux-sockets/
It is an article with some C code to send DNS request to server for IP address directly. At first I would like to use the method to make node npm work. After writing and testing simple JavaScript DNS module, I find that npm install seems always segment fault after fully static link build. Forget it and focus on Busybox. grep getaddrinfo and find that there are 3 files in Busybox source code: nslookup.c xconnect.c (ping wget), inet_common.c. For inet_common.c, it uses getaddrinfo to resolve ipv6. I would not like to make things complex at beginning. Thus let me write new getaddrinfo and freeaddrinfo for nslookup.c and xconnect.c to make DNS resolving work: getaddrinfo.h make to compile Busybox and run it on the target machine. Wow, it works finally! Below is a screenshot for busybox nslookup blog.csdn.net

這裡寫圖片描述

Cool, mission complete. Go to resolve NodeJS DNS resolving problem in fully static link build : ) It is also clear now.

相關推薦

nslookup靜態編譯busybox如何正常解析域名

busybox是自制簡易系統必備的工具集了,包含了coreutils等各種系統小工具。在自己編譯的核心上執行busybox就要靜態編譯它。以前一直遇到busybox靜態編譯後放在自制系統執行nslookup ping wget解析不了域名的問題,也一直懶,沒有去

怎樣一鍵檢測域名或url是否正常解析並傳輸

img 看到了 err 地方 eal usr 圖片 交互式 成交 好久沒有寫博客了,現在重新拾起來,我想還不算晚。工作中,經常會有人抱怨:網站好卡,dns是不是有問題...... 有沒有一鍵的方式了解到是dns解析的問題還是網絡傳輸導致的嗎?有。可以寫一些腳本檢查。shel

Windows 靜態編譯 Libevent 2.0.10 並實現一個簡單 HTTP 伺服器

      假設 Visual Studio 2005 的安裝路徑為“D:\Program Files\Microsoft Visual Studio 8\”,Libevent 2.0.10 解壓後的路徑為“D:\libevent-2.0.10-stable”。 編譯生成L

在虛擬機器安裝了Ubuntu然後sudo apt-get update 無法解析域名

錯誤 http://security.ubuntu.com trusty-security InRelease錯誤 http://security.ubuntu.com trusty-security

阿里雲如何解析二級域名

第一步:登入阿里雲,開啟左邊欄目裡產品與服務裡的域名。找到域名列表,點選有效的域名解析,進入解析設定。 第二步:點選左上紅色的新增解析,比如這裡的頂級域名是www.tjgbank.cn,假如你想開一個直播,名字是“look”,那二級域名就是:look.tjgbank.cn

重新編譯Nginx指導手冊【修復靜態編譯Openssl的Nginx漏洞 】(轉)

snippets asset 替換 業務需求 tps eight nbsp 出了 sof 1. 概述 當前爆出了Openssl漏洞,會泄露隱私信息,涉及的機器較多,環境迥異,導致修復方案都有所不同。不少服務器使用的Nginx,是靜態編譯opensssl,直接將open

apache動靜態編譯

apache 動靜態編譯為了使Apache支持https訪問,系統需要安有apache、openssl、mod_ssl.so1、安裝openssl:基本上系統都已經安裝了,在/usr/bin/openssl下,直接使用openssl命令即可;如果系統未安裝,則下載openssl進行安裝。2、安裝mod_ssl

【路由達人】簡單兩步搞定小米路由新增功能-DDNS(解析域名地址轉向在線工具)

固定ip cells hot ddn mic width cin spa family DDNS(Dynamic Domain Name Server)是動態域名服務的縮寫! 簡單來說目前ISP大多為我們提供動態IP(如ADSL撥號上網),而很多設備或服務需要通過遠

nginx泛解析域名實現多級域名多個域名跳轉

nginx 二級域名 需求如下:由於之前泛解析有上百個域名需要向上圖所示的這種需求,所以需要一個通用方法進行處理。思路如下:1,首先通過Nginx獲取到所有泛解析的二級子域名2,通過Nginx的 proxy_set_header 自定義變量獲取請求url主機頭部3,後端使用程序獲取Nginx傳遞過來的

VC++編譯lua庫 無法解析lua符號問題

dsm div number blog reg gist html name dpa VC編譯lua庫 出現錯誤: 錯誤 36 error LNK1120: 21 個無法解析的外部命令 C:\Users\luozhuang\Desktop\onscripter

Qt creator 編譯錯誤:無法解析的外部符號(命令)

log void virtual win idg track deb dia popu 問題來自於:僅僅是在creator 中加入了一個新的DIalog類,並在main(),中實例化並show。就出現例如以下的錯誤: main.obj:-1: error: L

dns解析域名過程

dns1.在瀏覽器中輸入 www.xiaoahehe.com 域名,瀏覽器會先檢查自己本地的hosts文件是否有這個網址映射關系,如果有,就先調用這個IP地址映射,完成域名解析2.如果hosts裏沒有這個域名的映射,瀏覽器則查找本地DNS(以下稱LDNS)解析器緩存,是否有這個網址映射關系,如果有,直接返

利用nginx解析域名到服務器指定服務端口

-s lis 技術分享 _for 服務器 .html 文件路徑 修改 技術 進入nginx配置文件路徑 cd /usr/local/nginx/conf/ 修改 文件 nginx.conf 內容如下 upstream tomcat8080_api {

vs2012+Qt5.3.1,xp系統無法運行,靜態編譯失敗

ips use left ucd rdl wpf one amp oai 8w剄19PFV囤衛5http://huiyi.docin.com/ogudb55786 kw覆97X技蘊1倜A澈慫http://shequ.docin.com/sina_5850645250 0

靜態編譯和動態編譯(lib和dll)

weibo docs p s 獎章 com 動態編譯 lan doc sin u2瓢剮JZP匪媳51http://www.docin.com/app/user/userinfo?userid=179185213 0宰9U拔7853E5噸渭3http://www.docin

Nginx動態解析域名方案?

nginx動態解析域名方案應用場景:由於有時候nginx代理的時候,第三方域名對應的ip可能發生變化,然而沒有提前通知,然而如果不配置什麽,nginx又不能智能解析,因此nginx動態解析域名就比較重要,這裏拿nignx來說,根據公司應用場景,分享一下,感謝公司可以讓自己了解更多,更全面的東西。一、使用模塊n

(轉)Linux命令:使用dig命令解析域名

要求 改名 標識 udp mman aid blog 大小 叠代查詢 Linux命令:使用dig命令解析域名 Linux下解析域名除了使用nslookup之外,開可以使用dig命令來解析域名,dig命令可以得到更多的域名信息。 dig的全稱是 (domain informa

Qt靜態編譯發布和動態編譯發布

打包 mage 5.6 動態編譯 完成 工具 開始 選擇 ref 靜態編譯發布 你寫了一個小型Qt程序,發布的時候不想要一大堆dll文件,就只想打包成一個exe文件,那麽就需要用到靜態編譯。 下面的教程就是Qt靜態編譯環境配置 Qt5.6靜態編譯包下載地址 1.下載Qt

修復OpenSSL漏洞 升級OpenSSL版本 nginx靜態編譯ssl模塊

nginx openssl背景 OpenSSL全稱為Secure Socket Layer,是Netscape所研發,利用數據加密(Encryption)作技術保障在Internet上數據傳輸的安全。可確保數據在網絡上的傳輸不會被竊聽及截取。 當然,OpenSSL是一個強大的密碼庫,我們在

靜態文件cdn自解析生成相對路徑

.com 直接 失敗 目的 外文 進行 獲得 訪問 及其 一、場景和目標: 用戶上傳一個包含 index.html 的靜態資源壓縮包,資源內所有文件都是相互依賴的,不需要用戶對內部文件內容做任何特殊處理,僅通過服務端邏輯處理達到用戶訪問 http://xxx.xx