?? 創(chuàng)建svchost_exe調(diào)用的服務(wù)原理與實踐.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0046)http://www.xfocus.net/articles/200308/601.html -->
<HTML><HEAD><TITLE>創(chuàng)建SvcHost.exe調(diào)用的服務(wù)原理與實踐</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META
content="安全焦點, xfocus, 陷阱網(wǎng)絡(luò), honeynet, honeypot, 調(diào)查取證, forensic, 入侵檢測, intrusion detection, 無線安全, wireless security, 安全論壇, security forums, 安全工具, security tools, 攻擊程序, exploits, 安全公告, security advisories, 安全漏洞, security vulnerabilities, 安全教程, security tutorials, 安全培訓(xùn), security training, 安全幫助, security help, 安全標(biāo)準(zhǔn), security standards, 安全代碼, security code, 安全資源, security resources, 安全編程, security programming, 加密, cryptography,SvcHost,共享進程,系統(tǒng)服務(wù)"
name=Keywords><LINK href="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/plone.css" type=text/css
rel=stylesheet>
<META content="MSHTML 6.00.2800.1400" name=GENERATOR></HEAD>
<BODY text=#000000 bgColor=#ffffff>
<DIV class=top>
<DIV class=searchBox>
<FORM name=searchform action=http://www.google.com/search method=get><INPUT
type=hidden value=www.xfocus.net name=domains> <INPUT type=hidden
value=www.xfocus.net name=sitesearch> <INPUT name=q> <INPUT type=submit value="Google Search" name=btnG> </FORM></DIV><IMG height=80
alt="xfocus logo" src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/logo.gif" width=180
border=0> <IMG height=20 alt="xfocus title"
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/title.gif" width=230 border=0> </DIV>
<DIV class=tabs><A class=plain href="http://www.xfocus.net/index.html">首頁</A> <A
class=plain href="http://www.xfocus.net/releases/index.html">焦點原創(chuàng)</A> <A
class=selected href="http://www.xfocus.net/articles/index.html">安全文摘</A> <A
class=plain href="http://www.xfocus.net/tools/index.html">安全工具</A> <A
class=plain href="http://www.xfocus.net/vuls/index.html">安全漏洞</A> <A class=plain
href="http://www.xfocus.net/projects/index.html">焦點項目</A> <A class=plain
href="https://www.xfocus.net/bbs/index.php?lang=cn">焦點論壇</A> <A class=plain
href="http://www.xfocus.net/about/index.html">關(guān)于我們</A> </DIV>
<DIV class=personalBar><A
href="https://www.xfocus.net/php/add_article.php">添加文章</A> <A
href="http://www.xfocus.org/">English Version</A> </DIV>
<TABLE class=columns>
<TBODY>
<TR>
<TD class=left>
<DIV class=box>
<H5> 文章分類 </H5>
<DIV class=body>
<DIV class="content odd">
<DIV style="WHITE-SPACE: nowrap"><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/4.html">專題文章</A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/2.html">漏洞分析</A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/3.html">安全配置</A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/1.html">黑客教學(xué)</A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/5.html"><B>編程技術(shù)
<<</B></A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/7.html">工具介紹</A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/6.html">火墻技術(shù)</A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/8.html">入侵檢測</A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/9.html">破解專題</A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/11.html">焦點公告</A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/folder_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/12.html">焦點峰會</A><BR></DIV></DIV></DIV></DIV>
<DIV class=box>
<H5> 文章推薦 </H5>
<DIV class=body>
<DIV class="content odd"><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/document_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/200308/593.html">技術(shù)挑戰(zhàn)<一></A><BR><IMG
src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/document_icon.gif" border=0> <A
href="http://www.xfocus.net/articles/200306/562.html">任意用戶模式下執(zhí)行 ring 0
代碼</A><BR><IMG src="創(chuàng)建SvcHost_exe調(diào)用的服務(wù)原理與實踐.files/document_icon.gif"
border=0> <A
href="http://www.xfocus.net/articles/200307/563.html">IIS的NSIISLOG.DLL溢出問題分析</A><BR></DIV></DIV></DIV></TD>
<TD class=main>
<H1>創(chuàng)建SvcHost.exe調(diào)用的服務(wù)原理與實踐</H1><BR>創(chuàng)建時間:2003-08-27
更新時間:2003-08-27<BR>文章屬性:原創(chuàng)<BR>文章提交:<A
href="https://www.xfocus.net/bbs/index.php?lang=cn&act=Profile&do=03&MID=1249">bingle</A>
(bingle_at_email.com.cn)<BR><BR>創(chuàng)建SvcHost.exe調(diào)用的服務(wù)原理與實踐<BR> by
bingle_at_email.com.cn<BR> <A
href="http://www.binglesite.net/"
target=_blank>http://www.binglesite.net/</A><BR><BR><BR>1.
多個服務(wù)共享一個Svchost.exe進程利與弊<BR><BR>windows 系統(tǒng)服務(wù)分為獨立進程和共享進程兩種,在windows
NT時只有服務(wù)器管理器SCM(Services.exe)有多個共享服務(wù),隨著系統(tǒng)內(nèi)置服務(wù)的增加,在windows
2000中ms又把很多服務(wù)做成共享方式,由svchost.exe啟動。windows
2000一般有2個svchost進程,一個是RPCSS(Remote Procedure
Call)服務(wù)進程,另外一個則是由很多服務(wù)共享的一個svchost.exe。而在windows
XP中,則一般有4個以上的svchost.exe服務(wù)進程,windows 2003
server中則更多,可以看出把更多的系統(tǒng)內(nèi)置服務(wù)以共享進程方式由svchost啟動是ms的一個趨勢。這樣做在一定程度上減少了系統(tǒng)資源的消耗,不過也帶來一定的不穩(wěn)定因素,因為任何一個共享進程的服務(wù)因為錯誤退出進程就會導(dǎo)致整個進程中的所有服務(wù)都退出。另外就是有一點安全隱患,首先要介紹一下svchost.exe的實現(xiàn)機制。<BR><BR><BR>2.
Svchost原理<BR><BR>Svchost本身只是作為服務(wù)宿主,并不實現(xiàn)任何服務(wù)功能,需要Svchost啟動的服務(wù)以動態(tài)鏈接庫形式實現(xiàn),在安裝這些服務(wù)時,把服務(wù)的可執(zhí)行程序指向svchost,啟動這些服務(wù)時由svchost調(diào)用相應(yīng)服務(wù)的動態(tài)鏈接庫來啟動服務(wù)。<BR><BR>那么svchost如何知道某一服務(wù)是由哪個動態(tài)鏈接庫負(fù)責(zé)呢?這不是由服務(wù)的可執(zhí)行程序路徑中的參數(shù)部分提供的,而是服務(wù)在注冊表中的參數(shù)設(shè)置的,注冊表中服務(wù)下邊有一個Parameters子鍵其中的ServiceDll表明該服務(wù)由哪個動態(tài)鏈接庫負(fù)責(zé)。并且所有這些服務(wù)動態(tài)鏈接庫都必須要導(dǎo)出一個ServiceMain()函數(shù),用來處理服務(wù)任務(wù)。<BR><BR>例如rpcss(Remote
Procedure
Call)在注冊表中的位置是 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RpcSs,它的參數(shù)子鍵Parameters里有這樣一項:<BR>"ServiceDll"=REG_EXPAND_SZ:"%SystemRoot%\system32\rpcss.dll"<BR>當(dāng)啟動rpcss服務(wù)時,svchost就會調(diào)用rpcss.dll,并且執(zhí)行其ServiceMain()函數(shù)執(zhí)行具體服務(wù)。<BR><BR>既然這些服務(wù)是使用共享進程方式由svchost啟動的,為什么系統(tǒng)中會有多個svchost進程呢?ms把這些服務(wù)分為幾組,同組服務(wù)共享一個svchost進程,不同組服務(wù)使用多個svchost進程,組的區(qū)別是由服務(wù)的可執(zhí)行程序后邊的參數(shù)決定的。<BR><BR>例如rpcss在注冊表中
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RpcSs
有這樣一項:<BR>"ImagePath"=REG_EXPAND_SZ:"%SystemRoot%\system32\svchost -k
rpcss"<BR>因此rpcss就屬于rpcss組,這在服務(wù)管理控制臺也可以看到。<BR><BR>svchost的所有組和組內(nèi)的所有服務(wù)都在注冊表的如下位置:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Svchost,例如windows
2000共有4組rpcss、netsvcs、wugroup、BITSgroup,其中最多的就是netsvcs=REG_MULTI_SZ:EventSystem.Ias.Iprip.Irmon.Netman.Nwsapagent.Rasauto.Rasman.Remoteaccess.SENS.Sharedaccess.Tapisrv.Ntmssvc.wzcsvc..<BR><BR>在啟動一個svchost.exe負(fù)責(zé)的服務(wù)時,服務(wù)管理器如果遇到可執(zhí)行程序內(nèi)容ImagePath已經(jīng)存在于服務(wù)管理器的映象庫中,就不在啟動第2個進程svchost,而是直接啟動服務(wù)。這樣就實現(xiàn)了多個服務(wù)共享一個svchost進程。<BR><BR><BR>3.
Svchost代碼<BR><BR>現(xiàn)在我們基本清楚svchost的原理了,但是要自己寫一個DLL形式的服務(wù),由svchost來啟動,僅有上邊的信息還有些問題不是很清楚。比如我們在導(dǎo)出的ServiceMain()函數(shù)中接收的參數(shù)是ANSI還是Unicode?我們是否需要調(diào)用RegisterServiceCtrlHandler和StartServiceCtrlDispatcher來注冊服務(wù)控制及調(diào)度函數(shù)?<BR><BR>這些問題要通過查看svchost代碼獲得。下邊的代碼是windows
2000+ service pack 4
的svchost反匯編片段,可以看出svchost程序還是很簡單的。<BR><BR>主函數(shù)首先調(diào)用ProcCommandLine()對命令行進行分析,獲得要啟動的服務(wù)組,然后調(diào)用SvcHostOptions()查詢該服務(wù)組的選項和服務(wù)組的所有服務(wù),并使用一個數(shù)據(jù)結(jié)構(gòu)
svcTable 來保存這些服務(wù)及其服務(wù)的DLL,然后調(diào)用PrepareSvcTable() 函數(shù)創(chuàng)建SERVICE_TABLE_ENTRY
結(jié)構(gòu),把所有處理函數(shù)SERVICE_MAIN_FUNCTION 指向自己的一個函數(shù)FuncServiceMain(),最后調(diào)用API
StartServiceCtrlDispatcher() 注冊這些服務(wù)的調(diào)度函數(shù)。<BR><BR>;
=============================== Main Funcion
===========================================<BR>.text:010010B8
public start<BR>.text:010010B8
start proc
near<BR>.text:010010B8
push esi<BR>.text:010010B9
push edi<BR>.text:010010BA
push offset sub_1001EBA ;
lpTopLevelExceptionFilter<BR>.text:010010BF
xor edi,
edi<BR>.text:010010C1
call ds:SetUnhandledExceptionFilter<BR>.text:010010C7
push 1
;
uMode<BR>.text:010010C9
call ds:SetErrorMode<BR>.text:010010CF
call ds:GetProcessHeap<BR>.text:010010D5
push eax<BR>.text:010010D6
call sub_1001142<BR>.text:010010DB
mov eax, offset
dword_1003018<BR>.text:010010E0
push offset unk_1003000 ;
lpCriticalSection<BR>.text:010010E5
mov dword_100301C,
eax<BR>.text:010010EA
mov dword_1003018,
eax<BR>.text:010010EF
call ds:InitializeCriticalSection<BR>.text:010010F5
call ds:GetCommandLineW<BR>.text:010010FB
push eax
;
lpString<BR>.text:010010FC
call ProcCommandLine<BR>.text:01001101
mov esi,
eax<BR>.text:01001103
test esi,
esi<BR>.text:01001105
jz short
lab_doservice<BR>.text:01001107
push esi<BR>.text:01001108
call SvcHostOptions<BR>.text:0100110D
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -