Skip to content

Instantly share code, notes, and snippets.

@maldevel
Created October 31, 2018 06:48
Show Gist options
  • Save maldevel/769cda5be3a2f9d0d30baf102196f1f8 to your computer and use it in GitHub Desktop.
Save maldevel/769cda5be3a2f9d0d30baf102196f1f8 to your computer and use it in GitHub Desktop.
Dexter retrieve active network interface physical address (MAC) snippet
//https://github.com/twelvesec/dexter
//GNU General Public License v3.0
//@maldevel
//...
std::string libsysteminfo::get_active_netface_mac(void) {
std::string mac = "";
DWORD size = 0;
ULONG result = 0;
PIP_ADAPTER_ADDRESSES aAddr = NULL;
PIP_ADAPTER_ADDRESSES aAddrIndex = NULL;
PIP_ADAPTER_UNICAST_ADDRESS aAddrUnicast = NULL;
char *buf;
unsigned long bufSize = 50;
if ((result = GetAdaptersAddresses(0, 0, 0, 0, &size)) != ERROR_BUFFER_OVERFLOW) {
return "";
}
if ((aAddr = (PIP_ADAPTER_ADDRESSES)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size)) == NULL) {
return "";
}
if (GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, 0, aAddr, &size) != NO_ERROR) {
HeapFree(GetProcessHeap(), 0, aAddr);
aAddr = NULL;
return "";
}
aAddrIndex = aAddr;
while (aAddrIndex) {
if ((aAddrIndex->IfType == 6 || aAddrIndex->IfType == 71) && aAddrIndex->OperStatus == 1 &&
std::wstring(aAddrIndex->Description).find(L"Hyper-V") == std::string::npos &&
std::wstring(aAddrIndex->Description).find(L"VirtualBox") == std::string::npos &&
std::wstring(aAddrIndex->Description).find(L"VMware") == std::string::npos) {
if ((buf = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufSize)) == NULL) {
continue;
}
_snprintf_s(buf, bufSize, _TRUNCATE, "%02X-%02X-%02X-%02X-%02X-%02X", aAddrIndex->PhysicalAddress[0], aAddrIndex->PhysicalAddress[1],
aAddrIndex->PhysicalAddress[2], aAddrIndex->PhysicalAddress[3], aAddrIndex->PhysicalAddress[4], aAddrIndex->PhysicalAddress[5]);
mac = std::string(buf);
HeapFree(GetProcessHeap(), 0, buf);
buf = NULL;
}
aAddrIndex = aAddrIndex->Next;
}
HeapFree(GetProcessHeap(), 0, aAddr);
aAddr = NULL;
return mac;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment