Get User Mac Address?

I’m using the uuid module to try to capture the source mac address ( cleinte ). Using the code below locally I can get the source mac address (local device), but when I remotely run the destination mac address is captured, (I believe it is from the remote device)? I know that the place where the mac addresses are processed (from source to destination), in the TCP/IP protocol stack, is in the Data Link layer. But how do I be able to get the Origin Mac Address ?

from wsgiref.simple_server import make_server
import uuid, re

def wsginter(environ, start_response):

    status = '200 OK'
    headers = [('Content-type', 'text/plain; charset=utf-16')]
    start_response(status, headers)

    macs = ':'.join(re.findall('..', '%012x' % uuid.getnode()))

    return [str(macs).encode('utf-16')]

# Syntax Objective -> start the test server and trigger the function intermediary to wsgiref
with make_server('', 8000, wsginter) as httpd:

    print(f'Serving on port 8000...')
    # Serve until process is killed
    httpd.serve_forever()

You want the MAC address of the client that made the connection to your server? You can’t use the uuid module for that, and in general you can’t get the MAC address of the client unless you are located on the same LAN (layer 2 network) as they are. If there are any routers between the client and the server, the server will only be able to see the MAC address of the nearest router to it.

But the header that is mounted at the data link layer in the TCP/IP protocol stack doesn’t come registered with the Mac Address from origin to Mac Address from destination ?

Yes, but if there is a router between the (original) origin and the server, the router becomes the origin, from the server’s point of view.

       origin == client's MAC             origin == router's MAC
Client --------------------------> Router --------------------------> Server
       destination == router's MAC        destination == server's MAC

Typically, there are multiple routers between a client and a server on the internet.

2 Likes

True, I’ll have to try to capture another way.
Thanks.

As @abessman pointed out, the ‘data link’ is only the link to the next hop, it’s not the link all the way to the IP destination. Thus the ‘data link layer’ information does not pass across that hop.