如何使用 SHC 加密 Shell 脚本

如何在Linux环境中加密shell脚本?shell脚本包含密码,不希望其他具有执行权限的人查看shell脚本并获取密码。可以安装使用shc工具,普通用户无法读取shc创建的加密Shell脚本。SHC是指:Shell脚本编译器(Shell Script Compiler)。

环 境

Centos8

安装 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

图片

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值