Thursday, February 11, 2010

Get Client MAC Address & IP Address using Javascript

This is somewhat similar to the previous post Get client cpu id with javascript, so the client still needs to enable ActiveX components on his browser. Read more on the said post for details.

Ok if you're done reading, you'll notice we simply change the script to this:

<script type="text/javascript">
    var macAddress = "";
    var ipAddress = "";
    var computerName = "";
    var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
    e = new Enumerator(wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"));
    for(; !e.atEnd(); e.moveNext()) {
        var s = e.item();
        macAddress = s.MACAddress;
        ipAddress = s.IPAddress(0);
        computerName = s.DNSHostName;
    }
</script>

Instead of Win32_Processor, here we'll access Win32_NetworkAdapterConfiguration to read network related details like the MAC Address, IP Address and the computer name.

Then we can simply use textboxes to display that information or whatever you like.

<input type="text" id="txtMACAdress" />
<input type="text" id="txtIPAdress" />
<input type="text" id="txtComputerName" />

<script type="text/javascript">
    document.getElementById("txtMACAdress").value = unescape(macAddress);
    document.getElementById("txtIPAdress").value = unescape(ipAddress);
    document.getElementById("txtComputerName").value = unescape(computerName);
</script>

And just like before, make sure to place the declarations snippet above this script for it to display the values properly. Now the client can see his/her own MAC and IP Addresses.

Read more about the Win32_NetworkAdapterConfiguration object here http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx


-k

45 comments:

  1. UPDATED:

    Set query to "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True" to get only active values.

    ReplyDelete
  2. Very Helpful !!

    ReplyDelete
  3. "automation server can't create object" error occured

    ReplyDelete
  4. "automation server can't create object" error occured

    ReplyDelete
  5. automation server can't create object" error occured

    ReplyDelete
    Replies
    1. please enable everything related to active X in internet options->local intranet->costum

      Delete
  6. Automation server can't create object

    ReplyDelete
  7. If "Automation server can't create object" error occurs, go to Tools -> Internet Options, Security tab -> Custom level and enable "Initialize and script ActiveX controls not marked as safe for scripting" setting.

    ReplyDelete
    Replies
    1. i made that but is still couldnt create object

      Delete
    2. GetObject("winmgmts:{impersonationLevel=impersonate}") dose not work

      Delete
  8. This is only useful where the client and server have a trusted relationship. On the general web this simply won't work.

    ReplyDelete
  9. Also only useful on Windows clients!!!

    ReplyDelete
  10. Will it work on a linux machine?

    ReplyDelete
  11. ^ I don't think so, because the object (Win32) we try to access here is for Windows.

    ReplyDelete
  12. So how did Kamkar do it at the hacker con then eh?

    http://www.securityweek.com/hacker-uses-xss-and-google-streetview-data-determine-physical-location

    Obviously he must have found a way to do the impossible.

    ReplyDelete
  13. uh.. thats the mac address of the last router passing the packets not the computer requesting the web page

    ReplyDelete
  14. this will work perfect
    ps.ramkumar@gmail.com
    function HttpRequest(){
    var pageRequest = false //variable to hold ajax object
    /*@cc_on
    @if (@_jscript_version >= 5)
    try {
    pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
    }
    catch (e){
    try {
    pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
    }
    catch (e2){
    pageRequest = false
    }
    }
    @end
    @*/

    if (!pageRequest && typeof XMLHttpRequest != 'undefined')
    pageRequest = new XMLHttpRequest()
    if (pageRequest){ //if pageRequest is not false
    pageRequest.open('GET', "http://checkip.dyndns.org", false) //get page synchronously
    pageRequest.send(null)
    if (window.location.href.indexOf("http")==-1 || request.status==200)
    //document.write(request.responseText)
    alert(request.responseText);
    extractIP(new String(request.responseText));
    }
    }

    function extractIP(testString){
    split = testString.split(":");
    nextSpl = split[1].split("<");
    document.getElementById("txtIPAdress").value = unescape(nextSpl[0]);
    }

    ReplyDelete
    Replies
    1. not working at my end please help me the regarding

      Delete
  15. I dont understand how you can actually use Javascript to retrieve remote users mac address without them activating ActiveX controls?

    ReplyDelete
  16. Thanks a lot!
    Very helpful and exactly what I needed! :)

    ReplyDelete
  17. How do we get mac address if the browser is Firefox?

    ReplyDelete
  18. Did you find something, i have the same problem...

    ReplyDelete
  19. Hi,
    It is working fine during debug mode, but after hosting the application, its not work . why?
    can anyone help me about this?

    ReplyDelete
  20. Nice try. If you could get a clients mac address with javascript that would be an egregious security hole and we would all be forced to turn off javascript support in our browsers.:(
    You would have to write a plug in (non IE) or an activeX control (IE) to do that sort of thing.

    Getting the client's gateway IP is easy with a request.servervariables at the server so there is little need to do it via javascript.

    ReplyDelete
  21. Please write plugin for working in nonIE or IE
    if need contact to sonntcil@yahoo.com
    I have just create plugin that can operate on CHROME, IE, FIREFOX , MAC OSX 10.6 above

    ReplyDelete
  22. it´s only for windows?

    ReplyDelete
  23. pls can i get a complete script, the above script is not working. pls pls

    ReplyDelete
  24. Well it isn't possible,usually.Also, you probably shouldn't care. The Java Script code found on internet is not compatible with internet explorer(IE8) .

    Thanks
    Silvester Norman

    Change Mac Address

    ReplyDelete
  25. hi,
    it is not working in mozilla firefox .....any solution for run active x object in firefox ???

    ReplyDelete
  26. can we get mac address with out using activeX using javascript ? if yes then how

    ReplyDelete
  27. Will this work for cell phones? Not very well. The best we might be able to do is find the nearest cell tower at the time of use.

    ReplyDelete
  28. hi, I have a problem ('GetObject' is undefined) using this code somebody could help me to resolve this

    ReplyDelete
    Replies
    1. but what about getting mac address of smart tv programming javascript??

      Delete
  29. but what about getting mac address of smart tv programming javascript??

    ReplyDelete
  30. What you're saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I'm sure you'll reach so many people with what you've got to say.

    ReplyDelete
  31. How to CREATE OBB FOLDER FOR OBB FILE ANDROID GAME
    This post will tell you how to create the OBB Folder of any game if you have its .obb file.
    .obb file is a file that contains the game’s graphics data and some other details. Not all the games have obb files with them.
    How to CREATE OBB FOLDER FOR OBB FILE ANDROID GAME
    If you have a zip folder that contains OBB, then you need to read this article How to CREATE OBB FOLDER FOR OBB FILE ANDROID GAME

    If you have a .obb file (not a folder) and you want to create an OBB folder with this file, then this post is perfect for you.
    Pre-requisites:
    .obb file
    Laptop or PC
    Android Device
    USB cord
    How to create OBB folder for OBB file
    Read more here How to CREATE OBB FOLDER FOR OBB FILE ANDROID GAME

    Naijabiggies

    InfoClickNigeria

    Rev HVBUZZ

    ReplyDelete
  32. Thank you for the share. You are helping others to grow their knowledge by sharing such a valuable information very helpful

    ReplyDelete
  33. Hi, How to get client IP address in IE? Please anyone help me with this. Thanks a lot in advance

    ReplyDelete