Friday, 7 December 2007

Chapter 11, lesson 3: Using Declarative and Imp[erative Security to Protect Methods.


  • CAS - declarative - compiler performs security checks prior to running code.
  • CAS - imperative - code itself performs checks and controls what happens.
  • SecurityAction.Demand, CodeAccessPermission.Demand() - checks an assm's caller for permissions.
  • SecurityMnager.IsGranted() - checks whether an assm itself has particular CAS perms.

Techniques for Limiting Permissions

  • use CAS assm decl to restrict perms granted to assm as a whole.
  • Then control perms on a more granular lvl by restricting perms within mthd decl + using imperative stmts.
  • EXAM HINT: use RequestRefuse(==Deny) and RequestOptional(==PermitOnly) for assm decl, and Deny and PermitOnly for mthds.
  • Exception handling - revert to PermitOnly (say for logging) and then revert.

  • Assert() - on a perm, it means that any code calling the mthd on which Assert is invoked is vouched for - it does not need said perm itself.

Tuesday, 27 November 2007

Chapter 11, lesson 2: Using Declarative Security to Protect Assemblies.

Because CAS can restric permissions given to an app., you must write apps to run in a partially trusted security context.

Use declarative CAS demands - ensure that assm has all necessary permissions but none that it does not require.

Reasons to use CAS Assm Decl

  • To ensure that the runtime will never run your app without granting access to all required resources - if user attempts to run app and CAS security policy does not grant a required permission, an exception is thrown. Use SecurityAction.RequestMinimum - to declare all CAS perms required by app.
  • To create a sandbox for app to ensure an attacker does not manipulae it to access unintended resources - use CAS decl to reduce your assms

Classes for CAS Permissions

  • For each type of resource (e.g. files/folders, printers, network access,...) that can be restricted by CAS there is a .NF class.
  • Permission Attribute classes - inherit from CodeAccessSecurityAttribute class.
  • Action property - specifies how the runtime will interpret the permission. Set to...
  • 1) SecurityAction.RequestMinimum - requires a perm. for assm to run. If assm lacks specified perm then runtime throws exception.
  • 2) SecurityAction.RequestOptional -
  • 3) SecurityAction.RequestRefuse -
  • NOTE: CAS is significant only for partially trusted assmebles. The runtime completely ignores CAS decl for fully trusted assemblies.
  • combine RequestMinimum and RequestOptional - for non-negotiable permissions for 1 thing, but nothing else required.
  • UIPermission, Unrestricted = true - for debugging + RequestOptional.

Friday, 23 November 2007

Chapter 11, lesson 1: Understanding Code Access Security.

Code Access Security (CAS) - enables users to control permissions that individual apps have. Developers thus have to create apps that work with restricted permissions.

Can also use CAS as a developer - by restricting which calers can use your code + forcibly limiting your app to a restricted permission set.

---
  • What is CAS - a security system that aloows administrators + developers to control application authorization.
  • can control auth to following resources: file sys., registry, printers, event logs, sending web requests, whether app can make DNS requests.
  • CAS can only be applied to managed apps - ones that use the .NF runtime. Unmanaged apps limitied only by O.Ss role-based security (RBS).
  • CAS identifies and assigns permissions to apps rather than to people.
  • CAS IDs assembiles ~ evidence.
  • evidence - location where assm is stored, a hash of assm code, assm signature.
  • An assembly's evidence determine which code group it belongs to.
  • code groups - grant an assm a permission set.
  • evidence - info that runtime gathers about an assm to determine which code group it belongs to e.g. folder or web site that it runs from, digitial signatures. It's info that provs identity and describes an assm as deserving a certain level of trust.
  • types of evidence - appn dir (in whcih assm resides), hash of assm, publisher (their digitial signature thus IDs software developer), Site (from which assm was downloaded), Strong Name, URL (from where downloaded).
  • host evidence - describes assm origin.
  • assm evidence - custom user or develoer-provided evidence.
  • Permission - a CAS access control entry e.g. File Dialog - determines whether an assm can prompt user with the Open dialog box. There exist 19 permissions. You can add custom permissions.
  • Permission set - CAS ACL - a group of permissions that apply to apps falling under a particular group. (e.g. LocalIntranet Zone contains more permissions that Internet). .NF has 7 default permission sets.
  • code groups - authorization devices that associate assm with permission sets. Membership to a code group is not set manually - it is determined by the evidence that you specify as the code group's membership condition.
  • Internet_Zone code group - membership condition - host presents Zone evidence + Zone evidence IDs assm as being in Internet zone.
  • A code group must be associated with a permission set.
  • You can only specify only a single type of evidence and a single permission set for a code group.
  • An assm can be a member of mult. code groups.
  • union of permsiions - assm will receive perms as union of all code grps.
  • security policy - a logical grouping of code groups and permission sets.
  • security policy - allow administrators to configure CAS settings at 4 lvls: Enterprise, Machine, User, App Domain.
  • Enterprise Security Policy - configured ~ Active directory svc.
  • Intersection of the permission sets - rutime evalutes Enterprise, Machine & User levels separately, and grants assm minimum set of perms.
  • .NF Configuration Tool - to grant/restrict an assm perms: 1. Evaluate assm to determine which code grps it is a member of, 2. eval assm to det. perms it will be assigned, 3. adding new perm sets, 4. adding mew code groups, 5. increasing an assm's trust.
  • CAS wokrs on top of OS security - if CAS grants an assm access to write to a folder but the user running the assm does not have that perm, then assm cannot write.
  • CAS Policy Tool - caspol.exe - to examine/modify machine/user/enterprise-lvl code access policies.

chapter 10, lesson 4: Detecting Management Events.

.System.Management namespace - tools to monitor + manage the system, devices and apps ~ WMI technology.

Enumerating Management Objects
  • DirectoryObjectSearcher obj - to programatically access resources ~ WMI.
  • mimics SQL.
  • To execute query:
    1. ConnectionOptions (UserName, Password). 2. instance of DirectoryObjectSearcher, 3. Instance of ManagementScope obj - set path 4. Instance of ObjectQuery obj., 5. ManagementObjectCollection = DirectoryObjectSearcher.Get()
  • enumerating logical drives - specify target - e.g. Win32_LogicalDisk.
  • enumerating network adapters - Win32_NetworkConfiguration - new ManagemmentObjectSearcher("SELECT * FRO MWin32_NetworkConfiguration");
  • enumerating windows services - Win32_Service - ManagementObjectCollection AllObjects = DemoSearcher.Get();

Wednesday, 21 November 2007

chapter 10, lesson 3: Monitoring Performance.

...for fixing perf probs like slow/sluggish UI, network access, db connectivity,...

  • Process class - can ref O.S process running ol local or remote machine.
  • Enumerating processes -
  • 1) GetCurrentProcess(),
  • 2) GetProcessById() - if process not found, ArgumentException thrown.
  • 3) GetProcessByName(),
  • 4) GetProcesses() - ArgumentException the 'catch-all' exception out of the set that could be couaght here.
  • PerformanceCounter class - for comparing baseline measurements against current measurements.
  • There exist built-in perf counters - dont reinvent the wheel!
  • CounterCreationData class - a container obj of props needed for PerformanceCounter obj.
  • PerformanceCounterCategory class - manage/manipulate PerformanceCounter objs + their categories. (See if given perf counter exists and if not, create it).

chapter 10, lesson 2: debugging and tracing.

Debug, Debugger classes - more efficient than stepping through code - less time consuming.

Debugger Class
  • enables comm with a debugger app.
  • Break() - equiv of setting bk'pt manually.
  • Log() - posts info to the attached Debugger, if present. To listener objs attached to debugger.
  • Listener - DefaultTraceListener - take Log mthds and write as text to specified target.

Debug Class

  • more granularity than Debugger.
  • Assert() - for true/false conditions - if condition isn't true it will break into the debugger automatically.
  • in release build no debug commands are compiled in so no perf degredation.
  • Fail() - doesn't use an evaluation - simply breaks at offending line and outputs failure msg.
  • Write(), WriteIf()...

Debug Attributes

  • DebuggerBrowsable attr - [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  • DebuggerDisplay - configure top line for class in Locals window.
  • DebuggerHidden - stops bk'pt being set inside anything it decorates.

Monday, 19 November 2007

chapter 10, lesson 1: logging events.

  • create an event log - EventLog class. 1 or 2 actions, then use static mthds, else create instance.
  • writing - EventLog.WriteEntry() -
  • built-in logs - Aplication, Security, System logs.
  • iterate thro entries - Entries property - contains EventLogEntry properties.