一個管道有讀端和寫端,當你把這個管道和一個進程搭接起來的時候,可以用這個管道當作輸入,也可以用這個管道當作輸出,如果是要向管道輸入,則數據輸入端是寫端,另一端是讀端,如果是由管道輸出,則由進程寫入管道,是寫端,另一端是讀端,也就是接收進程的輸出數據的一端,因此如果要重定向進程的輸入和輸出,則需要2根管道.
以下代碼演示了創建CMD.EXE進程,并用2根管道重定向他的輸入輸出,并讀取CMD產生的結果數據.
千萬不要忘記設置SECURITY_ATTRIBUTES里的bInheritHandle,比如創建管道的時候要把bInheritHandle設置為TRUE
同樣不一定要用管道,比如文件,SOCKET等都可以重定向.
如果是用SOCKET,根據網上的名詞,可以創建一個"零管道后門"程序.比如在服務器端上的后門程序監聽某個端口,一旦有連接請求,接受后創建SOCKET,就在這個時候開啟CMD.EXE,并重定向輸入輸出到服務器上的這個SOCKET,這樣便為遠程的這個連接請求開了一個服務器上的后門,從而遠程連接可以執行服務器并返回結果.
演示代碼如下:
//
// File: Main.cpp
// Purpose: Creates a process( cmd.exe for example ), and redirect its standard input
// by using creating a pipe, then writes some bytes as commands to pipe.
//
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
// Error report
void Error( const char *szErrMsg );
int main( int argc, char **argv )
{
// Create pipe
BOOL bRet;
HANDLE hPipeRead, hPipeWrite;
HANDLE hPipeReadII, hPipeWriteII;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof( SECURITY_ATTRIBUTES );
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
bRet = ::CreatePipe( &hPipeRead, &hPipeWrite, &sa, 512 );
bRet &= ::CreatePipe( &hPipeReadII, &hPipeWriteII, &sa, 512 );
if ( !bRet )
{
Error( "Can't create pipe!" );
return -1;
}
// Spawn a process
STARTUPINFO StartInfo;
PROCESS_INFORMATION ProcessInfo;
// memset( &StartInfo, 0, sizeof(StartInfo) );
// memset( &ProcessInfo, 0, sizeof(ProcessInfo) );
::GetStartupInfo( &StartInfo );
StartInfo.cb = sizeof(StartInfo);
StartInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
StartInfo.wShowWindow = SW_HIDE;
StartInfo.hStdInput = hPipeRead;
StartInfo.hStdOutput = hPipeWriteII; //::GetStdHandle( STD_OUTPUT_HANDLE );
StartInfo.hStdError = hPipeWriteII; //::GetStdHandle( STD_OUTPUT_HANDLE );
bRet = ::CreateProcess( TEXT("c:\\windows\\system32\\cmd.exe"), NULL, NULL, NULL, TRUE, 0, NULL, NULL, &StartInfo, &ProcessInfo );
::Sleep(1000);
printf("wake up...\n");
if ( !bRet )
{
Error( "Can't create process!" );
return -1;
}
// Write commands to pipe
char *szCmd = "netstat\r\n";
DWORD dwDummy;
::WriteFile( hPipeWrite, szCmd, 10, &dwDummy, NULL );
::Sleep(2000);
printf("had written...\n");
// Read from pipe
char szBuf[1024];
memset(szBuf, 0, sizeof(szBuf));
::ReadFile( hPipeReadII, szBuf, sizeof(szBuf), &dwDummy, NULL );
// Don't leave till the spawned process goes end
// ::Sleep(2000);
printf("had read...\n");
// ::WaitForSingleObject( ProcessInfo.hProcess, INFINITE );
printf("%s\n", szBuf);
return 0;
}
void Error( const char *szErrMsg )
{
printf( "Error: %s\n", szErrMsg );
}
在WinXP SP2 + VS 2005 Express Edition上編譯并運行通過
這個版本的VS需要下載并設置Platform SDK,并另外找一個MSVCRT80D.DLL如果是DEBUG版本的DLL,并設置
包含和鏈接目錄和鏈接器命令行(user32,kernel32,libcmt)