Skip to content

Commit cc8ee50

Browse files
committed
Update README.md
1 parent b588066 commit cc8ee50

File tree

6 files changed

+126
-10
lines changed

6 files changed

+126
-10
lines changed

CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ if(WIN32)
5151

5252
# Listed by dependency order
5353
Common
54-
55-
# Crypto
5654
Network
5755
Symbols
5856
System
@@ -77,8 +75,6 @@ else()
7775

7876
# Listed by dependency order
7977
Common
80-
81-
# Crypto
8278
Network
8379
System
8480

Modules/Common/Include/Error.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,11 @@ enum class ErrorCode : uint32_t
138138
/// @brief Unexpected size comparison
139139
SizeMismatch,
140140

141-
/// @brief MalformedFile
141+
/// @brief Malformed file
142142
MalformedFile,
143+
144+
/// @brief Malformed data
145+
MalformedData,
143146
};
144147

145148

Modules/Process/Include/Win32/Process.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,16 @@ class Process
413413
ExecuteCallbacks();
414414

415415

416-
// TODO:
417-
// - modules
416+
///
417+
/// @brief Enumerate the process modules
418+
///
419+
/// @return Result<std::vector<LDR_DATA_TABLE_ENTRY>>
420+
///
421+
Result<std::vector<LDR_DATA_TABLE_ENTRY>>
422+
Modules();
423+
424+
425+
// TODO (finish):
418426
// - inject
419427
// - hook
420428

@@ -478,6 +486,11 @@ class Process
478486
Result<std::unique_ptr<u8[]>>
479487
QueryInternal(const PROCESSINFOCLASS, const usize);
480488

489+
Result<std::vector<LDR_DATA_TABLE_ENTRY>>
490+
EnumerateLocalModules();
491+
492+
Result<std::vector<LDR_DATA_TABLE_ENTRY>>
493+
EnumerateRemoteModules();
481494

482495
private: // Members
483496
u32 m_ProcessId {0};

Modules/Process/Source/Win32/Process.cpp

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "Win32/Process.hpp"
2+
13
#include <accctrl.h>
24
#include <aclapi.h>
35
#include <psapi.h>
@@ -10,7 +12,6 @@
1012
#include "Log.hpp"
1113
#include "Utils.hpp"
1214
#include "Win32/API.hpp"
13-
#include "Win32/Process.hpp"
1415
#include "Win32/System.hpp"
1516
#include "Win32/Thread.hpp"
1617

@@ -26,6 +27,13 @@ usize
2627
GetPebLength();
2728
EXTERN_C_END
2829

30+
using CriticalSection = GenericHandle<
31+
RTL_CRITICAL_SECTION,
32+
[](auto p)
33+
{
34+
::LeaveCriticalSection(p);
35+
}>;
36+
2937

3038
namespace pwn::Process
3139
{
@@ -570,6 +578,71 @@ Process::QueryInternal(const PROCESSINFOCLASS ProcessInformationClass, const usi
570578
return Ok(std::move(Buffer));
571579
}
572580

581+
Result<std::vector<LDR_DATA_TABLE_ENTRY>>
582+
Process::Modules()
583+
{
584+
return IsRemote() ? EnumerateRemoteModules() : EnumerateLocalModules();
585+
}
586+
587+
588+
Result<std::vector<LDR_DATA_TABLE_ENTRY>>
589+
Process::EnumerateLocalModules()
590+
{
591+
std::vector<LDR_DATA_TABLE_ENTRY> res;
592+
auto peb = Peb();
593+
CriticalSection csLoaderLock {[&]()
594+
{
595+
auto lock = peb->LoaderLock;
596+
::EnterCriticalSection(lock);
597+
return lock;
598+
}()};
599+
600+
if ( !peb->Ldr->Initialized )
601+
return Ok(res);
602+
603+
604+
auto head = &(peb->Ldr->InLoadOrderModuleList);
605+
606+
for ( auto cur = head; cur->Flink && cur->Flink != head; cur = cur->Flink )
607+
{
608+
auto ptr = CONTAINING_RECORD(cur, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
609+
610+
// HACK We copy for now because the module may have been unloaded by the time we access it
611+
LDR_DATA_TABLE_ENTRY entry {};
612+
::memcpy(&entry, ptr, sizeof(LDR_DATA_TABLE_ENTRY));
613+
res.emplace_back(entry);
614+
}
615+
616+
return Ok(res);
617+
}
618+
619+
Result<std::vector<LDR_DATA_TABLE_ENTRY>>
620+
Process::EnumerateRemoteModules()
621+
{
622+
std::vector<LDR_DATA_TABLE_ENTRY> res;
623+
auto ppeb = Peb();
624+
auto mem = Memory(*this);
625+
auto peb_buf = Value(mem.Read((uptr)ppeb, sizeof(PEB)));
626+
auto peb = reinterpret_cast<PPEB>(peb_buf.data());
627+
628+
auto ldr_buf = Value(mem.Read((uptr)peb->Ldr, sizeof(PEB_LDR_DATA)));
629+
auto ldr = reinterpret_cast<PPEB_LDR_DATA>(ldr_buf.data());
630+
auto head = &(ldr->InLoadOrderModuleList);
631+
632+
for ( auto cur = head; cur->Flink && cur->Flink != head; cur = cur->Flink )
633+
{
634+
LDR_DATA_TABLE_ENTRY entry {};
635+
auto buf = Value(mem.Read(
636+
(uptr)CONTAINING_RECORD(cur, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks),
637+
sizeof(LDR_DATA_TABLE_ENTRY)));
638+
::memcpy(&entry, buf.data(), sizeof(LDR_DATA_TABLE_ENTRY));
639+
res.emplace_back(entry);
640+
}
641+
642+
return Ok(res);
643+
}
644+
645+
573646
#pragma endregion Process
574647

575648

@@ -633,7 +706,6 @@ AppContainer::AppContainer(
633706
throw std::runtime_error("Failed to get SID");
634707
}
635708

636-
637709
dbg(L"sid={}", m_SidAsString.c_str());
638710

639711
//

Modules/Process/Tests/pwn_win_process.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,35 @@ TEST_CASE("Process Memory", "[" NS "]")
151151
// TODO
152152
}
153153
}
154+
155+
156+
TEST_CASE("Process Modules", "[" NS "]")
157+
{
158+
SECTION("Local")
159+
{
160+
auto CurrentProcess = Process::Current();
161+
auto mods = CurrentProcess.Modules();
162+
REQUIRE(Success(mods));
163+
164+
for ( auto const& mod : Value(mods) )
165+
{
166+
// Check betterer
167+
CHECK((((uptr)mod.DllBase) & 0xfff) == 0);
168+
}
169+
}
170+
171+
SECTION("Remote")
172+
{
173+
auto values = Value(System::PidOf(L"explorer.exe"));
174+
REQUIRE(values.size() > 0);
175+
176+
auto explorer = Process::Process(values[0]);
177+
auto mods = explorer.Modules();
178+
REQUIRE(Success(mods));
179+
180+
for ( auto const& mod : Value(mods) )
181+
{
182+
CHECK((((uptr)mod.DllBase) & 0xfff) == 0);
183+
}
184+
}
185+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<a href="https://discord.gg/5HmwPxy3HP"><img alt="Discord" src="https://img.shields.io/badge/Discord-BlahCats-yellow"></a>
1111
<a href="https://github.dev/hugsy/pwn--"><img alt="Read Code" src="https://img.shields.io/badge/Code-Read%20pwn++-brightgreen?logo=visualstudiocode"></a>
1212
<a href="https://open.vscode.dev/hugsy/pwn--"><img alt="Open in VSCode" src="https://img.shields.io/static/v1?logo=visualstudiocode&label=&message=Open%20in%20VSCode&labelColor=2c2c32&color=007acc&logoColor=007acc"></a>
13-
<a href="https://github.com/hugsy/pwn--/actions?query=workflow%3A%22CI+Build+for+MSVC%22"><img alt="CI" src="https://github.com/hugsy/pwn--/workflows/CI%20Build%20for%20MSVC/badge.svg"></a>
13+
<a href="https://github.com/hugsy/pwn--/actions?query=workflow%3A%22Build%22"><img alt="CI" src="https://github.com/hugsy/pwn--/workflows/Build/badge.svg"></a>
1414
</p>
1515

1616
## Quick start

0 commit comments

Comments
 (0)