added page protections to theo::malloc_t....

2.0
_xeroxz 4 years ago
parent dd0ec1946b
commit 4ea2ba046b

@ -85,7 +85,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\..\misc\natvis\imgui.natvis" /> <None Include="..\..\misc\natvis\imgui.natvis" />
<None Include="..\README.txt" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

@ -55,7 +55,6 @@
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\README.txt" />
<None Include="..\..\misc\natvis\imgui.natvis"> <None Include="..\..\misc\natvis\imgui.natvis">
<Filter>sources</Filter> <Filter>sources</Filter>
</None> </None>

@ -33,7 +33,7 @@ namespace theo
{ {
case theo::theo_packet_type::alloc_memory: case theo::theo_packet_type::alloc_memory:
{ {
packet.alloc.addr = alloc(packet.alloc.alloc_size); packet.alloc.addr = alloc(packet.alloc.alloc_size, packet.alloc.prot);
break; break;
} }
case theo::theo_packet_type::copy_memory: case theo::theo_packet_type::copy_memory:

@ -45,6 +45,7 @@ namespace theo
{ {
void* addr; void* addr;
std::size_t alloc_size; std::size_t alloc_size;
std::uint32_t prot;
} alloc; } alloc;
struct struct
@ -64,7 +65,7 @@ namespace theo
} theo_data, * ptheo_data; } theo_data, * ptheo_data;
#pragma pack(pop) #pragma pack(pop)
using malloc_t = std::function<decltype(malloc)>; using malloc_t = std::function<void*(std::size_t, std::uint32_t)>;
using memcpy_t = std::function<decltype(memcpy)>; using memcpy_t = std::function<decltype(memcpy)>;
using resolve_symbol_t = std::function<std::uintptr_t(const char*)>; using resolve_symbol_t = std::function<std::uintptr_t(const char*)>;

@ -155,16 +155,18 @@ int __cdecl main(int argc, char** argv)
return -1; return -1;
} }
theo::malloc_t _alloc = [&](std::size_t size) -> void* theo::malloc_t _alloc =
[&](std::size_t size, std::uint32_t prot) -> void*
{ {
const auto result = VirtualAllocEx const auto result =
( VirtualAllocEx
phandle, (
nullptr, phandle,
size, nullptr,
MEM_COMMIT | MEM_RESERVE, size,
PAGE_EXECUTE_READWRITE MEM_COMMIT | MEM_RESERVE,
); prot
);
if (!result) if (!result)
{ {
@ -361,7 +363,9 @@ int __cdecl main(int argc, char** argv)
}; };
vdm::msrexec_ctx msrexec(_write_msr); vdm::msrexec_ctx msrexec(_write_msr);
theo::malloc_t _kalloc = [&](std::size_t size) -> void*
theo::malloc_t _kalloc =
[&](std::size_t size, std::uint32_t prot) -> void*
{ {
void* alloc_base; void* alloc_base;
msrexec.exec msrexec.exec
@ -453,7 +457,9 @@ int __cdecl main(int argc, char** argv)
// use VDM to syscall into ExAllocatePool... // use VDM to syscall into ExAllocatePool...
vdm::vdm_ctx vdm(_read_phys, _write_phys); vdm::vdm_ctx vdm(_read_phys, _write_phys);
theo::malloc_t _kalloc = [&](std::size_t size) -> void*
theo::malloc_t _kalloc =
[&](std::size_t size, std::uint32_t prot) -> void*
{ {
using ex_alloc_pool_t = using ex_alloc_pool_t =
void* (*)(std::uint32_t, std::uint32_t); void* (*)(std::uint32_t, std::uint32_t);

@ -86,7 +86,9 @@ int main(int argc, char** argv)
}; };
vdm::msrexec_ctx msrexec(_write_msr); vdm::msrexec_ctx msrexec(_write_msr);
theo::malloc_t _kalloc = [&](std::size_t size) -> void*
theo::malloc_t _kalloc =
[&](std::size_t size, std::uint32_t prot) -> void*
{ {
void* alloc_base; void* alloc_base;
msrexec.exec msrexec.exec

@ -2,6 +2,7 @@
#include <Windows.h> #include <Windows.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <functional>
#include <map> #include <map>
namespace obfuscation{ class obfuscate; } namespace obfuscation{ class obfuscate; }
@ -9,7 +10,7 @@ namespace lnk { using obj_buffer_t = std::vector<std::uint8_t>; }
namespace theo namespace theo
{ {
using malloc_t = std::function<decltype(malloc)>; using malloc_t = std::function<void*(std::size_t bytes, std::uint32_t prot)>;
using memcpy_t = std::function<decltype(memcpy)>; using memcpy_t = std::function<decltype(memcpy)>;
using resolve_symbol_t = std::function<std::uintptr_t(const char*)>; using resolve_symbol_t = std::function<std::uintptr_t(const char*)>;
@ -22,8 +23,8 @@ namespace theo
auto map_objs(std::vector<lnk::obj_buffer_t>& objs) -> bool; auto map_objs(std::vector<lnk::obj_buffer_t>& objs) -> bool;
auto get_symbol(std::string symbol_name) -> std::uintptr_t; auto get_symbol(std::string symbol_name) -> std::uintptr_t;
malloc_t kalloc; malloc_t alloc;
memcpy_t kmemcpy; memcpy_t mcopy;
resolve_symbol_t resolve_symbol; resolve_symbol_t resolve_symbol;
private: private:
bool map_symbols(std::vector<lnk::obj_buffer_t>& objs); bool map_symbols(std::vector<lnk::obj_buffer_t>& objs);

@ -95,7 +95,8 @@ int main(int argc, char** argv)
// use VDM to syscall into ExAllocatePool... // use VDM to syscall into ExAllocatePool...
vdm::vdm_ctx vdm(_read_phys, _write_phys); vdm::vdm_ctx vdm(_read_phys, _write_phys);
theo::malloc_t _kalloc = [&](std::size_t size) -> void* theo::malloc_t _kalloc =
[&](std::size_t size, std::uint32_t prot) -> void*
{ {
using ex_alloc_pool_t = using ex_alloc_pool_t =
void* (*)(std::uint32_t, std::uint32_t); void* (*)(std::uint32_t, std::uint32_t);

@ -10,7 +10,7 @@ namespace lnk { using obj_buffer_t = std::vector<std::uint8_t>; }
namespace theo namespace theo
{ {
using malloc_t = std::function<decltype(malloc)>; using malloc_t = std::function<void*(std::size_t bytes, std::uint32_t prot)>;
using memcpy_t = std::function<decltype(memcpy)>; using memcpy_t = std::function<decltype(memcpy)>;
using resolve_symbol_t = std::function<std::uintptr_t(const char*)>; using resolve_symbol_t = std::function<std::uintptr_t(const char*)>;

@ -63,13 +63,14 @@ namespace theo
return dest; return dest;
} }
void* client::wrapper_alloc(std::size_t size) const void* client::wrapper_alloc(std::size_t size, std::uint32_t prot) const
{ {
theo_data* packet = new theo_data; theo_data* packet = new theo_data;
memset(packet, NULL, sizeof theo_data); memset(packet, NULL, sizeof theo_data);
packet->type = theo_packet_type::alloc_memory; packet->type = theo_packet_type::alloc_memory;
packet->alloc.alloc_size = size; packet->alloc.alloc_size = size;
packet->alloc.prot = prot;
if (send(client_socket, reinterpret_cast<char*>(packet), if (send(client_socket, reinterpret_cast<char*>(packet),
sizeof theo_data, NULL) == SOCKET_ERROR) sizeof theo_data, NULL) == SOCKET_ERROR)
@ -147,8 +148,8 @@ namespace theo
{ {
case theo_packet_type::init: case theo_packet_type::init:
{ {
theo::malloc_t alloc = [&](std::size_t size) -> void* theo::malloc_t alloc = [&](std::size_t size, std::uint32_t prot) -> void*
{ return this->wrapper_alloc(size); }; { return this->wrapper_alloc(size, prot); };
theo::memcpy_t mcopy = theo::memcpy_t mcopy =
[&](void* dest, const void* src, std::size_t size) -> void* [&](void* dest, const void* src, std::size_t size) -> void*

@ -44,6 +44,7 @@ namespace theo
{ {
void* addr; void* addr;
std::size_t alloc_size; std::size_t alloc_size;
std::uint32_t prot;
} alloc; } alloc;
struct struct
@ -72,7 +73,7 @@ namespace theo
private: private:
void handler() const; void handler() const;
void* wrapper_memcpy(void* dest, const void* src, std::size_t size) const; void* wrapper_memcpy(void* dest, const void* src, std::size_t size) const;
void* wrapper_alloc(std::size_t size) const; void* wrapper_alloc(std::size_t size, std::uint32_t prot) const;
std::uintptr_t wrapper_resolve_symbol(const char* symbol_name) const; std::uintptr_t wrapper_resolve_symbol(const char* symbol_name) const;
const SOCKET client_socket; const SOCKET client_socket;

@ -95,6 +95,7 @@ int __cdecl main(int argc, char** argv)
sockaddr socket_info; sockaddr socket_info;
int socket_info_len = sizeof socket_info; int socket_info_len = sizeof socket_info;
const auto psocket_info = const auto psocket_info =
reinterpret_cast<sockaddr_in*>(&socket_info); reinterpret_cast<sockaddr_in*>(&socket_info);

@ -10,7 +10,7 @@ namespace lnk { using obj_buffer_t = std::vector<std::uint8_t>; }
namespace theo namespace theo
{ {
using malloc_t = std::function<decltype(malloc)>; using malloc_t = std::function<void*(std::size_t bytes, std::uint32_t prot)>;
using memcpy_t = std::function<decltype(memcpy)>; using memcpy_t = std::function<decltype(memcpy)>;
using resolve_symbol_t = std::function<std::uintptr_t(const char*)>; using resolve_symbol_t = std::function<std::uintptr_t(const char*)>;
@ -23,8 +23,8 @@ namespace theo
auto map_objs(std::vector<lnk::obj_buffer_t>& objs) -> bool; auto map_objs(std::vector<lnk::obj_buffer_t>& objs) -> bool;
auto get_symbol(std::string symbol_name) -> std::uintptr_t; auto get_symbol(std::string symbol_name) -> std::uintptr_t;
malloc_t kalloc; malloc_t alloc;
memcpy_t kmemcpy; memcpy_t mcopy;
resolve_symbol_t resolve_symbol; resolve_symbol_t resolve_symbol;
private: private:
bool map_symbols(std::vector<lnk::obj_buffer_t>& objs); bool map_symbols(std::vector<lnk::obj_buffer_t>& objs);

@ -92,16 +92,17 @@ int main(int argc, char** argv)
return -1; return -1;
} }
theo::malloc_t _alloc = [&](std::size_t size) -> void* theo::malloc_t _alloc = [&](std::size_t size, std::uint32_t prot) -> void*
{ {
const auto result = VirtualAllocEx const auto result =
( VirtualAllocEx
phandle, (
nullptr, phandle,
size, nullptr,
MEM_COMMIT | MEM_RESERVE, size,
PAGE_EXECUTE_READWRITE MEM_COMMIT | MEM_RESERVE,
); prot
);
if (!result) if (!result)
{ {

@ -5,12 +5,12 @@
#include <functional> #include <functional>
#include <map> #include <map>
namespace obfuscation { class obfuscate; } namespace obfuscation{ class obfuscate; }
namespace lnk { using obj_buffer_t = std::vector<std::uint8_t>; } namespace lnk { using obj_buffer_t = std::vector<std::uint8_t>; }
namespace theo namespace theo
{ {
using malloc_t = std::function<decltype(malloc)>; using malloc_t = std::function<void*(std::size_t bytes, std::uint32_t prot)>;
using memcpy_t = std::function<decltype(memcpy)>; using memcpy_t = std::function<decltype(memcpy)>;
using resolve_symbol_t = std::function<std::uintptr_t(const char*)>; using resolve_symbol_t = std::function<std::uintptr_t(const char*)>;
@ -21,10 +21,10 @@ namespace theo
public: public:
explicit hmm_ctx(const mapper_routines_t& routines); explicit hmm_ctx(const mapper_routines_t& routines);
auto map_objs(std::vector<lnk::obj_buffer_t>& objs) -> bool; auto map_objs(std::vector<lnk::obj_buffer_t>& objs) -> bool;
auto get_symbol(std::string symbol_name)->std::uintptr_t; auto get_symbol(std::string symbol_name) -> std::uintptr_t;
malloc_t kalloc; malloc_t alloc;
memcpy_t kmemcpy; memcpy_t mcopy;
resolve_symbol_t resolve_symbol; resolve_symbol_t resolve_symbol;
private: private:
bool map_symbols(std::vector<lnk::obj_buffer_t>& objs); bool map_symbols(std::vector<lnk::obj_buffer_t>& objs);

Binary file not shown.

Binary file not shown.

@ -10,7 +10,7 @@ namespace lnk { using obj_buffer_t = std::vector<std::uint8_t>; }
namespace theo namespace theo
{ {
using malloc_t = std::function<decltype(malloc)>; using malloc_t = std::function<void*(std::size_t bytes, std::uint32_t prot)>;
using memcpy_t = std::function<decltype(memcpy)>; using memcpy_t = std::function<decltype(memcpy)>;
using resolve_symbol_t = std::function<std::uintptr_t(const char*)>; using resolve_symbol_t = std::function<std::uintptr_t(const char*)>;

Before

Width:  |  Height:  |  Size: 240 KiB

After

Width:  |  Height:  |  Size: 240 KiB

@ -213,8 +213,9 @@ namespace theo
// these symbols are generated by llvm-obfuscator as "x" and "y"... // these symbols are generated by llvm-obfuscator as "x" and "y"...
else if (reloc.raw_symbol.StorageClass == IMAGE_SYM_CLASS_EXTERNAL && reloc.raw_symbol.Value) else if (reloc.raw_symbol.StorageClass == IMAGE_SYM_CLASS_EXTERNAL && reloc.raw_symbol.Value)
{ {
const auto zero_me = alloc(reloc.raw_symbol.Value); const auto zero_me = alloc(reloc.raw_symbol.Value, PAGE_READWRITE);
const auto zero_size = malloc(reloc.raw_symbol.Value); const auto zero_size = malloc(reloc.raw_symbol.Value);
memset(zero_size, NULL, reloc.raw_symbol.Value); memset(zero_size, NULL, reloc.raw_symbol.Value);
mcopy(zero_me, zero_size, reloc.raw_symbol.Value); mcopy(zero_me, zero_size, reloc.raw_symbol.Value);
free(zero_size); free(zero_size);
@ -383,7 +384,7 @@ namespace theo
// these symbols are generated by llvm-obfuscator as "x" and "y"... // these symbols are generated by llvm-obfuscator as "x" and "y"...
else if (reloc.raw_symbol.StorageClass == IMAGE_SYM_CLASS_EXTERNAL && reloc.raw_symbol.Value) else if (reloc.raw_symbol.StorageClass == IMAGE_SYM_CLASS_EXTERNAL && reloc.raw_symbol.Value)
{ {
const auto zero_me = alloc(reloc.raw_symbol.Value); const auto zero_me = alloc(reloc.raw_symbol.Value, PAGE_READWRITE);
const auto zero_size = malloc(reloc.raw_symbol.Value); const auto zero_size = malloc(reloc.raw_symbol.Value);
memset(zero_size, NULL, reloc.raw_symbol.Value); memset(zero_size, NULL, reloc.raw_symbol.Value);
mcopy(zero_me, zero_size, reloc.raw_symbol.Value); mcopy(zero_me, zero_size, reloc.raw_symbol.Value);
@ -527,7 +528,7 @@ namespace theo
mapped_symbols[new_symbol] = mapped_symbols[new_symbol] =
reinterpret_cast<std::uintptr_t>( reinterpret_cast<std::uintptr_t>(
alloc(new_gadget->get_size())); alloc(new_gadget->get_size(), PAGE_EXECUTE_READWRITE));
obfuscated_gadgets[mapped_symbols[new_symbol]] = new_gadget; obfuscated_gadgets[mapped_symbols[new_symbol]] = new_gadget;
DBG_PRINT("\t\t> %s allocated = 0x%p, size = %d\n", new_symbol.c_str(), DBG_PRINT("\t\t> %s allocated = 0x%p, size = %d\n", new_symbol.c_str(),
@ -565,7 +566,7 @@ namespace theo
if (symbol.type == IMAGE_SYM_FUNCTION) if (symbol.type == IMAGE_SYM_FUNCTION)
{ {
mapped_symbols[symbol.symbol_name] = mapped_symbols[symbol.symbol_name] =
reinterpret_cast<std::uintptr_t>(alloc(symbol.size)); reinterpret_cast<std::uintptr_t>(alloc(symbol.size, PAGE_EXECUTE_READWRITE));
DBG_PRINT("\t> %s allocated at = 0x%p, size = %d\n", DBG_PRINT("\t> %s allocated at = 0x%p, size = %d\n",
symbol.symbol_name.c_str(), mapped_symbols[symbol.symbol_name], symbol.size); symbol.symbol_name.c_str(), mapped_symbols[symbol.symbol_name], symbol.size);
@ -584,7 +585,7 @@ namespace theo
{ {
mapped_symbols[data_section_sym] = mapped_symbols[data_section_sym] =
reinterpret_cast<std::uintptr_t>(alloc( reinterpret_cast<std::uintptr_t>(alloc(
section_headers[symbol.section_number - 1].SizeOfRawData)); section_headers[symbol.section_number - 1].SizeOfRawData, PAGE_READWRITE));
DBG_PRINT("\t> section %s allocated at = 0x%p, size = %d\n", DBG_PRINT("\t> section %s allocated at = 0x%p, size = %d\n",
data_section_sym.c_str(), data_section_sym.c_str(),
@ -638,7 +639,7 @@ namespace theo
{ {
mapped_symbols[data_section_sym] = mapped_symbols[data_section_sym] =
reinterpret_cast<std::uintptr_t>(alloc( reinterpret_cast<std::uintptr_t>(alloc(
section_headers[idx].SizeOfRawData)); section_headers[idx].SizeOfRawData, PAGE_READWRITE));
DBG_PRINT("\t> section %s allocated at = 0x%p, size = %d\n", DBG_PRINT("\t> section %s allocated at = 0x%p, size = %d\n",
data_section_sym.c_str(), data_section_sym.c_str(),

@ -16,7 +16,7 @@
#pragma comment(lib, "Dbghelp.lib") #pragma comment(lib, "Dbghelp.lib")
namespace theo namespace theo
{ {
using malloc_t = std::function<decltype(malloc)>; using malloc_t = std::function<void*(std::size_t bytes, std::uint32_t prot)>;
using memcpy_t = std::function<decltype(memcpy)>; using memcpy_t = std::function<decltype(memcpy)>;
using resolve_symbol_t = std::function<std::uintptr_t(const char*)>; using resolve_symbol_t = std::function<std::uintptr_t(const char*)>;

Loading…
Cancel
Save