{"id":3099,"date":"2015-06-23T16:51:49","date_gmt":"2015-06-23T16:51:49","guid":{"rendered":"http:\/\/www.hexacorn.com\/blog\/?p=3099"},"modified":"2015-06-23T16:55:51","modified_gmt":"2015-06-23T16:55:51","slug":"lame-tricks-with-ldrregisterdllnotification","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2015\/06\/23\/lame-tricks-with-ldrregisterdllnotification\/","title":{"rendered":"(lame) tricks with LdrRegisterDllNotification"},"content":{"rendered":"<p>It was <a href=\"https:\/\/twitter.com\/attrc\">Andrew<\/a> (thanks!) who brought <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd347461%28v=vs.85%29.aspx\">LdrRegisterDllNotification<\/a> to my attention by asking on twitter if anyone has seen any malware using it. I couldn&#8217;t find any malware, but the function itself triggered my interest.<\/p>\n<p>I quickly implemented a proof of concept. It shows how the callback that the API is registering could be utilized as a lame anti-* trick by malware that drops DLLs (in a case I am demoing), and maybe to do a few more tricky things (as I discuss below).<\/p>\n<p>Consider the first .exe &#8211; let&#8217;s call it &#8216;good.exe&#8217;.<\/p>\n<p>When executed, it drops a DLL called &#8216;foo.dll&#8217;.<\/p>\n<p>It then resolves its one and only export &#8216;Bar&#8217; and executes the function. The function shows a simple message box with a &#8216;Good&#8217; message:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/06\/good.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3100\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/06\/good.png\" alt=\"good\" width=\"154\" height=\"154\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/06\/good.png 154w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/06\/good-150x150.png 150w\" sizes=\"(max-width: 154px) 100vw, 154px\" \/><\/a>The second .exe is called &#8216;better.exe&#8217; (obviously \ud83d\ude42 ).<\/p>\n<p>It drops the very same &#8216;foo.dll&#8217;.<\/p>\n<p>Functionally, it is almost identical with &#8216;good.exe&#8217; as it is using the same source code. The only difference is that it registers the callback using a LdrRegisterDllNotification function. From now on, anytime a DLL is loaded the callback function takes control and is free to modify the content of the loaded DLL &#8216;on the fly&#8217;. As such, it can easily patch it and create a complete different memory image from the one we may expect by just looking at a static file &#8216;dropped&#8217; by the .exe.<\/p>\n<p>In my case the change is of a &#8216;visualame&#8217; type. It&#8217;s for us to observe it.<\/p>\n<p>The callback does only one thing &#8211; it checks if the string at a hardcoded offset inside the loaded DLL is equal to &#8216;Good&#8217; and swaps it with a new string &#8216;Evil&#8217;; therefore, running the &#8216;better.exe&#8217; will produce the following result:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/06\/evil.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3101\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/06\/evil.png\" alt=\"evil\" width=\"154\" height=\"154\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/06\/evil.png 154w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/06\/evil-150x150.png 150w\" sizes=\"(max-width: 154px) 100vw, 154px\" \/><\/a><\/p>\n<p>The example is intentionally simple, but there are many things that could be done here &#8211; the callback allows one to f.ex.<\/p>\n<ul>\n<li>disable unwanted DLLs (by f.ex. patching their code)<\/li>\n<li>detecting sandbox DLLs at a different stage (also use as a different enumeration method for loaded DLLs)<\/li>\n<li>swapping data and code of modules on the fly<\/li>\n<li>resolving API pointers earlier and potentially hooking \/ patching APIs used later in the code<\/li>\n<li>introducing simple, atomic, business logic changes to the executed code (hard to spot) f.ex. modify JNZ to JZ, NOPing some instructions, etc.<\/li>\n<li>etc.<\/li>\n<\/ul>\n<p>Both files can be <a href=\"https:\/\/www.hexacorn.com\/examples\/2015-06-24_LdrRegisterDllNotification.zip\">downloaded here<\/a>.<\/p>\n<p>Nothing ground breaking, but good to know about.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It was Andrew (thanks!) who brought LdrRegisterDllNotification to my attention by asking on twitter if anyone has seen any malware using it. I couldn&#8217;t find any malware, but the function itself triggered my interest. I quickly implemented a proof of &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2015\/06\/23\/lame-tricks-with-ldrregisterdllnotification\/\">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\/3099"}],"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=3099"}],"version-history":[{"count":3,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/3099\/revisions"}],"predecessor-version":[{"id":3104,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/3099\/revisions\/3104"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=3099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=3099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=3099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}