Linux下的upx命令学习

upx学习

今天我们来学习一款给应用加壳的软件,叫做upx(the Ultimate Packer for eXecutables)

首先我们先看下它**百科的释义:

UPX (the Ultimate Packer for eXecutables)是一款先进的可执行程序文件压缩器,压缩过的可执行文件体积缩小50%-70% ,这样减少了磁盘占用空间、网络上传下载的时间和其它分布以及存储费用。 通过 UPX 压缩过的程序和程序库完全没有功能损失和压缩之前一样可正常地运行,对于支持的大多数格式没有运行时间或内存的不利后果。 UPX 支持许多不同的可执行文件格式 包含 Windows 95/98/ME/NT/2000/XP/CE 程序和动态链接库、DOS 程序、 Linux 可执行文件和核心。

那么百科的释义呢很简单,主要有两点,一个是加壳,一个是压缩。
具体原理嘛,我也想知道二哈

好,那么废话不多说,Let's do it

小试牛刀

先准备好材料

//hello.cpp
#include <iostream>

using namespace std;
int main() {
    int i = 10;
    for(;i > 0;i--) {
        cout<<"hello,i = "<<(10 - i)<<endl;
    }
    return 0;
}

还是熟悉的配方。好,那么我们upx一下

 $ g++ hello.cpp -o hello.out
 $ upx hello.out -o hello.upx.out
 Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2017
UPX 3.94        Markus Oberhumer, Laszlo Molnar & John Reiser   May 12th 2017

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
upx: hello.strip.out: NotCompressibleException

Packed 1 file: 0 ok, 1 error.
 $ 

那么是什么问题呢,我上网找了一下。原来是upx不支持太小的文件。小于40kb的文件就不支持了。
好,那么本文就到此结束啦,谢谢大家的观看。坏笑

再次尝试

等等,我辈岂是如此轻言放弃之人。呲牙

//hello.go
package main

import "fmt"
func main() {
    i := 10
    for ; i > 0; i-- {
        fmt.Printf("hello,i = %d\n",(10 - i))
    }
}

然后编译

 $ go build hello.go
 $ ls -l
total 2040
-rwxr-xr-x 1 root root 2080925 Jul 13 03:12 hello
-rw-r--r-- 1 root root     141 Jul 13 03:00 hello.go
 $ 

那么我们得到了一个20M的可执行文件,让我们来upx一下它

 $ upx hello -o hello.upx.out
 $ ls -l
total 4592
-rwxr-xr-x 1 root root 2080925 Jul 13 03:12 hello
-rw-r--r-- 1 root root     141 Jul 13 03:00 hello.go
-rwxr-xr-x 1 root root 1143336 Jul 13 03:23 hello.upx.out
 $ 

嗯,确实小了不少,upx处理后,变成了11M;看小减少了百分之多少。
(2080925-1143336)/2080925≈45.07%
嗯,比strip多一点。
那么看看,能不能再strip

 $ strip hello.upx.out -o hello.upxs.out
strip: error: the input file 'hello.upx.out' has no sections
 $ 

那么我们分析一下应该不难得知,upx把壳给加上了,导致strip找不到那些标记

反向操作

那么呢,我们是不能把这个过程给反一下。是不是就可以了呢
好,那么说干就干呲牙

 $ strip hello -p -o hello.s.out
 $ ls -l
total 4592
-rwxr-xr-x 1 root root 2080925 Jul 13 03:12 hello
-rw-r--r-- 1 root root     141 Jul 13 03:00 hello.go
-rwxr-xr-x 1 root root 1463768 Jul 13 03:23 hello.s.out
-rwxr-xr-x 1 root root 1143336 Jul 13 03:23 hello.upx.out
 $ upx hello.s.out -o hello.su.out
                       Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2017
UPX 3.94        Markus Oberhumer, Laszlo Molnar & John Reiser   May 12th 2017

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
   1463768 ->    574340   39.24%   linux/amd64   hello.su.out

Packed 1 file.
 $ ls -l
total 5156
-rwxr-xr-x 1 root root 2080925 Jul 13 03:12 hello
-rw-r--r-- 1 root root     141 Jul 13 03:00 hello.go
-rwxr-xr-x 1 root root 1463768 Jul 13 03:23 hello.s.out
-rwxr-xr-x 1 root root  574340 Jul 13 03:23 hello.su.out
-rwxr-xr-x 1 root root 1143336 Jul 13 03:23 hello.upx.out
 $ 

嗯,很好。套娃成功坏笑
看下减少了多少,1-574340/2080925≈72.40%
那么这个减少的量是比较多的,让我们来看看可以运行吗

 $ ./hello.su.out
hello,i = 0
hello,i = 1
hello,i = 2
hello,i = 3
hello,i = 4
hello,i = 5
hello,i = 6
hello,i = 7
hello,i = 8
hello,i = 9
 $ 

那么我们可以看到,这个是可以正常运行的。

继续研究

那么我们来看看upx可以带哪些选项吧,

 $ upx
                        Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2017
UPX 3.94        Markus Oberhumer, Laszlo Molnar & John Reiser   May 12th 2017

Usage: upx [-123456789dlthVL] [-qvfk] [-o file] file..

Commands:
  -1     compress faster                   -9    compress better
  -d     decompress                        -l    list compressed file
  -t     test compressed file              -V    display version number
  -h     give more help                    -L    display software license
  Options:
  -q     be quiet                          -v    be verbose
  -oFILE write output to 'FILE'
    -f     force compression of suspicious files
  -k     keep backup files
file..   executables to (de)compress

Type 'upx --help' for more detailed help.

UPX comes with ABSOLUTELY NO WARRANTY; for details visit https://upx.github.io
 $ 

命令:

有1到9的压缩等级;
-d 不压缩;
-l 列出压缩的文件;
-t 测试压缩文件;
-h 显示帮助文档
-V 显示版本号
-L 显示软件许可证

选项:

-q 安静模式
-v 详细模式
-o 大家都懂,输出到文件
-f 强制压缩可以的文件
-k 保持备份文件

嗯, 好像没有什么值得深究的功能, 这个压缩等级大家可以按照自己的需要去调整。
好,就这样。二哈

总结

最后我们来总结一下
首先这个upx之后的东西,嗯,它的时间戳还是保留之前的
然后呢,upx操作之后呢,这个sections会变得面目全非。所以再去strip呢,就不行了
所以我们需要采取反向操作先strip然后再upx
然后呢就是压缩的等级,-1是优先速度,-9是优先质量。那么中间的按照这个去推断。
好了,本文到这里就结束啦,如果喜欢这篇文章,记得点赞。谢谢呲牙

posted @ 2020-07-17 00:59  '昵称'undeclared  阅读(5131)  评论(1编辑  收藏  举报