Skip to content

Commit 5627fa6

Browse files
authored
Merge pull request #143 from acheron2302/main
Remove dump on exit
2 parents e458615 + 537153d commit 5627fa6

File tree

7 files changed

+106
-83
lines changed

7 files changed

+106
-83
lines changed

crates/libmwemu/src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub struct Config {
4444
pub command: Option<String>,
4545
pub definitions: HashMap<u64, Definition>,
4646
pub entropy: bool,
47+
pub shellcode: bool
4748
}
4849

4950
impl Default for Config {
@@ -79,7 +80,7 @@ impl Config {
7980
console_addr: 0,
8081
entry_point: constants::CFG_DEFAULT_BASE,
8182
exit_position: 0,
82-
dump_on_exit: true, // TODO: a way to make it false/set it through cli + lib
83+
dump_on_exit: false, // TODO: a way to make it false/set it through cli + lib
8384
dump_filename: Some("dumps/emu.bin".to_string()), // TODO: a way to set it through cli + lib
8485
code_base_addr: constants::CFG_DEFAULT_BASE,
8586
is_64bits: false,
@@ -94,6 +95,7 @@ impl Config {
9495
command: None,
9596
definitions: HashMap::new(),
9697
entropy: false,
98+
shellcode: false,
9799
}
98100
}
99101
}

crates/libmwemu/src/emu/loaders.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ impl Emu {
485485
//let map_name = self.filename_to_mapname(filename);
486486
//self.cfg.filename = map_name;
487487

488-
if Elf32::is_elf32(filename) {
488+
if Elf32::is_elf32(filename) && !self.cfg.shellcode {
489489
self.linux = true;
490490
self.cfg.is_64bits = false;
491491

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

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

523523
self.regs_mut().rip = ep;
524-
} else if self.cfg.is_64bits && PE64::is_pe64(filename) {
524+
} else if self.cfg.is_64bits && PE64::is_pe64(filename) && !self.cfg.shellcode {
525525
log::info!("PE64 header detected.");
526526
let clear_registers = false; // TODO: this needs to be more dynamic, like if we have a register set via args or not
527527
let clear_flags = false; // TODO: this needs to be more dynamic, like if we have a flag set via args or not

crates/libmwemu/src/emu/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ mod threading;
4949
mod tls;
5050
mod trace;
5151
mod winapi;
52-
#[macro_use]
53-
mod utils;
5452

5553
pub struct Emu {
5654
// Global/shared state

crates/libmwemu/src/emu/utils.rs

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +0,0 @@
1-
#[macro_export]
2-
macro_rules! color {
3-
("Black") => {
4-
"\x1b[0;30m"
5-
};
6-
("Red") => {
7-
"\x1b[0;31m"
8-
};
9-
("Green") => {
10-
"\x1b[0;32m"
11-
};
12-
("Orange") => {
13-
"\x1b[0;33m"
14-
};
15-
("Blue") => {
16-
"\x1b[0;34m"
17-
};
18-
("Purple") => {
19-
"\x1b[0;35m"
20-
};
21-
("Cyan") => {
22-
"\x1b[0;36m"
23-
};
24-
("LightGray") => {
25-
"\x1b[0;37m"
26-
};
27-
("DarkGray") => {
28-
"\x1b[1;30m"
29-
};
30-
("LightRed") => {
31-
"\x1b[1;31m"
32-
};
33-
("LightGreen") => {
34-
"\x1b[1;32m"
35-
};
36-
("Yellow") => {
37-
"\x1b[1;33m"
38-
};
39-
("LightBlue") => {
40-
"\x1b[1;34m"
41-
};
42-
("LightPurple") => {
43-
"\x1b[1;35m"
44-
};
45-
("LightCyan") => {
46-
"\x1b[1;36m"
47-
};
48-
("White") => {
49-
"\x1b[1;37m"
50-
};
51-
("nc") => {
52-
"\x1b[0m"
53-
};
54-
("ClearScreen") => {
55-
"\x1bc"
56-
};
57-
($unknown:tt) => {
58-
compile_error!(concat!(
59-
"Unknown color name: '",
60-
$unknown,
61-
"'. Valid options are: \
62-
Black, Red, Green, Orange, Blue, Purple, Cyan, LightGray, \
63-
DarkGray, LightRed, LightGreen, Yellow, LightBlue, \
64-
LightPurple, LightCyan, White, nc, ClearScreen"
65-
))
66-
};
67-
}

crates/libmwemu/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ pub mod threading;
4242
pub mod tracing;
4343
pub mod winapi;
4444

45+
// re-export the helper so the macro can reach it
46+
pub use utils::color_enabled;
47+
4548
#[cfg(test)]
4649
mod tests;
4750
mod utils;
48-
4951
use config::Config;
5052
use emu::Emu;
5153

crates/libmwemu/src/utils.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::sync::atomic::{AtomicBool, Ordering};
2+
3+
static COLOR_ENABLED: AtomicBool = AtomicBool::new(true);
4+
15
// TODO: remove the code when 'likely' and 'unlikely' are stable
26
#[inline(always)]
37
#[cold]
@@ -21,4 +25,85 @@ pub(crate) fn unlikely(b: bool) -> bool {
2125
} else {
2226
false
2327
}
28+
}
29+
30+
pub fn disable_color() {
31+
COLOR_ENABLED.store(false, Ordering::Relaxed);
32+
}
33+
34+
pub fn enable_color() {
35+
COLOR_ENABLED.store(true, Ordering::Relaxed);
36+
}
37+
38+
#[inline]
39+
pub fn color_enabled() -> bool {
40+
COLOR_ENABLED.load(Ordering::Relaxed)
41+
}
42+
43+
#[macro_export]
44+
macro_rules! color {
45+
("Black") => {
46+
if $crate::color_enabled() { "\x1b[0;30m" } else { "" }
47+
};
48+
("Red") => {
49+
if $crate::color_enabled() { "\x1b[0;31m" } else { "" }
50+
};
51+
("Green") => {
52+
if $crate::color_enabled() { "\x1b[0;32m" } else { "" }
53+
};
54+
("Orange") => {
55+
if $crate::color_enabled() { "\x1b[0;33m" } else { "" }
56+
};
57+
("Blue") => {
58+
if $crate::color_enabled() { "\x1b[0;34m" } else { "" }
59+
};
60+
("Purple") => {
61+
if $crate::color_enabled() { "\x1b[0;35m" } else { "" }
62+
};
63+
("Cyan") => {
64+
if $crate::color_enabled() { "\x1b[0;36m" } else { "" }
65+
};
66+
("LightGray") => {
67+
if $crate::color_enabled() { "\x1b[0;37m" } else { "" }
68+
};
69+
("DarkGray") => {
70+
if $crate::color_enabled() { "\x1b[1;30m" } else { "" }
71+
};
72+
("LightRed") => {
73+
if $crate::color_enabled() { "\x1b[1;31m" } else { "" }
74+
};
75+
("LightGreen") => {
76+
if $crate::color_enabled() { "\x1b[1;32m" } else { "" }
77+
};
78+
("Yellow") => {
79+
if $crate::color_enabled() { "\x1b[1;33m" } else { "" }
80+
};
81+
("LightBlue") => {
82+
if $crate::color_enabled() { "\x1b[1;34m" } else { "" }
83+
};
84+
("LightPurple") => {
85+
if $crate::color_enabled() { "\x1b[1;35m" } else { "" }
86+
};
87+
("LightCyan") => {
88+
if $crate::color_enabled() { "\x1b[1;36m" } else { "" }
89+
};
90+
("White") => {
91+
if $crate::color_enabled() { "\x1b[1;37m" } else { "" }
92+
};
93+
("nc") => {
94+
if $crate::color_enabled() { "\x1b[0m" } else { "" }
95+
};
96+
("ClearScreen") => {
97+
if $crate::color_enabled() { "\x1bc" } else { "" }
98+
};
99+
($unknown:tt) => {
100+
compile_error!(concat!(
101+
"Unknown color name: '",
102+
$unknown,
103+
"'. Valid options are: \
104+
Black, Red, Green, Orange, Blue, Purple, Cyan, LightGray, \
105+
DarkGray, LightRed, LightGreen, Yellow, LightBlue, \
106+
LightPurple, LightCyan, White, nc, ClearScreen"
107+
))
108+
};
24109
}

crates/mwemu/src/main.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ fn main() {
136136
.arg(clap_arg!("cmd", "", "cmd", "launch a console command", "COMMAND"))
137137
.arg(clap_arg!("entropy", "", "entropy", "display changes in the entropy"))
138138
.arg(clap_arg!("multithread", "", "multithread", "enable multithread emulation"))
139+
.arg(clap_arg!("is_shellcode", "", "is_shellcode", "Force the binary to be shellcode"))
139140
.get_matches();
140141

141142
if !matches.is_present("filename") {
@@ -397,22 +398,22 @@ fn main() {
397398

398399
// stack trace
399400
if matches.is_present("stack_trace") {
400-
emu.cfg.stack_trace = true;
401+
emu.cfg.stack_trace = false;
401402
}
402403

403404
// test mode
404405
if matches.is_present("test_mode") {
405-
emu.cfg.test_mode = true;
406+
emu.cfg.test_mode = false;
406407
}
407408

408409
// trace fpu
409410
if matches.is_present("fpu") {
410-
emu.fpu_mut().trace = true;
411+
emu.fpu_mut().trace = false;
411412
}
412413

413414
// trace flags
414415
if matches.is_present("flags") {
415-
emu.cfg.trace_flags = true;
416+
emu.cfg.trace_flags = false;
416417
}
417418

418419
// cmd
@@ -425,6 +426,10 @@ fn main() {
425426
);
426427
}
427428

429+
if matches.is_present("is_shellcode") {
430+
emu.cfg.shellcode = true;
431+
}
432+
428433
// args
429434
if matches.is_present("args") {
430435
log::info!(
@@ -449,16 +454,14 @@ fn main() {
449454
.format(CustomLogFormat::new())
450455
.file(filename)
451456
.chan_len(Some(100000)),
452-
)
453-
.unwrap();
457+
).unwrap();
454458
} else {
455459
fast_log::init(
456460
Config::new()
457461
.format(CustomLogFormat::new())
458462
.console()
459463
.chan_len(Some(100000)),
460-
)
461-
.unwrap();
464+
).unwrap();
462465
}
463466

464467
// definitions

0 commit comments

Comments
 (0)