Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/libmwemu/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Config {
pub command: Option<String>,
pub definitions: HashMap<u64, Definition>,
pub entropy: bool,
pub shellcode: bool
}

impl Default for Config {
Expand Down Expand Up @@ -79,7 +80,7 @@ impl Config {
console_addr: 0,
entry_point: constants::CFG_DEFAULT_BASE,
exit_position: 0,
dump_on_exit: true, // TODO: a way to make it false/set it through cli + lib
dump_on_exit: false, // TODO: a way to make it false/set it through cli + lib
dump_filename: Some("dumps/emu.bin".to_string()), // TODO: a way to set it through cli + lib
code_base_addr: constants::CFG_DEFAULT_BASE,
is_64bits: false,
Expand All @@ -94,6 +95,7 @@ impl Config {
command: None,
definitions: HashMap::new(),
entropy: false,
shellcode: false,
}
}
}
8 changes: 4 additions & 4 deletions crates/libmwemu/src/emu/loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ impl Emu {
//let map_name = self.filename_to_mapname(filename);
//self.cfg.filename = map_name;

if Elf32::is_elf32(filename) {
if Elf32::is_elf32(filename) && !self.cfg.shellcode {
self.linux = true;
self.cfg.is_64bits = false;

Expand All @@ -497,13 +497,13 @@ impl Emu {
let stack = self.alloc("stack", stack_sz, Permission::READ_WRITE);
self.regs_mut().rsp = stack + (stack_sz / 2);
//unimplemented!("elf32 is not supported for now");
} else if Elf64::is_elf64(filename) {
} else if Elf64::is_elf64(filename) && !self.cfg.shellcode {
self.linux = true;
self.cfg.is_64bits = true;
self.maps.clear();

let base = self.load_elf64(filename);
} else if !self.cfg.is_64bits && PE32::is_pe32(filename) {
} else if !self.cfg.is_64bits && PE32::is_pe32(filename) && !self.cfg.shellcode {
log::info!("PE32 header detected.");
let clear_registers = false; // TODO: this needs to be more dynamic, like if we have a register set via args or not
let clear_flags = false; // TODO: this needs to be more dynamic, like if we have a flag set via args or not
Expand All @@ -521,7 +521,7 @@ impl Emu {
}*/

self.regs_mut().rip = ep;
} else if self.cfg.is_64bits && PE64::is_pe64(filename) {
} else if self.cfg.is_64bits && PE64::is_pe64(filename) && !self.cfg.shellcode {
log::info!("PE64 header detected.");
let clear_registers = false; // TODO: this needs to be more dynamic, like if we have a register set via args or not
let clear_flags = false; // TODO: this needs to be more dynamic, like if we have a flag set via args or not
Expand Down
2 changes: 0 additions & 2 deletions crates/libmwemu/src/emu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ mod threading;
mod tls;
mod trace;
mod winapi;
#[macro_use]
mod utils;

pub struct Emu {
// Global/shared state
Expand Down
67 changes: 0 additions & 67 deletions crates/libmwemu/src/emu/utils.rs
Original file line number Diff line number Diff line change
@@ -1,67 +0,0 @@
#[macro_export]
macro_rules! color {
("Black") => {
"\x1b[0;30m"
};
("Red") => {
"\x1b[0;31m"
};
("Green") => {
"\x1b[0;32m"
};
("Orange") => {
"\x1b[0;33m"
};
("Blue") => {
"\x1b[0;34m"
};
("Purple") => {
"\x1b[0;35m"
};
("Cyan") => {
"\x1b[0;36m"
};
("LightGray") => {
"\x1b[0;37m"
};
("DarkGray") => {
"\x1b[1;30m"
};
("LightRed") => {
"\x1b[1;31m"
};
("LightGreen") => {
"\x1b[1;32m"
};
("Yellow") => {
"\x1b[1;33m"
};
("LightBlue") => {
"\x1b[1;34m"
};
("LightPurple") => {
"\x1b[1;35m"
};
("LightCyan") => {
"\x1b[1;36m"
};
("White") => {
"\x1b[1;37m"
};
("nc") => {
"\x1b[0m"
};
("ClearScreen") => {
"\x1bc"
};
($unknown:tt) => {
compile_error!(concat!(
"Unknown color name: '",
$unknown,
"'. Valid options are: \
Black, Red, Green, Orange, Blue, Purple, Cyan, LightGray, \
DarkGray, LightRed, LightGreen, Yellow, LightBlue, \
LightPurple, LightCyan, White, nc, ClearScreen"
))
};
}
4 changes: 3 additions & 1 deletion crates/libmwemu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ pub mod threading;
pub mod tracing;
pub mod winapi;

// re-export the helper so the macro can reach it
pub use utils::color_enabled;

#[cfg(test)]
mod tests;
mod utils;

use config::Config;
use emu::Emu;

Expand Down
85 changes: 85 additions & 0 deletions crates/libmwemu/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::sync::atomic::{AtomicBool, Ordering};

static COLOR_ENABLED: AtomicBool = AtomicBool::new(true);

// TODO: remove the code when 'likely' and 'unlikely' are stable
#[inline(always)]
#[cold]
Expand All @@ -21,4 +25,85 @@ pub(crate) fn unlikely(b: bool) -> bool {
} else {
false
}
}

pub fn disable_color() {
COLOR_ENABLED.store(false, Ordering::Relaxed);
}

pub fn enable_color() {
COLOR_ENABLED.store(true, Ordering::Relaxed);
}

#[inline]
pub fn color_enabled() -> bool {
COLOR_ENABLED.load(Ordering::Relaxed)
}

#[macro_export]
macro_rules! color {
("Black") => {
if $crate::color_enabled() { "\x1b[0;30m" } else { "" }
};
("Red") => {
if $crate::color_enabled() { "\x1b[0;31m" } else { "" }
};
("Green") => {
if $crate::color_enabled() { "\x1b[0;32m" } else { "" }
};
("Orange") => {
if $crate::color_enabled() { "\x1b[0;33m" } else { "" }
};
("Blue") => {
if $crate::color_enabled() { "\x1b[0;34m" } else { "" }
};
("Purple") => {
if $crate::color_enabled() { "\x1b[0;35m" } else { "" }
};
("Cyan") => {
if $crate::color_enabled() { "\x1b[0;36m" } else { "" }
};
("LightGray") => {
if $crate::color_enabled() { "\x1b[0;37m" } else { "" }
};
("DarkGray") => {
if $crate::color_enabled() { "\x1b[1;30m" } else { "" }
};
("LightRed") => {
if $crate::color_enabled() { "\x1b[1;31m" } else { "" }
};
("LightGreen") => {
if $crate::color_enabled() { "\x1b[1;32m" } else { "" }
};
("Yellow") => {
if $crate::color_enabled() { "\x1b[1;33m" } else { "" }
};
("LightBlue") => {
if $crate::color_enabled() { "\x1b[1;34m" } else { "" }
};
("LightPurple") => {
if $crate::color_enabled() { "\x1b[1;35m" } else { "" }
};
("LightCyan") => {
if $crate::color_enabled() { "\x1b[1;36m" } else { "" }
};
("White") => {
if $crate::color_enabled() { "\x1b[1;37m" } else { "" }
};
("nc") => {
if $crate::color_enabled() { "\x1b[0m" } else { "" }
};
("ClearScreen") => {
if $crate::color_enabled() { "\x1bc" } else { "" }
};
($unknown:tt) => {
compile_error!(concat!(
"Unknown color name: '",
$unknown,
"'. Valid options are: \
Black, Red, Green, Orange, Blue, Purple, Cyan, LightGray, \
DarkGray, LightRed, LightGreen, Yellow, LightBlue, \
LightPurple, LightCyan, White, nc, ClearScreen"
))
};
}
19 changes: 11 additions & 8 deletions crates/mwemu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ fn main() {
.arg(clap_arg!("cmd", "", "cmd", "launch a console command", "COMMAND"))
.arg(clap_arg!("entropy", "", "entropy", "display changes in the entropy"))
.arg(clap_arg!("multithread", "", "multithread", "enable multithread emulation"))
.arg(clap_arg!("is_shellcode", "", "is_shellcode", "Force the binary to be shellcode"))
.get_matches();

if !matches.is_present("filename") {
Expand Down Expand Up @@ -397,22 +398,22 @@ fn main() {

// stack trace
if matches.is_present("stack_trace") {
emu.cfg.stack_trace = true;
emu.cfg.stack_trace = false;
}

// test mode
if matches.is_present("test_mode") {
emu.cfg.test_mode = true;
emu.cfg.test_mode = false;
}

// trace fpu
if matches.is_present("fpu") {
emu.fpu_mut().trace = true;
emu.fpu_mut().trace = false;
}

// trace flags
if matches.is_present("flags") {
emu.cfg.trace_flags = true;
emu.cfg.trace_flags = false;
}

// cmd
Expand All @@ -425,6 +426,10 @@ fn main() {
);
}

if matches.is_present("is_shellcode") {
emu.cfg.shellcode = true;
}

// args
if matches.is_present("args") {
log::info!(
Expand All @@ -449,16 +454,14 @@ fn main() {
.format(CustomLogFormat::new())
.file(filename)
.chan_len(Some(100000)),
)
.unwrap();
).unwrap();
} else {
fast_log::init(
Config::new()
.format(CustomLogFormat::new())
.console()
.chan_len(Some(100000)),
)
.unwrap();
).unwrap();
}

// definitions
Expand Down