2013年4月28日 星期日

SSDT_Hook_Bypass_HackShield_OP

/*--------------------------------------------------------------
* 文件名稱 : Driver.c                                                 
* 作  者 : Vinurpe
* 創建日期 : 2013-03-02
* 編譯環境 : WDK 7600.16385.1
/---------------------------------------------------------------
* File Name            : Driver.c
* Date of creation     : 2013-03-02
* Author(s)            : Vinurpe
* Building environment : WDK 7600.16385.1
----------------------------------------------------------------*/
#include <ntddk.h>

//獲得由ntoskrnl.exe導出函數,以Zw*開頭函數的地址,這個函數的返回值就是Nt*函數,Nt*函數的地址就在SSDT中
#define SYSTEMSERVICE(_func) KeServiceDescriptorTable.ServiceTableBase[ *(PULONG)((PUCHAR)_func+1)]
//獲得Zw*函數的地址並返回與之通信的函數在SSDT中的索引。
#define SYSCALL_INDEX(_Function) *(PULONG)((PUCHAR)_Function+1)

typedef struct ServiceDescriptorEntry
{
        unsigned int *ServiceTableBase;
        unsigned int *ServiceCounterTableBase; //Used only in checked build
        unsigned int NumberOfServices;
        unsigned char *ParamTableBase;
} SSDTEntry;
__declspec(dllimport)  SSDTEntry KeServiceDescriptorTable;

typedef NTSTATUS (*ZWOPENPROCESS)(OUT PHANDLE ProcessHandle, 
 IN ACCESS_MASK DesiredAccess, 
 IN POBJECT_ATTRIBUTES ObjectAttributes, 
 IN PCLIENT_ID ClientId OPTIONAL);
ZWOPENPROCESS NtOpenProcess_Addr;

UCHAR NtOpenProcess_Win7_Org_Mem[6] = {0x00,0x00,0x00,0x00,0x00,0x00};

ULONG NewOpenProcess_Win7_Hook_Addr;
UCHAR NewOpenProcess_Win7_Hook_Mem[6] = {0xe9,0x00,0x00,0x00,0x00,0x90};
ULONG NewOpenProcess_Win7_Hook_Call;
ULONG NewOpenProcess_Win7_Hook_Jmp;

ULONG NewOpenProcess_Win7_JmpCalc;

///////////////////////////////////////////////////
VOID OnUnload(IN PDRIVER_OBJECT DriverObject);
///////////////////////////////////////////////////

VOID WPOFF()
{
 __asm
 {
 cli
 mov eax,cr0
 and eax,not 10000h
 mov cr0,eax
 }
}

VOID WPON()
{
 __asm
 {
 mov eax,cr0
 or eax,10000h
 mov cr0,eax
 sti
 }
}

__declspec(naked) NTSTATUS __stdcall NewOpenProcess_Win7() 
{
 __asm
 {
 _EMIT 0x90;
 _EMIT 0x90;
 _EMIT 0x90;
 _EMIT 0x90;
 _EMIT 0x90;
 _EMIT 0x90;
 }
 __asm  
 {
 call NewOpenProcess_Win7_Hook_Call
 jmp  [NewOpenProcess_Win7_Hook_Jmp]
 }
}

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,PUNICODE_STRING RegistryPath)
{
 DriverObject->DriverUnload = OnUnload;
 NtOpenProcess_Addr = (ZWOPENPROCESS)(SYSTEMSERVICE(ZwOpenProcess)); //非硬編碼,不過這種方法只可以用在DDK有導出的函數... 

//因 Win7 Ntop結構與 WinXp 不同所以只能針對ASM寫...地址偏移部分如要更嚴謹請使用反彙編引擎來計算
//----------------------------------------------------------------
 NewOpenProcess_Win7_Hook_Addr = (ULONG)NtOpenProcess_Addr + 0x22;
 NewOpenProcess_Win7_Hook_Jmp =  (ULONG)NtOpenProcess_Addr + 0x2D;

 //Call轉換
 NewOpenProcess_Win7_Hook_Call = (ULONG)NtOpenProcess_Addr + 0x29;
 NewOpenProcess_Win7_Hook_Call = 0xFFFFFFFF - *(PULONG)(NewOpenProcess_Win7_Hook_Call);  
 NewOpenProcess_Win7_Hook_Call = NewOpenProcess_Win7_Hook_Jmp - NewOpenProcess_Win7_Hook_Call - 0x1; 

 //Jmp轉換
 NewOpenProcess_Win7_JmpCalc = (PCHAR)NewOpenProcess_Win7- (PCHAR)NewOpenProcess_Win7_Hook_Addr - 5;
 RtlCopyMemory(NewOpenProcess_Win7_Hook_Mem + 1,&NewOpenProcess_Win7_JmpCalc,4);

 WPOFF();
 RtlCopyMemory (NtOpenProcess_Win7_Org_Mem, (PVOID)NewOpenProcess_Win7_Hook_Addr , 6);
 RtlCopyMemory((PVOID)NewOpenProcess_Win7,NtOpenProcess_Win7_Org_Mem,6); 
 RtlCopyMemory((PVOID)NewOpenProcess_Win7_Hook_Addr ,(PVOID)NewOpenProcess_Win7_Hook_Mem,6);
 WPON();
//----------------------------------------------------------------
 return STATUS_SUCCESS;
}
/////////////////////////////////////////////////////
VOID OnUnload(IN PDRIVER_OBJECT DriverObject)
{
 WPOFF();
 RtlCopyMemory((PVOID)NewOpenProcess_Win7_Hook_Addr,NtOpenProcess_Win7_Org_Mem,6);
 WPON(); 
 DbgPrint("Driver UnLoad!\n");
}
/////////////////////////////////////////////////////

首先,各路大神抱歉小弟獻醜了 <(_ _)>
想了很久終於OpenSource了
裡面的概念幾乎都是前幾篇筆記有提到的
當然如果想完整Bypass HackShield 並使用Debug的話 前面還有很多難關需要過
以過來人經驗建議直接學習內核重載技術比較省事

2013年4月20日 星期六

[Release] - Win7 GetPsSuspendThreadAddr

ULONG  GetPsSuspendThread(IN ULONG NtSuspendThread_Index)
{
UCHAR *cPtr, *pOpcode;
ULONG Length, CallCount=0;
ULONG uNtSuspendThread=0;
ULONG uPsSuspendThread=0;

uNtSuspendThread=GetFuncAddress_Index(NtSuspendThread_Index);
if (uNtSuspendThread==0) return;
for (cPtr = (PUCHAR)uNtSuspendThread; cPtr < (PUCHAR)uNtSuspendThread + PAGE_SIZE; cPtr += Length) 
{
  Length = SizeOfCode(cPtr, &pOpcode);
  if (!Length) return; 
  if (*pOpcode == 0xE8)
  {
   CallCount=CallCount+1;
   if (CallCount==3)
   {
    uPsSuspendThread=(*(PULONG)(pOpcode+1)+(ULONG)cPtr + 5);
    return uPsSuspendThread;
   }
  }
}
}

[Release] - Win7 GetKiAttachProcessAddr

ULONG GetKiAttachProcess()
{
UCHAR *cPtr, *pOpcode;
ULONG Length, CallCount = 0;
ULONG uKeAttachProcess = 0;
ULONG uKiAttachProcess = 0;

uKeAttachProcess = GetFuncAddr_FuncName(L"KeAttachProcess");
if (uKeAttachProcess == 0) return;
for (cPtr = (PUCHAR)uKeAttachProcess; cPtr < (PUCHAR)uKeAttachProcess + PAGE_SIZE; cPtr += Length) 
{
  Length = SizeOfCode(cPtr, &pOpcode);
  if (!Length) return; 
  if (*pOpcode == 0xE8)
  {
   CallCount=CallCount+1;
   if (CallCount==2)
   {
    uKiAttachProcess = (*(PULONG)(pOpcode+1)+(ULONG)cPtr + 5);
    return uKiAttachProcess;
   }
  }
}
}

2013年3月8日 星期五

[Driver] - Bypass HackShield Memory Protect Driver

FileName:HsDriverBypass.sys
Auther:Secret.. Only can say he is one of my frineds
Description:It can Bypass NtOpenprocess、NtWrite、Read、ProtecVirtualMemory
Download Link:Click me
P.s.:Only supports 32Bit operating system (Win7,Xp)

2013年3月3日 星期日

[Note] - SSDT Hook 筆記 (三)

有在玩對抗HackShield_Driver的朋友一定知道:
HackShield是對nt!_SEH_prolog進行Hook(Call Hook)
來繞到自己函數進行調用對象審查(非白名單就 Return False)

今天就來公布其中一種最簡單的取得nt!_SEH_prolog的方法吧
(本來不想公布的,可是發現已經有人發出來了)

我們以NtProtectVirtualMemory來作範例(Windows7 x86 環境下)

Original:
84490403 6A 38 push   38
84490405 68 485A2A84 push   842A5A48

8449040A E8 99B7E3FF call   842CBBA8  ->  Original
8449040F FF75 14 push   dword ptr [ebp+14]
84490412 E8 CAE9E3FF call   842CEDE1
84490417 83F8 FF cmp    eax, FFFFFFFF
8449041A 75 0A jne    84490426
8449041C B8 450000C0 mov    eax, 0C0000045

...

After HS Hook:
84490403 6A 38 push   38
84490405 68 485A2A84 push   842A5A48

8449040A E8 09E28702 call   86D0E618  -> After HS Hook
8449040F FF75 14 push   dword ptr [ebp+14]
84490412 E8 CAE9E3FF call   842CEDE1
84490417 83F8 FF cmp    eax, FFFFFFFF
8449041A 75 0A jne    84490426
8449041C B8 450000C0 mov    eax, 0C0000045

...

Follow into 86D0E618:
86D0E618 E9 63BE302A jmp    B101A480 -> HS Thread
86D0E61D 90 nop
86D0E61E 90 nop
86D0E61F 90 nop
86D0E620 E9 83D55BFD jmp    842CBBA8 -> Original
86D0E625 90 nop
86D0E626 90 nop
86D0E627 90 nop

...

看到了嗎?! 


2013年3月1日 星期五

[Note] - SSDT Hook 筆記 (二)

上一篇是在Ring3 用工具取得服務號 但每個系統的服務號(硬編碼)不可能都相同
所以這篇教大家在Driver中動態取得函數服務號和SSDT函數位子

in ring0:
lkd> u nt!ZwOpenProcess
nt!ZwOpenProcess:
804de044 b87a000000 mov eax,7Ah  ---> 這就是服務號
804de049 8d542404 lea edx,[esp+4]
804de04d 9c pushfd
804de04e 6a08 push 8
804de050 e8dc150000 call nt!KiSystemService (804df631)
804de055 c21000 ret 10h


lkd> u nt!NtOpenProcess
nt!NtOpenProcess:
80573d06 68c4000000 push 0C4h
80573d0b 6810b44e80 push offset nt!ObWatchHandles+0x25c (804eb410)
80573d10 e826f7f6ff call nt!_SEH_prolog (804e343b)
80573d15 33f6 xor esi,esi
80573d17 8975d4 mov dword ptr [ebp-2Ch],esi
80573d1a 33c0 xor eax,eax
80573d1c 8d7dd8 lea edi,[ebp-28h]
80573d1f ab stos dword ptr es:[edi]

可以看得出,ZwOpenProcess函数很短,首先把0x7a(NtOpenProcess的服务号)存入eax,然后做一些保存现场的工作即KiSystemService——这个函数根据eax中的service id在SSDT中查找相应的系统服务,然后调用之。


所以我們在驅動中常會使用

#define GetIndex(_Function) *(PULONG)((PUCHAR)_Function+1)
來取得函數的服務號 (注意_Function需填入Zw開頭函數而非Nt)

甚至更進階一點讓我們直接獲得函數位子
#define GetSystemFunc(FuncName) KeServiceDescriptorTable.ServiceTableBase[*(PULONG)((PUCHAR)FuncName+1)]


2013年2月27日 星期三

[Note] - SSDT Hook 筆記 (一)

測試系統:Windows7 x86 Ultimate

SSDT即系统服务描述符表,它的结构如下(参考《Undocument Windows 2000 Secretes》第二章):
     typedef struct _SYSTEM_SERVICE_TABLE
     {
       PVOID   ServiceTableBase;        //这个指向系统服务函数地址表
       PULONG  ServiceCounterTableBase;
       ULONG   NumberOfService;         //服务函数的个数,NumberOfService*4 就是整个地址表的大小
       ULONG   ParamTableBase;
     }SYSTEM_SERVICE_TABLE,*PSYSTEM_SERVICE_TABLE; 
//轉至http://bbs.pediy.com/showthread.php?t=40832

//0xD7为NtProtectVirtualMemory服务ID
Address = (ULONG)KeServiceDescriptorTable->ServiceTableBase + 0xD7 * 4;

服务ID 取得:
用CheatEngine 6.2 查看NtProtectVirtualMemory函數


ntdll.NtProtectVirtualMemory - B8 D7000000           - mov eax,000000D7 -> 這就是 服務ID
ntdll.ZwProtectVirtualMemory+5- BA 0003FE7F           - mov edx,7FFE0300 : [ntdll.KiFastSystemCall]
ntdll.ZwProtectVirtualMemory+A- FF 12                 - call dword ptr [edx]
ntdll.ZwProtectVirtualMemory+C- C2 1400               - ret 0014
ntdll.ZwProtectVirtualMemory+F- 90                    - nop 

在看ntdll.KiFastSystemCall:


ntdll.KiFastSystemCall - 8B D4                 - mov edx,esp
ntdll.KiFastSystemCall+2- 0F34                  - sysenter -> 由此進入內核 (Ring0層)
ntdll.KiFastSystemCallRet- C3                    - ret 

讓我們進入內核層看HS到底是怎樣Hook的
8425C539 -> NtProtectVirtualMemory原始地址

原始:
8425C539   6A 38           push   38
8425C53B   68 28 20 07 84  push   84072028
8425C540   E8 A3 BA E3 FF  call   84097FE8 -> Org_Call
8425C545   FF 75 14   push   dword ptr [ebp+14]
.....
HS 啟動後:
8425C539   6A 38   push   38
8425C53B   68 28 20 07 84  push   84072028
8425C540   E8 9B 73 95 02  call   86BB38E0 -> Fake_Call
8425C545   FF 75 14        push   dword ptr [ebp+14]
.....
由此可知:
這函數是 Call Inline Hook 而非 Head Inilne Hook (Win7的 NtClose 是 Head Inilne Hook)

破解思路:
直接恢復函數原始Call位子記憶體雖然可以Pass過沒多久又會被HS改回去所以:
在8425C53B設立跳轉點,自己建立一段ASM讓他使用Org_Call而非Fake_Call ,在跳回8425C545.

但此版的HS客戶端主程式會調用原函數以確定是否"真的不能使用"
單純的跳轉過法已經過不了(會跳Callback 0x10301錯誤)
必須把客戶端加入調用黑名單"只"讓他依舊無法使用
.