2

The following code in a batch file which gets the password from user Thru a HTA dialog box. It works fine. I want to pass a variable value !user[%%a]! to be displayed inside the pop up HTA dialog box so I can see Enter password for User ID: "BATCH FILE USER VARIABLE" in this window : enter image description here

How can I do that?

<!-- :
@setlocal enableextensions enabledelayedexpansion
:: PasswordSubmitter.bat
@echo off
set user[1]=me1
set user[2]=me2
set user[3]=me3


for /l %%a in (1,1,3)  do (
    set counter=%%a
    for /f "tokens=* delims=" %%p in ('mshta.exe "%~f0"') do (
        set pass[!counter!]=%%p
    )
    echo Password for User-!user[%%a]! is "!pass[%%a]!"
)
endlocal
exit /b


<html>
<HEAD><title>Password submitter</title>
<HTA:APPLICATION INNERBORDER="no" SYSMENU="no" SCROLL="no" >
   <style type="text/css">
   body {
      color: white;
      background: black;
      font-family: "Calibri", monospace;
   }
   </style>
</HEAD>
<body>
    <p>Enter password for User ID</p>
    <script language='javascript' >
    window.resizeTo(400,200);
        function pipePass() {
            var pass=document.getElementById('pass').value;
            var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
            close(fso.Write(pass));

    }
</script>

<input type='password' name='pass' size='16'></input>
<hr>
<button onclick='pipePass()'>Submit</button>

</body>

</html>
Abhi
  • 177
  • 1
  • 3
  • 16

2 Answers2

5

I'm always happy to see my scripts used :). Try this modification:

<!-- :
@setlocal enableextensions enabledelayedexpansion
:: PasswordSubmitter.bat
@echo off
set user[1]=me1
set user[2]=me2
set user[3]=me3


for /l %%a in (1,1,3)  do (
    set counter=%%a
    for /f "tokens=* delims=" %%p in (' echo %%user[!counter!]%%^|mshta.exe "%~f0"') do (
        set pass[!counter!]=%%p
    )
    echo Password for User-!user[%%a]! is "!pass[%%a]!"
)
endlocal
exit /b


<html>
<HEAD><title>Password submitter</title>
<HTA:APPLICATION INNERBORDER="no" SYSMENU="no" SCROLL="no" >
   <style type="text/css">
   body {
      color: white;
      background: black;
      font-family: "Calibri", monospace;
   }
   </style>
</HEAD>
<body>
    <p>Enter password for {User ID}</p>
    <script language='javascript' >
    window.resizeTo(400,200);
    //var sh = new ActiveXObject( 'WScript.Shell' );
    var input= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0);
    var user=input.ReadLine();
    document.body.innerHTML = document.body.innerHTML.replace('{User ID}', user);
    function pipePass() {
        var pass=document.getElementById('pass').value;
        var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
        close(fso.Write(pass));

    }
</script>

<input type='password' name='pass' size='16'></input>
<hr>
<button onclick='pipePass()'>Submit</button>

</body>

</html>
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Hi Thanks for the former HTA script :) . Whe I run the modified script you mentioned I get it as : "Enter password for user[1] " then "Enter password for user[2]" and so on, I want to display the value corresponding to the user[2] which is "me2" . Secondly if at all possible can you explain it a bit, I'm unable to follow through how the current User value is passed in the HTA – Abhi Dec 04 '17 at 12:05
  • 1
    got it . Edited variable user[!counter!] to !user[%%a]! of the "in" of second 'for loop'. Thanks a lot – Abhi Dec 04 '17 at 12:18
  • @Abhi - I've edited the answer. To pass arguments to the HTA app I've used `ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0)` which gets the input passed through the pipe. I couldn't find a way to pass command line arguments. Now in the the for loop double expanded variable is used - `echo %%user[!counter!]%%` – npocmaka Dec 04 '17 at 12:21
  • Thank you. Works fine now! :) – Abhi Dec 04 '17 at 12:26
  • Hi I converted this batch file to exe using Bat_To_Exe_Converter' the HTA application password window doesn't work properly. The input field in the form doesn't appear and the text also is not what appears in the bat file execution. Why is it so? what can I do? – Abhi Dec 13 '17 at 10:33
  • @Abhi - What converter have you used? Most probably I won't be able to help you with this ,as I have no idea how the converter works. Though you can take a look at this - https://stackoverflow.com/questions/28174386/how-can-a-bat-file-be-converted-to-exe-without-third-party-tools – npocmaka Dec 13 '17 at 11:46
  • www.f2ko.de/en/b2e.php this one – Abhi Dec 13 '17 at 11:52
  • I created a post her to explain the same in more details here:https://stackoverflow.com/questions/47793813/obfuscating-batchhta-hybrid-script – Abhi Dec 13 '17 at 13:08
  • @Abhi - I've tested with both scripts posted [**here**](https://stackoverflow.com/questions/28174386) - with both the hta application works fine. – npocmaka Dec 13 '17 at 17:17
3

Here's a Microsoft example of using the commandline property of the hta application object.

<HTML>
<HEAD>
   <TITLE>Scripting the HTA:APPLICATION Tag</TITLE>
   <HTA:APPLICATION
      ID="oHTA"
   >   <SCRIPT LANGUAGE="VBScript">
      Option Explicit      Dim cmdLineArray
      Dim strHello      ' This code assumes that you have no spaces in 
      ' the path to Hello.hta.  (In other words, this code
      ' splits the command line by spaces and assumes
      ' that your name is the second word.)
      '
      cmdLineArray = Split(oHTA.commandLine)
      strHello = "Hello " & cmdLineArray(1) & ", " _
               & "How are you?"      MsgBox strHello
   </SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

Writing HTML Applications for Internet Explorer 5.0, Scott Roberts, MSDN Library 2001.

ACatInLove
  • 530
  • 2
  • 5
  • this works but `` is need right after the opening of the `html` tag.If there's internet explorer 10 or 11 some hta tags will not work, so you need to force it to ie 9 – npocmaka Dec 04 '17 at 14:59