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
注意:在输入make之后会提示你怎么输入,
例如:
make
make test
make strings
make install
在安装的过程中会报一个错误,提示一个目录不存在,这个时候就得手动创建这个目录了,
mkdir -p /usr/local/man/man1
不知道是不是BUG,尝试了很多次都不行,当然我装的3.8.7的,我尝试着用3.8.9的版本装,还是不行,发现里面的包还少一个软连接,建立之后还报其他的错误,所以建议用3.8.7的版本。
其实不知道这不编译有什么用处,我直接把要加密的文件放到我下载SHC解压的目录下,然后运行命令,同样可以加密。
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 admin@thegeekstuff.com for new version of this script" -f random.sh $ ./random.sh.x ./random.sh.x: has expired! Contact admin@thegeekstuff.com 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' "$@" 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 28954 1410 15234
Finally, it is worth repeating again: You should not be encrypting your shell script in the first place. But, if you decided to encrypt your shell script using shc, please remember that a smart person can still generate the original shell script from the encrypted binary that was created by shc.
Link:http://www.thegeekstuff.com/2012/05/encrypt-bash-shell-script/
相关推荐
达梦数据库CASE_SENSITIVE参数学习报告 CASE_SENSITIVE参数是达梦数据库中一个重要的参数,它控制着数据库中标识符的大小写敏感性。在安装数据库时,该参数需要进行设置,默认情况下是敏感的,且设置后不能更改。 ...
### Cost-Sensitive Learning与类别不平衡问题 #### 定义与概念 成本敏感学习(Cost-Sensitive Learning)是一种数据挖掘中的学习方法,它考虑了误分类的成本(以及其他类型的成本)。其目标是最小化总成本。成本...
《基于iMACD的MetaTrader 5智能交易系统——Sensitive EA》 在金融交易领域,MetaTrader 5(MT5)是一款广泛使用的交易平台,它提供了丰富的功能和工具,包括自动交易系统,即专家顾问(Expert Advisor,简称EA)。...
### Distance-Sensitive Bloom Filters #### 引言与背景 距离敏感型布隆过滤器(Distance-Sensitive Bloom Filters)是亚当·基尔希(Adam Kirsch)和迈克尔·米特森马赫(Michael Mitzenmacher)于2006年提出的一...
【标题】"sensitive_props-v3.zip" 是一个包含敏感属性配置文件的压缩包,可能与Android系统或安全相关的设置有关。这个版本可能是对之前版本的更新或改进,旨在增强系统的安全性或隐私保护。 【描述】"sensitive_...
这是一个可以根据特定目标进行风格转换的ipython代码,运用了vgg作为特征提取器,相关论文可在各大搜索平台上搜取,特别之处在于可以进行特定的风格转换而非针对全部图案
python实现LSH(Locality Sensitive Hashing)查找最近邻的例子,原出处在https://github.com/guoziqingbupt/Locality-sensitive-hashing,上传只是为了存储,需要的自取
在这个项目中,我们关注的是一个名为"Sensitive"的敏感词过滤类,该类提供了源代码实现。下面将详细讨论这个类可能涉及的技术和实现原理。 首先,`Sensitive.cpp`是C++源代码文件,通常包含类的定义和实现。在这个...
本研究提出了一种改进版的PageRank算法——**话题敏感PageRank**(Topic-Sensitive PageRank),旨在提高搜索引擎返回结果的准确性。原始的PageRank算法通过计算一个单一的向量来评估网页的重要性,这一向量基于网页...
simple understanding of Java log desensitization framework sensitive Java日志脱敏框架Sensitive是基于Java注解的日志脱敏工具,旨在解决项目开发过程中的日志脱敏问题。该框架提供了基于注解的方式来实现日志...
### 高中英语单词“Sensitive”的详细解析 #### 一、词汇定义与发音 - **词汇**: Sensitive(形容词) - **音标**: [ˈsensətɪv] #### 二、词汇释义 根据提供的双解释义,“sensitive”有多重含义: 1. **易受...
several non-obvious techniques for dealing with passwords and other sensitive information in background processes. These techniques require no changes to existing programs and no new security systems ...
资源对北汇信息TSN Time Sensitive Network 时间敏感网络业务进行简介
### Query-Aware Locality-Sensitive Hashing (QALSH) for Approximate Nearest Neighbor Search #### 概述 本文提出了一种新的邻近点检索技术——Query-Aware Locality-Sensitive Hashing(QALSH),旨在解决高维...
### Topic-Sensitive PageRank:一种情境敏感的网页排名算法 #### 概述 Topic-Sensitive PageRank 是一种改进版的 PageRank 算法,它能够根据特定的主题或上下文来计算网页的重要性,从而提高搜索结果的相关性和...
本版本为双向词汇版本,顺序扫描文本时,会判断正向词汇和反向词汇,有交叉的以等级高的为准,原理:http://blog.csdn.net/ranjio_z/article/details/75446147,欢迎指教询问打赏。使用说明: 1、本 Java工具包由...
在网络信息管理领域,敏感信息过滤是一项重要的技术,它旨在识别和阻止网络上的有害内容传播,特别是色情、暴力等非法信息。这些有害内容对社会和年轻人尤其有严重的负面影响,因此,研究者们对于如何有效过滤网络...
本文介绍了一个名为AppIntent的新型分析框架,用于分析Android平台上敏感数据的传输情况以检测隐私泄露。Android智能手机经常携带个人信息,这吸引了一些恶意开发者,他们可能会在Android应用程序中嵌入代码以窃取...