Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Machine's MAC Address -- Good Solution?

I've heard it's not possible with my current library of winpcap.

Is this really true? I see lots of examples on the net but then comments saying "This doesn't work".

What's the best way to get a MAC address of the local machine?

like image 909
bobber205 Avatar asked Jan 15 '10 06:01

bobber205


People also ask

What is the benefit of using MAC address learning?

Here are pros/benefits of using MAC address: It provides a secure way to find senders or receivers in the network. MAC address helps you to prevent unwanted network access. MAC address is a unique number; hence it can be used to track the device.

What can you do with someones MAC address?

If you know the MAC address of someone's device it is possible to 'spoof' it and steal someone's internet connection. The only way you can connect to a computer remotely is using an IP or Internet Protocol address, and even then there are many security features in place to prevent you from doing such.


1 Answers

One common method is using bits from a UUID, but this isn't entirely dependable. For example, it'll return a value even on a machine that doesn't have a network adapter.

Fortunately, there is a way that works dependably on any reasonably recent version of Windows. MSDN says it only goes back to Windows 2000, but if memory serves, it also works on NT 4, starting around SP 5, in case anybody's still using NT 4.

#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>

int main() {         
    IP_ADAPTER_INFO *info = NULL, *pos;
    DWORD size = 0;

    GetAdaptersInfo(info, &size);

    info = (IP_ADAPTER_INFO *)malloc(size);

    GetAdaptersInfo(info, &size);

    for (pos=info; pos!=NULL; pos=pos->Next) {
        printf("\n%s\n\t", pos->Description);
        printf("%2.2x", pos->Address[0]);
        for (int i=1; i<pos->AddressLength; i++)
            printf(":%2.2x", pos->Address[i]);
    }

    free(info);
    return 0;
}

Please forgive the ancient C code...

like image 177
Jerry Coffin Avatar answered Oct 16 '22 12:10

Jerry Coffin