mirror of https://github.com/MISP/MISP
chg: [internal] Use ProcessTool in AttachmentTool
parent
cc25e2729c
commit
d7d8211dd9
|
@ -1050,7 +1050,7 @@ class ServersController extends AppController
|
|||
|
||||
$attachmentTool = new AttachmentTool();
|
||||
try {
|
||||
$advanced_attachments = $attachmentTool->checkAdvancedExtractionStatus($this->Server->getPythonVersion());
|
||||
$advanced_attachments = $attachmentTool->checkAdvancedExtractionStatus();
|
||||
} catch (Exception $e) {
|
||||
$this->log($e->getMessage(), LOG_NOTICE);
|
||||
$advanced_attachments = false;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
App::uses('AWSS3Client', 'Tools');
|
||||
App::uses('ProcessTool', 'Tools');
|
||||
App::uses('JsonTool', 'Tools');
|
||||
|
||||
class AttachmentTool
|
||||
{
|
||||
|
@ -316,13 +318,13 @@ class AttachmentTool
|
|||
'-j', // junk (don't record) directory names
|
||||
'-P', // use standard encryption
|
||||
self::ZIP_PASSWORD,
|
||||
escapeshellarg($zipFile),
|
||||
escapeshellarg($tempDir . DS . $md5),
|
||||
escapeshellarg($tempDir . DS . $md5 . '.filename.txt'),
|
||||
$zipFile,
|
||||
$tempDir . DS . $md5,
|
||||
$tempDir . DS . $md5 . '.filename.txt',
|
||||
];
|
||||
|
||||
try {
|
||||
$this->execute($exec);
|
||||
ProcessTool::execute($exec);
|
||||
return FileAccessTool::readFromFile($zipFile);
|
||||
|
||||
} catch (Exception $e) {
|
||||
|
@ -360,13 +362,13 @@ class AttachmentTool
|
|||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function advancedExtraction($pythonBin, $filePath)
|
||||
public function advancedExtraction($filePath)
|
||||
{
|
||||
return $this->executeAndParseJsonOutput([
|
||||
$pythonBin,
|
||||
ProcessTool::pythonBin(),
|
||||
self::ADVANCED_EXTRACTION_SCRIPT_PATH,
|
||||
'-p',
|
||||
escapeshellarg($filePath),
|
||||
$filePath,
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -375,9 +377,9 @@ class AttachmentTool
|
|||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function checkAdvancedExtractionStatus($pythonBin)
|
||||
public function checkAdvancedExtractionStatus()
|
||||
{
|
||||
return $this->executeAndParseJsonOutput([$pythonBin, self::ADVANCED_EXTRACTION_SCRIPT_PATH, '-c']);
|
||||
return $this->executeAndParseJsonOutput([ProcessTool::pythonBin(), self::ADVANCED_EXTRACTION_SCRIPT_PATH, '-c']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -496,50 +498,11 @@ class AttachmentTool
|
|||
*/
|
||||
private function executeAndParseJsonOutput(array $command)
|
||||
{
|
||||
$output = $this->execute($command);
|
||||
|
||||
$json = json_decode($output, true);
|
||||
if ($json === null) {
|
||||
throw new Exception("Command output is not valid JSON: " . json_last_error_msg());
|
||||
$output = ProcessTool::execute($command);
|
||||
try {
|
||||
return JsonTool::decode($output);
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Command output is not valid JSON.", 0, $e);
|
||||
}
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is much more complicated than just `exec`, but it also provide stderr output, so Exceptions
|
||||
* can be much more specific.
|
||||
*
|
||||
* @param array $command
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function execute(array $command)
|
||||
{
|
||||
$descriptorspec = [
|
||||
1 => ["pipe", "w"], // stdout
|
||||
2 => ["pipe", "w"], // stderr
|
||||
];
|
||||
|
||||
$command = implode(' ', $command);
|
||||
$process = proc_open($command, $descriptorspec, $pipes);
|
||||
if (!$process) {
|
||||
throw new Exception("Command '$command' could be started.");
|
||||
}
|
||||
|
||||
$stdout = stream_get_contents($pipes[1]);
|
||||
if ($stdout === false) {
|
||||
throw new Exception("Could not get STDOUT of command.");
|
||||
}
|
||||
fclose($pipes[1]);
|
||||
|
||||
$stderr = stream_get_contents($pipes[2]);
|
||||
fclose($pipes[2]);
|
||||
|
||||
$returnCode = proc_close($process);
|
||||
if ($returnCode !== 0) {
|
||||
throw new Exception("Command '$command' return error code $returnCode. STDERR: '$stderr', STDOUT: '$stdout'");
|
||||
}
|
||||
|
||||
return $stdout;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2516,7 +2516,7 @@ class Attribute extends AppModel
|
|||
public function isAdvancedExtractionAvailable()
|
||||
{
|
||||
try {
|
||||
$types = $this->loadAttachmentTool()->checkAdvancedExtractionStatus($this->getPythonVersion());
|
||||
$types = $this->loadAttachmentTool()->checkAdvancedExtractionStatus();
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
@ -2830,7 +2830,7 @@ class Attribute extends AppModel
|
|||
public function advancedAddMalwareSample($event_id, $attribute_settings, $filename, $tmpfile)
|
||||
{
|
||||
try {
|
||||
$result = $this->loadAttachmentTool()->advancedExtraction($this->getPythonVersion(), $tmpfile->path);
|
||||
$result = $this->loadAttachmentTool()->advancedExtraction($tmpfile->path);
|
||||
} catch (Exception $e) {
|
||||
$this->logException("Could not finish advanced extraction", $e);
|
||||
return $this->simpleAddMalwareSample($event_id, $attribute_settings, $filename, $tmpfile);
|
||||
|
|
Loading…
Reference in New Issue