Registry Berechtigung

Anfängerfragen zum Programmieren mit PureBasic.
Coder Pinhead
Beiträge: 234
Registriert: 27.02.2007 10:54
Wohnort: Germany

Registry Berechtigung

Beitrag von Coder Pinhead »

Hallo,

Also ich würde gerne bei einen bestimmten Wert in der Registry die Berechtigung auf Verweigern stellen.

Code: Alles auswählen

If RegCreateKeyEx_(#HKEY_CURRENT_USER, "Software\Programm\Ordner", 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_EXECUTE, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
  StringBuffer$ = ""
  RegSetValueEx_(NewKey, "", 0, #REG_SZ,  StringBuffer$, Len(StringBuffer$)+1)
  RegSetKeySecurity_(NewKey, SecurityInformation, pSecurityDescriptor)
  RegCloseKey_(NewKey)
EndIf
Das sollte ja mit RegSetKeySecurity() gehen nur wie?

Danke schon mal im voraus für die Hilfe :).

Grüße,
Windows 8.1 x64 | PureBasic v5.x x86/x64

Sämtliche Syntax, Tipp und Rechtschreibfehler unterliegen dem Copyright des Verfassers.
Benutzeravatar
NeoXiD
Beiträge: 6
Registriert: 17.10.2011 18:51
Computerausstattung: http://www.sysprofile.de/id157572
Wohnort: Schweiz
Kontaktdaten:

Re: Registry Berechtigung

Beitrag von NeoXiD »

Uff.. hoffen wir mal dass Coding Status: Still beginner nicht mehr ganz zutrifft ;) Ansonsten hast du dir da keine leichte API rausgepickt, aber ich habe dir mal einen Examplecode gebastelt und kommentiert:

Code: Alles auswählen

EnableExplicit

; Constants
#SDDL_REVISION_1 = 1
#DACL_SECURITY_INFORMATION = 4

; Defines
Define newKey.l, keyInfo.l, testString.s, libraryID.l
Define securityAttr.SECURITY_ATTRIBUTES, daclString.s

; Load some functions out of advapi32.dll
libraryID.l = OpenLibrary(#PB_Any, "advapi32.dll")
If IsLibrary(libraryID)
  ; Define a prototype
  Prototype.l ptConvertStringSecurityDescriptorToSecurityDescriptor(StringSecurityDescriptor.s, StringSDRevision.l, SecurityDescriptor.l, SecurityDescriptorSize.l)
  
  ; Get the right function (ANSI or Unicode?)
  Define ConvertStringSecurityDescriptorToSecurityDescriptor.ptConvertStringSecurityDescriptorToSecurityDescriptor
  CompilerIf #PB_Compiler_Unicode = 0
    ConvertStringSecurityDescriptorToSecurityDescriptor = GetFunction(libraryID, "ConvertStringSecurityDescriptorToSecurityDescriptorA")
  CompilerElse
    ConvertStringSecurityDescriptorToSecurityDescriptor = GetFunction(libraryID, "ConvertStringSecurityDescriptorToSecurityDescriptorW")
  CompilerEndIf
Else
  ; [TODO] Add some error handler if library can't be loaded
  End
EndIf

; Delete old keys with wrong rights
; [ATTENTION] Only works when you've got the necessary rights - See below!
If RegOpenKeyEx_(#HKEY_CURRENT_USER, "Test", 0, #KEY_READ | #KEY_WRITE, @newKey)
  RegDeleteKey_(@newKey, "Test")
EndIf

; Create the registry key with the necessary rights
If RegCreateKeyEx_(#HKEY_CURRENT_USER, "Test", 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS , 0, @newKey, @keyInfo) = #ERROR_SUCCESS
  ; Fill security attributes structure with default values
  With securityAttr
    \bInheritHandle = #False
    \nLength = SizeOf(SECURITY_ATTRIBUTES)
  EndWith
  
  ; Create DACL string
  daclString.s = "D:P"                         ; Discretionary ACL
  
  ; ---------------------------------------
  ; DIFFERENT EXAMPLES
  ; 
  ; DON'T JUST COPY & PASTE - CHOOSE ONE EXAMPLE CAREFULLY AND COMMENT THE OTHER EXAMPLES OUT
  ; ---------------------------------------
  
  ; Example 1 - block all access, only administrators could access (no read & no write for everyone else)
  daclString.s + "(D;OICI;GA;;;BG)"           ; Deny all access to built-in guests
  daclString.s + "(D;OICI;GA;;;AN)"           ; Deny all access to anonymous logons
  daclString.s + "(D;OICI;GA;;;AU)"           ; Deny all access to authenticated users
  daclString.s + "(A;OICI;GA;;;BA)"           ; Allow all access for administrators
  ; Example 2 - block all write access, only administrators could write (no write but read for everyone else)
  daclString.s + "(A;OICI;GR;;;BG)"           ; Allow all read access to built-in guests
  daclString.s + "(A;OICI;GR;;;AN)"           ; Allow all read access to anonymous logons
  daclString.s + "(A;OICI;GR;;;AU)"           ; Allow all read access to authenticated users
  daclString.s + "(A;OICI;GA;;;BA)"           ; Allow all access for administrators
  ; Example 3 - block everything & everyone
  daclString.s + "(D;OICI;GA;;;BG)"           ; Deny all access to built-in guests
  daclString.s + "(D;OICI;GA;;;AN)"           ; Deny all access to anonymous logons
  daclString.s + "(D;OICI;GA;;;AU)"           ; Deny all access to authenticated users
  daclString.s + "(D;OICI;GA;;;BA)"           ; Deny all access for administrators
  
  ; ---------------------------------------
  ; END OF DIFFERENT EXAMPLES
  ; ---------------------------------------
  
  ; Convert DACL string to security descriptor
  ConvertStringSecurityDescriptorToSecurityDescriptor(daclString, #SDDL_REVISION_1, @securityAttr\lpSecurityDescriptor, 0)
  
  ; Set security descriptor & close the registry key
  RegSetKeySecurity_(newKey, #DACL_SECURITY_INFORMATION, securityAttr\lpSecurityDescriptor)
  
  ; [IMPORTANT] AFTER everything, create the desired keys
  testString = "4 PureBoard" + Str(Random(9999))
  RegSetValueEx_(newKey, "test", 0, #REG_SZ, @testString, Len(testString) + 1)
  
  ; Close the key
  RegCloseKey_(newKey)
EndIf
Ich bitte dich allerdings, den Code zu studieren und nicht nur zu kopieren & einfügen. Du kannst damit ziemlich dumme Dinge anstellen und es ist alles ein wenig kompliziert. Wenn du Verständnisfragen hast, frag ruhig nach! Bedenke, dass alle APIs aktuell direkt aufgerufen werden - Ohne Errorhandler, den du noch jeweils einbauen solltest. (Also nur weitermachen, wenn die API erfolgreich war!) Ausserdem musst du im Rechtebereich dir ein Beispiel raussuchen, du kannst nicht alle 3 drin haben, ansonsten wird regedit meckern dass du komisch angeordnete Berechtigungen hast ;) Dann noch ein paar Doku-Links:

RegCreateKeyEx_ Funktion
RegSetKeySecurity Funktion
SECURITY_DESCRIPTOR Struktur
Guter MSDN Artikel zu DACL, ACE etc. (Das sind die Formate für Dateirechte)

Das wärs dann auch "schon" mit der Fachliteratur. Ich hoffe du kannst mit all dem was anfangen, ansonsten frag ruhig nach. Aber wie angemerkt, ist keine leichte Funktion, habe jetzt selber ein rechtes Weilchen lang recherchiert bis ich all das zusammenhatte :)
Gruss
NeoXiD

Bild
lite
Beiträge: 122
Registriert: 27.08.2012 21:08

Re: Registry Berechtigung

Beitrag von lite »

Hallo

Ist es möglich Registry-Berechtigungen nachträglich zu ändern ?

Grüße
Lite
Antworten