145 lines
4.3 KiB
C++
145 lines
4.3 KiB
C++
#include <Windows.h>
|
|
#include <iostream>
|
|
#include <comdef.h>
|
|
#include <Wbemidl.h>
|
|
|
|
#pragma comment(lib, "wbemuuid.lib")
|
|
|
|
int net_nat() {
|
|
HRESULT hr;
|
|
|
|
// Initialize COM
|
|
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
|
|
if (FAILED(hr)) {
|
|
std::cout << "Failed to initialize COM" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
// Initialize security
|
|
hr = CoInitializeSecurity(NULL,
|
|
-1,
|
|
NULL,
|
|
NULL,
|
|
RPC_C_AUTHN_LEVEL_DEFAULT,
|
|
RPC_C_IMP_LEVEL_IMPERSONATE,
|
|
NULL,
|
|
EOAC_NONE,
|
|
NULL);
|
|
if (FAILED(hr)) {
|
|
std::cout << "Failed to initialize security" << std::endl;
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
|
|
// Obtain the initial locator to WMI
|
|
IWbemLocator *pLocator = NULL;
|
|
hr = CoCreateInstance(CLSID_WbemLocator,
|
|
0,
|
|
CLSCTX_INPROC_SERVER,
|
|
IID_IWbemLocator,
|
|
reinterpret_cast<LPVOID *>(&pLocator));
|
|
if (FAILED(hr)) {
|
|
std::cout << "Failed to create IWbemLocator object" << std::endl;
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
|
|
// Connect to the root\StandardCimv2 namespace with the current user
|
|
IWbemServices *pServices = NULL;
|
|
hr = pLocator->ConnectServer(_bstr_t(L"ROOT\\StandardCimv2"), NULL, NULL, 0, NULL, 0, 0, &pServices);
|
|
if (FAILED(hr)) {
|
|
std::cout << "Failed to connect to WMI namespace" << std::endl;
|
|
pLocator->Release();
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
|
|
// Set the authentication information to the current user
|
|
hr = CoSetProxyBlanket(pServices,
|
|
RPC_C_AUTHN_WINNT,
|
|
RPC_C_AUTHZ_NONE,
|
|
NULL,
|
|
RPC_C_AUTHN_LEVEL_CALL,
|
|
RPC_C_IMP_LEVEL_IMPERSONATE,
|
|
NULL,
|
|
EOAC_NONE);
|
|
if (FAILED(hr)) {
|
|
std::cout << "Failed to set proxy blanket" << std::endl;
|
|
pServices->Release();
|
|
pLocator->Release();
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
|
|
// Get the NAT class object
|
|
IWbemClassObject *pClass = NULL;
|
|
hr = pServices->GetObject(_bstr_t("MSFT_NetNat"), 0, NULL, &pClass, NULL);
|
|
if (FAILED(hr)) {
|
|
std::cout << "Failed to get NAT class object" << std::endl;
|
|
pServices->Release();
|
|
pLocator->Release();
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
|
|
// Spawn an instance of the NAT class
|
|
IWbemClassObject *pInstance = NULL;
|
|
hr = pClass->SpawnInstance(0, &pInstance);
|
|
if (FAILED(hr)) {
|
|
std::cout << "Failed to spawn NAT instance" << std::endl;
|
|
pClass->Release();
|
|
pServices->Release();
|
|
pLocator->Release();
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
|
|
// Set the properties of the NAT instance
|
|
VARIANT var;
|
|
VariantInit(&var);
|
|
|
|
// Set the name of the NAT instance
|
|
var.vt = VT_BSTR;
|
|
var.bstrVal = SysAllocString(L"MyNATRule");
|
|
hr = pInstance->Put(L"Name", 0, &var, 0);
|
|
if (FAILED(hr)) {
|
|
std::cout << "Failed to set NAT instance name" << std::endl;
|
|
VariantClear(&var);
|
|
pInstance->Release();
|
|
pClass->Release();
|
|
pServices->Release();
|
|
pLocator->Release();
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
|
|
// Set other properties as needed
|
|
// ...
|
|
|
|
// Create the NAT instance
|
|
IWbemCallResult *pResult = NULL;
|
|
hr = pServices->PutInstance(pInstance, WBEM_FLAG_CREATE_ONLY, NULL, &pResult);
|
|
if (FAILED(hr)) {
|
|
std::cout << "Failed to create NAT instance" << std::endl;
|
|
VariantClear(&var);
|
|
pInstance->Release();
|
|
pClass->Release();
|
|
pServices->Release();
|
|
pLocator->Release();
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
|
|
// Cleanup
|
|
VariantClear(&var);
|
|
pResult->Release();
|
|
pInstance->Release();
|
|
pClass->Release();
|
|
pServices->Release();
|
|
pLocator->Release();
|
|
CoUninitialize();
|
|
|
|
std::cout << "NAT rule created successfully" << std::endl;
|
|
|
|
return 0;
|
|
} |