Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

SecurityException with PerformanceCounter on remote server in .NET 2.0

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Geoff McElhanon - 24 Aug 2006 17:54 GMT
I have been struggling with a security issue that occurs under .NET
2.0, but does not occur under .NET 1.1.  Essentially I am trying to
open up a performance counter on a remote server and monitor its value.
In .NET 1.1 this worked fine, however under .NET 2.0 it fails when I
am not an administrator on the remote server.

To provide a lean demonstration of the issue, I created the following
class:

============================

using System;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication3
{
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                string machineName = null;

                // Was exactly 1 argument was passed?
                if (args.Length != 1)
                {
                    // Prompt for machine name
                    Console.Write("Please enter a machine name: ");
                    machineName = Console.ReadLine();
                }
                else
                {
                    // Use command line argument
                    machineName = args[0];
                }

                // Get the performance counter
                PerformanceCounter c = new PerformanceCounter("Processor", "%
Processor Time", "_Total", machineName);

                // Display counter value once every second for 5 seconds
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine(c.RawValue);
                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("==============");
                Console.WriteLine(ex);
                Console.WriteLine("==============");
            }
            finally
            {
                Console.WriteLine();
                Console.WriteLine("Hit ENTER to quit application.");
                Console.ReadLine();
            }
        }
    }
}

============================

I then created a batch file that compiles this file under both .NET 1.1
and .NET 2.0:

c:\windows\Microsoft.NET\Framework\v1.1.4322\csc.exe
/out:CounterTest11.exe /t:exe Class1.cs
c:\windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
/out:CounterTest20.exe /t:exe Class1.cs

Now, when I run either version against a server on which I am an
administrator, I get the following output:
--------
C:\Projects\ConsoleApplication3>CounterTest20.exe
Please enter a machine name: speck
6708935078125
6708945000000
6708954921875
6708964609375
6708974687500

Hit ENTER to quit application.
--------

When I run the .NET 1.1 version against a server on which I am only a
member of the "Performance Monitoring" group, I get the following
results:
--------
C:\Projects\ConsoleApplication3>CounterTest11.exe
Please enter a machine name: bonnie
114549931445312
114549938945312
114549946445312
114549954023437
114549961523437

Hit ENTER to quit application.
--------

But when I run the .NET 2.0 version against the same server, I get the
following:
--------
C:\Projects\ConsoleApplication3>CounterTest20.exe
Please enter a machine name: bonnie
==============
System.Security.SecurityException: Requested registry access is not
allowed.
  at System.ThrowHelper.ThrowSecurityException(ExceptionResource
resource)
  at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean
writable)
  at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
  at
System.Diagnostics.PerformanceCounterLib.FindCustomCategory(String
categor
y, PerformanceCounterCategoryType& categoryType)
  at System.Diagnostics.PerformanceCounterLib.GetCategoryType(String
machine, S
tring category)
  at System.Diagnostics.PerformanceCounter.Initialize()
  at System.Diagnostics.PerformanceCounter..ctor(String categoryName,
String co
unterName, String instanceName, String machineName)
  at ConsoleApplication3.Class1.Main(String[] args)
The Zone of the assembly that failed was:
MyComputer
==============

Hit ENTER to quit application.

Now, I posted this earlier to another forum but the only suggestion was
that I was not running with Full Trust.  I am launching the application
locally, and so I don't understand how I could not be running under
Full Trust.  I poked around in the .NET 2.0 configuration tool, but
could not come up with any ideas.

Any insight into this problem would be very much appreciated!

Geoff McElhanon
Willy Denoyette [MVP] - 24 Aug 2006 21:55 GMT
V2 uses WMI to collect performance information from the remote perfmon
(through the remote registry), that means that the impersonated user
(remote) must be a member of the (remote)administrators alias, only
administrators are allowed to read perf data.

Willy.

|I have been struggling with a security issue that occurs under .NET
| 2.0, but does not occur under .NET 1.1.  Essentially I am trying to
[quoted text clipped - 144 lines]
|
| Geoff McElhanon
Geoff McElhanon - 24 Aug 2006 23:13 GMT
Thank you, oh bearer of bad news.  :-(

But seriously... thank you.

> V2 uses WMI to collect performance information from the remote perfmon
> (through the remote registry), that means that the impersonated user
> (remote) must be a member of the (remote)administrators alias, only
> administrators are allowed to read perf data.
>
> Willy.
Willy Denoyette [MVP] - 25 Aug 2006 12:38 GMT
I would never use the PerformanceCounter class to access remote systems when
not running in a domain realm, much better is to use System.Management
clases (WMI wrappers) to access management objects like performance
counters. Note that even in this case you need to enable remote access to
the WMI service namespace.
Willy.

| Thank you, oh bearer of bad news.  :-(
|
[quoted text clipped - 6 lines]
| >
| > Willy.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.