使用strings查看二進制文件中的字符串

使用strings查看二進制文件中的字符串 html

今天介紹的這個小工具叫作strings,它實現功能很簡單,就是找出文件內容中的可打印字符串。所謂可打印字符串的涵義是,它的組成部分都是可打印字符,而且以null或者newline結尾。linux

對於普通文本文件來講,strings沒有任何意義,由於文本文件中的任何內容實際都是可打印的字符串。strings最經常使用的場合就是列出動態庫或者可執行程序等二進制文件中出現的字符串,結合grep便可實現查找。bash

strings的使用方法很簡單,strings [文件]便可,它會默認輸出長度大於4的字符串,每行一個。
此外它還有幾個參數。編輯器

-n number 僅輸出長度大於number的字符串
-t d/o/x 除了字符串以外,還額外輸出字符串的位置(十進制/八進制/十六進制)工具

下面我用C語言寫一個很簡單的hello world程序:ui

#include 
 
int main()
{
    printf("hello world\n");
    return 0;
}

編譯過程當中,hello world做爲一個靜態字符串會保存在全局數據區。使用strings查看,便可看到」hello world」這個字符串:.net

[leconte@localhost test]$ strings test
/lib/ld-linux.so.2
__gmon_start__
libc.so.6
_IO_stdin_used
puts
__libc_start_main
GLIBC_2.0
PTRh
[^_]
world world

除了hello world以外還有許多字符串,這些都是二進制文件test裏包含的,它們都是編譯,連接期間從glibc等動態庫或其餘地方引入的。unix

更爲過度的是,咱們能夠用sed等編輯器對字符串進行修改:code

[leconte@localhost test]$ gcc -g -o test a.c
[leconte@localhost test]$ ./test
hello world
[leconte@localhost test]$ sed -i 's/hello world/linuxers.cn/g' test
[leconte@localhost test]$ ./test
linuxers.cn

可見我將hello world改爲了linuxers.cn,執行test後輸出已經變成了linuxers.cn,咱們成功修改了可執行程序。可是這種修改必須很當心,一般字符串的長度在修改先後必須嚴格相等,不然頗有可能產生段錯誤,由於畢竟咱們修改的是數據而非代碼邏輯。假如我像下面這樣修改,執行後就會段錯誤:htm

[leconte@localhost test]$ sed -i 's/linuxers.cn/linux/g' test
[leconte@localhost test]$ ./test
段錯誤
相關文章
相關標籤/搜索