読者です 読者をやめる 読者になる 読者になる

めいくりぷとのブログ

技術的なことやゲームのことやら・・・

NIC Mac Address Authentication

C++ Security
// CAuthentication.hpp
#pragma once

#include <vector>

class CAuthentication
{
public:
	CAuthentication();
	~CAuthentication();

public:
	BOOL IsUserAllowed();
	void SetWhiteList();

public:
	static CAuthentication *GetInstance()
	{
		static CAuthentication instance;
		return &instance;
	}

private:
        void RegisterMACAddress(LPCWSTR lpcwszAddress);

	std::vector<LPCWSTR> m_vData;
};
// CAuthentication.cpp
#include "stdafx.h"

#include "CAuthentication.hpp"
#include "utils.hpp"

#include <NtDDNdis.h>
#include <IPHlpApi.h>
#pragma comment(lib, "IPHlpApi")

CAuthentication::CAuthentication()
{

}
CAuthentication::~CAuthentication()
{

}

void CAuthentication::SetWhiteList()
{
	this->RegisterMACAddress(L"AA:BB:CC:DD:EE:FF");
}

BOOL CAuthentication::IsUserAllowed()
{
	IP_INTERFACE_INFO *pInterface;
	HANDLE hDevice;
	ULONG uLength;
	UCHAR uResult[6];
	WCHAR wszAdapter[MAX_PATH];
	LPVOID lpEthernet = (LPVOID)OID_802_3_CURRENT_ADDRESS;

	if (GetInterfaceInfo(NULL, &uLength) == ERROR_INSUFFICIENT_BUFFER)
	{
		pInterface = (IP_INTERFACE_INFO*)malloc(uLength);

		if (GetInterfaceInfo(pInterface, &uLength) == NO_ERROR)
		{
			for (int i = 0; i < pInterface->NumAdapters; i++)
			{
                                // NIC GUID
				std::wstring wszBuffer, wszTemp;
				wszBuffer = pInterface->Adapter[i].Name;
				wszBuffer = L"\\\\.\\" + wszBuffer.substr(wszBuffer.find_first_of('{'), wszBuffer.size());

                                // Open NIC from GUID
				hDevice = CreateFile(wszBuffer.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
				if (hDevice == INVALID_HANDLE_VALUE)
					return FALSE;

                                // Get Physical Mac-Address
				if (!DeviceIoControl(hDevice, IOCTL_NDIS_QUERY_GLOBAL_STATS,
					&lpEthernet, sizeof(lpEthernet), &uResult, sizeof(uResult), NULL, NULL))
					return FALSE;

				for (int i = 0; i < 6; i++)
					StringCchPrintf(wszAdapter, _countof(wszAdapter), _T("%s%02X:"), wszAdapter, uResult[i]);

				wszTemp = wszAdapter;
				wszTemp = wszTemp.substr(0, wszTemp.size() - 1);
				if (this->m_vData.size() > 0)
				{
					for (int i = 0; i < this->m_vData.size(); i++)
					{
						if (wcscmp(this->m_vData.at(i), wszTemp.c_str()) == NO_ERROR)
						{
							return TRUE;
						}
					}
				}
			}
		}
	}

	return FALSE;
}

void CAuthentication::RegisterMACAddress(LPCWSTR lpcwszAddress)
{
	this->m_vData.push_back(lpcwszAddress);
}
// main.cpp
#include "stdafx.h"

#include "CAuthentication.hpp"

int main()
{
        CAuthentication *pAuthentication = CAuthentication::GetInstance();
        pAuthentication->SetWhiteList();
        if (!pAuthentication->IsUserAllowed())
        {
                MessageBox(NULL, L"Authentication failed. ", L"License Error", MB_OK);
                TerminateProcess(GetCurrentProcess(), -1);
        }

        MessageBox(NULL, L"Authentication succeeded! ", L"License", MB_OK);

        return 0;
}