{"id":4315,"date":"2017-09-27T19:14:17","date_gmt":"2017-09-27T19:14:17","guid":{"rendered":"http:\/\/www.hexacorn.com\/blog\/?p=4315"},"modified":"2017-09-27T19:44:17","modified_gmt":"2017-09-27T19:44:17","slug":"beyond-good-ol-run-key-part-65","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2017\/09\/27\/beyond-good-ol-run-key-part-65\/","title":{"rendered":"Beyond good ol\u2019 Run key, Part 65"},"content":{"rendered":"<p>Looking for new ways to load code persistently I had a quick glance at Java. While it may not be present on all systems, it&#8217;s out there on at least 3 billions devices (so the ad claims).<\/p>\n<p>The first thing that caught my eyes were these nice command line options:<\/p>\n<ul>\n<li>-agentlib:&lt;agent-lib-name&gt;=&lt;options&gt;<\/li>\n<li>-agentpath:&lt;path-to-agent&gt;=&lt;options&gt;<\/li>\n<\/ul>\n<p>The agent library is just a DLL that needs to export an <em>Agent_OnLoad<\/em> function. I quickly prepared a test DLL with a dummy export and got it to load as shown below:<\/p>\n<ul>\n<li>java -agentlib:c:\\Test\\javaagent<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-4316\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent.png\" alt=\"\" width=\"500\" height=\"215\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent.png 613w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent-300x129.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<ul>\n<li>java -agentpath:c:\\Test\\javaagent.dll<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-4317\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent2.png\" alt=\"\" width=\"500\" height=\"199\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent2.png 661w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent2-300x119.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>So, the first takeaway is that if there is java.exe on the system, you can use it to load unsigned DLL, same way as <a href=\"https:\/\/www.hexacorn.com\/blog\/2017\/07\/31\/the-wizard-of-x-oppa-plugx-style\/\">xwizards<\/a>, and tones of other similar tricks from <a href=\"https:\/\/twitter.com\/search?q=%40subtee\">@subtee<\/a>.<\/p>\n<p>Having a way to load DLL is one thing, being able to load it persistently is another.<\/p>\n<p>This is where <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/platform\/jvmti\/jvmti.html#tooloptions\">JAVA_TOOL_OPTIONS<\/a> environment variable comes handy.<\/p>\n<p>Once you set it to f.ex.:<\/p>\n<pre>set JAVA_TOOL_OPTIONS=-agentpath:c:\\Test\\javaagent.dll<\/pre>\n<p>the library will be loaded anytime java.exe starts.<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-4318\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent3.png\" alt=\"\" width=\"500\" height=\"174\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent3.png 789w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent3-300x105.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2017\/09\/javaagent3-768x268.png 768w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Obviously, these errors shown on the screenshots is just me being lazy &#8211; my dummy library should return appropriate value for the java.exe to be happy about, and so it can actually continue the execution. Ignoring that, the second takeaway is that as long as you make the JAVA_TOOL_OPTIONS variable persistent f.ex. via the Registry:<\/p>\n<pre>HKCU\\Environment\\JAVA_TOOL_OPTIONS=\r\n-agentpath:c:\\Test\\javaagent.dll\r\n<\/pre>\n<p>&#8211; the library will be loaded anytime java.exe starts.<\/p>\n<p>The JAVA_TOOL_OPTIONS has a few undocumented sibling variables called _JAVA_OPTIONS and IBM_JAVA_OPTIONS as explained <a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-4971166\">here<\/a>. These can be leveraged as well. The Windows Java version I tested (jre1.8.0_144) refers to _JAVA_OPTIONS during the start-up, so you can add persistence via this Registry entry:<\/p>\n<pre>HKCU\\Environment\\_JAVA_OPTIONS=\r\n-agentpath:c:\\Test\\javaagent.dll<\/pre>\n<p>I have not confirmed the below, but I guess there are other options for toying around with persistence using Java. Looking at the CLI help we can see the following possible avenues for exploration:<\/p>\n<ul>\n<li>-javaagent:&lt;jarpath&gt;[=&lt;options&gt;] &#8211; load Java programming language agent, see java.lang.instrument<\/li>\n<li>-classpath &lt;class search path of directories and zip\/jar files&gt; -fA ; separated list of directories, JAR archives, and ZIP archives to search for class files.<\/li>\n<li>-Xbootclasspath:&lt;directories and zip\/jar files separated by ;&gt; &#8211; set search path for bootstrap classes and resources<\/li>\n<li>-Xbootclasspath\/a:&lt;directories and zip\/jar files separated by ;&gt; &#8211; append to end of bootstrap class path<\/li>\n<li>-Xbootclasspath\/p:&lt;directories and zip\/jar files separated by ;&gt; &#8211; prepend in front of bootstrap class path<\/li>\n<\/ul>\n<p>There is also another variable worth looking at: _ALT_JAVA_HOME_DIR. By changing it, you can manipulate the path that Java Virtual Machine uses while it is looking for Java Run-Time Environment. This may open some possibilities for companion virus-like persistence.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking for new ways to load code persistently I had a quick glance at Java. While it may not be present on all systems, it&#8217;s out there on at least 3 billions devices (so the ad claims). The first thing &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2017\/09\/27\/beyond-good-ol-run-key-part-65\/\">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":[43,35,15,19],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/4315"}],"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=4315"}],"version-history":[{"count":5,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/4315\/revisions"}],"predecessor-version":[{"id":4323,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/4315\/revisions\/4323"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=4315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=4315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=4315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}