对shell脚本进行加密

用shell脚本对系统进行自动化维护,简单,便捷而且可移植性好.
但shell脚本是可读写的,很有可能会泄露敏感信息,如用户名,密码,路径,IP等.
同样,在shell脚本运行时会也泄露敏感信息.
请问如何不影响脚本运行的前提下,对脚本进行加密?

一、shc方法

shc是一个加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件,这就很好的解决了上述问题.

yum安装:

yum -y install shc

编译安装:

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz
tar xvfz shc-3.8.7.tgz
cd shc-3.8.7
make
验证shc是否正确安装
[root@martin shc-3.8.7]# ./shc -v
shc parse(-f): No source file specified

shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script

创建一个示例Shell脚本

[root@martin shc-3.8.7]# vi random.sh
#!/bin/bash
read -p "How many random numbers do you want to generate?" max
for (( start = 1; start <= $max; start++ ))
do
  echo -e $RANDOM
done

给脚本增加可执行权限

[root@martin shc-3.8.7]# chmod u+x random.sh

执行示例脚本

[root@martin shc-3.8.7]# ./random.sh
How many random numbers do you want to generate?3
14235
9555
7671

使用shc加密Shell脚本

[root@martin shc-3.8.7]# ./shc -v -r -T -f random.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc  random.sh.x.c -o random.sh.x
shc: strip random.sh.x
shc: chmod go-r random.sh.x

运行后会生成两个文件,script-name.x 和 script-name.x.c
script-name.x是加密后的可执行的二进制文件
script-name.x.c是生成script-name.x的原文件(c语言)

[root@martin shc-3.8.7]# ll random.sh*
-rwxr-xr-x 1 root root   146 Aug  2 10:26 random.sh
-rwx--x--x 1 root root  9424 Aug  2 10:30 random.sh.x
-rw-r--r-- 1 root root 10080 Aug  2 10:30 random.sh.x.c

执行加密后的脚本

[root@martin shc-3.8.7]# ./random.sh.x 
How many random numbers do you want to generate?3
28955
21487
29513

还不完善,只能全路径执行shc命令或者进入目录内,加入全局环境变量/etc/profile未生效

二、gzexe

它是使用系统自带的gzexe程序,它不但加密,同时压缩文件

这种加密方式不是非常保险的方法,但是能够满足一般的加密用途,可以隐蔽脚本中的密码等信息。

使用方法:

[root@martin home]# gzexe random.sh 
random.sh:     20.5%
[root@martin home]# ll random.sh*
-rwxr-xr-x 1 root root 953 Aug  2 10:45 random.sh
-rwxr-xr-x 1 root root 146 Aug  2 10:45 random.sh~

它会把原来没有加密的文件备份为 file.sh~ ,同时 file.sh 即被变成加密文件

参考地址:

http://lidao.blog.51cto.com/3388056/1914205

https://yq.aliyun.com/ziliao/65848

 

posted @ 2017-08-02 10:48  martin1317  阅读(13101)  评论(1编辑  收藏  举报