bat script to get a users name and password to connect to shared network drives, the key element is:
net use u: \\server\users\%UserName% * /user:%UserName% /persistent:no
This works great as the password prompt is requested by the "*" which is hidden from view and I have already captured the user's login name. Mapping to other locations on the same server works without any further user input but as soon as I map to another server the user will be prompted to input their password again.
Is there anyway I can capture the entry typed at "*" to apply to a string for the other servers?
Thanks.
Thanks for the responses, unfortunately my email, must be blocking the replies as I didn't get any notifications.
I've never used PowerShell so that would be another learning curve and JosefZ's post certainly will be reviewed.
I did some more intensive searching out of work hours and located a command based utility:
EditVar/EditV32/EditV64 and Choose/Choose32/Choose64
(C) 2006-2014 by Bill Stewart (bstewart#iname.com)
This resides in the same location as the orinal script and just requires a line to call it and assign a string, in the example below %PASSWORD% will be the string which I can then use in place of "*", works a treat.
editv64 -p "Enter your password: " PASSWORD -m
Good to get feed back and I'll be sorting my email filters out - Cheers.
A clone from my answer to another question:
Set /P "=Type the password for \\server\users\%UserName%:" < Nul
Call :PasswordInput serverpass
net use u: \\server\users\%UserName% %serverpass% /user:%UserName% /persistent:no
rem skip over the procedure
goto :eof
:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
::
::Update 20150503: https://stackoverflow.com/users/3439404/josefz?tab=profile
::Changes made in next lines:
:: SetLocal EnableDelayedExpansion
:: If !CHR!==!CR! Echo(&EndLocal&set "%1=%Line%"&Goto :Eof
::Usage:
:: Call :PasswordInput variableName
::where variableName is a name of output variable (by reference call)
::
SetLocal EnableDelayedExpansion
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(&EndLocal&set "%1=%Line%"&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
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
PowerShell sample script to read hidden input string and pass to multiple net use commands:
Function Read-Password {
param(
[String] $promptString
)
$secureString = Read-Host $promptString -AsSecureString
try {
$intPtr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
$password = [Runtime.InteropServices.Marshal]::PtrToStringAuto($intPtr)
}
finally {
if ( $intPtr ) {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($intPtr)
}
}
$password
}
$password = Read-Password "Enter password"
if ( -not $password ) { return }
net use \\server1\share /user:foo $password
net use \\server2\share /user:foo $password
# ... etc.
Related
My batch script needs some values defined. I want to support prompt and params both.
Not sure why the first if username not defined is always not verified, either I pass -u <username> or not.
#echo off
setlocal enableextensions disabledelayedexpansion
:parse
IF "%~1"=="" GOTO endparse
IF "%~1"=="-u" SET ouser=%~2
IF "%~1"=="-s" SET studies=%~2
IF "%~1"=="-h" REM do something else
SHIFT
GOTO parse
:endparse
if not defined ouser (
rem set the username using a plain prompt
SET /p ouser=Enter your Username [ENTER]:
if not defined ouser (
echo Username must be defined
goto :eof
)
)
rem set the study using a plain prompt
SET /p studies=Enter the comma-separated list [ENTER]:
if not defined studies (
echo At least one item must be defined
goto :eof
)
echo %ouser% - %studies%
endlocal
exit /b
consumer side
> script.bat // in this case I would expect the prompt for both "ouser" and "studies"
> script.bat -u myuser // in this case I want to prompt just for the param "studies" (the same if I pass just "studies" as param)
> script.bat -u myuser -s study1,study2 // no prompt
:start
echo Try to hack this password!
set/p "pass=>"
if not %pass% == Ebaturvaline177 goto vale
echo Correct!!
pause
start "" https://stackoverflow.com
exit
:vale
echo Wrong.
goto :start
How can I encrypt/hash the password so people just cant view the code and see the code?
I was interested after reading your question so i decided to make a batch-file that is kind of accomplishing what you are trying to do. instead of storing raw password i am storing MD5 Hash in a file and then i am comparing user input that is also hashed with the hash stored in a file named pass.txt.
Crackme.bat:
#echo off & setlocal
:loop
set userinput=
set /p userinput=Try Crack me:
set "plaintext=%userinput%"
set "file=%temp%\%~n0.tmp"
set md5=
if not defined plaintext set /P "plaintext="
if exist "%plaintext%" (
set "file=%plaintext%"
) else for %%I in ("%file%") do if %%~zI equ 0 (
<NUL >"%file%" set /P "=%plaintext%"
)
for /f "skip=1 delims=" %%I in ('certutil -hashfile "%file%" MD5') do (
if not defined md5 set "md5=%%I"
)
2>NUL del "%temp%\%~n0.tmp"
echo %md5: =% >>file.txt
set /p passhash=<file.txt
ping localhost -n 2 >nul
del file.txt
set /p passtocrackhash=<pass.txt
if %passhash% equ %passtocrackhash% ( goto cracked) else ( goto error)
:error
echo Wrong pass
goto loop
:cracked
echo Hurrah!you cracked the password it was %userinput%
This batch file will take user input and compare it with the hash stored in pass.txt,if the password is correct then a success message is shown otherwise an error is thrown and is looped back for another try
pass.txt:
8b1a9953c4611296a827abf8c47804d7
Just make a file named as pass.txt and type the md5 hash of your password and save it.you can create an md5 hash from following batch-file or online . Just save the code as .bat open cmd in same directory and give the string that you want to hash as an argument to batch file
Well anyone is welcomed to edit the code for improvement...
If the user has access to the source code, they have access to bypass the password.
If it's just a fun challenge, you could try hashing the input and checking against the hashed version of your password instead of storing plaintext.
This would require some file system management, however. I do not know of a way to hash input to a batch file directly.
I need to read the ID from a file like this:
BitLocker Drive Encryption: Configuration Tool version 10.0.16299
Copyright (C) 2013 Microsoft Corporation. All rights reserved.
Volume C: [OSDisk]
All Key Protectors
External Key:
ID: {31116007-D1FB-43CC-A89C-927487BD5F00}
External Key File Name:
31116007-D1FB-43CC-A89C-927487BD5F00.BEK
Numerical Password:
ID: {C7948F32-F2F0-4E55-AD2E-06E982DFDB4F}
Password:
xxxx-xxxxx-xxxxx-xxxxxx-xxxxx-xxxx-xxxxx-xxx
TPM:
ID: {6497FF93-F678-4317-B5D7-423E9D682BF0}
PCR Validation Profile:
0, 2, 4, 8, 9, 10, 11
and then run the following command to to export the keys to AD for all IDs
manage-bde -protectors -adbackup c: -id {C7948F32-F2F0-4E55-AD2E-06E982DFDB4F}
If you're not willing to utilise another built-in scripting language for this, and your file to read uses Windows standard line endings, I have an alternative idea.
Instead of directly searching for lines with ID: {…, you may be able to search for <Anything><Carriage Return><Line Feed><Space(s)><Password:>. You could further refine <Anything> should you feel the need, but in this case I do not think it's necessary:
#Echo Off
SetLocal DisableDelayedExpansion
Set "Src=input.txt"
Set "Str=Password:"
Set "ID="
(Set LF=^
% 0x0A %
)
For /F %%A In ('Copy /Z "%~f0" Nul')Do Set "CR=%%A"
SetLocal EnableDelayedExpansion
FindStr /RC:".*!CR!*!LF! *%Str%" "%Src%">"%TEMP%\_$.tmp"
EndLocal
For /F "UseBackTokens=2Delims={}" %%A In ("%TEMP%\_$.tmp")Do Set "ID={%%A}"
Del "%TEMP%\_$.tmp"
If Not Defined ID GoTo :EOF
manage-bde -protectors -adbackup c: -id %ID%
In the example above, I have used input.txt as the name of the text file you're reading, please modify that as needed.
FOR /f "tokens=2delims={}" %%a IN ('findstr /x /r /c:" *ID: {.*}" "%filename1%"') DO ECHO manage-bde -protectors -adbackup c: -id {%%a}
where filename1 contains the filename you are examining and the command is being echoed. Remove the echo keyword to execute manage-bde.
The findstr looks for lines that exactly match the pattern [spaces]ID: {string} and assigns the string between {} to %%a, then displays the required command line, reapplying the {}
a pure batch script, which takes advantage of the fact, you look for the key immediately after a "trigger line" (Numerical Password:)
#echo off
setlocal
set "flag="
for /f "tokens=*" %%a in (t.txt) do (
if defined flag (for %%b in (%%a) do set "key=%%b") & goto :found
if "%%a" == "Numerical Password:" set "flag=yes"
)
echo not found & goto :eof
:found
echo %key%
Perhaps use a windows script to handle the task? This can be executed directly, using wscript, or from a batch file using cscript:
cscript //nologo myscript.js
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.OpenTextFile("C:\\myfile.txt", 1);
var content = file.readAll();
// Do something with file content
file.close();
You might be able to get what want with Regular Expressions and FindStr. Partial solution since I'm lazier than you. ;)
c:\projects>findstr {[0-9A-F/-]*} data.txt
ID: {31116007-D1FB-43CC-A89C-927487BD5F00}
ID: {C7948F32-F2F0-4E55-AD2E-06E982DFDB4F}
ID: {6497FF93-F678-4317-B5D7-423E9D682BF0}
EDIT: I overlooked that all IDs and created a solution based on the example line
The following PowerShell one liner will extract the ID from a text file .\BitLocker.txt
PoSh> Select-String -Path .\BitLocker.txt -Pattern 'Numerical Password' -Context 0,1|ForEach-Object {($_.Context.Postcontext.Trim() -split ' ')[1]}
{C7948F32-F2F0-4E55-AD2E-06E982DFDB4F}
To be on topic wrapped in a batch:
#Echo off
For /f "usebackq delims=" %%A in (`
powershell -NoP -C "Select-String -Path .\BitLocker.txt -Pattern 'Numerical Password' -Context 0,1|% {($_.Context.Postcontext.Trim() -split ' ')[1]}"
`) Do Set "ID=%%A"
manage-bde -protectors -adbackup c: -id %ID%
I have a text file, that has some text with this syntax:
mytextfile.txt:
websiteurl1 username1 password1
websiteurl2 username2 password2
websiteurl3 username3 password3
And so on....
And I'd like to be able to find username and password strings by pointing the websiteurl, so let's say I tell the batch file,
find websiteurl3, it should print the username3 and password3
and I was able to write a "FOR LOOP" but I am not sure how to use the syntax of the loop, as my code finds only the last line always, here is what I have:
FOR /F "tokens=2,3 delims= " %%A IN (URL.txt) DO IF EXIST URL.txt (set WEBUserName1=%%A) && (SET WEBUserPass1=%%B)
pause
echo Username:"%WEBUserName1%"
echo Password:"%WEBUserPass1%"
pause
exit
I know the loop is looking on the "URL.txt" for the tokens 2 and 3, and then sets the variables accordingly, what I'd like to know, is how I can use the loop, or if needed, any other command to be able to:
Find the "URL.txt file
Then find the specific string, in this case, the first word of the specified string line
Then find tokens 2 and 3 of that string line.
And I'd like to be able to find username and password strings
Use the following batch file.
test.cmd:
#echo off
setlocal enabledelayedexpansion
for /f "tokens=2,3" %%a in ('type mytextfile.txt ^| findstr "%1"') do (
echo Username:"%%a"
echo Password:"%%b"
pause
exit
)
endlocal
Notes:
Pass the website URL string as a parameter to the batch file.
findstr is used to find the matching line from the file, so we only need to parse a single line using for /f.
Example usage and output:
F:\test>type mytextfile.txt
websiteurl1 username1 password1
websiteurl2 username2 password2
websiteurl3 username3 password3
F:\test>test websiteurl3
Username:"username3"
Password:"password3"
Press any key to continue . . .
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
findstr - Search for strings in files.
for /f - Loop command against the results of another command.
type - Display the contents of one or more text files.
bad way, but this too work:
#echo off
setlocal
set srch_site=websiteurl2
FOR /F "tokens=1,2,3 delims= " %%A IN (URL.txt) DO (
IF EXIST URL.txt (
if "%%A"=="%srch_site%" (
(set WEBUserName1=%%B) && (SET WEBUserPass1=%%C)&goto:stdout
)
)
)
:stdout
pause
echo Username:"%WEBUserName1%"
echo Password:"%WEBUserPass1%"
pause
exit
May I offer you a different approach?
The code below get all usernames and passwords and store they in two vectors:
#echo off
setlocal EnableDelayedExpansion
for /F "tokens=1-3" %%A in (URL.txt) do set "User[%%A]=%%B" & set "Pass[%%A]=%%C"
After that, you may test if the username and password of a certain site was given this way:
set "site=websiteurl3"
if defined User[%site%] echo The site "%site%" have username and password
... or display their values this way:
echo Username:"!User[%site%]!"
echo Password:"!Pass[%site%]!"
You may review a detailed explanation of array management in Batch files at this post.
You get the last line, because you process the whole file. Filter your textfile and process only that one line:
set "site=websiteurl2"
FOR /F "tokens=2,3 delims= " %%A IN ('find "%site%" URL.txt') DO (
set "usr=%%A"
set "pwd=%%B"
)
echo %usr%, %pwd%
I would like to know why do I get an error("( was not expected at this time") in this script.
for /f "delims=" %%t in (%cd%\Multitool\Multitool.txt) do (
set /p username=
set /p password=
set /p created=
)
if %created%==accountcreated ( goto CONTINUE ) else ( goto CREATE )
I have those lines of codes wich always get me an error: "( was not expected" at this time.
In my program I want to know if the user created an account already...
When the user creates an account I echo this in %cd%\Multitool\Multitool.txt :
(
echo %username%
echo %password%
echo accountcreated
) > %cd%\Multitool\Multitool.txt
So can you tell me why I get that error and how to correct it please?
Sorry for my bad english im french...
#ECHO OFF
SETLOCAL enabledelayedexpansion
:: set up logfile and password
SET "logfile=U:\q28542185.txt"
SET "password=dummypassword"
SET "myusername=dummyusername"
:: Dummy log entry
(
echo %myusername%
echo %password%
echo accountcreated
) > %logfile%
:: This is just to ensure the three variables are empty
FOR %%t IN (myusername password created) DO SET "%%t="
for /f "delims=" %%t in (%logfile%) do (
SET "myusername=!password!"
SET "password=!created!"
SET "created=%%t"
)
if %created%==accountcreated ( goto CONTINUE ) else ( goto CREATE )
:continue
ECHO got to continue
GOTO :eof
:create
ECHO got to create
GOTO :EOF
I used a file named q28542185.txt to receive the log information for my testing.
Note that username is a magic variable initialised by the syste, so I don't like changing it, so I've substituted myusername.
The first part sets up a dummy log file for testing.
The replacement for loop reads the log file and "ripples" the lines through the variablenames using the delayedexpansion facility, so that the variables are set in appropriate sequence.
Note that if created contains Spaces then your if statement should be modified to
if "%created%"=="accountcreated" ....
to allow correct parsing sequence IF token operator token