Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listen for returning UDP packets in bash

Tags:

bash

sockets

I played around with bash(4.0.33) network support for learning purposes and tried to create a port scanner in bash. For TCP I opened a TCP/IP socket with

exec 3<>/dev/tcp/192.0.2.1/80

and appropriate action was taken if the connection was refused or connect system call timed out. However, with UDP I'm easily able to send a packet with

echo > /dev/udp/192.0.2.1/53

but how to read returning packets from correct socket? I mean UDP datagram sent to 192.0.2.1 has source port from ephemeral port range and thus I do not know which socket in /dev/udp/192.0.2.1/ directory should I read. Or isn't this doable without external utilities like tcpdump?

like image 753
Martin Avatar asked Jun 16 '14 16:06

Martin


1 Answers

Bash's UDP support isn't great, and is compiled out on many distros (Debian/Ubuntu and derivatives especially). The recommended tool is netcat:

nc -u 192.0.2.1 53

Note: Use coproc or named pipes to read and write from the same netcat process. Don't send a packet first and try to catch the reply netcat.

Bash is really the wrong language for this though. Consider using Python, which handles both UDP and binary data way better, with just a couple of more lines of code.

like image 54
that other guy Avatar answered Oct 25 '22 08:10

that other guy