rustbin

Crates.iorustbin
lib.rsrustbin
version0.8.0
sourcesrc
created_at2024-05-08 21:29:20.211702
updated_at2024-06-26 17:17:50.151399
descriptionParsers for binary file formats (mostly Windows related).
homepage
repositoryhttps://github.com/sunilkr/rustbin
max_upload_size
id1234584
size299,256
Sunil Kumar (sunilkr)

documentation

README

RUSTBIN

Build Status

This is a learning project to understand Rust language and create fairly complex file parsers.


Structure

Rustbin is created as a library which shall add more file parsers.

Every value which is part of a header is wrapped in HeaderField struct. HeaderField struct provides 3 values:

  • value: The value of the field read from file
  • offset: Offset of the the value in file. Structs have same offset as the offset of their first member
  • rva: Relative Virtual Address
    • Applicable only to PE file format
    • Shall be same as offset wherever not applicable

Supported Now

PE (WIP)

Usage:

Note: This example works only if built on Windows OS.

extern crate rustbin;
extern crate serde_json;

use std::{env, fs::OpenOptions, io::BufReader, path::Path};

use rustbin::{pe::{PeImage, ser::min::MinPeImage}, types::Header};

fn main() {
  //Create PathBuf for self (on Windows only).
  let args:Vec<String> = env::args().collect();
  let exe_name = args.get(0).unwrap();
  let binpath = Path::new(&exe_name);

  //Open file handle in read mode.
  let Ok(f) = OpenOptions::new()
    .read(true)
    .open(binpath)
  else {
    panic!("Failed to open file in read mode.");
  };
  
  //Parse the file from offset 0.
  let Ok(parsed) = parse_file(f, ParseAs::PE) else {
    panic!("Failed to parse as `PE`.");
  };

  let ParsedAs::PE(pe_image) = parsed;

  //Convert parsed image to a minimal set of `serde::Serialize`able values without metadata.
  let min_pe = MinPeImage::from(&pe_image);

  //Serialize minimal pe image to indented json.
  let json_str = serde_json::to_string_pretty(&min_pe).unwrap();

  println!("{}", json_str);
}

Parsing:

  • DOS Header
  • File Header
  • Optional Header x64
  • Optional Header x86
  • Data Directories
  • Section Headers
  • Imports
  • Exports
  • Relocations
  • Resources

Serialize (Minimal format)

  • DOS Header
  • File Header
  • Optional Header x64
  • Optional Header x86
  • Data Directories
  • Section Headers
  • Imports
  • Exports
  • Relocations
  • Resources
Commit count: 108

cargo fmt