13.4.16 vjj 1 Impersonation server service account vs. client account
13.4.16 vjj 2 Identity server identity (IIS, SQL, ) Administrator Local System (IIS webové stránky) Local Service Network Service (ASP.NET, SQL) custom user identity (local) domain internet (anonymous)
13.4.16 vjj 3 delegation When a service is trusted for delegation, that service can impersonate a user to use other network services The computer account can be set to Trusted for delegation to any service or Trusted for delegation to specified services only Domain Computers computer Properties Delegation An administrator must have the Enable computer and user accounts to be trusted for delegation privilege on the computer in order to enable delegation Security Settings Local Policies User Rights Assignment The account that the service is delegating for must not have the Account is sensitive and cannot be delegated option chosen Security Settings Local Policies User Rights Assignment guest or temporary account error: Account cannot be delegated
13.4.16 vjj 4 impersonation administrator Enable computer and user accounts to be trusted for delegation Server Trusted for delegation to any service or Trusted for delegation to specified services only impersonation user Account is sensitive and cannot be delegated Server
13.4.16 vjj 5 IIS + ASP.NET sample impersonating applications
13.4.2016 vjj 6 pro koho jsou určeny...?... IIS / ASP.NET stránky... data zpřístupňovaná těmito stránkami Internet Internet Intranet - pro každého - jen pro vyvolené - jen pro vyvolené
13.4.2016 vjj 7 úkol identifikovat autora každé žádosti - autentizace definovat pravidla, podle kterých se rozhoduje, kdo má přístup k té které stránce,... - autorizace IIS ASP.NET autentizace a autorizace autentizace a autorizace
13.4.2016 vjj 8 IIS + ASP.NET IIS.NET Framework HTTP request (ASPX / ASMX) INETINFO.EXE ASPNET_ISAPI.DLL W3WP.EXE ( ASPNET_WP.EXE ) Application domain SQL server
13.4.2016 vjj 9 Klient není přihlášen ve stejné doméně Windows Klient je přihlášen ve stejné doméně Windows IIS Anonymous Windows integrated Impersonation IUSR_machinename user domain account ASP.NET None Form Windows IUSR_machinename web account user domain account Impersonation Default IUSR_machinename mapped account user domain account Network Service
13.4.2016 vjj 10 IIS impersonation IIS - běží pod účtem Local System (NT AUTHORITY\SYSTEM) k souborům/stránkám přistupuje pod účtem IUSR_servername - anonymní přístup (NT AUTHORITY\ANONYMOUS LOGON) domain user nastavení v MMC snap-in \windows\system32\inetsrv\iis.msc
13.4.2016 vjj 11 IIS autentizace Anonymous Authentication ASP.NET Impersonation Forms Authentication Windows Authentication
13.4.2016 vjj 12 authentication - web.config <configuration> <system.web> <authentication mode="none" /> <authentication mode="forms" /> <authentication mode="windows" /> <authentication mode="passport" /> <identity impersonate="false" /> </system.web> </configuration>
None 13.4.16 vjj 13
13.4.2016 vjj 14 Anonymní přístup IIS impersonates the IUSR_servername account before executing any code IIS checks NTFS file and directory permissions (for IUSR_servername account) before returning a page to the client
Form 13.4.16 vjj 15
13.4.2016 vjj 16 Form - mylogin.aspx 1/2 <asp:textbox ID="UserName" RunAt="server" /> <asp:textbox ID="Password" RunAt="server" /> <asp:checkbox Text="pamatovat si přihlášení" ID="Persistent" RunAt="server" /> <asp:button Text="LogIn" OnClick="OnLogIn" RunAt="server" />
13.4.2016 vjj 17 Form - web.config <configuration> <system.web> <authentication mode="forms"> <forms loginurl="mylogin.aspx" timeout="30"> </forms> </authentication> </system.web> </configuration> <credentials passwordformat="clear"> <user name="bob" password="heslo1" /> <user name="alice" password="heslo2" /> </credentials>
13.4.2016 vjj 18 Form - mylogin.aspx 2/2 <script language="c#" runat="server"> void OnLogIn( Object sender, EventArgs e ) { if( FormsAuthentication.Authenticate ( UserName.Text, Password.Text )) } FormsAuthentication. RedirectFromLoginPage (UserName.Text, Persistent.Checked); else Output.Text = "Invalid login"; </script>
13.4.16 vjj 20 requested page protected void Page_Load(object sender, EventArgs e) { if (User.Identity.IsAuthenticated) { Page.Title = "Home page for " + User.Identity.Name; } else { Page.Title = "Home page for guest user."; } }
Windows 13.4.16 vjj 21
13.4.2016 vjj 22 Windows integrated - web.config <configuration> <system.web> <authentication mode = "Windows" /> <identity impersonate = "true" > <authorization> <allow users = "win\vjj, win\admin" /> <allow roles = "admins" /> <deny users = "Alice, Bob" /> <deny users = "*" /> všichni ostatní <deny users = "?" /> neautentizovaní, anonymní </authorization> </system.web> </configuration>
13.4.2016 vjj 23 Windows integrated - web.config <configuration> <location path="prvni.aspx"> <system.web> <authorization> <allow users="bob,alice" /> <deny users="*" /> </authorization> </system.web> </location> <location path="druha.aspx"> <system.web> <authorization> <allow users="mypc\bob" /> <deny users="*" /> </authorization> </system.web> </location> </configuration>
13.4.2016 vjj 24 ASP.NET ASPX / ASMX aplikace.net runtime (ASPNET_WP.EXE Windows XP a 2000, W3WP.EXE Windows 2003) nejdříve ověří, zda účet, který dostal od IIS, má oprávnění požadovanou stránku číst dále pak přistupuje k souborům/stránkám pod účtem ASPNET default pro IIS 5.0 NETWORK SERVICE default pro IIS 6.0,... který dostal od IIS impersonifikace viz nastavení v souboru web.config
13.4.2016 vjj 25 ASP.NET autorizace URL ACL - pravidla ve web.config - (Access Control List) nastavení přístupových práv pro jednotlivé soubory a adresáře
Run as... 13.4.16 vjj 26
13.4.2016 vjj 27 Run as... CreateProcessWithTokenW ( htoken, dwlogonflags, lpapplicationname, lpcommandline, dwcreationflags, lpenvironment, lpcurrentdirectory, lpstartupinfo, lpprocessinfo );
13.4.2016 vjj 28 Run as... CreateProcessAsUser ( htoken, lpapplicationname, lpcommandline, lpprocessattributes, lpthreadattributes, binherithandles, dwcreationflags, lpenvironment, lpcurrentdirectory, lpstartupinfo, lpprocessinformation );
13.4.2016 vjj 29 LogonUser HANDLE htoken ; LogonUser ( lpszusername, lpszdomain, lpszpassword, LOGON32_LOGON_SERVICE, 0, &htoken );
13.4.2016 vjj 30 zkratka BOOL WINAPI CreateProcessWithLogonW ( lpusername, lpdomain, lppassword, dwlogonflags, lpapplicationname, lpcommandline, dwcreationflags, lpenvironment, lpcurrentdirectory, lpstartupinfo, lpprocessinfo );
13.4.16 vjj 31 $cred = Get-Credential Get-Credential $cred = Get-Credential Credential "username" $cred.username [Runtime.InteropServices.Marshal]:: PtrToStringAuto( [Runtime.InteropServices.Marshal]:: SecureStringToBSTR($cred.Password))
Impersonation 13.4.16 vjj 32
13.4.2016 vjj 33 Impersonation vlákno procesu má standardně default Access Token svého procesu může ale získat Access Token určený pro zastupování jiného uživatele
13.4.2016 vjj 34 podmínky a omezení Windows Server 2000,... proces musí mít přiděleno privilegium SE_TCB_NAME jinak GetLastError vrátí ERROR_PRIVILEGE_NOT_HELD toto privilegium musí být aktivováno pro aktivaci příslušného privilegia XP účtem "Run as administrator" Vista, musí dojít k elevaci procesu Win32 API.NET aplikace musí být spuštěna pod administrátorským lze dynamicky za běhu aplikace jen při spuštění aplikace "Run as..." účet zastupovaného uživatele musí být doménový
13.4.16 vjj 35 TCB Trusted Computing Base Group Policy : (Start Control Panel Administrative Tools Local Security Policy) Windows Security Settings Local Policies User Rights Assignment Act as part of the operating system místo přidělení tohoto privilegia konkrétnímu uživatelskému účtu je prý vhodnější spustit zastupující aplikaci pod účtem SYSTEM, který privilegium TCB standardně má
13.4.2016 vjj 36 podmínky a omezení Windows Server 2000 SP4 (2009) proces musí mít přiděleno privilegium SeImpersonatePrivilege jinak GetLastError vrátí ERROR_PRIVILEGE_NOT_HELD local Administrators, local Service Services that are started by the Service Control Manager COM servers running under a specific account účet zastupovaného uživatele musí být doménový
13.4.16 vjj 37 Impersonate a client Group Policy : (Start Control Panel Administrative Tools Local Security Policy) Windows Security Settings Local Policies User Rights Assignment Impersonate a client after authentication
13.4.16 vjj 38 Windows Identity I. IntPtr token = IntPtr.Zero ; int ret = LogonUser (this.usernametextbox.text, this.userdomaintextbox.text, this.passwordtextbox.text, 2, 0, ref token); if (ret == 0) { MessageBox.Show ( System.Runtime.InteropServices.Marshal.... } GetLastWin32Error() ) ; WindowsIdentity wid = new WindowsIdentity (token) ;
LogonUser (P/Invoke: API ->.NET) [DllImport("advapi32.dll", SetLastError=true)] static extern int LogonUser (String UserName, String Domain, String Password, int LogonType, int LogonProvider, ref IntPtr Token) ; 13.4.16 vjj 39
Windows Identity II. WindowsIdentity wid = new WindowsIdentity( "username" ); 13.4.16 vjj 40
13.4.16 vjj 41 if( myadjustprivilege( Impersonation (string)"setcbprivilege", (bool)true ) ) using( WindowsImpersonationContext wic = { wid.impersonate( ) ) using( StreamWriter file = new StreamWriter( "WhoseFile.txt" )) { file.writeline("check the owner of this file"); file.close( ); } } // wic.undo ( ) ; // without use of "using"
myadjustprivilege (P/Invoke) [DllImport ( "myadjustprivilege.dll", CharSet = CharSet.Auto, EntryPoint = "myadjustprivilege", ExactSpelling = false, BestFitMapping = true, CallingConvention = CallingConvention.Winapi )] public static extern System.Boolean myadjustprivilege ( string lpprivilegename, System.Boolean benable ); 13.4.16 vjj 44
myadjustprivileges.cpp -> dll BOOL WINAPI myadjustprivilege ( LPCWSTR lpprivilegename, BOOL benable) { TOKEN_PRIVILEGES Privileges; HANDLE htoken; BOOL bresult; if (!OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &htoken)) { MessageBox(NULL,(LPCWSTR)L"OpenProcesToken failed", (LPCWSTR)L"AdjustPrivilege FAILURE", MB_OK); return FALSE; } Privileges.PrivilegeCount = 1; Privileges.Privileges[0].Attributes = (benable)? SE_PRIVILEGE_ENABLED : 0; 13.4.16 vjj 46
myadjustprivileges.cpp -> dll if (!LookupPrivilegeValueW( NULL, lpprivilegename, { MessageBox (NULL, &Privileges.Privileges[0].Luid )) (LPCWSTR)L"LookupPrivilegeValueW failed", (LPCWSTR)L"AdjustPrivilege FAILURE", MB_OK); CloseHandle(hToken); return FALSE; } bresult = AdjustTokenPrivileges (htoken, FALSE, &Privileges, 0, NULL, NULL); } CloseHandle(hToken); return bresult; 13.4.16 vjj 47
13.4.16 vjj 49 without TCB activation zastupování bez aktivace privilegia TCB slouží pouze pro identifikaci uživatele
13.4.16 vjj 50 without TCB activation using( WindowsIdentity wid = new WindowsIdentity( "username" ) ) { using(windowsimpersonationcontext wic = wid.impersonate( ) ) { MessageBox.Show("Impersonating: " + WindowsIdentity.GetCurrent().Name + "\n\nimpersonation Level: " + newid.impersonationlevel.tostring());... // exception: // Either a required impersonation level was not provided // or the provided level is invalid } } // wic.undo ( ) ; // without use of "using"