Skip to content

Instantly share code, notes, and snippets.

@adkinss
Created August 12, 2016 15:31
Show Gist options
  • Save adkinss/777266e8a0fe6ec06de4a734a33c340d to your computer and use it in GitHub Desktop.
Save adkinss/777266e8a0fe6ec06de4a734a33c340d to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
## This script listens for UDP messages on a port and hexdumps them to the screen.
## The listener will not send any response back to the client. It receives only.
## Clients can send messages to this listener using netcat. The following works:
## echo hello | nc -4u -w1 <listener_hostname> <listener_port>
## Substitute the hostname and port for where this listener is running from.
## Keep in mind that ports less than 1024 will require the listener to run as root.
##
## The listener does not yet take any command line options.
## Update $PORT for the port the udp server will listen on.
use strict;
use IO::Socket;
my $PORT = 1162;
sub hdump {
my $offset = 0;
my(@array,$format);
foreach my $data (unpack("a16"x(length($_[0])/16)."a*",$_[0])) {
my($len)=length($data);
if ($len == 16) {
@array = unpack('N4', $data);
$format="0x%08x (%05d) %08x %08x %08x %08x %s\n";
} else {
@array = unpack('C*', $data);
$_ = sprintf "%2.2x", $_ for @array;
push(@array, ' ') while $len++ < 16;
$format="0x%08x (%05d)" .
" %s%s%s%s %s%s%s%s %s%s%s%s %s%s%s%s %s\n";
}
$data =~ tr/\0-\37\177-\377/./;
printf $format,$offset,$offset,@array,$data;
$offset += 16;
}
}
my $sock = IO::Socket::INET->new(LocalPort => $PORT, Proto => 'udp') or die "socket: $@";
my $message;
while ($sock->recv($message, 1024)) {
my($port, $ipaddr) = sockaddr_in($sock->peername);
my $remotehost = gethostbyaddr($ipaddr, AF_INET);
my $datestring = localtime();
print "[$datestring] $remotehost: \n";
hdump($message);
}
die "recv: $!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment