chg: [internal] Simplify $hashTypes

pull/8317/head
Jakub Onderka 2022-04-29 16:32:48 +02:00
parent 9e4fa77ea7
commit be5ee0f736
2 changed files with 25 additions and 38 deletions

View File

@ -2196,10 +2196,10 @@ class AttributesController extends AppController
$validTypes = $this->Attribute->resolveHashType($hash);
if ($allSamples) {
if (empty($validTypes)) {
$error = 'Invalid hash format (valid options are ' . implode(', ', array_keys($this->Attribute->hashTypes)) . ')';
$error = 'Invalid hash format (valid options are ' . implode(', ', array_keys(Attribute::FILE_HASH_TYPES)) . ')';
} else {
foreach ($validTypes as $t) {
if ($t == 'md5') {
if ($t === 'md5') {
$types = array_merge($types, array('malware-sample', 'filename|md5', 'md5'));
} else {
$types = array_merge($types, array('filename|' . $t, $t));
@ -2207,7 +2207,7 @@ class AttributesController extends AppController
}
}
if (empty($error)) {
$event_ids = $this->Attribute->find('list', array(
$event_ids = $this->Attribute->find('column', array(
'recursive' => -1,
'contain' => array('Event'),
'fields' => array('Event.id'),
@ -2222,7 +2222,7 @@ class AttributesController extends AppController
),
));
$searchConditions = array(
'AND' => array('Event.id' => array_values($event_ids))
'AND' => array('Event.id' => $event_ids)
);
if (empty($event_ids)) {
$error = 'No hits with the given parameters.';
@ -2244,18 +2244,18 @@ class AttributesController extends AppController
if (empty($error)) {
$attributes = $this->Attribute->fetchAttributes(
$this->Auth->user(),
array(
'fields' => array('Attribute.event_id', 'Attribute.id', 'Attribute.value1', 'Attribute.value2', 'Event.info'),
'conditions' => array(
'AND' => array(
$searchConditions,
array('Attribute.type' => 'malware-sample')
)
),
'contain' => array('Event'),
'flatten' => 1
)
$this->Auth->user(),
array(
'fields' => array('Attribute.event_id', 'Attribute.id', 'Attribute.value1', 'Attribute.value2', 'Event.info'),
'conditions' => array(
'AND' => array(
$searchConditions,
array('Attribute.type' => 'malware-sample')
)
),
'contain' => array('Event'),
'flatten' => 1
)
);
if (empty($attributes)) {
$error = 'No hits with the given parameters.';

View File

@ -318,22 +318,11 @@ class Attribute extends AppModel
]
];
public $hashTypes = array(
'md5' => array(
'length' => 32,
'pattern' => '#^[0-9a-f]{32}$#',
'lowerCase' => true,
),
'sha1' => array(
'length' => 40,
'pattern' => '#^[0-9a-f]{40}$#',
'lowerCase' => true,
),
'sha256' => array(
'length' => 64,
'pattern' => '#^[0-9a-f]{64}$#',
'lowerCase' => true,
)
const FILE_HASH_TYPES = array(
'md5' => 32,
'sha1' => 40,
'sha256' => 64,
'sha512' => 128,
);
public function afterFind($results, $primary = false)
@ -2538,13 +2527,11 @@ class Attribute extends AppModel
public function resolveHashType($hash)
{
$hashTypes = $this->hashTypes;
$validTypes = array();
$validTypes = [];
$length = strlen($hash);
foreach ($hashTypes as $k => $hashType) {
$temp = $hashType['lowerCase'] ? strtolower($hash) : $hash;
if ($hashType['length'] == $length && preg_match($hashType['pattern'], $temp)) {
$validTypes[] = $k;
foreach (self::FILE_HASH_TYPES as $type => $hashLength) {
if ($length === $hashLength && ctype_xdigit($hash)) {
$validTypes[] = $type;
}
}
return $validTypes;