想一想很久沒更新博客了,今天在羣裏看到討論關於shell腳本加密的事情。mysql
想一想也是,咱們在寫腳本有時候會配置相關帳號和密碼的事情,這樣只要能權限都能看到該信息,很是的不安全,有沒有在正常運行的狀況下對文件進行加密。大體有如下兩個特色:redis
加密文件,防止別人看到我寫的具體內容。sql
能夠對隱藏敏感性知識點,好比腳本涉及到用戶和密碼,如mysql
shell
我特地搜了一下,有兩種方法,漲知識了,因此驗證了一下,過程以下:ubuntu
第一種方法(gzexe):基於ubuntu14.04centos
這種加密方式不是很是保險的方法,可是可以知足通常的加密用途。它是使用系統自帶的gzexe程序,它不但加密,同時壓縮文件。安全
使用方法: gzexe file.sh 它會把原來沒有加密的文件備份爲 xx.sh~ ,同時 xx.sh 即被變成加密文件bash
root@leco:~/test# ls test.sh root@leco:~/test# cat test.sh #!/bin/bash echo `date` root@loocha50:~/test# sh test.sh Mon Jul 17 15:08:01 HKT 2017 root@loocha50:~/test# gzexe test.sh test.sh: 25.0% root@loocha50:~/test# ls test.sh test.sh~ root@loocha50:~/test# sh test.sh Mon Jul 17 15:08:14 HKT 2017 root@loocha50:~/test# sh test.sh~ Mon Jul 17 15:08:18 HKT 2017 root@loocha50:~/test# cat test.sh~ #!/bin/bash echo `date` root@loocha50:~/test# cat test.sh #!/bin/bash skip=44 tab='' nl=' ' IFS=" $tab$nl" umask=`umask` umask 77 gztmpdir= trap 'res=$? test -n "$gztmpdir" && rm -fr "$gztmpdir" (exit $res); exit $res ' 0 1 2 3 5 10 13 15 if type mktemp >/dev/null 2>&1; then gztmpdir=`mktemp -dt` else gztmpdir=/tmp/gztmp$$; mkdir $gztmpdir fi || { (exit 127); exit 127; } gztmp=$gztmpdir/$0 case $0 in -* | */*' ') mkdir -p "$gztmp" && rm -r "$gztmp";; */*) gztmp=$gztmpdir/`basename "$0"`;; esac || { (exit 127); exit 127; } case `echo X | tail -n +1 2>/dev/null` in X) tail_n=-n;; *) tail_n=;; esac if tail $tail_n +$skip <"$0" | gzip -cd > "$gztmp"; then umask $umask chmod 700 "$gztmp" (sleep 5; rm -fr "$gztmpdir") 2>/dev/null & "$gztmp" ${1+"$@"}; res=$? else echo >&2 "Cannot decompress $0" (exit 127); res=127 fi; exit $res
此時咱們能夠看出test.sh 文件已經被加密了,可是不影響運行。此時你刪除test.sh~ 文件,別人就完全看不到了(本身也看不到,請酌情刪除)。app
root@leco:~/test# ls test.sh test.sh~ root@leco:~/test# rm test.sh~ root@leco:~/test# ls test.sh root@leco:~/test# sh test.sh Mon Jul 17 15:10:36 HKT 2017
建議使用,一方面系統自帶命令,達到目的便可,沒人閒的破解你的腳本,就算要破解,先進入你係統再說。ide
我基於centos6.8 (ubuntu14.04有問題,沒具體排查)
方法2: shc
shc 經常使用參數
-e date
Expiration date in dd/mm/yyyy format [none](指定過時日期)
-m message
message to display upon expiration ["Please contact your provider"](指定過時提示的信息)
-f script_name
File name of the script to compile(指定要編譯的shell的路徑及文件名)
-r Relax security.
Make a redistributable binary which executes on different systems running the same operat-ing system.(能夠相同操做系統的不一樣系統中執行)
-v Verbose compilation(編譯的詳細狀況)
shc是一個專業的加密shell腳本的工具.它的做用是把shell腳本轉換爲一個可執行的二進制文件,這個辦法也很好的解決了腳本中含有IP、密碼等不但願公開的問題
包下載連接:
http://www.datsi.fi.upm.es/~frosal/sources/
[root@leco ~]# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.6.tgz --2017-07-17 15:23:39-- http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.6.tgz Resolving www.datsi.fi.upm.es... 138.100.9.22 Connecting to www.datsi.fi.upm.es|138.100.9.22|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 35071 (34K) [application/x-gzip] Saving to: 「shc-3.8.6.tgz」 100%[=============================================================================================================================>] 35,071 21.9K/s in 1.6s 2017-07-17 15:23:46 (21.9 KB/s) - 「shc-3.8.6.tgz」 saved [35071/35071] [root@leco ~]# tar xf shc-3.8.6.tgz [root@leco ~]# mkdir -p /usr/local/man/man1
這步是必須的,否則安裝過程當中會報錯,shc將安裝命令到/usr/local/bin/目錄下;
將幫助文檔存放在/usr/local/man/man1/目錄下,若是系統中無此目錄,安裝時會報錯,可建立此目錄後再執行安裝。
[root@leco ~]# cd shc-3.8.6 [root@leco shc-3.8.6]# make install ***Installing shc and shc.1 on /usr/local ***Do you want to continue? y #此時等待輸入y install -c -s shc /usr/local/bin/ install -c -m 644 shc.1 /usr/local/man/man1/ 使用方法:-f [root@leco test]# cat test.sh #!/bin/bash echo `date` [root@leco test]# shc -r -f test.sh test.sh.x.c: In function 'chkenv': test.sh.x.c:211: warning: cast from pointer to integer of different size You have new mail in /var/spool/mail/root [root@leco test]# ls test.sh test.sh.x test.sh.x.c 運行後會生成兩個文件,xx.x 和 xx.x.c.
其中xx.x是加密後的可執行的二進制文件;用./xx.x便可運行,xx.x.c是生成xx.x的原文件(c語言).
不建議使用,須要單獨安裝管理,比較麻煩。若是安全性要求極高,卻是能夠參數。