问题:如何在Linux环境中加密shell 脚本
应用场景:
1.当shell脚本中包含密码,不希望其他人查看到shell脚本中的密码等信息,可以安装使用SHC工具进行加密。
安装SHC
系统环境:Centos7
yum -y install shc
创建一个shell脚本
c
cat test.sh
#!/bin/sh
echo "Welcome to linux world"
chmod +x test.sh
使用SHC加密改脚本文件
[root@node1 ~]# shc -v -f test.sh
shc shll=sh
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc test.sh.x.c -o test.sh.x
shc: strip test.sh.x
shc: chmod ug=rwx,o=rx test.sh.x
[root@node1 ~]# ll test.sh*
-rwxr-xr-x. 1 root root 40 Mar 23 20:08 test.sh
-rwxrwxr-x. 1 root root 11200 Mar 24 09:55 test.sh.x
-rw-r--r--. 1 root root 17662 Mar 24 09:55 test.sh.x.c
[root@node1 ~]#
test.sh 是原始的未加密
shell脚本 test.sh.x 是二进制格式的加密shell脚本
test.sh.x.c 是test.sh 文件的C源代码。
可以使用file命令查看文件的类型:
[root@node1 ~]# file test.sh
test.sh: POSIX shell script, ASCII text executable
[root@node1 ~]# file test.sh.x
test.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]=4e4076c4425cd5c26553f1fd8d2e50f86a223b86, stripped
[root@node1 ~]# file test.sh.x.c
test.sh.x.c: C source, ASCII text
[root@node1 ~]#
执行加密后的shell脚本
ps:尝试执行加密后的shell脚本,确保他能正常运行
[root@node1 ~]# ./test.sh.x
Welcome to linux world
[root@node1 ~]#
指定Shell脚本的过期时间
[root@node1 ~]# rm -rf test.sh.x*
[root@node1 ~]# shc -e 24/03/2021 -v -f test.sh
shc shll=sh
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc test.sh.x.c -o test.sh.x
shc: strip test.sh.x
shc: chmod ug=rwx,o=rx test.sh.x
[root@node1 ~]# ./test.sh.x
./test.sh.x: has expired!
Please contact your provider jahidulhamid@yahoo.com
[root@node1 ~]#
在示例中,如果有人执行了test.sh.x脚本文件后,会提示已过期
如果想要指定自定义到期消息,需要添加-m 参数选项
[root@node1 ~]# shc -e 24/03/2021 -m "The script has expired, please contact Aihuidi" -v -f test.sh
shc shll=sh
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc test.sh.x.c -o test.sh.x
shc: strip test.sh.x
shc: chmod ug=rwx,o=rx test.sh.x
[root@node1 ~]# ./test.sh.x
./test.sh.x: has expired!
The script has expired, please contact Aihuidi
[root@node1 ~]#