gpt4 book ai didi

带有通过标准输入传递的目标的 CreateProcessW - 找不到目标

转载 作者:太空宇宙 更新时间:2023-11-04 04:14:01 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <windows.h>

#pragma warning(disable : 4996)
int main(int argc, wchar_t** argv)
{
DWORD bytes_read;
WCHAR* buffer[4096];
LPWSTR str;
STARTUPINFO start_info = { 0 };
PROCESS_INFORMATION process_info = { 0 };

ReadFile(GetStdHandle(STD_INPUT_HANDLE), buffer, 4096, &bytes_read, NULL);
str = malloc(bytes_read);
memcpy(str, buffer, bytes_read);

// debug - stdout + file
wprintf(L"\nTrying to run -> \"%LS\"", str);
FILE *f = fopen("file.txt", "w");
fwprintf(f, L"%LS", str);
fclose(f);

BOOL result = CreateProcessW(
NULL,
str,
NULL,
NULL,
TRUE,
CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&start_info,
&process_info
);

if (!result) {
DWORD err = GetLastError();
WCHAR *errstr;

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&errstr, 0, NULL
);

wprintf(L"\nError -> %ls", errstr);
return 1;
}

return 0;
}

运行它会产生以下结果

C:\Users\Admin\source\repos\ConsoleApplication1\Debug                         
λ php -r "echo \"C:\\Windows\\System32\\PING.EXE\0\";"|ConsoleApplication1.exe

Trying to run -> "C:\Windows\System32\PING.EXE"
Error -> The system cannot find the file specified.

看来它使用了正确的输入。同样在 file.txt 中,在十六进制编辑器中查看时似乎没有任何可疑之处

hex dump of a file

当像下面这样硬编码 str 而不是使用 stdin 时

wchar_t cmd[] = L"C:\\Windows\\System32\\PING.EXE";

有效。

我需要做什么才能使 CreateProcessW 使用标准输入输入?

最佳答案

WCHAR* buffer[4096];

这将创建一个字符串数组。您只需要一个宽字符缓冲区。

使用L"%s" 格式说明符进行测试:

wprintf(L"%s\n", buffer)

如果可能,将 Unicode 文本作为命令行传递,示例

php -r "echo \"Test...\0\";"|app.exe Unicode

Unicode 文本可以包含 UTF16 字符集。

int main(void)
{
wprintf(L"GetCommandLineW %s\n", GetCommandLineW());

DWORD bytes_read = 0;
char buffer[4096] = { 0 };
ReadFile(GetStdHandle(STD_INPUT_HANDLE), buffer,
sizeof(buffer), &bytes_read, NULL);

int codepage = CP_UTF8;//or CP_ACP
int wlen = MultiByteToWideChar(codepage, 0, buffer, -1, 0, 0);
wchar_t *wstr = malloc(wlen * 2);
MultiByteToWideChar(codepage, 0, buffer, -1, wstr, wlen);

wprintf(L"pipe %s\n", wstr);

free(wstr);
return 0;
}

关于带有通过标准输入传递的目标的 CreateProcessW - 找不到目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53908208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com