Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any thoughts regarding an scemu lib? #6

Closed
Thell opened this issue Jul 6, 2022 · 14 comments
Closed

Any thoughts regarding an scemu lib? #6

Thell opened this issue Jul 6, 2022 · 14 comments

Comments

@Thell
Copy link

Thell commented Jul 6, 2022

Hi,
Thanks for making and releasing this! Being as light weight and fast as this is I'm thinking of using it in a lil project that needs to analyze some chunks of iced-x86 disassembled code and then read some register values. It works fine from the command line for exploration but would be nicer (in my case) to use as a lib where I could run and then query the state as needed. I was thinking of just pulling out the non console oriented bits but thought you might want to know that there is a use-case for your code as a lib as an alternative to working with the likes of unicorn within other projects.
It looks like you kept the code nicely separated... is it possible you already had that thought and have some input on why it would/wouldn't be a good thing to do?

@Thell
Copy link
Author

Thell commented Jul 6, 2022

Just wanted to let you know that my simple use for this seems to work. I wrapped pretty much all of the outputs with verbose tests and fiddled around with returning instead of termination/console spawning but it wasn't bad at all.

I initialize once and then re-use it to load chunks and get my value.

fn init_emu() -> Emu {
    let map_path = PathBuf::from("./src/client_commands/emu/maps64")
        .as_os_str()
        .to_str()
        .unwrap()
        .to_string();

    let mut emu = Emu::new();
    let mut cfg = emu::Config::new();
    cfg.maps_folder = map_path;
    cfg.is_64bits = true;
    emu.set_config(cfg);
    emu.init();

    emu
}

fn get_code(
    emu: &mut Emu,
    code_bytes: &[u8],
    start_addr: u64,
    stop_addr: u64,
) -> Result<u32, Box<dyn std::error::Error>> {
    emu.cfg.code_base_addr = start_addr;
    emu.cfg.entry_point = start_addr;
    emu.cfg.console_addr = stop_addr;

    emu.regs.clear::<64>();
    emu.load_code_bytes(code_bytes);
    emu.run();

    let code: u32 = emu.regs.get_r8d().try_into()?;
    Ok(code)
}

@sha0coder
Copy link
Owner

Hello Thell, looks really interesting this would allow the people do their implementations, I thought about doing a scripting language for automating the binary analysis, but for now I'm busy implementing more API, and windows internals, and I also have to implement 64bits PE loading.

I'm focused on using this for malware analysis mainly, the most automatically possible, but for sure can be used for other purposes and also can be forked to do cool modifications.

@sha0coder
Copy link
Owner

I adapted the code a bit for being used programatically. People can modify the main.rs and emulate specific malware functions.

I managed to generate the crypto-key of Xloader malware with a program using scemu :)

@Thell
Copy link
Author

Thell commented Aug 18, 2022

Cool! I'll have to take a look at that.
And this reminds me that one other thing I ended up doing while using scemu as a lib was to make the maps embed into the executable so it'd be usable without install dependencies. I used RustEmbed and it worked nicely. Since only the x64 was needed I used...

use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "src/client_commands/emu/maps64"]
struct EmuAsset;

@sha0coder
Copy link
Owner

sha0coder commented Aug 29, 2022 via email

@sha0coder
Copy link
Owner

I have the lib in local ok passing tests for 32bits and 64bits with the embedding thing working ok.
Im going to publish it soon.

@sha0coder
Copy link
Owner

crates.io don't allow that size, probably because of the maps.
"the remote server responded with an error: max upload size is: 10485760"

@sha0coder
Copy link
Owner

To use the lib, the developer will have to download the maps from a github release maps32.zip and maps64.zip and put them on specific place.
Is the unique solution i see :/

@sha0coder
Copy link
Owner

@Thell
Copy link
Author

Thell commented Sep 5, 2022

Could the embed be compressed?

@sha0coder
Copy link
Owner

yes i could embed just two zips and decompress from rust, but it would make slower the initialization. And not sure if fits the size allowed on crates.io

@sha0coder
Copy link
Owner

btw there is a bug in github and all my commits appear like the user wit00 by error, when github will solve the ticket i will publish the code of the lib.

@sha0coder
Copy link
Owner

@Thell
Copy link
Author

Thell commented Sep 9, 2022

🥳 Looking forward to trying it out!
I guess we can close this now... woot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants