2017-03-30 16:56:49 +02:00
|
|
|
/* Copyright 2017 R. Thomas
|
|
|
|
* Copyright 2017 Quarkslab
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#include "init.hpp"
|
|
|
|
#include "LIEF/Abstract/Binary.hpp"
|
|
|
|
#include "LIEF/ELF/Binary.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace LIEF;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using getter_t = T (Binary::*)(void) const;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using setter_t = void (Binary::*)(T);
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
using it_t = T (Binary::*)(void);
|
|
|
|
|
2017-08-22 09:47:29 +02:00
|
|
|
template<class T, class P>
|
|
|
|
using no_const_func = T (Binary::*)(P);
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
void init_LIEF_Binary_class(py::module& m) {
|
2018-03-13 16:49:02 +01:00
|
|
|
py::class_<Binary, Object>(m, "Binary")
|
2017-07-22 08:23:17 +02:00
|
|
|
|
|
|
|
.def_property_readonly("format",
|
|
|
|
&Binary::format,
|
|
|
|
"File format " RST_CLASS_REF(lief.EXE_FORMATS) " of the underlying binary.")
|
|
|
|
|
2017-08-01 17:57:38 +02:00
|
|
|
.def_property_readonly("is_pie",
|
|
|
|
&Binary::is_pie,
|
|
|
|
"Check if the binary is position independent")
|
|
|
|
|
2017-08-02 09:23:55 +02:00
|
|
|
.def_property_readonly("has_nx",
|
|
|
|
&Binary::has_nx,
|
|
|
|
"Check if the binary uses ``NX`` protection")
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
.def_property("name",
|
|
|
|
static_cast<getter_t<const std::string&>>(&Binary::name),
|
|
|
|
static_cast<setter_t<const std::string&>>(&Binary::name),
|
|
|
|
"Binary's name")
|
|
|
|
|
|
|
|
.def_property_readonly("header",
|
2017-09-07 14:45:41 +02:00
|
|
|
&Binary::header,
|
2017-03-30 16:56:49 +02:00
|
|
|
"Binary's header")
|
|
|
|
|
|
|
|
.def_property_readonly("entrypoint",
|
|
|
|
&Binary::entrypoint,
|
|
|
|
"Binary's entrypoint")
|
|
|
|
|
|
|
|
.def_property_readonly("sections",
|
2017-09-07 14:45:41 +02:00
|
|
|
static_cast<it_t<it_sections>>(&Binary::sections),
|
2017-03-30 16:56:49 +02:00
|
|
|
"Return a list in **read only** of binary's abstract " RST_CLASS_REF(lief.Section) "",
|
|
|
|
py::return_value_policy::reference_internal)
|
|
|
|
|
2017-09-05 22:00:31 +02:00
|
|
|
.def_property_readonly("relocations",
|
|
|
|
static_cast<it_t<it_relocations>>(&Binary::relocations),
|
|
|
|
"Return an iterator over abstract " RST_CLASS_REF(lief.Relocation) "",
|
|
|
|
py::return_value_policy::reference_internal)
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
.def_property_readonly("exported_functions",
|
2017-08-26 09:30:59 +02:00
|
|
|
[] (const Binary& binary) {
|
2017-09-07 14:45:41 +02:00
|
|
|
const std::vector<std::string>& exported_functions = binary.exported_functions();
|
2017-08-26 09:30:59 +02:00
|
|
|
std::vector<py::object> exported_functions_encoded;
|
|
|
|
exported_functions_encoded.reserve(exported_functions.size());
|
|
|
|
|
|
|
|
std::transform(
|
|
|
|
std::begin(exported_functions),
|
|
|
|
std::end(exported_functions),
|
|
|
|
std::back_inserter(exported_functions_encoded),
|
|
|
|
&safe_string_converter);
|
|
|
|
return exported_functions_encoded;
|
|
|
|
|
|
|
|
},
|
2017-03-30 16:56:49 +02:00
|
|
|
"Return binary's exported functions (name)")
|
|
|
|
|
|
|
|
.def_property_readonly("imported_functions",
|
2017-08-26 09:30:59 +02:00
|
|
|
[] (const Binary& binary) {
|
2017-09-07 14:45:41 +02:00
|
|
|
const std::vector<std::string>& imported_functions = binary.imported_functions();
|
2017-08-26 09:30:59 +02:00
|
|
|
std::vector<py::object> imported_functions_encoded;
|
|
|
|
imported_functions_encoded.reserve(imported_functions.size());
|
|
|
|
|
|
|
|
std::transform(
|
|
|
|
std::begin(imported_functions),
|
|
|
|
std::end(imported_functions),
|
|
|
|
std::back_inserter(imported_functions_encoded),
|
|
|
|
&safe_string_converter);
|
|
|
|
return imported_functions_encoded;
|
|
|
|
},
|
2017-03-30 16:56:49 +02:00
|
|
|
"Return binary's imported functions (name)")
|
|
|
|
|
|
|
|
.def_property_readonly("libraries",
|
2017-08-26 09:30:59 +02:00
|
|
|
[] (const Binary& binary) {
|
2017-09-07 14:45:41 +02:00
|
|
|
const std::vector<std::string>& imported_libraries = binary.imported_libraries();
|
2017-08-26 09:30:59 +02:00
|
|
|
std::vector<py::object> imported_libraries_encoded;
|
|
|
|
imported_libraries_encoded.reserve(imported_libraries.size());
|
|
|
|
|
|
|
|
std::transform(
|
|
|
|
std::begin(imported_libraries),
|
|
|
|
std::end(imported_libraries),
|
|
|
|
std::back_inserter(imported_libraries_encoded),
|
|
|
|
&safe_string_converter);
|
|
|
|
return imported_libraries_encoded;
|
|
|
|
},
|
2017-03-30 16:56:49 +02:00
|
|
|
"Return binary's imported libraries (name)")
|
|
|
|
|
|
|
|
.def_property_readonly("symbols",
|
2017-09-07 14:45:41 +02:00
|
|
|
static_cast<it_t<it_symbols>>(&Binary::symbols),
|
2017-03-30 16:56:49 +02:00
|
|
|
"Return a list in **read only** of binary's abstract " RST_CLASS_REF(lief.Symbol) "",
|
|
|
|
py::return_value_policy::reference_internal)
|
|
|
|
|
2017-08-22 09:47:29 +02:00
|
|
|
.def("has_symbol",
|
|
|
|
&Binary::has_symbol,
|
|
|
|
"Check if a " RST_CLASS_REF(lief.Symbol) " with the given name exists",
|
|
|
|
"symbol_name"_a)
|
|
|
|
|
|
|
|
.def("get_symbol",
|
|
|
|
static_cast<no_const_func<Symbol&, const std::string&>>(&Binary::get_symbol),
|
|
|
|
"Return the " RST_CLASS_REF(lief.Symbol) " with the given ``name``",
|
|
|
|
"symbol_name"_a,
|
|
|
|
py::return_value_policy::reference)
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
.def("get_function_address",
|
|
|
|
&Binary::get_function_address,
|
2017-07-01 18:35:10 +02:00
|
|
|
"Return the address of the given function name",
|
|
|
|
"function_name"_a)
|
2017-03-30 16:56:49 +02:00
|
|
|
|
|
|
|
.def("patch_address",
|
|
|
|
static_cast<void (Binary::*) (uint64_t, const std::vector<uint8_t>&)>(&Binary::patch_address),
|
|
|
|
"Patch the address with the given value",
|
|
|
|
py::arg("address"), py::arg("patch_value"))
|
|
|
|
|
|
|
|
.def("patch_address",
|
|
|
|
static_cast<void (Binary::*) (uint64_t, uint64_t, size_t)>(&Binary::patch_address),
|
|
|
|
"Patch the address with the given value",
|
|
|
|
py::arg("address"), py::arg("patch_value"), py::arg_v("size", 8))
|
|
|
|
|
|
|
|
|
|
|
|
.def("get_content_from_virtual_address",
|
|
|
|
&Binary::get_content_from_virtual_address,
|
2017-07-01 18:35:10 +02:00
|
|
|
"Return the content located at virtual address",
|
|
|
|
"virtual_address"_a, "size"_a)
|
2017-03-30 16:56:49 +02:00
|
|
|
|
2017-08-30 09:51:03 +02:00
|
|
|
.def_property_readonly("abstract",
|
2017-10-29 15:35:22 +01:00
|
|
|
[m] (py::object& self) {
|
|
|
|
self.attr("__class__") = m.attr("Binary");
|
|
|
|
return self;
|
2017-08-30 09:51:03 +02:00
|
|
|
},
|
2017-10-29 15:35:22 +01:00
|
|
|
"Return the " RST_CLASS_REF(lief.Binary) " object\n\n"
|
|
|
|
".. warning::\n\n"
|
|
|
|
"\tGetting this property modifies the ``__class__`` attribute so that "
|
|
|
|
"the current binary looks like a " RST_CLASS_REF(lief.Binary) ".\n\n"
|
|
|
|
"\tUse the " RST_ATTR_REF(lief.Binary.concrete) " to get back to the original binary.",
|
|
|
|
py::return_value_policy::reference)
|
|
|
|
|
|
|
|
|
|
|
|
.def_property_readonly("concrete",
|
|
|
|
[m] (py::object& self) {
|
|
|
|
self.attr("__class__") = py::cast(self.cast<Binary*>()).attr("__class__");
|
|
|
|
return self;
|
|
|
|
},
|
|
|
|
"Return either " RST_CLASS_REF_FULL(lief.ELF.Binary) ", " RST_CLASS_REF_FULL(lief.PE.Binary) ", " RST_CLASS_REF_FULL(lief.MachO.Binary) " object\n\n"
|
|
|
|
"",
|
2017-08-30 09:51:03 +02:00
|
|
|
py::return_value_policy::reference)
|
2017-03-30 16:56:49 +02:00
|
|
|
|
Improve the ELF part of LIEF
Major changes (features):
* Enable adding multiple sections/segments - Executable (PIE or not), Library
* Enable adding multiple dynamic entries (DT_NEEDED, DT_INIT etc)
* Enable adding multiple relocations
* Enable adding multiple dynamic symbols
* Enable segment replacement
Major changes (API):
* Getters Binary::get_*name*() has been renamed to "name()"
* Binary::add(const DynamicEntry& entry) - To add an entry in the dynamic table
* Section& Binary::add(const Section& section, bool loaded = true) - To add a section(s)
* Segment& Binary::add(const Segment& segment, uint64_t base = 0) - To add segments
* Segment& replace(const Segment& new_segment, const Segment& original_segment, uint64_t base = 0)
* Binary's last_offset_section(), last_offset_segment(), next_virtual_address()
to have information about offset
* Binary's add_library(), get_library(), has_library() to handle
DT_NEEDED entries
Other changes:
* Binary::insert_content() - Use add(const Section&) or add(const Segment&) instead
* ELF's DataHandler has been cleaned
* Through LIEF::Section one can look for integers, strings, data
within the section (see LIEF::Section::search,
LIEF::Section::search_all)
* Through LIEF::Binary one can get *xref* of a number (or address)
see LIEF::Binary::xref function
* To access to the Abstract binary in Python, one can now use
the 'abstract' attribute. (e.g. binary.abstract.header.is_32)
Resolve: #83
Resolve: #66
Resolve: #48
2017-09-02 08:54:54 +02:00
|
|
|
.def("xref",
|
|
|
|
&Binary::xref,
|
|
|
|
"Return all **virtual address** that *use* the ``address`` given in parameter"
|
|
|
|
"virtual_address"_a)
|
|
|
|
|
2017-03-30 16:56:49 +02:00
|
|
|
.def("__str__",
|
|
|
|
[] (const Binary& binary)
|
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << binary;
|
|
|
|
std::string str = stream.str();
|
|
|
|
return str;
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|