#include "stdafx.h"
#include <stdio.h>
#include <WINDOWS.H>
#define SEND_BUFF_SIZE 1024
//实现去除执行结果中的 命令字符串+"\n"
void print(char *cmdstr)
{
while(*((char*)cmdstr++)!='\n');
printf(cmdstr);
}
int main()
{
HANDLE hReadPipe1,hWritePipe1,hReadPipe2,hWritePipe2; //四个HANDLE 用来创建两个管道
CHAR Buff[SEND_BUFF_SIZE] = {0};
CHAR sendBuff[SEND_BUFF_SIZE] = ("dir \n");
//安全属性的东西
SECURITY_ATTRIBUTES sa;
sa.nLength=sizeof(sa);
sa.lpSecurityDescriptor=0;
sa.bInheritHandle=true;
int ret;
if(!CreatePipe(&hReadPipe1,&hWritePipe1,&sa,0))//创建两个匿名管道 导向DOS输入输出通道
{
return -1;
}
if(!CreatePipe(&hReadPipe2,&hWritePipe2,&sa,0))
{
return -1;
}
//启动信息
STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
//GetStartupInfo(&si);
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hReadPipe2;
si.hStdOutput = si.hStdError = hWritePipe1;
char cmdLine[256] = {0};
GetSystemDirectory(cmdLine,sizeof(cmdLine));
strcat(cmdLine, ("\\cmd.exe"));
PROCESS_INFORMATION ProcessInformation;
if(CreateProcess(cmdLine,NULL,NULL,NULL,TRUE,0,NULL,NULL,&si,&ProcessInformation) == 0)
{
return -1;
}
unsigned long lBytesRead,lBytesWrite;//读写数量存放变量
while(TRUE){
lBytesRead=0;
ZeroMemory(Buff,sizeof(Buff));
ret=PeekNamedPipe(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesRead,0,0);//管道是否有数据可读
if(lBytesRead)
{
//第一次可以读到cmd的初始化信息
memset(Buff, 0, sizeof(Buff));
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesRead,0);//读取管道里的数据
//如果读到数据,则对数据进行下一步处理。。。。。。。
//printf(Buff);
print(Buff);
}
else{
//解决方案 估计windows是以"\r\n"结束的吧,所以要在命令执行后,显示完全后写入这个
[COLOR="Blue"] /***********************************************************/
WriteFile(hWritePipe2, "\r\n",2,&lBytesWrite,0);
Sleep(100);
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesWrite,0);//读取管道里的数据
/***********************************************************/[/COLOR]
//读入输入数据 以回车结束
char ch;
ZeroMemory(sendBuff,sizeof(sendBuff));
int count=0;
while((ch=getchar())!='\n')
{
sendBuff[count]=ch;
count++;
}
//加入回车
strcat(sendBuff,"\r\n");
//写入数据,执行命令
if(!WriteFile(hWritePipe2, sendBuff,sizeof(sendBuff),&lBytesWrite,0))
{
printf("WriteFile Error!!\r\n");
return -1;
}
//延时,等待程序执行
Sleep(100);
}
}
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)