3 releases (breaking)

0.8.0 Jun 26, 2024
0.7.0 May 27, 2024
0.6.0 May 8, 2024

#834 in Parser implementations

Download history 5/week @ 2024-07-29 14/week @ 2024-09-16 15/week @ 2024-09-23 28/week @ 2024-09-30

186 downloads per month

MIT license

285KB
5.5K SLoC

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

Dependencies

~2.3–3.5MB
~63K SLoC