postject

repository·main·Indexed 42 days ago

https://github.com/nodejs/postject

A tool and Node.js module for injecting arbitrary read-only resources into Mach-O, PE, and ELF executable formats for use at runtime. Version 1.0.0-alpha.6 provides a CLI and a programmatic inject() function to embed binary data, supporting custom sentinel fuses and API header generation for C/C++ applications.

Tokens
1.1K
Snippets
4
Records
6
Agent score
4%

What's inside postject

  1. How postject handles different executable formats

    main

    Postject uses standard practices for embedding binary data to ensure maximum compatibility with each platform's executable format:

    • Windows (PE): Resources are added to the .rsrc section using the RT_RCDATA (raw data) type. Runtime lookup uses FindResource and LoadResource APIs.
    • macOS (Mach-O): Resources are added as sections inside a new segment. The default segment name is __POSTJECT. Runtime lookup uses APIs from <mach-o/getsect.h>.
    • Linux (ELF): Resources are added as notes.
  2. Customizing the postject sentinel fuse

    main

    Runtime implementers looking for injected resources can search the binary compiled with postject-api.h for a specific sentinel fuse.

    By default, the fuse is: POSTJECT_SENTINEL_fce680ab2cc467b6e072b8b5df1996b2:0. To indicate a resource has been injected, the last character is flipped to 1.

    You can define a custom fuse by setting the POSTJECT_SENTINEL_FUSE macro before including postject-api.h and passing that same string to the postject CLI using the --sentinel-fuse <sentinel_fuse> flag.

  3. Inject resources programmatically with inject()

    main

    You can use the postject Node.js module to inject resources directly within your code using the inject function. This function is asynchronous and requires a filename, a resource name, and a Buffer containing the data.

    const { inject } = require('postject');
    
    await inject('a.out', 'lol', Buffer.from('Hello, world!'));
  4. Use the postject CLI to inject resources

    main

    The postject CLI allows you to inject arbitrary read-only resources into executable formats (Mach-O, PE, ELF) for use at runtime.

    Usage Syntax: postject [options] <filename> <resource_name> <resource>

    Arguments:

    • filename: The executable to inject into.
    • resource_name: The resource name to use (this becomes the section name on Mach-O and ELF, or the resource name for PE).
    • resource: The actual data/resource to inject.

    Options:

    • --macho-segment-name <segment_name>: Name for the Mach-O segment (defaults to __POSTJECT).
    • --output-api-header: Outputs the API header to stdout.
    • --overwrite: Overwrites the resource if it already exists.
    • -h, --help: Displays help information.
    $ postject -h
    Usage: postject [options] <filename> <resource_name> <resource>
    
    Inject arbitrary read-only resources into an executable for use at runtime
    
    Arguments:
      filename                             The executable to inject into
      resource_name                        The resource name to use (section name on Mach-O and ELF, resource name for PE)
      resource                             The resource to inject
    
    Options:
      --macho-segment-name <segment_name>  Name for the Mach-O segment (default: "__POSTJECT")
      --output-api-header                  Output the API header to stdout
      --overwrite                          Overwrite the resource if it already exists
      -h, --help                           display help command
  5. Output the postject API header

    main

    If you are developing a C/C++ application that needs to access the injected resources, you can use the postject CLI to output the required API header to stdout using the --output-api-header flag.

    postject <filename> <resource_name> <resource> --output-api-header
    postject my_app MY_DATA data.bin --output-api-header