對shell腳本進行加密


用shell腳本對系統進行自動化維護,簡單,便捷而且可移植性好.
但shell腳本是可讀寫的,很有可能會泄露敏感信息,如用戶名,密碼,路徑,IP等.
同樣,在shell腳本運行時會也泄露敏感信息.
請問如何不影響腳本運行的前提下,對腳本進行加密?

一、shc方法

shc是一個加密shell腳本的工具.它的作用是把shell腳本轉換為一個可執行的二進制文件,這就很好的解決了上述問題.

yum安裝:

yum -y install shc

編譯安裝:

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz
tar xvfz shc-3.8.7.tgz
cd shc-3.8.7
make
驗證shc是否正確安裝
[root@martin shc-3.8.7]# ./shc -v
shc parse(-f): No source file specified

shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script

創建一個示例Shell腳本

[root@martin shc-3.8.7]# vi random.sh
#!/bin/bash
read -p "How many random numbers do you want to generate?" max
for (( start = 1; start <= $max; start++ ))
do
  echo -e $RANDOM
done

給腳本增加可執行權限

[root@martin shc-3.8.7]# chmod u+x random.sh

執行示例腳本

[root@martin shc-3.8.7]# ./random.sh
How many random numbers do you want to generate?3
14235
9555
7671

使用shc加密Shell腳本

[root@martin shc-3.8.7]# ./shc -v -r -T -f random.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc  random.sh.x.c -o random.sh.x
shc: strip random.sh.x
shc: chmod go-r random.sh.x

運行后會生成兩個文件,script-name.x 和 script-name.x.c
script-name.x是加密后的可執行的二進制文件
script-name.x.c是生成script-name.x的原文件(c語言)

[root@martin shc-3.8.7]# ll random.sh*
-rwxr-xr-x 1 root root   146 Aug  2 10:26 random.sh
-rwx--x--x 1 root root  9424 Aug  2 10:30 random.sh.x
-rw-r--r-- 1 root root 10080 Aug  2 10:30 random.sh.x.c

執行加密后的腳本

[root@martin shc-3.8.7]# ./random.sh.x 
How many random numbers do you want to generate?3
28955
21487
29513

還不完善,只能全路徑執行shc命令或者進入目錄內,加入全局環境變量/etc/profile未生效

二、gzexe

它是使用系統自帶的gzexe程序,它不但加密,同時壓縮文件

這種加密方式不是非常保險的方法,但是能夠滿足一般的加密用途,可以隱蔽腳本中的密碼等信息。

使用方法:

[root@martin home]# gzexe random.sh 
random.sh:     20.5%
[root@martin home]# ll random.sh*
-rwxr-xr-x 1 root root 953 Aug  2 10:45 random.sh
-rwxr-xr-x 1 root root 146 Aug  2 10:45 random.sh~

它會把原來沒有加密的文件備份為 file.sh~ ,同時 file.sh 即被變成加密文件

參考地址:

http://lidao.blog.51cto.com/3388056/1914205

https://yq.aliyun.com/ziliao/65848

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM