签到成功

知道了

CNDBA社区CNDBA社区

Linux 平台使用shc 工具加密shell 脚本

2021-08-03 20:45 2288 0 原创 Linux
作者: dave

1 shc 工具说明

shell 脚本是常用脚本,运维中经常使用,但有时候在shell 脚本中会包含一些敏感的信息,比如密码或者特殊的参数,此时我们就可以使用shc 工具对shell 脚本进行加密。 shc是一个脚本编译工具, 使用RC4加密算法, 它能够把shell程序转换成二进制可执行文件(支持静态链接和动态链接)。http://www.cndba.cn/dave/article/4642http://www.cndba.cn/dave/article/4642

shc官方网址:
http://www.datsi.fi.upm.es/~frosal/http://www.cndba.cn/dave/article/4642

github地址:
https://github.com/neurobin/shc

2 安装shc

[dave@www.cndba.cn  www]# unzip shc-master.zip
[dave@www.cndba.cn  www]# cd shc-master
[dave@www.cndba.cn  shc-master]# ls
aclocal.m4  ChangeLog  configure.ac  Makefile.am  man.md  README.md  test
AUTHORS     config     COPYING       Makefile.in  NEWS    shc.1
autogen.sh  configure  INSTALL       man.html     README  src
[dave@www.cndba.cn  shc-master]#
[dave@www.cndba.cn  shc-master]# ./configure
[dave@www.cndba.cn  shc-master]# make
[dave@www.cndba.cn  shc-master]# make install
[dave@www.cndba.cn  shc-master]# which shc
/usr/local/bin/shc

命令帮助:http://www.cndba.cn/dave/article/4642http://www.cndba.cn/dave/article/4642http://www.cndba.cn/dave/article/4642

[dave@www.cndba.cn  scripts]# shc -help
shc Version 4.0.3, Generic Shell Script Compiler
shc GNU GPL Version 3 Md Jahidul Hamid <jahidulhamid@yahoo.com>
shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmd] [-l lopt] [-o outfile] [-rvDSUHCABh] -f script

    -e %s  Expiration date in dd/mm/yyyy format [none]
    -m %s  Message to display upon expiration ["Please contact your provider"]
    -f %s  File name of the script to compile
    -i %s  Inline option for the shell interpreter i.e: -e
    -x %s  eXec command, as a printf format i.e: exec('%s',@ARGV);
    -l %s  Last shell option i.e: --
    -o %s  output filename
    -r     Relax security. Make a redistributable binary
    -v     Verbose compilation
    -S     Switch ON setuid for root callable programs [OFF]
    -D     Switch ON debug exec calls [OFF]
    -U     Make binary untraceable [no]
    -H     Hardening : extra security protection [no]
           Require bourne shell (sh) and parameters are not supported
    -C     Display license and exit
    -A     Display abstract and exit
    -B     Compile for busybox
    -h     Display help and exit

    Environment variables used:
    Name    Default  Usage
    CC      cc       C compiler command
    STRIP   strip    Strip command
    CFLAGS  <none>   C compiler flags
    LDFLAGS <none>   Linker flags

    Please consult the shc man page.

[dave@www.cndba.cn  scripts]#

3 加密测试

[dave@www.cndba.cn  scripts]# vim cndba.sh
[dave@www.cndba.cn  scripts]# cat cndba.sh
#!/bin/bash
 echo "https://www.cndba.cn"
[dave@www.cndba.cn  scripts]# chmod a+x cndba.sh
[dave@www.cndba.cn  scripts]#

[dave@www.cndba.cn  scripts]# shc -r -v -f cndba.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc   cndba.sh.x.c -o cndba.sh.x
shc: strip cndba.sh.x
shc: chmod ug=rwx,o=rx cndba.sh.x
[dave@www.cndba.cn  scripts]#

[dave@www.cndba.cn  scripts]# ll cndba*
-rwxr-xr-x 1 root root    41 Aug  3 19:52 cndba.sh
-rwxrwxr-x 1 root root  9752 Aug  3 19:55 cndba.sh.x
-rw-r--r-- 1 root root 17929 Aug  3 19:55 cndba.sh.x.c
[dave@www.cndba.cn  scripts]#

加密之后有三个文件:

http://www.cndba.cn/dave/article/4642

  1. .sh 源文件
  2. .sh.x 加密后二进制文件
  3. .sh.x.c 脚本对应的C语言版本源码

从安全角度来说:加密后,只需保留.x的二进制文件即可,其他两个文件均可以删除。

另外需要注意的就是shc生成的二进制文件只能通过 ./xxx 命令来执行,不能通过 /bin/bash xxx 来执行。http://www.cndba.cn/dave/article/4642http://www.cndba.cn/dave/article/4642

http://www.cndba.cn/dave/article/4642

[dave@www.cndba.cn  scripts]# sh cndba.sh
https://www.cndba.cn
[dave@www.cndba.cn  scripts]# sh cndba.sh.x
cndba.sh.x: cndba.sh.x: cannot execute binary file
[dave@www.cndba.cn  scripts]# ./cndba.sh.x
https://www.cndba.cn
[dave@www.cndba.cn  scripts]#

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2237
    原创
  • 3
    翻译
  • 546
    转载
  • 186
    评论
  • 访问:6597388次
  • 积分:4242
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ