From e121bc619a47d885db67da1918ce0b801444b1a5 Mon Sep 17 00:00:00 2001 From: Garrett Dickinson Date: Thu, 26 Jan 2023 14:32:06 -0600 Subject: [PATCH] Add code for finding ELF magic number --- src/main.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8702591..9931cda 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,24 +3,34 @@ use std::env; use std::fs; use std::process::exit; +const ELF_MAGIC_NUMBER: &[u8] = &[0x7F,0x45,0x4C,0x46]; + fn main() { + // Collect our execution args let args: Vec = env::args().collect(); + // Grab our filepath from our options let file_path = &args[1]; - println!("Got target file '{}'", file_path); - if path::Path::new(file_path).exists() { println!("File exists, reading '{}'", file_path); + let contents: Result, std::io::Error> = fs::read(file_path); + if contents.is_ok() { let bytes: &Vec = &contents.expect(""); - for byte in bytes { - println!("{}", byte); + let magic_num: &[u8] = &bytes[0..4]; + + if magic_num == ELF_MAGIC_NUMBER { + println!("Found ELF Magic Number!"); + } else { + println!("[Error] Could not find magic number, is this an executable?") } } } else { println!("[Error] '{}' does not exist", file_path); exit(-1); } + + return; }