my $socket = connect_to_server;
$socket->send("register\n");
$socket->send("$hostname\n");
+
+ # Wait for connection to be established, try up to 5 times
my $response;
foreach (1..5) {
sleep $_;
sub login {
my $socket = connect_to_server;
- my $response = "";
+
+ # Wait for connection to be established, try up to 5 times
+ my $response;
$socket->send("login\n");
$socket->send("$clientName\n");
$socket->send("$clientKey\n");
sub send_info {
my $socket = login($clientName, $clientKey);
+
my $info = join "", ns_system('./busybox', 'sh', '-c', 'hostname; date; uname -a; lspci; lsusb; ifconfig');
+
$socket->send("info\n");
$socket->send($info);
$socket->send("⟃---EOF---⟄\n");
return;
}
+ # Check that log exists and is readable by current user
exit if (! -e $file || ! -r _);
+
my $socket = login($clientName, $clientKey);
+
+ # Replace / character with similar-looking character that is valid
+ # for filenames. Used to show full path to file
my $fileName = $file =~ s/\////gr;
+
+ # Upload tailed log continuously
$socket->send("log\n");
$socket->send("$fileName\n");
print_log "Log: Uploading $file";
}
my $socket = login($clientName, $clientKey);
+
+ # Upload process log continuously
$socket->send("processes\n");
print_log "Processes: Started";
my $commandLog = ns_systemFH('./pspy64', '-f');
}
my $socket = login($clientName, $clientKey);
+
+ # Upload command output continously with provided filename
my ($fileName) = $name;
$socket->send("command\n");
$socket->send("$fileName\n");
return;
}
+ # Check that log exists and is readable by current user
exit if (! -e $file || ! -r _);
+
+ # Replace / character with similar-looking character that is valid
+ # for filenames. Used to show full path to file
my $fileName = $file =~ s/\////gr;
my ($fileHash) = ns_system('./busybox', 'md5sum', "$file");
chomp $fileName; chomp $fileHash;
($fileHash) = $fileHash =~ m/([0-9a-f]+)/;
my $socket = login($clientName, $clientKey);
+
+ # Send filename and hash to server, wait for a response with the port to
+ # upload the file to
$socket->send("file\n");
$socket->send("$fileName\n");
$socket->send("$fileHash\n");
$socket->recv(my $port, 128);
($port) = $port =~ m/(\d+)/;
+ # Send file once
print_log "File: upload port is $port ($file)";
open(my $fileFH, '<', "$file") || die "Failed to open $file";
my $fileSocket = connect_to_server $port;
close($fileFH);
close($fileSocket);
+ # Server checks that the uploaded hash matches and informs on error
+ # No retry is attempted on failed upload
$socket->recv(my $response, 128);
if ($response =~ m/Transfer success/) {
print_log "File: upload success ($file)";
- exit;
} else {
print_log "File: upload failure ($file)";
}
push @child_processes, $pid;
return;
}
+
+ # Recursively monitor directory for files that are written to. Uploads files when found
my $monitor = ns_systemFH('./inotifywait', '-r', '-m', '-e', 'close_write', '--format', '%w%f', $dir);
while (<$monitor>) {
chomp;
send_info();
send_processes();
-# Files, logs, and commands to send to the server
# ------------------------------------------------------------------------------
+# Files, logs, and commands to send to the server
# These files will have their contents sent as they are updated
send_log('/var/log/secure');