linux文件加密 shc 脚本专用加密

Linux下的shell脚本用途广泛,经常包含IP、Pwd等关键信息,可读可写的特点很容易造成信息泄露,导致严重后果。基于这些原因,对脚本实施加密便变得很有必要。本文介绍的shc便是这样的一款加密工具。

官网:http://www.datsi.fi.upm.es

shc的帮助命令:

shc -help

shc Version 3.8.9, Generic Script Compiler
shc Copyright (c) 1994-2012 Francisco Rosales frosal@fi.upm.es
shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -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   指定要编译的shell的路径及文件名
-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: --
-r     Relax security. Make a redistributable binary   可以相同操作系统的不同系统中执行
-v     Verbose compilation    编译的详细情况
-D     Switch ON debug exec calls [OFF]
-T     Allow binary to be traceable [no]
-C     Display license and exit
-A     Display abstract and exit
-h     Display help and exit

Environment variables used:
Name    Default  Usage
CC      cc       C compiler command
CFLAGS  <none>   C compiler flags

Please consult the shc(1) man page.

1、安装

cd /usr/local/src

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz

tar xf shc-3.8.9.tgz

cd shc-3.8.9/

make install  #会报一个错,如下图所示

anzhuang.png

创建 mkdir -p /usr/local/man/man1/ ,然后运行make install,也可以直接无视,一般没什么影响,只是少了对应的帮助文档。

常用参数:
-e date (指定过期日期)例:shc -e 01/01/2020
-m message (指定过期提示的信息)
-f script_name(指定要编译的shell的路径及文件名)
-r Relax security. (可以相同操作系统的不同系统中执行)
-v Verbose compilation(编译的详细情况)

加密方法

这里我的脚本为install_nextcloud.sh
**注意!!:这样只能在自己的主机运行,换一台相同的主机就算相同的操作系统也不行,如果需要在不同主机也能运行需要加-r参数
**
-v 加密过程

-f 后面跟需要加密的文件

加密后会发现多出了两个文件

shc.png

install_nextcloud.sh.x 为二进制文件,赋予执行权限后(自动帮你赋予了x权限),可直接执行。当你查看此脚本如下图所示,都是乱码:

luanma.png

install_nextcloud.sh.x.c 为c源文件,基本没用可以直接删了。

这里也可以指定脚本过期的时间

选项“-e”指定过期时间,格式为“日/月/年”;选项“-m”指定过期后执行此shell程序的提示信息。

shc -e 28/01/2012 -m "过期了" -f test.sh

如果在过期后执行,则会有如下提示:

#./test.sh.x

./test.sh.x: has expired!

过期了

!!!不过使用以上方法需要防止用户更改系统,可以通过在脚本中加入自动更新系统时间的命令来解决此问题。