想一想很久没更新博客了,今天在群里看到讨论关于shell脚本加密的事情。mysql
想一想也是,咱们在写脚本有时候会配置相关帐号和密码的事情,这样只要能权限都能看到该信息,很是的不安全,有没有在正常运行的状况下对文件进行加密。大体有如下两个特色:redis
加密文件,防止别人看到我写的具体内容。sql
能够对隐藏敏感性知识点,好比脚本涉及到用户和密码,如mysql
shell
我特地搜了一下,有两种方法,涨知识了,因此验证了一下,过程以下:ubuntu
第一种方法(gzexe):基于ubuntu14.04centos
这种加密方式不是很是保险的方法,可是可以知足通常的加密用途。它是使用系统自带的gzexe程序,它不但加密,同时压缩文件。安全
使用方法: gzexe file.sh 它会把原来没有加密的文件备份为 xx.sh~ ,同时 xx.sh 即被变成加密文件bash
root@leco:~/test# ls test.sh root@leco:~/test# cat test.sh #!/bin/bash echo `date` root@loocha50:~/test# sh test.sh Mon Jul 17 15:08:01 HKT 2017 root@loocha50:~/test# gzexe test.sh test.sh: 25.0% root@loocha50:~/test# ls test.sh test.sh~ root@loocha50:~/test# sh test.sh Mon Jul 17 15:08:14 HKT 2017 root@loocha50:~/test# sh test.sh~ Mon Jul 17 15:08:18 HKT 2017 root@loocha50:~/test# cat test.sh~ #!/bin/bash echo `date` root@loocha50:~/test# cat test.sh #!/bin/bash skip=44 tab='' nl=' ' IFS=" $tab$nl" umask=`umask` umask 77 gztmpdir= trap 'res=$? test -n "$gztmpdir" && rm -fr "$gztmpdir" (exit $res); exit $res ' 0 1 2 3 5 10 13 15 if type mktemp >/dev/null 2>&1; then gztmpdir=`mktemp -dt` else gztmpdir=/tmp/gztmp$$; mkdir $gztmpdir fi || { (exit 127); exit 127; } gztmp=$gztmpdir/$0 case $0 in -* | */*' ') mkdir -p "$gztmp" && rm -r "$gztmp";; */*) gztmp=$gztmpdir/`basename "$0"`;; esac || { (exit 127); exit 127; } case `echo X | tail -n +1 2>/dev/null` in X) tail_n=-n;; *) tail_n=;; esac if tail $tail_n +$skip <"$0" | gzip -cd > "$gztmp"; then umask $umask chmod 700 "$gztmp" (sleep 5; rm -fr "$gztmpdir") 2>/dev/null & "$gztmp" ${1+"$@"}; res=$? else echo >&2 "Cannot decompress $0" (exit 127); res=127 fi; exit $res
此时咱们能够看出test.sh 文件已经被加密了,可是不影响运行。此时你删除test.sh~ 文件,别人就完全看不到了(本身也看不到,请酌情删除)。app
root@leco:~/test# ls test.sh test.sh~ root@leco:~/test# rm test.sh~ root@leco:~/test# ls test.sh root@leco:~/test# sh test.sh Mon Jul 17 15:10:36 HKT 2017
建议使用,一方面系统自带命令,达到目的便可,没人闲的破解你的脚本,就算要破解,先进入你系统再说。ide
我基于centos6.8 (ubuntu14.04有问题,没具体排查)
方法2: shc
shc 经常使用参数
-e date
Expiration date in dd/mm/yyyy format [none](指定过时日期)
-m message
message to display upon expiration ["Please contact your provider"](指定过时提示的信息)
-f script_name
File name of the script to compile(指定要编译的shell的路径及文件名)
-r Relax security.
Make a redistributable binary which executes on different systems running the same operat-ing system.(能够相同操做系统的不一样系统中执行)
-v Verbose compilation(编译的详细状况)
shc是一个专业的加密shell脚本的工具.它的做用是把shell脚本转换为一个可执行的二进制文件,这个办法也很好的解决了脚本中含有IP、密码等不但愿公开的问题
包下载连接:
http://www.datsi.fi.upm.es/~frosal/sources/
[root@leco ~]# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.6.tgz --2017-07-17 15:23:39-- http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.6.tgz Resolving www.datsi.fi.upm.es... 138.100.9.22 Connecting to www.datsi.fi.upm.es|138.100.9.22|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 35071 (34K) [application/x-gzip] Saving to: “shc-3.8.6.tgz” 100%[=============================================================================================================================>] 35,071 21.9K/s in 1.6s 2017-07-17 15:23:46 (21.9 KB/s) - “shc-3.8.6.tgz” saved [35071/35071] [root@leco ~]# tar xf shc-3.8.6.tgz [root@leco ~]# mkdir -p /usr/local/man/man1
这步是必须的,否则安装过程当中会报错,shc将安装命令到/usr/local/bin/目录下;
将帮助文档存放在/usr/local/man/man1/目录下,若是系统中无此目录,安装时会报错,可建立此目录后再执行安装。
[root@leco ~]# cd shc-3.8.6 [root@leco shc-3.8.6]# make install ***Installing shc and shc.1 on /usr/local ***Do you want to continue? y #此时等待输入y install -c -s shc /usr/local/bin/ install -c -m 644 shc.1 /usr/local/man/man1/ 使用方法:-f [root@leco test]# cat test.sh #!/bin/bash echo `date` [root@leco test]# shc -r -f test.sh test.sh.x.c: In function 'chkenv': test.sh.x.c:211: warning: cast from pointer to integer of different size You have new mail in /var/spool/mail/root [root@leco test]# ls test.sh test.sh.x test.sh.x.c 运行后会生成两个文件,xx.x 和 xx.x.c.
其中xx.x是加密后的可执行的二进制文件;用./xx.x便可运行,xx.x.c是生成xx.x的原文件(c语言).
不建议使用,须要单独安装管理,比较麻烦。若是安全性要求极高,却是能够参数。