You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将C可执行二进制文件存入C源码并向已有二进制追加该内容?

Hey there! Let's walk through your two C binary manipulation questions step by step—both are straightforward once you know the right tools and patterns, so I'll include practical code examples to make it concrete.

1. 将C可执行二进制文件存储到C源码中

The core idea here is to convert the raw binary data into a C-style unsigned char array, since you can't directly embed binary blobs in source code. The easiest way to do this is with the xxd utility (preinstalled on most Linux/macOS systems), but I'll also show a custom C program for conversion if you don't have xxd available.

Method 1: Use xxd (Quick & Easy)

Suppose you have an executable file named my_executable. Run this command in your terminal:

xxd -i my_executable > embedded_binary.h

This generates a header file embedded_binary.h with content like this:

unsigned char my_executable[] = {
  0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  // ... rest of the binary bytes
};
unsigned int my_executable_len = 12345; // actual length of your binary

Now you can include this header in your C source code and use the array directly. For example, to write the embedded binary back to a file:

#include "embedded_binary.h"
#include <stdio.h>

int main() {
    FILE *output = fopen("restored_executable", "wb");
    if (!output) {
        perror("Failed to open output file");
        return 1;
    }

    // Write the embedded binary data to the file
    fwrite(my_executable, 1, my_executable_len, output);
    fclose(output);

    // Don't forget to make it executable if needed (Linux/macOS)
    // system("chmod +x restored_executable");

    printf("Binary successfully restored!\n");
    return 0;
}

Method 2: Custom Conversion Program (If No xxd)

If xxd isn't available, you can write a tiny C program to convert any binary file into a C array:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <binary_file>\n", argv[0]);
        return 1;
    }

    FILE *bin_file = fopen(argv[1], "rb");
    if (!bin_file) {
        perror("Failed to open binary file");
        return 1;
    }

    // Get the size of the binary file
    fseek(bin_file, 0, SEEK_END);
    long file_size = ftell(bin_file);
    fseek(bin_file, 0, SEEK_SET);

    unsigned char *buffer = malloc(file_size);
    if (!buffer) {
        perror("Failed to allocate memory");
        fclose(bin_file);
        return 1;
    }

    // Read the entire binary file into the buffer
    fread(buffer, 1, file_size, bin_file);
    fclose(bin_file);

    // Print the C array definition
    printf("unsigned char %s[] = {\n", argv[1]);
    for (long i = 0; i < file_size; i++) {
        printf("0x%02x, ", buffer[i]);
        // Add a newline every 16 bytes for readability
        if ((i + 1) % 16 == 0) printf("\n");
    }
    printf("\n};\n");
    printf("size_t %s_len = %ld;\n", argv[1], file_size);

    free(buffer);
    return 0;
}

Compile this program, run it with your target binary as an argument, and copy the output into your source code or a header file.


2. 通过C程序将自身编写的C程序的二进制代码添加到另一个二进制文件

This builds on the first question—we'll first embed the target binary into our C source, then write a program that appends this embedded data to an existing binary file. Here's the step-by-step process:

Step 1: Embed Your Target Binary into Source Code

Use either the xxd method or custom conversion program above to turn your compiled C executable (let's call it my_payload) into a C array. Save this into a header file like payload.h.

Step 2: Write the Injection Program

This program will take an existing binary file as input, open it in append mode, and write the embedded payload to its end:

#include "payload.h"
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <target_binary>\n", argv[0]);
        return 1;
    }

    // Open the target binary in append + binary mode
    // "ab" ensures we write to the end and don't modify existing data
    FILE *target = fopen(argv[1], "ab");
    if (!target) {
        perror("Failed to open target binary");
        return 1;
    }

    // Write the embedded payload to the target file
    size_t bytes_written = fwrite(my_payload, 1, my_payload_len, target);
    if (bytes_written != my_payload_len) {
        fprintf(stderr, "Warning: Only %zu bytes written (expected %zu)\n",
                bytes_written, my_payload_len);
    } else {
        printf("Successfully appended payload to %s\n", argv[1]);
    }

    fclose(target);
    return 0;
}

Key Notes

  • Always use binary mode ("ab" instead of "a") when opening files—this prevents the system from converting newline characters, which would corrupt the binary data.
  • On Linux/macOS, if you need to extract the payload later, you'll need to track where it starts in the target file (you could add a marker before the payload, or store the payload length at the end of the file).
  • Ensure you have write permissions for the target binary file—if it's a system file or read-only, you may need to run the program with elevated privileges.

内容的提问来源于stack exchange,提问作者Saheen Shoukath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:19:32