Why does VirusTotal return this error?

I am trying to use the public VirusTotal API to scan files. This is my code:

$post_url = 'https://www.virustotal.com/vtapi/v2/file/scan';    

$filemime = exec("file -b --mime-type '$file_to_scan'");

$post = array('apikey' => $virustotal_api_key, 'file' => '@' . realpath($file_to_scan) . ';type=' . $filemime . ';filename=' . $name);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$post_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Expect:'));
$api_reply = curl_exec($ch);
curl_close($ch);

$api_reply_array = json_decode($api_reply, true);

Whenever I run this code, I get the following error:

[response_code] => 0
[verbose_msg] => Invalid submission format, the uploaded file must travel as a multipart MIME message, please review the documentation

I spent several hours trying to figure it out, but it just won't work. Can someone point me in the right direction?

This is print_rof $postin the code above:

[apikey] => xxx
[file] => @/absolute/path/to/file.exe;type=application/octet-stream;filename=file.exe

Thank!

+4
source share
2 answers

I thought, after the help of VirusTotal itself (kudos, Karl). The problem was changing PHP 5.6 cURL. If you are on PHP 5.6 you need to add:

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

+3
source

Fixed line

$post = array('apikey' => $virustotal_api_key, 'file' => '@' . realpath($file_to_scan) . ';type=' . $filemime . ';filename=' . $name);

This should be changed to:

$cfile = new CURLFile(realpath($file_to_scan),$filemime,$name);
$post = array('apikey' => $virustotal_api_key, 'file' => $cfile);

Then it also works with PHP 7 :)

0

Source: https://habr.com/ru/post/1629995/


All Articles