Powered by GeSHi

Source code for checkregaccess.bat

(view source code of checkregaccess.bat as plain text)

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3. IF      "%~2"==""           GOTO Syntax
  4. IF NOT  "%~3"==""           GOTO Syntax
  5. WMIC /? >NUL 2>&1 ||        GOTO Syntax
  6. ECHO.%* | FINDSTR /R /C:"[/?]" >NUL && GOTO Syntax
  7.  
  8. SETLOCAL ENABLEDELAYEDEXPANSION
  9.  
  10. SET hDefKey=
  11. SET sSubKeyName=
  12. SET uRequired=
  13.  
  14. :: permission name       	 number 	description
  15. :: ===============       	 ====== 	===========
  16. :: KEY_QUERY_VALUE       	    0x1 	query the values of a registry key
  17. :: KEY_SET_VALUE         	    0x2 	create, delete, or set a registry value
  18. :: KEY_CREATE_SUB_KEY    	    0x4 	create a subkey of a registry key
  19. :: KEY_ENUMERATE_SUB_KEYS	    0x8 	enumerate the subkeys of a registry key
  20. :: KEY_NOTIFY            	   0x10 	request change notifications for a registry key or for subkeys of a registry key
  21. :: KEY_CREATE            	   0x20 	create a registry key
  22. :: DELETE                	0x10000 	delete a registry key
  23. :: READ_CONTROL          	0x20000 	Combines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY values
  24. :: WRITE_DAC             	0x40000 	modify the DACL in the object's security descriptor
  25. :: WRITE_OWNER          	0x80000 	change the owner in the object's security descriptor
  26.  
  27. SET KEY_QUERY_VALUE="&H1"
  28. SET KEY_SET_VALUE="&H2"
  29. SET KEY_CREATE_SUB_KEY="&H4"
  30. SET KEY_ENUMERATE_SUB_KEYS="&H8"
  31. SET KEY_NOTIFY="&H10"
  32. SET KEY_CREATE="&H20"
  33. SET DELETE="&H10000"
  34. SET READ_CONTROL="&H20000"
  35. SET WRITE_DAC="&H40000"
  36. SET WRITE_OWNER="&H80000"
  37.  
  38. IF DEFINED %~2 (
  39. 	SET uRequired=!%~2!
  40. ) ELSE (
  41. 	SET uRequired=%~2
  42. 	SET uRequired="!uRequired:0x=&H!"
  43. )
  44.  
  45. FOR /F "tokens=1* delims=\" %%A IN ("%~1") DO (
  46. 	IF "%%~A"=="HKEY_CLASSES_ROOT"   SET hDefKey="&H80000000"
  47. 	IF "%%~A"=="HKCR"                SET hDefKey="&H80000000"
  48. 	IF "%%~A"=="HKEY_CURRENT_USER"   SET hDefKey="&H80000001"
  49. 	IF "%%~A"=="HKCU"                SET hDefKey="&H80000001"
  50. 	IF "%%~A"=="HKEY_LOCAL_MACHINE"  SET hDefKey="&H80000002"
  51. 	IF "%%~A"=="HKLM"                SET hDefKey="&H80000002"
  52. 	IF "%%~A"=="HKEY_USERS"          SET hDefKey="&H80000003"
  53. 	IF "%%~A"=="HKU"                 SET hDefKey="&H80000003"
  54. 	IF "%%~A"=="HKEY_CURRENT_CONFIG" SET hDefKey="&H80000005"
  55. 	IF "%%~A"=="HKCC"                SET hDefKey="&H80000005"
  56. 	IF NOT "%%~B"==""                SET sSubKeyName="%%~B"
  57. )
  58.  
  59. IF DEFINED hDefKey IF DEFINED sSubKeyName IF DEFINED uRequired (
  60. 	IF DEFINED Debug (
  61. 		ECHO WMIC /NameSpace:\\root\default Class StdRegProv Call CheckAccess hDefKey=%hDefKey% sSubKeyName=%sSubKeyName% uRequired=%uRequired%
  62. 	)
  63. 	WMIC /NameSpace:\\root\default Class StdRegProv Call CheckAccess hDefKey=%hDefKey% sSubKeyName=%sSubKeyName% uRequired=%uRequired%
  64. ) ELSE (
  65. 	ENDLOCAL
  66. 	GOTO Syntax
  67. )
  68. ENDLOCAL
  69. GOTO:EOF
  70.  
  71.  
  72. :Syntax
  73. ECHO.
  74. ECHO CheckRegAccess.bat,  Version 1.00 for Windows XP Professional and later
  75. ECHO Check if you have access to the specified registry key
  76. ECHO
  77. ECHO Usage:  %~n0  "HIVE\RegKeyPath"  permission
  78. ECHO.
  79. ECHO Where:  HIVE        is the registry hive name, e.g. HKEY_USERS or HKU
  80. ECHO         RegKeyPath  is the registry key path to be queried for access
  81. ECHO         permission  is the requested acces permission (name or hexadecimal)
  82. ECHO.
  83. ECHO Return codes: 0 for true (access), 1 for syntax error, 2 for false (no access)
  84. ECHO.
  85. ECHO Examples:
  86. ECHO     Check write acces:
  87. ECHO         %~n0 "HKLM\Software\My Software" KEY_SET_VALUE
  88. ECHO         %~n0 "HKLM\Software\My Software" 0x2
  89. ECHO     Check read control access:
  90. ECHO         %~n0 "HKLM\Software\My Software" READ_CONTROL
  91. ECHO         %~n0 "HKLM\Software\My Software" 0x20000
  92. ECHO.
  93. ECHO Note:   For more details on permission values, check this script's comments or
  94. ECHO         http://msdn.microsoft.com/en-us/library/aa384911^(v=VS.85^).aspx
  95. EXIT /B 1
  96.