C语言 在编译的ELF可执行文件中修改静态字节数组[已关闭]

b09cbbtk  于 2023-05-22  发布在  其他
关注(0)|答案(3)|浏览(503)

已关闭,此问题需要更focused。目前不接受答复。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。

昨天关门了。
Improve this question
我有一个场景,我不想为我的用户提供一个实用程序来创建一个压缩的自解压可执行文件(类似于UPX,但具有其他属性)。
目前这是通过压缩可执行文件,然后生成包含字节数组的c源代码来完成的,该字节数组包含可执行文件:

#include "exdata.c"

exdata.c:

const unsigned char compressedData[] = { 0x28,0xB5... }
const size_t uncompressedSize =  3697664;

问题是,我希望能够改变这个字节数组,而不重新编译程序,使我的用户能够使用实用程序,而不需要他们安装一个c编译器。
我可以使用一个“占位符”字节数组,在编译后的二进制文件中找到那个值,然后用真实的的数据替换它吗?或者我可以以某种方式将数据添加到一个自定义的“段”,并只是修改?
编辑:我发现了一个名为lief的Python库,它似乎能够修改段内容,但修改一个数据比现有数据更多的段似乎非常复杂,因为需要处理所有其他段和地址+偏移量

o4hqfura

o4hqfura1#

简单的愚蠢的方式来做这件事。
1.让可执行文件知道它的长度。简单的方法是使用const,然后构建、测量、再构建。这对自动化来说是一种痛苦。
1.将压缩流连接到可执行文件。
1.在可执行源代码中,打开/proc/self/exe,查找到指定的字节并开始阅读。
我已经做到了这一点,正是这些步骤,并提供了一个小工具来做2.我把这个工具称为第四步。

jgwigjjp

jgwigjjp2#

@CraigEstey这是一个替代方案,但理想情况下,为了方便和编译时间,希望避免使用docker- Richard
好吧,所以没有容器。但是,许多商业安装程序使用sh脚本作为初始提取器。
下面是我自己开发的一种格式:

#!/usr/bin/bash -
# ovcbin/ovcext.sh -- shell based extractor
#
# this is a self extracting archive
#
# INSTALL:
# (1) save this block to a file (e.g.) /tmp/archive
# (2) create a directory (e.g.) /tmp/extract
# (3) cd to /tmp/extract
# (4) For a dry run, if you wish to see what the archive contains, type:
#       bash /tmp/archive
# (5) To actually extract the archive, type:
#       bash /tmp/archive -go
# (6) This will create the following subdirs under /tmp/extract:
#       hello_package

function ovcopt()
{
    local opt
    local match
    local tmp="/tmp/ovcopt.$$"

    cp /dev/null $tmp

    for opt in $*
    do
        match=$(_ovcopt $opt)
        ovcdbg "ovcopt: TRY match=$match opt=$opt"
    done

    source $tmp

    rm -f $tmp
}

function _ovcopt
{
    local opt=$1
    local sym
    local match=0

    case $opt in
        -*)
            sym=$(echo $opt | sed -e s/-/opt_/ -e '/=/!s/$/=1/')
            ovcdbg "ovcopt: EVAL opt='$opt' sym='$sym'"
            echo "$sym" >> $tmp
            match=1
        ;;
    esac

    echo "$match"
}

function ovcext()
{
    local file=$1
    local opt_chmod
    local opt_b64
    local opt_z
    local dir
    local found=0
    local go=0

    ovcopt $*

    ovcdbg "ovcext: DEBUG_CAE opt_go='$opt_go'"

    if [ -n "$opt_go" ]; then
        go=1
    else
        exit 99
    fi

    while [ 1 ]
    do
        if [ -e "$file" ]; then
            found=1

            if [ -n "$opt_f" ] ; then
                echo "file already exists -- $file (overwriting!)"
                break
            fi

            echo "file already exists -- $file (rerun with -f to force)"
            go=0
            file=/dev/null
            break
        fi

        if [ $go -gt 0 ]; then
            echo "extracting file $file ..."
            dir=$(dirname $file)
            mkdir -p $dir
            break
        fi

        echo "extracting $file (DRY RUN)"
        file=/dev/null
        break
    done

    if [ -n "$zipflg_spl" ]; then
        base64 -d | $zipflg_cmd -d > $file
    else
        cat > $file
    fi

    if [ $go -ne 0 ]; then
        if [ -n "$opt_chmod" ]; then
            chmod $opt_chmod $file
        fi
    fi
}

function ovcdbg()
{

    if [ -n "$ZPXHOWOVC" ]; then
        echo "$*" 1>&2
    fi
}

ovcopt $*
zipflg_spl=1; zipflg_cmd="gzip";

ovcext hello_package/hello.c -b64 -zgzip << '%%%EOF%%%'
H4sICO3/Y2QCA292Y2V4dF96aXBfMQBTzsxLzilNSVWwKS5JyczXy7Dj4srMK+HKTczM0yjLz0zR
5Krm4uIsKAIKpmkoZaTm5OQrlOcX5aTE5ClpWgOlilJ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mkbQ8J1RyaOTeacGjRAZxVum3bCUgravKU
UsjmzeEbHdbJOP6sLh8rQeRs3mxaBUaOrKlncmAmjxeLLHtzSCqP7dzBQkPixblpGAcCN3STT/Ej
GIuq+gyJFzRDjaf6RzoNZZJJk4VSfLyUy2c7c9DvUJrCZ1Lx7FwB8jM5DIppyjEY9OMjqWpBhjTw
VUFFdjSTN7BIqEjcUGfhlwY1rms0hnF1ivWYqaxekUwLs/1MC34MGSvTOeghk5pBf8wCzMwgICQO
nXgaehupVCauGIaeGy8Z0PHjWXW8NAm5KYXJipgrTGhW0vi4rh7jEpzBKj/Gqr99amP3VL5+Utm/
h9jWdYhj/YjTbWwthdtX9qth6xKL7m126nbY83H3FQa0L2GP7wW+Xi5r3J6Pz0M+u/8Bx/oUp11s
rUl0rEd1MPsjDK9jdQ851oXeT+x71PDx/kM++3pWrfgdZmtJ3J7PCy767P5y/0UHP8rWprjM5w+X
fPb4O/23xhUspqJjPWzBZ18Pc8aP1/9BZt/vWF+L+e16AfY82Gn/GKnssVS9Hsk3vmlbov3nHfZ8
fhOLuPe/iIM/4bC39stiCiXB3Z7TFx32fBxzlj2YblzC/9OO86+yL5X7+edsvzMO+8q+T6a8ZYny
v+Ww5/O0A8z+6hLln4P/cNV6rLUPVNxdP+TgP2TrDz7HenLLDdpfJva9jqx9tuKV8Wv1OlLI0Y6/
YfXn9nweuZBwb29n+b9z2Fvj5y73/ue0f9Vhz8e1sS739nJeP/7G8uL2fFzZfoP2bzCsy4Fz+ztr
2HOOawrBqnQ+H97D7A8Ert//WgX3/ajObLJff2r531HD/jKzF3zXt99Yw/5nfSb/8RLl99ew/7r5
lJ38dQn7XYJ7/L90t8ll8frxt107Fk38oW+wxsEn05tdrr8Sse8TZvVTtsHc58Tr+x+tYf+VIZMf
FK5vv+zfyzPiKLlQAAA=
%%%EOF%%%
oalqel3c

oalqel3c3#

我认为GCC和Objcopy可以解决你的很多问题:

  • 使用gcc的__attribute__扩展将默认的压缩图像放在它自己的部分中
  • 用用户的压缩图像制作一个文件。
  • 一个用户脚本运行objcopy来用用户的文件替换你的节
$ more replaceable_contents.c 
#include <stdio.h>

struct {
    unsigned char sz;
    const char data[];
} datablob __attribute__((section("COMPRESSEDDATA"))) = {12, "Hello World!"};

int global_counter = 0;

int main(int argc, const char* argv[]) {

    for (int i;i<argc;i++) {
        printf("printing %d characters: ", datablob.sz);
        printf("%.*s\n", datablob.sz, datablob.data);
    }
}

$ gcc replaceable_contents.c && ./a.out 2
printing 12 characters: Hello World!
printing 12 characters: Hello World!

$ more text.txt
2This is a test of the emergency compression system

$ objcopy --update-section COMPRESSEDDATA=text.txt a.out a2.out
objcopy: a2.out: section .bss lma 0x201020 adjusted to 0x201044

$ ./a2.out 2
printing 50 characters: This is a test of the emergency compression system
printing 50 characters: This is a test of the emergency compression system

相关问题