x7code 发表于 2011-12-26 13:19:13

反调试之DebugObject: NtQueryObject,附上源码

8、 DebugObject: NtQueryObject() 除了识别进程是否被调试之外,其他的调试器检测技术牵涉到检查系统当中是否有调试器正在运行。 逆向论坛中讨论的一个有趣的方法就是检查 DebugObject 10 类型内核对象的数量。这种方法之所以有效是因为每当一个应用程序被调试的时候,将会为调试对话在内核中创建一个 DebugObject 类型的对象。 DebugObject 的数量可以通过 ntdll!NtQueryObject() 检索所有对象类型的信息而获得。 NtQueryObject 接受 5 个参数,为了查询所有的对象类型, ObjectHandle 参数被设为 NULL , ObjectInformationClass 参数设为 ObjectAllTypeInformation(3) : NTSTATUS NTAPI NtQueryObject( HANDLE                                       ObjectHandle, OBJECT_INFORMATION_CLASS      ObjectInformationClass, PVOID                                                ObjectInformation, ULONG                                                Length, PULONG                                              ResultLength ) 这个 API 返回一个 OBJECT_ALL_INFORMATION 结构,其中 NumberOfObjectsTypes 成员为所有的对象类型在 ObjectTypeInformation 数组中的计数: typedef struct _OBJECT_ALL_INFORMATION{ ULONG                                                NumberOfObjectsTypes; OBJECT_TYPE_INFORMATION            ObjectTypeInformation; } 检测例程将遍历拥有如下结构的 ObjectTypeInformation 数组: typedef struct _OBJECT_TYPE_INFORMATION{ UNICODE_STRING      TypeName; ULONG                     TotalNumberofHandles; ULONG                     TotalNumberofObjects; ...more fields... } TypeName 成员与 UNICODE 字符串 "DebugObject" 比较,然后检查 TotalNumberofObjects 或 TotalNumberofHandles 是否为非 0 值。 对策 与 NtQueryInformationProcess() 解决方法类似,在 NtQueryObject() 返回处设断点,然后补丁 返回的 OBJECT_ALL_INFORMATION 结构,另外 NumberOfObjectsTypes 成员可以置为 0 以防止壳遍历 ObjectTypeInformation 数组。可以通过创建一个类似于 NtQueryInformationProcess() 解决方法的 ollyscript 脚本来执行这个操作。 类似地, Olly Advanced 插件向 NtQueryObject() API 中注入代码,如果检索的是 ObjectAllTypeInformation 类型则用 0 清空整个返回的缓冲区。

以下是VC 6.0源码
typedef long NTSTATUS;
typedef enum _POOL_TYPE {
NonPagedPool,
PagedPool,
NonPagedPoolMustSucceed,
DontUseThisType,
NonPagedPoolCacheAligned,
PagedPoolCacheAligned,
NonPagedPoolCacheAlignedMustS
} POOL_TYPE;

#define ObjectAllTypeInformation 3
typedef NTSTATUS (WINAPI *NTQUERYOBJECT)(HANDLE ObjectHandle,
         long ObjectInformationClass,
         PVOID ObjectInformation,
         ULONG Length,
         PULONG ReusltLength
         );
#pragma pack(push)
#pragma pack(1)
typedef struct _UNICODE_STRING {
USHORTLength;
USHORTMaximumLength;
PWSTRBuffer;
} UNICODE_STRING, *PUNICODE_STRING;
typedef struct _OBJECT_TYPE_INFORMATION
{
UNICODE_STRING          TypeName;//判断TypeName是否等于"DebugObject"
ULONG                   TotalNumberOfHandles; //判断TotalNumberOfHandles 是否大于0
ULONG                   TotalNumberOfObjects;//判断 TotalNumberOfObjects 是否大于0
WCHAR                   Unused1;
ULONG                   HighWaterNumberOfHandles;
ULONG                   HighWaterNumberOfObjects;
WCHAR                   Unused2;
ACCESS_MASK             InvalidAttributes;
GENERIC_MAPPING         GenericMapping;
ACCESS_MASK             ValidAttributes;
BOOLEAN               SecurityRequired;
BOOLEAN               MaintainHandleCount;
USHORT                  MaintainTypeList;
POOL_TYPE               PoolType;
ULONG                   DefaultPagedPoolCharge;
ULONG                   DefaultNonPagedPoolCharge;
BYTE                  Unknown2;
} OBJECT_TYPE_INFORMATION, *POBJECT_TYPE_INFORMATION;
typedef struct _OBJECT_ALL_INFORMATION
{
ULONG                   NumberOfObjectsTypes;
OBJECT_TYPE_INFORMATION ObjectTypeInformation;
} OBJECT_ALL_INFORMATION, *POBJECT_ALL_INFORMATION;
#pragma pack(pop)
int main(int argc, char* argv[])
{
ULONG ulngReusltLength;
HMODULE ntHandle=::LoadLibrary("NTDLL.DLL");
NTQUERYOBJECT NtQueryObject=(NTQUERYOBJECT)::GetProcAddress(ntHandle,"NtQueryObject");
if (NtQueryObject)//判断是否获取了函数地址
{
NtQueryObject(NULL,
   ObjectAllTypeInformation,
   NULL,
   NULL,
   &ulngReusltLength);//第一次调用获取需要分配的空间
POBJECT_ALL_INFORMATION pOAI=(POBJECT_ALL_INFORMATION)
   malloc(ulngReusltLength);//分配一个堆保存信息
NtQueryObject(NULL,
   ObjectAllTypeInformation,
   pOAI,
   ulngReusltLength,
   NULL);//第二次调用获取数据
PUNICODE_STRING pUS=(PUNICODE_STRING)&pOAI->ObjectTypeInformation->TypeName;
printf("%d,%d\n",pOAI->ObjectTypeInformation->TotalNumberOfHandles,
    pOAI->ObjectTypeInformation->TotalNumberOfObjects);
for (ULONG i=0;i<pOAI->NumberOfObjectsTypes;++i)
{
   
   if (wcscmp(pUS->Buffer,L"DebugObject")==0){printf("waring!Debuging!!!\n"); // //这里只是判断名称
                                                                                          // ,还需要判断TotalNumberOfHandles 是否大于0
                                                                                          // 和 判断TotalNumberOfObjects 是否大于0   
    //break;
   }
   printf("%ws \n",pUS->Buffer);
   pUS=(PUNICODE_STRING)((DWORD)pUS->Buffer+pUS->Length);
   //下面两个代码是以4字节对齐方式
   pUS=(PUNICODE_STRING) (((DWORD)pUS) & -4);
   pUS=(PUNICODE_STRING)((DWORD)pUS+4);
}
if (pOAI)
   free(pOAI);
}
FreeLibrary(ntHandle);//释放库
system("pause");
return 0;
}

汇编源码
FD_DebugObject_NtQueryObject:
FD_DebugObject_NtQueryObject_Arg_Win32Api      equ 04h   
mov eax, dword ptr             
push ebx   
push ecx   
push edx   
push edi   
push esi      
mov ebx, eax   
assume ebx : ptr WIN32APIBASE   
push edx    ; alloc the stack分配一个4个字节临时变量栈      
push esp    ; ReturnLength    返回的长度
push 0   
push 0   
push ObjectAllTypeInformation    ;查询的类型信息
push 0   
call dword ptr .xNtQueryObject   ;调用函数
pop ecx    ;; make a tmp stack ecx此时获得返回的长度   
push ebp   
mov ebp, esp   
sub esp, ecx    ;申请一个栈空间
mov esi, esp    ;; ObjectInformationLength   
push 0   
push ecx    ;长度
push esi    ;地址,此时指向OBJECT_ALL_INFORMATION
push ObjectAllTypeInformation   
push 0   
call dword ptr .xNtQueryObject;继续调用NtQueryObject
cld    ;; NumberOfObjectsTypes设置方向标志位使得串指令操作时寄存器递增
lodsd    ;执行lodsd后 eax获取了,esi+4,eax==NumberOfObjectsTypes,esi指向ObjectTypeInformation
xchg ecx, eax ; ecx = NumberOfObjectsTypes,FD_DebugObject_NtQueryObject_Loop这里代表循环的次数
FD_DebugObject_NtQueryObject_Loop:      
;; load string lengths   
lodsd    ;执行lodsd后eax获取了,esi+4,esi指向Buffer地址,eax=MaximumLength+Length
movzx edx, ax ;此时edx等于Length   
;; pointer to TypeName   
lodsd    ;执行lodsd后eax=,esi+4,eax等于Buffer地址,esi=TotalNumberOfHandles
xchg esi, eax    ;寄存器交换esi=buffer ,eax=TotalNumberOfHandles的地址
;; sizeof(L"DebugObject")   
;; avoids superstrings   
;; like "DebugObjective"   
cmp edx, 16h    ;判断edx的长度是否等于22个字节
jnz FD_DebugObject_NtQueryObject_Tmp2   
xchg ecx, edx    ;ecx字符串的比较长度
FD_DebugObject_NtQueryObject_Tmp1:   
call FD_DebugObject_NtQueryObject_UnicodeStr1      
dw 'D','e','b','u','g'      
dw 'O','b','j','e','c','t'   
FD_DebugObject_NtQueryObject_UnicodeStr1:   
pop edi    ;edi此时的地址指向DebugObject的地址
repe cmpsb ;esi和edi进行循环比较   
xchg ecx, edx   
jnz FD_DebugObject_NtQueryObject_Tmp2   
;; TotalNumberOfObjects   
cmp dword ptr , edx   
jnz Found_FD_DebugObject_NtQueryObject   
;; point to trailingnull   
FD_DebugObject_NtQueryObject_Tmp2:   
add esi, edx    ;; round down to dword   
and esi, -4    ;; skip trailing null   
;; and any alignment bytes   
lodsd   
loop FD_DebugObject_NtQueryObject_Loop   
xor eax, eax
Exit_FD_DebugObject_NtQueryObject:   
;; clear the tmp stack   
mov esp, ebp   
pop ebp      
pop esi   
pop edi   
pop edx   
pop ecx   
pop ebx   
assume ebx : nothing      
retn 04hFound_FD_DebugObject_NtQueryObject:      
mov eax, 1   
jmp Exit_FD_DebugObject_NtQueryObjectEnd_FD_DebugObject_NtQueryObject

xhjzgsdzylj 发表于 2011-12-30 00:32:23

破解必学啊,但是我是学不会啦

艾楠京 发表于 2012-1-2 11:31:16

破解必学啊

alanage 发表于 2012-1-2 13:57:19

好资源 收藏了
页: [1]
查看完整版本: 反调试之DebugObject: NtQueryObject,附上源码