File: pyBinary.cpp

package info (click to toggle)
lief 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 16,036 kB
  • sloc: cpp: 76,013; python: 6,167; ansic: 3,355; pascal: 404; sh: 98; makefile: 32
file content (215 lines) | stat: -rw-r--r-- 8,585 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* 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;

#define PY_ENUM(x) LIEF::to_string(x), x

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);

template<class T, class P>
using no_const_func = T (Binary::*)(P);

void init_LIEF_Binary_class(py::module& m) {


  py::class_<Binary, Object> pybinary(m, "Binary");

  py::enum_<LIEF::Binary::VA_TYPES>(pybinary, "VA_TYPES")
    .value(PY_ENUM(LIEF::Binary::VA_TYPES::AUTO))
    .value(PY_ENUM(LIEF::Binary::VA_TYPES::VA))
    .value(PY_ENUM(LIEF::Binary::VA_TYPES::RVA));

    pybinary
    .def_property_readonly("format",
        &Binary::format,
        "File format " RST_CLASS_REF(lief.EXE_FORMATS) " of the underlying binary.")

    .def_property_readonly("is_pie",
        &Binary::is_pie,
        "Check if the binary is position independent")

    .def_property_readonly("has_nx",
        &Binary::has_nx,
        "Check if the binary uses ``NX`` protection")

    .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",
        &Binary::header,
        "Binary's header")

    .def_property_readonly("entrypoint",
        &Binary::entrypoint,
        "Binary's entrypoint")

    .def_property_readonly("sections",
        static_cast<it_t<it_sections>>(&Binary::sections),
        "Return a list in **read only** of binary's abstract " RST_CLASS_REF(lief.Section) "",
        py::return_value_policy::reference_internal)

    .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)

    .def_property_readonly("exported_functions",
        [] (const Binary& binary) {
          const std::vector<std::string>& exported_functions = binary.exported_functions();
          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;

        },
        "Return binary's exported functions (name)")

    .def_property_readonly("imported_functions",
        [] (const Binary& binary) {
          const std::vector<std::string>& imported_functions = binary.imported_functions();
          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;
        },
        "Return binary's imported functions (name)")

    .def_property_readonly("libraries",
        [] (const Binary& binary) {
          const std::vector<std::string>& imported_libraries = binary.imported_libraries();
          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;
        },
        "Return binary's imported libraries (name)")

    .def_property_readonly("symbols",
        static_cast<it_t<it_symbols>>(&Binary::symbols),
        "Return a list in **read only** of binary's abstract " RST_CLASS_REF(lief.Symbol) "",
        py::return_value_policy::reference_internal)

    .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)

    .def("get_function_address",
        &Binary::get_function_address,
        "Return the address of the given function name",
        "function_name"_a)

    .def("patch_address",
        static_cast<void (Binary::*) (uint64_t, const std::vector<uint8_t>&, LIEF::Binary::VA_TYPES)>(&Binary::patch_address),
        "Patch the address with the given value",
        "Virtual address is specified in the first argument and the content in the second (as a list of bytes).\n"
        "If the underlying binary is a PE, one can specify if the virtual address is a " RST_ATTR_REF(lief.Binary.VA_TYPES.RVA) ""
        " or a " RST_ATTR_REF(lief.Binary.VA_TYPES.VA) ". By default it is set to " RST_ATTR_REF(lief.Binary.VA_TYPES.AUTO) "",
        "address"_a, "patch_value"_a, "va_type"_a = LIEF::Binary::VA_TYPES::AUTO)

    .def("patch_address",
        static_cast<void (Binary::*) (uint64_t, uint64_t, size_t, LIEF::Binary::VA_TYPES)>(&Binary::patch_address),
        "Patch the address with the given value",
        "Virtual address is specified in the first argument, integer in the second and sizeof the integer in third one.\n"
        "If the underlying binary is a PE, one can specify if the virtual address is a " RST_ATTR_REF(lief.Binary.VA_TYPES.RVA) ""
        " or a " RST_ATTR_REF(lief.Binary.VA_TYPES.VA) ". By default it is set to " RST_ATTR_REF(lief.Binary.VA_TYPES.AUTO) "",
        "address"_a, "patch_value"_a, "size"_a = 8, "va_type"_a = LIEF::Binary::VA_TYPES::AUTO)


   .def("get_content_from_virtual_address",
        &Binary::get_content_from_virtual_address,
        "Return the content located at virtual address.\n\n"
        "Virtual address is specified in the first argument and size to read (in bytes) in the second.\n"
        "If the underlying binary is a PE, one can specify if the virtual address is a " RST_ATTR_REF(lief.Binary.VA_TYPES.RVA) ""
        " or a " RST_ATTR_REF(lief.Binary.VA_TYPES.VA) ". By default it is set to " RST_ATTR_REF(lief.Binary.VA_TYPES.AUTO) "",
        "virtual_address"_a, "size"_a, "va_type"_a = LIEF::Binary::VA_TYPES::AUTO)

    .def_property_readonly("abstract",
        [m] (py::object& self) {
          self.attr("__class__") = m.attr("Binary");
          return self;
        },
        "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"
        "",
        py::return_value_policy::reference)

    .def("xref",
        &Binary::xref,
        "Return all **virtual address** that *use* the ``address`` given in parameter"
       "virtual_address"_a)

    .def("__str__",
        [] (const Binary& binary)
        {
          std::ostringstream stream;
          stream << binary;
          std::string str = stream.str();
          return str;
        });

}