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

48 comments:

  1. UPDATED:

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

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

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

    ReplyDelete
  4. 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
  5. Automation server can't create object

    ReplyDelete
  6. 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
  7. This is only useful where the client and server have a trusted relationship. On the general web this simply won't work.

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

    ReplyDelete
  9. Will it work on a linux machine?

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

    ReplyDelete
  11. 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
  12. uh.. thats the mac address of the last router passing the packets not the computer requesting the web page

    ReplyDelete
  13. 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
  14. I dont understand how you can actually use Javascript to retrieve remote users mac address without them activating ActiveX controls?

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

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

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

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

    ReplyDelete
  19. 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
  20. 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
  21. it´s only for windows?

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

    ReplyDelete
  23. 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
  24. hi,
    it is not working in mozilla firefox .....any solution for run active x object in firefox ???

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

    ReplyDelete
  26. 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
  27. 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
  28. but what about getting mac address of smart tv programming javascript??

    ReplyDelete
  29. 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
  30. Thank you for the share. You are helping others to grow their knowledge by sharing such a valuable information very helpful

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

    ReplyDelete
    Replies
    1. no one will help you...pray to god

      Delete
  32. Replies
    1. This comment has been removed by the author.

      Delete