[转帖]Linux 平台使用shc 工具加密shell 脚本

linux,平台,使用,shc,工具,加密,shell,脚本 · 浏览次数 : 0

小编点评

**使用shc工具加密shell脚本** **步骤:** 1. **编写shell脚本**,包含敏感信息。 2. **使用shc工具加密脚本**,使用 `-r`选项指定加密类型为`-r`,并指定目标文件名为`shc`。例如: ```bash shc -r -v -f cndba.shshc shll=bashshc [-i]=-cshc [-x]=exec '%s' \"$@\"shc [-l]=shc opts=shc: cc cndba.sh.x.c -o cndba.sh.xshc: strip cndba.sh.xshc: chmod ug=rwx,o=rx cndba.sh.x[dave@www.cndba.cn scripts] ``` **步骤二:** 1. **创建加密后的二进制文件**,文件名如`cndba.sh.x`。 2. **将脚本文件从原始文件中删除**,因为加密后二进制文件只包含`.x`的二进制代码。 3. **验证加密文件是否合法**,可以使用 `file`命令。例如: ```bash file cndba.sh.x ``` **注意:** * `shc`工具仅适用于基于bash的shell。 * 生成的二进制文件只能通过`./xxx`命令执行,不能通过`/bin/bash xxx`命令执行。 * 确保脚本中没有敏感信息。

正文

 

2021-08-03 20:4510030原创Linux

1 shc 工具说明

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

shc官方网址:
http://www.datsi.fi.upm.es/~frosal/

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

命令帮助:

 

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

加密之后有三个文件:

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

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

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

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

与[转帖]Linux 平台使用shc 工具加密shell 脚本相似的内容:

[转帖]Linux 平台使用shc 工具加密shell 脚本

2021-08-03 20:4510030原创Linux 本文链接:https://www.cndba.cn/dave/article/4642 1 shc 工具说明 shell 脚本是常用脚本,运维中经常使用,但有时候在shell 脚本中会包含一些敏感的信息,比如密码或者特殊的参数,此时我们就可以

[转帖]Linux平台shell脚本输入密码,不显示明文

需求:shell脚本中输入密码,要求不显示明文,需要将其转换为“*”星号,或者不显示 实现方案:有两种实现方案,一是通过stty命令来实现,二是直接使用read来实现 方案一:使用stty来实现 使用stty -echo可以实现不显示密码,就像登录Linux系统输入密码时一样,stty的代码如下:

[转帖]Linux开发环境——SCL软件集

一、SCL简介 1、SCL简介 SCL(Software Collections)是一个CentOS/RHEL Linux平台的软件多版本共存解决方案,为RHEL/CentOS Linux用户提供一种方便、安全地安装和使用应用程序和运行时环境的多个版本的方式,同时避免把系统搞乱。 CentOS/RH

[转帖]Linux 命令详解(三)./configure、make、make install 命令

https://www.cnblogs.com/tinywan/p/7230039.html 这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤 一、基本信息 1、./configure 是用来检测你的安装平台的目标特征的。比如它会检测你是不是有CC或GCC,并不是需要

[转帖]通过Shell脚本自动监控JAVA进程中线程cpu使用率

https://gitee.com/jialy/auto-monitor-java-process/tree/master 本文主要介绍在 show-busy-java-threads.sh 脚本的功能基础上,通过 process-cpu-monitor.sh 脚本实现Linux平台上Java进程或

[转帖]linux(centos8):zabbix配置邮件报警(监控错误日志)(zabbix5.0)

http://t.zoukankan.com/architectforest-p-13204184.html 一,zabbix5.0发邮件报警的准备工作: zabbix5.0在linux平台上的安装:参见这一篇: https://www.cnblogs.com/architectforest/p/1

[转帖]Linux中./configure、make、make install命令详解

简单来说,make 是编译,make install 是安装。 总结:linux编译安装中configure、make和make install各自的作用 • ./configure是用来检测你的安装平台的目标特征的。比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本。

[转帖]linux 调优篇 :硬件调优(BIOS配置)* 壹

https://blog.csdn.net/tony_vip?type=blog 一. 设置内存刷新频率为Auto二. 开启NUMA三. 设置Stream Write Mode四. 开启CPU预取配置五. 开启SRIOV六. 开启SMMU 通过在BIOS中设置一些高级选项,可以有效提升虚拟化平台性能

【转帖】linux 调优篇 :硬件调优(BIOS配置)* 壹

一. 设置内存刷新频率为Auto二. 开启NUMA三. 设置Stream Write Mode四. 开启CPU预取配置五. 开启SRIOV六. 开启SMMU 通过在BIOS中设置一些高级选项,可以有效提升虚拟化平台性能。表1列出了TaiShan服务器和性能相关的BIOS推荐配置项。 表1 BIOS性

[转帖]a.out、coff、elf三种文件格式

补充:a.out早期并不是elf格式的,而是unix下另一种可执行格式,新的a.out是本文讨论了 UNIX/LINUX 平台下三种主要的可执行文件格式:a.out(assembler and link editor output 汇编器和链接编辑器的输出)、COFF(Common Object F