bytepatch: Patch byte-representable data in a bytestream

[ cli, library, mit, program ] [ Propose Tags ] [ Report a vulnerability ]
Versions [RSS] 0.2.0, 0.2.1, 0.3.0, 0.3.1, 0.4.0, 0.4.1
Change log CHANGELOG.md
Dependencies aeson (>=2.0.2.0 && <2.1), base (>=4.12 && <5), binrep (>=0.3.1 && <0.4), blake3 (>=0.3 && <0.4), bytepatch, bytestring (>=0.10 && <0.12), either (>=5.0.1.1 && <5.1), generic-optics (>=2.2.1.0 && <2.3), heystone (>=0.1.0 && <0.2), megaparsec (>=9.0 && <9.3), memory (>=0.17 && <0.18), mtl (>=2.2.2 && <2.3), optics (>=0.3 && <0.5), optparse-applicative (>=0.17 && <0.18), refined (>=0.7 && <0.9), singletons (>=3.0 && <3.1), singletons-base (>=3.1 && <3.2), singletons-th (>=3.1 && <3.2), text (>=1.2.4.1 && <1.3), text-short (>=0.1.5 && <0.2), vinyl (>=0.14.1 && <0.15), yaml (>=0.11.6.0 && <0.12) [details]
Tested with ghc ==9.2.4
License MIT
Author Ben Orchard
Maintainer Ben Orchard <thefirstmuffinman@gmail.com>
Category CLI
Home page https://github.com/raehik/bytepatch#readme
Bug tracker https://github.com/raehik/bytepatch/issues
Source repo head: git clone https://github.com/raehik/bytepatch
Uploaded by raehik at 2023-02-15T14:20:43Z
Distributions
Executables bytepatch
Downloads 535 total (15 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for bytepatch-0.4.1

[back to package description]

bytepatch

A Haskell library and CLI tool for writing declarative patches over streams. Write patchscripts (in-place edits to a file) using a configurable YAML schema, and apply them with a safe patching algorithm. On the library side, define patches of various forms over any type, apply them to pure/impure streams, and write further patch data types and streams to extend as you like.

bytepatch works with many data types:

  • read pretty binary 00 FF 1234, parse to bytes
  • read text, re-encode, null-terminate to generate a C-style string
  • read assembly, assemble down to machine code

The schema itself is highly configurable:

  • align: add a value (set in patch file) to each patch offset
  • compare: bytepatch can check expected data against actual during patching, to assert that you're patching the expected file. You can select how this check works: compare equality (exact, or match prefix), hashes
  • seek type: what the patch offsets mean. You'll likely want absolute offsets - but you may also write them as linear offsets, forward from the previous.

You may also ask bytepatch to compile a patchscript, which reads it and then outputs a YAML file with a simplified schema: pre-aligned, compare via hashes, forward seeks.

The bytepatch executable aims to be a general tool for reverse engineers, hoping to make simple binary & assembly editing more manageable. The Haskell library is quite sprawling, and has personally made for a fun investigation into type-level Haskell.

What?

If you're modifying binaries, you often end up needing to make edits in a hex editor. This is fun for a very short while, then you realise how easy it is to mess up.

bytepatch is primarily intended as a developer tool for writing a human-friendly static patchscript. It provides a nice schema for defining edits to a binary file, so that developers can read and write patches in a structured, readable format. If one wanted, they could very easily read the patch file and use it to make the changes manually (though bytepatch wants to do this for you).

bytepatch is not aimed at replacing binary patch tools such as IPS, BPS: these take an input and a result file, and generate a patch file that can be applied to the input in order to recreate the result. You need the result file in the first place. These tools both generate the patch file and apply it; bytepatch only applies the patch file, and leaves the writing to the user.

When might I want to use this?

You might find bytepatch useful if you want to define edits to be made on binary files (especially executables) that are static and in-place. Examples are:

  • instruction patching (via assembly)
  • string patching (via text)

Less relevant use cases

You can't use bytepatch directly for edits which are dynamic in nature (e.g. you need to read a file header in order to know what to change). (You could have a patch preparer that does the dynamic work and emits a bytepatch patch, but this tool is largely focused the patch representation, so you wouldn't be gaining much.)

You can't use bytepatch to make edits that aren't in-place. Edits are located using stream offsets. Allowing edits to change the length of the segment they replace would introduce issues for following edits: do we use the original offset, or do we implicitly shift them so they still write to the "same place"? There is some support in the library for such edits, however, and it would be interesting to explore.

License

Provided under the MIT license. Please see LICENSE for the full license text.