什么是Shell脚本程序?
它是一种程序设计语言。
作为命令语言,它交互式解释和执行用户输入的命令或者自动地解释和执行预先设定好的一连串的命令;
作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。
为什么要加密?
- 相对私密
- 不想被别人修改
- “付费”脚本程序
有哪些加密方式?
- SHC
- Gzexe
- UPX
取了三种相对常见的加密方式,还有其他就不一一举例了。
SHC加密
SHC将Shell脚本生成C源代码,然后编译成一个可执行的二进制文件。
以下操作均在Centos 7.6下操作。
安装
yum -y install shc
如果是精简系统可能没有,那就需要自行编译安装了。
yum -y install wget gcc unzip wget https://hub.fastgit.org/neurobin/shc/archive/4.0.3.zip unzip 4.0.3.zip cd shc-4.0.3 ./configure mkdir -p /usr/local/man/man1 make make install
编译安装很容易报错,如果出现报错可以使用万能的搜索引擎。
使用
先写个脚本
#!/bin/bash echo "Hello World !" echo "by:www.mkx.im"
使用shc加密脚本mkx.sh
shc -r -f mkx.sh
生成了mkx.sh.x和mkx.sh.x.c
mkx.sh.x为加密后的脚本程序,mkx.sh.x.c为C源文件。
./mkx.sh.x
cat mkx.sh.x
Gzexe加密
Gzexe是一个压缩程序,与UPX类似,都为压缩壳。
安装
Gzexe为系统自带程序无需安装
使用
继续使用上面的脚本 mkx.sh
压缩一下
gzexe mkx.sh
生成了一个mkx.sh~,mkx.sh~为源文件。mkx.sh为压缩后的脚本程序。
执行一下
bash mkx.sh
查看压缩后的脚本
cat mkx.sh
UPX加密
UPX是一款先进的可执行程序文件压缩程序,通俗来讲就是压缩壳。
安装
yum -y install upx
使用
UPX不能直接压缩Shell脚本程序,但是可以通过SHC生成的C源文件压缩。
所以这里直接使用SHC生成的mkx.sh.x.c编译成可执行文件在进行UPX压缩。
gcc mkx.sh.x.c -o mkxupx.sh upx --best mkxupx.sh
执行一下
./mkxupx.sh
查看一下
cat mkxupx.sh
解密
SHC解密
git clone https://hub.fastgit.org/yanncam/UnSHc.git cp UnSHc/latest/unshc.sh /root/ ./unshc.sh mkx.sh.x
目前4.0.3版本加密的shc脚本程序无法通过unshc.sh解密。
查询安装的SHC版本
rpm -qa | grep shc
原文如下:
Due to the many problems since shc 4.0.3, there seems to be a need for clarification. In shc 4.0.3 many structural changes have been incorporated, so that shc now makes use of various security mechanisms provided by the linux-kernel itself. Therefore, it is now almost impossible to extract the original shell script at all, if the new shc version was used. This requires a more in-depth approach, which means that a modified bash or a modified linux-kernel is needed to bypass the security measures. On the basis of a system with regular behaviour I don’t see a big chance, even if there may be weaknesses in terms of security holes. At least in my own tests I was not able to extract the shell script without sabotaging the system.
机翻:
由于自shc4.0.3以来存在许多问题,似乎需要澄清。在shc4.0.3中加入了许多结构变化,因此shc现在可以利用linux内核本身提供的各种安全机制。因此,如果使用新的shc版本,现在几乎不可能提取原始shell脚本。这需要更深入的方法,这意味着需要修改bash或修改linux内核来绕过安全措施。基于一个行为规范的系统,我认为可能性不大,即使在安全漏洞方面可能存在弱点。至少在我自己的测试中,我无法在不破坏系统的情况下提取shell脚本。
Gzexe解密
gzexe -d mkx.sh
UPX解密
upx -d mkxupx.sh
通过SHC生成的C源文件编译之后UPX压缩的脚本程序先通过upx解压缩,在通过unshc.sh脚本解密。
鸣谢
Github加速:fastgit.org