使用Shell脚本制作Linux自解压文件

系统 Linux
前几天在使用阿里云时候发现阿里工程师提供的一个脚本里,后面都是乱码加密文件,并且文件只要一更改自解压就会失效,心里是什么技术进行代码加密,仔细查看代码后发现,原来是 .tar.gz 的二进制文件,把打包压缩以后的文件进行直接存进shell脚本后面,从而实现自解压,次篇文件 使用 tar 编码进行嵌入存文件

前几天在使用阿里云时候发现阿里工程师提供的一个脚本里,后面都是乱码加密文件,并且文件只要一更改自解压就会失效,心里是什么技术进行代码加密,仔细查看代码后发现,原来是 .tar.gz 的二进制文件,把打包压缩以后的文件进行直接存进shell脚本后面,从而实现自解压,次篇文件使用 tar 编码进行嵌入存文件。

一、系统环境说明

  • Centos 7 系统
  • shell使用自带bash

二、开始编码测试

1.先来看看阿里工程师编写的部分代码,从代码可以看出后面一连串的乱码是一个 .tar.gz 的打包压缩文件,可以看到是使用 awk 进行获取 .tar.gz 二进制开始行,所以才有那句话 #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±ÐD<8c>¯º(Ø+ö<92>^LÃî,<8c>lc^KÅh²v<8c>ÆÞ0FÑ Á^^RM¢Ñ¬]^T<8d>F<

2.编写的测试代码如下


#!/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__

3.创建一个 test 文件夹,里面建两个文件分别是 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]#

4.把文件进行打包加压嵌入 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

5.再次查看 SelfExtracting.sh 脚本,发现已经有 .tar.gz 的二进制文件,但注意此时的脚本不能修改和增加任何字符

#!/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!èªÐÆû;ÍB<8b> â"<95>âû^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ο×Ï¡Þ^]sÀ)<84>×ùKZä/<9a>¿<84>Ö^[ëê<95>ôðçù÷çËdu<94>'°ûu5XÛ½ÿ»zw|×ÿóÿ_¢nÓÿ+Xö^?Gÿ^C^@^@^@^@^@^@^@^@^@^@°I7^Z<8a>Êá^@(^@^@

6.运行脚本 SelfExtracting.sh 测试

[root@gz--vm-workstation-0001 scripts]# ./SelfExtracting.sh
Success
[root@gz--vm-workstation-0001 scripts]# ls
SelfExtracting.sh

7.聪明的你可能发现,运行显示成功,怎么不见原来的的目录呢,哈哈哈,有没有注意我们是把解压目录指向 /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]#

8.在深入研究过程中,看到一篇博客[点击这里查看],发现还有专门的命令可以搞定在脚本嵌入二进制文件: uuencode,安装这个工具请在命令行输入yum install sharutils 进行安装,原理和tar一样,具体操作留给各位去探究了咯。

总结: Shell 自解压脚本在很多情况可以使用,比如防止别人进行修改你的文件,把一些必要的文件发给别人时候,如果使用的是 linux ,可以进行嵌入到shell脚本,就发一个脚本给别人,他们一运行就可以得到他们需要的文件或者配置,如果他们自作聪明打开文件进行修改,哈哈哈哈哈哈哈,对不起,脚本直接报错运行不了。

责任编辑:华轩 来源: 微技术之家
相关推荐

2024-01-19 16:37:17

Linux操作系统

2009-08-04 09:53:21

linux创建文件命令tail命令自解压

2021-12-03 08:00:00

脚本数据容器

2014-08-08 16:17:49

shell脚本linux

2017-07-03 12:19:46

LinuxShell交换文件

2022-06-21 09:26:21

Shell脚本JavaScript

2009-11-18 13:52:30

PHP shell脚本

2021-03-14 09:28:24

Linux Shell脚本

2022-10-09 10:18:44

LinuxShell脚本

2009-12-01 09:13:51

shell脚本linux

2023-05-20 17:45:25

LinuxShell

2015-08-10 14:42:40

Explain SheShell 命令

2012-05-08 11:11:43

Linuxcrontab命令

2009-12-03 10:06:33

Ubuntushell脚本

2017-08-11 17:20:07

LinuxShell

2009-12-25 18:05:05

Linux压缩程式

2019-08-12 07:45:44

Linux脚本shell

2019-08-28 06:58:06

Linux监控脚本Shell

2018-08-15 08:45:38

2019-08-23 06:22:47

LinuxShell监控脚本
点赞
收藏

51CTO技术栈公众号