There are multiple questions here; the claim of a "malformed packet" may be due to checksum offloading and if so is a false error as it reflects the packet capture not having a complete view--some of the work was instead done on the network hardware. WireShark should have documentation on this.
Otherwise, various tools (such as socat
, nc
or netcat
, or via extremely similar features in shells such as ksh93
or bash
) can take bytes from standard input and dump them into what becomes a UDP packet. Whether this is "proper" depends on the protocol; one could in theory construct and send a DNS or DHCP packet this way, though more often folks use a library or dedicated software that (hopefully) correctly implements the protocol in question, as there is usually a lot more involved than setting a few bits in the body of a packet and sending that out over the wire, notably handling responses, retrying after timeout or error, packet header fields, etc. The protocols are usually very well documented in RFC, or see the "TCP/IP Illustrated" book series by Stevens for even more documentation.
Specific tools such as nmap
do very custom things with packet construction. Otherwise for manual packet construction a programming language is typically used, though again most software will use existing libraries or system services to send out DNS or DHCP or other UDP packets, as those are far less work and far less error prone than manually crafting a raw packet from scratch.
echo $[61002+RANDOM%4532]
to pick a random port number in that range.