shell 脚本加密

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

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

  1. 加密文件,防止别人看到我写的具体内容。
  2. 可以对隐藏敏感性知识点,比如脚本涉及到用户和密码,如mysql

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

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

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

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

[email protected]:~/test# ls
test.sh
[email protected]:~/test# cat test.sh 
#!/bin/bash
echo `date`
[email protected]:~/test# sh test.sh 
Mon Jul 17 15:08:01 HKT 2017
[email protected]:~/test# gzexe  test.sh 
test.sh: 25.0%
[email protected]:~/test# ls
test.sh  test.sh~
[email protected]:~/test# sh test.sh
Mon Jul 17 15:08:14 HKT 2017
[email protected]:~/test# sh test.sh~
Mon Jul 17 15:08:18 HKT 2017
[email protected]:~/test# cat test.sh~
#!/bin/bash
echo `date`
[email protected]:~/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+"[email protected]"}; res=$?
else
  echo >&2 "Cannot decompress $0"
  (exit 127); res=127
fi; exit $res

此时我们可以看出test.sh 文件已经被加密了,但是不影响运行。此时你删除test.sh~ 文件,别人就彻底看不到了(自己也看不到,请酌情删除)。

[email protected]:~/test# ls
test.sh  test.sh~
[email protected]:~/test# rm test.sh~
[email protected]:~/test# ls
test.sh
[email protected]:~/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/

[[email protected] ~]# 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]
[[email protected] ~]# tar xf shc-3.8.6.tgz 
[[email protected] ~]# mkdir -p /usr/local/man/man1

这步是必须的,不然安装过程中会报错,shc将安装命令到/usr/local/bin/目录下;

将帮助文档存放在/usr/local/man/man1/目录下,如果系统中无此目录,安装时会报错,可创建此目录后再执行安装。

[[email protected] ~]# cd shc-3.8.6
[[email protected] 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 
[[email protected] test]# cat test.sh
#!/bin/bash
echo `date`
[[email protected] 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
[[email protected] 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语言).

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

时间: 2024-08-28 20:21:23

shell 脚本加密的相关文章

sch 助shell脚本加密 02

sch 助shell脚本加密 02 一.  简介 SHC(shell script compiler),即shell脚本编译器.通过SHC编译过的脚本对普通用户而言是不可读的,因此如果你想让你的代码实现加密功能,让其有效的屏蔽一些敏感信息,这个时候可以考虑使用SHC:它通常情况下是不太容易被破解的,但是还是有些人可以通过反编译SHC的方法来实现破解加密过的脚本. 二.  实验测试开始 2.1 下载并编译SHC [[email protected] ~]# wget http://www.dats

gzexe 助shell脚本加密 01

gzexe 助shell脚本加密 01 一.  简介 很多时候我们的脚本会涉及到一些私密的信息,例如:用户名,密码,或者其它重要信息的时候,我们使用一些加密的手段来屏蔽这些信息,确保系统的安全已经脚本的可流传性,通常情况下我们只需要通过系统自带的gzexe这个工具就够了. 二.         实验测试开始: 2.1 编写一个用于测试的脚本 [[email protected] ~]# vi woo.sh #! /bin/ksh echo 'PrudentWoo'' '`date +%Y-%m-

shell脚本加密形式

shell脚本在日常运维过程中显得尤为重要,通常我们在编写shel脚本中会涉及到很多参数包括密码等一些隐私信息,这个时需要将shell脚本加密,并且可执行结果.下面文章简介两种shell脚本加密方式: shc加密方式 1.shc软件安装 cd /mnt //进入或者创建目录,即选择下载位置 wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz //下载软件包 tar zxvf shc-3.8.9.tgz -C /opt //解

shell脚本加密

#shc是一个专业的加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件,这个办法很好的解决了脚本中含有IP.密码等不希望公开的问题.http://www.datsi.fi.upm.es/~frosal/sources/ shc的官网下载地址 wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgztar -xf shc-3.8.9.tgzcd shc-3.8.9mkdir -pv /usr/local

shell脚本加密笔记!

你写的shell在生产环境是否加密?反正我是没有,线上环境看就看呗,觉得没啥.其实想想,好像也有几个好处. 1.简单加密,防止别人看里面具体内容. 2.可以隐蔽脚本中的密码等信息.(比如你的备份脚本,涉及到密码等问题) 好像还有点用处,大概常用的两种方式. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 第一种方法(gzexe): 这种加密方式不是非常保险的方法,但是能够满足一般的加密用途.它是使用系统自带的gzexe程序

shell脚本加密工具

1. 加密工具 1.1 gzexe 1.1.1 说明 gzexe是用来压缩执行文件的程序.当您去执行被压缩过的执行文件时,该文件会自动解压然后继续执行,和使用一般的执行文件相同. 1.1.2 安装 Linux自带,不用单独安装. 1.1.3 用法 gzexe filename.sh 它会把原来没有加密的文件备份为 file.sh~ ,同时 file.sh 即被变成加密文件. 1.1.4 参数 -d 解开压缩文件 1.2 shc 1.2.1 说明 shc是一个专业的加密shell脚本的工具,它的作

linxu下的shell脚本加密

1.简单的加密 2.使用shc加密:下载地址:http://www.datsi.fi.upm.es/~frosal/ shc可以把shell脚本转换为一个可执行的二进制文件 参考: 1.http://www.datsi.fi.upm.es/~frosal/ 2.https://www.cnblogs.com/yuzhoushenqi/p/6950425.html 原文地址:https://www.cnblogs.com/shengulong/p/9030682.html

如何对shell脚本进行加密且不影响脚本运行

方法一:shcshc是一个加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件.shc 安装yum -y install shc使用方法:shc -r -f script-name 注意:要有-r选项, -f 后跟要加密的脚本名.运行后会生成两个文件,script-name.x 和 script-name.x.cscript-name.x是加密后的可执行的二进制文件../script-name 即可运行.script-name.x.c是生成script-name.x的

对shell脚本进行加密

用shell脚本对系统进行自动化维护,简单,便捷而且可移植性好.但shell脚本是可读写的,很有可能会泄露敏感信息,如用户名,密码,路径,IP等.同样,在shell脚本运行时会也泄露敏感信息.请问如何不影响脚本运行的前提下,对脚本进行加密? 一.shc方法 shc是一个加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件,这就很好的解决了上述问题. yum安装: yum -y install shc 编译安装: wget http://www.datsi.fi.upm