溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Hexdump怎么用

發(fā)布時間:2022-02-19 10:39:58 來源:億速云 閱讀:260 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“Hexdump怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Hexdump怎么用”這篇文章吧。

hexdump是Linux下的一個二進制文件查看工具,它可以將二進制文件轉(zhuǎn)換為ASCII、八進制、十進制、十六進制格式進行查看。

Hexdump怎么用

學(xué)習(xí)基本用法

Hexdump 讓你毫不費力地得到輸出結(jié)果,依你所查看文件的尺寸,輸出結(jié)果可能會非常多。本文中我們會創(chuàng)建一個 1×1 像素的 PNG 文件。你可以用圖像處理應(yīng)用如 GIMP 或 Mtpaint 來創(chuàng)建該文件,或者也可以在終端內(nèi)用 ImageMagick 創(chuàng)建。

用 ImagiMagick 生成 1×1 像素 PNG 文件的命令如下:

$ convert -size 1x1 canvas:black pixel.png

你可以用 file 命令確認此文件是 PNG 格式:

$ file pixel.png
pixel.png: PNG image data, 1 x 1, 1-bit grayscale, non-interlaced

你可能好奇 file 命令是如何判斷文件是什么類型。巧的是,那正是 hexdump 將要揭示的原理。眼下你可以用你常用的圖像查看軟件來看看你的單一像素圖片(它看上去就像這樣:.),或者你可以用 hexdump 查看文件內(nèi)部:

$ hexdump pixel.png
0000000 5089 474e 0a0d 0a1a 0000 0d00 4849 5244
0000010 0000 0100 0000 0100 0001 0000 3700 f96e
0000020 0024 0000 6704 4d41 0041 b100 0b8f 61fc
0000030 0005 0000 6320 5248 004d 7a00 0026 8000
0000040 0084 fa00 0000 8000 00e8 7500 0030 ea00
0000050 0060 3a00 0098 1700 9c70 51ba 003c 0000
0000060 6202 474b 0044 dd01 138a 00a4 0000 7407
0000070 4d49 0745 07e3 081a 3539 a487 46b0 0000
0000080 0a00 4449 5441 d708 6063 0000 0200 0100
0000090 21e2 33bc 0000 2500 4574 7458 6164 6574
00000a0 633a 6572 7461 0065 3032 3931 302d 2d37
00000b0 3532 3254 3a30 3735 353a 2b33 3231 303a
00000c0 ac30 5dcd 00c1 0000 7425 5845 6474 7461
00000d0 3a65 6f6d 6964 7966 3200 3130 2d39 3730
00000e0 322d 5435 3032 353a 3a37 3335 312b 3a32
00000f0 3030 90dd 7de5 0000 0000 4549 444e 42ae
0000100 8260
0000102

透過一個你以前可能從未用過的視角,你所見的是該示例 PNG 文件的內(nèi)容。它和你在圖像查看軟件中看到的是完全一樣的數(shù)據(jù),只是用一種你或許不熟悉的方式編碼。

提取熟悉的字符串

盡管默認的數(shù)據(jù)輸出結(jié)果看上去毫無意義,那并不意味著其中沒有有價值的信息。你可以用 –canonical 選項將輸出結(jié)果,或至少是其中可翻譯的部分,翻譯成更加熟悉的字符集:

$ hexdump --canonical foo.png
00000000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 |.PNG........IHDR|
00000010 00 00 00 01 00 00 00 01 01 00 00 00 00 37 6e f9 |.............7n.|
00000020 24 00 00 00 04 67 41 4d 41 00 00 b1 8f 0b fc 61 |$....gAMA......a|
00000030 05 00 00 00 20 63 48 52 4d 00 00 7a 26 00 00 80 |.... cHRM..z&...|
00000040 84 00 00 fa 00 00 00 80 e8 00 00 75 30 00 00 ea |...........u0...|
00000050 60 00 00 3a 98 00 00 17 70 9c ba 51 3c 00 00 00 |`..:....p..Qcd 5d c1 00 00 00 25 74 45 58 74 64 61 74 |0..]....%tEXtdat|
000000d0 65 3a 6d 6f 64 69 66 79 00 32 30 31 39 2d 30 37 |e:modify.2019-07|
000000e0 2d 32 35 54 32 30 3a 35 37 3a 35 33 2b 31 32 3a |-25T20:57:53+12:|
000000f0 30 30 dd 90 e5 7d 00 00 00 00 49 45 4e 44 ae 42 |00...}....IEND.B|
00000100 60 82 |`.|
00000102

在右側(cè)的列中,你看到的是和左側(cè)一樣的數(shù)據(jù),但是以 ASCII 碼展現(xiàn)的。如果你仔細看,你可以從中挑選出一些有用的信息,如文件格式(PNG)以及文件創(chuàng)建、修改日期和時間(向文件底部尋找一下)。 file 命令通過頭 8 個字節(jié)獲取文件類型。程序員會參考 libpng 規(guī)范 來知曉需要查看什么。具體而言,那就是你能在該圖像文件的頭 8 個字節(jié)中看到的字符串 PNG。這個事實顯而易見,因為它揭示了 file 命令是如何知道要報告的文件類型。 你也可以控制 hexdump 顯示多少字節(jié),這在處理大于一個像素的文件時很實用:

$ hexdump --length 8 pixel.png
0000000 5089 474e 0a0d 0a1a
0000008

hexdump 不只限于查看 PNG 或圖像文件。你也可以用 hexdump 查看你日常使用的二進制文件,如 ls、rsync,或你想檢查的任何二進制文件。

用 hexdump 實現(xiàn) cat 命令

閱讀 PNG 規(guī)范的時候你可能會注意到頭 8 個字節(jié)中的數(shù)據(jù)與 hexdump 提供的結(jié)果看上去不一樣。實際上,那是一樣的數(shù)據(jù),但以一種不同的轉(zhuǎn)換方式展現(xiàn)出來。所以 hexdump 的輸出是正確的,但取決于你在尋找的信息,其輸出結(jié)果對你而言不總是直接了當(dāng)?shù)摹3鲇谶@個原因,hexdump 有一些選項可供用于定義格式和轉(zhuǎn)化其轉(zhuǎn)儲的原始數(shù)據(jù)。 轉(zhuǎn)換選項可以很復(fù)雜,所以用無關(guān)緊要的東西練習(xí)會比較實用。下面這個簡易的介紹,通過重新實現(xiàn) cat 命令來演示如何格式化 hexdump 的輸出。首先,對一個文本文件運行 hexdump 來查看其原始數(shù)據(jù)。通常你可以在硬盤上某處找到 GNU 通用許可證GNU General Public License(GPL)的一份拷貝,也可以用你手頭的任何文本文件。你的輸出結(jié)果可能不同,但下面是如何在你的系統(tǒng)中找到一份 GPL(或至少其部分)的拷貝:

$ find /usr/share/doc/ -type f -name "COPYING" | tail -1
/usr/share/doc/libblkid-devel/COPYING

對其運行hexdump:

$ hexdump /usr/share/doc/libblkid-devel/COPYING
0000000 6854 7369 6c20 6269 6172 7972 6920 2073
0000010 7266 6565 7320 666f 7774 7261 3b65 7920
0000020 756f 6320 6e61 7220 6465 7369 7274 6269
0000030 7475 2065 7469 6120 646e 6f2f 0a72 6f6d
0000040 6964 7966 6920 2074 6e75 6564 2072 6874
0000050 2065 6574 6d72 2073 666f 7420 6568 4720
0000060 554e 4c20 7365 6573 2072 6547 656e 6172
0000070 206c 7550 6c62 6369 4c0a 6369 6e65 6573
0000080 6120 2073 7570 6c62 7369 6568 2064 7962
[...]

如果該文件輸出結(jié)果很長,用 –length(或短選項 -n)來控制輸出長度使其易于管理。 原始數(shù)據(jù)對你而言可能沒什么意義,但你已經(jīng)知道如何將其轉(zhuǎn)換為 ASCII 碼:

hexdump --canonical /usr/share/doc/libblkid-devel/COPYING
00000000  54 68 69 73 20 6c 69 62  72 61 72 79 20 69 73 20  |This library is |
00000010  66 72 65 65 20 73 6f 66  74 77 61 72 65 3b 20 79  |free software; y|
00000020  6f 75 20 63 61 6e 20 72  65 64 69 73 74 72 69 62  |ou can redistrib|
00000030  75 74 65 20 69 74 20 61  6e 64 2f 6f 72 0a 6d 6f  |ute it and/or.mo|
00000040  64 69 66 79 20 69 74 20  75 6e 64 65 72 20 74 68  |dify it under th|
00000050  65 20 74 65 72 6d 73 20  6f 66 20 74 68 65 20 47  |e terms of the G|
00000060  4e 55 20 4c 65 73 73 65  72 20 47 65 6e 65 72 61  |NU Lesser Genera|
00000070  6c 20 50 75 62 6c 69 63  0a 4c 69 63 65 6e 73 65  |l Public.License|
[...]

這個輸出結(jié)果有幫助但太累贅且難于閱讀。要將 hexdump 的輸出結(jié)果轉(zhuǎn)換為其選項不支持的其他格式,可組合使用 –format(或 -e)和專門的格式代碼。用來自定義格式的代碼和 printf 命令使用的類似,所以如果你熟悉 printf 語句,你可能會覺得 hexdump 自定義格式不難學(xué)會。 在 hexdump 中,字符串 %_p 告訴 hexdump 用你系統(tǒng)的默認字符集輸出字符。–format 選項的所有格式符號必須以單引號包括起來:

$ hexdump -e'"%_p"' /usr/share/doc/libblkid-devel/COPYING
This library is fre*
software; you can redistribute it and/or.modify it under the terms of the GNU Les*
er General Public.License as published by the Fre*
Software Foundation; either.version 2.1 of the License, or (at your option) any later.version..*
The complete text of the license is available in the..*
/Documentation/licenses/COPYING.LGPL-2.1-or-later file..

這次的輸出好些了,但依然不方便閱讀。傳統(tǒng)上 UNIX 文本文件假定 80 個字符的輸出寬度(因為很久以前顯示器一行只能顯示 80 個字符)。

盡管這個輸出結(jié)果未被自定義格式限制輸出寬度,你可以用附加選項強制 hexdump 一次處理 80 字節(jié)。具體而言,通過 80 除以 1 這種形式,你可以告訴 hexdump將 80 字節(jié)作為一個單元對待:

$ hexdump -e'80/1 "%_p"' /usr/share/doc/libblkid-devel/COPYING
This library is free software; you can redistribute it and/or.modify it under the terms of the GNU Lesser General Public.License as published by the Free Software Foundation; either.version 2.1 of the License, or (at your option) any later.version...The complete text of the license is available in the.../Documentation/licenses/COPYING.LGPL-2.1-or-later file..

現(xiàn)在該文件被分割成 80 字節(jié)的塊處理,但沒有任何換行。你可以用 \n 字符自行添加換行,在 UNIX 中它代表換行:

$ hexdump -e'80/1 "%_p""\n"'This library is free software; you can redistribute it and/or.modify it under th
e terms of the GNU Lesser General Public.License as published by the Free Softwa
re Foundation; either.version 2.1 of the License, or (at your option) any later.
version...The complete text of the license is available in the.../Documentation/
licenses/COPYING.LGPL-2.1-or-later file..

現(xiàn)在你已經(jīng)(大致上)用 hexdump 自定義格式實現(xiàn)了 cat 命令。 控制輸出結(jié)果 實際上自定義格式是讓 hexdump 變得有用的方法?,F(xiàn)在你已經(jīng)(至少是原則上)熟悉 hexdump 自定義格式,你可以讓 hexdump -n 8 的輸出結(jié)果跟 libpng 官方規(guī)范中描述的 PNG 文件頭相匹配了。

首先,你知道你希望 hexdump 以 8 字節(jié)的塊來處理 PNG 文件。此外,你可能通過識別這些整數(shù)從而知道 PNG 格式規(guī)范是以十進制數(shù)表述的,根據(jù) hexdump 文檔,十進制用 %d 來表示:

$ hexdump -n8 -e'8/1 "%d""\n"' pixel.png
13780787113102610

你可以在每個整數(shù)后面加個空格使輸出結(jié)果變得完美:

$ hexdump -n8 -e'8/1 "%d ""\n"' pixel.png
137 80 78 71 13 10 26 10

現(xiàn)在輸出結(jié)果跟 PNG 規(guī)范完美匹配了。

以上是“Hexdump怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI