歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux可執行文件的深入分析(二)

Linux可執行文件的深入分析(二)

日期:2017/2/25 10:38:41   编辑:Linux教程
 同樣的共享文件(.so)的前四個字節也是7F,45,4C,46

  readelf -h libtest.so

  ELF Header:

  Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00

  Class: ELF64

  Data: 2's complement, little endian

  Version: 1 (current)

  OS/ABI: UNIX - System V

  ABI Version: 0

  Type: DYN (Shared object file)

  Machine: Advanced Micro Devices X86-64

  Version: 0x1

  Entry point address: 0x490

  Start of program headers: 64 (bytes into file)

  Start of section headers: 3560 (bytes into file)

  Flags: 0x0

  Size of this header: 64 (bytes)

  Size of program headers: 56 (bytes)

  Number of program headers: 5

  Size of section headers: 64 (bytes)

  Number of section headers: 33

  Section header string table index: 30

  如何來區別ELF文件是否是可執行呢?答案是除了檢查魔術號以外還要檢查ELF文件頭以確定它是可執行的文件.

  在上面的例子中,只要觀察Type:即可,Type為EXEC (Executable file),即為可執行的ELF二進制程序.

  三)雜項文件

  這裡所指的雜項文件為Windows程序的二進制文件,或是JAVA二進制文件.

  我們通過配置BINFMT_MISC選項來擴展execve處理這種二進制文件.

  BINFMT_MISC在內核構件中指定,它允許超級用戶定義幫助應用程序,以使execve可以調用這些幫助應用程序來運行程序,所以應用程序調用execve時不需要知道將要執行的程序是否是內部linux文件.

  首先需要掛載一個專門的procfs入口

  mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc/

  ls -l

  total 0

  --w------- 1 root root 0 2010-09-20 11:01 register

  -rw-r--r-- 1 root root 0 2010-09-20 11:01 status

  其中register偽文件是為了向內核寫入新規則.

  而status入口允許啟用和禁用內核處理雜項文件,其中status默認為enable,表示啟用內核處理雜項文件

  向register偽文件寫入一個專門格式化的字符串,就可以添加新規則.格式中的多個字符由冒號隔開,如下:

  :name:type:offset:magic:mask:interpreter:flags

  解析:

  name域是任意的名稱,它會在binfmt_misc目錄下顯示.

  type域指識別類型(M表示魔數,E表示擴展).

  offset:可選的,魔數在文件中的起始偏移量.

  magic:以魔數或者以擴展名匹配的字節序列.

  mask:用來屏蔽掉string中的一些位的字符串.

  interpreter:程序解釋器的完整路徑名.

  flags:可選標志,控制必須怎樣調用解釋程序.

  例如加入如下的規則:

  echo ':Windows:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register

  加入新規則後,查看新binfmt_misc目錄

  ls -l /proc/sys/fs/binfmt_misc/

  total 0

  --w------- 1 root root 0 2010-09-20 17:40 register

  -rw-r--r-- 1 root root 0 2010-09-20 11:01 status

  -rw-r--r-- 1 root root 0 2010-09-20 17:40 Windows

  查看Windows規則

  more /proc/sys/fs/binfmt_misc/Windows

  enabled

  interpreter /usr/bin/wine

  flags:

  offset 0

  magic 4d5a

  禁用該規則,向文件寫入-1即可刪除它,例如:

  echo -1 > /proc/sys/fs/binfmt_misc/Windows

  此時Windows文件已經不在了,查看該目錄,如下:

  ls -l /proc/sys/fs/binfmt_misc/

  total 0

  --w------- 1 root root 0 2010-09-20 17:40 register

  -rw-r--r-- 1 root root 0 2010-09-20 11:01 status

Copyright © Linux教程網 All Rights Reserved