[Linux] 用 pev 工具來檢查 Windows PE 檔案是否有開啟 DEP/ASLR

[Linux] 用 pev 工具來檢查 Windows PE 檔案是否有開啟 DEP/ASLR

公司最近要開始推行強制 Windows PE 檔案的 DEP/ASLR 設定,

DEP (Data Execution Prevention) 用來減低 buffer overrun 造成的危害,

ASLR (Address Space Load Randomization) 則是增加攻擊者利用 buffer overrun 的難度~

之後如果沒有開的專案,就沒辦法透過公司的 CI 編譯出執行檔~

 

看了一下,在 Windows 上他們會用 dumpbin.exe 來檢查,

不過我平常的工作平台是 Mac/Linux,自然想找找看沒有對應的工具可以來使用囉~

找了一下,有套 pev 的工具可以達到需求,

而且還是跨 Windows/Mac/Linux 平台的,真的是太強大了~

 

1. 在 CentOS 7 x64 上安裝 pev

雖然 pev 有提供 RPM 檔,但我直接用 yum 來安裝那個 RPM 檔時,

會遇到其他關聯套件的相依性問題:

Error: Package: pev-0.70-2.x86_64 (/pev-0.70.x86_64)
Requires: libpcre.so.3()(64bit)
Error: Package: pev-0.70-2.x86_64 (/pev-0.70.x86_64)
Requires: libcrypto.so.1.0.0()(64bit)
Error: Package: pev-0.70-2.x86_64 (/pev-0.70.x86_64)
Requires: libcrypto.so.1.0.0(OPENSSL_1.0.0)(64bit)

 

因此決定還是從原始碼來編譯…

執行下面的指令,下載 pev 0.70 版本,編譯後安裝:

wget http://downloads.sourceforge.net/project/pev/pev-0.70/pev-0.70.tar.gz
tar zxvf pev-0.70.tar.gz
cd pev/
make && sudo make install

 

pev 裝好之後,會有 readpe rva2ofs ofs2rva pehash pesec pescan

pepack pestr pedis peres 這些工具,試著執行 readpe 看看…

結果在我的 CentOS 7 x64上出現了錯誤:

testuser@localhost ~ $ readpe
readpe: error while loading shared libraries: libpe.so.1: cannot open shared object file: No such file or directory

 

有點奇怪,在 make install 的時候有看到 libpe 這個東西的訊息,用 ldd 檢查一下: 

testuser@localhost ~ $ which readpe
/usr/bin/readpe
testuser@localhost ~ $ ldd /usr/bin/readpe
linux-vdso.so.1 =>  (0x00007ffccbfec000)
libpe.so.1 => not found
libc.so.6 => /lib64/libc.so.6 (0x00007f6792a85000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6792e63000)

 

平常 x64 上的 library 是放在 /lib64 目錄下,

但由 make install 的 log 來看,libpe.so 應該是被裝到 /usr/lib 下面去了:

testuser@localhost ~ $ ll /usr/lib/libpe*
lrwxrwxrwx. 1 root root    12 Jun 28 22:52 /usr/lib/libpe.so -> libpe.so.1.0
lrwxrwxrwx. 1 root root    12 Jun 28 22:52 /usr/lib/libpe.so.1 -> libpe.so.1.0
-rw-r--r--. 1 root root 26912 Jun 28 22:52 /usr/lib/libpe.so.1.0

 

把這些檔案搬移到 /lib64 下面,再執行 ldd 就沒問題了:

testuser@localhost ~ $ sudo mv /usr/lib/libpe* /lib64/
testuser@localhost ~ $ ldd /usr/bin/readpe
linux-vdso.so.1 =>  (0x00007ffe4dde5000)
libpe.so.1 => /lib64/libpe.so.1 (0x00007fd711a1c000)
libc.so.6 => /lib64/libc.so.6 (0x00007fd71165a000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd711c3d000)

 

2. 使用 pev 檢查 DEP/ASLR 旗標

要檢查 DEP/ASLR 旗標的話,只要用 readpe 就行了,

它的用途是秀出 PE 檔案的 header 部分:

testuser@localhost ~ $ readpe
Usage: readpe OPTIONS FILE
Show PE file headers
Example: readpe --header optional winzip.exe
Options:
-A, --all                              full output (default)
-H, --all-headers                      print all PE headers
-S, --all-sections                     print all PE sections headers
-f, --format <text|csv|xml|html>       change output format (default: text)
-d, --dirs                             show data directories
-h, --header <dos|coff|optional>       show specific header
-i, --imports                          show imported functions
-e, --exports                          show exported functions
-v, --version                          show version and exit
--help                                 show this help and exit

 

執行 readpe <PE file name> 看看,但又遇到了問題:

testuser@localhost ~ $ readpe Configure.exe
ERROR [-14]: fdopen() failed ()

 

真是有點莫名其妙… 用 strace 追一下的話,可以看到在 open() 的時候失敗了,

要求的似乎是 Read/Write 權限,因此檔案如果沒有 w 權限就會不能開啟…

用 chmod a+w 將要檢視的 PE 檔加上可寫入的屬性,再執行 readpe 就沒問題了:

testuser@localhost ~ $ chmod a+w Configure.exe
testuser@localhost ~ $ readpe Configure.exe
DOS Header
Magic number:                    0x5a4d (MZ)
Bytes in last page:              144
Pages in file:                   3
Relocations:                     0
Size of header in paragraphs:    4
Minimum extra paragraphs:        0
Maximum extra paragraphs:        65535
Initial (relative) SS value:     0
Initial SP value:                0xb8
Initial IP value:                0
Initial (relative) CS value:     0
Address of relocation table:     0x40
Overlay number:                  0
OEM identifier:                  0
OEM information:                 0
PE header offset:                0xe0
......

 

如果 PE 檔案有開啟 DEP/ASLR 的話,那在 Optional/Image header 下面,

可以找到 DLL characteristics,這邊就可以看到:

  – IMAGE_DLLCHARACTERISTICS_NX_COMPAT (DEP)

  – IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE (ASLR)

Optional/Image header
......
DLL characteristics:   0x8140
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
IMAGE_DLLCHARACTERISTICS_NX_COMPAT
IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE

 

另一個更簡單的方法是執行 pesec 來檢查,會直接顯示跟安全性相關的設定:

testuser@localhost ~ $ pesec Configure.exe
ASLR:                            yes
DEP/NX:                          yes
SEH:                             yes
Stack cookies (EXPERIMENTAL):    yes

 

不過要注意被檢查的 PE 檔案的 +w 權限要開 (雖然 pesec 不會去改檔案),

不然會出現奇怪的錯誤:

ERROR [-14]: fdopen() failed (Permission denied)

 

pev 是個不錯用的工具,感覺還可以拿它來做很多事喔~^^

 

(本頁面已被瀏覽過 437 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料