8

I have a batch script with a password sitting in it as part of a command that requires credentials that I do not want to prompt for credentials. I am not worried about external threats, but I don't really want a co-worker going in there and seeing that password. While I trust them not to abuse it, I'd rather not have it there at all.

I was able to do this pretty easily with PowerShell by just storing a secure string in a text file. Pretty basic, but at least there's no plain text passwords laying around. That's all I really need.

How can I obfuscate a password in a batch script?

6
  • you can't hide a password effectively without 3rd party tools in batch.
    – Endoro
    Aug 28, 2013 at 20:01
  • you can try to encode it with certutil to hex or base64 (or both) -> ss64.com/nt/certutil.html .That's the easiest thing that comes to me right now.
    – npocmaka
    Aug 28, 2013 at 20:04
  • @npocmaka That could work, I don't really even need to encrypt it... just anything that's not plain text.
    – tnw
    Aug 28, 2013 at 20:05
  • the only issue is that you'll need temporary files to extract the password...check -encode/-decode and -encodehex\-decodehex options.You can also try to store the password as binary in the registry.
    – npocmaka
    Aug 28, 2013 at 20:12
  • @npocmaka That works well enough for me. If you'd like to add that as an answer, please do.
    – tnw
    Aug 28, 2013 at 21:07

2 Answers 2

7

You could also hide the password in an alternate data stream:

First, add the somewhat secret password to an alternate data stream of your script:

echo somewhatsecretpassword>script.bat:pwd

Here's how to retrieve the password into the variable %p%:

for /f "usebackq delims=" %i in (script.bat:pwd) do set p=%i

From within the batch file itself you may use something like:

for /f "usebackq delims=" %%i in (%~0:pwd) do set p=%%i

This is not secure!

Please consider:

  • This is not secure!
  • Alternate data streams do not get copied everywhere (FAT)
  • Passwords containing special characters may need to be escaped in order to get written correctly to the stream
  • ... it is not secure
3
  • 1
    I would recommend using command-line immediately in FOR to avoid storing password in environment variables. Aug 29, 2013 at 10:10
  • nitpicky grammar: 'You could also hide the password in a[n] alternate data stream'. the 'an' form is always used when the following word begins with a vowel sound (compare 'a engine' with 'an engine'). Solution is just what I was looking for though - just enough to prevent the superficially curious and for the secret to be lost if the file is copied away.
    – Charemer
    Feb 15, 2018 at 9:56
  • @Charemer I just corrected it, I did get it right on the second line....
    – marapet
    Feb 15, 2018 at 21:52
1

Another option might be to obfuscate a "password", which is not secure at all but might be sufficent in certain situations

:main
set a=pas
set b=rd
set /p input=
if %input%==%a%swo%b% goto start
:start
<your code here>
goto main

the password is "password", but it's a bit obfuscated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.