ZwqueryInformationProcessを使用して、カーネルドライバーでProcessImageFileNameを取得する方法は?

StackOverflow https://stackoverflow.com/questions/3707133

  •  02-10-2019
  •  | 
  •  

質問

アプリケーション用のシンプルなカーネルドライバーを書いています(非常にシンプルなマルウェア対策アプリケーションを考えてください。)

私は夢中になりました ZwOpenFile() 使用されています PsGetCurrentProcess() 発信者プロセスを処理するため。

ペプロセス構造を返します。

PEPROCESS proc = PsGetCurrentProcess();

私は使用しています ZwQueryInformationProcess() を取得します PIDImageFileName:

DbgPrint("ZwOpenFile Called...\n");
DbgPrint("PID: %d\n", PsGetProcessId(proc));
DbgPrint("ImageFileName: %.16s\n", PsGetProcessImageFileName(proc));

プロセスを取得しようとしています FullPath このように(しかし、私はBSODを取得します):

WCHAR strBuffer[260];
UNICODE_STRING str;

//initialize
str.Buffer = strBuffer;
str.Length = 0x0;
str.MaximumLength = sizeof(strBuffer);

//note that the seconds arg (27) is ProcessImageFileName
ZwQueryInformationProcess(proc, 27, &str, sizeof(str), NULL);

DbgPrint("FullPath: %wZ\n", str.Buffer);

DbgView Output

ご覧のとおり str.Buffer 空またはごみで満たされています。おそらく、充填中のバッファオーバーフロー str 経由 ZwQueryInformationProcess() BSODをトリガーします。

alt text

どんな助けも感謝します。

役に立ちましたか?

解決

このAPIのMSDNドキュメントは、それを示しています

ProcessInformationClassパラメーターがProcessImageFileNameの場合、プロセス情報パラメーターによって指されるバッファーは、Sunicode_String構造と文字列自体を保持するのに十分な大きさでなければなりません。バッファメンバーに保存されている文字列は、画像file.fileの名前です。

これを念頭に置いて、このようなバッファ構造を変更してみることをお勧めします。

WCHAR strBuffer[(sizeof(UNICODE_STRING) / sizeof(WCHAR)) + 260];
UNICODE_STRING str;
str = (UNICODE_STRING*)&strBuffer;

//initialize
str.Buffer = &strBuffer[sizeof(UNICODE_STRING) / sizeof(WCHAR)];
str.Length = 0x0;
str.MaximumLength = 260 * sizeof(WCHAR);

//note that the seconds arg (27) is ProcessImageFileName
ZwQueryInformationProcess(proc, 27, &strBuffer, sizeof(strBuffer), NULL);

さらに、コードはこちらのドキュメントで説明されているエラーケースを確認および処理する必要があります。これが、BSODトリガーケースを逃した理由かもしれません。

バッファが小さすぎる場合、status_info_length_mismatchエラーコードで関数が失敗し、returnelentsパラメーターが必要なバッファサイズに設定されます。

他のヒント

//関数定義の前にそうでない場合は、このコードをヘッダーファイルで宣言します。

typedef NTSTATUS (*QUERY_INFO_PROCESS) (
__in HANDLE ProcessHandle,
__in PROCESSINFOCLASS ProcessInformationClass,
__out_bcount(ProcessInformationLength) PVOID ProcessInformation,
__in ULONG ProcessInformationLength,
__out_opt PULONG ReturnLength
);

QUERY_INFO_PROCESS ZwQueryInformationProcess;

//関数定義

NTSTATUS GetProcessImageName(HANDLE processId, PUNICODE_STRING ProcessImageName)
{
NTSTATUS status;
ULONG returnedLength;
ULONG bufferLength;
HANDLE hProcess;
PVOID buffer;
PEPROCESS eProcess;
PUNICODE_STRING imageName;

PAGED_CODE(); // this eliminates the possibility of the IDLE Thread/Process

status = PsLookupProcessByProcessId(processId, &eProcess);

if(NT_SUCCESS(status))
{
    status = ObOpenObjectByPointer(eProcess,0, NULL, 0,0,KernelMode,&hProcess);
    if(NT_SUCCESS(status))
    {
    } else {
        DbgPrint("ObOpenObjectByPointer Failed: %08x\n", status);
    }
    ObDereferenceObject(eProcess);
} else {
    DbgPrint("PsLookupProcessByProcessId Failed: %08x\n", status);
}


if (NULL == ZwQueryInformationProcess) {

    UNICODE_STRING routineName;

    RtlInitUnicodeString(&routineName, L"ZwQueryInformationProcess");

    ZwQueryInformationProcess =
           (QUERY_INFO_PROCESS) MmGetSystemRoutineAddress(&routineName);

    if (NULL == ZwQueryInformationProcess) {
        DbgPrint("Cannot resolve ZwQueryInformationProcess\n");
    }
}

/* Query the actual size of the process path */
status = ZwQueryInformationProcess( hProcess,
                                    ProcessImageFileName,
                                    NULL, // buffer
                                    0, // buffer size
                                    &returnedLength);

if (STATUS_INFO_LENGTH_MISMATCH != status) {
    return status;
}

/* Check there is enough space to store the actual process
   path when it is found. If not return an error with the
   required size */
bufferLength = returnedLength - sizeof(UNICODE_STRING);
if (ProcessImageName->MaximumLength < bufferLength)
{
    ProcessImageName->MaximumLength = (USHORT) bufferLength;
    return STATUS_BUFFER_OVERFLOW;   
}

/* Allocate a temporary buffer to store the path name */
buffer = ExAllocatePoolWithTag(NonPagedPool, returnedLength, 'uLT1');

if (NULL == buffer) 
{
    return STATUS_INSUFFICIENT_RESOURCES;   
}

/* Retrieve the process path from the handle to the process */
status = ZwQueryInformationProcess( hProcess,
                                    ProcessImageFileName,
                                    buffer,
                                    returnedLength,
                                    &returnedLength);

if (NT_SUCCESS(status)) 
{
    /* Copy the path name */
    imageName = (PUNICODE_STRING) buffer;
    RtlCopyUnicodeString(ProcessImageName, imageName);
}

/* Free the temp buffer which stored the path */
ExFreePoolWithTag(buffer, 'uLT1');

return status;
}

// function call ..このコードを事前操作に書き込むコールバックとIRQはpassive_levelである必要があります

PEPROCESS objCurProcess=NULL;
HANDLE hProcess;
UNICODE_STRING fullPath;

objCurProcess=IoThreadToProcess(Data->Thread);//Note: Date is type of FLT_CALLBACK_DATA which is in PreOperation Callback as argument

hProcess=PsGetProcessID(objCurProcess);

fullPath.Length=0;
fullPath.MaximumLength=520;
fullPath.Buffer=(PWSTR)ExAllocatePoolWithTag(NonPagedPool,520,'uUT1');

GetProcessImageName(hProcess,&fullPath);

FullPath変数には、プロセスのフルパスがあります。プロセスがExplorer.exeである場合、パスは次のようになります。

\Device\HarddiskVolume3\Windows\explorer.exe

注: device harddiskvolume3パスは、マシンとハードディスクのさまざまなボリュームのために変更される場合があります。これは私の場合の例です。

ZwQueryInformationProcess 必要です HANDLE, 、ではありません PROCESS!使用する必要があります ObOpenObjectByPointer 最初にハンドルを取得します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top