MFC C++ application: how to clear command line arguments in Task Manager?

Ji Shirley 181 Reputation points
2020-08-04T05:08:34.413+00:00

I have a MFC C++ application which uses command line arguments. But when the program is running, sensitive information entered on the command line will be displayed in the Command Line column of the Task Manager. 1.So how to delete them? 2.How to change command line arguments in MFC C++?
I use following function in my x32 InitInstance , but it does not work. The Param.CommandLine.Buffer has been changed to empty, because Task Manager still display Command Line. Is there any mistake?

#include <Windows.h>
#include <Winternl.h>
#include <stdio.h>
#include <tchar.h>

typedef NTSTATUS (NTAPI *PFN_NT_QUERY_INFORMATION_PROCESS) (
    IN HANDLE ProcessHandle,
    IN PROCESSINFOCLASS ProcessInformationClass,
    OUT PVOID ProcessInformation,
    IN ULONG ProcessInformationLength,
    OUT PULONG ReturnLength OPTIONAL);

void ClearCommandLine()
{
    HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
    PROCESS_BASIC_INFORMATION pbi = {0};
    RTL_USER_PROCESS_PARAMETERS Param = {0};
    PFN_NT_QUERY_INFORMATION_PROCESS pfnNtQueryInformationProcess =
    (PFN_NT_QUERY_INFORMATION_PROCESS) GetProcAddress (
                                     GetModuleHandle(TEXT("ntdll.dll")), "NtQueryInformationProcess");
    NTSTATUS status = pfnNtQueryInformationProcess (
                                    hProcess, ProcessBasicInformation, (PVOID)&pbi, sizeof(pbi), NULL);

    wchar_t* lpwszCmd=L"";
    USHORT usCmdLen = 2 + 2 * (wcslen(lpwszCmd));
    ReadProcessMemory(hProcess, pbi.PebBaseAddress, &peb, sizeof(peb), NULL);
    ReadProcessMemory(hProcess, peb.ProcessParameters, &Param, sizeof(Param), NULL);
    WriteProcessMemory(hProcess, Param.CommandLine.Buffer, lpwszCmd, usCmdLen,NULL);
    WriteProcessMemory(hProcess,&Param.CommandLine.Length, &usCmdLen, sizeof(usCmdLen), NULL);

    CloseHandle(hProcess);
}
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,544 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rita Han - MSFT 2,161 Reputation points
    2020-08-04T07:53:17.927+00:00

    Hello,

    The command line is stored in process memory, even if you clear it in memory, Task Manager still hold a copy.
    So for protecting sensitive information, one suggested solution is store them encrypted in a file and then load that file as a parameter.

    Thank you!


  2. RLWA32 40,771 Reputation points
    2020-08-04T09:25:51.77+00:00

    Another possibility is to obtain the sensitive information from the user at runtime from a dialog displayed by the application's InitInstance function.