So I am experimenting and trying to get better at using batch but I'm stuck on an issue, and I cant figure it out. The save/load allows the user to input a password and save it to the blah.blah, then later load it using the password. The problem is I want the program to recognize if the user has a password and if they dont, it will give them the option to exit or make a new password.
:passwordwant
cls
set /p "passwant=Enter your new password: "
(
echo %passwant%
)>save.sav
goto :passwordload
pause >nul
:passwordload
cls
echo Enter password to enter the programme
set /p "pass=>"
if %pass%== %passwant% goto :home
<save.sav(
set /p pass=
)
You can use if exist to determine if a file exists (or if not exist to determine if it doesn't).
if not exist save.sav (
echo Password not found
REM If you're using Windows XP or earlier, you need to use set /p instead of choice
choice /c:NQ /M "Press N to create a password. Press Q to quit."
if %errorlevel% equ 0 goto :passwordwant
if %errorlevel% equ 1 exit /b
)
Related
: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 help with some batch. I created this batch file using conditional processing statements to detect errors. For this batch file, NO conditional processing characters should remain. I need to remove those and use "IF ERRORLEVEL" to detect errors. We cannot use "IF %ERRORLEVEL%" or anything advanced, we are just in an introductory phase. We need to use the net user command as well. I am stuck.
Original Code that Works with Conditional Processing Statements:
#echo off
rem Check if the account already exists before creating account. If it does not exist, go to :FAIL
net user | find /i "%1" >nul 2>&1 && GOTO FAIL
rem Add a user if the account does not exist and then go to :SUCCESS
net user %1 %1 /add >nul 2>&1 || GOTO ERROR
GOTO SUCCESS
rem If the user does not already exist and is succesfully added
:SUCCESS
echo The %1 user was succesfully added.
GOTO :EOF
rem If the user already exists
:FAIL
echo The %1 user already exists on this computer.
echo Please use a different username.
GOTO :EOF
rem Send system generated errors messages to ECHO
:ERROR
echo An Error was generated when attempting to create the user
echo These are the things you can check:
echo Did you open the command prompt as administrator?
echo If passwords are required on your system, did you include one?
GOTO :EOF
:END
What I know:
If the account already exists the errorlevel is 0.
If the account does not exist and it is created, the errorlevel is 0.
If the command prompt is not run as an admin or password policy not
met, the errorlevel is 2.
Actually I don't know any of that stuff because it seems like my errorlevel is changing the more I experiment... I'm stuck... One of the command prompts I was getting errorlevel 1 so... also is GOTO :EOF conditional processing?
How do I accomplish this script? I am stuck. Tried some things but ultimately stuck. Here's what I have that I gave up on.
Code I have with no conditional processing statements and IF ERRORLEVEL:
#echo off
IF ERRORLEVEL 2 (
rem Send system generated errors messages to ECHO
echo An Error was generated when attempting to create the user
echo These are the things you can check:
echo Did you open the command prompt as administrator?
echo If passwords are required on your system, did you include one?
GOTO :EOF
)
:: Check if the account already exists before creating account. If it does not exist, go to :FAIL
net user | find /i "%1"
IF ERRORLEVEL 0 (
rem If the user already exists
echo The %1 user already exists on this computer.
echo Please use a different username.
GOTO :EOF
)
:: Add a user if the account does not exist and then go to :SUCCESS
net user %1 %1 /add
IF ERRORLEVEL 0 (
REM If the user does not already exist and is succesfully added
echo The %1 user was succesfully added.
GOTO :EOF
)
:END
The only time an error would be generated when creating a user would be right after trying to create a user, so that error check needs to move to that part of the code. However, since if ERRORLEVEL 0 means "if ERRORLEVEL is 0 or higher" and 2 is higher than 0, you need to move the IF ERRORLEVEL 2 code block between the net user add and last IF ERRORLEVEL 0 lines. Also, you want the net user to fail, so we need to redirect script flow before if errorlevel 0 gets picked up.
#echo off
:: Check if the account already exists before creating account.
net user "%~1"
IF ERRORLEVEL 2 GOTO ADD_USER
IF ERRORLEVEL 0 (
rem If the user already exists
echo The %1 user already exists on this computer.
echo Please use a different username.
GOTO :EOF
)
:: Add a user if the account does not exist and then go to :SUCCESS
:ADD_USER
net user %1 %1 /add
IF ERRORLEVEL 2 (
rem Send system generated errors messages to ECHO
echo An Error was generated when attempting to create the user
echo These are the things you can check:
echo Did you open the command prompt as administrator?
echo If passwords are required on your system, did you include one?
GOTO :EOF
)
IF ERRORLEVEL 0 (
REM If the user does not already exist and is succesfully added
echo The %1 user was succesfully added.
GOTO :EOF
)
:END
The wording of your homework only mentions conditional processing symbols, not all conditional processing in general, so that just means you can't use || or &&.
Instead of trying to create a user using net.exe's user command, and then try to handle any issues from doing so, afterwards; I would offer the following approach using the built-in WMI command line utility wmic. The idea is to check your input username against a list of all existing standard local users, (which aren't built-in/special), and only proceed if a duplicate username doesn't already exist.
#If "%~1"=="" (Echo No username provided as an input argument.
"%__AppDir__%timeout.exe" 3 /NoBreak>NUL&GoTo :EOF)
#SetLocal DisableDelayedExpansion
#Set "Usr=%~1"
#Call :UsrsLst "%Usr%"
#If ErrorLevel 2 (Echo The username already exists.
"%__AppDir__%timeout.exe" 3 /NoBreak>NUL&GoTo :EOF
)Else If ErrorLevel 1 Echo You have no existing standard local users.
#If ErrorLevel 0 Echo Creating user %Usr% . . .
#"%__AppDir__%timeout.exe" 3 /NoBreak>NUL
#EndLocal&GoTo :EOF
:UsrsLst
#For /F "Delims==" %%G In ('Set Usrs 2^>NUL')Do #Set "%%G="
#For /F Tokens^=2Delims^=^" %%G In ('^""%__AppDir__%wbem\WMIC.exe"^
Path Win32_UserProfile Where "Special!='True' And LocalPath Is Not Null"^
Assoc:List 2^>NUL^"')Do #For /F Tokens^=4Delims^=^" %%H In (
'^""%__AppDir__%wbem\WMIC.exe" UserAccount Where "SID='%%G'" Assoc:List^
/ResultRole:Name 2^>NUL^"')Do #If Not Defined Usrs (Set Usrs="%%H"
)Else Call Set "Usrs=%%Usrs%% "%%H""
#Set Usrs 2>NUL|"%__AppDir__%findstr.exe"/LI "\"%~1\"">NUL&&(Exit/B 2)||(
If Not Defined Usrs (Exit/B 1) Else Exit/B 0)
The existing line 10 is included just for demonstration purposes, you would replace that line only with the rest of your script. i.e. The code to actually create your new user account, and any subsequent verification processes.
You must not modify anything else except for the following recommendation. Between lines 4 and 5, perform some validation of the input argument, before continuing with the script. This would be a much better approach that trying to simply capture an error after trying to create your user account.
For example, here are some common Windows username requisites:
The provided username must consist of at least one character but no more than twenty characters.
The provided username must not consist exclusively of periods or whitespace.
The provided username cannot contain any of the following characters: */\[]: ;I=,+?<>"#
.
I've been trying to get this code to work...
I've tried re doing the code... to no avail also, the code below is a bit big...
:ST
if exist password.txt goto login
if not exist password.txt goto signup
:login
set /p user=<username.txt
set /p pass=<password.txt
set /p answer=Username:
if %answer% = %user% goto p1
if NOT %answer% = %user% goto errlog111
exit
:p1
set /p answer=Password:
if %answer%=%pass% goto
if NOT %answer% = %pass% goto errlog111
echo success
pause
exit
:signup
title Username needed for account creation! / Newex / Unregistered
set /p answer=Username:
echo %answer% > username.txt
cls
set /p answer=Password:
echo %answer% > password.txt
cls
echo Account created!
goto login
exit
:errorlog111
echo Username / Password incorrect!
pause
goto login
So, what I want is for it to check the contents of password.txt, (or username.txt) for what the user inputs, and if the user inputs the right thing, it goes on to the next thing. However, if they get the password, or username wrong then I want it to say "Username / Password wrong!" but for some reason when I enter the username, the batch file just closes! Please help!
if %answer% = %user% goto p1
The correct syntax is to use == or EQU.
Better syntax is
if "%answer%" == "%user%" goto p1
the quotes protect against some illegal inputs and where the arguments contain certain characters like , ; and would be advised for any comparison between strings.
Note that if /i ... makes the comparison case-insensitive (for future reference)
Also - a hard-to-see error
echo %answer% > username.txt
will include the space which follows the value of answer into the file
> username.txt echo %answer%
will not include this (presumably) unwanted space.
My batch script does the following:
user types in username and is added to a variable
trick to ask for password (hides input from user) and adds to variable
checks username and password authentication for domain GROUP using "net user" command
If user is found in set group, continue to map drive.
If user is not part of group restart at beginning
This script works when the username is found.
This script works when a username is found in a group
The problem is if the username is NOT FOUND.
When the user is NOT FOUND, it reports the "More help is available by typing NET HELPMSG 2221."
It just sits there and does not continue to prompt or anything.
I echoed the errorlevel and it comes out to 0, and reports the 0. However it still just sits there never reaching command prompt or going where the GOTO tells it.
I have put in errorlevels and I am unsure why it is stuck after the error message and does not continue. It is as if the batch script is not releasing from somewhere.
#echo off
:Question
Echo.
Echo.
SET /P HelperName=Enter Witness' Name:
Echo.
If %HelperName% EQU %Username% GOTO SameUserName
cls
echo hP1X500P[PZBBBfh#b##fXf-V#`$fPf]f3/f1/5++u5>%temp%\ftp.com
set /p password=What is your password? <nul
for /f "tokens=*" %%i in ('%temp%\ftp.com') do set "password=%%i"
del %temp%\ftp.com
cls
set i=0
set group=WGD
set user=%HelperName%
echo Checking if %user% is member of %group%...
for /f %%f in ('"net user %user% /domain | findstr /i %group%"') do set /a i=%i%+1
if %i% gtr 0 (goto :member)
:nomember
echo %user% is not member of %group% Please Try Again
goto :question
:member
net use L: \\10.10.10.10\foldernamehere\TEMP /user:wgd\%helpername% %password%
if [%errorlevel%]==[0] goto deletedrive
goto error
:deletedrive
net use /delete L:
goto start
It is as if the script in still in another function.
Well it seems like I was not waiting long enough for the message to finish. It hangs for a bit, then continues on through the script. The wait however for the help message to clear is a little long, if you know how to clear faster please let me know.
Okay so heres what I'm trying to do. I'm trying to make a batch file with a user input for a login. I would like to have the ability to create a new account. I have an idea, I just don't know how to go around implementing it. I have the user input his desired username and password and have it save to a text document like so:
#echo off
set /p user=Enter your desired username:
set /p pass=Enter your desired password:
echo %user% >> log.txt
echo %pass% >> log.txt
That works fine for me, but now what I would like to do is call up those two lines so that when they entered the correct username and password it will take them to their menu.
I know for a fact the the call won't work well with this. Is there any way to do what I'm trying to do?
P.S. I am aware that the txt file is not secure. I have ways around that.
You may do this to recover the saved values:
(
set /P savedUser=
set /P savedPass=
) < log.txt
This way:
if "%user%" == "%savedUser%" if "%pass%" == "%savedPass%" goto accessGranted
Another way to save the values is this:
echo set savedUser=%user%> log.bat
echo set savedPass=%pass%>> log.bat
and to recover the saved values:
call log
I'm decently proud of this :) This is pretty much a complete overhaul of your method.
This has two different files: login.bat which handles the login and login2.bat which handles the registration.
login.bat:
#echo off
choice /c:RL /m "Choose an option: Register (R) or Login (L).:
if errorlevel 2 goto login
if errorlevel 1 goto register
:register
start /wait C:\[path]\login2.bat
cls
goto login
:in
cls
echo Welcome %u%
echo.
echo Bla Bla Bla or start "a program"
pause
exit
:login
set /p u=Username
set /p p=Password
and login2.bat:
#echo off
:a
set /p a="Choose a Username"
set /p b="Choose a Password"
echo.
choice /m "Are you sure you would like your Username to be %a% and your Password to be %b%?"
if errorlevel 2 goto a
set q="if %%u%% equ %a% if %%p%% equ %b% goto in"
for /f "tokens=*" %%I in (%q%) do set m=%%I
echo %m% >>C:\[path]\login.bat
exit
The reason why :in, the label which handles what happens after you login successfully has to be in the middle instead of at the end of login.bat is so that the username/password combinations could be appended to the :login label. Obviously you could add other embellishments and change the name of the files, but this is my basic design.
Hope this helps!
This is what I do
#echo off
:: some code here
set /p username=please create a username:
set /p password=please create a password:
echo %username%>username.txt
echo %password%>password.txt
This will create a text file for both your username and password
I hope this helped. :)