-
-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86e1769
commit f7a127e
Showing
57 changed files
with
1,408 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env python | ||
''' | ||
This script disassembles an ELF/PE/Mach-O binary using LIEF's extended API | ||
Note: this script is only working with the extended version of LIEF | ||
''' | ||
import argparse | ||
import lief | ||
|
||
from pathlib import Path | ||
|
||
def disassemble(target: lief.Binary, addr: int): | ||
for inst in target.disassemble(addr): | ||
print(inst) | ||
|
||
def main() -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("file", help='Target file', type=Path) | ||
parser.add_argument("address", help='Address to disassemble', | ||
type=lambda x: int(x,0)) | ||
|
||
args = parser.parse_args() | ||
|
||
target_file = args.file | ||
addr = args.address | ||
|
||
target = lief.parse(target_file) | ||
if target is None: | ||
print(f"Can't load: {target_file}") | ||
return 1 | ||
|
||
disassemble(target, addr) | ||
|
||
if __name__ == "__main__": | ||
raise SystemExit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Engine: | ||
def __init__(self, *args, **kwargs) -> None: ... | ||
|
||
class Instruction: | ||
def __init__(self, *args, **kwargs) -> None: ... | ||
def to_string(self) -> str: ... | ||
@property | ||
def address(self) -> int: ... | ||
@property | ||
def mnemonic(self) -> str: ... | ||
@property | ||
def raw(self) -> bytes: ... | ||
@property | ||
def size(self) -> int: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
target_sources(pyLIEF PRIVATE | ||
init.cpp | ||
pyEngine.cpp | ||
pyInstruction.cpp | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "asm/init.hpp" | ||
#include "asm/pyAssembly.hpp" | ||
|
||
|
||
namespace LIEF::assembly { | ||
class Engine; | ||
class Instruction; | ||
} | ||
|
||
namespace LIEF::assembly::py { | ||
void init(nb::module_& m) { | ||
nb::module_ mod = m.def_submodule("assembly"); | ||
|
||
create<LIEF::assembly::Engine>(mod); | ||
create<LIEF::assembly::Instruction>(mod); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef PY_LIEF_ASM_INIT_H | ||
#define PY_LIEF_ASM_INIT_H | ||
#include "pyLIEF.hpp" | ||
|
||
namespace LIEF::assembly::py { | ||
void init(nb::module_& m); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef PY_LIEF_ASM_H | ||
#define PY_LIEF_ASM_H | ||
#include "pyLIEF.hpp" | ||
|
||
namespace LIEF::assembly::py { | ||
|
||
namespace assembly = LIEF::assembly; | ||
|
||
template<class T> | ||
void create(nb::module_&); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "LIEF/asm/Engine.hpp" | ||
#include "asm/pyAssembly.hpp" | ||
|
||
#include <nanobind/stl/string.h> | ||
#include <nanobind/stl/unique_ptr.h> | ||
|
||
namespace LIEF::assembly::py { | ||
template<> | ||
void create<assembly::Engine>(nb::module_& m) { | ||
nb::class_<assembly::Engine> obj(m, "Engine", | ||
R"doc( | ||
This class interfaces the assembler/disassembler support | ||
)doc"_doc | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <ostream> | ||
#include <sstream> | ||
#include "pyLIEF.hpp" | ||
#include "LIEF/asm/Instruction.hpp" | ||
#include "asm/pyAssembly.hpp" | ||
|
||
#include <nanobind/stl/string.h> | ||
#include <nanobind/stl/unique_ptr.h> | ||
|
||
namespace LIEF::assembly::py { | ||
template<> | ||
void create<assembly::Instruction>(nb::module_& m) { | ||
nb::class_<assembly::Instruction> obj(m, "Instruction", | ||
R"doc( | ||
This class represents an assembly instruction | ||
)doc"_doc | ||
); | ||
|
||
obj | ||
.def_prop_ro("address", &Instruction::address, | ||
R"doc(Address of the instruction)doc"_doc | ||
) | ||
|
||
.def_prop_ro("size", &Instruction::size, | ||
R"doc(Size of the instruction in bytes)doc"_doc | ||
) | ||
|
||
.def_prop_ro("mnemonic", &Instruction::mnemonic, | ||
R"doc(Instruction mnemonic (e.g. ``br``))doc"_doc | ||
) | ||
|
||
.def("to_string", &Instruction::to_string, | ||
R"doc(Representation of the current instruction in a pretty assembly way)doc"_doc | ||
) | ||
|
||
.def_prop_ro("raw", [] (const Instruction& inst) { | ||
const std::vector<uint8_t>& raw = inst.raw(); | ||
return nb::bytes((const char*)raw.data(), raw.size()); | ||
}, R"doc(Raw bytes of the current instruction)doc"_doc | ||
) | ||
|
||
LIEF_DEFAULT_STR(Instruction) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//! Assembly/Disassembly Module | ||
//! | ||
//! ### Getting Started | ||
//! | ||
//! One can start disassembling code by using the different functions exposes in the | ||
//! [`crate::generic::Binary`] trait: | ||
//! | ||
//! ``` | ||
//! fn disassemble(target: &dyn lief::generic::Binary) { | ||
//! for inst in target.disassemble_symbol("_entrypoint") { | ||
//! println!("{}", inst.to_string()); | ||
//! } | ||
//! } | ||
//! ``` | ||
|
||
pub mod instruction; | ||
|
||
#[doc(inline)] | ||
pub use instruction::{Instructions, Instruction}; |
Oops, something went wrong.