In this paper, we simulate the udp packets sent by Hikvision cameras and capture the packets returned by the cameras to parse and extract the relevant information.
Through the packet capture found that the Hikvision camera sends and receives data using the udp protocol, and later compared to find that the use of python simulation is relatively simple. As the camera has a built-in udp protocol server-side program, this article mainly uses python to simulate the client to send udp packets.
Client Code
import socket import re ANY = "0.0.0.0" DES_IP = "239.255.255.250" PORT = 37020 # xml_str = b'<?xml version="1.0" encoding="utf-8"?><Probe><Uuid>B2D5D4D2-808C-40F6-87CD-694C05C2B274</Uuid><Types>inquiry</Types></Probe> ' xml_str = b'<?xml version="1.0" encoding="utf-8"?><Probe><Uuid>CB09F608-E016-4EE8-869A-CA186852F12E</Uuid><Types>inquiry</Types></Probe> ' # Create a UDP socket s = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # Allow port multiplexing (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Bind the port on which to listen for multicast packets ((ANY, PORT)) # Declare the socket as multicast (socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) # Join a multicast group, the group address is defined by the third parameter ( socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(DES_IP) + socket.inet_aton(ANY) ) (False) (xml_str, (DES_IP, PORT)) while True: try: data, address = (2048) except Exception as e: # print(e) pass else: print(address) # print(data) try: IPv4 = ((r"<IPv4Address>(.*?)</IPv4Address>", ), str(data))[1] MAC = ((r"<MAC>(.*?)</MAC>", ), str(data))[1] except TypeError: pass else: # print(data) print("IPv4: {}".format(IPv4)) print("MAC: {}".format(MAC))
The code mainly uses a socket to send data and the re module to process and extract the data.
Through the use of wireshark packet capture found that the first need for the client to send an xml type of data, the server side to receive the data, will return a variety of information about the camera, including the camera IPv4 and MAC address that will be obtained in this paper.
The result of running the program is shown in Fig:
Write it on the back.
This program can be used not only as a device discovery, but also when replacing a device to find out if it has been replaced in the first place.
Additional knowledge:python3 udp can send but not receive messages solution
Right now there are two systems, win10 and xp in a virtual machine, with python3 in win10 and network debugging assistant in xp.
Messages sent by python3 via udp can be received in the network debugging assistant, but messages sent on the network debugging assistant are not received on python3. First of all python3 receives the written code as follows:
import socket def receive_message(): udp_socket = (socket.AF_INET, socket.SOCK_DGRAM) # Create a socket udp_socket.bind(('', 63630)) #Bind local information, their own computer's ip and program port. ip generally do not need to write, said the machine's any ip, port greater than the commonly used 1023 on it! while True: udp_data = udp_socket.recvfrom(1024) #Receive data, 1024 indicates the maximum number of bytes for this reception if udp_data[0].decode(encoding='gbk') != 'end': print('{}:{}'.format(str(udp_data[1]), udp_data[0].decode(encoding='gbk'))) else: break udp_socket.close() # Close the socket if __name__ == '__main__': receive_message()
Thought it was a problem with the program or the network debugging assistant, but after checking multiple times and trying again I still can't receive it. Put it down for now.
Today, I turned off the win10 firewall and found that it is possible to receive it normally, but I want to still be able to receive it without turning off the firewall, and Baidu did not find the desired answer.
Just when I was about to give up I saw the following setup:
So the checkbox indicated by the red arrow was checked.
Run the program again, win10 pop-up dialog box as follows:
After clicking allow access, sending a message on the xp network debugging assistant will be received properly on python3!!!!
Above this python3 through udp to realize the multicast data sending and receiving operations is all I have shared with you, I hope to be able to give you a reference, and I hope that you support me more.