1) 工具說明
shell腳本是可讀寫的, 很有可能會泄露敏感信息, 如用戶名/密碼/路徑/IP等. 同樣在shell腳本運行時會也泄露敏感信息. shc是一個加密shell腳本的工具, 它的作用是把shell腳本轉換為一個可執行的二進制文件. 這就很好的解決了上述問題.
2) 下載安裝
下載: http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.6.tgz
安裝:
# mkdir -p /orahome/wangnc/shc && cd /orahome/wangnc/shc
# rz (上傳安裝包shc-3.8.6.tgz)
# mkdir /usr/local/man/man1/ (install時會把man文件放入該目錄,如果該目錄不存在需提前建好)
# tar vxf shc-3.8.6.tgz && cd shc-3.8.6
# make test
# make strings
# make install
*** Installing shc and shc.1 on /usr/local
*** Do you want to continue? <輸入y>
install -c -s shc /usr/local/bin/
install -c -m 644 shc.1 /usr/local/man/man1/
用法:
# which shc
/usr/local/bin/shc
# shc
shc parse(-f): No source file specified
shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script
3) 加密腳本
# shc -v -f test.sh
-v是verbose模式, 輸出更詳細編譯日志;
-f 指定腳本的名稱.
$ ll test*
-rwxr-xr-x 1 Oracle oinstall 1178 Aug 18 10:00 test.sh
-rwx--x--x 1 oracle oinstall 8984 Aug 18 18:01 test.sh.x
-rw-r--r-- 1 oracle oinstall 14820 Aug 18 18:01 test.sh.x.c
$ file test.sh.x
test.sh.x: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped
可以看到生成了動態鏈接可執行二進制文件test.sh.x和C源文件testup.sh.x.c, 注意生成的二進制文件因為是動態鏈接形式, 所以在其它平台上不能運行.
4) 生成靜態鏈接的二進制可執行文件
可以通過下面的方法生成一個靜態鏈接的二進制可執行文件:
$ CFLAGs=-static shc -r -f test.sh
$ file testup.sh.x
5) 通過sch加密后的腳本文件很安全嗎?
一般來說是安全的, 不過可以使用gdb和其它的調試工具獲得最初的源代碼. 如果需要更加安全的方法, 可以考慮使用wzshSDK. 另外shc還可以設置腳本的運行期限和自定義返回信息:
$ shc -e 03/31/2007 -m "the MySQL backup scrīpt is now out of date." -f test.sh
-e表示腳本將在2007年3月31日前失效, 並根據-m定義的信息返回給終端用戶.