如何在Linux環境中加密shell腳本?shell腳本包含密碼,不希望其他具有執行權限的人查看shell腳本並獲取密碼。可以安裝使用shc工具,普通用戶無法讀取shc創建的加密Shell腳本。SHC是指:Shell腳本編譯器(Shell Script Compiler)。 |
環境
安裝shc
[root@localhost ~]# yum -y install shc
創建一個shell腳本
下面創建一個腳本文件:
[root@localhost ~]# vim welcome.sh #!/bin/sh echo "Welcome to linux world"
使用shc加密該腳本文件
如下所示,使用shc加密welcome.sh腳本。
[root@localhost scripts]# shc -v -f welcome.sh shc shll=sh shc [-i]=-c shc [-x]=exec '%s' "$@" shc [-l]= shc opts= shc: cc welcome.sh.x.c -o welcome.sh.x shc: strip welcome.sh.x shc: chmod ug=rwx,o=rx welcome.sh.x
- welcome.sh 是原始的未加密shell腳本
- welcome.sh.x 是二進制格式的加密shell腳本
- welcome.sh.x.c 是welcome.sh文件的C源代碼。編譯該C源代碼以創建上面的加密的welcome.sh.x文件。
可以使用file
命令查看文件的類型:
[root@localhost scripts]# file welcome.sh welcome.sh: POSIX shell script, ASCII text executable [root@localhost scripts]# file welcome.sh.x welcome.sh.x: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=35e0e2569eca90774e379d6fef51ad6fedf346f5, stripped [root@localhost scripts]# file welcome.sh.x.c welcome.sh.x.c: C source, ASCII text [root@localhost scripts]#
執行加密后的shell腳本
現在,讓我們執行加密的Shell腳本,確保能夠運行:
[root@localhost scripts]# ./welcome.sh.x Welcome to linux world
指定Shell腳本的過期時間
使用shc,您還可以指定到期日期。即在這個到期日期之后,當有人嘗試執行Shell腳本時,將收到錯誤消息。使用shc -e
選項創建一個新的加密Shell腳本,指定到期日期。到期日期以dd/mm/yyyy 格式指定。
# 刪除之前創建的.x , .x.c文件 [root@localhost scripts]# rm -rf welcome.sh.x* # 創建帶有過期時間的加密腳本 [root@localhost scripts]# shc -e 01/02/2021 -v -f welcome.sh shc shll=sh shc [-i]=-c shc [-x]=exec '%s' "$@" shc [-l]= shc opts= shc: cc welcome.sh.x.c -o welcome.sh.x shc: strip welcome.sh.x shc: chmod ug=rwx,o=rx welcome.sh.x
在此示例中,如果有人嘗試執行welcome.sh.x腳本文件,會提示已過期。
[root@localhost scripts]# ./welcome.sh.x ./welcome.sh.x: has expired! Please contact your provider jahidulhamid@yahoo.com
如果要指定自定義到期消息,需要加入-m
選項。
[root@localhost scripts]# shc -e 01/02/2021 -m "Please contact admin@example.com!" -v -f welcome.sh shc shll=sh shc [-i]=-c shc [-x]=exec '%s' "$@" shc [-l]= shc opts= shc: cc welcome.sh.x.c -o welcome.sh.x shc: strip welcome.sh.x shc: chmod ug=rwx,o=rx welcome.sh.x
總結
本文介紹了如何使用shc加密shell腳本。