PowerPoint Presentation

Similar documents
untitled

untitled

移民資料

untitled

台灣經濟新報資料庫

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

微處理機實習期末專題

untitled

第一章 簡介

untitled

I/O Files讀寫檔案:

untitled

概述

PowerPoint 簡報

untitled

Contents

Powerpoint 2003

中華民國第45屆中小學科學展覽會

Contents

錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更

4-04 論文封面(樣式)

untitled

untitled

untitled

untitled

C++ 程式設計

個人教室 / 網路硬碟

ebook

untitled

untitled

人身保險業務員資格測驗方案

untitled

untitled

untitled

依據教育部八十九年 月 日臺(八九)技(二)字第 號函

九十三年第三期檔案管理工作研習營學員建議事項答覆情形彙整表

untitled

朝 陽 科 技 大 學

中華人民共和國殘疾人保障法(2008年修訂)

untitled

untitled

untitled

untitled

untitled

untitled

untitled

C 1

untitled

MATLAB介紹

untitled

untitled

untitled

了 立 連 立 量 領 來 例 蘭 便 不 數 不 論 更 更 更 力 更 參 例 來 例 見 量 度 量 量 參 論 量 行 量 量 瑩 理 來 錄 量 量 不 力 省 力 立 力 量 量 量 了 量 便 錄 錄 錄 料 說 省 6

untitled

untitled

untitled

untitled

第五章 鄉鎮圖書館閱讀推廣活動之分析

untitled

untitled

untitled

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

地方公共服務績效比較評量之探討—標竿學習策略的觀點

untitled

untitled

FY.DOC

untitled

untitled

自由軟體社群發展經驗與 Linux認證介紹

ARBURG Qualitätssicherung AQS 4.0

行政院國科會九十一年度專題研究

國立陽明大學輻射防護計畫書

第五章 實例個案

untitled

untitled

國立自然科學博物館館訊第263期

untitled

untitled

untitled

untitled

公立學校教職員成績考核辦法修正草案總說明

untitled

兼營營業人營業稅額 計算辦法及申報實務

untitled

untitled

untitled

大陸黨報集團化發展之研究

Microsoft PowerPoint 馮天俊-問題分析與決策力

廢證相關作業

1

臺灣地區的警察教育現況與展望

untitled

公告99年度全民健康保險醫療給付費用總額及其分配

龍 華 科 技 大 學

PowerPoint 簡報

untitled

untitled

專 題 論 述

untitled

untitled

Transcription:

列 Kernel Objects Windows Kernel Object 來 理 行 行

What is a Kernel Object? The data structure maintains information about the object Process Object: 錄了 PID, priority, exit code File Object: 錄了 byte offset, sharing mode, open mode Ex: Open for writing or Open for reading A memory block allocated by kernel and accessible only by the kernel application kernel object 料

How to access these kernel objects? CreateFileMapping( ) CreateThread( ) CrateFile( ) 立 kernel object Handle ex HANDLE hfile; hfile = CreateFile("myfile.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - We use the handle to manipulate the kernel object - The kernel object handle is process relative kernel object 不 Process 不

Usage Counting Usage count 1 5 0 deleted Process A CreateFileMapping( ) 3 kernel Process B CreateFileMapping( ) Process C CreateFileMapping( )

Security Kernel Object can be protected with a security descriptor, which decrypts Who create the object Who can gain access to or use the object Who is denied access Usually used when writing server applications

Security Almost all function that create kernel object need ex HANDLE CreateFileMapping( HANDLE hfile, PSECURITY_ATTRIBUTES psa, DWORD flprotect, DWORD dwmaximumsizeheight, DWORD dwmaximumsizelow, PCTSTR pszname); SECURITY_ATTRIBUTES structure NULL (default security) Full access only for 1. administrator group 2. Creator

Access to an existing kernel object access right FileMapping handle HANDLE hfilemapping=openfilemapping(file_map_read, FALSE MyFileMapping ); READ op 讀 File-Mapping Security check fail Return:NULL ok Valid handle

Example: Read the registry HKEY hkey; LONG lret; lret = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Office", 0, KEY_QUERY_VALUE, &hkey ); if( lret == ERROR_SUCCESS ) MessageBox(NULL,"You have Office software","ok",mb_ok); else MessageBox(NULL,"fail","fail",MB_OK); RegCloseKey( hkey ); key security descriptor, user 例 : OperRegstryDemo

Kernel object Object object 滑 Kernel Object object? 立, Security attribute 參數, GDI User Object security attribute 例 : : CreateIcon CreateIcon HICON CreateIcon( HINSTANCE hinst, int nwidth, int nheight, BYTE cplanes, BYTE cbitspixel, CONST BYTE *pbandbits, CONST BYTE *pbxorbits);

Kernel Object? Process Kernel Object Handle Table : 來 理 Kernel Kernel Object Object 列 Index Pointer to Kernel Access Mask Flags 1 0xF0000000 0x???? 0x???????

立 Kernel Object, 1 Process Object Handle Handle Table Table 行 2 Process Thread 立了 kernel object 1. 1. Kernel Kernel kernel object object 2. 2. Process Process Handle Table Table 3. 3. Handle Table Table 立 kernel object HANDLE CreateThread ( ) HANDLE CreateFile( ) HANDLE CreateFileMapping( ) HANDLE CreateSemaphore( ) HANDLE,, Handle Table Table process thread

kernel object 不 : GetLastError() == ERROR_INVALID_ HANDLE 例 function HANDLE 數 BOOL WriteFile( HANDLE hfile, LPCVOID lpbuffer, DWORD nnumberofbytestowrite, LPDWORD lpnumberofbyteswritten, LPOVERLAPPED lpoverlapped ); Step 1 Step 2 Handle 利 HANDLE HANDLE HANDLE HANDLE TABLE TABLE kernel Object Object Step 3 理 Object Object 料 行

立 kernel object NULL FUNCTION -1 (INVALID_HANDLE_VALUE ) : : : HANDLE hmutex = CreateMutex(...); CreateMutex CreateMutex, NULL if (hmutex = = INVALID_HANDLE_VALUE) { } : HANDLE hfile = CreateFile(...); if (hfile = = NULL) { : // We will never execute this code because CreateFile : // returns INVALID_HANDLE_VALUE(-1) if it fails. CreateFile CreateFile, -1-1 }

不 kernel object CloseHandle BOOL CloseHandle(HANDLE hobj); CloseHandle 不 Memory Leak? : process terminated process resource Step 1 Handle Step 2 行 Handle Table Table Kernel Object Object Step 3 不, CloseHandle FALSE kernel object object Usage Usage count count 若 = 00 kernel object object Step 4 Handle Handle Table Table entry

說 Sharing Kernel Objects Across Process, kernel object thread Handle Handle, process 不 kernel object 1. 不 processes 料 MemoryMapFile MemoryMapFile 2. 不 process, 料 Mailslots Mailslots & named named piples piples 3. 不 processes thread 理 行 Mutexes, Mutexes, semaphores, semaphores, and and events events

Object Handle Inheritance -- parent parent-child kernel object handle Parent Parent Process Process Spawn Child Child Process Process Kernel Kernel Objects Objects security security 立,, Object I am a inheritable mutex object SECURITY_ATTRIBUTES sa; sa.nlength=sizeof(sa); sa.lpsecuritydescriptor=null; sa.binherithandle=true; HANDLE hmutex=createmutex(&sa, FALSE, NULL); Can I access? SECURITY_ATTRIBUTES that the object handle should be inheritable!

說 The process s s handle table entry 列 Index Pointer to Kernel 1 0xF0000000 2 0x00000000 3 0xF0000010 Process handle table Access Mask 0x???? (N/A) 0x???? Flags 0x00000000 ( inheritance ) (N/A) 0x00000001 ( Handle child process ) 立 Mutex security security HANDLE hmutex1=createmutex(null, FALSE, FALSE, NULL); NULL); security security : : SECURITY_ATTRIBUTES sa; sa.nlength=sizeof(sa); sa.lpsecuritydescriptor=null; sa.binherithandle=true; HANDLE hmutex2=createmutex(&sa FALSE, NULL) 立 Mutex

Two Steps process process security security : : process process BOOL BOOL CreateProcess( LPCTSTR lpapplicationname, LPTSTR lpcommandline, LPSECURITY_ATTRIBUTES lpprocessattributes, Make Make your your object object LPSECURITY_ATTRIBUTES lpthreadattributes, to to inheritance BOOL BOOLbInheritHandles, DWORD dwcreationflags, LPVOID lpenvironment, Let Let your your child child LPCTSTR lpcurrentdirectory, process process to to inherit inheritall all LPSTARTUPINFO lpstartupinfo, inheritable handles handles LPPROCESS_INFORMATION lpprocessinformation); thread thread security security binherithandles FALSE: FALSE: 不 child child process process parent parent kernel kernel object object TRUE: TRUE: child process parent parent inheritable kernel kernel objects objects

How about the child process s s handle table? Parent Parent Process Process Spawn Index Pointer to Kernel 1 0xF0000000 inheritable inheritable 2 0x00000000 3 0xF0000010 Access Mask 0x???? (N/A) 0x???? Flags 0x00000000( inheritance ) (N/A) 0x00000001 ( Handle child process ) Parent s handle table Child Child Process Process Index Pointer to Kernel 1 0x00000000 2 0x00000000 Access Mask (N/A) (N/A) Flags (N/A) (N/A) child child process process (: (: Index Index 3 0xF0000010 0x???? 0x00000001 Child s handle table

例 例 行流行流 Parent Parent 立 Event Kernel Object 立 Child Process Event Object Child Object handle Event Signal 行 Step 1: 立 Step 2: Command line 串 HANDLE Child Child Step 3: 利 HANDLE Kernel Object signal Step 4: Kernel Object Handle Table 欄 CloseHandle(hsecondHandle);

#include "stdafx.h" #include <windows.h> #include <iostream> using namespace std; Child Child Child // Step 1: 立 int _tmain(int argc, _TCHAR* argv[]) { HANDLE void*, %p %p // Step 2: Command line 串 HANDLE HANDLE hsecondhandle=null; sscanf(argv[0],"%p",&hsecondhandle); // Step 3: 利 HANDLE Kernel Object signal BOOL bsuccess=setevent(hsecondhandle); if(bsuccess) cout <<" parent handle"<<endl; else cout <<""<< endl; 行 行,, 行 } // Step 4: Kernel Object Handle Table 欄 CloseHandle(hsecondHandle); return 0;

void main(){ // Step 1: 立 Kernel Object ( Event ) SECURITY_ATTRIBUTES sa; sa.nlength = sizeof(sa); sa.lpsecuritydescriptor = NULL; sa.binherithandle = TRUE; // << -- HANDLE hmyevent=createevent(&sa,true,false,null); // Step 2: Kernel Object HANDLE 串 char buffer[200]; sprintf( buffer,"%p",hmyevent); // Step 3: 立 Child process HANDLE 串 CommandLine // Child CreateProcess( ); // Step 4: Child process 利 HANDLE Kernel Object signal WaitForSingleObject(hmyEvent, INFINITE ); } CloseHandle( pi.hprocess ); CloseHandle( pi.hthread ); CloseHandle(hmyEvent); return 0; Parent Parent () () Childe 行 SetEvent SetEvent

Named Object Sharing kernel object Not all kernel object can be named Functions HANDLE CreateMutex( PCTSTR pszname) Description HANDLE CreateEvent( PCTSTR pszname) HANDLE CreateSemaphore( PCTSTR pszname) HANDLE CreateWaitableTimer( PCTSTR pszname) HANDLE CreateFileMapping( PCTSTR pszname) Waitable_TimerDemo.txt HANDLE CreateJob( PCTSTR pszname)

說 How to sharing a object Process A HANDLE hmutexprocessa=createmutex(null,false, JeffMutex );,, process B thread thread 立 object Process B HANDLE hmutexprocessa=createmutex(null,false, JeffMutex ); kernel Object Object Name Name kernel Object Object Type fail NULL Yes 1. Handle Table 2. Point to the existing kernel object 3. Increase the usage count

, 不 share 了? : : parent child,? SetHandleInformation( ) kernel object object SetHandleInformation(hobj, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT SetHandleInformation(hobj, HANDLE_FLAG_INHERIT, 0 不

說 Kernel Object Close? : process 立 process, close Handle Handle process process process process Handle 行 不 SetHandleInformation(hobj, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE ) CloseHandle(hobj); 不,, exception, process 行 SetHandleInformation(hobj, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0 ) CloseHandle

object? GetHandleInformation 例 Handle 料 DWORD dwflags; GetHandleInformation (hobj, &dwflags); BOOL fhandleisinheritable = (0!= (dwflags & HANDLE_FLAG_INHERIT)); 若 dwflags dwflags bit bit 欄 =1 =1 11 00 0, TRUE

Prevent multiple instances named kernel object #include "stdafx.h" #include <windows.h> int _tmain(int argc, _TCHAR* argv[]) 立 Baby Baby kernel object object { HANDLE h = CreateEvent(NULL,FALSE,FALSE,"Baby"); } if (GetLastError( )==ERROR_ALREADY_EXISTS) { MessageBox(NULL," 行 ","Error",MB_OK); return(0); } getchar(); // Before exiting, close the object. CloseHandle(h); return(0); 立兩 kernel object,

Baby? Universally Universally Unique Unique Identifier Identifier (UUID) (UUID) VS UUIDGEN.EXE GUID(Globally Unique Identifier) 來 GUID HANDLE h = CreateEvent(NULL,FALSE,FALSE, "d00fead7-d497-4765-a5ef-3d10f9d23023");

End UUIDGEN.EXE VC tool, CoCreateGuid 參 例 例 : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample/html /_sample_mfc_guidgen.asp