Article 5E8Y6 PERL: Not a HASH reference at /usr/local/bin/vt.pl line 169.

PERL: Not a HASH reference at /usr/local/bin/vt.pl line 169.

by
//////
from LinuxQuestions.org on (#5E8Y6)
hello all.

one prog that i am making has a bug in it.

it dies with error message :
Code:Not a HASH reference at /usr/local/bin/vt.pl line 169.https://i.ibb.co/smFvx9x/vt.png

this program sends to virustotal.com suricata's filestore files that could be malicious. it uses vt api v2.

it dies when it tries to get reply from virustotal.com
"Your resource is queued for analysis." <-- after that message.
the whole code of it is here :
Code:#!/usr/bin/perl
# vt.pl
use warnings;
use strict;
use File::Find;
use File::Remove 'remove';
use LWP::UserAgent;
use JSON;
use Term::ANSIColor qw(:constants); # for colorful output
use Getopt::Std; # for options
use Data::Dumper;

use vars qw( @fullpaths $duration $end_time $time $deletefile $filepath $ua $url $key $key1 $key2 $key3 $response $result $results $json $decjson $sha $num );

use vars qw( $opt_h );
getopts('h');

if ($opt_h) {
print q(
-h Usage. just start the program with perl /usr/local/bin/vt.pl

/);
exit(0);
};

my $path = "/var/log/suricata/filestore/";

find( \&pathfinder , "$path" );

# main function.
sub pathfinder {

$path = $_[0];

if ( $File::Find::dir =~ '/[0-f]{2}$' || length($_) =~ 64 ) {

push(@fullpaths, ($File::Find::name));

}

foreach $filepath (@fullpaths) {
# delete foo.json files
if ($filepath =~ /((.*)\.json$)/) {

pop(@fullpaths);
deleter($filepath);

} else {

if ($filepath =~ //) {

print ' Pathfinder() : No files to upload.\n';
exit(0);
}

print BLUE, ' Pathfinder() : Countdown to feed Uploadtovt() with file_sha256 path -> Uploadtovt().', RESET;
countdown(15); print "\n";

uploadtovt($filepath);
pop(@fullpaths);

}
}
}

sub countdown($) {

($duration) = @_;
$end_time = time + $duration;
$time = time;
while ($time < $end_time) {
$time = time;
printf("\r%02d:%02d:%02d", ($end_time - $time) / (60*60), ($end_time - $time) / (60) % 60,($end_time - $time) % 60);
$|++;
sleep 1;
}
}

# deletes sha256 file from filestore.
sub deleter {

$deletefile = $_[0];

if ($deletefile =~ /((.*)\.json$)/) {

remove("$1");

#print RED, " DEL json ", RESET, YELLOW, "$deletefile", RESET, "\n";
#print "---------------------------------------------------------------------------------------------------------------\n";

}

elsif ($deletefile =~ /((.*)[a-z0-9]{64}$)/) {

remove("$1");

print RED, " DEL sha256 : ", RESET, YELLOW, "$deletefile", RESET, "\n";
print "---------------------------------------------------------------------------------------------------------------\n";

}
}
# uploads sha256 file to virustotal.
sub uploadtovt {

$filepath = $_[0];

$ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
$url='https://www.virustotal.com/vtapi/v2/file/scan';
$key='<your api key here>';

$response = $ua->post( $url,
Content_Type => 'multipart/form-data',
Content => ['apikey' => $key,
'file' => ["$filepath"]]
);
die "$url error: ", $response->status_line
unless $response->is_success;
$results = $response->content;

print BLUE, ' Uploadtovt() : Countdown to send file_sha256 to Virustotal -> Downloader().', RESET;
countdown(5); print "\n";

downloader($key, $sha);

}

# downloads report of sha256 file.
sub downloader {

$key = $_[0];
$sha = $_[1];
$num = $_[2];

$ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
$key='ee6e69074a21711e1125bbe678b595e40b2cc82ff3f82cd923a9100f7d50e77b';
$url='https://www.virustotal.com/vtapi/v2/file/report';

if (defined $num) {

print BLUE, ' Downloader() : Second try - Countdown to pull results of file_sha256 from Virustotal -> Printer().', RESET;
countdown(30); print "\n";

} else {

print BLUE, ' Downloader() : Countdown to pull results of file_sha256 from Virustotal -> Printer().', RESET;
countdown(45); print "\n";

}

#pulls the sha256 value out of the JSON response
#Note: there are many other values that could also be pulled out
$json = JSON->new->allow_nonref;
$decjson = $json->decode( $results);
$sha = $decjson->{"sha256"};
#print " $sha", "\n\n";

$response = $ua->post( $url,
['apikey' => $key,
'resource' => $sha]
);
die "$url error: ", $response->status_line
unless $response->is_success;
$results=$response->content;

#processes the JSON
$json = JSON->new->allow_nonref;
$decjson = $json->decode($results);

printer($decjson->{'verbose_msg'},$decjson->{'scan_date'},$decjson->{'positives'},$decjson->{'total'},$filepath,$results,$sha,$key);

}
# prints information about file_sha256.
sub printer {

$decjson->{'verbose_msg'} = $_[0];
$decjson->{'scan_date'} = $_[1];
$decjson->{'positives'} = $_[2];
$decjson->{'total'} = $_[3];
$filepath = $_[4];
$results = $_[5];
$sha = $_[6];
$key = $_[7];
$num = '1';

if ($results =~ /"Your resource is queued for analysis"/i) {
# if queued go to Downloader.
print YELLOW," ", $decjson->{'verbose_msg'},".", RESET; print "\n";
print BLUE,' Printer() : Waiting for results from Virustotal -> Downloader().', RESET; print "\n";
downloader($key, $sha, $num);

} else {

if ($results =~ /"Scan finished, information embedded"/i) {
print "---------------------------------------------------------------------------------------------------------------\n";
# print selected values from the json file
print YELLOW, " Sample name: ". $filepath. RESET"\n";
print YELLOW, " Scan Date: ".$decjson->{"scan_date"}. RESET"\n";
print RED, " Detection rate: ", RESET, WHITE, $decjson->{"positives"}, RESET, "/", RED, $decjson->{"total"}, RESET, "\n";
deleter($filepath);
}
}
}i know i am asking kinda a lot, but if someone could give some instructions how to solve this i would be grateful.

thanks.latest?d=yIl2AUoC8zA latest?i=SkbkMIrVBPY:ma_d2FCgDs8:F7zBnMy latest?i=SkbkMIrVBPY:ma_d2FCgDs8:V_sGLiP latest?d=qj6IDK7rITs latest?i=SkbkMIrVBPY:ma_d2FCgDs8:gIN9vFwSkbkMIrVBPY
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments