2016-09-11 4 views
0

別のプロセスからすべてのモジュールを取得したい。しかし、それは不条理な価値を返す。プログラムはdo-whileループに一度だけ留まります。その後、do-whileループが終了します。他のプロセスからロードされたモジュールを取得する

どこが間違っているのかわかりません - どうすれば修正できますか?私はプログラムが何度もdo-whileループに入っていなければならないことを知っていますが、そうではありません。

NTSTATUS Status; 
PROCESS_BASIC_INFORMATION pbi; 
ULONG ReturnLength; 
Status = NtQueryInformationProcess(
    INJECTOR_INFO.process.processHandle, 
    ProcessBasicInformation, 
    &pbi, 
    sizeof(PROCESS_BASIC_INFORMATION), 
    &ReturnLength); 
if (!NT_SUCCESS(Status)) { 
    printf("NtQueryInformationProcess failed.(pbi)\n"); 
    return; 
} 
else { 
    PLIST_ENTRY HeadEntry = pbi.PebBaseAddress->LoaderData->InMemoryOrderModuleList.Flink; 
    PLIST_ENTRY nextEntry = pbi.PebBaseAddress->LoaderData->InMemoryOrderModuleList.Blink; 


    DWORD dwBytesRead = 0; 
    PLDR_MODULE pLdrModule = nullptr; 
    LDR_MODULE LdrModule; 
    do 
    { 
     LDR_DATA_TABLE_ENTRY LdrEntry; 
     PLDR_DATA_TABLE_ENTRY Base = CONTAINING_RECORD(HeadEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks); 

     if (NT_SUCCESS(Status = NtReadVirtualMemory(INJECTOR_INFO.process.processHandle, Base, &LdrEntry, sizeof(LdrEntry), &dwBytesRead))) 
     { 
      if (dwBytesRead != sizeof(LdrEntry)) { 
       printf("length doesn't match"); 
       return; 
      } 
      char* pLdrModuleOffset = reinterpret_cast<char*>(HeadEntry) - sizeof(LIST_ENTRY); 
      if (!NT_SUCCESS(Status = NtReadVirtualMemory(INJECTOR_INFO.process.processHandle, pLdrModuleOffset, &pLdrModule, sizeof(pLdrModule), &dwBytesRead))) { 
       printf("pLdrModuleOffset doesn't read"); return; 
      }else if (dwBytesRead != sizeof(pLdrModule)) { printf("pLdrModule length doesn't match"); return; } 
      if (!NT_SUCCESS(Status = NtReadVirtualMemory(INJECTOR_INFO.process.processHandle, pLdrModule, &LdrModule, sizeof(LdrModule), &dwBytesRead))) { 
       printf("pLdrModule doesn't read"); return; 
      }else if (dwBytesRead != sizeof(LdrModule)) { printf("LdrModule length doesn't match"); return; } 

      if (LdrEntry.DllBase) 
      { 
       printf("BaseAddress:  %p\n", LdrModule.BaseAddress); 
       printf("Reference Count: %d\n", LdrModule.LoadCount); 
      } 

      HeadEntry = LdrEntry.InMemoryOrderLinks.Flink; 
     } 
     else { printf("LDR_DATA_TABLE_ENTRY doesn't read"); return; } 
    } while (HeadEntry != nextEntry); 
} 

私は変数の値のためNtQueryInformationProcess後!NT_SUCCESS(Status)にブレークポイントを置く:DO中の変数の値に対する

Values after NtQueryInformationProcess

別のブレークポイントながら、最初のサイクルの終わりのために:

Values for the end of the first cycle

+0

[EnumProcessModules](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682631.aspx)に問題がありますか? – IInspectable

答えて

0

リモートプロセスに問い合わせています。PROCESS_BASIC_INFORMATIONが、その後、あなたはあなた自身のプロセス内のポインタをたどるに進ん: は、リモートプロセスからLoaderDataを読んで、これが機能するために

PLIST_ENTRY HeadEntry = pbi.PebBaseAddress->LoaderData->InMemoryOrderModuleList.Flink;

は、(pbi.PebBaseAddressから)リモートプロセスからPEBを読みます(PEB.LoaderData)。 次に、InMemoryOrderModuleListに従ってください(もう一度、リモートプロセスからデータを読み込みます)。

リモートプロセスから各エントリを読み取ることで、リスト全体を反復処理できます。

関連する問題