Shell脚本加密
在日常运维工作中,Shell脚本的使用非常广泛。然而,随着脚本的重要性和敏感性增加,如何对其进行加密保护变得尤为重要。本文将介绍两种常见的Shell脚本加密工具:SHC和gzexe。SHC提供了高等级的加密,而gzexe则适用于安全性要求不高的场景。
SHC - 加密等级高
SHC(Shell Script Compiler)是一款专门用于加密和编译Shell脚本的工具。它可以将Shell脚本转换成二进制可执行文件,保护脚本的源代码不被直接查看和修改。
注意:
• CentOS 7与CentOS 8生成的脚本不通用,在哪个版本生成,在哪个版本使用。
通过yum安装
首先,通过yum安装shc工具:
yum install -y shc
通过下载源码安装
如果需要通过源码安装,可以按照以下步骤进行:
1. 安装gcc及make
如果已安装gcc和make,可略过此步骤。
yum -y install gcc make
2. 下载并编译安装shc-4.0.3
由于需要从GitHub下载源码,建议使用科学上网工具。下载地址如下: shc-4.0.3下载地址[1]
3. 解压下载的压缩包
tar -xzvf shc-4.0.3.tar.gz
4. 编译并安装shc
进入解压后的目录并执行以下命令进行编译安装:
cd shc-4.0.3
./configure && make install
5. 安装完成
至此,shc安装完成。
使用shc加密脚本
创建一个Shell脚本
[root@jast ~]# vim test-shc.sh
#!/bin/sh
echo `date`
生成加密脚本
[root@10 ~]# shc -v -f test-shc.sh
shc shll=sh
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc test-shc.sh.x.c -o test-shc.sh.x
shc: strip test-shc.sh.x
shc: chmod ug=rwx,o=rx test-shc.sh.x
查看生成的文件
[root@10 ~]# ll
total 36
-rw-r--r-- 1 root root 22 May 26 14:52 test-shc.sh
-rwxrwxr-x 1 root root 11120 May 26 14:52 test-shc.sh.x
-rw-r--r-- 1 root root 17592 May 26 14:52 test-shc.sh.x.c
文件说明:
•
test-shc.sh
是原始的未加密Shell脚本。•
test-shc.sh.x
是二进制格式的加密Shell脚本。•
test-shc.sh.x.c
是test-shc.sh
文件的C源代码。编译该C源代码以创建加密的test-shc.sh.x
文件。
查看文件类型
[root@10 ~]# file test-shc.sh
test-shc.sh: POSIX shell script, ASCII text executable
[root@10 ~]# file test-shc.sh.x
test-shc.sh.x: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=65a9d57e8eb0a24f4a000fe680e030dbc23468f6, stripped
[root@10 ~]# file test-shc.sh.x.c
test-shc.sh.x.c: C source, ASCII text
执行加密后的脚本
[root@10 ~]# ./test-shc.sh.x
Thu May 26 14:56:30 CST 2022
指定脚本过期时间并设置提示信息
shc -e 06/10/2023 -m "error" -v -f test-shc.sh
执行脚本后,如果过期,会提示如下信息:
[root@10 ~]# ./test-shc.sh.x
./test-shc.sh.x: has expired!
error
如果不指定-m
,默认提示如下:
[root@10 ~]# ./test-shc.sh.x
./test-shc.sh.x: has expired!
Please contact your provider jahidulhamid@yahoo.com
gzexe - 加密等级低
gzexe是系统自带的工具,无需另外安装。它适用于对安全性要求不高的文件进行简单加密,支持除Shell脚本外的其他文本文件。
gzexe加密/解密用法
加密
使用gzexe对脚本进行加密非常简单,加密后会将源文件改名为xxx.sh~,生成一个可执行的加密文件。
gzexe xxx.sh
解密
解密同样简单,只需指定加密的脚本名即可,解密后源文件会被还原,原先的加密文件会改名为xxx.sh~。
gzexe -d xxx.sh
示例
假设我们有一个名为example.sh的脚本,内容如下:
#!/bin/sh
echo "Hello, World!"
加密脚本
执行加密命令:
gzexe example.sh
执行后会生成两个文件:
•
example.sh
:加密后的脚本文件。•
example.sh~
:原始脚本的备份文件。
运行加密后的脚本
./example.sh
输出:
Hello, World!
解密脚本
执行解密命令:
gzexe -d example.sh
解密后会恢复原始脚本,文件名为example.sh,加密文件会被重命名为example.sh~。
您可以根据实际需求选择合适的Shell脚本加密工具,以保护脚本的安全。SHC提供了更高等级的加密保护,而gzexe则适用于对安全性要求较低的场景。
引用链接
[1]
shc-4.0.3下载地址: https://github.com/neurobin/shc/archive/refs/tags/4.0.3.tar.gz
欢迎关注我的公众号“Hadoop学习之路”,原创技术文章第一时间推送。





