使用shc加密bash脚本程序

摘要
以前写看到别人写的脚本用shc加密的,我也有就了解了下。

SHC代表shell script compiler,即shell脚本编译器。通过SHC编译过的脚本程序对普通用户而言是不读的,因此如果你想保护你的代码(例如含有密钥),则可以考虑SHC;然而有些人可以通过反向编译的方式破解SHC加密过的脚本。
下面我们开始介绍:

一、使用SHC加密bash脚本程序
1.下载并编译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官方网站找到其最新源代码。
现在我们验证SHC是否正确安装:
$ ./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脚本
#!/bin/bash

echo -n “How many random numbers do you want to generate? “
read max

for (( start = 1; start <= $max; start++ ))
do
  echo -e $RANDOM
done
3.使用SHC加密bash脚本
$ ./shc -f random.sh
之后我们可以看到多出两个文件:
$ ll random.sh*
-rwxr-xr-x 1 lesca lesca   153 2012-05-16 06:34 random.sh*
-rwx–x–x 1 lesca lesca 10512 2012-05-16 06:34 random.sh.x*
-rw-r–r– 1 lesca lesca 10145 2012-05-16 06:34 random.sh.x.c
 random.sh 是原始的未加密的bash脚本
 random.sh.x 是加密的二进制格式的bash脚本
 random.sh.x.c 是random.sh的C源代码。该文件是从random.sh转换而来的,SHC就是通过将bash脚本转为C语言再编译之进行加密的。
$ file random.sh*
random.sh:     Bourne-Again shell script text executable
random.sh.x:   ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped
random.sh.x.c: ASCII C program text
4.执行加密的bash脚本
$ ./random.sh.x
How many random numbers do you want to generate? 3
15146
20741
17825
二、SHC的其他功能
1.设置脚本使用期限
我们可以通过SHC指定程序的有效期,过期后程序将失效,任何尝试运行的用户将收到错误消息。SHC使用-e dd/mm/yyyy来开启该功能:
$ ./shc -e 31/12/2011 -f random.sh
如果程序过期了,将会得到以下消息:
$ ./random.sh.x
./random.sh.x: has expired!
Please contact your provider
结合-m “message”选项,我们可以指定发生错误时输出的消息:
$ ./shc -e 31/12/2011 -m “Contact [email protected] for new version of this script” -f random.sh

$ ./random.sh.x
./random.sh.x: has expired!
Contact [email protected]me for new version of this script
2.创建可重复发布的加密脚本
 -r: 允许该脚本在同操作系统的不同硬件平台上运行
 -T: 允许让ltrace, strace那样的程序追踪脚本运行
 -v: 输出详细信息
通常-r与-T一起使用,用于创建可重复发布且可追踪的加密脚本,例如:
$ ./shc -v -r -T -f random.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec ‘%s’ “[email protected]”
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

$ ./random.sh.x
How many random numbers do you want to generate? 3
1311
19637
14891

Q: How do I encrypt my bash shell script on Linux environment? The shell script contains password, and I don’t want others who have execute access to view the shell script and get the password. Is there a way to encrypt my shell script?

A: First, as a best practice you should not be encrypting your shell script. You should really document your shell script properly so that anybody who views it understands exactly what it does. If it contains sensitive information like password, you should figure out a different approach to write the shell script without having to encrypt it.
That being said, if you still insist on encrypting a shell script, you can use SHC utility as explained below. Please note that encrypted shell script created by shc is not readable by normal users. However someone who understands how this works can extract the original shell script from the encrypted binary created by shc.
SHC stands for shell script compiler.
1. Download shc and install it
Download shc and install it as shown below.
# 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
Verify that shc is installed properly.
$ ./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. Create a Sample Shell Script
Create a sample bash shell script that you like to encrypt using shc for testing purpose.
For testing purpose, let us create the following random.sh shell script which generates random numbers. You have to specify how many random numbers you like to generate.
$ vi random.sh
#!/bin/bash

echo -n “How many random numbers do you want to generate? “
read max

for (( start = 1; start <= $max; start++ ))
do
  echo -e $RANDOM
done

$ ./random.sh
How many random numbers do you want to generate? 3
24682
1678
491
3. Encrypt the Shell Script Using shc
Encrypt the random.sh shell scripting using shc as shown below.
$ ./shc -f random.sh
This will create the following two files:
$ ls -l random.sh*
-rwxrw-r–. 1 ramesh ramesh   149 Mar 27 01:09 random.sh
-rwx-wx–x. 1 ramesh ramesh 11752 Mar 27 01:12 random.sh.x
-rw-rw-r–. 1 ramesh ramesh 10174 Mar 27 01:12 random.sh.x.c
 random.sh is the original unencrypted shell script
 random.sh.x is the encrypted shell script in binary format
 random.sh.x.c is the C source code of the random.sh file. This C source code is compiled to create the above encrypted random.sh.x file. The whole logic behind the shc is to convert the random.sh shell script to random.sh.x.c C program (and of course compile that to generate the random.sh.x executable)
$ file random.sh
random.sh: Bourne-Again shell script text executable

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

$ file random.sh.x.c
random.sh.x.c: ASCII C program text
4. Execute the Encrypted Shell Script
Now, let us execute the encrypted shell script to make sure it works as expected.
$ ./random.sh.x
How many random numbers do you want to generate? 3
7489
10494
29627
Please note that the binary itself is still dependent on the shell (the first line provided in the random.sh. i.e /bin/bash) to be available to execute the script.
5. Specifying Expiration Date for Your Shell Script
Using shc you can also specify an expiration date. i.e After this expiration date when somebody tries to execute the shell script, they’ll get an error message.
Let us say that you don’t want anybody to execute the random.sh.x after 31-Dec-2011 (I used last year date for testing purpose).
Create a new encrypted shell script using “shc -e” option to specify expiration date. The expiration date is specified in the dd/mm/yyyy format.
$ ./shc -e 31/12/2011 -f random.sh
In this example, if someone tries to execute the random.sh.x, after 31-Dec-2011, they’ll get a default expiration message as shown below.
$ ./random.sh.x
./random.sh.x: has expired!
Please contact your provider
If you like to specify your own custom expiration message, use -m option (along with -e option as shown below).
$ ./shc -e 31/12/2011 -m “Contact [email protected] for new version of this script” -f random.sh

$ ./random.sh.x
./random.sh.x: has expired!
Contact [email protected] for new version of this script
6. Create Redistributable Encrypted Shell Scripts
Apart from -e, and -m (for expiration), you can also use the following options:
 -r will relax security to create a redistributable binary that executes on other systems that runs the same operating system as the one on which it was compiled.
 -T will allow the created binary files to be traceable using programs like strace, ltrace, etc.
 -v is for verbose
Typically you might want to use both -r and -T option to craete a redistributable and tracable shell encrypted shell script as shown below.
$ ./shc -v -r -T -f random.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec ‘%s’ “[email protected]”
shc [-l]=
shc opts=
shc: cc  random.sh.x.c -o random.sh.x
shc: strip random.sh.x
s

时间: 2024-08-01 05:32:53

使用shc加密bash脚本程序的相关文章

12.3、bash脚本循环语句

1.bash脚本程序执行顺序: 顺序执行 选择执行:if.case 循环执行:for.while.until 2.for循环: 格式: for VAR in list:do 循环体 done 注释:VAR是变量,list是循环列表: list的生成方式: 直接给出     //如:1 2 3 4 {start..end}  //注意是两个点,内建列表方式 seq [start [increment]] last seq 10 seq 5 10 seq 2 2 10   //步长为2的整数列表 返

高级Bash脚本编程指南

http://tldp.org/LDP/abs/html/ 高级Bash脚本编程指南对脚本语言艺术的深入探索 本教程不承担以前的脚本或编程知识,但进展迅速走向一个中级/高级水平的指令...一直偷偷在细小的UNIX®智慧和学识.它作为一本教科书,一本手册,自学,并作为一个参考和知识的来源,壳牌的脚本技术.练习和大量的评论实例请读者参与,在这样的前提下,真正学习脚本的唯一途径是编写脚本.这本书是适合课堂使用的一般介绍编程的概念.本文件被授予公共领域.没有版权! 奉献对于安妮塔,所有魔术的来源内容表第

献给命令行重度用户的一组实用 BASH 脚本

今天,我偶然发现了一组适用于命令行重度用户的实用 BASH 脚本,这些脚本被称为 Bash-Snippets,它们对于那些整天都与终端打交道的人来说可能会很有帮助.想要查看你居住地的天气情况?它为你做了.想知道股票价格?你可以运行显示股票当前详细信息的脚本.觉得无聊?你可以看一些 YouTube 视频.这些全部在命令行中完成,你无需安装任何严重消耗内存的 GUI 应用程序. 在撰写本文时,Bash-Snippets 提供以下 19 个实用工具: Cheat – Linux 命令备忘单. Clou

MySQL之登陆密码加密认证脚本

一.登陆密码加密认证脚本应用场景 日常操作,经常明文指定了MySQL密码来登录MySQL服务,在登录成功之后就会抛出下面的警告:[root@git-server ~]# mysql -uroot -p'wujianwei' Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MyS

Linux Bash脚本基本语法知识

写在前面:如果此文有幸被某位朋友看见并发现有错的地方,希望批评指正.如有不明白的地方,愿可一起探讨. 前提:读者能够基本使用一款文本编辑器以及了解文件的权限概念. 准备工作 在/home目录创建script文件用于保存所有的脚本程序: # mkdir /home/script # cd /home/script/ Bash脚本的基本输入输出 第一个bash脚本: # vim ./hello-world.sh 说明: "#!"是一个约定标记,它告诉系统这个脚本需要什么解释器来执行 作为可

Bash脚本之if、case、read和位置参数

在学会了基本的命令之后,我们就可以使用这些命令来进行编程了.在Linux中的编程称为shell脚本,是将命令进行结合形成的类似Windows中批处理的东西.在这个脚本中,可以有变量和结构体.每一个程序所拥有的程序执行过程,例如:顺序执行.选择执行和循环执行都可以在脚本中体现出来.下面就对shell脚本进行介绍. 首先,shell脚本编程是过程式编程语言,也就是说shell脚本强调接下来要执行的步骤,就好像是人在对shell说接下来要做什么,要明确的指出每一步的方法.然后shell还是脚本类语言,

从此编写 Bash 脚本不再难【转】

从此编写 Bash 脚本不再难 原创 Linux技术 2017-05-02 14:30 在这篇文章中,我们会介绍如何通过使用 bash-support vim 插件将 Vim 编辑器安装和配置 为一个编写 Bash 脚本的 IDE. -- Aaron Kili 本文导航 -什么是 bash-support.vim 插件? …… 05% -如何在 Linux 中安装 Bash-support 插件 …… 10% -如何在 Vim 编辑器中使用 Bash-support 插件 …… 17% -如何为

用Bash脚本将Linux普通用户添加为系统管理员

将Linux普通用户添加为系统管理员在Gnome或KDE这样强大与完善的桌面环境下是非常简单的事情,一般来说在用户设置的对话框里就直接有相应选项.不过,出于简洁与高效的风格,自己目前并未使用这些高端但吃内存的“重量级”桌面环境,使用的就是最基本的X视窗+Sawfish窗口管理器的组合.在这样的环境下进行用户管理,都是通过命令行来完成.如,使用useradd命令添加新用户.不过,由useradd命令添加的用户只具有普通使用者的权限,不具备系统管理的能力.这样一来,就给一些常见的操作带来不便,如,使

Linux应用环境实战10:Bash脚本编程语言中的美学与哲学(转)

阅读目录 一.一切皆是字符串 二.引用和元字符 三.字符串从哪里来.到哪里去 四.再加上一点点的定义,就可以推导出整个Bash脚本语言的语法了 五.输入输出重定向 六.Bash脚本语言的美学:大道至简 总结: 我承认,我再一次地当了标题党.但是不可否认,这一定是一篇精华随笔.在这一篇中,我将探讨Bash脚本语言中的美学与哲学. 这不是一篇Bash脚本编程的教程,但是却能让人更加深入地了解Bash脚本编程,更加快速地学习Bash脚本编程. 阅读这篇随笔,不需要你有Bash编程的经验,但一定要和我一