Hide Input in Batch File
Asked Answered
E

5

7

I was wondering how I would go about Hiding the input from a 'set /p' command in a batch file.

set /p Password=What is your password?

We all know that inputting your password, you would be able to see it. How would I go 'bout hiding it ?

I tried conset.exe from here. And used:

conset /PH Password=What is your password?

And i get "Conset: Error setting variable" :(

Another idea I had, was to change the colour of the console window. But how could I change the colour on the same line? So that you could see the question, but not see the answer?

Any ideas from the pros?

Everyplace answered 2/5, 2011 at 1:31 Comment(2)
Possible duplicate of #287371Orthman
Possible duplicate of Can I mask an input text in a bat fileSartorius
K
0

You can probably look at the answers here: Can I mask an input text in a bat file

I think EditV32.exe and EditV64.exe are the best options you have (and since you were OK to use conset, which as far as I know does not come with Windows, I don't think you would oppose to using editv?)

http://www.westmesatech.com/editv.html

Also: http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/7d2d92a5-5d59-4954-bf3e-22eeb5cf0ee6/

Kosher answered 2/5, 2011 at 1:38 Comment(1)
Note that my editv32/edit64 utilities have been superseded by my open-source editenv utility.Gristede
M
12

1.Pure batch solution that (ab)uses XCOPY command and its /P /L switches found here :

:: Hidden.cmd
::Tom Lavedas, 02/05/2013, 02/20/2013
::Carlos, 02/22/2013
::https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/f7mb_f99lYI


@Echo Off
:HInput
SetLocal EnableExtensions EnableDelayedExpansion
Set "FILE=%Temp%.\T"
Set "FILE=.\T"
Keys List >"%File%"
Set /P "=Hidden text ending with Ctrl-C?: " <Nul
Echo.
Set "HInput="
:HInput_
For /F "tokens=1* delims=?" %%A In (
 '"Xcopy /P /L "%FILE%" "%FILE%" 2>Nul"'
) Do (
  Set "Text=%%B"
  If Defined Text (
    Set "Char=!Text:~1,1!"
    Set "Intro=1"
    For /F delims^=^ eol^= %%Z in ("!Char!") Do Set "Intro=0"
    Rem If press Intro
    If 1 Equ !Intro! Goto :HInput#
    Set "HInput=!HInput!!Char!"
  )
)
Goto :HInput_
:HInput#
Echo(!HInput!
Goto :Eof 

2.1 Another way based on replace command

@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion

Set /P "=Enter a Password:" < Nul
Call :PasswordInput
Echo(Your input was:!Line!

Goto :Eof

:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line="
:_PasswordInput_Kbd
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&Goto :Eof
If !CHR!==!BS! (If Defined Line (Set /P "=!BS! !BS!" <Nul
Set "Line=!Line:~0,-1!"
)
) Else (Set /P "=*" <Nul
If !CHR!==! (Set "Line=!Line!^!"
) Else Set "Line=!Line!!CHR!"
)
Goto :_PasswordInput_Kbd

2.Password submitter that uses a HTA pop-up . This is a hybrid .bat/jscript/mshta file and should be saved as a .bat:

<!-- :
:: PasswordSubmitter.bat
@echo off
for /f "tokens=* delims=" %%p in ('mshta.exe "%~f0"') do (
    set "pass=%%p"
)

echo your password is %pass%
exit /b
-->

<html>
<head><title>Password submitter</title></head>
<body>

    <script language='javascript' >
        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='15'></input>
    <hr>
    <button onclick='pipePass()'>Submit</button>

</body>
</html>

3.A self-compiled .net hybrid .Again should be saved as .bat .In difference with other solutions it will create/compile a small .exe file that will be called (if you wish you can delete it). Also requires installed .net framework but that's rather not a problem:

@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal enableDelayedExpansion
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
   set "jsc=%%v"
)
if not exist "%~n0.exe" (
    "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)

for /f "tokens=* delims=" %%p in ('"%~n0.exe"') do (
    set "pass=%%p"
)
echo your password is !pass!

endlocal & exit /b %errorlevel%
*/

import System;

var pwd = "";
var key;
Console.Error.Write("Enter password: ");

        do {
           key = Console.ReadKey(true);

           if ( (key.KeyChar.ToString().charCodeAt(0)) >= 20 && (key.KeyChar.ToString().charCodeAt(0) <= 126) ) {
              pwd=pwd+(key.KeyChar.ToString());
              Console.Error.Write("*");
           }

           if ( key.Key == ConsoleKey.Backspace && pwd.Length > 0 ) {
               pwd=pwd.Remove(pwd.Length-1);
               Console.Error.Write("\b \b");
           }   
        } while (key.Key != ConsoleKey.Enter);
        Console.Error.WriteLine();
        Console.WriteLine(pwd);
Mak answered 24/6, 2014 at 21:1 Comment(2)
Nice answer, but xcopy.exe is not a "pure batch" solution. There is no pure batch solution as far as I know.Kidd
Solution 2.1 (based on the replace command) looks nice, but the CTRL^C command is treated as if the user had entered a '!' character. Is there a way to differentiate these two cases?Laser
G
1

OK - so this answer comes 9 years later, but I guess better late than never...

I wrote the editv32/editv64 tool noted in the marked answer, but since then I wrote a slightly better tool:

https://github.com/Bill-Stewart/editenv

The tool is free and open-source. Binary download is available from the Releases tab.

The --maskinput (-m) option lets you mask input (optionally with a user-defined character). Note that this feature is NOT SECURE (the variable value is still in memory and the environment in plain-text) -- it's for convenience only.

My most frequent use of this tool is to interactively edit the Path variable for the current process (it's turned out to be pretty handy for that purpose).

Gristede answered 1/4, 2020 at 21:23 Comment(0)
K
0

You can probably look at the answers here: Can I mask an input text in a bat file

I think EditV32.exe and EditV64.exe are the best options you have (and since you were OK to use conset, which as far as I know does not come with Windows, I don't think you would oppose to using editv?)

http://www.westmesatech.com/editv.html

Also: http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/7d2d92a5-5d59-4954-bf3e-22eeb5cf0ee6/

Kosher answered 2/5, 2011 at 1:38 Comment(1)
Note that my editv32/edit64 utilities have been superseded by my open-source editenv utility.Gristede
H
0

The simple code worked for me

:CVSPassword
echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5>hide.com
set /P cvspassword=Please enter CVS Password:: <nul
for /f "tokens=*" %%i in ('hide.com') do set cvspassword=%%i
IF "%cvspassword%"=="" GOTO CVSPassword
echo cvspassword %cvspassword%
Hourly answered 7/7, 2015 at 18:25 Comment(3)
hide.com? Where is it? (and isn't it a 16 bit application, which won't run on 64 bit windows?)Backsword
Forgot to add echo command which will create a hide.com file which is used to hiding the passwordHourly
@Backsword is correct, this does not work on a 64-bit OS.Gristede
H
0

To enter hidden numbers:

@echo off
setlocal enabledelayedexpansion

call:SecretInput "Password: " pw 4
call:SecretInput "Code: " c 5

echo.
echo Password is %pw%
echo Code is %c%
pause&exit

:SecretInput <String> <Var> <Len>
set %2=
set/a len=%3
for /l %%i in (1,1,99) do (
set/a l=%3-len
echo Press d to Delete
set/p .=%~1 <nul
for /l %%j in (1,1,!l!) do (
<nul set/p .=*)
choice /n /c:1234567890d>nul
set e=!errorlevel!&cls
if !e!==11 (
if "!%2!" neq "" (
set %2=!%2:~,-1!
set/a len+=1)
) else (
set/a e%%=10,len-=1
set %2=!%2!!e!)
if !len!==0 exit/b)
Hierarchize answered 28/8, 2022 at 20:43 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Arletha

© 2022 - 2024 — McMap. All rights reserved.