shell 腳本加密

    想想好久沒更新博客了,今天在羣裏看到討論關於shell腳本加密的事情。

    想想也是,我們在寫腳本有時候會配置相關賬號和密碼的事情,這樣只要能權限都能看到該信息,非常的不安全,有沒有在正常運行的情況下對文件進行加密。大致有以下兩個特點:

  1. 加密文件,防止別人看到我寫的具體內容。

  2. 可以對隱藏敏感性知識點,比如腳本涉及到用戶和密碼,如mysql

    我特意搜了一下,有兩種方法,漲知識了,所以驗證了一下,過程如下:


第一種方法(gzexe):基於ubuntu14.04

    這種加密方式不是非常保險的方法,但是能夠滿足一般的加密用途。它是使用系統自帶的gzexe程序,它不但加密,同時壓縮文件。

使用方法: gzexe file.sh 它會把原來沒有加密的文件備份爲 xx.sh~ ,同時 xx.sh 即被變成加密文件

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~ 文件,別人就徹底看不到了(自己也看不到,請酌情刪除)。

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

建議使用,一方面系統自帶命令,達到目的即可,沒人閒的破解你的腳本,就算要破解,先進入你係統再說。


我基於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語言).

不建議使用,需要單獨安裝管理,比較麻煩。如果安全性要求極高,倒是可以參數。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章