shc加密shell脚本总结

打印 上一主题 下一主题

主题 888|帖子 888|积分 2664

shc介绍

shc是shell编译器(Shell Compiler)的缩写, 它可以对shell脚本进行编译和加密。它能够将shell脚本编译为可实行的二进制文件,其中包含了脚本的功能和逻辑,而不袒露源代码。可以说shc就是一个加密shell脚本的工具。shc的官方网址为:http://www.datsi.fi.upm.es/~frosal/sources/。shc在github上没有对应的链接。实在也能理解,不是每一个人都喜欢将本身的项目上传到github上。
官方文档关于shc的描述说明如下:
  1.      shc creates a stripped  binary  executable  version  of  the
  2.      script specified with -f on the command line.

  3.      The binary version will get a .x extension appended and will
  4.      usually  be  a  bit  larger  in size than the original ascii
  5.      code. Generated C source code is saved in a  file  with  the
  6.      extension .x.c

  7.      If you supply an expiration date with the -e option the com-
  8.      piled  binary  will  refuse to run after the date specified.
  9.      The message "Please contact your provider" will be displayed
  10.      instead.  This message can be changed with the -m option.

  11.      You can compile any kind of shell script, but  you  need  to
  12.      supply valid -i, -x and -l options.

  13.      The compiled binary will still be  dependent  on  the  shell
  14.      specified  in  the  first  line  of  the  shell  code  (i.e.
  15.      #!/bin/sh), thus shc does not create completely  independent
  16.      binaries.

  17.      shc itself is not a compiler such as cc, it  rather  encodes
  18.      and encrypts a shell script and generates C source code with
  19.      the added expiration capability. It  then  uses  the  system
  20.      compiler  to compile a stripped binary which behaves exactly
  21.      like the  original  script.  Upon  execution,  the  compiled
  22.      binary  will  decrypt and execute the code with the shell -c
  23.      option.  Unfortunatelly, it will  not  give  you  any  speed
  24.      improvement as a real C program would.

  25.      shc's main purpose is to protect  your  shell  scripts  from
  26.      modification  or  inspection.  You can use it if you wish to
  27.      distribute your scripts but don't want  them  to  be  easily
  28.      readable by other people.
复制代码
shc安装

sqc的安装有多种方式,可以根据适合本身的方式来安装,由于各自环境不一样,可能选择的安装方式不一样。
yum安装
  1. # yum -y install shc
复制代码
不外有些Linux版本的yum源可能没有shc包,所以这种方式只适用yum源有shc包的环境。
离线安装

根据对应的Linux发行版本,从https://pkgs.org/ 搜索shc对应平台的rpm包,如下所示,当前测试环境的rpm包下载地点[1]
  1. # yum install -y shc-4.0.3-1.el8.x86_64.rpms
复制代码
源码安装

源码下载地点http://www.datsi.fi.upm.es/~frosal/sources/
  1. mkdir /usr/local/man
  2. mkdir /usr/local/man/man1  #install时会把man文件放入该目录。

  3. tar vxf shc-3.8.9.tgz  && cd shc-3.8.9
  4. make test
  5. make strings
  6. make install
复制代码
shc利用

下面是shc比较常用的参数说明,更多参数说明请参考man手册或官方文档。
参数参数说明-h显示帮助信息并退出-f指定需要加密的shell脚本-v参数-v表现verbose模式,输出更详细的编译日志-r可以在相同操作系统的差别系统中实行,也就是放宽安全限制,天生可再分发的二进制文件-o输出文件名,也可以不指定-f指定shell脚本名称-e指定过期日期-m指定过期后的提示信息-U使二进制无法被追踪,默认不开启-H强化:额外的安全保护,默认不开启,它需要shell不支持参数加密shell脚本的例子
  1. # shc -v -f monitor_long_trx.sh
  2. shc shll=bash
  3. shc [-i]=-c
  4. shc [-x]=exec '%s' "$@"
  5. shc [-l]=
  6. shc opts=
  7. shc: cc   monitor_long_trx.sh.x.c -o monitor_long_trx.sh.x
  8. shc: strip monitor_long_trx.sh.x
  9. shc: chmod ug=rwx,o=rx monitor_long_trx.sh.x
复制代码
如下所示,脚本实行后天生了两个文件,其中monitor_long_trx.sh.x是加密事后的可实行的二进制文件。monitor_long_trx.sh.x.c是天生monitor_long_trx.sh.x的原文件(C语言),也就是说编译这个C源代码文件可以创建上面加密的monitor_long_trx.sh.x文件。
  1. # ls -lrt monitor_long_trx*
  2. -rw-r--r-- 1 root root  10185 Sep 27 15:51 monitor_long_trx.sh
  3. -rw-r--r-- 1 root root  70193 Sep 29 22:16 monitor_long_trx.sh.x.c
  4. -rwxrwxr-x 1 root root  24584 Sep 29 22:16 monitor_long_trx.sh.x
  5. # file monitor_long_trx.sh.x
  6. monitor_long_trx.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]=906ccbd32b4e0fa3307be46ff7736bbfac9be25c, stripped
  7. # file monitor_long_trx.sh.x.c
  8. monitor_long_trx.sh.x.c: ASCII text
复制代码
不外这个C源代码文件,跟你想象的C语言源代码文件可能有点不一样。如下所示,
shc还提供了一种设定有效实行期限的方法,编译天生的可实行二进制文件在过了这个有效性后,就不能实行。
  1. shc -e 09/20/2024 -v -f monitor_long_trx.sh

  2. shc -e 09/20/2024 -v -m "the script has expired, please contact your provierder xxx@xxx.com" -f monitor_long_trx.sh 
复制代码
  1. # shc -e 09/20/2024 -v -f monitor_long_trx.sh
  2. shc shll=bash
  3. shc [-i]=-c
  4. shc [-x]=exec '%s' "$@"
  5. shc [-l]=
  6. shc opts=
  7. shc: cc   monitor_long_trx.sh.x.c -o monitor_long_trx.sh.x
  8. shc: strip monitor_long_trx.sh.x
  9. shc: chmod ug=rwx,o=rx monitor_long_trx.sh.x
复制代码
一些简单常用的例子
  1. shc -f monitor_long_trx.sh

  2. shc -v -r -f monitor_long_trx.sh -o mon_long_trx.sh

  3. shc -v -r -u -H -f monitor_long_trx.sh -o mon_long_trx.sh

  4. shc -v -r -e 09/20/2025 -m "the script has expired..."  -f monitor_long_trx.sh -o mon_long_trx.sh
复制代码
shc评测

shc编译出来的二进制可实行文件,可能比原shell脚本在文件巨细上稍微大上一些,相比gzexe和Bashfuscator等工具,它要可靠很多(Bashfuscator就非常不可靠,有些混淆出来的脚本实行会报错)。从个人简单的测试和实践来看,这个工具非常好用,而且用途非常广泛。
那么shc加密事后的可实行二进制文件,能否被解密呢? 答案是低版本sqc天生的加密二进制可实行文件可以被解密,可以被工具UnSHc[2]解密。而高版本shc(4.x)天生加密文件越来越难解密(暂时不能解密,不代表不能被后续的工具或方法解密)。
下面是UnSHc中的介绍说明,有兴趣可以看看相识一下
Due to the many problems since shc 4.0.3, there seems to be a need for clarification. In shc 4.0.3 many structural changes have been incorporated, so that shc now makes use of various security mechanisms provided by the linux-kernel itself. Therefore, it is now almost impossible to extract the original shell script at all with current UnSHc version, if the new shc version was used. This requires a more in-depth approach, which means that a modified bash or a modified linux-kernel is needed to bypass the security measures.`
参考资料[1] 1: https://dl.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/s/shc-4.0.3-1.el8.x86_64.rpm
[2] 2: https://github.com/yanncam/UnSHc/
扫描上面二维码关注我假如你真心以为文章写得不错,而且对你有所帮助,那就不妨帮助“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!本文版权归作者全部,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

石小疯

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表