{"id":233,"date":"2009-10-12T23:14:50","date_gmt":"2009-10-12T17:44:50","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=233"},"modified":"2011-04-12T23:15:09","modified_gmt":"2011-04-12T17:45:09","slug":"how-to-embed-an-exe-inside-another-exe-as-a-resource-and-then-launch-it","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2009\/10\/12\/how-to-embed-an-exe-inside-another-exe-as-a-resource-and-then-launch-it\/","title":{"rendered":"How to embed an exe inside another exe as a resource and then launch it"},"content":{"rendered":"<div style=\"text-align: justify; font-family: Comic Sans MS;\">While working on a utility project today, I stumbled upon wanting to embed an executable inside another executable. Sounds fun doesn&#8217;t it? And what is even more fun is to be able to launch the embedded exe!<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">Basically, here&#8217;s how it works. You embed <span style=\"color: rgb(0, 0, 128); font-family: Verdana;\">Foo.exe<\/span> inside <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Bar.exe<\/span>. And by embed I mean, add <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Foo.exe<\/span> as a resource in <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Bar.exe<\/span>. And then from <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Bar.exe&#8217;s<\/span> code, you can launch <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Foo.exe<\/span> using <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">CreateProcess()<\/span>.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">So before answering the &quot;Why?&quot; lets answer the &quot;How?&quot;<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">Rename <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Foo.exe<\/span> to <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Foo.txt<\/span>. We do this just to be safe and to prevent the resource compiler (manager) from throwing unwanted errors. Now add <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Foo.txt<\/span> as a normal resource in <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Bar.exe<\/span>. Create an entry in <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Bar.exe&#8217;s<\/span> resource script as below:<\/div>\n<p><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">IDR_FOO &nbsp; &nbsp; RCDATA&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &quot;Foo.txt&quot;<\/span><\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">And of course, you need to <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">#define IDR_FOO<\/span> in the resource header file. Just make sure its a unique value.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">The steps are:<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">1) From within <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Bar.exe&#8217;s<\/span> code, get a pointer to the first byte of <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Foo.txt<\/span><\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">2) You should know the size of <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Foo.txt<\/span> in bytes.<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">3) Using the pointer copy that many bytes into a separate file. (&quot;<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">\\\\Voila.exe<\/span>&quot;)<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">4) Call <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">CreateProcess()<\/span> on &quot;<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">\\\\Voila.exe<\/span>&quot;<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">5) And <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">voila!<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">Let&#8217;s dive into the code: (from the entry point of <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">Bar.exe<\/span>)<\/div>\n<p><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; HRSRC hrsrc = NULL;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; HGLOBAL hGlbl = NULL;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; BYTE *pExeResource = NULL;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; HANDLE hFile = INVALID_HANDLE_VALUE;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; DWORD size = 7168;<span style=\"color: rgb(51, 153, 102);\">\/\/hardcoding the size of the exe resource (in bytes)<\/span><\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; hrsrc = FindResource(hInstance, (LPCWSTR)IDR_FOO, RT_RCDATA);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; if (hrsrc == NULL)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return FALSE;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; hGlbl = LoadResource(hInstance, hrsrc);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; if (hGlbl == NULL)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return FALSE;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; pExeResource = (BYTE*)LockResource(hGlbl);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; if (pExeResource == NULL)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return FALSE;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; <\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; hFile = CreateFile(L&quot;\\\\Voila.exe&quot;, GENERIC_WRITE|GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; if (hFile != INVALID_HANDLE_VALUE)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; {<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DWORD bytesWritten = 0;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WriteFile(hFile, <\/span><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">pExeResource<\/span><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">, size, &amp;bytesWritten, NULL);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n&nbsp; &nbsp; &nbsp;<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\"> &nbsp;&nbsp; CloseHandle(hFile);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; }<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; int ret = CreateProcess(L&quot;\\\\Voila.exe&quot;, NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &amp;pi);<\/span><\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">First, we find the resource using its resource identifier and then load it. Next we use <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">LockResource()<\/span> to get the pointer to the first byte of the resource data, which in this case would be the executable code. One downside, if you may say so, is that you need to know the exact size of the executable beforehand. Of course its easy to find out and I think its not a problem to hardcode because the size of the embedded executable won&#8217;t change. But if you still insist, then you can read it from the registry or a file or something.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">Once you get the pointer, just copy all the bytes into another file, using <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">WriteFile()<\/span> API.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">And finally do a <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">CreateProcess()<\/span> on the file you just created.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">If you happen to know any alternate ways of doing this, please leave a message.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">And coming to the important question of&nbsp; &quot;<span style=\"color: rgb(128, 0, 0);\">Why would any sane person want to embed an exe within an exe?<\/span>&quot; Well, you will have to wait till the <a href=\"http:\/\/geekswithblogs.net\/TechTwaddle\/archive\/2009\/10\/22\/applications-getting-device-information-embedded-exe-rapi-and-more.aspx\">next post<\/a> to find out (;<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">Aloha!<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">&nbsp;<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">Update:<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">I have changed the code above to copy all the 7168 bytes into &quot;Voila.exe&quot; in one go, instead of copying byte after byte. And just to be clear this is not a production code, it is just to demonstrate what can be done. Of course, creating the file &quot;Voila.exe&quot; in the root folder is not ideal and it may fail on many devices, and you also need to clean up by deleting the exe file, which can be done using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa916021.aspx\">DeleteFile()<\/a> API. Ideally, &quot;voila.exe&quot; should be created not in the root folder but instead using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa931257.aspx\">SHGetSpecialFolderPath()<\/a> API passing for e.g. <span style=\"color: rgb(0, 0, 128);\"><span style=\"font-family: Verdana;\"><span xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"><span id=\"src16\" class=\"srcSentence\">CSIDL_APPDATA<\/span><\/span><\/span><\/span><span xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"><span><span id=\"src16\" class=\"srcSentence\">. <\/span><\/span><\/span>I left out all these details because I thought they were trivial for this post.<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>While working on a utility project today, I stumbled upon wanting to embed an executable inside another executable. Sounds fun doesn&#8217;t it? And what is even more fun is to be able to launch the embedded exe! &nbsp; Basically, here&#8217;s how it works. You embed Foo.exe inside Bar.exe. And by embed I mean, add Foo.exe &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2009\/10\/12\/how-to-embed-an-exe-inside-another-exe-as-a-resource-and-then-launch-it\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to embed an exe inside another exe as a resource and then launch it<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-3L","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/233"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=233"}],"version-history":[{"count":1,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":234,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions\/234"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}