-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from raffclar/general-fixes
General fixes
- Loading branch information
Showing
6 changed files
with
81 additions
and
103 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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
file(GLOB SOURCE_FILES *.hpp *.cpp) | ||
add_executable(pe_ep_intercept ${SOURCE_FILES}) | ||
set_property(TARGET pe_ep_intercept PROPERTY CXX_STANDARD 14) | ||
set_property(TARGET pe_ep_intercept PROPERTY CXX_STANDARD_REQUIRED OFF) | ||
set_property(TARGET pe_ep_intercept PROPERTY CXX_STANDARD_REQUIRED ON) | ||
target_link_libraries(pe_ep_intercept keystone) |
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 |
---|---|---|
@@ -1,5 +1,67 @@ | ||
#include <keystone/include/keystone/keystone.h> | ||
#include "PePatch.hpp" | ||
|
||
PeEpIntercept::PePatch::PePatch(std::string &path) : PeFile(path) { | ||
namespace PeEpIntercept { | ||
PePatch::PePatch(std::string &path) : PeFile(path) { | ||
|
||
} | ||
|
||
std::vector<char> PePatch::Assemble(const std::string &assembly) { | ||
std::vector<char> instructions; | ||
|
||
if (assembly.empty()) { | ||
return instructions; | ||
} | ||
|
||
unsigned char *encode = nullptr; | ||
ks_engine *ks = nullptr; | ||
size_t count; | ||
size_t size; | ||
|
||
auto code_deleter = [](unsigned char *code_ptr) { | ||
ks_free(code_ptr); | ||
}; | ||
|
||
auto ks_deleter = [](ks_engine *ks_ptr) { | ||
ks_close(ks_ptr); | ||
}; | ||
|
||
ks_mode instruct_mode; | ||
|
||
switch (type) { | ||
case PeArch::x86: | ||
instruct_mode = KS_MODE_32; | ||
break; | ||
case PeArch::x64: | ||
instruct_mode = KS_MODE_64; | ||
break; | ||
default: | ||
throw std::runtime_error("executable type not supported"); | ||
} | ||
|
||
if (ks_open(KS_ARCH_X86, instruct_mode, &ks) != KS_ERR_OK) { | ||
throw std::runtime_error("failed to open keystone"); | ||
} | ||
|
||
std::unique_ptr<ks_engine[], | ||
decltype(ks_deleter)> ks_ptr(ks, ks_deleter); | ||
|
||
if (ks_asm(ks, assembly.c_str(), 0, &encode, &size, &count) != KS_ERR_OK) { | ||
throw std::runtime_error("failed to assemble instructions"); | ||
} | ||
|
||
std::unique_ptr<unsigned char[], | ||
decltype(code_deleter)> encode_ptr(encode, code_deleter); | ||
|
||
if (size > 0xffffffff) { | ||
throw std::runtime_error("exceeded max section size"); | ||
} | ||
|
||
for (size_t i = 0; i < size; i++) { | ||
auto encoded = static_cast<char>(encode[i]); | ||
instructions.push_back(encoded); | ||
} | ||
|
||
return instructions; | ||
} | ||
} |
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