{"id":3282,"date":"2015-10-15T16:54:10","date_gmt":"2015-10-15T16:54:10","guid":{"rendered":"http:\/\/www.hexacorn.com\/blog\/?p=3282"},"modified":"2015-10-15T17:00:04","modified_gmt":"2015-10-15T17:00:04","slug":"the-story-of-a-failed-research","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2015\/10\/15\/the-story-of-a-failed-research\/","title":{"rendered":"The story of a failed research :)"},"content":{"rendered":"<p>Most of the stuff I publish here are stories of a successful research. Today, for a change, I will talk about a little failed project. Little, because I didn&#8217;t spend too much time on it, failed &#8211; because it did fail \ud83d\ude42<\/p>\n<p>I think it is important to talk about such projects as well, because not only we do fail many times in our research activities + who knows, maybe someone will \u00a0come up with a better, more clever idea than me and make it actually successful.<\/p>\n<p>In the <a href=\"https:\/\/www.hexacorn.com\/blog\/2015\/01\/06\/beyond-good-ol-run-key-part-22\/\">Beyond good ol\u2019 Run key, Part 22<\/a>\u00a0 series. I talked about perl2exe and how the executables created using this tool can load some\u00a0other arbitrary perl scripts. So, when I\u00a0found out about it I thought\u00a0how cool would it be to access the hidden script in a programmatic way and just dump it (assuming that I could use the fact I can load an arbitrary code as a backdoor &amp; that I can somehow find ways to access the source code of the perl script).<\/p>\n<p>Of course, if you ever dumped the hidden script from perl2exe you may wonder why would I even bother to try &#8211; knowing that the script can be easily dumped using typical reversing tricks. Well, it was simply an appealing idea to me to be able to do it in a neat way. In the end I couldn&#8217;t find a way to do it. The only consolation is that I was able to at least print the names of the routines used in the code, and enable various debugging messages.<\/p>\n<p>In\u00a0the aforementioned post I mentioned you can load an arbitrary perl code when the perl2exe-compiled script is loaded.<\/p>\n<p>For example, we can create a\u00a0<em>(null)\\sitecustomize.pl<\/em> script that will be executed before the main script.<\/p>\n<p>If it contains the code<\/p>\n<pre style=\"padding-left: 30px;\">print \"foobar\\n\";<\/pre>\n<p>it will load like this (running the old wmi.exe perl2exed script here)<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3291\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar1.png\" alt=\"foobar1\" width=\"271\" height=\"106\" \/><\/a><\/p>\n<p>Running the code before the script is one thing, it may be handy to run code at the end of the execution as well &#8211; at that stage we will be able to enumerate some properties that are not available\u00a0until the main script actually executes.<\/p>\n<p>We can add the END section to our backdoor:<\/p>\n<pre style=\"padding-left: 30px;\">print \"foobar - start\\n\";<\/pre>\n<pre style=\"padding-left: 30px;\">sub END\r\n{\r\n print \"foobar - end\\n\";\r\n}<\/pre>\n<p>that will do the following:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar1.png\"><br \/>\n<\/a> <a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-3292\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar2.png\" alt=\"foobar2\" width=\"193\" height=\"69\" \/><\/a><\/p>\n<p>To dump \u00a0the code of the perl script one needs to employ the\u00a0B::Deparse module.<\/p>\n<p>One can write something like this:<\/p>\n<pre style=\"padding-left: 30px;\">use B::Deparse;\r\nmy $deparser = B::Deparse-&gt;new;<\/pre>\n<pre style=\"padding-left: 30px;\">print $deparser-&gt;coderef2text(\\&amp;foobar);\r\nsub foobar\r\n{\r\n print \"Hello world\\n\";\r\n}<\/pre>\n<p>and when executed it will print the source code of the <em>foobar<\/em> function :<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3293\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar3.png\" alt=\"foobar3\" width=\"230\" height=\"119\" \/><\/a><\/p>\n<p>A more complex script:<\/p>\n<pre style=\"padding-left: 30px;\">use warnings;\r\nuse B::Deparse;\r\n\r\nprintit (\\%main::, 0);\r\n\r\nsub printit\r\n{\r\n  my $hash1 = shift;my %hash=%{$hash1};\r\n  my $n     = shift;\r\n\r\n  foreach my $k ( keys %hash )\r\n  {\r\n      #print \"-\" x $n;\r\n\r\n      if (defined $h{$k})\r\n         {\r\n           print \"(hash)\";\r\n           printit (%{$h{$k}}, $n+1);\r\n         }\r\n      if (defined &amp;{$k})\r\n      {\r\n        my $deparser = B::Deparse-&gt;new;\r\n        print \"$k\\n\";\r\n        print $deparser-&gt;coderef2text(\\&amp;{$k});\r\n      }\r\n\r\n   }\r\n}\r\n\r\nsub foobar\r\n{\r\n  print \"Hello world\\n\";\r\n}\r\n<\/pre>\n<p>will produce\u00a0something\u00a0like this:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar4.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-3295\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar4-300x206.png\" alt=\"foobar4\" width=\"300\" height=\"206\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar4-300x206.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar4.png 415w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>It is kinda getting close to a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Quine_%28computing%29\">quine<\/a> and what we need, but&#8230;<\/p>\n<p>But unfortunately, B::Deparse is\u00a0not available inside the compile perl2exe-compiled binary. It also doesn&#8217;t seem to be possible to include this module from a\u00a0separate perl\u00a0repository. One can change the content of the INC environment variable (responsible for inclusion of modules) with a push f.ex.:<\/p>\n<pre style=\"padding-left: 30px;\">push (@INC, \"C:\/Perl\/site\/lib\");\r\npush (@INC,\"C:\/Perl\/lib\" );<\/pre>\n<p>at the top of our backdoor, but\u00a0for some reason the B::Deparse doesn&#8217;t load (the engine finds the files, but you can&#8217;t use its\u00a0functionality\u00a0. Perhaps the appropriate version of perl (same as in perl2exe used to compile the script) may work &#8211; this is one of the potential paths\u00a0that I have not explored. Perhaps other ways to load a library may work as well.<\/p>\n<p>At this stage I kinda gave up, but still played a bit with some environmental variables.<\/p>\n<p>For example, adding these to the top of the backdoor (so they are executed before the main script executes):<\/p>\n<pre style=\"padding-left: 30px;\">$ENV{PERL_DL_DEBUG}=1;\r\n$ENV{DEBUG_TIE_REGISTRY}=1;<\/pre>\n<p>will enable a lot of debugging messages.<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar5.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-3296\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar5-300x109.png\" alt=\"foobar5\" width=\"300\" height=\"109\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar5-300x109.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar5.png 646w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>and also related to Registry:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar6.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-3297\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar6-300x113.png\" alt=\"foobar6\" width=\"300\" height=\"113\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar6-300x113.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar6.png 643w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Finally, adding this bit to the END section:<\/p>\n<pre style=\"padding-left: 30px;\"> foreach my $k ( keys %main:: )\r\n {\r\n   print \"FUNCTION: $k\\n\" if (defined &amp;{$k});\r\n }<\/pre>\n<p>so that the final script looks like<\/p>\n<pre style=\"padding-left: 30px;\">print \"foobar - start\\n\";\r\n\r\n#$ENV{PERL_DL_DEBUG}=1;\r\n#$ENV{DEBUG_TIE_REGISTRY}=1;\r\n\r\nsub END\r\n{\r\n  print \"foobar - end\\n\";\r\n \r\n foreach my $k ( keys %main:: )\r\n  {\r\n    print \"FUNCTION: $k\\n\" if (defined &amp;{$k});\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>will allow us to print all the routines defined in the main script:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar7.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3298\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2015\/10\/foobar7.png\" alt=\"foobar7\" width=\"211\" height=\"43\" \/><\/a><\/p>\n<p><em>GetSoftwareInfo<\/em> &amp;\u00a0<em>GetHardwareInfo<\/em> are indeed functions defined inside the wmi.exe &#8211; these may be still handy pieces of information if you want to quickly narrow down where the source of the original script is in memory (if you do a memory dump).<\/p>\n<p>At this stage it is not a very useful piece of research. One could potentially employ this in sandboxing to produce a very detailed log, but \u00a0dumping the actual source code would be a much better choice.<\/p>\n<p>Was it a wasted time? Hmm probably.<\/p>\n<p>If you know, or come up with an idea how to print more useful info, or maybe even a source code &#8211; please let me know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of the stuff I publish here are stories of a successful research. Today, for a change, I will talk about a little failed project. Little, because I didn&#8217;t spend too much time on it, failed &#8211; because it did &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2015\/10\/15\/the-story-of-a-failed-research\/\">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\/3282"}],"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=3282"}],"version-history":[{"count":7,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/3282\/revisions"}],"predecessor-version":[{"id":3301,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/3282\/revisions\/3301"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=3282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=3282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=3282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}