2323#include < cryptopp/hex.h>
2424#include < cryptopp/files.h>
2525#include < cryptopp/sha.h>
26+ #include < psapi.h>
2627
2728
2829struct ArgumentManager {
2930
3031 void validateArguments (int argc, char * argv[]) {
3132
3233 namespace po = boost::program_options;
33- std::string version = " v1.0.4 " ;
34+ std::string version = " v1.0.5 " ;
3435 po::options_description description (" Windows memory extractor " + version + " \n Usage" );
3536
3637 description.add_options ()
@@ -67,14 +68,16 @@ struct ArgumentManager {
6768 module = suppliedModule;
6869 isModuleOptionSupplied = true ;
6970 }
71+ }
7072
71- if (vm.count (" join" )) {
72- isJoinOptionSupplied = true ;
73+ if (vm.count (" join" )) {
74+ isJoinOptionSupplied = true ;
75+ if (!isModuleOptionSupplied) {
76+ // The --join option was included to work alongside the --module option
77+ // If the --join option is supplied without the --module option, the tool interprets that the user is asking for the contents of the main module
78+ isModuleOptionSupplied = true ;
7379 }
7480 }
75- else if (vm.count (" join" )) {
76- throw std::invalid_argument{ " The --join option can only be used alongside the --module option" };
77- }
7881
7982 if (vm.count (" pid" )) {
8083 pid = vm[" pid" ].as <int >();
@@ -100,6 +103,10 @@ struct ArgumentManager {
100103 return pid;
101104 }
102105
106+ void setModule (std::string newModule) {
107+ module = newModule;
108+ }
109+
103110 std::string& getModule () {
104111 return module ;
105112 }
@@ -196,23 +203,6 @@ struct MemoryExtractionManager {
196203
197204 void extractMemoryContents () {
198205
199- BYTE* memoryPointer = NULL ; // Virtual address 0x0000000000000000
200-
201- // Module option related variables
202- BYTE* moduleBaseAddress;
203- DWORD moduleSize;
204- size_t moduleBaseAddressAsNumber;
205-
206- // If the --module option is supplied, I only extract the memory corresponding to the requiered module
207- // In order to do that, I start at the module's base address, instead of at virtual address 0x0000000000000000
208- if (argumentManager.getIsModuleOptionSupplied ()) {
209- MODULEENTRY32 moduleInformation = getModuleInformation (argumentManager.getModule ());
210- memoryPointer = moduleInformation.modBaseAddr ;
211- moduleBaseAddress = moduleInformation.modBaseAddr ;
212- moduleSize = moduleInformation.modBaseSize ;
213- moduleBaseAddressAsNumber = reinterpret_cast <size_t >(moduleInformation.modBaseAddr );
214- }
215-
216206 HANDLE processHandle = OpenProcess (PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE , argumentManager.getPid ());
217207 if (processHandle == NULL ) {
218208
@@ -238,6 +228,37 @@ struct MemoryExtractionManager {
238228
239229 }
240230
231+ if (argumentManager.getIsModuleOptionSupplied () && argumentManager.getModule ().length () == 0 ) {
232+ // The user is asking for the contents of the main module
233+ char mainModulePathAsCharArray[MAX_PATH];
234+ if (GetProcessImageFileNameA (processHandle, mainModulePathAsCharArray, MAX_PATH) != 0 ) {
235+ std::string mainModulePath (mainModulePathAsCharArray);
236+ std::string mainModuleName (mainModulePath.substr (mainModulePath.rfind (" \\ " ) + 1 ));
237+ argumentManager.setModule (mainModuleName);
238+ }
239+ else {
240+ CloseHandle (processHandle);
241+ throw std::exception{ " The name of the main module could not be obtained" };
242+ }
243+ }
244+
245+ BYTE* memoryPointer = NULL ; // Virtual address 0x0000000000000000
246+
247+ // Module option related variables
248+ BYTE* moduleBaseAddress;
249+ DWORD moduleSize;
250+ size_t moduleBaseAddressAsNumber;
251+
252+ // If the --module option is supplied, I only extract the memory corresponding to the requiered module
253+ // In order to do that, I start at the module's base address, instead of at virtual address 0x0000000000000000
254+ if (argumentManager.getIsModuleOptionSupplied ()) {
255+ MODULEENTRY32 moduleInformation = getModuleInformation (argumentManager.getModule ());
256+ memoryPointer = moduleInformation.modBaseAddr ;
257+ moduleBaseAddress = moduleInformation.modBaseAddr ;
258+ moduleSize = moduleInformation.modBaseSize ;
259+ moduleBaseAddressAsNumber = reinterpret_cast <size_t >(moduleInformation.modBaseAddr );
260+ }
261+
241262 directoryName = createDirectory ();
242263
243264 std::ofstream resultsFile (directoryName + " /results.txt" , std::ofstream::out);
0 commit comments