温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

shell 脚本加密

发布时间:2020-08-02 22:51:31 来源:网络 阅读:887 作者:雨后的春笋 栏目:开发技术

    想想好久没更新博客了,今天在群里看到讨论关于shell脚本加密的事情。

    想想也是,我们在写脚本有时候会配置相关账号和密码的事情,这样只要能权限都能看到该信息,非常的不安全,有没有在正常运行的情况下对文件进行加密。大致有以下两个特点:

  1. 加密文件,防止别人看到我写的具体内容。

  2. 可以对隐藏敏感性知识点,比如脚本涉及到用户和密码,如mysql

    我特意搜了一下,有两种方法,涨知识了,所以验证了一下,过程如下:


第一种方法(gzexe):基于ubuntu14.04

    这种加密方式不是非常保险的方法,但是能够满足一般的加密用途。它是使用系统自带的gzexe程序,它不但加密,同时压缩文件。

使用方法: gzexe file.sh 它会把原来没有加密的文件备份为 xx.sh~ ,同时 xx.sh 即被变成加密文件

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~ 文件,别人就彻底看不到了(自己也看不到,请酌情删除)。

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

建议使用,一方面系统自带命令,达到目的即可,没人闲的破解你的脚本,就算要破解,先进入你系统再说。


我基于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语言).

不建议使用,需要单独安装管理,比较麻烦。如果安全性要求极高,倒是可以参数。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI