Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Enhance PE resources
* Improve Resource Manager
* Fix bug when rebuilding resources (aligment issue)
* Provide a Python API to access to the resource tree
* Pretty print lang/sublang of resources
* Parse resource dialog
* Parse resource icons
* Parse resource version
* Add tests on the resource builder
* Add tutorial (related to #28)
  • Loading branch information
romainthomas committed Jun 11, 2017
1 parent a9dcfb5 commit 8473c8e
Show file tree
Hide file tree
Showing 135 changed files with 9,757 additions and 38,240 deletions.
13 changes: 13 additions & 0 deletions api/python/PE/CMakeLists.txt
@@ -1,4 +1,17 @@
set(LIEF_PYTHON_PE_SRC
"${CMAKE_CURRENT_LIST_DIR}/objects/pyResourceNode.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyResourceData.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/pyResourceDirectory.cpp"

"${CMAKE_CURRENT_LIST_DIR}/objects/resources/pyResourceIcon.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/resources/pyResourceVersion.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/resources/pyResourceVarFileInfo.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/resources/pyResourceFixedFileInfo.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/resources/pyResourceStringFileInfo.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/resources/pyLangCodeItem.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/resources/pyResourceDialog.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/resources/pyResourceDialogItem.cpp"

"${CMAKE_CURRENT_LIST_DIR}/objects/signature/pySignerInfo.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/signature/pyAuthenticatedAttributes.cpp"
"${CMAKE_CURRENT_LIST_DIR}/objects/signature/pyx509.cpp"
Expand Down
5 changes: 5 additions & 0 deletions api/python/PE/objects/pyBinary.cpp
Expand Up @@ -205,6 +205,11 @@ void init_PE_Binary_class(py::module& m) {
static_cast<no_const_getter<ResourcesManager>>(&Binary::get_resources_manager),
"Return the " RST_CLASS_REF(lief.PE.ResourcesManager) " to manage resources")

.def_property_readonly("resources",
static_cast<no_const_getter<ResourceNode&>>(&Binary::get_resources),
"Return the " RST_CLASS_REF(lief.PE.ResourceNode) " tree",
py::return_value_policy::reference)

.def_property_readonly("overlay",
static_cast<no_const_getter<std::vector<uint8_t>&>>(&Binary::overlay),
"Return the overlay content",
Expand Down
74 changes: 74 additions & 0 deletions api/python/PE/objects/pyResourceData.cpp
@@ -0,0 +1,74 @@
/* 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 "pyPE.hpp"

#include "LIEF/visitors/Hash.hpp"
#include "LIEF/PE/ResourceData.hpp"

#include <string>
#include <sstream>

template<class T>
using getter_t = T (ResourceData::*)(void) const;

template<class T>
using setter_t = void (ResourceData::*)(T);

void init_PE_ResourceData_class(py::module& m) {
py::class_<ResourceData, ResourceNode>(m, "ResourceData")
.def(py::init<>(),
"Default constructor")

.def(py::init<const std::vector<uint8_t>&, uint32_t>(),
"content"_a, "code_page"_a)

.def_property("code_page",
static_cast<getter_t<uint32_t>>(&ResourceData::code_page),
static_cast<setter_t<uint32_t>>(&ResourceData::code_page),
"The code page that is used to decode code point "
"values within the resource data. Typically, the code "
"page would be the Unicode code page")

.def_property("content",
static_cast<getter_t<const std::vector<uint8_t>&>>(&ResourceData::content),
static_cast<setter_t<const std::vector<uint8_t>&>>(&ResourceData::content),
"Resource content")

.def_property("reserved",
static_cast<getter_t<uint32_t>>(&ResourceData::reserved),
static_cast<setter_t<uint32_t>>(&ResourceData::reserved),
"Reserved value. Should be ``0``")

.def_property_readonly("offset",
&ResourceData::offset,
"Offset of the content within the resource")

.def("__eq__", &ResourceData::operator==)
.def("__ne__", &ResourceData::operator!=)

.def("__hash__",
[] (const ResourceData& node) {
return LIEF::Hash::hash(node);
})

.def("__str__",
[] (const ResourceData& data) {
std::ostringstream stream;
stream << data;
std::string str = stream.str();
return str;
});
}
89 changes: 89 additions & 0 deletions api/python/PE/objects/pyResourceDirectory.cpp
@@ -0,0 +1,89 @@
/* 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 "pyPE.hpp"

#include "LIEF/visitors/Hash.hpp"
#include "LIEF/PE/ResourceDirectory.hpp"

#include <string>
#include <sstream>

template<class T>
using getter_t = T (ResourceDirectory::*)(void) const;

template<class T>
using setter_t = void (ResourceDirectory::*)(T);

void init_PE_ResourceDirectory_class(py::module& m) {
py::class_<ResourceDirectory, ResourceNode>(m, "ResourceDirectory")
.def(py::init<>(),
"Default constructor")

.def_property("characteristics",
static_cast<getter_t<uint32_t>>(&ResourceDirectory::characteristics),
static_cast<setter_t<uint32_t>>(&ResourceDirectory::characteristics),
"Resource flags. This field is reserved for future use. "
"It is currently set to zero.")

.def_property("time_date_stamp",
static_cast<getter_t<uint32_t>>(&ResourceDirectory::time_date_stamp),
static_cast<setter_t<uint32_t>>(&ResourceDirectory::time_date_stamp),
"The time that the resource data was created by the "
"resource compiler.")

.def_property("major_version",
static_cast<getter_t<uint16_t>>(&ResourceDirectory::major_version),
static_cast<setter_t<uint16_t>>(&ResourceDirectory::major_version),
"The major version number, set by the user.")

.def_property("minor_version",
static_cast<getter_t<uint16_t>>(&ResourceDirectory::minor_version),
static_cast<setter_t<uint16_t>>(&ResourceDirectory::minor_version),
"The minor version number, set by the user.")

.def_property("numberof_name_entries",
static_cast<getter_t<uint16_t>>(&ResourceDirectory::numberof_name_entries),
static_cast<setter_t<uint16_t>>(&ResourceDirectory::numberof_name_entries),
"The number of directory entries immediately "
"following the table that use strings to identify Type, "
"Name, or Language entries (depending on the level "
"of the table")

.def_property("numberof_id_entries",
static_cast<getter_t<uint16_t>>(&ResourceDirectory::numberof_id_entries),
static_cast<setter_t<uint16_t>>(&ResourceDirectory::numberof_id_entries),
"The number of directory entries immediately "
"following the Name entries that use numeric IDs for "
"Type, Name, or Language entries.")


.def("__eq__", &ResourceDirectory::operator==)
.def("__ne__", &ResourceDirectory::operator!=)

.def("__hash__",
[] (const ResourceDirectory& node) {
return LIEF::Hash::hash(node);
})

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

}
108 changes: 108 additions & 0 deletions api/python/PE/objects/pyResourceNode.cpp
@@ -0,0 +1,108 @@
/* 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 "pyPE.hpp"

#include "LIEF/visitors/Hash.hpp"
#include "LIEF/PE/ResourceNode.hpp"

#include <string>
#include <sstream>

template<class T>
using getter_t = T (ResourceNode::*)(void) const;

template<class T>
using setter_t = void (ResourceNode::*)(T);

void init_PE_ResourceNode_class(py::module& m) {
py::class_<ResourceNode>(m, "ResourceNode")

.def_property("id",
static_cast<getter_t<uint32_t>>(&ResourceNode::id),
static_cast<setter_t<uint32_t>>(&ResourceNode::id),
"Integer that identifies the Type, Name, or "
"Language ID entry.")

.def_property_readonly("is_directory",
&ResourceNode::is_directory,
"``True`` if the current resource is a " RST_CLASS_REF(lief.PE.ResourceDirectory) "")

.def_property_readonly("is_data",
&ResourceNode::is_data,
"``True`` if the current resource is a " RST_CLASS_REF(lief.PE.ResourceData) "")

.def_property_readonly("has_name",
&ResourceNode::has_name,
"``True`` if the current resource uses a name")

.def_property("name",
[] (const ResourceNode& node) {
return u16tou8(node.name());
},
static_cast<void (ResourceNode::*)(const std::string&)>(&ResourceNode::name),
"Resource name")

.def_property_readonly("childs",
static_cast<it_childs (ResourceNode::*)(void)>(&ResourceNode::childs),
"Node's childs")

.def("add_directory_node",
static_cast<ResourceNode& (ResourceNode::*)(const ResourceDirectory&)>(&ResourceNode::add_child),
"Add a " RST_CLASS_REF(lief.PE.ResourceDirectory) " to the current node",
"resource_directory"_a)

.def("add_data_node",
static_cast<ResourceNode& (ResourceNode::*)(const ResourceData&)>(&ResourceNode::add_child),
"Add a " RST_CLASS_REF(lief.PE.ResourceData) " to the current node",
"resource_data"_a)

.def("delete_child",
static_cast<void (ResourceNode::*)(const ResourceNode&)>(&ResourceNode::delete_child),
"Delete the given " RST_CLASS_REF(lief.PE.ResourceNode) " from childs",
"node"_a)

.def("delete_child",
static_cast<void (ResourceNode::*)(uint32_t)>(&ResourceNode::delete_child),
"Delete the " RST_CLASS_REF(lief.PE.ResourceNode) " with the given :attr:`~lief.PE.ResourceNode.id` from childs",
"id"_a)

.def("sort_by_id",
&ResourceNode::sort_by_id,
"Sort resource childs by ID")

.def_property_readonly("depth",
&ResourceNode::depth,
"Current depth of the entry in the resource tree")

.def("__eq__", &ResourceNode::operator==)
.def("__ne__", &ResourceNode::operator!=)

.def("__hash__",
[] (const ResourceNode& node) {
return LIEF::Hash::hash(node);
})

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



}
86 changes: 83 additions & 3 deletions api/python/PE/objects/pyResourcesManager.cpp
Expand Up @@ -22,11 +22,91 @@
#include <sstream>


template<class T>
using getter_t = T (ResourcesManager::*)(void) const;

template<class T>
using setter_t = void (ResourcesManager::*)(T);

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

void init_PE_ResourcesManager_class(py::module& m) {
py::class_<ResourcesManager>(m, "ResourcesManager")
py::class_<ResourcesManager>(m, "ResourcesManager",
"The Resource Manager provides an enhanced API to manipulate the resource tree")

.def_property_readonly("has_manifest",
&ResourcesManager::has_manifest,
"``True`` if resources contain Manifest element")

.def_property("manifest",
static_cast<getter_t<std::string>>(&ResourcesManager::manifest),
static_cast<setter_t<const std::string&>>(&ResourcesManager::manifest),
"Manifest as a ``string``")


.def_property_readonly("has_version",
&ResourcesManager::has_version,
"``true`` if resources contain " RST_CLASS_REF(lief.PE.ResourceVersion) "")

.def_property_readonly("version",
static_cast<getter_t<ResourceVersion>>(&ResourcesManager::version),
"Return the " RST_CLASS_REF(lief.PE.ResourceVersion) "")
//static_cast<setter_t<const ResourceVersion&>>(&ResourcesManager::version))


.def_property_readonly("has_icons",
&ResourcesManager::has_icons,
"``true`` if resources contain " RST_CLASS_REF(lief.PE.ResourceIcon) "")

.def_property_readonly("icons",
&ResourcesManager::icons,
"Return the list of the " RST_CLASS_REF(lief.PE.ResourceIcon) " present in the resource")

.def("change_icon",
&ResourcesManager::change_icon,
"Switch the given icons",
"old_one"_a, "new_one"_a)

.def_property_readonly("has_dialogs",
&ResourcesManager::has_dialogs,
"``true`` if resources contain " RST_CLASS_REF(lief.PE.ResourceDialog) "")

.def_property_readonly("dialogs",
&ResourcesManager::dialogs,
"Return the list of the " RST_CLASS_REF(lief.PE.ResourceDialog) " present in the resource")

.def_property_readonly("types_available",
&ResourcesManager::get_types_available,
"Return list of " RST_CLASS_REF(lief.PE.RESOURCE_TYPES) " present in the resources")

.def_property_readonly("langs_available",
&ResourcesManager::get_langs_available,
"Return list of " RST_CLASS_REF(lief.PE.RESOURCE_LANGS) " present in the resources")

.def_property_readonly("sublangs_available",
&ResourcesManager::get_sublangs_available,
"Return list of " RST_CLASS_REF(lief.PE.RESOURCE_SUBLANGS) " present in the resources")

.def("add_icon",
&ResourcesManager::add_icon,
"Add an icon to the resources",
"icon"_a)

.def("has_type",
&ResourcesManager::has_type,
"``True`` if the resource has the given " RST_CLASS_REF(lief.PE.RESOURCE_TYPES) "",
"type"_a)

.def("get_node_type",
static_cast<no_const_func<ResourceNode&, RESOURCE_TYPES>>(&ResourcesManager::get_node_type),
"Return " RST_CLASS_REF(lief.PE.ResourceNode) " with "
"the given " RST_CLASS_REF(lief.PE.RESOURCE_TYPES) "",
"type"_a,
py::return_value_policy::reference)

.def("__str__", [] (const ResourcesManager& manager)
{
.def("__str__",
[] (const ResourcesManager& manager) {
std::ostringstream stream;
stream << manager;
std::string str = stream.str();
Expand Down

0 comments on commit 8473c8e

Please sign in to comment.