{"id":7082,"date":"2020-04-08T22:19:52","date_gmt":"2020-04-08T22:19:52","guid":{"rendered":"http:\/\/www.hexacorn.com\/blog\/?p=7082"},"modified":"2020-04-09T14:03:28","modified_gmt":"2020-04-09T14:03:28","slug":"procmon-as-an-api-monitor","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2020\/04\/08\/procmon-as-an-api-monitor\/","title":{"rendered":"ProcMon as&#8230; an API Monitor"},"content":{"rendered":"\n<p>A competition for the most important research tool on Windows platform is not necessary &#8211; ProcMon will always win. <\/p>\n\n\n\n<p>Why?<\/p>\n\n\n\n<p>It helps us with research, troubleshooting, and it still works after so many years, and despite so many changes introduced to Windows during this time.<\/p>\n\n\n\n<p>And it still can surprise you.<\/p>\n\n\n\n<p>In this post I briefly describe ProcMon functionality that many people may not be aware of. Actually, two features that offer a very interesting functionality.<\/p>\n\n\n\n<p>The first one is Stack Trace.<\/p>\n\n\n\n<p>Any event you see caught by ProcMon has an associated Stack trace that you can explore by double clicking the event of interest, and selecting the &#8216;Stack&#8217; tab:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/04\/procmon_1.png\"><img decoding=\"async\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/04\/procmon_1.png\" alt=\"\" class=\"wp-image-7083\" width=\"500\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/04\/procmon_1.png 812w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/04\/procmon_1-269x300.png 269w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/04\/procmon_1-768x855.png 768w\" sizes=\"(max-width: 812px) 100vw, 812px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>This is pretty cool as it helps researches to find out where the possible access to an interesting object (a key, a file, etc.) comes from -i.e. from main .exe or loaded .dll. <\/p>\n\n\n\n<p>The second feature is the export to XML that may include the aforementioned stack trace (tick the &#8216;Resolve stack symbols&#8217; as well &#8211; it will resolve addresses to actual function names if these are available in symbols).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/04\/procmon_2.png\"><img decoding=\"async\" loading=\"lazy\" width=\"414\" height=\"352\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/04\/procmon_2.png\" alt=\"\" class=\"wp-image-7084\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/04\/procmon_2.png 414w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/04\/procmon_2-300x255.png 300w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/a><\/figure>\n\n\n\n<p>This will create a HUGE XML file.<\/p>\n\n\n\n<p>And a very interesting one&#8230;<\/p>\n\n\n\n<p>It includes sections for process list and events. The first one includes a list of processes and their properties:<\/p>\n\n\n\n<ul><li>Time<\/li><li>Processes name<\/li><li>Process ID<\/li><li>Parent process ID<\/li><li>Command Line<\/li><li>Integrity<\/li><li>Owner<\/li><li>Base Addresses<\/li><li>Modules Loaded<\/li><\/ul>\n\n\n\n<p>And the second one lists the actual events:<\/p>\n\n\n\n<ul><li>Time<\/li><li>Process Name<\/li><li>Process ID<\/li><li>Operation<\/li><li>Path<\/li><li>Result<\/li><li>Location<\/li><li>Detail<\/li><\/ul>\n\n\n\n<p>followed by the stack trace &#8211; all frames one bye one:<\/p>\n\n\n\n<ul><li>Depth<\/li><li>Address<\/li><li>Path<\/li><li>Location<\/li><\/ul>\n\n\n\n<p>With this data, and taking selective stack trace entries, it&#8217;s very easy to convert it to a timeline that resembles a log from an API Monitor&#8230;<\/p>\n\n\n\n<p>A small snippet of data:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;procmon&gt;&lt;processlist&gt;&lt;process&gt;\n&lt;ProcessIndex&gt;100&lt;\/ProcessIndex&gt;\n&lt;ProcessId&gt;160&lt;\/ProcessId&gt;\n&lt;ParentProcessId&gt;2880&lt;\/ParentProcessId&gt;\n&lt;ParentProcessIndex&gt;101&lt;\/ParentProcessIndex&gt;\n&lt;AuthenticationId&gt;00000000:00071cd1&lt;\/AuthenticationId&gt;\n&lt;CreateTime&gt;132308446793816226&lt;\/CreateTime&gt;\n&lt;FinishTime&gt;0&lt;\/FinishTime&gt;\n&lt;IsVirtualized&gt;0&lt;\/IsVirtualized&gt;\n&lt;Is64bit&gt;1&lt;\/Is64bit&gt;\n&lt;Integrity&gt;High&lt;\/Integrity&gt;\n&lt;Owner&gt;user&lt;\/Owner&gt;\n&lt;ProcessName&gt;Procmon64.exe&lt;\/ProcessName&gt;\n&lt;ImagePath&gt;C:\\Users\\user\\AppData\\Local\\Temp\\Procmon64.exe&lt;\/ImagePath&gt;\n&lt;CommandLine&gt;\"C:\\Users\\user\\AppData\\Local\\Temp\\Procmon64.exe\" \/originalpath \"C:\\tools\\Procmon.exe\"&lt;\/CommandLine&gt;\n&lt;CompanyName&gt;Sysinternals - www.sysinternals.com&lt;\/CompanyName&gt;\n&lt;Version&gt;3.53&lt;\/Version&gt;\n&lt;Description&gt;Process Monitor&lt;\/Description&gt;\n&lt;modulelist&gt;\n&lt;module&gt;\n&lt;Timestamp&gt;132308446979926644&lt;\/Timestamp&gt;\n&lt;BaseAddress&gt;0x2360000&lt;\/BaseAddress&gt;\n&lt;Size&gt;77824&lt;\/Size&gt;\n&lt;Path&gt;C:\\Windows\\system32\\wbem\\wbemsvc.dll&lt;\/Path&gt;\n&lt;Version&gt;10.0.14409.1005 (rs1_srvoob.161208-1155)&lt;\/Version&gt;\n&lt;Company&gt;Microsoft Corporation&lt;\/Company&gt;\n&lt;Description&gt;WMI&lt;\/Description&gt;\n&lt;\/module&gt;\n...\n&lt;eventlist&gt;\n&lt;event&gt;\n&lt;ProcessIndex&gt;105&lt;\/ProcessIndex&gt;\n&lt;Time_of_Day&gt;11:38:18.1001657&lt;\/Time_of_Day&gt;\n&lt;Process_Name&gt;Explorer.EXE&lt;\/Process_Name&gt;\n&lt;PID&gt;2192&lt;\/PID&gt;\n&lt;Operation&gt;CreateFile&lt;\/Operation&gt;\n&lt;Path&gt;C:\\Users\\user\\AppData\\Local\\Temp\\Procmon64.exe&lt;\/Path&gt;\n&lt;Result&gt;SUCCESS&lt;\/Result&gt;\n&lt;Detail&gt;Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n\/a, ShareMode: Read, Write, Delete, AllocationSize: n\/a, OpenResult: Opened&lt;\/Detail&gt;\n&lt;stack&gt;\n&lt;frame&gt;\n&lt;depth&gt;0&lt;\/depth&gt;\n&lt;address&gt;0xfffff8800116d067&lt;\/address&gt;\n&lt;path&gt;C:\\Windows\\system32\\drivers\\fltmgr.sys&lt;\/path&gt;\n&lt;location&gt;FltAcquirePushLockShared + 0x907&lt;\/location&gt;\n&lt;\/frame&gt;\n\u2026\n&lt;frame&gt;\n&lt;depth&gt;1&lt;\/depth&gt;\n&lt;address&gt;0xfffff8800116f9aa&lt;\/address&gt;\n&lt;path&gt;C:\\Windows\\system32\\drivers\\fltmgr.sys&lt;\/path&gt;\n&lt;location&gt;FltIsCallbackDataDirty + 0x20ba&lt;\/location&gt;\n&lt;\/frame&gt;\n\u2026\n&lt;frame&gt;\n&lt;depth&gt;39&lt;\/depth&gt;\n&lt;address&gt;0x78e7c521&lt;\/address&gt;\n&lt;path&gt;C:\\Windows\\SYSTEM32\\ntdll.dll&lt;\/path&gt;\n&lt;location&gt;RtlUserThreadStart + 0x21&lt;\/location&gt;\n&lt;\/frame&gt;\n&lt;\/stack&gt;\n&lt;\/event&gt;<\/pre>\n\n\n\n<p>The Stack Trace covers the stack from user and kernel mode.<\/p>\n\n\n\n<p>You may be wondering now&#8230; what happens when Procmon detects API calls from a code injected into another process?<\/p>\n\n\n\n<p>Great question&#8230;<\/p>\n\n\n\n<p>They stand out as hell&#8230; as Procmon is unable to resolve them:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;ProcessIndex&gt;105&lt;\/ProcessIndex&gt;\n&lt;Time_of_Day&gt;11:38:34.0276039&lt;\/Time_of_Day&gt;\n&lt;Process_Name&gt;Explorer.EXE&lt;\/Process_Name&gt;\n&lt;PID&gt;2192&lt;\/PID&gt;\n&lt;Operation&gt;RegOpenKey&lt;\/Operation&gt;\n&lt;Path&gt;HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run&lt;\/Path&gt;\n&lt;Result&gt;SUCCESS&lt;\/Result&gt;\n&lt;Detail&gt;Desired Access: Write, Query Value, Enumerate Sub Keys, Delete&lt;\/Detail&gt;\n&lt;stack&gt;\n&lt;frame&gt;\n&lt;depth&gt;0&lt;\/depth&gt;\n&lt;address&gt;0xfffff80002e3e550&lt;\/address&gt;\n&lt;path&gt;C:\\Windows\\system32\\ntoskrnl.exe&lt;\/path&gt;\n&lt;location&gt;MmUnmapViewInSessionSpace + 0x7a0&lt;\/location&gt;\n&lt;\/frame&gt;\n\u2026\n&lt;frame&gt;\n&lt;depth&gt;12&lt;\/depth&gt;\n&lt;address&gt;<strong>0x16000067c<\/strong>&lt;\/address&gt;\n&lt;\/frame&gt;\n&lt;\/stack&gt;\n&lt;\/event&gt;<\/pre>\n\n\n\n<p>That last address 0x16000067c?<\/p>\n\n\n\n<p>Bingo! It&#8217;s a code injected by malware.<\/p>\n\n\n\n<p>Now, all you have to do is to filter this data by PID, address range, etc. I am not a XML wizard so I take a shortcut and parse this data in a line-by line way using a simple state machine; still, the result is pretty&#8230; API Monitorish&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">105 svchost.exe 0x2600475cc 9 RegQueryKey HKCU SUCCESS\n105 svchost.exe 0x2600475cc 12 RegOpenKey HKCU\\\nSoftware\\Microsoft\\Windows\\CurrentVersion\\Run SUCCESS\n105 svchost.exe 0x2600475cc 8 RegSetInfoKey HKCU\\\nSoftware\\Microsoft\\Windows\\CurrentVersion\\Run SUCCESS\n105 svchost.exe 0x2600471dd 13 RegCloseKey HKCU SUCCESS\n105 svchost.exe 0x2600471dd 14 RegOpenKey HKCU SUCCESS\n105 svchost.exe 0x260047291 7 RegEnumKey HKCU SUCCESS\n105 svchost.exe 0x260047291 7 RegEnumKey HKCU SUCCESS<\/pre>\n\n\n\n<p>And the bonus:<\/p>\n\n\n\n<p>You could convert these events and incorporate them as comments into IDA, extract IOCs (e.g. by filtering over &#8216;CreateFile&#8217;, etc.), and probably do a few more interesting things (support PE dumping of injected code?)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A competition for the most important research tool on Windows platform is not necessary &#8211; ProcMon will always win. Why? It helps us with research, troubleshooting, and it still works after so many years, and despite so many changes introduced &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2020\/04\/08\/procmon-as-an-api-monitor\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/7082"}],"collection":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/comments?post=7082"}],"version-history":[{"count":5,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/7082\/revisions"}],"predecessor-version":[{"id":7091,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/7082\/revisions\/7091"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=7082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=7082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=7082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}