Jump to content

Recommended Posts

Posted

How can I scan a network for mDSN devices and see there TXT records.
A while a go I stript down the DIG udf to make it a mDNS discovery device, see the example.
I search the internet over 5 hours but there is little information how this works.
Is there someone how knows how to scan the network for the devices (shows all the mDNS devices) and get there TXT records?

 

$mdsn_return = _mDNS("http://xxxxxxx.local")
ConsoleWrite($mdsn_return & @CRLF)


Func _mDNS($domain)
    Local $return_ip, $domain_convert, $mDNS_msg

    $domain = StringReplace($domain , "http://", "")
    Local $domain_array = StringSplit($domain, ".")
    For $i = 1 To $domain_array[0]
        $domain_convert = $domain_convert & Hex(BinaryLen($domain_array[$i]), 2) & StringTrimLeft(StringToBinary($domain_array[$i]), 2)
    Next

    UDPStartup()

    Local $mDNS_sock = UDPOpen("224.0.0.251", 5353)
    If @error Then
        UDPShutdown()
        SetError(1)
        Return ""
    EndIf

    Local $query_time = TimerInit()

    UDPSend($mDNS_sock, "0x5A8901000001000000000000" & $domain_convert & "0000010001")

    Do
        $mDNS_msg = UDPRecv($mDNS_sock, 512, 1)
    Until $mDNS_msg <> "" Or TimerDiff($query_time) > 1000

    UDPShutdown()

    For $i = 3 To 0 Step -1
        $return_ip = $return_ip & Dec(StringMid(BinaryMid($mDNS_msg, BinaryLen($mDNS_msg) - $i, 1), 3))
        If $i <> 0 Then
            $return_ip = $return_ip & "."
        EndIf
    Next

    If $return_ip <> "0.0.0.0" Then
        Return $return_ip
    Else
        SetError(2)
    EndIf
EndFunc

 

Posted

To scan a network for mDNS devices and retrieve their TXT records, you can utilize the mDNS protocol and perform the necessary DNS queries

Assuming  you have the necessary privileges to send and receive UDP packets on the network and appropriate network setup to allow mDNS traffic you can try this: 

 

#include <Array.au3>
#include <String.au3>
#include <UDPOs.au3>

Local $mDNSDevices = _ScanMDNSDevices()
If @error Then
    MsgBox(16, "Error", "Failed to scan mDNS devices.")
    Exit
EndIf

For $i = 0 To UBound($mDNSDevices) - 1
    ConsoleWrite("Device: " & $mDNSDevices[$i][0] & ", IP: " & $mDNSDevices[$i][1] & @CRLF)

    Local $txtRecords = _GetMDNSTXTRecords($mDNSDevices[$i][1])
    If @error Then
        MsgBox(16, "Error", "Failed to retrieve TXT records for device: " & $mDNSDevices[$i][0])
    Else
        ConsoleWrite("TXT Records:" & @CRLF)
        _ArrayDisplay($txtRecords, "Device: " & $mDNSDevices[$i][0] & " TXT Records")
    EndIf
Next

Func _ScanMDNSDevices()
    Local $mDNSDevices = []
    Local $mDNS_msg

    UDPStartup()
    Local $mDNS_sock = UDPOpen("224.0.0.251", 5353)
    If @error Then
        UDPShutdown()
        SetError(1)
        Return $mDNSDevices
    EndIf

    Local $query_time = TimerInit()

    UDPSend($mDNS_sock, "0x5A8901000001000000000000000000000000000000000000FFFF0000")

    Do
        $mDNS_msg = UDPRecv($mDNS_sock, 512, 1)
    Until $mDNS_msg <> "" Or TimerDiff($query_time) > 1000

    UDPShutdown()

    Local $domain_start = StringInStr($mDNS_msg, "00" & Chr(1))
    While $domain_start <> 0
        $domain_start += 4
        Local $domain_len = Hex(StringToNumber(StringMid($mDNS_msg, $domain_start, 2), 2))
        $domain_start += 2
        Local $domain = StringMid($mDNS_msg, $domain_start, $domain_len)
        $domain_start += $domain_len

        Local $ip_address = _ExtractIPAddress($mDNS_msg, $domain_start)
        If @error Then
            ContinueLoop
        EndIf

        $mDNSDevices &= _ArrayCreate($domain, $ip_address)

        $domain_start = StringInStr($mDNS_msg, "00" & Chr(1), $domain_start)
    WEnd

    Return $mDNSDevices
EndFunc

Func _ExtractIPAddress($mDNS_msg, ByRef $offset)
    Local $ip_address = ""
    For $i = 3 To 0 Step -1
        $ip_address &= Dec(StringMid(BinaryMid($mDNS_msg, $offset, 1), 3))
        If $i <> 0 Then
            $ip_address &= "."
        EndIf
        $offset += 1
    Next

    If $ip_address = "0.0.0.0" Then
        SetError(1)
        Return ""
    EndIf

    Return $ip_address
EndFunc

Func _GetMDNSTXTRecords($ip_address)
    Local $txtRecords = []

    Local $query = "_services._dns-sd._udp.local"
    Local $query_len = Hex(BinaryLen($query), 2) & StringTrimLeft(StringToBinary($query), 2)

    Local $query_packet = "0x5A8901000001000000000000" & $query_len & "0000" & $query & "0000100010"

    UDPStartup()
    Local $mDNS_sock = UDPOpen($ip_address, 5353)
    If @error Then
        UDPShutdown()
        SetError(1)
        Return $txtRecords
    EndIf

    UDPSend($mDNS_sock, $query_packet)

    Local $query_time = TimerInit()
    Local $mDNS_msg

    Do
        $mDNS_msg = UDPRecv($mDNS_sock, 512, 1)
    Until $mDNS_msg <> "" Or TimerDiff($query_time) > 1000

    UDPShutdown()

    Local $txt_record_start = StringInStr($mDNS_msg, $query_len, 1, 1)
    While $txt_record_start <> 0
        $txt_record_start += 2 + StringLen($query_len)
        Local $txt_record_len = Hex(StringToNumber(StringMid($mDNS_msg, $txt_record_start, 2), 2))
        $txt_record_start += 2
        Local $txt_record = StringMid($mDNS_msg, $txt_record_start, $txt_record_len)
        $txt_record_start += $txt_record_len

        $txtRecords &= $txt_record

        $txt_record_start = StringInStr($mDNS_msg, $query_len, $txt_record_start, 1)
    WEnd

    Return $txtRecords
EndFunc


_ScanMDNSDevices function is used to scan the network for mDNS devices. It sends a mDNS query to the multicast address 224.0.0.251 on port 5353. The received responses are then parsed to extract the domain names and IP addresses of the mDNS devices.

_GetMDNSTXTRecords function is used to retrieve the TXT records of a specific mDNS device by sending a DNS query to the device's IP address on port 5353. The received responses are parsed to extract the TXT records.

please let me know if it  helped 

❤️

Posted (edited)
On 6/15/2023 at 1:57 PM, 20Ice18 said:

To scan a network for mDNS devices and retrieve their TXT records, you can utilize the mDNS protocol and perform the necessary DNS queries

Assuming  you have the necessary privileges to send and receive UDP packets on the network and appropriate network setup to allow mDNS traffic you can try this: 

 

#include <Array.au3>
#include <String.au3>
#include <UDPOs.au3>

Local $mDNSDevices = _ScanMDNSDevices()
If @error Then
    MsgBox(16, "Error", "Failed to scan mDNS devices.")
    Exit
EndIf

For $i = 0 To UBound($mDNSDevices) - 1
    ConsoleWrite("Device: " & $mDNSDevices[$i][0] & ", IP: " & $mDNSDevices[$i][1] & @CRLF)

    Local $txtRecords = _GetMDNSTXTRecords($mDNSDevices[$i][1])
    If @error Then
        MsgBox(16, "Error", "Failed to retrieve TXT records for device: " & $mDNSDevices[$i][0])
    Else
        ConsoleWrite("TXT Records:" & @CRLF)
        _ArrayDisplay($txtRecords, "Device: " & $mDNSDevices[$i][0] & " TXT Records")
    EndIf
Next

Func _ScanMDNSDevices()
    Local $mDNSDevices = []
    Local $mDNS_msg

    UDPStartup()
    Local $mDNS_sock = UDPOpen("224.0.0.251", 5353)
    If @error Then
        UDPShutdown()
        SetError(1)
        Return $mDNSDevices
    EndIf

    Local $query_time = TimerInit()

    UDPSend($mDNS_sock, "0x5A8901000001000000000000000000000000000000000000FFFF0000")

    Do
        $mDNS_msg = UDPRecv($mDNS_sock, 512, 1)
    Until $mDNS_msg <> "" Or TimerDiff($query_time) > 1000

    UDPShutdown()

    Local $domain_start = StringInStr($mDNS_msg, "00" & Chr(1))
    While $domain_start <> 0
        $domain_start += 4
        Local $domain_len = Hex(StringToNumber(StringMid($mDNS_msg, $domain_start, 2), 2))
        $domain_start += 2
        Local $domain = StringMid($mDNS_msg, $domain_start, $domain_len)
        $domain_start += $domain_len

        Local $ip_address = _ExtractIPAddress($mDNS_msg, $domain_start)
        If @error Then
            ContinueLoop
        EndIf

        $mDNSDevices &= _ArrayCreate($domain, $ip_address)

        $domain_start = StringInStr($mDNS_msg, "00" & Chr(1), $domain_start)
    WEnd

    Return $mDNSDevices
EndFunc

Func _ExtractIPAddress($mDNS_msg, ByRef $offset)
    Local $ip_address = ""
    For $i = 3 To 0 Step -1
        $ip_address &= Dec(StringMid(BinaryMid($mDNS_msg, $offset, 1), 3))
        If $i <> 0 Then
            $ip_address &= "."
        EndIf
        $offset += 1
    Next

    If $ip_address = "0.0.0.0" Then
        SetError(1)
        Return ""
    EndIf

    Return $ip_address
EndFunc

Func _GetMDNSTXTRecords($ip_address)
    Local $txtRecords = []

    Local $query = "_services._dns-sd._udp.local"
    Local $query_len = Hex(BinaryLen($query), 2) & StringTrimLeft(StringToBinary($query), 2)

    Local $query_packet = "0x5A8901000001000000000000" & $query_len & "0000" & $query & "0000100010"

    UDPStartup()
    Local $mDNS_sock = UDPOpen($ip_address, 5353)
    If @error Then
        UDPShutdown()
        SetError(1)
        Return $txtRecords
    EndIf

    UDPSend($mDNS_sock, $query_packet)

    Local $query_time = TimerInit()
    Local $mDNS_msg

    Do
        $mDNS_msg = UDPRecv($mDNS_sock, 512, 1)
    Until $mDNS_msg <> "" Or TimerDiff($query_time) > 1000

    UDPShutdown()

    Local $txt_record_start = StringInStr($mDNS_msg, $query_len, 1, 1)
    While $txt_record_start <> 0
        $txt_record_start += 2 + StringLen($query_len)
        Local $txt_record_len = Hex(StringToNumber(StringMid($mDNS_msg, $txt_record_start, 2), 2))
        $txt_record_start += 2
        Local $txt_record = StringMid($mDNS_msg, $txt_record_start, $txt_record_len)
        $txt_record_start += $txt_record_len

        $txtRecords &= $txt_record

        $txt_record_start = StringInStr($mDNS_msg, $query_len, $txt_record_start, 1)
    WEnd

    Return $txtRecords
EndFunc


_ScanMDNSDevices function is used to scan the network for mDNS devices. It sends a mDNS query to the multicast address 224.0.0.251 on port 5353. The received responses are then parsed to extract the domain names and IP addresses of the mDNS devices.

_GetMDNSTXTRecords function is used to retrieve the TXT records of a specific mDNS device by sending a DNS query to the device's IP address on port 5353. The received responses are parsed to extract the TXT records.

please let me know if it  helped 

Thanks for the help but..... it needs a UDF which I can't find anywhere "UDPOs.au3".
Can you show me where I can download it?
I also get a error "StringToNumber" undefined function, is that the same as "Number"?
Error "_ArrayCreate" undefined function.

If I replace the "StringToNumber" to "Number" it won't work.
Did this script works on your PC?

Edited by nend
Posted

I apologize, I think I used to have the script on my old computer but I can't find it now and I can't find that UDF either, I will look for another solution today.
Will let you know if I find anything.

❤️

Posted

Is there anybody else how knows how to discover mDNS devices in local network and get there TXT records?
The delivered examples from 20ice18 doesn't work, I'm just stuck.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...