1. Code access security
2. Identity-based security.
Identity-based Security Important classes are:
2. Principal objects
• FormsIdentity
• PassPortIdentity
• GenericIdentity
A Principal object is a holder for all the roles the user belongs to (according to the active authentication mechanism). Again, any .NET class implementing the IPrincipal interface is a valid Principal object. The IPrincipal interface exposes the Identity property (returning the underlying Identity object) and the IsInRole method.
For user base security – PrincipalPermission Class is used.: IPermission
For CAS – CodeAccessPermission Derived classes is used. : IPermission
IPermission Interface – Demand method – verifies if caller higher on the stack trace have not been granted permission specified by the permission object.
User Based Security:
- GenericIdentity – represent the identity of the user authenticated by custom authentication.
- Idenity gid = new GenericIdentity(Auth type, IsAuth., Name of user)
- WindowsIdentity – represent identity of the user authenticated by windows authentication.
- Idenity wid = new WindowsIdentidy(User token, AuthType., windows Account Type, IsAuth)User token is of IntPtr data type which refers to a user account token and can be a pointer or a handle.This can be obtained via unmanaged win32 APIs such as logonUser
- Windows identity object can also be obtained using GetCurrent and GetAnonymous methods of it’s class.
- FormIdentity
- PasssportIdentity
- IPrincipal ip = Thread.CurrentPrincipal.
- IPrincipal interface
- IIdentity – represents the identity object associated with principal object.
- IsInRole – take string role as param and verify if principal object roles collection contains given role.
- GenericPrincipal Object
- Constructed using GenericIdentity and Roles array. New GenericPrincipal(myGenericIdentity, srint() Roles)
- WindowsPrincipal Object
- Constructed by windows identity object as roles for particular identity are already mapped in windows
- By default principal policy on appdomain is GenericPrincipal so if authentication mode is windows we need to explicitly set the principal policy to windows else we wouldn’t get the user information we interested in. Also we need to cast the generic principal object retrived from Thread.CurrentPrincipal to windows principal object.
- Either user if statement to check role and name as required like
- If Principal.idenitty.name.Equals(“Gauri”) …..
- If Principal.Isinrole(“Admin”) ..
- Or use PrincipalPermission object demand method
- Pp = New PrincipalPermission(username , Role) Pp.Demand() – this line verify the username and role from principal objectr against username and role provided in the principalpermission object and if not verified will throw SecurityException.
- Or use Declarative Approach
- [PrincipalPermission(SecurityActin.Demand,Name = “Gauri”] – this method can only be executed by Gauri.
Cas is all about setting or getting the trustworthiness of an assembly.i.e What code you can trust and to what degree. Same application can have assemblies with different trust level
Security in .net is based on two fundamentals:
- Type safety of managed code: It means
- Bound checking is performed on all data types
- Prevent inappropriate or dangerous type conversion.
- Prevents direct memory manipulations.
- Vigilance of CLR
If code is granted SkipVerificaiton – then it is possible to bypass verification process.
Not all .net languages generate verifiable type safe code. Vb.net does always, C# does if unsafe is not used.
PEVerify.exe can be used to test if code is verifiable type safe.
Verifiable means some features according to JIT verification definition.
SkipVeification assemblies can only execute if they are fully trusted.
Using good code by some malicious client is called luring attack.
Using Demand code can verify if it has all permission required to execute before starting the execution.
Working
Security permissions are configured not at assemble level but at code groups level.
There are four levels:
- Enterprise level
- Machine level
- User level
- Appdomain level
Lower level can not have more liberal permission than higher level.
Each code group define Membership condition and has only one permission set. One permission set can define number of permission.
- Mscorcfg.msc – for manual configuration.
- Caspol.exe – for automated scripting
Some member ship conditions are:
- Publisher
- Site
- Strong Name – providing digital signature
- URL with protocol.
- Zone
- ….
Imperative:
- Using evidence classes (System.securityPolicy)
- Using derived codeAccessPermission classes.
Declarative:
- Get Evidence object, get it enumerator and loop thru it’s attributes. Like - Zone, URL, Hash, Strong Name, Site…
Ienumerator en = ev.GetEnumerator();
While en.MoveNext
Object item = en.Current
If Item.StrongName = “MyAssembly”
Do smth Secure
- webPermission
- FileIOPermission
- UrlIdentityPermission
- ZonalIdentityPermission
Imp method of this class are:
- Assert: it request the permission starting from calling function to down the stack trace if all code down the stack trace will have the permission as requested by assert calling object.Even if higher heriarchy denied the permission assert can be used to get it for some operations. Only one assert is allowed per stackframe else security exception is thrown.Use cautiously as it may introduce luring attack.
- Demand: It is used to verify the that calling method have the permission it does this by performing the a stalk walk checking the permission of each method found higher on the call stack
- Deny: use to restrict the permission down the stack. Only one deny per stack frame. UrlIdentityPermissin uip = new UrlIdentityPermissin(file://c:/./....) Uip.Demand()
- Declaritive:
- [UrlIdentityPermission(SecurityAction.LinkDemand,Url=”file://c:/../...”] [
( )]
- Assert
- Demand
- Deny
- LinkDemand
- PermitOnly
- RequestMinimum etc…
[assembly:UIPermission(SecurityAction.RequestMinimum,Clipboard=uipermissionclipbooard.allclipboard)]
assembly will be loaded only if it has minimum permission to access clipboard
Permission set class can also be configured in configuration file.