Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / XML
Tip/Trick

Manipulate network adapter via WMI in plain C and C++

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
18 Dec 2021GPL33 min read 5.4K   8  
Win32_NetworkAdapterConfiguration WMI class in plain C and C++
Manipulate Win32_NetworkAdapterConfiguration WMI class in pure C and C++

 

Introduction

 

The Win32_NetworkAdapterConfiguration WMI class can manipulate the attributes of a network adapter. We created a fully working real world example program called BlockDNS, which shows how to use the methods and properties of this WMI class.

Image 1

Background

BlockDNS is used to block DNS traffic for OpenVPN in Windows XP, because the OpenVPN implementation for Windows XP and Server 2003 can not effectively block DNS traffic and we have DNS leaks during the VPN connection.

After many days of research this WMI class seems to be the only officially supported way to manipulate IP addresses of a network adapter that Microsoft provides in Windows XP and Server 2003. We found a few examples during the research, but none of them worked out of the box. A big problem of the code was, that most times the network adapter index was hardcoded. To make a working sample we have to retrieve the class object path for the ExecMethod of the network adapter.

C++
// get the class object path
hr = clsObj->lpVtbl->Get(clsObj,L"__PATH",0,&pathVariable,NULL,NULL);
if(FAILED(hr))
{
    printf("Error: Get \"__PATH\" HRESULT 0x%.08X\n",hr);
    rc = 13;
    goto cleanup;
}

We included two example versions of BlockDNS to illustrate the differences between plain C and C++ code for COM objects. The differences are minimal and can clearly be seen if you compare the source code files.

Program Structure

The program is structured in the following 4 steps:

Step 1 is used for OpenVPN before the connection is established. For static network adapters we get the static DNS servers. After that we save the static DNS server entries to a global structure.

Step 2 is used for OpenVPN before the connection is established. For DHCP enabled network adapters we set IP address, subnet mask, default gateway and DNS server to static values. We save the static DNS server entries to a global structure.

Step 3 is used for OpenVPN directly after the connection is established. For all network adapters we remove the static DNS servers completely. This way every possible DNS leak attempt is blocked.

Step 4 is used for OpenVPN directly after the connection is closed. For DHCP enabled network adapters we reenable DHCP and set the old static DNS servers if there were any present before. For static network adapters we set the old static DNS servers.

Using the code

The code can be used easily. We only included one source code file for the C and C++ example. We made no extra header to make the sample more clean to read. Pay attention that we link against the old Microsoft Visual C++ 6 import library msvcrt.lib. This way we don't have to distribute any VC-Redistributable to make the code work on Windows XP / Server 2003. In the release configuration you will see the following two warnings during linking, which is normal and does not trigger any later problems at runtime.

Image 2

Credits

The source code was inspired by the internet site "www.dnsleaktest.com". This site has an executable called "dnsfixsetup.exe" which includes the 3 scripts called "pre.vbs", "up.vbs" and "down.vbs". These scripts can also block the DNS leak for OpenVPN in Windows XP successfully.

Many thanks go out to the guys at "dnsleaktest.com", which did a great job with their DNS blocking scripts!

History

  • 18th December, 2021 - Initial release

This project is also available at Sourceforge at https://sourceforge.net/projects/blockdns/.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --