如何用Visual C++实现远程线程嵌入?

xdimfan 2004-05-03 08:34:56


目前正在学习dll方面的知识,对于远程线程的嵌入不太明白如何实现,请高手指教:)
...全文
68 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ygzdev 2004-05-03
  • 打赏
  • 举报
回复
嘿嘿,这个看《WINDOWS 核心编程》里面就有啦!不过我想问楼上的PS工具是什么啊,我不知道,介绍一下如何!
itmaster 2004-05-03
  • 打赏
  • 举报
回复
远程线程技术指的是通过在另一个进程中创建远程线程的方法进入那个进程的内存地址空间。我们知道,在进程中,可以通过CreateThread函数创建线程,被创建的新线程与主线程(就是进程启动时被同时自动建立的那个线程)共享地址空间以及其他的资源。 但是很少有人知道,通过CreateRemoteThread也同样可以在另一个进程内创建新线程,被创建的远程线程同样可以共享远程进程(是远程进程耶!)的地址空间,所以,实际上,我们通过一个远程线程,进入了远程进程的内存地址空间,也就拥有了那个远程进程相当的权限。例如在远程进程内部启动一个DLL木马(与进入进程内部相比,启动一个DLL木马是小意思,实际上我们可以随意篡改那个远程进程的数据)。

  首先,我们通过OpenProcess 来打开我们试图嵌入的进程(如果远程进程不允许打开,那么嵌入就无法进行了,这往往是由于权限不足引起的,解决方法是通过种种途径提升本地进程的权限)

 hRemoteProcess = OpenProcess( PROCESS_CREATE_THREAD | file://允许远程创建线程
                PROCESS_VM_OPERATION | file://允许远程VM操作
                PROCESS_VM_WRITE,//允许远程VM写
                FALSE, dwRemoteProcessId )

  由于我们后面需要写入远程进程的内存地址空间并建立远程线程,所以需要申请足够的权限(PROCESS_CREATE_THREAD、VM_OPERATION、VM_WRITE)。

  然后,我们可以建立LoadLibraryW函数这个线程来启动我们的DLL木马,LoadLibraryW函数是在kernel32.dll中定义的,用来加载DLL文件,它只有一个参数,就是DLL文件的绝对路径名pszLibFileName,(也就是木马DLL的全路径文件名),但是由于木马DLL是在远程进程内调用的,所以我们首先还需要将这个文件名复制到远程地址空间:(否则远程线程是无法读到这个参数的)

 file://计算DLL路径名需要的内存空间
 int cb = (1 + lstrlenW(pszLibFileName)) * sizeof(WCHAR);
 file://使用VirtualAllocEx函数在远程进程的内存地址空间分配DLL文件名缓冲区
 pszLibFileRemote = (PWSTR) VirtualAllocEx( hRemoteProcess, NULL, cb,
            MEM_COMMIT, PAGE_READWRITE);
 file://使用WriteProcessMemory函数将DLL的路径名复制到远程进程的内存空间
 iReturnCode = WriteProcessMemory(hRemoteProcess,
            pszLibFileRemote, (PVOID) pszLibFileName, cb, NULL);
 file://计算LoadLibraryW的入口地址
 PTHREAD_START_ROUTINE pfnStartAddr = (PTHREAD_START_ROUTINE)
     GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "LoadLibraryW");

  万事俱备,我们通过建立远程线程时的地址pfnStartAddr(实际上就是LoadLibraryW的入口地址)和传递的参数pszLibFileRemote(实际上是我们复制过去的木马DLL的全路径文件名)在远程进程内启动我们的木马DLL:

 file://启动远程线程LoadLibraryW,通过远程线程调用用户的DLL文件
 hRemoteThread = CreateRemoteThread( hRemoteProcess, NULL, 0,
                 pfnStartAddr, pszLibFileRemote, 0, NULL);

  至此,远程嵌入顺利完成,为了试验我们的DLL是不是已经正常的在远程线程运行,我编写了以下的测试DLL:

 BOOL APIENTRY DllMain(HANDLE hModule, DWORD reason, LPVOID lpReserved)
   {
    char szProcessId[64] ;
    switch ( reason )
     {
      case DLL_PROCESS_ATTACH:
       {
         file://获取当前进程ID
         _itoa ( GetCurrentProcessId(), szProcessId, 10 );
         MessageBox ( NULL, szProcessId, "RemoteDLL", MB_OK );
       }
      default:
      return TRUE;
     }
   }

  当我使用RmtDll.exe程序将这个TestDLL.dll嵌入Explorer.exe进程后(PID=1208),该测试DLL弹出了1208字样的确认框,同时使用PS工具也能看到

   Process ID: 1208
   C:\WINNT\Explorer.exe (0x00400000)
   ……
   C:\TestDLL.dll (0x100000000)
   ……

  这证明TestDLL.dll已经在Explorer.exe进程内正确地运行了。

  无论是使用特洛伊DLL还是使用远程线程,都是让木马的核心代码运行于别的进程的内存空间,这样不仅能很好地隐藏自己,也能更好的保护自己
kugou123 2004-05-03
  • 打赏
  • 举报
回复
PS是一个查看活动进程ID的工具
MyNameEPC 2004-05-03
  • 打赏
  • 举报
回复
使用CreateReomteThead()函数。
DAY 1 Become familiar with the Visual C++ development envi- ronment by building a simple application. DAY 2 Learn about the stan- dard controls used in Windows applica- tions, how you can place and configure them on an applica- tion window, and how you can interact with them. DAY 3 Learn how you can capture mouse and keyboard events and react to them in your applications. DAY 4 Learn how to work with timers in a Visual C++ applica- tion. Learn how to have two or more timers running at the same time and how you can tell them apart. DAY 5 Learn how you can use some built-in dialogs to ask the user simple questions and how you can build your own cus- tom dialogs to get more detailed infor- mation from the user. DAY 6 Learn how to create menus to add to your application. DAY 7 Learn about the font infrastructure in Windows and how you can access it in your Visual C++ applications. DAY 8 Learn how to draw graphics in a Windows appli- cation. DAY 9 Learn how easy it is to incorporate ActiveX controls into your applications. DAY 10 Learn how to build a basic Single Document Interface (SDI) application and how to use the Document/View architecture. DAY 11 Learn how you can build Multiple Document Interface (MDI) applications. DAY 12 Learn how you can create and modify your own toolbars and status bars. DAY 13 Learn how you can use the structure pro- vided for you by the Document/View architecture to save and restore the data created in your appli- cation. DAY 14 Learn how easy it is to build a database application using an ODBC database. DAY 15 Learn about Microsoft’s latest database access tech- nology, ActiveX Data Objects (ADO), and how you can incor- porate it into your Visual C++ appli- cations. DAY 16 Learn how to build your functionality into library modules that you can give to other Visual C++ programmers for use in their applications. DAY 17 Learn how to build two different types of DLLs. DAY 18 Learn how you can enable your applica- tions to work on two or more separate tasks at the same time. DAY 19 Learn how you can build your own ActiveX controls that can be used in other applications or even in Web pages. DAY 20 Learn how Internet applications commu- nicate with each other using the Winsock interface and how you can make your applica- tions communicate over a network. DAY 21 See how easy it is to incorporate the Microsoft Internet Explorer Web browser into your own Visual C++ application.

15,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧