前言: 前幾天在使用阿里云時(shí)候發(fā)現(xiàn)阿里工程師提供的一個(gè)腳本里寇壳,后面都是亂碼加密文件瓷耙,并且文件只要一更改自解壓就會(huì)失效朱躺,心里是什么技術(shù)進(jìn)行代碼加密,仔細(xì)查看代碼后發(fā)現(xiàn)搁痛,原來是 .tar.gz
的二進(jìn)制文件长搀,把打包壓縮以后的文件進(jìn)行直接存進(jìn)shell腳本后面,從而實(shí)現(xiàn)自解壓鸡典,次篇文件 使用 tar 編碼進(jìn)行嵌入存文件
一源请、系統(tǒng)環(huán)境說明
- Centos 7 系統(tǒng)
- shell使用自帶bash
二、開始編碼測試
- 先來看看阿里工程師編寫的部分代碼轿钠,從代碼可以看出后面一連串的亂碼是一個(gè)
.tar.gz
的打包壓縮文件巢钓,可以看到是使用 awk 進(jìn)行獲取.tar.gz
二進(jìn)制開始行,所以才有那句話 #This line must be the last line of the file 疗垛。
#! /bin/bash
#
# Copyright (c) 2009-2015 Aliyun Corporation
# All Rights Reserved
.......
ARCHIVE=`awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' "$0"`
tail -n+$ARCHIVE "$0" | tar -ixzvm -C $tmp_dir > /dev/null 2>&1 3>&1
if [ $? -ne 0 ]
then
echo "Error: prepare env error, failed create extract xbstream..." >&2
rm -rf $tmp_dir
exit 1
fi
......
exit 0
#This line must be the last line of the file
__ARCHIVE_BELOW__
^_<8b>^H^@_÷^U^@^C?[wX^TW×?à<82><8b>¨Y°!<92>?<8a>5±DD<8c>ˉo(?+?<92>^L??,<8c>lc^K?h2v<8c>?T0F? á^^RM¢??]^T<8d>F<
- 編寫的測試代碼如下
#!/bin/bash
#Test shell self-extracting
TmpDir=/tmp
ARCHIVE=$(awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' "$0")
tail -n+$ARCHIVE "$0" | tar -xzvm -C $TmpDir > /dev/null 2>&1 3>&1
if [ $? == 0 ];then
echo "Success"
else
echo "Fail"
fi
exit 0
#This line must be the last line of the file
__ARCHIVE_BELOW__
- 創(chuàng)建一個(gè) test 文件夾症汹,里面建兩個(gè)文件分別是 TestA 和 TestB ,文件里面分別寫著
This is TestA
和This is TestB
[root@gz--vm-workstation-0001 scripts]# mkdir test
[root@gz--vm-workstation-0001 scripts]# cd test/
[root@gz--vm-workstation-0001 test]# echo "This is TestA" > TestA
[root@gz--vm-workstation-0001 test]# echo "This is TestB" > TestB
[root@gz--vm-workstation-0001 test]# cat TestA
This is TestA
[root@gz--vm-workstation-0001 test]# cat TestB
This is TestB
[root@gz--vm-workstation-0001 test]#
- 把文件進(jìn)行打包加壓嵌入 shell 腳本(也可以使用cat .tar.gz 文件嵌入 shell 腳本)贷腕,并且刪除 test 文件夾
[root@gz--vm-workstation-0001 scripts]# tar -zcvm test >> SelfExtracting.sh
test/
test/TestA
test/TestB
[root@gz--vm-workstation-0001 scripts]# ls
SelfExtracting.sh test
[root@gz--vm-workstation-0001 scripts]# rm -rf test
[root@gz--vm-workstation-0001 scripts]# ls
SelfExtracting.sh
- 再次查看
SelfExtracting.sh
腳本背镇,發(fā)現(xiàn)已經(jīng)有.tar.gz
的二進(jìn)制文件,但注意此時(shí)的腳本不能修改和增加任何字符
#!/bin/bash
#Test shell self-extracting
TmpDir=/tmp
ARCHIVE=$(awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' "$0")
tail -n+$ARCHIVE "$0" | tar -xzvm -C $TmpDir > /dev/null 2>&1 3>&1
if [ $? == 0 ];then
echo "Success"
else
echo "Fail"
fi
exit 0
#This line must be the last line of the file
__ARCHIVE_BELOW__
^_<8b>^H^@^\<8e>¨Z^@^Cí?M
?0^P<86>á?=En`|ùéú<9e>?^Wp!èaD??;íB<8b> a"<95>a?^P<9a>@^B^Y?2í§)?M]Nμ1<96>Y=?e-?<89><8d>?<94><82><9e>^S^W}kl?\Wq<9d>òq′?<8c>?<90>?<9d>?′?Qy??×??T^]sà)<84>×ùKZ?/<9a>?<84>?^[?ê<95>?e?ù÷??du<94>'°?u5X????zw|×?ó?_¢nó?+X?^?G?^C^@^@^@^@^@^@^@^@^@^@°I7^Z<8a>êá^@(^@^@
- 運(yùn)行腳本
SelfExtracting.sh
測試
[root@gz--vm-workstation-0001 scripts]# ./SelfExtracting.sh
Success
[root@gz--vm-workstation-0001 scripts]# ls
SelfExtracting.sh
- 聰明的你可能發(fā)現(xiàn)泽裳,運(yùn)行顯示成功瞒斩,怎么不見原來的的目錄呢,哈哈哈涮总,有沒有注意我們是把解壓目錄指向 /tmp 下面胸囱,返回去查看上面腳本定義的變量,所以查看如下
[root@gz--vm-workstation-0001 test]# cd /tmp/test
[root@gz--vm-workstation-0001 test]# ls
TestA TestB
[root@gz--vm-workstation-0001 test]# cat TestA
This is TestA
[root@gz--vm-workstation-0001 test]# cat TestB
This is TestB
[root@gz--vm-workstation-0001 test]#
- 在深入研究過程中瀑梗,看到一篇博客[點(diǎn)擊這里查看]烹笔,發(fā)現(xiàn)還有專門的命令可以搞定在腳本嵌入二進(jìn)制文件:
uuencode
,安裝這個(gè)工具請?jiān)诿钚休斎?br>yum install sharutils
進(jìn)行安裝抛丽,原理和tar一樣谤职,具體操作留給各位去探究了咯