1. 程式人生 > 其它 >shell指令碼加密

shell指令碼加密

需求:

  公司的伺服器登入都是通過jumpserver堡壘機管理的,由於實在不喜歡web終端的操作,於是就寫了個自動登入的shell指令碼,由於指令碼要公開給部分研發人員使用,但是指令碼中包含了伺服器的普通使用者和root使用者的密碼,所以為了不使密碼洩露要將指令碼內容加密;

示例指令碼:

#!/bin/bash
for i in {1..10}
do
    echo $i
done

一、gzexe加密

  gzexe命令比較簡單,其本質就是一個壓縮軟體,在CentOS 7.7上是自帶的,其他版本不清楚;

1、加密

[root@message test]# gzexe test.sh
test.sh:	  2.0%
[root@message test]# ls
test.sh  test.sh~

  執行完成後,會在當前目錄生成一個備份檔案test.sh~,該檔案的內容是原始明文檔案的內容,是可以刪除的;test.sh檔案就是加密壓縮後的檔案,用cat檢視時就是一堆亂碼,但是該加密指令碼是可以執行的;

2、解密

[root@message test]# gzexe -d test.sh
[root@message test]# ls
test.sh  test.sh~

  執行完解密操作後,test.sh這個原來的加密檔案就變成明文了;而test.sh~這個檔案就是原先加密檔案的備份,是加密的;

二、shc加密

  shc是一個專業的加密工具,它可以將shell指令碼轉換為一個可執行的二進位制檔案;  
  官網地址:http://www.datsi.fi.upm.es/~frosal/sources/ #目前官網已經斷更了
  軟體地址:http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz

1、安裝

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz
tar xvf shc-3.8.9.tgz -C /usr/local/
cd /usr/local/shc-3.8.9
mkdir -p /usr/local/man/man1    # 該步驟不能省略,make install的時候會報錯

[root@message shc-3.8.9]# 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/
[root@message shc-3.8.9]# which shc  #能看到環境變數則安裝成功
/usr/local/bin/shc 

2、加密

[root@message test]# shc -r -f test.sh
[root@message test]# ls
test.sh  test.sh.x  test.sh.x.c
[root@message test]# cat test.sh
#!/bin/bash
for i in {1..10}
do
    echo $i
done
[root@message test]# bash test.sh.x
test.sh.x: test.sh.x: 無法執行二進位制檔案
[root@message test]# ./test.sh.x
1
2
3
4
5
6
7
8
9
10  

  test.sh 原始檔案
  test.sh.x 二進位制可執行檔案,用cat檢視會是一堆亂碼
  test.sh.x.c 生成的C原始碼,可以刪除
  -r 製作可執行的二進位制檔案
  -f 要編譯的指令碼的檔名

三、總結

  gzexe和shc相比來看,gzexe不需要安裝,操作也方便,但是安全性較差,懂行的人直接就解壓了;shc相對來說還需要安裝,目前官網也不再更新,但是安全性較高,破譯難度較大;