DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why ChatGPT Is Not as Intelligent as Many Believe
  • 2-Tier Architecture vs 3-Tier Architecture in DBMS
  • Your Old Laptop Is Your New Database Server
  • Modeling Saga as a State Machine

Trending

  • Building a Software Factory With AI Agents and Workflows
  • Multi-Agent Orchestration on AWS With AgentCore Runtime and A2A
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough
  • Git Branching Strategies for Maintainable Test Automation

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Asynchronous WMI Queries: Stay Away From Them

Asynchronous WMI Queries: Stay Away From Them

By 
Sasha Goldshtein
·
Sep. 22, 12 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

So, it turns out that I have a WMI category on my blog. During the last couple of years I almost forgot about it, but WMI got a chance to wrap its poisonous tentacles around me again yesterday. Here’s another story.

WMI is known for requiring lots of attention to security. To establish a WMI connection to a remote machine, you need to muck around with registry settings, DCOM configuration, group policy details, and other infernal things which we developers like to defer to someone else. But at least you know that once a machine has been configured properly to give you access through WMI, you can then access it from any other machine. Right? Right?

Not so much. WMI has a concept of asynchronous queries, which are notably used for receiving event notifications. For example, the following code registers for an event notification whenever a process is created on my desktop machine:

ManagementScope scope = new ManagementScope(@"\\sasha-desktop\root\cimv2");
WqlEventQuery query = new WqlEventQuery(
                             "SELECT * FROM Win32_ProcessStartTrace");
ManagementEventWatcher watcher = new ManagementEventWatcher(scope, query);
watcher.EventArrived += (o, e) => ...; //TODO: process the event
watcher.Start();

Indeed, this thing works just fine if you point it to a local machine; but it fails when you call the Start method when you connect it to a remote machine. You could now strip the remote machine bare and have it expose its very innate networking guts to the entire Internet, and it still wouldn’t help you establish the connection. Interesting.

When troubleshooting this nasty bug, I looked up a VBScript sample that receives new process creation events on another machine. Here it is:

Set wmi = GetObject("winmgmts:\\sasha-desktop\root\cimv2")
Set query = wmi.ExecNotificationQuery _       
    ("SELECT * FROM Win32_ProcessStartTrace'")
Set process = query.NextEvent

VBScript and all, it worked just fine. I started to suspect something smelly in the kingdom of .NET, so I rewrote the VBScript sample in C#, using the long-forgotten Microsoft.VisualBasic.Interaction class:

dynamic wmi = Microsoft.VisualBasic.Interaction.GetObject(
                   "winmgmts:\\sasha-desktop\root\cimv2");
dynamic query = wmi.ExecNotificationQuery(
                   "SELECT * FROM Win32_ProcessStartTrace");
dynamic evt = query.NextEvent;

This, too, worked just fine – although it’s not much a surprise, as it’s pretty much equivalent to the VBScript code at this time. Still interesting.

This is when it hit me – the asynchronous nature of the ManagementEventWatcher.EventArrived event relies on an asynchronous WMI query, which requires a reverse connection to the client machine! This is configuration inferno, x2, on the client machine now, what with the DCOM security settings and sacrifices to the gods of group policy.

Unless, of course, we give away the asynchrony and rely on the ManagementEventWatcher.WaitForNextEvent method. It’s synchronous. It burns a thread that has to sit idly by and wait while its siblings execute useful work. But it doesn’t establish a reverse DCOM connection to the caller. At least that.

Windows Management Instrumentation Database Machine

Opinions expressed by DZone contributors are their own.

Related

  • Why ChatGPT Is Not as Intelligent as Many Believe
  • 2-Tier Architecture vs 3-Tier Architecture in DBMS
  • Your Old Laptop Is Your New Database Server
  • Modeling Saga as a State Machine

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook