Trao đổi với tôi

http://www.buidao.com

8/7/09

[Reverse] Taskkill.exe tắt process bằng API nào

Taskkill.exe tắt process bằng API nào

Link: http://virusvn.com/forum/printthread.php?t=1569&pp=40


gianghoplus 08-02-2009 03:59 AM

Taskkill.exe tắt process bằng API nào ?
Không biết là Taskkill.exe của Windows xài API nào để kill process nhỉ. Xem import ở PE thì thấy có TerminateProcess nhưng thử chặn 2 hàm TerminateProcess và EndTask rồi mà vẫn không có kết quả. Giả thuyết cuối của gianghoplus là nó dùng WMI. Mình đang bí. Các bác có cao kiến gì không ?





TQN 08-02-2009 01:01 PM

Trên máy tui, search *kill*.exe ra 1 đống *kill.exe:
1. kill.exe của WinDbg
2. pskill.exe của SysInternal
3. tskill.exe của MS
4. taskkill.exe của MS

Trong đống trên, pskill là phức tạp nhất, taskkill của MS đứng nhì. Nhưng RE taskkill.exe thì dể hơn vì ta có thể lấy pdb symbol file của nó.

Taskkill.exe được viết = VC++ 2003, viết theo OOP. Chủ đạo của taskkill là class CTaskKill, làm tất tần tật, parse cmd line, kill, kết nối remote machine...

Trên local machine, CTaskKill có 2 method private:
int __thiscall CTaskKill::KillProcessOnLocalSystem(int &)
int __thiscall CTaskKill::ForciblyKillProcessOnLocalSystem(void)

Thông số /F sẽ gọi hàm Forciblyxxx, còn không thì gọi KillProcessxxx

Hàm KillProcessOnLocalSystem chỉ dùng PostMessage với WM_CLOSE tới window của PID hay IM chỉ ra
Hàm ForciblyKillProcessOnLocalSystem thì dùng OpenProcess với param PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, kiểm tra status của process với hàm API GetExitCodeProcess. Nếu process còn STILL_ACTIVE thì call API TerminateProcess.

Code:

.text:01003447 private: int __thiscall CTaskKill::ForciblyKillProcessOnLocalSystem(void) proc near
.text:01003447 ; CODE XREF: CTaskKill::Kill(int &):loc_100476Dp
.text:01003447
.text:01003447 ExitCode= dword ptr -4
.text:01003447
.text:01003447 push ebp
.text:01003448 mov ebp, esp
.text:0100344A push ecx
.text:0100344B and [ebp+ExitCode], 0
.text:0100344F push esi
.text:01003450 push dword ptr [ecx+58h] ; dwProcessId
.text:01003453 push 0 ; bInheritHandle
.text:01003455 push 401h ; dwDesiredAccess
.text:0100345A call ds:OpenProcess(x,x,x) ; Opens an existing process object.
.text:0100345A
.text:01003460 mov esi, eax
.text:01003462 test esi, esi
.text:01003464 jz short @@TerminateProcess_Failed
.text:01003464
.text:01003466 lea eax, [ebp+ExitCode]
.text:01003469 push eax ; lpExitCode
.text:0100346A push esi ; hProcess
.text:0100346B call ds:GetExitCodeProcess(x,x) ; Retrieves the termination status of the specified process.
.text:0100346B
.text:01003471 test eax, eax
.text:01003473 jnz short @@ProcessStillActive
.text:01003473
.text:01003475 push esi ; hObject
.text:01003476 call ds:CloseHandle(x) ; Closes an open object handle
.text:01003476
.text:0100347C jmp short @@Return_FALSE
.text:0100347C
.text:0100347E ; ---------------------------------------------------------------------------
.text:0100347E
.text:0100347E @@ProcessStillActive: ; CODE XREF: CTaskKill::ForciblyKillProcessOnLocalSystem(void)+2Cj
.text:0100347E cmp [ebp+ExitCode], STILL_ACTIVE
.text:01003485 jz short @@TerminateProcess
.text:01003485
.text:01003487 push esi ; hObject
.text:01003488 call ds:CloseHandle(x) ; Closes an open object handle
.text:01003488
.text:0100348E push SCHED_E_TASK_NOT_RUNNING
.text:01003493 jmp short @@SetComLastError
.text:01003493
.text:01003495 ; ---------------------------------------------------------------------------
.text:01003495
.text:01003495 @@TerminateProcess: ; CODE XREF: CTaskKill::ForciblyKillProcessOnLocalSystem(void)+3Ej
.text:01003495 push 1 ; uExitCode
.text:01003497 push esi ; hProcess
.text:01003498 call ds:TerminateProcess(x,x) ; Terminates the specified process and all of its threads.
.text:01003498
.text:0100349E test eax, eax
.text:010034A0 push esi ; hObject
.text:010034A1 jnz short @@Return_TRUE
.text:010034A1
.text:010034A3 call ds:CloseHandle(x) ; Closes an open object handle
.text:010034A3
.text:010034A9
.text:010034A9 @@TerminateProcess_Failed: ; CODE XREF: CTaskKill::ForciblyKillProcessOnLocalSystem(void)+1Dj
.text:010034A9 call ds:GetLastError() ; Retrieves the calling thread's last-error code value.
.text:010034A9 ; The last-error code is maintained on a per-thread basis.
.text:010034A9
.text:010034AF cmp eax, ERROR_INVALID_PARAMETER
.text:010034B2 jnz short @@Return_FALSE
.text:010034B2
.text:010034B4 push CO_E_NOT_SUPPORTED ; dwErrCode
.text:010034B4
.text:010034B9
.text:010034B9 @@SetComLastError: ; CODE XREF: CTaskKill::ForciblyKillProcessOnLocalSystem(void)+4Cj
.text:010034B9 call ds:SetLastError(x) ; Sets the last-error code for the calling thread.
.text:010034B9
.text:010034BF
.text:010034BF @@Return_FALSE: ; CODE XREF: CTaskKill::ForciblyKillProcessOnLocalSystem(void)+35j
.text:010034BF ; CTaskKill::ForciblyKillProcessOnLocalSystem(void)+6Bj
.text:010034BF call SaveLastError()
.text:010034BF
.text:010034C4 xor eax, eax
.text:010034C6 jmp short @@Return
.text:010034C6
.text:010034C8 ; ---------------------------------------------------------------------------
.text:010034C8
.text:010034C8 @@Return_TRUE: ; CODE XREF: CTaskKill::ForciblyKillProcessOnLocalSystem(void)+5Aj
.text:010034C8 call ds:CloseHandle(x) ; Closes an open object handle
.text:010034C8
.text:010034CE xor eax, eax
.text:010034D0 inc eax
.text:010034D0
.text:010034D1
.text:010034D1 @@Return: ; CODE XREF: CTaskKill::ForciblyKillProcessOnLocalSystem(void)+7Fj
.text:010034D1 pop esi
.text:010034D2 leave
.text:010034D3 retn
.text:010034D3
.text:010034D3 private: int __thiscall CTaskKill::ForciblyKillProcessOnLocalSystem(void) endp
.text:010034D3

CTaskKill trước gọi method Kill để kill PID/IM ta chỉ ra trong command line. Trong method Kill, nó gọi method CanTerminate. CanTerminate return FALSE nếu PID/IM của ta chỉ ra là 1 trong 4 system process sau: csrss.exe, smss.exe, services.exe, winlogon.exe.

Với remote machine, Kill method call private method: int __thiscall CTaskKill::ForciblyKillProcessOnRemoteSystem(void) . Method này dùng WMI command để kill.

Cpro 08-02-2009 01:08 PM



cho em hỏi nhỏ làm sao bác biết được tên của hàm,class của source thế

TQN 08-02-2009 01:15 PM

Dùng symchk.exe của WinDbg để lấy taskkill.pdb. File pdb này là public symbol.
Apply taskkill.pdb vào IDA và WinDbg/OllyDbg.

TQN 08-04-2009 04:30 AM

Với các process có window, cho dù có visible hay không, taskkill vẫn kill được dùng PostMessage(WM_CLOSE,....), còn nếu không có window, taskkill sẽ print ra error và yêu cầu chúng ta dùng thông số /F (Force kill).