Search code examples
javawindowscmddnssniffer

Listen DNS requests with Java in continuous and execute cmd command


Ok let's be precise. For many days I've been asking myself a question : Could it be possible to listen DNS request on a computer ( in my case a windows machine ) and ask to the program to execute a shell command if a special DNS is detected ?

I've been playing with this command :

ipconfig /displaydns | find "my.dns.com"

And as expected I'm able to find "my.dns.com" in my DNS cache.

BUT I'm not able to ask to the cmd :

Hey, listen for all the requests in continuous and when you detect "my.dns.com" please execute "music.exe".

So I imagined a program ( in Java ) doing it for me. Why java ? Because i'ts the only programming language that I'm able to use, I started with it so it's easier for me to understand it. But if what I'm looking for is not possible in Java, let me know it !

BUT ( again ) I don't know how to manage with DNS requests and neither ask for a java program to run in continuous. For the moment here is everything I could add to my program :

if ("my.dns.com" is detected) {
try {
String command = "cmd /c start music.exe";
Process child = Runtime.getRuntime().exec(command);
OutputStream out = child.getOutputStream();
} catch (IOException e) {
}

And maybe for running in continuous :

boolean running = true;
while(running)
{
  //main loop...
}

I know, that is very few but I don't even know where to look to answer my question.

Please, if you'r able to understand me ( Yes you behind your screen while eating Doritos ) could you at least help me to find my way ?

Precise if you want me to edit it :)


Solution

  • Perhaps this (untested) clip of PowerShell will get you going. I do not know that it is a good idea to clear the DNS cache, but that is up to you. If you do not, it will still be in there.

    while ($true) {
        $a = Get-DnsClientCache | ForEach-Object { $_.Name }
        if ($a -contains 'the.dns.com') {
            Start-Process C:\path\to\music.exe -Wait
            Clear-DnsClientCache
        }
    }