前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell脚本文件加密

shell脚本文件加密

作者头像
保持热爱奔赴山海
发布2022-09-23 15:34:56
1.4K0
发布2022-09-23 15:34:56
举报
文章被收录于专栏:饮水机管理员饮水机管理员

生产上,有些场景下(例如一次性的归档数据等)可能需要在脚本里面声明账号密码之类的敏感信息。 可以使用python、valut等更强大的加密方法,但是对于一些临时性的工作,有时候有种杀鸡用牛刀的感觉。

这里,我们可以直接使用shc命令对shell脚本进行加密,简单易用,不依赖其它组件。

    安装相关包:

        yum -y install shc

        $ shc -h    

        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 cmnd] [-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

            CFLAGS  <none>   C compiler flags

            LDFLAGS <none>   Linker flags

            Please consult the shc man page.

    案例:

​        [root@localhost ~]# vim ​​​welcome.sh​​ # 注意脚本的头一行必须是#!/bin/bash 否则会影响到shc的加密!

        #!/bin/sh

        echo "Welcome to linux world"

        #  说明,加密对于脚本传参完全不影响的。

        # 执行加密操作

        [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

来源​    https://zhuanlan.zhihu.com/p/429169464​

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-08-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档