如何创建批处理文件一定时间后超时密码吗? [英] How do I create a password that timeouts after certain period of time in batch file?

查看:125
本文介绍了如何创建批处理文件一定时间后超时密码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使这个code索要10秒后超时是一个密码,如果你写了正确的密码,它去:哇但如果是不正确,它只是延续了code。 (在code是只是一个例子)

  @ECHO OFF
TIMEOUT /吨,10 -10 NUL
出口
:哇
现在呼应这是乐趣!
暂停
出口


解决方案

MD5SUM

批处理脚本,因为他们生活在一个纯文本文件的世界,也不是很利于储存和匹配的密码。毕竟,运行该脚本的人可以很容易地在记事本打开它,看到预期的密码。这不是很hacksy,现在,是吗?

这些批次的密码问题,不时弹出的时间,并在脚本中硬codeD密码总是激怒我。那么试问一下,换一换,我们混淆与MD5哈希密码?有内置到Windows,使这种情况发生没有简单的命令,但它可以用(诚然令人费解)的PowerShell单行来完成。

保存此助手脚本md5sum.bat:

关闭@echo
SETLOCAL如果%〜1==GOTO:EOFPowerShell的\"[Security.Cryptography.HashAlgorithm]::Create('MD5').ComputeHash([Text.Encoding]::UTF8.GetBytes('%~1')) | %% {写主机-n $ _。的ToString('X2')}

然后用它来找到你想要这样的密码的MD5哈希值:

md5sum.bat密码

结果将输出


  

5f4dcc3b5aa765d61d8327deb882cf99


而不是硬编码密码本身到脚本,您现在可以硬code 5f4dcc3b5aa765d61d8327deb882cf99


用户输入

现在到了有趣的东西。您也可以从Powershell的借用混淆这样的用户条目:

关闭@echo
SETLOCAL< NUL集/ P=密码?
设置psCommand = PowerShell的-command$ P =读主机-AsSecureString; ^
$ M = [Runtime.InteropServices.Marshal]; $ M ::的PtrToStringAuto($ M :: SecureStringToBSTR($ P))
FOR / F有usebackq delims =%%的(`%psCommand%`)p做设置通= %% PSETLOCAL enabledelayedexpansion
回声您输入!通过!
ENDLOCAL

两者结合起来的方法,你可以检查用户输入的密码是否哈希硬codeD插入脚本相匹配。

关闭@echo
SETLOCAL< NUL集/ P=密码?
设置psCommand = PowerShell的$ P =读主机-AsSecureString; ^
$ M = [Runtime.InteropServices.Marshal]; $ X = $ M ::的PtrToStringAuto($ M :: SecureStringToBSTR($ P)); ^
[Security.Cryptography.HashAlgorithm] ::创建(MD5)ComputeHash([Text.Encoding] :: UTF8.GetBytes($ X))^。| %% {写主机-n $ _。的ToString('X2')}FOR / F在有usebackq delims =%% I(`%psCommand%`)做(
    如果%%〜我==5f4dcc3b5aa765d61d8327deb882cf99转到:比赛
)回声不。
GOTO:EOF:匹配
回声W00!

瞧!现在,你可以期望密码的用户条目,但用户无法很容易地在记事本中打开该密码是看正确的密码。很酷吧?


自毁

返回到原来的你怎么能在10秒后使这个问题超时,你必须得到一个小创意。一个解决办法是发动了等待10秒的最小化帮助脚本,然后杀死所有的 powershell.exe 任务。这可以用你可能运行其他的PowerShell十岁上下的东西干扰 - 但说实话。鉴于问题的基本性质,我认为它是安全的假设,也不会在这种情况下的一个问题。

我会做这种方式:

关闭@echo
SETLOCAL如果%〜1==帮手转到帮手
启动/分钟%〜F0帮手< NUL集/ P=密码?
设置psCommand = PowerShell的$ P =读主机-AsSecureString; ^
$ M = [Runtime.InteropServices.Marshal]; $ X = $ M ::的PtrToStringAuto($ M :: SecureStringToBSTR($ P)); ^
[Security.Cryptography.HashAlgorithm] ::创建(MD5)ComputeHash([Text.Encoding] :: UTF8.GetBytes($ X))^。| %% {写主机-n $ _。的ToString('X2')}FOR / F在有usebackq delims =%% I(`%psCommand%`)做(
    WAITFOR /秒%计算机%/ SI PasswordEntered> NUL 2 - ; NUL
    如果%%〜我==5f4dcc3b5aa765d61d8327deb882cf99转到:比赛
)回声不。
GOTO:EOF:匹配
回声W00!
GOTO:EOF
:: // END主要RUNTIME:帮手
> NUL 2 - ; NUL(
    WAITFOR PasswordEntered /吨10 || TASKKILL / IMpowershell.exe/˚F

出口

如果你不想 TASKKILL PowerShell中,有可能使读主机超时,但我会离开,作为一个练习留给读者。

I want to make this code to ask for a password that timeouts after 10 seconds and if you wrote the correct password it goes to :wow but if it's incorrect it just continues the code . (the code is just an example)

@ECHO OFF
TIMEOUT /t 10>nul
exit
:wow
echo now this is fun!
pause
exit

解决方案

MD5sum

Batch scripts, as they live in the world of plain text files, aren't very conducive to storing and matching passwords. After all, the person running the script could just as easily open it in Notepad and see the expected password. That's not very hacksy, now, is it?

These batch password questions pop up from time to time, and hard-coded passwords in scripts always irritate me. So how about, for a change, we obfuscate the password with an MD5 hash? There's no simple command built into Windows that makes this happen, but it can be accomplished with a (admittedly convoluted) PowerShell one-liner.

Save this helper script as md5sum.bat:

@echo off
setlocal

if "%~1"=="" goto :EOF

powershell "[Security.Cryptography.HashAlgorithm]::Create('MD5').ComputeHash([Text.Encoding]::UTF8.GetBytes('%~1')) | %%{write-host -n $_.tostring('x2')}"

Then use it to find the MD5 hash of the password you want like this:

md5sum.bat "password"

The result will output

5f4dcc3b5aa765d61d8327deb882cf99

Rather than hard coding the password itself into your script, you can now hard code 5f4dcc3b5aa765d61d8327deb882cf99.


User Entry

Now comes the interesting stuff. You can also borrow from Powershell to obfuscate the user entry like this:

@echo off
setlocal

<NUL set /P "=Password? "
set "psCommand=powershell -command "$p=read-host -AsSecureString;^
$m=[Runtime.InteropServices.Marshal];$m::PtrToStringAuto($m::SecureStringToBSTR($p))""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set "pass=%%p"

setlocal enabledelayedexpansion
echo You entered !pass!
endlocal

Combine the two methods and you can check to see whether the password entered by the user matches the hash hard coded into the script.

@echo off
setlocal

<NUL set /P "=Password? "
set "psCommand=powershell "$p=read-host -AsSecureString;^
$m=[Runtime.InteropServices.Marshal];$x=$m::PtrToStringAuto($m::SecureStringToBSTR($p));^
[Security.Cryptography.HashAlgorithm]::Create('MD5').ComputeHash([Text.Encoding]::UTF8.GetBytes($x)) ^| %%{write-host -n $_.tostring('x2')}""

for /f "usebackq delims=" %%I in (`%psCommand%`) do (
    if "%%~I"=="5f4dcc3b5aa765d61d8327deb882cf99" goto :match
)

echo Nope.
goto :EOF

:match
echo w00!

Voila! Now you can expect a user entry of password but the user can't easily see by opening in Notepad that password is the correct password. Cool, huh?


Self-Destruct

Back to your original question of how you can make this timeout after 10 seconds, you'll have to get a little creative. One solution would be to launch a minimized helper script that waits for 10 seconds, then kills all powershell.exe tasks. This can interfere with other powershell-ish stuff you might have running -- but let's be honest. Given the elementary nature of the question, I think it's safe to assume that won't be a problem in this situation.

I would do it this way:

@echo off
setlocal

if "%~1"=="helper" goto helper
start /min "" "%~f0" helper

<NUL set /P "=Password? "
set "psCommand=powershell "$p=read-host -AsSecureString;^
$m=[Runtime.InteropServices.Marshal];$x=$m::PtrToStringAuto($m::SecureStringToBSTR($p));^
[Security.Cryptography.HashAlgorithm]::Create('MD5').ComputeHash([Text.Encoding]::UTF8.GetBytes($x)) ^| %%{write-host -n $_.tostring('x2')}""

for /f "usebackq delims=" %%I in (`%psCommand%`) do (
    waitfor /s %computername% /si PasswordEntered >NUL 2>NUL
    if "%%~I"=="5f4dcc3b5aa765d61d8327deb882cf99" goto :match
)

echo Nope.
goto :EOF

:match
echo w00!
goto :EOF
:: // END MAIN RUNTIME

:helper
>NUL 2>NUL (
    waitfor PasswordEntered /t 10 || taskkill /im "powershell.exe" /f
)
exit

If you'd rather not taskkill Powershell, it is possible to make read-host time out, but I'll leave that as an exercise for the reader.

这篇关于如何创建批处理文件一定时间后超时密码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆