
{"id":1650,"date":"2015-07-03T23:25:51","date_gmt":"2015-07-04T03:25:51","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1650"},"modified":"2017-09-20T23:31:57","modified_gmt":"2017-09-21T03:31:57","slug":"win32-who-loads-the-library-that-implements-loadlibrary","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1650","title":{"rendered":"Win32: Who loads the library that implements LoadLibrary?"},"content":{"rendered":"<p>I was sitting in a <a href=\"http:\/\/www.dairyqueen.com\/\">Dairy Queen<\/a> this afternoon thinking about random stuff and suddenly this question hit me: <font color=\"red\">who loads the library that implements <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms684175%28v=vs.85%29.aspx\"><code>LoadLibrary<\/font><\/code><\/a>? Isn&#8217;t it some kind of chicken-and-egg situation?<\/p>\n<p><b>Short answer<\/b>: It&#8217;s <a href=\"https:\/\/en.wikipedia.org\/wiki\/Loader_%28computing%29\">the loader<\/a>.<\/p>\n<p><b>Longer answer<\/b>: Win32 executable contains a list of DLLs and functions it needs to import. When creating a running process, the loader locates these DLLs, brings them into memory, finds requested functions, and puts their addresses in a well known place. This is done before passing control to the executable&#8217;s entry point. From the executable&#8217;s point of view when it starts running, the DLLs are already there. If the process wishes to load more DLLs at a later stage, it includes <code>kernel32.dll!LoadLibrary<\/code> in the list of imports and calls this function when necessary.<\/p>\n<p><b>Really long answer<\/b>. <a href=\"http:\/\/download.microsoft.com\/download\/e\/b\/a\/eba1050f-a31d-436b-9281-92cdfeae4b45\/pecoff.doc\">Windows executable format specification (.DOC file)<\/a> defines an Import Directory Table (section 6.4.1), which is a linked list of DLL names. For each imported DLL there is an array of function names called Import Lookup Table (section 6.4.2) and array of their addresses called Import Address Table (section 6.4.4). See also a <a href=\"https:\/\/code.google.com\/p\/corkami\/wiki\/PE101?show=content\">pretty picture from Google Code (PDF)<\/a>. When the operating system loads the executable file into memory, it scans the Import Directory Table, loads requested DLLs (and their imports, recursively), finds requested names inside those DLLs and puts their addresses in the Import Address Tables. This code is baked into the loader and does not invoke <code>LoadLibrary<\/code>.<\/p>\n<p>To use <code>LoadLibrary<\/code>, the executable needs to have an Import Directory Table entry for <code>kernel32.dll<\/code>, and then an entry for <code>LoadLibrary<\/code> in its Import Lookup Table. The loader will kindly put <code>LoadLibrary<\/code>&#8216;s address in the corresponding entry of the Import Address Table.<\/p>\n<p>However, if you <i>inject<\/i> external code into the process (for the purpose of monitoring, debugging, or some malicious activity), that code will have harder time to find <code>LoadLibrary<\/code>, since it doesn&#8217;t know in advance which entry in Import Address Table contains <code>LoadLibrary<\/code>&#8216;s address. In fact, <code>LoadLibrary<\/code> may not be in that table at all. Fortunately, <code>kernel32.dll<\/code> is loaded into every Win32 process, and <code>LoadLibrary<\/code> is guaranteed to be inside it, so you only need to find where it is. Your first task is to determine the base address of <code>kernel32.dll<\/code>. Apparently, on versions of Windows prior to Windows 7 it is the second DLL in the Import Directory Table, but on Windows 7 and later it is the third. A number of articles on the Internet describe how to implement the look up in a portable manner, e.g. <a href=\"http:\/\/www.ragestorm.net\/blogs\/?p=369\">http:\/\/www.ragestorm.net\/blogs\/?p=369<\/a>. Many mention &#8220;<a href=\"http:\/\/skypher.com\/index.php\/2009\/07\/22\/shellcode-finding-kernel32-in-windows-7\/\">skypher.com<\/a>&#8220;, but that link is now dead. Once you&#8217;ve found the location of <code>kernel32.dll<\/code>, you&#8217;ll have to traverse its &#8220;export&#8221; tables to find <code>LoadLibrary<\/code>. This is what system call <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms683212(v=vs.85).aspx\"><code>GetProcAddress<\/code><\/a> would do, but your injected code doesn&#8217;t yet know where to find <code>GetProcAddress<\/code>. Fear not, there is <a href=\"https:\/\/sites.google.com\/site\/x64lab\/home\/notes-on-x64-windows-gui-programming\/customizing-the-getprocaddress\">a customized implementation of <code>GetProcAddress<\/code><\/a>, but use it at your own risk.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was sitting in a Dairy Queen this afternoon thinking about random stuff and suddenly this question hit me: who loads the library that implements LoadLibrary? Isn&#8217;t it some kind <a href=\"https:\/\/ikriv.com\/blog\/?p=1650\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4,18],"tags":[],"class_list":["entry","author-ikriv","post-1650","post","type-post","status-publish","format-standard","category-hack","category-win32"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1650"}],"version-history":[{"count":1,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1650\/revisions"}],"predecessor-version":[{"id":2436,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1650\/revisions\/2436"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}