Tuesday, July 12, 2022

Broadcasting UDP data with Perl!

    The other day a scenario presented itself in which a device waited for broadcast traffic to a specific port in order to perform different actions. Horrible security posture aside, this presented an interesting opportunity to practice some sockets in Perl!

#!/usr/bin/env perl
use strict;
use warnings;
use IO::Socket;

my $sock = IO::Socket::INET->new(
	Proto => 'udp',
	PeerPort => '5555',
    # Could use network specific broadcast below instead
	PeerAddr => '255.255.255.255',
	LocalAddr => '192.168.1.19',
	Broadcast => 1,
	autoflush => 1,
) or die "Error opening socket: $!\n";

$sock->send('Perl broadcasting to the UDP world!');

    After a quick save and some root privileges, tcpdump presented the following datagram heading out to the ether!


root@aut0exec:~# tcpdump -i any -nn -X 'udp and port 5555'

21:04 eth0  Out IP 192.168.1.19.60735 > 255.255.255.255.5555: UDP, length 35
	0x0000:  4500 003f 44a1 4000 4011 3452 c0a8 0113  E..?D.@.@.4R....
	0x0010:  ffff ffff ed3f 15b3 002b c1f7 5065 726c  .....?...+..Perl
	0x0020:  2062 726f 6164 6361 7374 696e 6720 746f  .broadcasting.to
	0x0030:  2074 6865 2055 4450 2077 6f72 6c64 21    .the.UDP.world!

    From here, I had some other things to do with code for other purposes but none the less, simply getting the broadcast to 255.255.255.255 via UDP was an ordeal. Hopefully, this post can save someone else some troubles later!

No comments:

Post a Comment

Have a thought or question? Please share!