目前,我在尝试从命名管道连接创建的以null结尾的缓冲区执行C ++字符串比较时遇到问题。
以下是相关的C ++服务器端代码:
// now block for data from the pipe
wchar_t buffer[10];
DWORD numBytesRead = 0;
BOOL fSuccess = ReadFile(
pipe, // handle to pipe
buffer, // buffer to receive data
127 * sizeof(wchar_t), // size of buffer
&numBytesRead, // number of bytes read
NULL); // not overlapped I/O
// create pill logic
if (fSuccess) {
buffer[numBytesRead / sizeof(wchar_t)] = '\0'; // null-term string
wcout << "[*] Server recv bytes: " << numBytesRead << endl;
printf("[*] Client sent the following message: %s \n", buffer);
wcout << wcscmp(buffer, L"00") << endl; //attempt to compare strings
if (buffer) {
if (wcscmp(buffer, L"00") == 0) {
//00 = stop - dont write / output data(pause)
wcout << "[!] Server received stop message - now pausing" << endl;
pill = 0;
}
,客户端连接是PowerShell:
param ($ComputerName = '.')
$npipeClient = New-Object System.IO.Pipes.NamedPipeClientStream($ComputerName, 'my_pipe', [System.IO.Pipes.PipeDirection]::InOut,
[System.IO.Pipes.PipeOptions]::None,
[System.Security.Principal.TokenImpersonationLevel]::Impersonation)
$pipeReader = $pipeWriter = $null
try {
$npipeClient.Connect()
$pipeReader = new-object System.IO.StreamReader($npipeClient)
$pipeWriter = new-object System.IO.StreamWriter($npipeClient)
$pipeWriter.AutoFlush = $true
# Connect Loop
while (1) {
write-host "[*] Server Sent: " $pipeReader.ReadLine()
$User = Read-Host -Prompt 'Send code to server: '
$pipeWriter.Write($User)
}
最后,这是服务器的输出:
[*] Starting pipe server! [*] Pipe thread started! [*] Pipe server waiting for command... [*] Server sent bytes: 92 [*] Server recv bytes: 2 [*] Client sent the following message: 00 1 [!] Server received invalid request! [*] Server sent bytes: 92 [*] Server recv bytes: 5 [*] Client sent the following message: 0000 1 [!] Server received invalid request! [*] Server sent bytes: 92 [*] Server recv bytes: 1 [*] Client sent the following message: -1 [!] Server received invalid request! [*] Server sent bytes: 92
有趣的是,当我发送一个字符时,它无法正常显示。我不知道这是否来自我打印缓冲区的方式。