安装

shc依赖gcc,需要先安装gcc:

yum -y install gcc shc
  • 1.

创建测试脚本

cat test.sh
#!/bin/sh
echo "Welcome to linux world"
chmod +x test.sh
  • 1.
  • 2.
  • 3.
  • 4.

使用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 ~]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • test.sh 是原始的未加密脚本
  • 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 ~]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

验证加密后的脚本是否可正常执行

[root@node1 ~]# ./test.sh.x
Welcome to linux world
[root@node1 ~]#
  • 1.
  • 2.
  • 3.

设置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 ~]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

如果想要指定自定义的到期消息提示内容,需要添加-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 ~]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

在示例中,如果有人执行了过期的test.sh.x脚本文件后,会提示已过期:

Linux centos7安装shc加密shell脚本命令行工具_linux