隐藏批处理文件中的输入

时间:2011-05-02 01:31:51

标签: batch-file hide user-input

我想知道如何在批处理文件中隐藏'set / p'命令的输入。

  

设置/ p密码=您的密码是什么?

我们都知道输入您的密码,您将能够看到它。我该怎么回事呢?

我尝试了来自here.的conset.exe并使用了:

  

conset / PH密码=您的密码是什么?

我得到“Conset:错误设置变量”:(

我的另一个想法是改变控制台窗口的颜色。但是我怎么能改变同一条线上的颜色呢?所以你可以看到问题,但看不到答案?

来自专业人士的任何想法?

4 个答案:

答案 0 :(得分:9)

1.纯批处理解决方案(ab)使用XCOPY命令及其/P /L转换找到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.使用HTA弹出窗口的密码提交者This is a hybrid .bat/jscript/mshta文件,应保存为.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 。应该保存为.bat。与其他解决方案不同的是,它会创建/编译一个将被调用的小.exe文件(如果你希望你可以删除它)。还需要安装.net框架,但这不是问题:

@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);

答案 1 :(得分:1)

简单的代码对我有用

: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%

答案 2 :(得分:0)

您可以在这里查看答案:Can I mask an input text in a bat file

我认为EditV32.exe和EditV64.exe是你拥有的最佳选择(因为你可以使用conset,据我所知,Windows不附带,我认为你不会反对使用editv?)

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

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

答案 3 :(得分:0)

好的-所以这个答案要9年后才能到,但我想迟到总比没有好...

我写了标记答案中提到的editv32 / editv64工具,但是从那时起,我写了一个更好的工具:

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

该工具是免费的开放源代码。可以从Releases标签下载二进制文件。

通过--maskinput-m)选项,您可以屏蔽输入(可以选择使用用户定义的字符)。请注意,此功能为 NOT SECURE (不安全)(变量值仍以纯文本格式保存在内存中,环境仍以纯文本格式)-仅出于方便起见。

我最经常使用此工具的是为当前进程交互式地编辑Path变量(事实证明,此变量非常方便)。

相关问题