27
RoP Hooks [email protected]

Csw2016 macaulay eh_trace-rop_hooks

Embed Size (px)

Citation preview

Page 2: Csw2016 macaulay eh_trace-rop_hooks

Introduction•  K2/[email protected]

•  [email protected]

Page 3: Csw2016 macaulay eh_trace-rop_hooks

Intro/Outline•  Hooking/Tracing

•  Whatisabinarydoing?

•  Canwemodify/detour

•  Frustrations/Hurdles

•  Friendlyinputs•  Symbolsupport

Page 4: Csw2016 macaulay eh_trace-rop_hooks

TOOLS/Open/Github/CODE•  Github.com/K2

•  inVtero.net•  EvolutionfromCSW14(processdetection)

•  Crossplatform(Windows,*BSD,Linux)memoryanalysis

•  Crossmicro-architechture(sandybridge,skylake,…)

•  Crosshypervisor(basedonauto-magicVMCS/EPTPextraction)•  Includesnestedsupport

•  EhTrace(pronounced“ATrace”)•  Whatweregoingtocoverthistime@CSW!J

•  LetmeknowifImissedanycodeinthecheck-in!!

Page 5: Csw2016 macaulay eh_trace-rop_hooks

EhTrace•  Aboottimeforatraceeh?

•  UsesVEHunderthecovers•  Needtobealittlecareful•  Don’twanttoalterorchange

behaviorofwhatwerelookingat

Page 6: Csw2016 macaulay eh_trace-rop_hooks

Hookingexecution•  Detours

•  Requiresaninstructionlengthdecoder•  Rewritesfunctionprologintoaspecializedfunctionwhichperformslogging,analysis

etc…

•  Usuallystatic,canbedynamic/jitter,•  mayjmptoaleaflikedetourwhichcanworkwithoutknowingthefunctionprototype/

stackrequirements

•  Mostofthetimeyouwillneedsymbolsorreallygoodlogicinthehookertonotbreakexecution

Page 7: Csw2016 macaulay eh_trace-rop_hooks

What’stheproblemagain?•  Debuggersareslow

•  Secondprocesscontextswitchingisfairlyexpensive

•  Logicforconditionalbreakpointsisexponentiallymoreexpensive

•  Checksums•  Maliciousbinariesoftenchecksumtheircodetovalidatetheyarenotbeinganalyzed

•  Highlysecureenvironmentsmaychecksumtheirbinariestomakesuretheyarenottamperedwith

Page 8: Csw2016 macaulay eh_trace-rop_hooks

Ret2code•  Originallibcwork,Solardesigner

•  http://seclists.org/bugtraq/1997/Aug/63

•  Handysincemostoverflowscontainapointertousefuladdresses•  Yourinput•  Systemlibraries

•  Stillusedtothisday(RoP)

Page 9: Csw2016 macaulay eh_trace-rop_hooks

StackHooking•  Attemptedtouseasalternativetowhatwewoundupusing

•  Fromasecond“manager”thread•  LoadfromaRoPchainpool(memoryareawithRoPgagets)

•  Borrowmemoryfromtheexecutingstackfromabovethestacktop•  Usuallysomesparememorythere

•  Notverygreat•  Onlypostconditionhooking

•  Havetofindawaytogetnotificationonnewcalls•  Dosomesortofshadowstack/memoryprotectiontrickery

•  Tendstobefairlyfragile

RET

RET

Page 10: Csw2016 macaulay eh_trace-rop_hooks

EhTrace–howitworks

• Remarkablyeasytotriggerbranchsteppingofabinary•  IntheVEHhandlerset3bitsandreturn.

•  THAT’SIT•  TRAPFLAG•  OTHERFLAGS:D

LONGWINAPIvEhTracer(PEXCEPTION_POINTERSExceptionInfo){ //singlestepExceptionInfo->ContextRecord->EFlags|=0x100;//setupbranchtracingExceptionInfo->ContextRecord->Dr7|=0x300;

Page 11: Csw2016 macaulay eh_trace-rop_hooks

EhTrace–RoPHooks•  RegisteraVEHhandlerCreateRemoteThread(…&VeH_RoP,..);

•  VeH_RoP–useaRoPgadgetfinder(therearemany)

•  Handleronlyneedstosetthe3bitsthenexitwithcontinuestatus

•  Usingtheexceptiondispatcherwereabletonowgetthepreconditionswemissedwiththestack/shadowmodel

•  Debuggerfunctionalityw/oadebugger•  i.e.passesallchecksfromhttps://github.com/Trietptm-on-Security/DebugDetector

•  Mayintroduceaplugintoallowwindbgtouseourengineasasideloadedinprocdebugger

Page 12: Csw2016 macaulay eh_trace-rop_hooks

Whatelseisitgoodfor?•  Branchsteppingisprettysweet!

•  Alotmorethandetoursonfunctions

•  Basicblockanalysis•  Codecoverages•  CanweputthisintoaDBI(DynamicBinaryInstrumentation)framework?

•  Doweneedtoemulate?Isn’tthatslow?•  Ifweredealingwithamaliciousbinarywehaveseveralthingstoconsider.

•  Ofcourseweneedtoalsowatchoutforanotherwisenon-malbinarydoingsomethingthatmightdisruptourtrace

Page 13: Csw2016 macaulay eh_trace-rop_hooks

Maintainingcontrol•  Maybeusepageprotectiontoforceanexceptiononexecution(don’twanttoplaceanint3obviously)•  Whenpageisattemptedtobeexecutedwechecktoseewhatemulationisneeded

•  IfsomebodytriestotakeoverVEH

•  Whataboutintra-blockstuff?•  Can’ttheyjustwriteoverourVEHhandlerinmemory?

•  Sure,mayberegister2!AlsosetuptheVEHcontinuehandler

Page 14: Csw2016 macaulay eh_trace-rop_hooks

Blockfightingwithahooker• BlockFighterhastobesmart,fastandintotalcontrol!

•  MuchlikeaStreetFighterIIchamp!

Page 15: Csw2016 macaulay eh_trace-rop_hooks

BlockFighting

Page 16: Csw2016 macaulay eh_trace-rop_hooks

BlockFighting

•  Simplifiedanalysis•  Usingcapstonewe&thebranchstep•  Atthepointofanyjmp/ret/callcontroltransferwecanstopourfightuntilthenextround•  Round2FIGHT!•  Actuallyweresogoodwealways“givesecondround”!•  Thatmeansreallythatifthere’saconditionalweneedtofollowthroughaconditional

•  Jne–wefollowthenon-jumptoensurewecompletethecontextuntilaret/jmp/call

Page 17: Csw2016 macaulay eh_trace-rop_hooks

BlockFighting•  Watchtheeflags&DRanymanipulationwillcauseproblemsforus

•  DEBUG_MSR?

•  Lotsofthingsprobably

•  Overallhoweverwehaveaplatformtobuildprimitivesonthatcaneventuallydobattleinastructuredway•  Maybecombineblockfighterwithstackinjectiontoensurewehaveadditionalpost-

conditionchecksonourflag/branch-step/vehstate

Page 18: Csw2016 macaulay eh_trace-rop_hooks

Coverage•  Canyouhearmenow?

•  Flamegraph•  CurrentminimalstateincludesRIP,LAST_RIP,TID,FLAGSandESP

•  Thisissufficienttobuildanycodegraph!Intra-procedural,callgraphorfulltrace

•  FLAMINGBlockFighter!•  http://www.brendangregg.com/FlameGraphs

Page 19: Csw2016 macaulay eh_trace-rop_hooks

CPUFLAMEGRAPH•  CLICKHERE

•  Origfromhere->http://www.brendangregg.com/FlameGraphs/cpu-bash-flamegraph.svg

(PowerPointdoesn’tdoSVG’z)

Wehaveallthedatarequiredforgeneratingthese,howeverit’saTODO

Page 20: Csw2016 macaulay eh_trace-rop_hooks

Upcomingstuff…•  MSAGLgraphmaps–fun/interactivemeshgraph,sortoflookslikeanexpandablespiderweb!

•  SVGbuilder(withoutthe.plscriptsfromBrendan)

•  TighterSymbols(graphsandimagesnotasfunwithoutEnglisheh?)

•  Strace/ltrace/*traceforWindows(autoinject&logtoconsole)

Page 21: Csw2016 macaulay eh_trace-rop_hooks

Upcomingstuff:Blockfighters•  AFlagfighter

•  Rflagschecks

•  APageFighter•  Pageprotectionmonitor

•  E.g.protecttheentrypointCreateRemoteThreadcall’sbeforeitcallsthespecified&funcargumenttodetectremotethreadsbeforetheDLLthreadnotificationrun’s•  Usetrickslikethistoensureyournotbeingtrickedyourself

•  EmuFighter•  Emulateanoperationthatwouldotherwisedetectus

Page 22: Csw2016 macaulay eh_trace-rop_hooks

Privateimplementationsdiffer!

•  Yourfighterswillbevarious•  i.e.ifyournotusinganysystem/runtimeAPIyoudon’tneedtoworryaboutlockingas

much(obviously)

Page 23: Csw2016 macaulay eh_trace-rop_hooks

Notepad.exeBasicBlocksexecution

Page 24: Csw2016 macaulay eh_trace-rop_hooks

Withdisassembly

Page 25: Csw2016 macaulay eh_trace-rop_hooks

FlameGraph–nosymbolsyet

Page 26: Csw2016 macaulay eh_trace-rop_hooks

Questions?•  Feedback,bugs&Featurerequestsplease

•  https://github.com/K2•  Keepwatchingforupdates

Page 27: Csw2016 macaulay eh_trace-rop_hooks

Thankyou•