I was wondering how can I through a Python web app, get the DNS configuration of a remote client who's connected to my app.
I would like to know how I can tell what DNS server the remote client is using.
It would look like something like this : https://www.perfect-privacy.com/dns-leaktest/
You can get the same information as from the service you linked, but
- You will need more than a web app
- The information may not be entirely accurate
The way the service works is by embedding resources in the HTML page that are in turn attempted to be downloaded by the browser. Downloading requires the browser to resolve the domain name, which allows you to force a DNS request from the browser to your authoritative DNS servers. By using a unique domain name for each client or request, you can track the DNS server requesting the unique domain name from your servers.
The process would go like this:
Generate a random, unique ID for each page request, e.g. rndun1qu3
(must be a valid DNS label, consult RFC1035). This must be a new ID every time so that it will not be present in any DNS cache.
Embed a HTML <img>
element in the page to trigger a download, e.g. <img src="rndun1qu3.your-tracking-domain.com/example.png">
. As the browser sees this domain the first time, it will attempt to resolve rndun1qu3.your-tracking-domain.com
. Note that anything that triggers a DNS request is OK for this purpose, e.g. AJAX calls, <script>
elements, etc.
Monitor the DNS requests arriving to the DNS server responsible for rndun1qu3.your-tracking-domain.com
. When you see a request for rndun1qu3.your-tracking-domain.com
, note the remote peer IP address requesting the DNS information (that will be the IP address you are looking for), then notify your web application.
The above explains why I said you will need more than a web app -- you will also need a DNS server that you can monitor. This may be a plain vanilla DNS server with logs that you can monitor, or something purpose-built. There are plenty of open source projects and even Python examples you can customize for your purposes. All you need to do is to point the authoritative servers for your subdomain to that server.
As for why the information may not be accurate, consider that DNS servers are often configured to forward all requests to another server instead of going through the root DNS servers. In this case, the DNS request will arrive from the server that eventually carries out the name resolution, i.e. the forwarder server. For instance, your corporate network may have a DNS server at 192.168.251.1 with a public IP of 1.2.3.4, but if it forwards requests to Google DNS, you may very well see that the request came from 8.8.8.8 (the Google DNS IP) instead of 1.2.3.4. There is no way in DNS to find out the original host requesting the query, so that is pretty much the best you can get.