{"id":67793,"date":"2006-03-09T16:06:00","date_gmt":"2006-03-09T16:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/09\/how-can-i-encode-scripts-within-an-hta\/"},"modified":"2006-03-09T16:06:00","modified_gmt":"2006-03-09T16:06:00","slug":"how-can-i-encode-scripts-within-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-encode-scripts-within-an-hta\/","title":{"rendered":"How Can I Encode Scripts Within an HTA?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I encode scripts within an HTA?<BR><BR>&#8212; LL<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, LL. You know, when we first read this question we thought, \u201cWhat\u2019s the big deal? Can\u2019t you encode scripts in an HTML Application (HTA) the same way you encode scripts in an HTML file?\u201d Much to our surprise, it appears as though you <I>can\u2019t<\/I> encode scripts in an HTA; each time we tried we got back an error message similar to this:<\/P><PRE class=\"codeSample\">Scripting encoder object (&#8220;Scripting.Encoder&#8221;) failed on C:\\Scripts\\test.hta\n<\/PRE>\n<P>To be honest, we never found a way to encode scripts within an HTA. But that\u2019s OK, because we <I>did<\/I> find a sneaky way to work around the problem. And we\u2019ll show you that in just a moment.<\/P>\n<P>Before we do that, however, we need to take a second and explain what we mean by encoding scripts. Microsoft has a utility &#8211; aptly named the <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=e7877f67-c447-4873-b1b0-21f0626a6329&amp;DisplayLang=en\" target=\"_blank\"><B>Script Encoder<\/B><\/A> &#8211; that can \u201cobfuscate\u201d your script code. For example, suppose you have a script that looks like this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp;  &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>   Set colOperatingSystems = objWMIService.ExecQuery(&#8220;Select * from Win32_OperatingSystem&#8221;)<\/p>\n<p>For Each objOperatingSystem in colOperatingSystems\n    Msgbox objOperatingSystem.Caption &amp; &#8221; &#8221; &amp;   objOperatingSystem.Version\nNext\n<\/PRE>\n<P>Let\u2019s say that, for some reason, you\u2019d prefer that people not be able to read the script code. That\u2019s a problem: after all, anyone who has permission to run a script is going to have permission to open the script up in Notepad and gawk at the code all they want. But here\u2019s what that same script looks like after the Script Encoder gets done with it:<\/P><PRE class=\"codeSample\">#@~^8AEAAA==@#@&amp;@#@&amp;@#@&amp;,PP,?!8PV+OrU.n.kkW@#@&amp;,PP,~~PkYMZG:2;D+.P{~JcJ@#@&amp;,PP~~,P~P,~U+O,W(Ltq\nUnD7k^+,&#8217;~!Yr(%+1Y`rArxsosYd)wwrP&#8217;PkOD;Wh2!Y+.~LP~J&#8217;.KWO&#8217;mb:\\yJ*@#@&amp;@#@&amp;~P,P~~,PP,~?YP1Gsra+MlOkULUXdYh\/,&#8217;~G(\nL\\(U+.\\b^R3X+1pEDH~{@#@&amp;~P,P~~,PP,~P,PPvEj+^+1Y~e~W\nMWhPqrx2 m6a+DCObxL?HdD+hr#@#@&amp;@#@&amp;P,~P,P~P,P~oKDPACm4PW(%6wDmYr\nxLjH\/O+s~kP^G^rwn.mYrxTjH\/O:k@#@&amp;,P,~P,P~P,P~~,PHkL4K6PK8%ra+M\nlOkULUXdYhR;l2ObWx~&#8217;,J~J,&#8217;,{@#@&amp;P,PP,P,~P,P~P,P~~,PW(%ra+DmOrxT?H\/O+h \nj+.\/bGx@#@&amp;~~,PP~~,P~PgnXY@#@&amp;P,PPAx9~?!4@#@&amp;QYQAAA==^#~@\n<\/PRE>\n<P>The script still works, it\u2019s just a bit harder for anyone to glance at the code and plagiarize your work. (And you\u2019re right: even at that it <I>does<\/I> make more sense than most of the jokes used in <I>Hey, Scripting Guy!<\/I>)<\/P>\n<P>Now, the important thing to keep in mind is that the script is simply encoded (or obfuscated); it is definitely <I>not<\/I> encrypted. What does that mean? That means the encoder will hide your script from most people; however, a truly determined hacker &#8211; armed with a knowledge of codes or armed with a utility downloaded from the Internet &#8211; could crack the code. Among other things, that means that you should never do something like \u201chide\u201d an Administrator password in a script and assume that the Script Encoder will keep it safe from prying eyes. It won\u2019t. It\u2019s an encoder, not an encrypter, and there\u2019s definitely a difference.<\/P>\n<P>But what about the problem with encoding scripts in an HTA? Well, let\u2019s assume you have an HTA that looks like this (we\u2019ll also assume that you\u2019ve already downloaded and installed the Script Encoder):<\/P><PRE class=\"codeSample\">&lt;head&gt;\n&lt;title&gt;Operating System Version&lt;\/title&gt;\n&lt;HTA:APPLICATION \n     APPLICATIONNAME=&#8221;Operating System Version&#8221;\n     SCROLL=&#8221;yes&#8221;\n     SINGLEINSTANCE=&#8221;yes&#8221;\n&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;script language=&#8221;VBScript&#8221;&gt;<\/p>\n<p>    Sub GetOSVersion\n       strComputer = &#8220;.&#8221;\n           Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp;  &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>           Set colOperatingSystems = objWMIService.ExecQuery _\n               (&#8220;Select * from Win32_OperatingSystem&#8221;)<\/p>\n<p>           For Each objOperatingSystem in colOperatingSystems\n               Msgbox objOperatingSystem.Caption &amp; &#8221; &#8221; &amp; _\n                   objOperatingSystem.Version\n           Next\n    End Sub\n&lt;\/script&gt;<\/p>\n<p>&lt;body&gt;\n&lt;input type=&#8221;button&#8221; value=&#8221;Operating System&#8221; name=&#8221;run_button&#8221;  onClick=&#8221;GetOSVersion&#8221;&gt;<\/p>\n<p>&lt;\/body&gt;\n<\/PRE>\n<P>If you want to encode the VBScript portion of this HTA the first thing you need to do is use the <B>&#8216;**Start Encode**<\/B> tag to mark the beginning of the section to be encoded. In other words, your &lt;Script&gt; tag needs to look like this, with &#8216;**Start Encode** coming right before the very first line of VBScript code:<\/P><PRE class=\"codeSample\">&lt;script language=&#8221;VBScript&#8221;&gt;<\/p>\n<p>&#8216;**Start Encode**<\/p>\n<p>    Sub GetOSVersion\n       strComputer = &#8220;.&#8221;\n           Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp;  &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>           Set colOperatingSystems = objWMIService.ExecQuery _\n               (&#8220;Select * from Win32_OperatingSystem&#8221;)<\/p>\n<p>           For Each objOperatingSystem in colOperatingSystems\n               Msgbox objOperatingSystem.Caption &amp; &#8221; &#8221; &amp; _\n                   objOperatingSystem.Version\n           Next\n    End Sub\n&lt;\/script&gt;\n<\/PRE>\n<P>In theory, you should then be able to encode the script by running Screnc.exe and passing two parameters, the name of the existing HTA file (Test.hta) and the name to be given to the encoded file (Encoded.hta):<\/P><PRE class=\"codeSample\">C:\\Scripts&gt;screnc test.hta encoded.hta\n<\/PRE>\n<P>Unfortunately, though, this doesn\u2019t work.<\/P>\n<P><FONT><STRONG>Update Note: You&nbsp;do not actually need to rename the file.<\/STRONG> &nbsp;<\/P>\n<P class=\"MsoNormal\"><SPAN lang=\"EN-GB\"><FONT size=\"3\"><FONT face=\"Calibri\">All you need to do is to use the \/e flag to tell the encoder how to handle file types it doesn\u2019t recognise natively, thus:<\/FONT><\/FONT><\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN lang=\"EN-GB\"><FONT size=\"3\" face=\"Calibri\">&nbsp;<\/FONT><\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN lang=\"EN-GB\"><FONT size=\"3\"><FONT face=\"Calibri\">Screnc \/e htm test.hta test.hte<\/FONT><\/FONT><\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN lang=\"EN-GB\"><FONT size=\"3\" face=\"Calibri\">&nbsp;<\/FONT><\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN lang=\"EN-GB\"><FONT size=\"3\"><FONT face=\"Calibri\">Or to do it in place (overwriting the original):<\/FONT><\/FONT><\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN lang=\"EN-GB\"><FONT size=\"3\" face=\"Calibri\">&nbsp;<\/FONT><\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN lang=\"EN-GB\"><FONT size=\"3\"><FONT face=\"Calibri\">Screnc \/f \/e htm test.hta<\/FONT><\/FONT><\/SPAN><\/P>\n<P class=\"MsoNormal\"><SPAN lang=\"EN-GB\"><FONT size=\"3\"><FONT face=\"Calibri\"><\/FONT><\/FONT><\/SPAN>&nbsp;<\/P>\n<P class=\"MsoNormal\"><SPAN lang=\"EN-GB\"><FONT size=\"3\"><FONT face=\"Calibri\">Thanks DL, for the update!&nbsp;<\/FONT><\/FONT><\/SPAN><\/P><\/FONT>\n<P>Why? Well, we aren\u2019t really sure. But we do know what <I>does<\/I> work. All we have to do is change the file extension on the existing file from .hta to .htm; in other words, we simply rename the file Test.htm. The Script Encoder can\u2019t handle HTA files, but it has no problems with HTML files. Now we can start the Script Encoder using syntax like this:<\/P><PRE class=\"codeSample\">C:\\Scripts&gt;screnc test.htm encoded.hta\n<\/PRE>\n<P>Are we sure that will work? Don\u2019t tell us you doubt the Scripting Guys! Here\u2019s what Encoded.hta looks like:<\/P><PRE class=\"codeSample\">&lt;head&gt;\n&lt;title&gt;Operating System Version&lt;\/title&gt;\n&lt;HTA:APPLICATION \n     APPLICATIONNAME=&#8221;Operating System Version&#8221;\n     SCROLL=&#8221;yes&#8221;\n     SINGLEINSTANCE=&#8221;yes&#8221;\n&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;script language=&#8221;VBScript.Encode&#8221;&gt;<\/p>\n<p>&#8216;**Start Encode**#@~^8AEAAA==@#@&amp;@#@&amp;@#@&amp;,PP,?!8PV+OrU.n.kkW@#@&amp;,PP,~~PkYMZG:2;D+.P{~JcJ@#@&amp;,PP~~,P~P,~U+O,W(Ltq\nUnD7k^+,&#8217;~!Yr(%+1Y`rArxsosYd)wwrP&#8217;PkOD;Wh2!Y+.~LP~J&#8217;.KWO&#8217;mb:\\yJ*@#@&amp;@#@&amp;~P,P~~,PP,~?YP1Gsra+MlOkULUXdYh\/,&#8217;~G(\nL\\(U+.\\b^R3X+1pEDH~{@#@&amp;~P,P~~,PP,~P,PPvEj+^+1Y~e~W\nMWhPqrx2 m6a+DCObxL?HdD+hr#@#@&amp;@#@&amp;P,~P,P~P,P~oKDPACm4PW(%6wDmYr\nxLjH\/O+s~kP^G^rwn.mYrxTjH\/O:k@#@&amp;,P,~P,P~P,P~~,PHkL4K6PK8%ra+M\nlOkULUXdYhR;l2ObWx~&#8217;,J~J,&#8217;,{@#@&amp;P,PP,P,~P,P~P,P~~,PW(%ra+DmOrxT?H\/O+h \nj+.\/bGx@#@&amp;~~,PP~~,P~PgnXY@#@&amp;P,PPAx9~?!4@#@&amp;QYQAAA==^#~@&lt;\/script&gt;<\/p>\n<p>&lt;body&gt;\n&lt;input type=&#8221;button&#8221; value=&#8221;Operating System&#8221; name=&#8221;run_button&#8221;  onClick=&#8221;GetOSVersion&#8221;&gt;<\/p>\n<p>&lt;\/body&gt;\n<\/PRE>\n<P>It\u2019s still a valid HTA; click the button and it will report back the name and version of the operating system installed on the local machine. But to anyone who tries looking at the code it will look like pure gibberish. (Needless to say, the Scripting Guys know gibberish better than anyone!)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I encode scripts within an HTA?&#8212; LL Hey, LL. You know, when we first read this question we thought, \u201cWhat\u2019s the big deal? Can\u2019t you encode scripts in an HTML Application (HTA) the same way you encode scripts in an HTML file?\u201d Much to our surprise, it appears as though [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-67793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I encode scripts within an HTA?&#8212; LL Hey, LL. You know, when we first read this question we thought, \u201cWhat\u2019s the big deal? Can\u2019t you encode scripts in an HTML Application (HTA) the same way you encode scripts in an HTML file?\u201d Much to our surprise, it appears as though [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67793"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67793\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}