Impersonation server service account vs. client account
13.4.16
vjj
1
Identity • server identity (IIS, SQL, …) • • • • •
Administrator Local System (IIS – webové stránky) Local Service Network Service (ASP.NET, SQL) custom
• user identity • • •
13.4.16
(local) domain internet (anonymous) vjj
2
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
•
•
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
• • • 13.4.16
Domain | Computers | computer | Properties | Delegation
Security Settings | Local Policies | User Rights Assignment guest or temporary account error: Account cannot be delegated
vjj
3
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
Server
Account is sensitive and cannot be delegated 13.4.16
vjj
4
IIS + ASP.NET sample
impersonating applications
13.4.16
vjj
5
pro koho jsou určeny ... ? • • • • •
13.4.2016
...
IIS / ASP.NET stránky
...
data zpřístupňovaná těmito stránkami
Internet
- pro každého
Internet
- jen pro vyvolené
Intranet
- jen pro vyvolené
vjj
6
ú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
autentizace a autorizace
• ASP.NET
autentizace a autorizace
13.4.2016
vjj
7
IIS + ASP.NET IIS
.NET Framework
INETINFO.EXE
W3WP.EXE ( ASPNET_WP.EXE )
ASPNET_ISAPI.DLL
Application domain
HTTP request (ASPX / ASMX)
SQL server
13.4.2016
vjj
8
IIS
Klient není přihlášen ve stejné doméně Windows
Klient je přihlášen ve stejné doméně Windows
Anonymous
Windows integrated Impersonation
IUSR_machinename
user domain account
ASP.NET None
Form
Windows
IUSR_machinename
web account
user domain account
Impersonation
IUSR_machinename
13.4.2016
mapped account
vjj
user domain account
Default
Network Service
9
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
10
IIS autentizace • Anonymous Authentication • ASP.NET Impersonation • Forms Authentication • Windows Authentication
13.4.2016
vjj
11
authentication - web.config
<system.web>
13.4.2016
vjj
12
None
13.4.16
vjj
13
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
13.4.2016
vjj
14
Form
13.4.16
vjj
15
Form - myLogin.aspx
ID="UserName" RunAt="server" />
ID="Password" RunAt="server" />
1/2
13.4.2016
Text="LogIn" OnClick="OnLogIn" RunAt="server" /> vjj
16
Form - web.config <system.web> <user name="Bob" password="heslo1" /> <user name="Alice" password="heslo2" /> 13.4.2016
vjj
17
Form - myLogin.aspx
2/2
<script language="C#" runat="server">
void OnLogIn( Object sender, EventArgs e ) { if( FormsAuthentication.Authenticate ( UserName.Text, Password.Text ))
else }
FormsAuthentication. RedirectFromLoginPage (UserName.Text, Persistent.Checked); Output.Text = "Invalid login";
13.4.2016
vjj
18
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."; } }
13.4.16
vjj
20
Windows
13.4.16
vjj
21
Windows integrated - web.config <system.web> <deny users = "Alice, Bob" /> <deny users = "*" /> všichni ostatní <deny users = "?" /> neautentizovaní, anonymní 13.4.2016
vjj
22
Windows integrated - web.config <system.web> <deny users="*" /> <system.web> <deny users="*" /> 13.4.2016
vjj
23
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
•
•
• 13.4.2016
ASPNET NETWORK SERVICE který dostal od IIS
default pro IIS 5.0 default pro IIS 6.0, ... impersonifikace
viz nastavení v souboru web.config vjj
24
ASP.NET autorizace • URL
- pravidla ve web.config
• ACL
- (Access Control List) nastavení přístupových práv pro jednotlivé soubory a adresáře
13.4.2016
vjj
25
Run as ...
13.4.16
vjj
26
Run as ... CreateProcessWithTokenW ( hToken, dwLogonFlags,
lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo,
lpProcessInfo );
13.4.2016
vjj
27
Run as ... CreateProcessAsUser ( hToken, lpApplicationName, lpCommandLine,
lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment,
lpCurrentDirectory, lpStartupInfo, lpProcessInformation );
13.4.2016
vjj
28
LogonUser HANDLE hToken ;
LogonUser ( lpszUsername, lpszDomain, lpszPassword, LOGON32_LOGON_SERVICE, 0, &hToken );
13.4.2016
vjj
29
zkratka BOOL WINAPI CreateProcessWithLogonW ( lpUsername, lpDomain,
lpPassword, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags,
lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInfo );
13.4.2016
vjj
30
Get-Credential $cred = Get-Credential
$cred = Get-Credential –Credential "username" $cred.username [Runtime.InteropServices.Marshal]:: PtrToStringAuto( [Runtime.InteropServices.Marshal]:: SecureStringToBSTR($cred.Password))
13.4.16
vjj
31
Impersonation
13.4.16
vjj
32
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
33
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
• •
• 13.4.2016
XP aplikace musí být spuštěna pod administrátorským účtem – "Run as administrator" Vista,… musí dojít k elevaci procesu
• •
Win32 API .NET
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ý vjj
34
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
•
13.4.16
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á vjj
35
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
• • •
•
13.4.2016
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ý
vjj
36
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
37
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) ; 13.4.16
vjj
38
LogonUser (P/Invoke: API -> .NET) [DllImport("advapi32.dll", SetLastError=true)]
static extern int LogonUser (String
13.4.16
UserName,
String
Domain,
String
Password,
int
LogonType,
int
LogonProvider,
ref IntPtr
Token) ; vjj
39
Windows Identity II. WindowsIdentity
wid =
new WindowsIdentity( "userName" );
13.4.16
vjj
40
Impersonation if( myAdjustPrivilege( (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 ( ) ; 13.4.16
// without use of "using" vjj
41
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, &Privileges.Privileges[0].Luid )) { MessageBox (NULL, (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
without TCB activation • zastupování bez aktivace privilegia TCB slouží pouze pro identifikaci uživatele
13.4.16
vjj
49
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" 13.4.16
vjj
50