{"id":6441,"date":"2019-06-12T23:22:14","date_gmt":"2019-06-12T23:22:14","guid":{"rendered":"http:\/\/www.hexacorn.com\/blog\/?p=6441"},"modified":"2019-06-13T08:09:43","modified_gmt":"2019-06-13T08:09:43","slug":"code-execution-via-surgical-callback-overwrites-e-g-dns-memory-functions","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2019\/06\/12\/code-execution-via-surgical-callback-overwrites-e-g-dns-memory-functions\/","title":{"rendered":"Code Execution via surgical callback overwrites (e.g. DNS memory functions)"},"content":{"rendered":"\n<p>Today I looked at Sysmon v10 and its support for logging DNS queries. It&#8217;s a pretty cool feature that intercepts all the DNS requests on a monitored host, and if possible, maps them to the process name making that request. It is a nice addition to Sysmon&#8217;s already awesome logging capabilities.<\/p>\n\n\n\n<p>Just for fun, I created a simple POC that used <em>DnsQuery_A<\/em> API to send a multiline DNS query, because I wanted to see how Sysmon will react to it. It was obviously a non-sensical exercise, but it&#8217;s fun to see we can modify the layout of Event Logs by introducing some unexpected, redundant data:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"331\" height=\"300\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/multiline_dns_req.png\" alt=\"\" class=\"wp-image-6442\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/multiline_dns_req.png 331w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/multiline_dns_req-300x272.png 300w\" sizes=\"(max-width: 331px) 100vw, 331px\" \/><\/figure>\n\n\n\n<p>Anyway&#8230;<\/p>\n\n\n\n<p>I decided to look at the <em>DnsQuery_A<\/em> function in IDA as well. I was curious if\/what characters it accepts &amp; if there is any limit to the buffer it can process. This was a quick &amp; dirty attempt to see if I could send a query that Sysmon would truncate <a href=\"https:\/\/www.hexacorn.com\/blog\/2018\/06\/29\/sysmon-doing-lines-part-3\/\">in a similar fashion as I described in this post<\/a>. <\/p>\n\n\n\n<p>While digging into the code I noticed an interesting way dnsapi.dll is allocating memory. Instead of a fixed (inline) function it relies on a couple of callbacks. One of them is a memory allocation routine. When the library needs memory, it calls the function, and if it is not set, it relies on its own internal routines.<\/p>\n\n\n\n<p>This immediately caught my attention. If we can find the address of this callback inside a remote process we can use it to execute code next time DNS library asks for memory. <\/p>\n\n\n\n<p>This is the memory allocation function used by <em>DnsQuery_*<\/em> functions (32-bit): <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"573\" height=\"473\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/dns_mem_alloc.png\" alt=\"\" class=\"wp-image-6443\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/dns_mem_alloc.png 573w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/dns_mem_alloc-300x248.png 300w\" sizes=\"(max-width: 573px) 100vw, 573px\" \/><\/figure>\n\n\n\n<p>Under normal circumstances finding callback pointers in a remote process memory is quite hard and noisy (lots of <em>ReadProcessMemory<\/em> calls, possible disassembling). Unless of course there is an interface we can use to surgically target some specific callback (e.g. using documented <a href=\"https:\/\/www.hexacorn.com\/blog\/2019\/04\/23\/wordwarper-new-code-injection-trick\/\">windows messages<\/a>, or <a href=\"https:\/\/www.hexacorn.com\/blog\/2017\/10\/26\/propagate-a-new-code-injection-trick\/\">SetProp<\/a> function). As far as I can tell there is no such interface in our case. <\/p>\n\n\n\n<p>I found a surrogate solution that we can try to exploit though. <\/p>\n\n\n\n<p>When I looked at references to the callback function (which I named <em>fnMemAlloc<\/em> on the listing above) I discovered a exported function called <em>DnsApiHeapReset<\/em>. It takes 3 arguments and each of them is &#8230; a callback replacement:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"733\" height=\"464\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/dns_mem_callbacks.png\" alt=\"\" class=\"wp-image-6444\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/dns_mem_callbacks.png 733w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/dns_mem_callbacks-300x190.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/06\/dns_mem_callbacks-80x50.png 80w\" sizes=\"(max-width: 733px) 100vw, 733px\" \/><\/figure>\n\n\n\n<p>I quickly analyzed each callback&#8217;s role and they just are 3 basic\/core memory allocation\/reallocation\/release primitives. <\/p>\n\n\n\n<p>So&#8230;<\/p>\n\n\n\n<p>If we can locate the address of <em>dnsapi.dll<\/em> in a remote process (easy), find the address of exported <em>DnsApiHeapReset<\/em> function (easy), then with a basic parsing of its code we can discover the address of each callback (also easy). Then, with a single, surgical <em>WriteProcessMemory<\/em> call we can modify any of them. <\/p>\n\n\n\n<p>This is not a new code injection trick. It&#8217;s just one way to execute code without engaging remote threads, APCs, windows hooks, side-loading, process hollowing, patching API code (e.g. NtClose), etc.. <\/p>\n\n\n\n<p>There are of course tons of other callbacks like this, but finding their exact location without any point of reference is hard. Or&#8230; not really. Just think of all the Windows Procedures &#8211; all of them are callbacks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I looked at Sysmon v10 and its support for logging DNS queries. It&#8217;s a pretty cool feature that intercepts all the DNS requests on a monitored host, and if possible, maps them to the process name making that request. &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2019\/06\/12\/code-execution-via-surgical-callback-overwrites-e-g-dns-memory-functions\/\">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":[57,82],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/6441"}],"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=6441"}],"version-history":[{"count":2,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/6441\/revisions"}],"predecessor-version":[{"id":6447,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/6441\/revisions\/6447"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=6441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=6441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=6441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}