曾经有段时间使用Blackduck扫描docker image,在扫描过程中发现一个奇怪的shell脚本,shell脚本中上半部份是一段shell脚本内容,下半部分是一大段的乱码,就很好奇,这段乱码是干啥用的呢?
首先可以确认的是,这段乱码就是一些二进制内容的变体,那么脚本开发者是基于和目的去创建这样一个脚本?
我们知道,在脚本中执行二进制文件一般就是直接通过命令路径调用,比如执行系统工具:
另外一种方式就是把可执行文件经过编码后放到shell脚本。这种处理方法一般是方便打包,避免多个文件。比如现在网上比较流行的是使用uudecode,uudecode是一个用于解码uuencode编码的工具。它通常与sharutils软件包一起安装。
[root@VM-12-8-centos ~]# yum install sharutils
Loaded plugins: fastestmirror, langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* nux-dextop: mirror.li.nux.ro
* updates: mirrors.aliyun.com
http://mirrors.tencentyun.com/epel/7/x86_64/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.tencentyun.com; Unknown error"
Trying other mirror.
Resolving Dependencies
--> Running transaction check
---> Package sharutils.x86_64 0:4.13.3-8.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================================================
Installing:
sharutils x86_64 4.13.3-8.el7 base 252 k
Transaction Summary
=========================================================================================================================================================
Install 1 Package
Total download size: 252 k
Installed size: 898 k
Is this ok [y/d/N]: y
Downloading packages:
sharutils-4.13.3-8.el7.x86_64.rpm | 252 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : sharutils-4.13.3-8.el7.x86_64 1/1
Verifying : sharutils-4.13.3-8.el7.x86_64 1/1
Installed:
sharutils.x86_64 0:4.13.3-8.el7
Complete!
[root@VM-12-8-centos ~]# uuencode
uuencode (GNU sharutils) - encode a file into email friendly text - Ver. 4.13.3
USAGE: uuencode [ -<flag> | --<name> ]... [ in-file ] output-name
使用uuencode将二进制文件转换为文本:
我们写一个hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
go build 得到hello二进制文件。
在写一个shell脚本:
#!/bin/bash
echo "this is a test:"
rm -f hello
uudecode $0
./hello
rm -f hello
exit
然后使用uuencode生成二进制内容追加到shell脚本,上述第一个rm是删除本地已经存在的hello文件,再使用uudecode解码得到新的hello文件,再执行,并退出。后面追加的就是hello经过编码后的hello二进制文件内容文本形式:
uuencode hello hello >> test.sh
现在可以看下内容:
[root@VM-12-8-centos ~]# cat test.sh
#!/bin/bash
echo "this is a test:"
rm -f hello
uudecode $0
./hello
rm -f hello
exit
begin 755 hello
M?T5,1@(!`0````````````(`/@`!````8,!%``````!``````````,@!````
M`````````$``.``'`$``%P`#``8````$````0`````````!``$```````$``
M0```````B`$```````"(`0`````````0````````!`````0```"<#P``````
M`)P/0```````G`]```````!D`````````&0`````````!``````````!````
M!0````````````````!``````````$````````?A!P``````!^$'````````
M$`````````$````$`````/`'````````\$<```````#P1P``````B)$(````
M``"(D0@````````0`````````0````8`````D!````````"04````````)!0
M```````@?P$``````"C`!````````!````````!1Y71D!@``````````````
M```````````````````````````````````````````(`````````(`5!&4`
.
.
.
end
我们执行下test.sh脚本:
[root@VM-12-8-centos ~]# ./test.sh
this is a test:
Hello, World!
可以看出它打印和期待的一样。这真是神一样的设计。
所以这些情况是为了在网络传输过程或者存储过程中做数据加密或者数据完整性保证,才出现一些不可读的内容存在于shell脚本中。还有一些是有是,shell脚本需要处理一些数据块,这些数据块可能以二进制形式存在。脚本中可能会包含这些数据块的定义或拷贝操作,从而使其包含二进制内容。
总之,这些内容是不可读,就是达到不让别人知道的目的和保障数据传输可靠和完整。还有一些场景也能看到类似的情景,那就是挖矿脚本,有机会大家可以再深入挖下。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。