带有通过标准输入传递的目标的 CreateProcessW - 找不到目标
CreateProcessW with target passed via stdin - cannot find the target
#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[=11=]\";"|ConsoleApplication1.exe
Trying to run -> "C:\Windows\System32\PING.EXE"
Error -> The system cannot find the file specified.
看来它使用了正确的输入。同样在 file.txt 中,在十六进制编辑器
中查看时似乎没有任何可疑之处
当像下面这样硬编码 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...[=12=]\";"|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;
}
#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[=11=]\";"|ConsoleApplication1.exe
Trying to run -> "C:\Windows\System32\PING.EXE"
Error -> The system cannot find the file specified.
看来它使用了正确的输入。同样在 file.txt 中,在十六进制编辑器
中查看时似乎没有任何可疑之处当像下面这样硬编码 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...[=12=]\";"|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;
}