Creates and sends a LLMNR request to the UDP multicast address.
(ip net.IP)
| 155 | |
| 156 | // Creates and sends a LLMNR request to the UDP multicast address. |
| 157 | func sendLLMNRProbe(ip net.IP) string { |
| 158 | var cName string |
| 159 | responderIP := "" |
| 160 | // 2 byte random transaction id eg. 0x8e53 |
| 161 | randomTransactionID := fmt.Sprintf("%04x", rand.Intn(65535)) |
| 162 | |
| 163 | switch hostnameType { |
| 164 | case def, newHostname: |
| 165 | cName = string(*hostnamePtr) |
| 166 | case randHostname: |
| 167 | cName = randomHostname() |
| 168 | } |
| 169 | |
| 170 | cNameLen := fmt.Sprintf("%02x", len(cName)) |
| 171 | encCName := hex.EncodeToString([]byte(cName)) |
| 172 | // LLMNR request in raw bytes |
| 173 | llmnrRequest := randomTransactionID + |
| 174 | "00000001000000000000" + cNameLen + encCName + "0000010001" |
| 175 | n, _ := hex.DecodeString(llmnrRequest) |
| 176 | |
| 177 | remoteAddr := net.UDPAddr{IP: net.ParseIP(BcastAddr), Port: LLMNRPort} |
| 178 | |
| 179 | conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: ip}) |
| 180 | if err != nil { |
| 181 | fmt.Printf("Could not bind to the interface. Is it disabled? ") |
| 182 | logger.Printf("Bind error: %+v\nSource IP: %v\n", err, ip) |
| 183 | return responderIP // return with IP = '' |
| 184 | } |
| 185 | |
| 186 | defer conn.Close() |
| 187 | _, _ = conn.WriteToUDP(n, &remoteAddr) |
| 188 | |
| 189 | conn.SetReadDeadline(time.Now().Add(TimeoutSec * time.Second)) |
| 190 | buffer := make([]byte, 1024) |
| 191 | bytes, clientIP, err := conn.ReadFromUDP(buffer) |
| 192 | if err == nil { // no timeout (or any other) error |
| 193 | responderIP = strings.Split(clientIP.String(), ":")[0] |
| 194 | logger.Printf("LLMNR request payload was: %x\n", n) |
| 195 | logger.Printf("Data received on %s from responder IP %s: %x\n", |
| 196 | ip, clientIP, buffer[:bytes]) |
| 197 | } else { |
| 198 | logger.Printf("Error getting response: %s\n", err) |
| 199 | } |
| 200 | return responderIP |
| 201 | } |
| 202 | |
| 203 | // Calculate random hostname by taking random length |
| 204 | // of the SHA1 of current time. |
no test coverage detected