资源描述:
《C中通过溢出覆盖虚函数指针列表执行代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Catalog:Staticlinkageanddynamiclinkingofvirtualfunctionsin1.C++Spatialorganizationandoverflowtestofobjectsin2.VCSpatialorganizationandoverflowtestofobjectsin3.GCC4.referencesStaticlinkageanddynamiclinkingofvirtualfunctionsin<AmagicweaponinC++isthevirtualfunction,whichissimpl
2、yafunctiondefinedbyaddingthevirtualkeyword.Itsfeatureistosupportdynamicbinding.Today,almostallofthelargesoftwaredevelopedbyC++cannotbeseparatedfromvirtualfunctionsAtypicalexampleofuseisthatvirtualfunctionsareoneofthecornerstonesofMFC・Herearetwoconceptsthatneedtobeexplainedfirst:S
3、taticlinking:popularpointisthatwhentheprogramcompiles,itdeterminestheaddressofthecallingtarget・Dynamiclinking:theprogramrunphasedeterminestheaddressofthecallingtarget.InC++,theusualfunctioncallsarestaticbinding,butifthefunctionisdefined,thevirtualkeyisaddedAndwhencallingafunction
4、iscalledbyapointerorreference,thendynamicbindingisusedatthispoint・Asimpleexample://test・cpp#include〈iostreani・h>ClassClassAPublic:Intnuml;ClassA(){numl二Oxffff;};Virtualvoidtestl(void){};Virtualvoidtest2(void){};};ClassA,objA,*pobjA;Intmain(void)PobjA二&objA;ObjA.testl();ObjA.test2
5、();PobjA->testl();PobjA->test2();Return0;}CompileusingVC:OpenacommandlineandcallCLdirectlyonthecommandlinetocompile:(ifyouinstallVC,thereisnoregistrationenvironmentVariable,thenrunbinVCVARS32.BATintheVCdirectoryonthecommandlineCLtest・cpp/FaGeneratestest・asmintermediateassemblyco
6、deThentakealookatwhatisthemysteryofASM,theanalysisisabitlong,wanttohavepatience!Let'ssee:Datadefinition:_BSSSEGMENT0bjA@@3VClassA@@A,DQ,01H,DUP(...);objA64bitsPobjA@@3PAVClassA@@A,DD,01H,DUP(...);pobjA,anaddress32bits_BSSENDSSeetheobjAfor64bits,whatdoesitcontaininside?Andthenlook
7、attheconstructor:_this$=-4OC1assA@@QAE@XZ,PROC,NEAR,ClassA::ClassA()definesavariablethis?!Filetest・cppLine6PushEBPMOV,EBP,ESPPushECXMOV,DWORD,PTR,_this$[ebp],ECX,ECX,assignedto_this??don,tyousee?MOV,eax,DWORD,PTR,_this$[ebp]MOV,DWORD,PTR,[eax],OFFSET,FLAT:?_7ClassA@@6B@?ClassA::'
8、vftable,Thefrontpartisthecompilerplus・Ou