线程注入

来源:互联网 发布:sql报表开发教程 编辑:程序博客网 时间:2024/05/09 03:19
运行关键是隐藏,神不知鬼不觉才是王道.要隐藏,先要隐藏进程,Windows操作系统中程序以进程的
方式运行,大多数操作系统也是如此.任务管理器就可以看到当前运行的进程,所以有人HOOK相关枚举进程的函
数,让任务管理器不显示 进程,也有人把自己的 注册成服务运行,"任务管理器"不显示服务的.这样做只
是障眼法,进程还是存在的,最好的方法是让进程不存在,让 作为其他进程的一个线程来运行.Windows操
作系统提出了DLL的概念,其系统API都是通过DLL的形式出现的,应用程序动态链接到DLL来调用API,DLL在
内存中只存在一个副本就可以满足不同应用程序的调用了,因此可以把 写成DLL文件,让他作为进程的一部
分运行,最好是系统进程的一部分,一般人很难看到一个进程加载了哪些DLL,也就很难发现这种 (用
IceSword可以看到进程的DLL模块).
一  编写一个DLL :
    使用IDE : Visual C++ 6.0 Visual Studio.NET 2003/2005都可以.
    首先建立一个Win32 Dynamic-Link Library工程.选择 A simple DLL project建立工程,然后就会看到:
BOOL APIENTRY DllMain( HANDLE hModule,             // 模块句柄.
                       DWORD  ul_reason_for_call,  // 调用标志.
                       LPVOID lpReserved           // 返回数据.
                     )
{
    return TRUE;
}
这个是DLL的入口点函数,只能做一些简单的初始化工作.这个函数和WinMain wWinMain _tWinMain main这四个标准的入口点函数是完全不同的,DllMain会被多次调用,上述四个入口点只被系统调用一次.顺便说一句dll文件结构和exe文件是完全一致的.
DWORD  ul_reason_for_call,  // 调用标志.
这个参数由系统传递,用于判断调用DllMain函数时候的状态.可能是以下四个常量:
DLL_PROCESS_ATTACH:     DLL被进程第一次使用时,就是进程调用LoadLibrary函数时
DLL_THREAD_ATTACH:
DLL_THREAD_DETACH:
DLL_PROCESS_DETACH:     DLL被释放的时候
MSDN原文:
DLL_PROCESS_ATTACH
   The DLL is being loaded into the virtual address space of the current process as a result of the process starting up or as a result of a call to LoadLibrary. DLLs can use this opportunity to initialize any instance data or to use the TlsAlloc function to allocate a thread local storage (TLS) index.
DLL_THREAD_ATTACH
   The current process is creating a new thread. When this occurs, the system calls the entry-point function of all DLLs currently attached to the process. The call is made in the context of the new thread. DLLs can use this opportunity to initialize a TLS slot for the thread. A thread calling the DLL entry-point function with DLL_PROCESS_ATTACH does not call the DLL entry-point function with DLL_THREAD_ATTACH.
Note that a DLL's entry-point function is called with this value only by threads created after the DLL is loaded by the process. When a DLL is loaded using LoadLibrary, existing threads do not call the entry-point function of the newly loaded DLL.
 
DLL_THREAD_DETACH
   A thread is exiting cleanly. If the DLL has stored a pointer to allocated memory in a TLS slot, it should use this opportunity to free the memory. The system calls the entry-point function of all currently loaded DLLs with this value. The call is made in the context of the exiting thread.
DLL_PROCESS_DETACH
   The DLL is being unloaded from the virtual address space of the calling process as a result of unsuccessfully loading the DLL, termination of the process, or a call to FreeLibrary. The DLL can use this opportunity to call the TlsFree function to free any TLS indices allocated by using TlsAlloc and to free any thread local data.
Note that the thread that receives the DLL_PROCESS_DETACH notification is not necessarily the same thread that received the DLL_PROCESS_ATTACH notification.
判断:当 ul_reason_for_call 等于 DLL_PROCESS_ATTACH 时就新开一个线程启动.记住一定要新开一个线程,不能把DllMain当main函数用.

二  DLL的启动:

    远程进程插入启动 :将 DLL插入运行中的进程空间中,让其运行.

    具体做法是先将系统权限提升到DEBUG模式下,因为只有DEBUG模式才能打开进程句柄.然后用OpenProcess函数远程以PROCESS_Create_THREAD , PROCESS_VM_OPERATION , PROCESS_VM_WRITE
的权限打开要插入的进程,得到进程的句柄.用VirtualAllocEx函数给DLL文件的路径分配内存空间.
用WriteProcessMemory函数将DLL文件内容写入进程空间中.用CreateRemoteThread函数启动就完成
了进程的远程插入.