Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1905 connectés 

  FORUM HardWare.fr
  Programmation
  Delphi/Pascal

  "Executer en tant que"

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

"Executer en tant que"

n°523034
caRnAGE
Posté le 25-09-2003 à 11:17:04  profilanswer
 

Bonjour à tous,
 
Voilà, j'essaie de créer un process qui tournera avec les droits d'un autre utilisateur. En fait, j'ai plein de postes clients dont les utilisateurs n'ont pas assez de droits pour modifier leur base de registre, et mon but est donc de créer un petit prog qui va la modifier au démarrage de leur session.
Le seul bout de code que je suis arrivé à trouvé sur google jusque maintenant est celui-ci :
 

Citation :

procedure WinExecAsUser(FileName: string; username: string; password: string; Visibility: integer);
var { V1 by Pat Ritchey, V2 by P.Below }
 zAppName          : array[0..512] of char;
 StartupInfo       : TStartupInfo;
 ProcessInfo       : TProcessInformation;
 h                 : thandle;
begin
 StrPCopy(zAppName, FileName);
 FillChar(StartupInfo, Sizeof(StartupInfo), #0);
 StartupInfo.cb := Sizeof(StartupInfo);
 StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
 StartupInfo.wShowWindow := Visibility;
 if not LogonUser(pchar(username), '.', pchar(Password),LOGON32_LOGON_INTERACTIVE,
   LOGON32_PROVIDER_DEFAULT, h) then
   ShowMessage(SysErrorMessage(GetLastError));
 CreateProcessAsUser(h, nil,
   zAppName, { pointer to command line string }
   nil, { pointer to process security attributes }
   nil, { pointer to thread security attributes }
   false, { handle inheritance flag }
   CREATE_NEW_CONSOLE or { creation flags }
   NORMAL_PRIORITY_CLASS,
   nil, { pointer to new environment block }
   nil, { pointer to current directory name }
   StartupInfo, { pointer to STARTUPINFO }
   ProcessInfo); { pointer to PROCESS_INF }
 if GetLastError <> 0 then ShowMessage(SysErrorMessage(GetLastError));
end;


 
Mais j'ai un message d'erreur du type : "A required privilege is not held by the client" ...  
Pourtant, j'utilise le login/pass administrateur de la machine.
 
Quelqu'un aurait-il une idée ?
Merci à tous !

mood
Publicité
Posté le 25-09-2003 à 11:17:04  profilanswer
 

n°523136
gatorette
Posté le 25-09-2003 à 13:17:50  profilanswer
 

caRnAGE a écrit :

Mais j'ai un message d'erreur du type : "A required privilege is not held by the client" ...  
Pourtant, j'utilise le login/pass administrateur de la machine.


 
Il serait pas mal de savoir à quel appel de fonction ce message apparaît. Est-ce que c'est au moment de "LogonUser" ou de "CreateProcessAsUser" ?
 
Voici un extrait de la "doc MSDN de LogonUSer" :

Citation :

Windows 2000:  The process calling LogonUser requires the SE_TCB_NAME privilege. If the calling process does not have this privilege, LogonUser fails and GetLastError returns ERROR_PRIVILEGE_NOT_HELD. In some cases, the process that calls LogonUser must also have the SE_CHANGE_NOTIFY_NAME privilege enabled; otherwise, LogonUser fails and GetLastError returns ERROR_ACCESS_DENIED. This privilege is not required for the local system account or accounts that are members of the administrators group. By default, SE_CHANGE_NOTIFY_NAME is enabled for all users, but some administrators may disable it for everyone. For more information about privileges, see Privileges.


 
Je pense que ton application n'a pas le privilège SE_TCB_NAME, donc ton LogonUser échoue. Je ne sais pas très bien comment attribuer ce privilège a ton application, mais voici un bout de code (en C) qui peut marcher (je l'ai adapté d'un code à moi qui permettait d'éteindre le PC).

Code :
  1. HANDLE hToken = INVALID_HANDLE_VALUE;
  2. if( !OpenProcessToken( GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken ) )
  3. {
  4. DWORD dwErr = GetLastError();
  5. // impossible d'obtenir le security token de notre application
  6. }
  7. TOKEN_PRIVILEGES tp;
  8. LUID luid;
  9. if( !LookupPrivilegeValue( NULL, SE_TCB_NAME, &luid ) )
  10. {
  11. DWORD dwErr = GetLastError();
  12. // impossible de trouver l'identifiant système du privilège
  13. }
  14. tp.PrivilegeCount = 1;
  15. tp.Privileges[0].Luid = luid;
  16. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  17. // Enables the privilege
  18. if( !AdjustTokenPrivileges( hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL ) )
  19. {
  20. DWORD dwErr = GetLastError();
  21. // impossible de définir ce privilège
  22. }


---------------
each day I don't die is cheating
n°523172
caRnAGE
Posté le 25-09-2003 à 13:56:30  profilanswer
 

Effectivement, j'ai aussi pensé ça, et j'ai même trouvé une fontion delphi permettant de donner des privileges à mon token (qui d'ailleurs au passage utilise aussi "AdjustTokenPrivileges" ), mais même en me le donnant, j'ai le même message.
 
J'ai essayé de trouver d'autres pistes, et je me suis rendu compte qu'il existe une fonction windows "CreateProcessWithLogonW", à laquelle il suffit de passer en parametre juste le minimum pour utiliser le login que l'on veut, du coup, je me suis fait un bout de code pour l'utiliser, mais mon appli me répond qu'il n'arrive pas à m'authentifier :
 
 

Code :
  1. var
  2. Jycroisamort : function (lpUsername: PAnsiChar; lpDomain: PAnsiChar;
  3.   lpPassword: PAnsiChar; dwLogonFlags: DWORD; lpApplicationName : PAnsiChar;
  4.   lpCommandLine : PAnsiChar; dwCreationFlags : DWord; lpEnvironment : Pointer;
  5.   lpCurrentDirectory : PAnsiChar; const lpStartupInfo : TStartupInfo ;
  6.   var lpProcessInfo : TProcessInformation) : boolean ; stdcall;
  7. procedure WinExecAsUser(FileName: string; username: string; password: string; Visibility: integer);
  8. var { V1 by Pat Ritchey, V2 by P.Below }
  9. zAppName          : array[0..512] of char;
  10. StartupInfo       : TStartupInfo;
  11. ProcessInfo       : TProcessInformation;
  12. h                 : thandle;
  13. begin
  14.   StrPCopy(zAppName, FileName);
  15.   FillChar(StartupInfo, Sizeof(StartupInfo), #0);
  16.   StartupInfo.cb := Sizeof(StartupInfo);
  17.   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  18.   StartupInfo.wShowWindow := Visibility;
  19.   h :=  LoadLibrary(PChar('advapi32.dll'));
  20.   Jycroisamort := GetProcAddress(h,pchar('CreateProcessWithLogonW'));
  21.   Jycroisamort(pchar(username),'.',pchar(password),$1,nil,zappname,$400,nil,nil,StartupInfo,ProcessInfo);
  22.   if GetLastError <> 0 then ShowMessage(SysErrorMessage(GetLastError));
  23.   FreeLibrary(h);
  24. end;
  25. ;


 
 
le $1 et le $400, ce sont les équivalent que j'ai trouvé sur le net pour "LOGON_WITH_PROFILE" et "CREATE_UNICODE_ENVIRONMENT".
Valà, je vais essayer d'adapter ton bout de code pour voir.
 
Merci pour ton aide gatorette !


Message édité par caRnAGE le 25-09-2003 à 14:27:56

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Delphi/Pascal

  "Executer en tant que"

 

Sujets relatifs
Exécuter une library (.so) sous Linuxexecuter un script chaque jour à une heure +/- precise
Nouvelle commande executer !?Defit : Nouvelle commande executer
exécuter un programme externe sur la machine du visiteur[EXCEL] Exécuter une macro suite à une saisie
[Résolu] Impossible d'exécuter des javascriptexecuter du code 1 fois sur deux dans un while
[C] Executer une application à partir d'une autre 
Plus de sujets relatifs à : "Executer en tant que"


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)