双管道(CreatePipe)与cmd.exe进程间通信的有关问题 完美解决
2013-11-17 16:18
375 查看
最近研究怎么在别人电脑里开个CMD shell,就是直接用CMD执行我们的命令,遇到不少问题,但终于把问题完美解决了!!!!
源代码:http://download.csdn.net/detail/aq782645210/6566225
问题1:为什么启动cmd.exe时能读到(ReadFile)的初始化信息,但是再WriteFile(往管道里写数据后),cmd却没有任何信息,即PeekNamedPipe函数中的参数lBytesRead总是为零?难道是管道没写进去?
问题2:怎么有时输出 “MORE? ”的字符串?这是什么东西?
解决方案看代码:#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"结束的吧,所以要在命令执行后,显示完全后写入这个
/***********************************************************/
WriteFile(hWritePipe2, "\r\n",2,&lBytesWrite,0);
Sleep(100);
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesWrite,0);//读取管道里的数据
/***********************************************************/
//读入输入数据 以回车结束
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);
}
}
}
//解决方案 估计windows是以"\r\n"结束的吧,所以要在命令执行后,显示完全后写入这个
/***********************************************************/
WriteFile(hWritePipe2, "\r\n",2,&lBytesWrite,0);
Sleep(100);
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesWrite,0);//读取管道里的数据
/***********************************************************/
蓝色部分就是解决代码,原来程序执行完后,并没有像DOS那样直接加上"\r\n",就是结束符,所以我们只有自己动手了,经过测试完全OK
我相信大家都可以写远程SHELL了吧,哦哈哈哈~~~~
已经在看雪同步发布,呵呵 ~~~
源代码:http://download.csdn.net/detail/aq782645210/6566225
问题1:为什么启动cmd.exe时能读到(ReadFile)的初始化信息,但是再WriteFile(往管道里写数据后),cmd却没有任何信息,即PeekNamedPipe函数中的参数lBytesRead总是为零?难道是管道没写进去?
问题2:怎么有时输出 “MORE? ”的字符串?这是什么东西?
解决方案看代码:#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"结束的吧,所以要在命令执行后,显示完全后写入这个
/***********************************************************/
WriteFile(hWritePipe2, "\r\n",2,&lBytesWrite,0);
Sleep(100);
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesWrite,0);//读取管道里的数据
/***********************************************************/
//读入输入数据 以回车结束
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);
}
}
}
//解决方案 估计windows是以"\r\n"结束的吧,所以要在命令执行后,显示完全后写入这个
/***********************************************************/
WriteFile(hWritePipe2, "\r\n",2,&lBytesWrite,0);
Sleep(100);
ret=ReadFile(hReadPipe1,Buff,SEND_BUFF_SIZE,&lBytesWrite,0);//读取管道里的数据
/***********************************************************/
蓝色部分就是解决代码,原来程序执行完后,并没有像DOS那样直接加上"\r\n",就是结束符,所以我们只有自己动手了,经过测试完全OK
我相信大家都可以写远程SHELL了吧,哦哈哈哈~~~~
已经在看雪同步发布,呵呵 ~~~
相关文章推荐
- 完美解决"javaw.exe-损坏的图像"问题
- 完美解决事件查看器中查看应用程序日志导致MMC.EXE出错问题
- (转)完美解决 Android WebView 文本框获取焦点后自动放大有关问题
- windows找不到文件cmd.exe,regedit.exe,msconfig.exe等问题的解决办法
- VS2015+Qt5.9.1编译报错:Moc系统找不到指定路径,error MSB6006 cmd.exe 已退出,代码为3 -- 完美解决
- 解决C# WinForm 中 VSHOST.EXE 程序不关闭的有关问题
- 双管道与cmd.exe进程间通信演示
- 完美解决无法无法显示隐藏文件的问题(svohost.exe xsx.exe)
- 完美解决事件查看器中查看应用程序日志导致MMC.EXE出错问题
- 解决visual 2008中error PRJ0003 : 生成 cmd.exe 时出错”的问题---(使用优化大师清理文件之后)
- 关于SQL的安全问题(ftp.exe、cmd.exe的解决方法)
- 完美解决LINK : fatal error LNK1104: cannot open file "Debug/xx.exe"的问题
- 解决Error spawning 'cmd.exe'的问题
- aidl.exe'' finished with non-zero exit value 1问题解决
- 解决ISE使用过程中出现 _SN.exe的问题
- 完美解决 SlidingMenu appcompat_7的冲突问题
- wine 1.1.15 中的中文显示问题完美解决办法
- 解决无法在虚拟机中CMD到真机的问题
- 完美解决php验证码session不同步的问题
- 使用.net完美解决服务器注销后go的web服务停止的问题