ITPub博客

首页 > 应用开发 > > 介绍两种bash脚本加密机制

介绍两种bash脚本加密机制

作者:snowmanjy 时间:2019-09-05 13:43:24 0 删除 编辑

 在写bash脚本时,有时候会涉及到 用户名、密码、IP等敏感信息,当涉及到这些信息的时候,我们不能被别人发现。就需要一种加密的机制来保护这些信息。

下面介绍两种不同的加密机制。

    1、linux自带的加密命令 gzexe  

使用该命令,主要是防止一些菜鸟和一些不太懂得加密的新手。这种加密不是很安全,但是能满足一般的加密用途(隐藏敏感信息)。它不但加密而且还是压缩文件。

[root@super ~]# which gzexe

/usr/bin/gzexe

[root@super ~]# rpm -qf /usr/bin/gzexe

gzip-1.3.12-18.el6.x86_64

新建一个脚本进行测试,脚本如下:

[root@super ~]# vim hello.sh

#!/bin/bash

echo hello world .

使用gzexe 进行加密:

[root@super ~]# gzexe hello.sh

hello.sh:19.4%

[root@super ~]# ll hello.sh*

-rw-r--r-- 1 root root 861 Jun 15 15:43 hello.sh

-rw-r--r-- 1 root root  31 Jun 15 15:42 hello.sh~

加密后,会变成两个文件,其中 hello.sh 为加密后的文件,hello.sh~为源文件。

[root@super ~]# sh hello.sh

hello world .

[root@super ~]# sh hello.sh~

hello world .

两个执行的结果都是一致的。

但是使用gzexe解密也是非常简单的,这就是使用gzexe的弊端。

[root@super ~]# cat hello.sh

#!/bin/sh

skip=44

tab=''

nl='

这是加密后脚本的一部分。如果我们要解密。只需要执行 gzexe -d 。

ll hello.sh*

-rw-r--r-- 1 root root  31 Jun 15 15:48 hello.sh

-rw-r--r-- 1 root root 861 Jun 15 15:43 hello.sh~

也是会生成两个文件,这是源文件为:hello.sh  加密文件为:hello.sh~

二、使用SHC加密。

SHC代表shell script compiler, 即shell脚本编译器。通过SHC编译过后的脚本程序对普通用户来说,一般都是不可读的。因此想要正真保护代码就得用到SHC加密。

    1.下载并编译SHC

[root@super bash]# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz            #下载压缩包

[root@super bash]# tar zxvf shc-3.8.7.tgz

[root@super bash]# cd shc-3.8.7[root@super shc-3.8.7]# make

[root@super 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

    2.建立一个bash脚本测试。

[root@super shc-3.8.7]# vim hello.sh

#!/bin/bash

echo hello world .

    3.使用SHC加密bash脚本

[root@super shc-3.8.7]# ll hello.sh*

-rw-r--r-- 1 root root    31 Jun 15 15:57 hello.sh

-rwx--x--x 1 root root 11624 Jun 15 16:01 hello.sh.x

-rw-r--r-- 1 root root  9411 Jun 15 16:01 hello.sh.x.c

这样会产生三个文件。

hello.sh                    是原始未加密的脚本。

hello.sh.x                是加密的二进制格式的bash脚本。

hello.sh.x.c是原始脚本的C源代码,该文件是从源bash脚本中转换而来的。SHC就是通过将bash脚本转为C语言在编译加密的。

[root@super shc-3.8.7]# file hello.sh*

hello.sh:     Bourne-Again shell script text executable

hello.sh.x:   ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

hello.sh.x.c: ASCII C program text

    4.执行bash脚本。

[root@super shc-3.8.7]# ./hello.sh.x

hello world .

二、SHC的其他功能

    1、设置脚本使用期限

可以通过SHC设置有限期,当过了有限期,用户再次使用时,就会收到报错信息。SHC使用-e  dd/mm/yyyy来实现该功能

[root@super shc-3.8.7]# ./shc -e 14/6/2015 -f hello.sh

[root@super shc-3.8.7]# ./hello.sh.x

./hello.sh.x: has expired!

Please contact your provider

[root@super shc-3.8.7]# date

Mon Jun 15 16:11:15 CST 2015

    2.创建可灵活使用的脚本

可以使用./shc --h  来查看帮助。通常把以下选项进行连用:

-r :允许该脚本在同操作系统的不同硬件上运行。

-T:运行然后调试命令来跟踪脚本。

-v:输出详细信息。

[root@super shc-3.8.7]# ./shc -v -T -r -f hello.sh

shc shll=bash

shc [-i]=-c

shc [-x]=exec '%s' "$@"

shc [-l]=

shc opts=

shc: cc  hello.sh.x.c -o hello.sh.x

shc: strip hello.sh.x

shc: chmod go-r hello.sh.x

[root@super shc-3.8.7]# ll hello.sh*

-rw-r--r-- 1 root root   30 Jun 15 16:17 hello.sh

-rwx--x--x 1 root root 9336 Jun 15 16:17 hello.sh.x

-rw-r--r-- 1 root root 9630 Jun 15 16:17 hello.sh.x.c

[root@super shc-3.8.7]# file !$

file hello.sh*

hello.sh:     Bourne-Again shell script text executable

hello.sh.x:   ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

hello.sh.x.c: ASCII C program text

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1973/viewspace-3148323/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 关于sql的问题
请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    104
  • 访问量
    826