T3RRYT3RR0R

user_login_example

Jul 16th, 2021 (edited)
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.71 KB | None | 0 0
  1. @Echo off & CD "%~dp0"
  2.  
  3. :# REQUIRES buttons.bat [ link below ] to exist in same working directoy
  4. :# https://pastebin.com/zjwgdZiM
  5. :# Youtube tutorial for buttons.bat :
  6. :# https://www.youtube.com/watch?v=ZYhvUbek4Xc
  7. :# EXAMPLE of a multi user login script for batch RPG's
  8. :# PURPOSE: Input by clicked letters / numbers only; NO SET /P ; User cannot perform Code Injection.
  9.  
  10.  Mode 100,40
  11.  If not exist buttons.bat (
  12.   Echo( REQUIRES buttons.bat [ link below ] to exist in same working directoy
  13.   Echo( https://pastebin.com/zjwgdZiM
  14.   Pause
  15.   Goto :Eof
  16.  )
  17.  Call buttons.bat def
  18.  If not Exist "%TEMP%\BG.exe" (
  19.   Echo( REQUIRES BG.exe to be saved in %TEMP%
  20.   Pause
  21.   Goto :Eof
  22.  )
  23.  Goto :main
  24.  
  25. ================================:# Functions
  26.  
  27. :Confirm [Prompt]
  28.  cls
  29.  Echo(%~1
  30.  %Get.Click% YN
  31.  If not defined ValidClick[YN] Goto :Confirm
  32.  Goto :Eof
  33.  
  34. ====
  35. :End
  36.  %Clean.Exit%
  37.  
  38. =====
  39. :Save - Requires use of # prefixed variable names
  40. :# Define variables for retention with a # prefix
  41. :# and output to the !User!_Save.cmd stream by calling this function
  42.  Setlocal EnableDelayedExpansion
  43.  (For /f "Tokens=1,2 Delims==" %%G in ('Set #')Do (
  44.    Set "tmpvar=%%G"
  45.    Set "tmpval=%%H"
  46.    (Echo(Set !tmpvar!=!tmpval!)
  47.   )
  48.  ) >"%~f0:%#User%_Save.cmd"
  49.  Endlocal
  50. Exit /B 0
  51.  
  52. ======
  53. :Input <Variable> <Prompt> <Descriptor>
  54.  If "%~1" == "" Exit /B 1
  55.  If "%~2" == "" Exit /B 1
  56.  If "%~3" == "" Exit /B 1
  57.  Set "%~1="
  58.  Set "input="
  59.  
  60. :BuildInput
  61.  Cls
  62.  Echo(%~2: %\E%[7m!input!%\E%[0m
  63.  %Get.click% Keys
  64.  If not defined Group Goto :BuildInput
  65.  If /I "!Clicked[%Group%]!"=="space" (
  66.   Set "input=!input! "
  67.   Goto:BuildInput
  68.  )
  69.  If /I "!Clicked[%Group%]!"=="backspace" (
  70.   If not "!input!"=="" (Set "input=!input:~0,-1!")Else start /b "" %BG.exe% Play "C:\Windows\Media\Windows Critical Stop.wav"
  71.   Goto:BuildInput
  72.  )
  73.  If /I "!Clicked[%Group%]!"=="done" (
  74.   Goto:finishInput
  75.  )
  76.  For /f "tokens=2 Delims=;" %%G in ("!C{Pos}!")Do For /f "delims=" %%k in ('Set /A "%%G-2"')Do (
  77.   For /f "Tokens=2 Delims=[]" %%i in ("!ValidClick[%Group%]!")Do (
  78.    If not "!btn[keys][%%i][string]:~%%k,1!"==" " Set "input=!input!!btn[keys][%%i][string]:~%%k,1!"
  79.   )
  80.  )
  81.  
  82. Goto:BuildInput
  83.  
  84. :finishInput
  85.  Set "%~1=!Input!"
  86.  If Not Defined %~1 (
  87.   start /b "" %BG.exe% Play "C:\Windows\Media\Windows Critical Stop.wav"
  88.   Echo(%~3 required
  89.   Endlocal
  90.   Pause
  91.   Goto :Input
  92.  )
  93. Exit /B 0
  94.  
  95. :Main
  96.  Cls
  97.  Setlocal EnableDelayedExpansion
  98.  
  99.  %Make.btn% postLogin /S Delete user /y 3 /x 3 /bo 33 /N
  100.  %Make.btn% postLogin /S Change Password /y 3 /bo 34
  101.  %Make.btn% postLogin /S Exit /y 3 /bo 31
  102.  %Make.btn% postLogin /S Continue /y 3 /bo 32
  103.  %Make.btn% Keys /S "       1234567890          " /Y 5 /bo 34 /N
  104.  %Make.btn% Keys /S "  QWERTYUIOP qwertyuiop    " /bo 34
  105.  %Make.btn% Keys /S "   ASDFGHJKL  asdfghjkl    " /bo 34
  106.  %Make.btn% Keys /S "    ZXCVBNM    zxcvbnm     " /bo 34
  107.  %Make.btn% Keys /S Space /Y 17 /X 2 /bo 34
  108.  %Make.btn% Keys /S Done /Y 17 /X 10 /bo 32
  109.  %Make.btn% Keys /S Backspace /X 18 /Y 17 /bo 34
  110.  %Make.btn% Quit /S "Exit" /bo 31 /N /Y 38
  111.  %Make.btn% YN /S Yes /y 4 /bo 32 /N
  112.  %Make.btn% YN /S No /y 4 /bo 31
  113.  
  114.  Set "Users=0"
  115.  For /f "Skip=6 Tokens=2 Delims=:" %%G in ('Dir /R "%~f0"^|Findstr /vc:"_Save.cmd"')Do Set /A Users+=1
  116.  If not !Users! GEQ 10 %Make.Btn% Users /S New User /N
  117.  For /f "Skip=6 Tokens=2 Delims=:" %%G in ('Dir /R "%~f0"^|Findstr /vc:"_Save.cmd"')Do %Make.Btn% Users /S %%G
  118.  
  119. :Login
  120.  For /f "tokens=1 Delims==" %%G in ('Set # 2^> nul')Do Set "%%G=" 2> nul
  121.  Set "#Status=New"
  122.  Cls
  123.  
  124. :username
  125.  cls
  126.  %Get.Click% Users
  127.  If not defined Group Goto:username
  128.  If "!Clicked[Users]!"=="New User" (
  129.   Call :input #User "Enter your username" "Username"
  130.  )Else Set "#User=!btn%ValidClick[Users]%[String]!"
  131. Title [!#User!]
  132. :Verify
  133.  Cls
  134.  More < "%~f0:!#User!" > nul 2>&1 && (
  135.   Set "#Pass="
  136.   Call :input #Pass "Enter your current password" "Password"
  137.   (More < "%~f0:!#User!"|findstr.exe /LXC:"!#Pass!") > nul 2>&1 || (
  138.    Call :Confirm "Password invalid. Try again?"
  139.    If "!Clicked[YN]!"=="Yes" Goto:Login
  140.    Goto :End
  141.   )
  142.  )
  143.  If "!#Pass!"=="" (
  144.   cls
  145.   Call :input #Pass "Enter your new password" Password
  146.   Call :input PassC "Confirm your password" Password
  147.   If not "!#Pass!" == "!PassC!" (
  148.    Echo(Passwords do not match.
  149.    Pause
  150.    Goto :Login
  151.   )
  152.   (Echo(!#Pass!) >"%~f0:!#User!" 2>Nul || (
  153.    Echo(Password contains invalid characters.
  154.    Goto :Verify
  155.   )
  156.   Call :Save
  157.  )
  158.  
  159. :ReturnAction
  160.  cls
  161.  %Get.Click% PostLogin
  162.  If not defined Group Goto:ReturnAction
  163.  If /I "!Clicked[%Group%]!"=="Delete User" (
  164.   Call :Confirm "Are you Sure?"
  165.   If "!Clicked[YN]!"=="No" Goto:ReturnAction
  166.   (Powershell -c "remove-item -path '%~nx0' -Stream '!#User!_Save.cmd'") 2> nul
  167.   (Powershell -c "remove-item -path '%~nx0' -Stream '!#User!'") 2> nul
  168.   Endlocal
  169.   Goto :main
  170.  )
  171.   If /I "!Clicked[%Group%]!"=="Change Password" (
  172.   more < "%~f0:%#User%_Save.cmd" > nul 2> null && For /F "UsebackQ Delims=" %%G in ("%~f0:%#User%_Save.cmd")Do %%G
  173.   Call :input #Pass "Enter your new password" Password
  174.   Call :input PassC "Confirm your new password" Password
  175.   If not "!#Pass!" == "!PassC!" (
  176.    Echo(Passwords do not match.
  177.    Pause
  178.    Goto :ReturnAction
  179.   )
  180.   (Echo(!#Pass!) >"%~f0:!#User!" 2>Nul || (
  181.    Echo(Username contains invalid characters.
  182.    Timeout /t 3 /Nobreak
  183.    Goto :Verify
  184.   )
  185.   Call :Save
  186.  )
  187.   If /I "!Clicked[%Group%]!"=="Exit" %Clean.Exit%
  188.  
  189. :Start
  190. :# Load user information from the users stream.
  191.  more < "%~f0:%#User%_Save.cmd" > nul 2> nul && For /F "UsebackQ Delims=" %%G in ("%~f0:%#User%_Save.cmd")Do %%G
  192. rem demo restoration of saved values. Substitute with your script below
  193.  Set #
  194.  Set #Status=returned
  195.  Call :Save
  196. :Quit
  197.  %Get.Click% Quit
  198.  If not Defined Group Goto :Quit
  199.  
  200. Goto :end
  201.  
Add Comment
Please, Sign In to add comment