2012-03-15 15:06:45 +01:00
< ? php
2012-09-24 16:02:01 +02:00
2012-03-15 15:06:45 +01:00
App :: uses ( 'AppModel' , 'Model' );
2012-05-22 13:58:37 +02:00
App :: uses ( 'Folder' , 'Utility' );
2012-03-23 20:04:22 +01:00
App :: uses ( 'File' , 'Utility' );
2015-07-06 18:19:51 +02:00
App :: uses ( 'FinancialTool' , 'Tools' );
2016-08-25 20:19:16 +02:00
App :: uses ( 'RandomTool' , 'Tools' );
2020-05-05 15:23:26 +02:00
App :: uses ( 'AttachmentTool' , 'Tools' );
2020-06-19 16:21:53 +02:00
App :: uses ( 'TmpFileTool' , 'Tools' );
2020-10-10 16:42:23 +02:00
App :: uses ( 'ComplexTypeTool' , 'Tools' );
2012-03-23 20:04:22 +01:00
2020-08-06 11:52:53 +02:00
/**
* @ property Event $Event
2020-10-11 11:50:11 +02:00
* @ property AttributeTag $AttributeTag
2021-02-02 23:11:27 +01:00
* @ property Sighting $Sighting
2021-07-24 22:57:15 +02:00
* @ property MispObject $Object
2021-08-12 14:05:13 +02:00
* @ property SharingGroup $SharingGroup
2021-09-02 21:38:50 +02:00
* @ property Correlation $Correlation
2021-01-09 21:12:57 +01:00
* @ property - read array $typeDefinitions
* @ property - read array $categoryDefinitions
2020-08-06 11:52:53 +02:00
*/
2018-07-19 11:48:22 +02:00
class Attribute extends AppModel
{
public $combinedKeys = array ( 'event_id' , 'category' , 'type' );
2019-02-10 13:08:12 +01:00
public $name = 'Attribute' ; // TODO general
2018-07-19 11:48:22 +02:00
public $actsAs = array (
2021-01-22 13:01:23 +01:00
'AuditLog' ,
2019-02-10 13:08:12 +01:00
'SysLogLogable.SysLogLogable' => array ( // TODO Audit, logable
2018-07-19 11:48:22 +02:00
'userModel' => 'User' ,
'userKey' => 'user_id' ,
'change' => 'full' ),
'Trim' ,
'Containable' ,
'Regexp' => array ( 'fields' => array ( 'value' )),
);
public $displayField = 'value' ;
public $virtualFields = array (
'value' => " CASE WHEN Attribute.value2 = '' THEN Attribute.value1 ELSE CONCAT(Attribute.value1, '|', Attribute.value2) END " ,
); // TODO hardcoded
// explanations of certain fields to be used in various views
public $fieldDescriptions = array (
'signature' => array ( 'desc' => 'Is this attribute eligible to automatically create an IDS signature (network IDS or host IDS) out of it ?' ),
2020-06-18 16:34:01 +02:00
'distribution' => array ( 'desc' => 'Describes who will have access to the attribute.' )
2018-07-19 11:48:22 +02:00
);
2021-07-27 18:58:04 +02:00
const EDITABLE_FIELDS = [
'timestamp' ,
'category' ,
'value' ,
'value1' ,
'value2' ,
'to_ids' ,
'comment' ,
'distribution' ,
'sharing_group_id' ,
'deleted' ,
'disable_correlation' ,
'first_seen' ,
'last_seen' ,
];
2020-07-23 11:15:01 +02:00
2018-07-19 11:48:22 +02:00
public $distributionDescriptions = array (
0 => array ( 'desc' => 'This field determines the current distribution of the event' , 'formdesc' => " This setting will only allow members of your organisation on this server to see it. " ),
1 => array ( 'desc' => 'This field determines the current distribution of the event' , 'formdesc' => " Organisations that are part of this MISP community will be able to see the event. " ),
2 => array ( 'desc' => 'This field determines the current distribution of the event' , 'formdesc' => " Organisations that are either part of this MISP community or part of a directly connected MISP community will be able to see the event. " ),
3 => array ( 'desc' => 'This field determines the current distribution of the event' , 'formdesc' => " This will share the event with all MISP communities, allowing the event to be freely propagated from one server to the next. " ),
4 => array ( 'desc' => 'This field determines the current distribution of the event' , 'formdesc' => " This distribution of this event will be handled by the selected sharing group. " ),
5 => array ( 'desc' => 'This field determines the current distribution of the event' , 'formdesc' => " Inherit the event's distribution settings " ),
);
2019-04-02 15:59:49 +02:00
public $distributionLevels = array ();
2018-07-19 11:48:22 +02:00
public $shortDist = array ( 0 => 'Organisation' , 1 => 'Community' , 2 => 'Connected' , 3 => 'All' , 4 => ' Sharing Group' , 5 => 'Inherit' );
2021-09-02 21:12:31 +02:00
/** @var array */
private $old ;
2021-01-09 21:12:57 +01:00
public function __construct ( $id = false , $table = null , $ds = null )
{
2019-04-02 15:59:49 +02:00
parent :: __construct ( $id , $table , $ds );
$this -> distributionLevels = array (
0 => __ ( 'Your organisation only' ),
1 => __ ( 'This community only' ),
2 => __ ( 'Connected communities' ),
3 => __ ( 'All communities' ),
4 => __ ( 'Sharing group' ),
5 => __ ( 'Inherit event' )
);
}
2018-07-19 11:48:22 +02:00
// these are definitions of possible types + their descriptions and maybe later other behaviors
// e.g. if the attribute should be correlated with others or not
// if these then a category may have upload to be zipped
2021-07-27 16:16:52 +02:00
const ZIPPED_DEFINITION = [ 'malware-sample' ];
2018-07-19 11:48:22 +02:00
// if these then a category may have upload
2021-07-27 16:16:52 +02:00
const UPLOAD_DEFINITIONS = [ 'attachment' ];
2018-07-19 11:48:22 +02:00
// skip Correlation for the following types
2021-07-27 15:19:41 +02:00
const NON_CORRELATING_TYPES = array (
'comment' ,
'http-method' ,
'aba-rtn' ,
'gender' ,
'counter' ,
'port' ,
'nationality' ,
'cortex' ,
'boolean' ,
'anonymised'
2018-07-19 11:48:22 +02:00
);
2021-07-27 16:16:52 +02:00
const PRIMARY_ONLY_CORRELATING_TYPES = array (
2018-07-19 11:48:22 +02:00
'ip-src|port' ,
2020-10-13 23:08:59 +02:00
'ip-dst|port' ,
'hostname|port' ,
2018-07-19 11:48:22 +02:00
);
2018-08-03 15:03:35 +02:00
public $captureFields = array (
'event_id' ,
'category' ,
'type' ,
'value' ,
'value1' ,
'value2' ,
'to_ids' ,
'uuid' ,
'timestamp' ,
'distribution' ,
'comment' ,
'sharing_group_id' ,
'deleted' ,
'disable_correlation' ,
'object_id' ,
2019-06-13 09:16:34 +02:00
'object_relation' ,
'first_seen' ,
'last_seen'
2018-08-03 15:03:35 +02:00
);
2018-07-19 11:48:22 +02:00
public $searchResponseTypes = array (
'xml' => array (
'type' => 'xml' ,
'layout' => 'xml/default' ,
'header' => 'Content-Disposition: download; filename="misp.search.attribute.results.xml"'
),
'json' => array (
'type' => 'json' ,
'layout' => 'json/default' ,
'header' => 'Content-Disposition: download; filename="misp.search.attribute.results.json"'
),
'openioc' => array (
'type' => 'xml' ,
'layout' => 'xml/default' ,
'header' => 'Content-Disposition: download; filename="misp.search.attribute.results.openioc.xml"'
),
);
2018-11-23 14:11:33 +01:00
public $validFormats = array (
2020-04-30 12:35:38 +02:00
'attack-sightings' => array ( 'json' , 'AttackSightingsExport' , 'json' ),
'cache' => array ( 'txt' , 'CacheExport' , 'cache' ),
2020-08-31 12:32:28 +02:00
'count' => array ( 'txt' , 'CountExport' , 'txt' ),
2020-04-30 12:35:38 +02:00
'csv' => array ( 'csv' , 'CsvExport' , 'csv' ),
'hashes' => array ( 'txt' , 'HashesExport' , 'txt' ),
2021-08-31 08:09:43 +02:00
'hosts' => array ( 'txt' , 'HostsExport' , 'txt' ),
2018-11-23 14:11:33 +01:00
'json' => array ( 'json' , 'JsonExport' , 'json' ),
2020-04-30 12:35:38 +02:00
'netfilter' => array ( 'txt' , 'NetfilterExport' , 'sh' ),
'opendata' => array ( 'txt' , 'OpendataExport' , 'txt' ),
2018-12-17 16:36:27 +01:00
'openioc' => array ( 'xml' , 'OpeniocExport' , 'ioc' ),
2020-04-30 12:35:38 +02:00
'rpz' => array ( 'txt' , 'RPZExport' , 'rpz' ),
2018-11-23 14:11:33 +01:00
'snort' => array ( 'txt' , 'NidsSnortExport' , 'rules' ),
2020-04-30 12:35:38 +02:00
'suricata' => array ( 'txt' , 'NidsSuricataExport' , 'rules' ),
2018-11-23 14:11:33 +01:00
'text' => array ( 'txt' , 'TextExport' , 'txt' ),
2020-04-30 12:35:38 +02:00
'xml' => array ( 'xml' , 'XmlExport' , 'xml' ),
2019-04-29 19:23:14 +02:00
'yara' => array ( 'txt' , 'YaraExport' , 'yara' ),
2020-04-30 12:35:38 +02:00
'yara-json' => array ( 'json' , 'YaraExport' , 'json' )
2018-11-23 14:11:33 +01:00
);
2018-10-04 22:23:58 +02:00
2018-07-19 11:48:22 +02:00
// typeGroupings are a mapping to high level groups for attributes
// for example, IP addresses, domain names, hostnames and e-mail addresses are network related attribute types
// whilst filenames and hashes are file related attribute types
// This helps generate quick filtering for the event view, but we may reuse this and enhance it in the future for other uses (such as the API?)
public $typeGroupings = array (
2020-10-13 22:29:43 +02:00
'file' => array ( 'attachment' , 'pattern-in-file' , 'filename-pattern' , 'md5' , 'sha1' , 'sha224' , 'sha256' , 'sha384' , 'sha512' , 'sha512/224' , 'sha512/256' , 'sha3-224' , 'sha3-256' , 'sha3-384' , 'sha3-512' , 'ssdeep' , 'imphash' , 'telfhash' , 'impfuzzy' , 'authentihash' , 'vhash' , 'pehash' , 'tlsh' , 'cdhash' , 'filename' , 'filename|md5' , 'filename|sha1' , 'filename|sha224' , 'filename|sha256' , 'filename|sha384' , 'filename|sha512' , 'filename|sha512/224' , 'filename|sha512/256' , 'filename|sha3-224' , 'filename|sha3-256' , 'filename|sha3-384' , 'filename|sha3-512' , 'filename|authentihash' , 'filename|vhash' , 'filename|ssdeep' , 'filename|tlsh' , 'filename|imphash' , 'filename|pehash' , 'malware-sample' , 'x509-fingerprint-sha1' , 'x509-fingerprint-sha256' , 'x509-fingerprint-md5' ),
2020-12-24 11:56:58 +01:00
'network' => array ( 'ip-src' , 'ip-dst' , 'ip-src|port' , 'ip-dst|port' , 'mac-address' , 'mac-eui-64' , 'hostname' , 'hostname|port' , 'domain' , 'domain|ip' , 'email-dst' , 'url' , 'uri' , 'user-agent' , 'http-method' , 'AS' , 'snort' , 'bro' , 'zeek' , 'pattern-in-traffic' , 'x509-fingerprint-md5' , 'x509-fingerprint-sha1' , 'x509-fingerprint-sha256' , 'ja3-fingerprint-md5' , 'jarm-fingerprint' , 'favicon-mmh3' , 'hassh-md5' , 'hasshserver-md5' , 'community-id' ),
2018-07-04 10:22:58 +02:00
'financial' => array ( 'btc' , 'xmr' , 'iban' , 'bic' , 'bank-account-nr' , 'aba-rtn' , 'bin' , 'cc-number' , 'prtn' , 'phone-number' )
2018-07-19 11:48:22 +02:00
);
private $__fTool = false ;
public $order = array ( " Attribute.event_id " => " DESC " );
public $validate = array (
'event_id' => array (
'numeric' => array (
'rule' => array ( 'numeric' )
)
),
'type' => array (
'rule' => array ( 'validateTypeValue' ),
'message' => 'Options depend on the selected category.' ,
'required' => true
),
'category' => array (
'rule' => array ( 'validCategory' ),
'message' => 'Options : Payload delivery, Antivirus detection, Payload installation, Files dropped ...'
),
'value' => array (
'stringNotEmpty' => array (
'rule' => array ( 'stringNotEmpty' )
),
2021-06-15 13:14:02 +02:00
'stringControlCharacters' => array (
'rule' => array ( 'stringNotControlCharacters' ),
'message' => 'Value provided consists purely of control characters and is therefore considered to be empty.'
),
2021-02-04 11:31:38 +01:00
'validComposite' => array (
'rule' => array ( 'validComposite' ),
'message' => 'Composite type found but the value not in the composite (value1|value2) format.'
),
2018-07-19 11:48:22 +02:00
'userdefined' => array (
'rule' => array ( 'validateAttributeValue' ),
'message' => 'Value not in the right type/format. Please double check the value or select type "other".'
),
'uniqueValue' => array (
2021-02-04 11:31:38 +01:00
'rule' => array ( 'valueIsUnique' ),
'message' => 'A similar attribute already exists for this event.'
2018-07-19 11:48:22 +02:00
),
'maxTextLength' => array (
'rule' => array ( 'maxTextLength' )
)
),
'to_ids' => array (
'boolean' => array (
'rule' => array ( 'boolean' ),
'required' => false
)
),
'uuid' => array (
'uuid' => array (
2020-09-01 19:05:06 +02:00
'rule' => 'uuid' ,
2020-09-18 10:55:52 +02:00
'message' => 'Please provide a valid RFC 4122 UUID'
2018-07-19 11:48:22 +02:00
),
'unique' => array (
'rule' => 'isUnique' ,
'message' => 'The UUID provided is not unique' ,
2021-08-15 20:08:28 +02:00
'on' => 'create'
2018-07-19 11:48:22 +02:00
)
),
'distribution' => array (
2021-09-02 21:12:31 +02:00
'rule' => array ( 'inList' , array ( '0' , '1' , '2' , '3' , '4' , '5' )),
'message' => 'Options: Your organisation only, This community only, Connected communities, All communities, Sharing group, Inherit event' ,
'required' => true
2019-06-13 09:16:34 +02:00
),
'first_seen' => array (
'rule' => array ( 'datetimeOrNull' ),
'required' => false ,
'message' => array ( 'Invalid ISO 8601 format' )
),
'last_seen' => array (
2021-05-06 15:01:58 +02:00
'datetimeOrNull' => array (
'rule' => array ( 'datetimeOrNull' ),
'required' => false ,
'message' => array ( 'Invalid ISO 8601 format' )
),
'validateLastSeenValue' => array (
'rule' => array ( 'validateLastSeenValue' ),
'required' => false ,
'message' => array ( 'Last seen value should be greater than first seen value' )
),
2018-07-19 11:48:22 +02:00
)
);
// automatic resolution of complex types
// If the complex type "file" is chosen for example, then the system will try to categorise the values entered into a complex template field based
// on the regular expression rules
public $validTypeGroups = array (
'File' => array (
'description' => '' ,
'types' => array ( 'filename' , 'filename|md5' , 'filename|sha1' , 'filename|sha256' , 'md5' , 'sha1' , 'sha256' ),
),
'CnC' => array (
'description' => '' ,
'types' => array ( 'url' , 'domain' , 'hostname' , 'ip-dst' ),
),
);
public $typeGroupCategoryMapping = array (
'Payload delivery' => array ( 'File' , 'CnC' ),
'Payload installation' => array ( 'File' ),
'Artifacts dropped' => array ( 'File' ),
'Network activity' => array ( 'CnC' ),
);
public $belongsTo = array (
'Event' => array (
'className' => 'Event' ,
'foreignKey' => 'event_id' ,
'conditions' => '' ,
'fields' => '' ,
//'counterCache' => 'attribute_count',
//'counterScope' => array('Attribute.deleted' => 0),
'order' => ''
),
'SharingGroup' => array (
'className' => 'SharingGroup' ,
'foreignKey' => 'sharing_group_id'
),
'Object' => array (
'className' => 'MispObject' ,
'foreignKey' => 'object_id'
)
);
2021-04-25 17:36:29 +02:00
public $hasMany = [
'AttributeTag' => [
2018-07-19 11:48:22 +02:00
'dependent' => true
2021-04-25 17:36:29 +02:00
],
'Correlation' => [
'dependent' => false
],
'Sighting' => [
2018-07-19 11:48:22 +02:00
'className' => 'Sighting' ,
'dependent' => true ,
2021-04-25 17:36:29 +02:00
]
];
2018-07-19 11:48:22 +02:00
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 ,
)
);
public function afterFind ( $results , $primary = false )
{
foreach ( $results as $k => $v ) {
if ( isset ( $v [ 'Attribute' ][ 'object_relation' ]) && $v [ 'Attribute' ][ 'object_relation' ] === null ) {
$results [ $k ][ 'Attribute' ][ 'object_relation' ] = '' ;
}
2019-12-16 10:47:07 +01:00
$results [ $k ] = $this -> UTCToISODatetime ( $results [ $k ], $this -> alias );
2018-07-19 11:48:22 +02:00
}
return $results ;
}
public function beforeSave ( $options = array ())
{
2019-09-29 21:07:35 +02:00
if ( ! empty ( $this -> data [ 'Attribute' ][ 'id' ])) {
$this -> old = $this -> find ( 'first' , array (
'recursive' => - 1 ,
2021-09-02 21:38:50 +02:00
'conditions' => array ( 'Attribute.id' => $this -> data [ 'Attribute' ][ 'id' ]),
'fields' => [ 'value' , 'disable_correlation' , 'type' , 'distribution' , 'sharing_group_id' ],
2019-09-29 21:07:35 +02:00
));
} else {
2021-09-02 21:12:31 +02:00
$this -> old = null ;
2019-09-29 21:07:35 +02:00
}
2018-07-19 11:48:22 +02:00
// explode value of composite type in value1 and value2
// or copy value to value1 if not composite type
if ( ! empty ( $this -> data [ 'Attribute' ][ 'type' ])) {
// explode composite types in value1 and value2
2021-09-02 19:28:34 +02:00
if ( in_array ( $this -> data [ 'Attribute' ][ 'type' ], $this -> getCompositeTypes (), true )) {
2018-07-19 11:48:22 +02:00
$pieces = explode ( '|' , $this -> data [ 'Attribute' ][ 'value' ]);
2021-09-02 19:28:34 +02:00
if ( 2 !== count ( $pieces )) {
2018-07-19 11:48:22 +02:00
throw new InternalErrorException ( __ ( 'Composite type, but value not explodable' ));
}
$this -> data [ 'Attribute' ][ 'value1' ] = $pieces [ 0 ];
$this -> data [ 'Attribute' ][ 'value2' ] = $pieces [ 1 ];
} else {
$this -> data [ 'Attribute' ][ 'value1' ] = $this -> data [ 'Attribute' ][ 'value' ];
$this -> data [ 'Attribute' ][ 'value2' ] = '' ;
}
}
2019-12-16 10:47:07 +01:00
$this -> data = $this -> ISODatetimeToUTC ( $this -> data , $this -> alias );
2018-07-19 11:48:22 +02:00
// always return true after a beforeSave()
return true ;
}
private function __alterAttributeCount ( $event_id , $increment = true )
{
2020-05-11 18:18:17 +02:00
return $this -> Event -> updateAll (
2020-06-05 16:48:53 +02:00
array ( 'Event.attribute_count' => $increment ? 'Event.attribute_count+1' : 'GREATEST(Event.attribute_count, 1) - 1' ),
2020-05-11 18:18:17 +02:00
array ( 'Event.id' => $event_id )
);
2018-07-19 11:48:22 +02:00
}
public function afterSave ( $created , $options = array ())
{
2021-09-07 10:09:08 +02:00
// Passing event in `parentEvent` field will speed up correlation
$passedEvent = isset ( $options [ 'parentEvent' ]) ? $options [ 'parentEvent' ] : false ;
2019-07-30 12:03:49 +02:00
// add attributeTags via the shorthand ID list
if ( ! empty ( $this -> data [ 'Attribute' ][ 'tag_ids' ])) {
foreach ( $this -> data [ 'Attribute' ][ 'tag_ids' ] as $tag_id ) {
$this -> AttributeTag -> attachTagToAttribute ( $this -> id , $this -> data [ 'Attribute' ][ 'event_id' ], $tag_id );
}
}
2018-07-19 11:48:22 +02:00
// update correlation...
if ( isset ( $this -> data [ 'Attribute' ][ 'deleted' ]) && $this -> data [ 'Attribute' ][ 'deleted' ]) {
2021-04-25 17:36:29 +02:00
$this -> Correlation -> beforeSaveCorrelation ( $this -> data [ 'Attribute' ]);
2018-07-19 11:48:22 +02:00
if ( isset ( $this -> data [ 'Attribute' ][ 'event_id' ])) {
2020-05-11 18:18:17 +02:00
$this -> __alterAttributeCount ( $this -> data [ 'Attribute' ][ 'event_id' ], false );
2018-07-19 11:48:22 +02:00
}
} else {
2019-09-29 21:07:35 +02:00
/*
* Only recorrelate if :
* - We are dealing with a new attribute OR
* - The existing attribute ' s previous state is known AND
2020-04-17 11:29:09 +02:00
* value , type , disable correlation or distribution have changed
2019-09-29 21:07:35 +02:00
* This will avoid recorrelations when it ' s not really needed , such as adding a tag
*/
if ( ! $created ) {
if (
empty ( $this -> old ) ||
$this -> data [ 'Attribute' ][ 'value' ] != $this -> old [ 'Attribute' ][ 'value' ] ||
$this -> data [ 'Attribute' ][ 'disable_correlation' ] != $this -> old [ 'Attribute' ][ 'disable_correlation' ] ||
2020-04-17 11:29:09 +02:00
$this -> data [ 'Attribute' ][ 'type' ] != $this -> old [ 'Attribute' ][ 'type' ] ||
$this -> data [ 'Attribute' ][ 'distribution' ] != $this -> old [ 'Attribute' ][ 'distribution' ] ||
$this -> data [ 'Attribute' ][ 'sharing_group_id' ] != $this -> old [ 'Attribute' ][ 'sharing_group_id' ]
2019-09-29 21:07:35 +02:00
) {
2021-04-25 17:36:29 +02:00
$this -> Correlation -> beforeSaveCorrelation ( $this -> data [ 'Attribute' ]);
$this -> Correlation -> afterSaveCorrelation ( $this -> data [ 'Attribute' ], false , $passedEvent );
2019-09-29 21:07:35 +02:00
}
} else {
2021-04-25 17:36:29 +02:00
$this -> Correlation -> afterSaveCorrelation ( $this -> data [ 'Attribute' ], false , $passedEvent );
2019-09-29 21:07:35 +02:00
}
2018-07-19 11:48:22 +02:00
}
$result = true ;
// if the 'data' field is set on the $this->data then save the data to the correct file
2021-10-09 17:07:29 +02:00
if ( isset ( $this -> data [ 'Attribute' ][ 'type' ]) && $this -> typeIsAttachment ( $this -> data [ 'Attribute' ][ 'type' ])) {
2021-10-09 18:45:51 +02:00
if ( isset ( $this -> data [ 'Attribute' ][ 'data_raw' ])) {
2021-10-09 17:07:29 +02:00
$this -> data [ 'Attribute' ][ 'data' ] = $this -> data [ 'Attribute' ][ 'data_raw' ];
2021-10-09 18:45:51 +02:00
unset ( $this -> data [ 'Attribute' ][ 'data_raw' ]);
} elseif ( isset ( $this -> data [ 'Attribute' ][ 'data' ])) {
2021-10-09 18:21:31 +02:00
$this -> data [ 'Attribute' ][ 'data' ] = base64_decode ( $this -> data [ 'Attribute' ][ 'data' ]);
2021-10-09 17:07:29 +02:00
}
2021-10-09 18:21:31 +02:00
$result = $this -> saveAttachment ( $this -> data [ 'Attribute' ]);
2018-07-19 11:48:22 +02:00
}
2019-03-05 12:24:56 +01:00
$pubToZmq = Configure :: read ( 'Plugin.ZeroMQ_enable' ) && Configure :: read ( 'Plugin.ZeroMQ_attribute_notifications_enable' );
$kafkaTopic = Configure :: read ( 'Plugin.Kafka_attribute_notifications_topic' );
$pubToKafka = Configure :: read ( 'Plugin.Kafka_enable' ) && Configure :: read ( 'Plugin.Kafka_attribute_notifications_enable' ) && ! empty ( $kafkaTopic );
if ( $pubToZmq || $pubToKafka ) {
2018-07-19 11:48:22 +02:00
$attribute = $this -> fetchAttribute ( $this -> id );
if ( ! empty ( $attribute )) {
$user = array (
'org_id' => - 1 ,
'Role' => array (
'perm_site_admin' => 1
)
);
2020-07-28 00:01:32 +02:00
$attribute [ 'Attribute' ][ 'Sighting' ] = $this -> Sighting -> attachToEvent ( $attribute , $user , $attribute );
2018-07-19 11:48:22 +02:00
if ( empty ( $attribute [ 'Object' ][ 'id' ])) {
unset ( $attribute [ 'Object' ]);
}
$action = $created ? 'add' : 'edit' ;
if ( ! empty ( $this -> data [ 'Attribute' ][ 'deleted' ])) {
$action = 'soft-delete' ;
}
2019-03-05 12:24:56 +01:00
if ( $pubToZmq ) {
if ( Configure :: read ( 'Plugin.ZeroMQ_include_attachments' ) && $this -> typeIsAttachment ( $attribute [ 'Attribute' ][ 'type' ])) {
$attribute [ 'Attribute' ][ 'data' ] = $this -> base64EncodeAttachment ( $attribute [ 'Attribute' ]);
}
$pubSubTool = $this -> getPubSubTool ();
$pubSubTool -> attribute_save ( $attribute , $action );
unset ( $attribute [ 'Attribute' ][ 'data' ]);
}
if ( $pubToKafka ) {
if ( Configure :: read ( 'Plugin.Kafka_include_attachments' ) && $this -> typeIsAttachment ( $attribute [ 'Attribute' ][ 'type' ])) {
$attribute [ 'Attribute' ][ 'data' ] = $this -> base64EncodeAttachment ( $attribute [ 'Attribute' ]);
}
$kafkaPubTool = $this -> getKafkaPubTool ();
$kafkaPubTool -> publishJson ( $kafkaTopic , $attribute , $action );
2018-07-19 11:48:22 +02:00
}
}
}
2021-09-02 21:12:31 +02:00
if ( Configure :: read ( 'MISP.enable_advanced_correlations' ) && in_array ( $this -> data [ 'Attribute' ][ 'type' ], [ 'ip-src' , 'ip-dst' ], true ) && strpos ( $this -> data [ 'Attribute' ][ 'value' ], '/' )) {
2018-07-19 11:48:22 +02:00
$this -> setCIDRList ();
}
if ( $created && isset ( $this -> data [ 'Attribute' ][ 'event_id' ]) && empty ( $this -> data [ 'Attribute' ][ 'skip_auto_increment' ])) {
$this -> __alterAttributeCount ( $this -> data [ 'Attribute' ][ 'event_id' ]);
}
return $result ;
}
public function beforeDelete ( $cascade = true )
{
// delete attachments from the disk
$this -> read (); // first read the attribute from the db
if ( $this -> typeIsAttachment ( $this -> data [ 'Attribute' ][ 'type' ])) {
2020-08-13 15:58:42 +02:00
$this -> loadAttachmentTool () -> delete ( $this -> data [ 'Attribute' ][ 'event_id' ], $this -> data [ 'Attribute' ][ 'id' ]);
2018-07-19 11:48:22 +02:00
}
// update correlation..
2021-09-03 16:25:10 +02:00
$this -> Correlation -> beforeSaveCorrelation ( $this -> data [ 'Attribute' ]);
2018-07-19 11:48:22 +02:00
if ( ! empty ( $this -> data [ 'Attribute' ][ 'id' ])) {
if ( Configure :: read ( 'Plugin.ZeroMQ_enable' ) && Configure :: read ( 'Plugin.ZeroMQ_attribute_notifications_enable' )) {
$pubSubTool = $this -> getPubSubTool ();
$pubSubTool -> attribute_save ( $this -> data , 'delete' );
}
2019-03-05 12:24:56 +01:00
$kafkaTopic = Configure :: read ( 'Plugin.Kafka_attribute_notifications_topic' );
if ( Configure :: read ( 'Plugin.Kafka_enable' ) && Configure :: read ( 'Plugin.Kafka_attribute_notifications_enable' ) && ! empty ( $kafkaTopic )) {
$kafkaPubTool = $this -> getKafkaPubTool ();
$kafkaPubTool -> publishJson ( $kafkaTopic , $this -> data , 'delete' );
}
2018-07-19 11:48:22 +02:00
}
}
public function afterDelete ()
{
2021-10-07 09:51:59 +02:00
if ( Configure :: read ( 'MISP.enable_advanced_correlations' ) && in_array ( $this -> data [ 'Attribute' ][ 'type' ], [ 'ip-src' , 'ip-dst' ], true ) && strpos ( $this -> data [ 'Attribute' ][ 'value' ], '/' )) {
2018-07-19 11:48:22 +02:00
$this -> setCIDRList ();
}
if ( isset ( $this -> data [ 'Attribute' ][ 'event_id' ])) {
if ( empty ( $this -> data [ 'Attribute' ][ 'deleted' ])) {
$this -> __alterAttributeCount ( $this -> data [ 'Attribute' ][ 'event_id' ], false );
}
}
if ( ! empty ( $this -> data [ 'Attribute' ][ 'id' ])) {
$this -> Object -> ObjectReference -> deleteAll (
array (
'ObjectReference.referenced_type' => 0 ,
'ObjectReference.referenced_id' => $this -> data [ 'Attribute' ][ 'id' ],
),
false
);
2020-05-21 17:54:40 +02:00
if ( $this -> data [ 'Attribute' ][ 'type' ] === 'ssdeep' ) {
$this -> FuzzyCorrelateSsdeep = ClassRegistry :: init ( 'FuzzyCorrelateSsdeep' );
$this -> FuzzyCorrelateSsdeep -> purge ( null , $this -> data [ 'Attribute' ][ 'id' ]);
}
2018-07-19 11:48:22 +02:00
}
}
public function beforeValidate ( $options = array ())
{
2021-09-02 19:28:34 +02:00
if ( empty ( $this -> data [ 'Attribute' ][ 'type' ])) {
2019-11-15 14:11:24 +01:00
$this -> validationErrors [ 'type' ] = [ 'No type set.' ];
2018-07-19 11:48:22 +02:00
return false ;
}
2021-09-02 19:28:34 +02:00
$type = $this -> data [ 'Attribute' ][ 'type' ];
2018-07-19 11:48:22 +02:00
if ( is_array ( $this -> data [ 'Attribute' ][ 'value' ])) {
2021-09-02 19:28:34 +02:00
$this -> validationErrors [ 'value' ] = [ 'Value is an array.' ];
2018-07-19 11:48:22 +02:00
return false ;
}
2020-03-06 10:58:50 +01:00
2018-07-19 11:48:22 +02:00
if ( ! empty ( $this -> data [ 'Attribute' ][ 'object_id' ]) && empty ( $this -> data [ 'Attribute' ][ 'object_relation' ])) {
2021-09-02 19:28:34 +02:00
$this -> validationErrors [ 'object_relation' ] = [ 'Object attribute sent, but no object_relation set.' ];
2018-07-19 11:48:22 +02:00
return false ;
}
2021-09-02 19:28:34 +02:00
// If `value1` or `value2` provided and `value` is empty, merge them into `value` because of validation
if ( empty ( $this -> data [ 'Attribute' ][ 'value' ])) {
if ( ! empty ( $this -> data [ 'Attribute' ][ 'value1' ]) && ! empty ( $this -> data [ 'Attribute' ][ 'value2' ])) {
$this -> data [ 'Attribute' ][ 'value' ] = " { $this -> data [ 'Attribute' ][ 'value1' ] } | { $this -> data [ 'Attribute' ][ 'value2' ] } " ;
} else if ( ! empty ( $this -> data [ 'Attribute' ][ 'value1' ])) {
$this -> data [ 'Attribute' ][ 'value' ] = $this -> data [ 'Attribute' ][ 'value1' ];
}
}
// remove leading and trailing blanks and refang value and
$this -> data [ 'Attribute' ][ 'value' ] = ComplexTypeTool :: refangValue ( trim ( $this -> data [ 'Attribute' ][ 'value' ]), $type );
// make some changes to the inserted value
$this -> data [ 'Attribute' ][ 'value' ] = $this -> modifyBeforeValidation ( $type , $this -> data [ 'Attribute' ][ 'value' ]);
// Run user defined regexp to attribute value
$result = $this -> runRegexp ( $type , $this -> data [ 'Attribute' ][ 'value' ]);
if ( $result === false ) {
$this -> invalidate ( 'value' , 'This value is blocked by a regular expression in the import filters.' );
} else {
$this -> data [ 'Attribute' ][ 'value' ] = $result ;
2018-07-19 11:48:22 +02:00
}
if ( empty ( $this -> data [ 'Attribute' ][ 'comment' ])) {
$this -> data [ 'Attribute' ][ 'comment' ] = " " ;
}
// generate UUID if it doesn't exist
if ( empty ( $this -> data [ 'Attribute' ][ 'uuid' ])) {
$this -> data [ 'Attribute' ][ 'uuid' ] = CakeText :: uuid ();
2020-09-01 19:05:06 +02:00
} else {
$this -> data [ 'Attribute' ][ 'uuid' ] = strtolower ( $this -> data [ 'Attribute' ][ 'uuid' ]);
2018-07-19 11:48:22 +02:00
}
// generate timestamp if it doesn't exist
if ( empty ( $this -> data [ 'Attribute' ][ 'timestamp' ])) {
2021-09-02 19:28:34 +02:00
$this -> data [ 'Attribute' ][ 'timestamp' ] = time ();
2018-07-19 11:48:22 +02:00
}
2019-06-13 09:16:34 +02:00
// parse first_seen different formats
if ( isset ( $this -> data [ 'Attribute' ][ 'first_seen' ])) {
$this -> data [ 'Attribute' ][ 'first_seen' ] = $this -> data [ 'Attribute' ][ 'first_seen' ] === '' ? null : $this -> data [ 'Attribute' ][ 'first_seen' ];
}
// parse last_seen different formats
if ( isset ( $this -> data [ 'Attribute' ][ 'last_seen' ])) {
$this -> data [ 'Attribute' ][ 'last_seen' ] = $this -> data [ 'Attribute' ][ 'last_seen' ] === '' ? null : $this -> data [ 'Attribute' ][ 'last_seen' ];
}
2018-07-19 11:48:22 +02:00
// Set defaults for when some of the mandatory fields don't have defaults
// These fields all have sane defaults either based on another field, or due to server settings
if ( ! isset ( $this -> data [ 'Attribute' ][ 'distribution' ])) {
2021-10-09 23:32:51 +02:00
$this -> data [ 'Attribute' ][ 'distribution' ] = $this -> defaultDistribution ();
2018-07-19 11:48:22 +02:00
}
2021-09-02 19:28:34 +02:00
// If category is not provided, assign default category by type
if ( empty ( $this -> data [ 'Attribute' ][ 'category' ])) {
$this -> data [ 'Attribute' ][ 'category' ] = $this -> typeDefinitions [ $type ][ 'default_category' ];
2018-07-19 11:48:22 +02:00
}
if ( ! isset ( $this -> data [ 'Attribute' ][ 'to_ids' ])) {
2021-09-02 19:28:34 +02:00
$this -> data [ 'Attribute' ][ 'to_ids' ] = $this -> typeDefinitions [ $type ][ 'to_ids' ];
2018-07-19 11:48:22 +02:00
}
if ( $this -> data [ 'Attribute' ][ 'distribution' ] != 4 ) {
$this -> data [ 'Attribute' ][ 'sharing_group_id' ] = 0 ;
}
// return true, otherwise the object cannot be saved
return true ;
}
public function validComposite ( $fields )
{
2021-09-02 19:28:34 +02:00
if ( in_array ( $this -> data [ 'Attribute' ][ 'type' ], $this -> getCompositeTypes (), true )) {
2020-10-13 18:56:26 +02:00
if ( substr_count ( $fields [ 'value' ], '|' ) !== 1 ) {
2018-07-19 11:48:22 +02:00
return false ;
}
}
return true ;
}
2021-06-15 13:14:02 +02:00
public function stringNotControlCharacters ( $fields )
{
2021-06-15 13:54:38 +02:00
if ( ctype_cntrl ( $this -> data [ 'Attribute' ][ 'value' ])) {
2021-06-15 13:14:02 +02:00
return false ;
}
2021-09-02 19:28:34 +02:00
if ( in_array ( $this -> data [ 'Attribute' ][ 'type' ], $this -> getCompositeTypes (), true )) {
2021-06-15 15:56:41 +02:00
$values = explode ( '|' , $this -> data [ 'Attribute' ][ 'value' ]);
2021-06-15 20:10:56 +02:00
if ( ctype_cntrl ( $values [ 0 ])) {
return false ;
}
if ( ! empty ( $values [ 1 ]) && ctype_cntrl ( $values [ 1 ])) {
2021-06-15 15:56:41 +02:00
return false ;
}
}
2021-06-15 13:14:02 +02:00
return true ;
}
2018-07-19 11:48:22 +02:00
public function maxTextLength ( $fields )
{
if ( strlen ( $fields [ 'value' ]) > 65535 ) {
return __ ( 'The entered string is too long and would get truncated. Please consider adding the data as an attachment instead' );
}
return true ;
}
public function validCategory ( $fields )
{
2020-06-26 22:40:37 +02:00
return isset ( $this -> categoryDefinitions [ $fields [ 'category' ]]);
2018-07-19 11:48:22 +02:00
}
2020-06-26 22:40:37 +02:00
/**
* Check if the attribute already exists in the same event .
*
* @ param array $fields
* @ return bool
*/
2018-07-19 11:48:22 +02:00
public function valueIsUnique ( $fields )
{
2021-10-09 17:45:12 +02:00
if ( ! empty ( $this -> data [ 'Attribute' ][ 'deleted' ])) {
2018-07-19 11:48:22 +02:00
return true ;
}
// We escape this rule for objects as we can have the same category/type/value combination in different objects
if ( ! empty ( $this -> data [ 'Attribute' ][ 'object_relation' ])) {
return true ;
}
2020-06-26 22:40:37 +02:00
$type = $this -> data [ 'Attribute' ][ 'type' ];
2018-07-19 11:48:22 +02:00
$conditions = array (
2021-10-09 17:45:12 +02:00
'Attribute.event_id' => $this -> data [ 'Attribute' ][ 'event_id' ],
2018-07-19 11:48:22 +02:00
'Attribute.type' => $type ,
2021-10-09 17:45:12 +02:00
'Attribute.category' => $this -> data [ 'Attribute' ][ 'category' ],
2018-07-19 11:48:22 +02:00
'Attribute.deleted' => 0 ,
2020-06-26 22:40:37 +02:00
'Attribute.object_id' => 0 ,
2018-07-19 11:48:22 +02:00
);
2020-06-26 22:40:37 +02:00
$value = $fields [ 'value' ];
2021-09-02 19:28:34 +02:00
if ( in_array ( $type , $this -> getCompositeTypes (), true )) {
2020-06-26 22:40:37 +02:00
$value = explode ( '|' , $value );
$conditions [ 'Attribute.value1' ] = $value [ 0 ];
$conditions [ 'Attribute.value2' ] = $value [ 1 ];
} else {
$conditions [ 'Attribute.value1' ] = $value ;
}
2018-07-19 11:48:22 +02:00
if ( isset ( $this -> data [ 'Attribute' ][ 'id' ])) {
$conditions [ 'Attribute.id !=' ] = $this -> data [ 'Attribute' ][ 'id' ];
}
2021-10-09 17:45:12 +02:00
return ! $this -> hasAny ( $conditions );
2018-07-19 11:48:22 +02:00
}
public function validateTypeValue ( $fields )
{
$category = $this -> data [ 'Attribute' ][ 'category' ];
if ( isset ( $this -> categoryDefinitions [ $category ][ 'types' ])) {
2021-01-09 21:12:57 +01:00
return in_array ( $fields [ 'type' ], $this -> categoryDefinitions [ $category ][ 'types' ], true );
2018-07-19 11:48:22 +02:00
}
return false ;
}
public function validateAttributeValue ( $fields )
{
$value = $fields [ 'value' ];
return $this -> runValidation ( $value , $this -> data [ 'Attribute' ][ 'type' ]);
}
2019-06-13 09:16:34 +02:00
// check whether the variable is null or datetime
public function datetimeOrNull ( $fields )
{
$k = array_keys ( $fields )[ 0 ];
$seen = $fields [ $k ];
try {
new DateTime ( $seen );
$returnValue = true ;
} catch ( Exception $e ) {
$returnValue = false ;
}
return $returnValue || is_null ( $seen );
}
2021-05-06 15:01:58 +02:00
public function validateLastSeenValue ( $fields )
{
$ls = $fields [ 'last_seen' ];
2021-07-27 14:33:54 +02:00
if ( ! isset ( $this -> data [ 'Attribute' ][ 'first_seen' ]) || is_null ( $ls )) {
2021-05-06 15:01:58 +02:00
return true ;
}
$converted = $this -> ISODatetimeToUTC ([ 'Attribute' => [
'first_seen' => $this -> data [ 'Attribute' ][ 'first_seen' ],
'last_seen' => $ls
]], 'Attribute' );
2021-05-11 11:22:19 +02:00
if ( $converted [ 'Attribute' ][ 'first_seen' ] > $converted [ 'Attribute' ][ 'last_seen' ]) {
2021-05-06 15:01:58 +02:00
return false ;
}
return true ;
}
2021-07-27 16:16:52 +02:00
const HEX_HAS_LENGTHS = array (
2018-07-19 11:48:22 +02:00
'authentihash' => 64 ,
'md5' => 32 ,
'imphash' => 32 ,
2020-10-13 22:29:43 +02:00
'telfhash' => 70 ,
2018-07-19 11:48:22 +02:00
'sha1' => 40 ,
2020-05-26 12:26:24 +02:00
'git-commit-id' => 40 ,
2018-07-19 11:48:22 +02:00
'x509-fingerprint-md5' => 32 ,
'x509-fingerprint-sha1' => 40 ,
'x509-fingerprint-sha256' => 64 ,
2018-12-30 12:26:33 +01:00
'ja3-fingerprint-md5' => 32 ,
2020-11-23 21:03:36 +01:00
'jarm-fingerprint' => 62 ,
2019-01-13 11:47:30 +01:00
'hassh-md5' => 32 ,
'hasshserver-md5' => 32 ,
2018-07-19 11:48:22 +02:00
'pehash' => 40 ,
'sha224' => 56 ,
'sha256' => 64 ,
'sha384' => 96 ,
'sha512' => 128 ,
'sha512/224' => 56 ,
2020-08-23 18:26:56 +02:00
'sha512/256' => 64 ,
'sha3-224' => 56 ,
'sha3-256' => 64 ,
'sha3-384' => 96 ,
'sha3-512' => 128
2018-07-19 11:48:22 +02:00
);
public function runValidation ( $value , $type )
{
$returnValue = false ;
// check data validation
switch ( $type ) {
case 'md5' :
case 'imphash' :
2020-10-13 22:29:43 +02:00
case 'telfhash' :
2018-07-19 11:48:22 +02:00
case 'sha1' :
case 'sha224' :
case 'sha256' :
case 'sha384' :
case 'sha512' :
case 'sha512/224' :
case 'sha512/256' :
2020-08-23 18:26:56 +02:00
case 'sha3-224' :
case 'sha3-256' :
case 'sha3-384' :
case 'sha3-512' :
2018-07-19 11:48:22 +02:00
case 'authentihash' :
2018-12-30 12:26:33 +01:00
case 'ja3-fingerprint-md5' :
2020-11-23 21:03:36 +01:00
case 'jarm-fingerprint' :
2019-01-13 11:47:30 +01:00
case 'hassh-md5' :
case 'hasshserver-md5' :
2018-07-19 11:48:22 +02:00
case 'x509-fingerprint-md5' :
case 'x509-fingerprint-sha256' :
case 'x509-fingerprint-sha1' :
2020-05-26 12:26:24 +02:00
case 'git-commit-id' :
2020-10-13 09:23:44 +02:00
if ( $this -> isHashValid ( $type , $value )) {
return true ;
2018-07-19 11:48:22 +02:00
} else {
2021-07-22 12:56:47 +02:00
$length = self :: HEX_HAS_LENGTHS [ $type ];
2020-10-13 09:23:44 +02:00
return __ ( 'Checksum has an invalid length or format (expected: %s hexadecimal characters). Please double check the value or select type "other".' , $length );
2018-07-19 11:48:22 +02:00
}
case 'tlsh' :
if ( preg_match ( " #^[0-9a-f] { 35,} $ # " , $value )) {
$returnValue = true ;
} else {
$returnValue = __ ( 'Checksum has an invalid length or format (expected: at least 35 hexadecimal characters). Please double check the value or select type "other".' );
}
break ;
case 'pehash' :
2020-10-13 09:23:44 +02:00
if ( $this -> isHashValid ( 'pehash' , $value )) {
2018-07-19 11:48:22 +02:00
$returnValue = true ;
} else {
$returnValue = __ ( 'The input doesn\'t match the expected sha1 format (expected: 40 hexadecimal characters). Keep in mind that MISP currently only supports SHA1 for PEhashes, if you would like to get the support extended to other hash types, make sure to create a github ticket about it at https://github.com/MISP/MISP!' );
}
break ;
case 'ssdeep' :
2020-10-13 18:13:50 +02:00
if ( substr_count ( $value , ':' ) === 2 ) {
2018-07-19 11:48:22 +02:00
$parts = explode ( ':' , $value );
2020-10-13 18:13:50 +02:00
if ( $this -> isPositiveInteger ( $parts [ 0 ])) {
2020-10-13 09:23:44 +02:00
return true ;
2018-07-19 11:48:22 +02:00
}
}
2020-10-13 09:23:44 +02:00
return __ ( 'Invalid SSDeep hash. The format has to be blocksize:hash:hash' );
2018-07-19 11:48:22 +02:00
case 'impfuzzy' :
2020-10-13 18:13:50 +02:00
if ( substr_count ( $value , ':' ) === 2 ) {
2018-07-19 11:48:22 +02:00
$parts = explode ( ':' , $value );
2020-10-13 18:13:50 +02:00
if ( $this -> isPositiveInteger ( $parts [ 0 ])) {
2018-07-19 11:48:22 +02:00
$returnValue = true ;
}
}
if ( ! $returnValue ) {
$returnValue = __ ( 'Invalid impfuzzy format. The format has to be imports:hash:hash' );
}
break ;
2018-12-19 20:19:49 +01:00
case 'cdhash' :
if ( preg_match ( " #^[0-9a-f] { 40,} $ # " , $value )) {
$returnValue = true ;
} else {
$returnValue = __ ( 'The input doesn\'t match the expected format (expected: 40 or more hexadecimal characters)' );
}
break ;
2018-07-19 11:48:22 +02:00
case 'http-method' :
if ( preg_match ( " #(OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT|PROPFIND|PROPPATCH|MKCOL|COPY|MOVE|LOCK|UNLOCK|VERSION-CONTROL|REPORT|CHECKOUT|CHECKIN|UNCHECKOUT|MKWORKSPACE|UPDATE|LABEL|MERGE|BASELINE-CONTROL|MKACTIVITY|ORDERPATCH|ACL|PATCH|SEARCH)# " , $value )) {
$returnValue = true ;
} else {
2020-10-13 09:23:44 +02:00
$returnValue = __ ( 'Unknown HTTP method.' );
2018-07-19 11:48:22 +02:00
}
break ;
case 'filename|pehash' :
// no newline
if ( preg_match ( " #^.+ \ |[0-9a-f] { 40} $ # " , $value )) {
$returnValue = true ;
} else {
$returnValue = __ ( 'The input doesn\'t match the expected filename|sha1 format (expected: filename|40 hexadecimal characters). Keep in mind that MISP currently only supports SHA1 for PEhashes, if you would like to get the support extended to other hash types, make sure to create a github ticket about it at https://github.com/MISP/MISP!' );
}
break ;
case 'filename|md5' :
case 'filename|sha1' :
case 'filename|imphash' :
case 'filename|sha224' :
case 'filename|sha256' :
case 'filename|sha384' :
case 'filename|sha512' :
case 'filename|sha512/224' :
case 'filename|sha512/256' :
2020-08-23 18:26:56 +02:00
case 'filename|sha3-224' :
case 'filename|sha3-256' :
case 'filename|sha3-384' :
case 'filename|sha3-512' :
2018-07-19 11:48:22 +02:00
case 'filename|authentihash' :
$parts = explode ( '|' , $type );
2021-07-22 12:56:47 +02:00
$length = self :: HEX_HAS_LENGTHS [ $parts [ 1 ]];
2018-07-19 11:48:22 +02:00
if ( preg_match ( " #^.+ \ |[0-9a-f] { " . $length . " } $ # " , $value )) {
$returnValue = true ;
} else {
$returnValue = __ ( 'Checksum has an invalid length or format (expected: filename|%s hexadecimal characters). Please double check the value or select type "other".' , $length );
}
break ;
case 'filename|ssdeep' :
if ( substr_count ( $value , '|' ) != 1 || ! preg_match ( " #^.+ \ |.+ $ # " , $value )) {
$returnValue = __ ( 'Invalid composite type. The format has to be %s.' , $type );
} else {
$composite = explode ( '|' , $value );
$value = $composite [ 1 ];
if ( substr_count ( $value , ':' ) == 2 ) {
$parts = explode ( ':' , $value );
2020-10-13 18:13:50 +02:00
if ( $this -> isPositiveInteger ( $parts [ 0 ])) {
2018-07-19 11:48:22 +02:00
$returnValue = true ;
}
}
if ( ! $returnValue ) {
$returnValue = __ ( 'Invalid SSDeep hash (expected: blocksize:hash:hash).' );
}
}
break ;
case 'filename|tlsh' :
if ( preg_match ( " #^.+ \ |[0-9a-f] { 35,} $ # " , $value )) {
$returnValue = true ;
} else {
$returnValue = __ ( 'Checksum has an invalid length or format (expected: filename|at least 35 hexadecimal characters). Please double check the value or select type "other".' );
}
break ;
2020-08-17 16:36:47 +02:00
case 'filename|vhash' :
2020-11-25 21:53:56 +01:00
if ( preg_match ( '#^.+\|.+$#' , $value )) {
2020-08-17 16:36:47 +02:00
$returnValue = true ;
} else {
$returnValue = __ ( 'Checksum has an invalid length or format (expected: filename|string characters). Please double check the value or select type "other".' );
}
break ;
2018-07-19 11:48:22 +02:00
case 'ip-src' :
case 'ip-dst' :
if ( strpos ( $value , '/' ) !== false ) {
$parts = explode ( " / " , $value );
2020-10-13 18:13:50 +02:00
if ( count ( $parts ) !== 2 || ! $this -> isPositiveInteger ( $parts [ 1 ])) {
2020-05-14 18:13:39 +02:00
return __ ( 'Invalid CIDR notation value found.' );
2018-07-19 11:48:22 +02:00
}
2020-05-14 18:13:39 +02:00
if ( filter_var ( $parts [ 0 ], FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
if ( $parts [ 1 ] > 32 ) {
2020-10-13 18:13:50 +02:00
return __ ( 'Invalid CIDR notation value found, for IPv4 must be lower or equal 32.' );
2020-05-14 18:13:39 +02:00
}
} else if ( filter_var ( $parts [ 0 ], FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )) {
if ( $parts [ 1 ] > 128 ) {
2020-10-13 18:13:50 +02:00
return __ ( 'Invalid CIDR notation value found, for IPv6 must be lower or equal 128.' );
2020-05-14 18:13:39 +02:00
}
} else {
return __ ( 'IP address has an invalid format.' );
}
} else if ( ! filter_var ( $value , FILTER_VALIDATE_IP )) {
return __ ( 'IP address has an invalid format.' );
2018-07-19 11:48:22 +02:00
}
2020-05-14 18:13:39 +02:00
return true ;
2018-07-19 11:48:22 +02:00
case 'port' :
2020-07-06 20:15:56 +02:00
if ( ! $this -> isPortValid ( $value )) {
2020-10-13 18:13:50 +02:00
$returnValue = __ ( 'Port numbers have to be integers between 1 and 65535.' );
2018-07-19 11:48:22 +02:00
} else {
$returnValue = true ;
}
break ;
case 'ip-dst|port' :
case 'ip-src|port' :
$parts = explode ( '|' , $value );
2020-10-13 22:33:42 +02:00
if ( ! filter_var ( $parts [ 0 ], FILTER_VALIDATE_IP )) {
return __ ( 'IP address has an invalid format.' );
2018-07-19 11:48:22 +02:00
}
2020-10-13 22:33:42 +02:00
if ( ! $this -> isPortValid ( $parts [ 1 ])) {
return __ ( 'Port numbers have to be integers between 1 and 65535.' );
}
return true ;
2018-07-19 11:48:22 +02:00
case 'mac-address' :
2020-10-13 18:20:37 +02:00
if ( preg_match ( '/^([a-fA-F0-9]{2}[:]?){6}$/' , $value )) {
2018-07-19 11:48:22 +02:00
$returnValue = true ;
}
break ;
case 'mac-eui-64' :
2020-10-13 18:20:37 +02:00
if ( preg_match ( '/^([a-fA-F0-9]{2}[:]?){8}$/' , $value )) {
2018-07-19 11:48:22 +02:00
$returnValue = true ;
}
break ;
case 'hostname' :
case 'domain' :
2020-07-06 20:15:56 +02:00
if ( $this -> isDomainValid ( $value )) {
2018-07-19 11:48:22 +02:00
$returnValue = true ;
} else {
2020-10-13 22:33:42 +02:00
$returnValue = __ ( '%s has an invalid format. Please double check the value or select type "other".' , ucfirst ( $type ));
2018-07-19 11:48:22 +02:00
}
break ;
case 'hostname|port' :
$parts = explode ( '|' , $value );
2020-10-13 22:33:42 +02:00
if ( ! $this -> isDomainValid ( $parts [ 0 ])) {
return __ ( 'Hostname has an invalid format.' );
2018-07-19 11:48:22 +02:00
}
2020-10-13 22:33:42 +02:00
if ( ! $this -> isPortValid ( $parts [ 1 ])) {
return __ ( 'Port numbers have to be integers between 1 and 65535.' );
}
return true ;
2018-07-19 11:48:22 +02:00
case 'domain|ip' :
if ( preg_match ( " #^[A-Z0-9. \ -_]+ \ .[A-Z0-9 \ -] { 2,} \ |.* $ #i " , $value )) {
$parts = explode ( '|' , $value );
if ( filter_var ( $parts [ 1 ], FILTER_VALIDATE_IP )) {
$returnValue = true ;
} else {
$returnValue = __ ( 'IP address has an invalid format.' );
}
} else {
$returnValue = __ ( 'Domain name has an invalid format.' );
}
break ;
2020-09-03 12:02:59 +02:00
case 'email' :
2018-07-19 11:48:22 +02:00
case 'email-src' :
2019-12-05 19:22:38 +01:00
case 'eppn' :
2018-07-19 11:48:22 +02:00
case 'email-dst' :
case 'target-email' :
case 'whois-registrant-email' :
case 'dns-soa-email' :
case 'jabber-id' :
// we don't use the native function to prevent issues with partial email addresses
if ( preg_match ( " #^.* \ @.* \ ..* $ #i " , $value )) {
$returnValue = true ;
} else {
$returnValue = __ ( 'Email address has an invalid format. Please double check the value or select type "other".' );
}
break ;
case 'vulnerability' :
if ( preg_match ( " #^(CVE-)[0-9] { 4}(-)[0-9] { 4,} $ # " , $value )) {
$returnValue = true ;
} else {
$returnValue = __ ( 'Invalid format. Expected: CVE-xxxx-xxxx...' );
}
break ;
2019-08-01 16:42:10 +02:00
case 'weakness' :
if ( preg_match ( " #^(CWE-)[0-9] { 1,} $ # " , $value )) {
$returnValue = true ;
} else {
$returnValue = __ ( 'Invalid format. Expected: CWE-x...' );
}
break ;
2018-07-19 11:48:22 +02:00
case 'named pipe' :
if ( ! preg_match ( " # \n # " , $value )) {
$returnValue = true ;
}
break ;
case 'windows-service-name' :
case 'windows-service-displayname' :
if ( strlen ( $value ) > 256 || preg_match ( '#[\\\/]#' , $value )) {
$returnValue = __ ( 'Invalid format. Only values shorter than 256 characters that don\'t include any forward or backward slashes are allowed.' );
} else {
$returnValue = true ;
}
break ;
case 'mutex' :
2020-11-24 12:19:05 +01:00
case 'process-state' :
2018-07-19 11:48:22 +02:00
case 'snort' :
2018-11-23 14:11:33 +01:00
case 'bro' :
2019-01-30 22:53:38 +01:00
case 'zeek' :
2019-07-13 08:38:43 +02:00
case 'community-id' :
2019-02-01 09:07:32 +01:00
case 'anonymised' :
2018-07-19 11:48:22 +02:00
case 'pattern-in-file' :
case 'pattern-in-traffic' :
case 'pattern-in-memory' :
2020-10-01 15:04:13 +02:00
case 'filename-pattern' :
2020-09-03 17:40:55 +02:00
case 'pgp-public-key' :
case 'pgp-private-key' :
2017-12-21 16:21:23 +01:00
case 'yara' :
case 'stix2-pattern' :
2018-02-02 15:08:42 +01:00
case 'sigma' :
case 'gene' :
2019-12-28 15:21:19 +01:00
case 'kusto-query' :
2018-02-08 11:07:13 +01:00
case 'mime-type' :
2018-02-05 16:49:10 +01:00
case 'identity-card-number' :
2018-07-19 11:48:22 +02:00
case 'cookie' :
case 'attachment' :
case 'malware-sample' :
case 'comment' :
case 'text' :
case 'other' :
2020-10-15 14:39:26 +02:00
case 'cpe' :
2018-07-19 11:48:22 +02:00
case 'email-attachment' :
case 'email-body' :
2020-10-13 09:23:44 +02:00
case 'email-header' :
2020-09-08 15:53:04 +02:00
case 'first-name' :
case 'middle-name' :
case 'last-name' :
2021-02-22 12:36:59 +01:00
case 'full-name' :
2018-07-19 11:48:22 +02:00
$returnValue = true ;
break ;
2020-10-13 18:56:26 +02:00
case 'link' :
// Moved to a native function whilst still enforcing the scheme as a requirement
if ( filter_var ( $value , FILTER_VALIDATE_URL , FILTER_FLAG_SCHEME_REQUIRED ) && ! preg_match ( " # \n # " , $value )) {
2018-07-19 11:48:22 +02:00
$returnValue = true ;
}
break ;
case 'hex' :
2020-10-13 09:23:44 +02:00
return ctype_xdigit ( $value );
2018-07-19 11:48:22 +02:00
case 'target-user' :
case 'campaign-name' :
case 'campaign-id' :
case 'threat-actor' :
case 'target-machine' :
case 'target-org' :
case 'target-location' :
case 'target-external' :
case 'email-subject' :
case 'malware-type' :
// TODO: review url/uri validation
case 'url' :
case 'uri' :
case 'user-agent' :
case 'regkey' :
case 'regkey|value' :
case 'filename' :
case 'pdb' :
case 'windows-scheduled-task' :
2020-10-10 17:01:45 +02:00
case 'whois-registrant-name' :
case 'whois-registrant-org' :
2018-07-19 11:48:22 +02:00
case 'whois-registrar' :
case 'whois-creation-date' :
case 'date-of-birth' :
case 'place-of-birth' :
case 'gender' :
case 'passport-number' :
case 'passport-country' :
case 'passport-expiration' :
case 'redress-number' :
case 'nationality' :
case 'visa-number' :
case 'issue-date-of-the-visa' :
case 'primary-residence' :
case 'country-of-residence' :
case 'special-service-request' :
case 'frequent-flyer-number' :
case 'travel-details' :
case 'payment-details' :
case 'place-port-of-original-embarkation' :
case 'place-port-of-clearance' :
case 'place-port-of-onward-foreign-destination' :
case 'passenger-name-record-locator-number' :
case 'email-dst-display-name' :
case 'email-src-display-name' :
case 'email-reply-to' :
case 'email-x-mailer' :
case 'email-mime-boundary' :
case 'email-thread-index' :
case 'email-message-id' :
case 'github-username' :
case 'github-repository' :
case 'github-organisation' :
case 'twitter-id' :
2021-02-19 18:16:33 +01:00
case 'dkim' :
case 'dkim-signature' :
2020-12-24 11:56:58 +01:00
case 'favicon-mmh3' :
2020-01-21 09:40:25 +01:00
case 'chrome-extension-id' :
2018-07-19 11:48:22 +02:00
case 'mobile-application-id' :
2020-10-13 09:23:44 +02:00
if ( strpos ( $value , " \n " ) !== false ) {
return __ ( 'Value must not contain new line character.' );
2018-07-19 11:48:22 +02:00
}
2020-10-13 09:23:44 +02:00
return true ;
2018-07-19 11:48:22 +02:00
case 'datetime' :
try {
new DateTime ( $value );
$returnValue = true ;
} catch ( Exception $e ) {
$returnValue = __ ( 'Datetime has to be in the ISO 8601 format.' );
}
break ;
case 'size-in-bytes' :
case 'counter' :
2020-10-13 18:13:50 +02:00
if ( $this -> isPositiveInteger ( $value )) {
2020-10-13 09:23:44 +02:00
return true ;
2018-07-19 11:48:22 +02:00
}
2020-10-13 09:23:44 +02:00
return __ ( 'The value has to be a whole number greater or equal 0.' );
2018-07-19 11:48:22 +02:00
case 'targeted-threat-index' :
if ( ! is_numeric ( $value ) || $value < 0 || $value > 10 ) {
$returnValue = __ ( 'The value has to be a number between 0 and 10.' );
} else {
$returnValue = true ;
}
break ;
case 'iban' :
case 'bic' :
case 'btc' :
2019-10-01 19:47:26 +02:00
case 'dash' :
2018-07-19 11:48:22 +02:00
case 'xmr' :
2020-10-13 18:50:22 +02:00
if ( preg_match ( '/^[a-zA-Z0-9]+$/' , $value )) {
$returnValue = true ;
}
break ;
2020-08-17 16:36:47 +02:00
case 'vhash' :
2020-11-25 21:53:56 +01:00
if ( preg_match ( '/^.+$/' , $value )) {
2018-07-19 11:48:22 +02:00
$returnValue = true ;
}
break ;
case 'bin' :
case 'cc-number' :
case 'bank-account-nr' :
case 'aba-rtn' :
case 'prtn' :
case 'phone-number' :
case 'whois-registrant-phone' :
if ( is_numeric ( $value )) {
$returnValue = true ;
}
break ;
case 'cortex' :
json_decode ( $value );
$returnValue = ( json_last_error () == JSON_ERROR_NONE );
break ;
case 'float' :
2020-10-10 16:27:16 +02:00
return is_numeric ( $value );
2018-07-19 11:48:22 +02:00
case 'boolean' :
if ( $value == 1 || $value == 0 ) {
$returnValue = true ;
}
2020-09-08 15:53:04 +02:00
break ;
2020-10-15 22:37:59 +02:00
case 'AS' :
if ( $this -> isPositiveInteger ( $value ) && $value <= 4294967295 ) {
return true ;
}
return __ ( 'AS number have to be integers between 1 and 4294967295' );
2018-07-19 11:48:22 +02:00
}
return $returnValue ;
}
// do some last second modifications before the validation
public function modifyBeforeValidation ( $type , $value )
{
2020-07-30 09:17:27 +02:00
$value = $this -> handle4ByteUnicode ( $value );
2018-07-19 11:48:22 +02:00
switch ( $type ) {
case 'md5' :
case 'sha1' :
case 'sha224' :
case 'sha256' :
case 'sha384' :
case 'sha512' :
case 'sha512/224' :
case 'sha512/256' :
2020-08-23 18:26:56 +02:00
case 'sha3-224' :
case 'sha3-256' :
case 'sha3-384' :
case 'sha3-512' :
2018-12-30 12:26:33 +01:00
case 'ja3-fingerprint-md5' :
2020-11-23 21:03:36 +01:00
case 'jarm-fingerprint' :
2019-01-13 11:47:30 +01:00
case 'hassh-md5' :
case 'hasshserver-md5' :
2018-07-19 11:48:22 +02:00
case 'hostname' :
case 'pehash' :
case 'authentihash' :
2020-08-17 16:36:47 +02:00
case 'vhash' :
2018-07-19 11:48:22 +02:00
case 'imphash' :
2020-10-13 22:29:43 +02:00
case 'telfhash' :
2018-07-19 11:48:22 +02:00
case 'tlsh' :
2019-01-31 23:39:46 +01:00
case 'anonymised' :
2018-12-19 20:19:49 +01:00
case 'cdhash' :
2020-09-03 12:02:59 +02:00
case 'email' :
2018-07-19 11:48:22 +02:00
case 'email-src' :
case 'email-dst' :
case 'target-email' :
case 'whois-registrant-email' :
$value = strtolower ( $value );
break ;
case 'domain' :
$value = strtolower ( $value );
$value = trim ( $value , '.' );
2020-07-06 20:15:56 +02:00
// Domain is not valid, try to convert to punycode
if ( ! $this -> isDomainValid ( $value ) && function_exists ( 'idn_to_ascii' )) {
$punyCode = idn_to_ascii ( $value );
if ( $punyCode !== false ) {
$value = $punyCode ;
}
}
2018-07-19 11:48:22 +02:00
break ;
case 'domain|ip' :
$value = strtolower ( $value );
$parts = explode ( '|' , $value );
2021-09-16 13:23:13 +02:00
if ( ! isset ( $parts [ 1 ])) {
return $value ; // not a composite
}
2018-07-19 11:48:22 +02:00
$parts [ 0 ] = trim ( $parts [ 0 ], '.' );
2020-07-06 20:15:56 +02:00
// Domain is not valid, try to convert to punycode
if ( ! $this -> isDomainValid ( $parts [ 0 ]) && function_exists ( 'idn_to_ascii' )) {
$punyCode = idn_to_ascii ( $parts [ 0 ]);
if ( $punyCode !== false ) {
$parts [ 0 ] = $punyCode ;
}
}
2018-07-19 11:48:22 +02:00
if ( filter_var ( $parts [ 1 ], FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )) {
// convert IPv6 address to compressed format
2021-09-16 13:23:13 +02:00
$parts [ 1 ] = inet_ntop ( inet_pton ( $parts [ 1 ]));
2018-07-19 11:48:22 +02:00
}
2021-09-16 13:23:13 +02:00
return " $parts[0] | $parts[1] " ;
2018-07-19 11:48:22 +02:00
case 'filename|md5' :
case 'filename|sha1' :
case 'filename|imphash' :
case 'filename|sha224' :
case 'filename|sha256' :
case 'filename|sha384' :
case 'filename|sha512' :
case 'filename|sha512/224' :
case 'filename|sha512/256' :
2020-08-23 18:26:56 +02:00
case 'filename|sha3-224' :
case 'filename|sha3-256' :
case 'filename|sha3-384' :
case 'filename|sha3-512' :
2018-07-19 11:48:22 +02:00
case 'filename|authentihash' :
2020-08-17 16:36:47 +02:00
case 'filename|vhash' :
2018-07-19 11:48:22 +02:00
case 'filename|pehash' :
case 'filename|tlsh' :
$pieces = explode ( '|' , $value );
$value = $pieces [ 0 ] . '|' . strtolower ( $pieces [ 1 ]);
break ;
case 'http-method' :
2020-10-10 17:01:45 +02:00
case 'hex' :
2020-10-13 09:23:44 +02:00
return strtoupper ( $value );
2020-10-10 17:01:45 +02:00
case 'vulnerability' :
2020-10-13 09:23:44 +02:00
case 'weakness' :
$value = str_replace ( '– ' , '-' , $value );
2020-10-10 17:01:45 +02:00
return strtoupper ( $value );
2018-07-19 11:48:22 +02:00
case 'cc-number' :
case 'bin' :
$value = preg_replace ( '/[^0-9]+/' , '' , $value );
break ;
case 'iban' :
case 'bic' :
$value = strtoupper ( $value );
$value = preg_replace ( '/[^0-9A-Z]+/' , '' , $value );
break ;
case 'prtn' :
case 'whois-registrant-phone' :
case 'phone-number' :
if ( substr ( $value , 0 , 2 ) == '00' ) {
$value = '+' . substr ( $value , 2 );
}
$value = preg_replace ( '/\(0\)/' , '' , $value );
$value = preg_replace ( '/[^\+0-9]+/' , '' , $value );
break ;
2020-10-10 17:01:45 +02:00
case 'x509-fingerprint-md5' :
case 'x509-fingerprint-sha256' :
2018-07-19 11:48:22 +02:00
case 'x509-fingerprint-sha1' :
$value = str_replace ( ':' , '' , $value );
$value = strtolower ( $value );
break ;
case 'ip-src' :
case 'ip-dst' :
if ( filter_var ( $value , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )) {
// convert IPv6 address to compressed format
$value = inet_ntop ( inet_pton ( $value ));
}
break ;
case 'ip-dst|port' :
case 'ip-src|port' :
2020-03-06 10:54:06 +01:00
if ( substr_count ( $value , ':' ) >= 2 ) { // (ipv6|port) - tokenize ip and port
if ( strpos ( $value , '|' )) { // 2001:db8::1|80
$parts = explode ( '|' , $value );
} elseif ( strpos ( $value , '[' ) === 0 && strpos ( $value , ']' ) !== false ) { // [2001:db8::1]:80
$ipv6 = substr ( $value , 1 , strpos ( $value , ']' ) - 1 );
$port = explode ( ':' , substr ( $value , strpos ( $value , ']' )))[ 1 ];
$parts = array ( $ipv6 , $port );
} elseif ( strpos ( $value , '.' )) { // 2001:db8::1.80
$parts = explode ( '.' , $value );
} elseif ( strpos ( $value , ' port ' )) { // 2001:db8::1 port 80
$parts = explode ( ' port ' , $value );
} elseif ( strpos ( $value , 'p' )) { // 2001:db8::1p80
$parts = explode ( 'p' , $value );
} elseif ( strpos ( $value , '#' )) { // 2001:db8::1#80
$parts = explode ( '#' , $value );
} else { // 2001:db8::1:80 this one is ambiguous
$temp = explode ( ':' , $value );
$parts = array ( implode ( ':' , array_slice ( $temp , 0 , count ( $temp ) - 1 )), end ( $temp ));
}
} elseif ( strpos ( $value , ':' )) { // (ipv4:port)
2018-07-19 11:48:22 +02:00
$parts = explode ( ':' , $value );
2020-03-06 10:54:06 +01:00
} elseif ( strpos ( $value , '|' )) { // (ipv4|port)
2018-07-19 11:48:22 +02:00
$parts = explode ( '|' , $value );
} else {
return $value ;
}
if ( filter_var ( $parts [ 0 ], FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )) {
// convert IPv6 address to compressed format
$parts [ 0 ] = inet_ntop ( inet_pton ( $parts [ 0 ]));
}
return $parts [ 0 ] . '|' . $parts [ 1 ];
case 'mac-address' :
case 'mac-eui-64' :
2020-10-13 18:20:37 +02:00
$value = str_replace ( array ( '.' , ':' , '-' , ' ' ), '' , strtolower ( $value ));
2018-07-19 11:48:22 +02:00
$value = wordwrap ( $value , 2 , ':' , true );
break ;
case 'hostname|port' :
$value = strtolower ( $value );
2020-10-11 11:17:36 +02:00
return str_replace ( ':' , '|' , $value );
2018-07-19 11:48:22 +02:00
case 'boolean' :
if ( 'true' == trim ( strtolower ( $value ))) {
$value = 1 ;
}
if ( 'false' == trim ( strtolower ( $value ))) {
$value = 0 ;
}
$value = ( $value ) ? '1' : '0' ;
break ;
2020-01-28 14:38:58 +01:00
case 'datetime' :
try {
$value = ( new DateTime ( $value )) -> setTimezone ( new DateTimeZone ( 'GMT' )) -> format ( 'Y-m-d\TH:i:s.uO' ); // ISO8601 formating with microseconds
} catch ( Exception $e ) {
// silently skip. Rejection will be done in runValidation()
}
break ;
2020-10-15 22:37:59 +02:00
case 'AS' :
if ( strtoupper ( substr ( $value , 0 , 2 )) === 'AS' ) {
$value = substr ( $value , 2 ); // remove 'AS'
}
if ( strpos ( $value , '.' ) !== false ) { // maybe value is in asdot notation
$parts = explode ( '.' , $value );
if ( $this -> isPositiveInteger ( $parts [ 0 ]) && $this -> isPositiveInteger ( $parts [ 1 ])) {
return $parts [ 0 ] * 65536 + $parts [ 1 ];
}
}
break ;
2018-07-19 11:48:22 +02:00
}
return $value ;
}
public function getCompositeTypes ()
{
2020-06-26 22:40:37 +02:00
static $compositeTypes ;
if ( $compositeTypes === null ) {
// build the list of composite Attribute.type dynamically by checking if type contains a |
// default composite types
$compositeTypes = array ( 'malware-sample' ); // TODO hardcoded composite
// dynamically generated list
foreach ( $this -> typeDefinitions as $type => $foo ) {
if ( strpos ( $type , '|' ) !== false ) {
$compositeTypes [] = $type ;
}
2018-07-19 11:48:22 +02:00
}
}
return $compositeTypes ;
}
2020-06-22 10:52:10 +02:00
public function getRelatedAttributes ( $user , $attribute , $fields = array (), $includeEventData = false )
2018-07-19 11:48:22 +02:00
{
// LATER getRelatedAttributes($attribute) this might become a performance bottleneck
// exclude these specific categories from being linked
switch ( $attribute [ 'category' ]) {
case 'Antivirus detection' :
return null ;
}
// exclude these specific types from being linked
switch ( $attribute [ 'type' ]) {
case 'other' :
case 'comment' :
return null ;
}
// prepare the conditions
$conditions = array (
'Attribute.event_id !=' => $attribute [ 'event_id' ],
2021-03-29 10:37:27 +02:00
'Attribute.deleted !=' => 1 ,
2018-07-19 11:48:22 +02:00
);
// prevent issues with empty fields
if ( empty ( $attribute [ 'value1' ])) {
return null ;
}
if ( empty ( $attribute [ 'value2' ])) {
// no value2, only search for value 1
$conditions [ 'OR' ] = array (
'Attribute.value1' => $attribute [ 'value1' ],
'Attribute.value2' => $attribute [ 'value1' ],
);
} else {
// value2 also set, so search for both
$conditions [ 'AND' ] = array ( // TODO was OR
'Attribute.value1' => array ( $attribute [ 'value1' ], $attribute [ 'value2' ]),
'Attribute.value2' => array ( $attribute [ 'value1' ], $attribute [ 'value2' ]),
);
}
2020-06-22 10:52:10 +02:00
$baseConditions = $this -> buildConditions ( $user );
$baseConditions [ 'AND' ][] = $conditions ;
2018-07-19 11:48:22 +02:00
// do the search
if ( empty ( $fields )) {
$fields = array ( 'Attribute.*' );
}
2019-08-02 13:41:20 +02:00
$params = array (
2020-06-22 10:52:10 +02:00
'conditions' => $baseConditions ,
2019-08-02 13:41:20 +02:00
'fields' => $fields ,
'recursive' => 0 ,
2020-06-22 10:52:10 +02:00
'group' => array ( 'Attribute.id' , 'Attribute.event_id' , 'Attribute.object_id' , 'Attribute.object_relation' , 'Attribute.category' , 'Attribute.type' , 'Attribute.value' , 'Attribute.uuid' , 'Attribute.timestamp' , 'Attribute.distribution' , 'Attribute.sharing_group_id' , 'Attribute.to_ids' , 'Attribute.comment' , 'Event.id' , 'Event.uuid' , 'Event.threat_level_id' , 'Event.analysis' , 'Event.info' , 'Event.extends_uuid' , 'Event.distribution' , 'Event.sharing_group_id' , 'Event.published' , 'Event.date' , 'Event.orgc_id' , 'Event.org_id' , 'Object.id' , 'Object.uuid' , 'Object.distribution' , 'Object.name' , 'Object.template_uuid' , 'Object.distribution' , 'Object.sharing_group_id' ),
2019-08-02 13:41:20 +02:00
'order' => 'Attribute.event_id DESC'
);
if ( ! empty ( $includeEventData )) {
$params [ 'contain' ] = array (
'Event' => array (
'fields' => array (
'Event.id' , 'Event.uuid' , 'Event.threat_level_id' , 'Event.analysis' , 'Event.info' , 'Event.extends_uuid' , 'Event.distribution' , 'Event.sharing_group_id' , 'Event.published' , 'Event.date' , 'Event.orgc_id' , 'Event.org_id'
)
2020-06-22 10:52:10 +02:00
),
'Object' => array (
'fields' => array (
'Object.id' , 'Object.uuid' , 'Object.distribution' , 'Object.name' , 'Object.template_uuid' , 'Object.distribution' , 'Object.sharing_group_id'
)
2019-08-02 13:41:20 +02:00
)
);
}
2018-07-19 11:48:22 +02:00
$similarEvents = $this -> find (
'all' ,
2019-08-02 13:41:20 +02:00
$params
2018-07-19 11:48:22 +02:00
);
2019-08-02 13:41:20 +02:00
if ( ! empty ( $includeEventData )) {
foreach ( $similarEvents as $k => $similarEvent ) {
$similarEvents [ $k ] = array_merge (
$similarEvent [ 'Attribute' ],
array (
'Event' => $similarEvent [ 'Event' ]
)
);
}
}
2018-07-19 11:48:22 +02:00
return $similarEvents ;
}
public function typeIsMalware ( $type )
{
2021-07-20 17:46:20 +02:00
return in_array ( $type , self :: ZIPPED_DEFINITION , true );
2018-07-19 11:48:22 +02:00
}
public function typeIsAttachment ( $type )
{
2021-07-20 17:46:20 +02:00
return in_array ( $type , self :: ZIPPED_DEFINITION , true ) || in_array ( $type , self :: UPLOAD_DEFINITIONS , true );
2018-07-19 11:48:22 +02:00
}
2019-04-09 12:57:21 +02:00
public function getAttachment ( $attribute , $path_suffix = '' )
2018-07-19 11:48:22 +02:00
{
2020-08-13 15:58:42 +02:00
return $this -> loadAttachmentTool () -> getContent ( $attribute [ 'event_id' ], $attribute [ 'id' ], $path_suffix );
}
2018-08-15 15:07:44 +02:00
2020-08-13 15:58:42 +02:00
/**
* @ param array $attribute
* @ param string $path_suffix
* @ return File
* @ throws Exception
*/
public function getAttachmentFile ( array $attribute , $path_suffix = '' )
{
return $this -> loadAttachmentTool () -> getFile ( $attribute [ 'event_id' ], $attribute [ 'id' ], $path_suffix );
2018-07-19 11:48:22 +02:00
}
2021-10-09 18:21:31 +02:00
/**
* @ param array $attribute
* @ param string $path_suffix
* @ return bool
* @ throws Exception
*/
private function saveAttachment ( array $attribute , $path_suffix = '' )
2018-07-19 11:48:22 +02:00
{
2021-10-09 18:21:31 +02:00
if ( $attribute [ 'data' ] === false ) {
$this -> log ( " Invalid attachment data provided for attribute with ID { $attribute [ 'id' ] } . " );
return false ;
}
2020-10-08 19:27:49 +02:00
$result = $this -> loadAttachmentTool () -> save ( $attribute [ 'event_id' ], $attribute [ 'id' ], $attribute [ 'data' ], $path_suffix );
if ( $result ) {
$this -> loadAttachmentScan () -> backgroundScan ( AttachmentScan :: TYPE_ATTRIBUTE , $attribute );
}
return $result ;
2018-07-19 11:48:22 +02:00
}
2020-06-24 21:36:03 +02:00
/**
* Returns attribute attachment content as base64 encoded string . If file doesn ' t exists , empty string is returned .
*
* @ param array $attribute
* @ return string
*/
public function base64EncodeAttachment ( array $attribute )
2019-04-09 12:57:21 +02:00
{
2020-06-24 21:36:03 +02:00
try {
return base64_encode ( $this -> getAttachment ( $attribute ));
} catch ( NotFoundException $e ) {
$this -> log ( $e -> getMessage (), LOG_NOTICE );
return '' ;
}
2019-04-09 12:57:21 +02:00
}
2019-10-12 09:34:49 +02:00
/**
* Currently , as image are considered files with JPG ( JPEG ), PNG or GIF extension .
* @ param array $attribute
* @ return bool
*/
public function isImage ( array $attribute )
{
return $attribute [ 'type' ] === 'attachment' &&
Validation :: extension ( $attribute [ 'value' ], array ( 'jpg' , 'jpeg' , 'png' , 'gif' ));
}
2019-10-09 20:17:06 +02:00
/**
* @ param array $attribute
* @ param bool $thumbnail
* @ param int $maxWidth - When $thumbnail is true
* @ param int $maxHeight - When $thumbnail is true
* @ return string
* @ throws Exception
*/
public function getPictureData ( array $attribute , $thumbnail = false , $maxWidth = 200 , $maxHeight = 200 )
2019-04-09 12:57:21 +02:00
{
2019-10-09 20:17:06 +02:00
if ( $thumbnail && extension_loaded ( 'gd' )) {
if ( $maxWidth == 200 && $maxHeight == 200 ) {
// Return thumbnail directly if already exists
2020-06-28 14:31:23 +02:00
try {
return $this -> getAttachment ( $attribute [ 'Attribute' ], $path_suffix = '_thumbnail' );
} catch ( NotFoundException $e ) {
// pass
2019-04-09 12:57:21 +02:00
}
}
2019-10-09 20:17:06 +02:00
// Thumbnail doesn't exists, we need to generate it
$imageData = $this -> getAttachment ( $attribute [ 'Attribute' ]);
2021-10-10 11:13:00 +02:00
$imageData = $this -> loadAttachmentTool () -> resizeImage ( $imageData , $maxWidth , $maxHeight );
2019-10-09 20:17:06 +02:00
// Save just when requested default thumbnail size
if ( $maxWidth == 200 && $maxHeight == 200 ) {
$attribute [ 'Attribute' ][ 'data' ] = $imageData ;
$this -> saveAttachment ( $attribute [ 'Attribute' ], $path_suffix = '_thumbnail' );
}
} else {
$imageData = $this -> getAttachment ( $attribute [ 'Attribute' ]);
}
return $imageData ;
}
2021-03-10 15:42:06 +01:00
/**
* @ param array $user
* @ param array $resultArray
*/
public function fetchRelated ( array $user , array & $resultArray )
{
if ( empty ( $resultArray )) {
return ;
}
$composeTypes = $this -> getCompositeTypes ();
foreach ( $resultArray as $key => $result ) {
if ( in_array ( $result [ 'default_type' ], $composeTypes , true )) {
$pieces = explode ( '|' , $result [ 'value' ]);
2021-07-21 08:42:05 +02:00
if ( in_array ( $result [ 'default_type' ], self :: PRIMARY_ONLY_CORRELATING_TYPES , true )) {
2021-03-10 15:42:06 +01:00
$or = [ 'Attribute.value1' => $pieces [ 0 ], 'Attribute.value2' => $pieces [ 0 ]];
} else {
$or = [ 'Attribute.value1' => $pieces , 'Attribute.value2' => $pieces ];
}
} else {
$or = [ 'Attribute.value1' => $result [ 'value' ], 'Attribute.value2' => $result [ 'value' ]];
}
$options = array (
'conditions' => [
'OR' => $or ,
'NOT' => [
2021-07-27 15:19:41 +02:00
'Attribute.type' => Attribute :: NON_CORRELATING_TYPES ,
2021-03-10 15:42:06 +01:00
],
'Attribute.disable_correlation' => 0 ,
],
2021-09-08 12:21:36 +02:00
'fields' => [ 'Attribute.uuid' , 'Attribute.type' , 'Attribute.category' , 'Attribute.value' , 'Attribute.comment' ],
2021-03-10 15:42:06 +01:00
'order' => false ,
'limit' => 11 ,
'flatten' => 1 ,
2021-09-08 10:41:27 +02:00
'contain' => [ 'AttributeTag' => false ],
2021-03-10 15:42:06 +01:00
);
$resultArray [ $key ][ 'related' ] = $this -> fetchAttributes ( $user , $options );
}
}
2018-07-19 11:48:22 +02:00
public function checkComposites ()
{
$compositeTypes = $this -> getCompositeTypes ();
$fails = array ();
$attributes = $this -> find ( 'all' , array ( 'recursive' => 0 ));
foreach ( $attributes as $attribute ) {
if (( in_array ( $attribute [ 'Attribute' ][ 'type' ], $compositeTypes )) && ( ! strlen ( $attribute [ 'Attribute' ][ 'value1' ]) || ! strlen ( $attribute [ 'Attribute' ][ 'value2' ]))) {
$fails [] = $attribute [ 'Attribute' ][ 'event_id' ] . ':' . $attribute [ 'Attribute' ][ 'id' ];
}
}
return $fails ;
}
2019-12-16 10:47:07 +01:00
public function ISODatetimeToUTC ( $data , $alias )
{
// convert into utc and micro sec
if ( ! empty ( $data [ $alias ][ 'first_seen' ])) {
$d = new DateTime ( $data [ $alias ][ 'first_seen' ]);
$d -> setTimezone ( new DateTimeZone ( 'GMT' ));
$fs_sec = $d -> format ( 'U' );
$fs_micro = $d -> format ( 'u' );
$fs_micro = str_pad ( $fs_micro , 6 , " 0 " , STR_PAD_LEFT );
$fs = $fs_sec . $fs_micro ;
$data [ $alias ][ 'first_seen' ] = $fs ;
}
if ( ! empty ( $data [ $alias ][ 'last_seen' ])) {
$d = new DateTime ( $data [ $alias ][ 'last_seen' ]);
$d -> setTimezone ( new DateTimeZone ( 'GMT' ));
$ls_sec = $d -> format ( 'U' );
$ls_micro = $d -> format ( 'u' );
$ls_micro = str_pad ( $ls_micro , 6 , " 0 " , STR_PAD_LEFT );
$ls = $ls_sec . $ls_micro ;
$data [ $alias ][ 'last_seen' ] = $ls ;
}
return $data ;
}
public function UTCToISODatetime ( $data , $alias )
{
if ( ! empty ( $data [ $alias ][ 'first_seen' ])) {
$fs = $data [ $alias ][ 'first_seen' ];
$fs_sec = intval ( $fs / 1000000 ); // $fs is in micro (10^6)
$fs_micro = $fs % 1000000 ;
$fs_micro = str_pad ( $fs_micro , 6 , " 0 " , STR_PAD_LEFT );
$fs = $fs_sec . '.' . $fs_micro ;
$data [ $alias ][ 'first_seen' ] = DateTime :: createFromFormat ( 'U.u' , $fs ) -> format ( 'Y-m-d\TH:i:s.uP' );
}
if ( ! empty ( $data [ $alias ][ 'last_seen' ])) {
$ls = $data [ $alias ][ 'last_seen' ];
$ls_sec = intval ( $ls / 1000000 ); // $ls is in micro (10^6)
$ls_micro = $ls % 1000000 ;
$ls_micro = str_pad ( $ls_micro , 6 , " 0 " , STR_PAD_LEFT );
$ls = $ls_sec . '.' . $ls_micro ;
$data [ $alias ][ 'last_seen' ] = DateTime :: createFromFormat ( 'U.u' , $ls ) -> format ( 'Y-m-d\TH:i:s.uP' );
}
return $data ;
}
2018-07-19 11:48:22 +02:00
public function hids ( $user , $type , $tags = '' , $from = false , $to = false , $last = false , $jobId = false , $enforceWarninglist = false )
{
if ( empty ( $user )) {
throw new MethodNotAllowedException ( __ ( 'Could not read user.' ));
}
// check if it's a valid type
if ( $type != 'md5' && $type != 'sha1' && $type != 'sha256' ) {
throw new UnauthorizedException ( __ ( 'Invalid hash type.' ));
}
$conditions = array ();
$typeArray = array ( $type , 'filename|' . $type );
if ( $type == 'md5' ) {
$typeArray [] = 'malware-sample' ;
}
$rules = array ();
2021-04-21 09:09:29 +02:00
$eventIds = $this -> Event -> fetchEventIds ( $user , [
'from' => $from ,
'to' => $to ,
'last' => $last
]);
2018-07-19 11:48:22 +02:00
if ( ! empty ( $tags )) {
$tag = ClassRegistry :: init ( 'Tag' );
$args = $this -> dissectArgs ( $tags );
$tagArray = $tag -> fetchEventTagIds ( $args [ 0 ], $args [ 1 ]);
if ( ! empty ( $tagArray [ 0 ])) {
foreach ( $eventIds as $k => $v ) {
if ( ! in_array ( $v [ 'Event' ][ 'id' ], $tagArray [ 0 ])) {
unset ( $eventIds [ $k ]);
}
}
}
if ( ! empty ( $tagArray [ 1 ])) {
foreach ( $eventIds as $k => $v ) {
if ( in_array ( $v [ 'Event' ][ 'id' ], $tagArray [ 1 ])) {
unset ( $eventIds [ $k ]);
}
}
}
}
App :: uses ( 'HidsExport' , 'Export' );
$continue = false ;
$eventCount = count ( $eventIds );
if ( $jobId ) {
$this -> Job = ClassRegistry :: init ( 'Job' );
$this -> Job -> id = $jobId ;
if ( ! $this -> Job -> exists ()) {
$jobId = false ;
}
}
foreach ( $eventIds as $k => $event ) {
$conditions [ 'AND' ] = array ( 'Attribute.to_ids' => 1 , 'Event.published' => 1 , 'Attribute.type' => $typeArray , 'Attribute.event_id' => $event [ 'Event' ][ 'id' ]);
$options = array (
'conditions' => $conditions ,
'group' => array ( 'Attribute.type' , 'Attribute.value1' ),
'enforceWarninglist' => $enforceWarninglist ,
'flatten' => true
);
$items = $this -> fetchAttributes ( $user , $options );
if ( empty ( $items )) {
continue ;
}
$export = new HidsExport ();
$rules = array_merge ( $rules , $export -> export ( $items , strtoupper ( $type ), $continue ));
$continue = true ;
if ( $jobId && ( $k % 10 == 0 )) {
$this -> Job -> saveField ( 'progress' , $k * 80 / $eventCount );
}
}
return $rules ;
}
public function nids ( $user , $format , $id = false , $continue = false , $tags = false , $from = false , $to = false , $last = false , $type = false , $enforceWarninglist = false , $includeAllTags = false )
{
if ( empty ( $user )) {
throw new MethodNotAllowedException ( __ ( 'Could not read user.' ));
}
2021-04-21 09:09:29 +02:00
$eventIds = $this -> Event -> fetchEventIds ( $user , [
'from' => $from ,
'to' => $to ,
'last' => $last
]);
2018-07-19 11:48:22 +02:00
// If we sent any tags along, load the associated tag names for each attribute
if ( $tags ) {
$tag = ClassRegistry :: init ( 'Tag' );
$args = $this -> dissectArgs ( $tags );
$tagArray = $tag -> fetchEventTagIds ( $args [ 0 ], $args [ 1 ]);
if ( ! empty ( $tagArray [ 0 ])) {
foreach ( $eventIds as $k => $v ) {
if ( ! in_array ( $v [ 'Event' ][ 'id' ], $tagArray [ 0 ])) {
unset ( $eventIds [ $k ]);
}
}
}
if ( ! empty ( $tagArray [ 1 ])) {
foreach ( $eventIds as $k => $v ) {
if ( in_array ( $v [ 'Event' ][ 'id' ], $tagArray [ 1 ])) {
unset ( $eventIds [ $k ]);
}
}
}
}
if ( $id ) {
foreach ( $eventIds as $k => $v ) {
if ( $v [ 'Event' ][ 'id' ] !== $id ) {
unset ( $eventIds [ $k ]);
}
}
}
if ( $format == 'suricata' ) {
App :: uses ( 'NidsSuricataExport' , 'Export' );
} else {
App :: uses ( 'NidsSnortExport' , 'Export' );
}
$rules = array ();
foreach ( $eventIds as $event ) {
$conditions [ 'AND' ] = array ( 'Attribute.to_ids' => 1 , " Event.published " => 1 , 'Attribute.event_id' => $event [ 'Event' ][ 'id' ]);
2020-09-03 12:02:59 +02:00
$valid_types = array ( 'ip-dst' , 'ip-src' , 'ip-dst|port' , 'ip-src|port' , 'eppn' , 'email' , 'email-src' , 'email-dst' , 'email-subject' , 'email-attachment' , 'domain' , 'domain|ip' , 'hostname' , 'url' , 'user-agent' , 'snort' );
2018-07-19 11:48:22 +02:00
$conditions [ 'AND' ][ 'Attribute.type' ] = $valid_types ;
if ( ! empty ( $type )) {
$conditions [ 'AND' ][] = array ( 'Attribute.type' => $type );
}
$params = array (
'conditions' => $conditions , // array of conditions
'recursive' => - 1 , // int
'fields' => array ( 'Attribute.id' , 'Attribute.event_id' , 'Attribute.type' , 'Attribute.value' ),
'contain' => array ( 'Event' => array ( 'fields' => array ( 'Event.id' , 'Event.threat_level_id' ))),
'group' => array ( 'Attribute.type' , 'Attribute.value1' ), // fields to GROUP BY
'enforceWarninglist' => $enforceWarninglist ,
'includeAllTags' => $includeAllTags ,
'flatten' => true
);
$items = $this -> fetchAttributes ( $user , $params );
if ( empty ( $items )) {
continue ;
}
// export depending on the requested type
switch ( $format ) {
case 'suricata' :
$export = new NidsSuricataExport ();
break ;
case 'snort' :
$export = new NidsSnortExport ();
break ;
}
$rules = array_merge ( $rules , $export -> export ( $items , $user [ 'nids_sid' ], $format , $continue ));
// Only prepend the comments once
$continue = true ;
}
return $rules ;
}
2018-08-20 10:50:09 +02:00
public function set_filter_tags ( & $params , $conditions , $options )
{
2018-08-05 19:10:52 +02:00
if ( empty ( $params [ 'tags' ])) {
return $conditions ;
}
$tag = ClassRegistry :: init ( 'Tag' );
2018-08-09 15:11:57 +02:00
$params [ 'tags' ] = $this -> dissectArgs ( $params [ 'tags' ]);
2019-05-23 11:25:29 +02:00
foreach ( array ( 0 , 1 , 2 ) as $tag_operator ) {
$tagArray [ $tag_operator ] = $tag -> fetchTagIdsSimple ( $params [ 'tags' ][ $tag_operator ]);
2018-08-09 15:11:57 +02:00
}
2018-08-05 19:10:52 +02:00
$temp = array ();
if ( ! empty ( $tagArray [ 0 ])) {
2018-11-23 14:11:33 +01:00
if ( $tagArray [ 0 ][ 0 ] === - 1 ) {
$conditions [] = array ( 'Event.id' => - 1 );
} else {
$subquery_options = array (
'conditions' => array (
'tag_id' => $tagArray [ 0 ]
),
'fields' => array (
'event_id'
)
);
$lookup_field = ( $options [ 'scope' ] === 'Event' ) ? 'Event.id' : 'Attribute.event_id' ;
$temp = array_merge (
$temp ,
$this -> subQueryGenerator ( $tag -> EventTag , $subquery_options , $lookup_field )
);
$subquery_options = array (
'conditions' => array (
'tag_id' => $tagArray [ 0 ]
),
'fields' => array (
$options [ 'scope' ] === 'Event' ? 'Event.id' : 'attribute_id'
)
);
$lookup_field = $options [ 'scope' ] === 'Event' ? 'Event.id' : 'Attribute.id' ;
$temp = array_merge (
$temp ,
$this -> subQueryGenerator ( $tag -> AttributeTag , $subquery_options , $lookup_field )
);
}
}
if ( ! empty ( $temp )) {
$conditions [ 'AND' ][] = array ( 'OR' => $temp );
}
$temp = array ();
2018-08-05 19:10:52 +02:00
if ( ! empty ( $tagArray [ 1 ])) {
2018-08-09 15:11:57 +02:00
if ( $options [ 'scope' ] == 'all' || $options [ 'scope' ] == 'Event' ) {
$subquery_options = array (
2018-08-02 15:41:51 +02:00
'conditions' => array (
'tag_id' => $tagArray [ 1 ]
),
'fields' => array (
'event_id'
)
);
2018-08-09 15:11:57 +02:00
$lookup_field = ( $options [ 'scope' ] === 'Event' ) ? 'Event.id' : 'Attribute.event_id' ;
$conditions [ 'AND' ][] = array_merge ( $temp , $this -> subQueryGenerator ( $tag -> EventTag , $subquery_options , $lookup_field , 1 ));
2018-08-05 19:10:52 +02:00
}
2018-08-09 15:11:57 +02:00
if ( $options [ 'scope' ] == 'all' || $options [ 'scope' ] == 'Attribute' ) {
$subquery_options = array (
2018-08-02 15:41:51 +02:00
'conditions' => array (
'tag_id' => $tagArray [ 1 ]
),
'fields' => array (
2018-08-09 15:11:57 +02:00
$options [ 'scope' ] === 'Event' ? 'event.id' : 'attribute_id'
2018-08-02 15:41:51 +02:00
)
);
2018-08-09 15:11:57 +02:00
$lookup_field = $options [ 'scope' ] === 'Event' ? 'Event.id' : 'Attribute.id' ;
$conditions [ 'AND' ][] = array_merge ( $temp , $this -> subQueryGenerator ( $tag -> AttributeTag , $subquery_options , $lookup_field , 1 ));
2018-08-05 19:10:52 +02:00
}
}
2019-05-23 11:25:29 +02:00
$temp = array ();
if ( ! empty ( $tagArray [ 2 ])) {
if ( $tagArray [ 2 ][ 0 ] === - 1 ) {
$conditions [] = array ( 'Event.id' => - 1 );
} else {
foreach ( $tagArray [ 2 ] as $k => $anded_tag ) {
$subquery_options = array (
'conditions' => array (
'tag_id' => $anded_tag
),
'fields' => array (
'event_id'
)
);
$lookup_field = ( $options [ 'scope' ] === 'Event' ) ? 'Event.id' : 'Attribute.event_id' ;
$temp [ $k ][ 'OR' ] = array ();
$temp [ $k ][ 'OR' ] = array_merge (
$temp [ $k ][ 'OR' ],
$this -> subQueryGenerator ( $tag -> EventTag , $subquery_options , $lookup_field )
);
$subquery_options = array (
'conditions' => array (
'tag_id' => $anded_tag
),
'fields' => array (
$options [ 'scope' ] === 'Event' ? 'Event.id' : 'attribute_id'
)
);
$lookup_field = $options [ 'scope' ] === 'Event' ? 'Event.id' : 'Attribute.id' ;
$temp [ $k ][ 'OR' ] = array_merge (
$temp [ $k ][ 'OR' ],
$this -> subQueryGenerator ( $tag -> AttributeTag , $subquery_options , $lookup_field )
);
}
}
}
if ( ! empty ( $temp )) {
$conditions [ 'AND' ][] = array ( 'AND' => $temp );
}
2018-08-09 15:11:57 +02:00
$params [ 'tags' ] = array ();
if ( ! empty ( $tagArray [ 0 ]) && empty ( $options [ 'pop' ])) {
$params [ 'tags' ][ 'OR' ] = $tagArray [ 0 ];
}
2018-08-09 07:46:31 +02:00
if ( ! empty ( $tagArray [ 1 ])) {
2018-08-09 15:11:57 +02:00
$params [ 'tags' ][ 'NOT' ] = $tagArray [ 1 ];
}
2019-05-23 11:25:29 +02:00
if ( ! empty ( $tagArray [ 2 ]) && empty ( $options [ 'pop' ])) {
$params [ 'tags' ][ 'AND' ] = $tagArray [ 2 ];
}
2018-08-09 15:11:57 +02:00
if ( empty ( $params [ 'tags' ])) {
2018-08-09 07:46:31 +02:00
unset ( $params [ 'tags' ]);
}
2018-08-05 19:10:52 +02:00
return $conditions ;
}
2018-07-19 11:48:22 +02:00
public function text ( $user , $type , $tags = false , $eventId = false , $allowNonIDS = false , $from = false , $to = false , $last = false , $enforceWarninglist = false , $allowNotPublished = false )
{
//permissions are taken care of in fetchAttributes()
$conditions [ 'AND' ] = array ();
if ( $allowNonIDS === false ) {
$conditions [ 'AND' ][ 'Attribute.to_ids' ] = 1 ;
if ( $allowNotPublished === false ) {
$conditions [ 'AND' ][ 'Event.published' ] = 1 ;
}
}
if ( ! is_array ( $type ) && $type !== 'all' ) {
$conditions [ 'AND' ][ 'Attribute.type' ] = $type ;
}
if ( $from ) {
$conditions [ 'AND' ][ 'Event.date >=' ] = $from ;
}
if ( $to ) {
$conditions [ 'AND' ][ 'Event.date <=' ] = $to ;
}
if ( $last ) {
$conditions [ 'AND' ][ 'Event.publish_timestamp >=' ] = $last ;
}
if ( $eventId !== false ) {
$conditions [ 'AND' ][] = array ( 'Event.id' => $eventId );
} elseif ( $tags !== false ) {
2018-11-23 14:11:33 +01:00
$passed_param = array ( 'tags' => $tags );
2019-01-25 09:11:01 +01:00
$conditions = $this -> set_filter_tags ( $passed_param , $conditions , array ( 'scope' => 'Attribute' ));
2018-07-19 11:48:22 +02:00
}
$attributes = $this -> fetchAttributes ( $user , array (
'conditions' => $conditions ,
'order' => 'Attribute.value1 ASC' ,
'fields' => array ( 'value' ),
'contain' => array ( 'Event' => array (
'fields' => array ( 'Event.id' , 'Event.published' , 'Event.date' , 'Event.publish_timestamp' ),
)),
'enforceWarninglist' => $enforceWarninglist ,
'flatten' => 1
));
return $attributes ;
}
public function rpz ( $user , $tags = false , $eventId = false , $from = false , $to = false , $enforceWarninglist = false )
{
// we can group hostname and domain as well as ip-src and ip-dst in this case
$conditions [ 'AND' ] = array ( 'Attribute.to_ids' => 1 , 'Event.published' => 1 );
$typesToFetch = array ( 'ip' => array ( 'ip-src' , 'ip-dst' ), 'domain' => array ( 'domain' ), 'hostname' => array ( 'hostname' ));
if ( $from ) {
$conditions [ 'AND' ][ 'Event.date >=' ] = $from ;
}
if ( $to ) {
$conditions [ 'AND' ][ 'Event.date <=' ] = $to ;
}
if ( $eventId !== false ) {
$conditions [ 'AND' ][] = array ( 'Event.id' => $eventId );
}
if ( $tags !== false ) {
// If we sent any tags along, load the associated tag names for each attribute
$tag = ClassRegistry :: init ( 'Tag' );
$args = $this -> dissectArgs ( $tags );
$tagArray = $tag -> fetchEventTagIds ( $args [ 0 ], $args [ 1 ]);
$temp = array ();
foreach ( $tagArray [ 0 ] as $accepted ) {
$temp [ 'OR' ][] = array ( 'Event.id' => $accepted );
}
$conditions [ 'AND' ][] = $temp ;
$temp = array ();
foreach ( $tagArray [ 1 ] as $rejected ) {
$temp [ 'AND' ][] = array ( 'Event.id !=' => $rejected );
}
$conditions [ 'AND' ][] = $temp ;
}
$values = array ();
foreach ( $typesToFetch as $k => $v ) {
$tempConditions = $conditions ;
$tempConditions [ 'type' ] = $v ;
$temp = $this -> fetchAttributes (
$user ,
array (
'conditions' => $tempConditions ,
'fields' => array ( 'Attribute.value' ), // array of field names
2018-09-08 23:46:37 +02:00
'enforceWarninglist' => $enforceWarninglist ,
2018-11-23 14:11:33 +01:00
'flatten' => 1
2018-07-19 11:48:22 +02:00
)
);
if ( empty ( $temp )) {
continue ;
}
if ( $k == 'hostname' ) {
foreach ( $temp as $value ) {
$found = false ;
if ( isset ( $values [ 'domain' ])) {
foreach ( $values [ 'domain' ] as $domain ) {
if ( strpos ( $value [ 'Attribute' ][ 'value' ], $domain ) != 0 ) {
$found = true ;
}
}
}
if ( ! $found ) {
$values [ $k ][] = $value [ 'Attribute' ][ 'value' ];
}
}
} else {
foreach ( $temp as $value ) {
$values [ $k ][] = $value [ 'Attribute' ][ 'value' ];
}
}
unset ( $temp );
}
return $values ;
}
2020-01-31 11:45:23 +01:00
public function bro ( $user , $type , $tags = false , $eventId = false , $from = false , $to = false , $last = false , $enforceWarninglist = false , $skipHeader = false )
2018-07-19 11:48:22 +02:00
{
App :: uses ( 'BroExport' , 'Export' );
$export = new BroExport ();
if ( $type == 'all' ) {
$types = array_keys ( $export -> mispTypes );
} else {
$types = array ( $type );
}
$intel = array ();
foreach ( $types as $type ) {
//restricting to non-private or same org if the user is not a site-admin.
$conditions [ 'AND' ] = array ( 'Attribute.to_ids' => 1 , 'Event.published' => 1 );
if ( $from ) {
$conditions [ 'AND' ][ 'Event.date >=' ] = $from ;
}
if ( $to ) {
$conditions [ 'AND' ][ 'Event.date <=' ] = $to ;
}
if ( $last ) {
$conditions [ 'AND' ][ 'Event.publish_timestamp >=' ] = $last ;
}
if ( $eventId !== false ) {
$temp = array ();
$args = $this -> dissectArgs ( $eventId );
foreach ( $args [ 0 ] as $accepted ) {
$temp [ 'OR' ][] = array ( 'Event.id' => $accepted );
}
$conditions [ 'AND' ][] = $temp ;
$temp = array ();
foreach ( $args [ 1 ] as $rejected ) {
$temp [ 'AND' ][] = array ( 'Event.id !=' => $rejected );
}
$conditions [ 'AND' ][] = $temp ;
}
if ( $tags !== false ) {
// If we sent any tags along, load the associated tag names for each attribute
$tag = ClassRegistry :: init ( 'Tag' );
$args = $this -> dissectArgs ( $tags );
$tagArray = $tag -> fetchEventTagIds ( $args [ 0 ], $args [ 1 ]);
$temp = array ();
foreach ( $tagArray [ 0 ] as $accepted ) {
$temp [ 'OR' ][] = array ( 'Event.id' => $accepted );
}
$conditions [ 'AND' ][] = $temp ;
$temp = array ();
foreach ( $tagArray [ 1 ] as $rejected ) {
$temp [ 'AND' ][] = array ( 'Event.id !=' => $rejected );
}
$conditions [ 'AND' ][] = $temp ;
}
2020-09-01 15:59:38 +02:00
$this -> Allowedlist = ClassRegistry :: init ( 'Allowedlist' );
$this -> allowedlist = $this -> Allowedlist -> getBlockedValues ();
2018-07-19 11:48:22 +02:00
$instanceString = 'MISP' ;
if ( Configure :: read ( 'MISP.host_org_id' ) && Configure :: read ( 'MISP.host_org_id' ) > 0 ) {
$this -> Event -> Orgc -> id = Configure :: read ( 'MISP.host_org_id' );
if ( $this -> Event -> Orgc -> exists ()) {
$instanceString = $this -> Event -> Orgc -> field ( 'name' ) . ' MISP' ;
}
}
$mispTypes = $export -> getMispTypes ( $type );
foreach ( $mispTypes as $mispType ) {
$conditions [ 'AND' ][ 'Attribute.type' ] = $mispType [ 0 ];
2020-09-01 15:59:38 +02:00
$intel = array_merge ( $intel , $this -> __bro ( $user , $conditions , $mispType [ 1 ], $export , $this -> allowedlist , $instanceString , $enforceWarninglist ));
2018-07-19 11:48:22 +02:00
}
}
natsort ( $intel );
$intel = array_unique ( $intel );
2020-01-31 11:45:23 +01:00
if ( empty ( $skipHeader )) {
array_unshift ( $intel , $export -> header );
}
2018-07-19 11:48:22 +02:00
return $intel ;
}
2020-09-01 15:59:38 +02:00
private function __bro ( $user , $conditions , $valueField , $export , $allowedlist , $instanceString , $enforceWarninglist )
2018-07-19 11:48:22 +02:00
{
$attributes = $this -> fetchAttributes (
$user ,
array (
'conditions' => $conditions , // array of conditions
'order' => 'Attribute.value' . $valueField . ' ASC' ,
'recursive' => - 1 , // int
2019-03-19 14:46:16 +01:00
'fields' => array ( 'Attribute.id' , 'Attribute.event_id' , 'Attribute.type' , 'Attribute.category' , 'Attribute.comment' , 'Attribute.to_ids' , 'Attribute.value' , 'Attribute.value' . $valueField ),
2018-07-19 11:48:22 +02:00
'contain' => array ( 'Event' => array ( 'fields' => array ( 'Event.id' , 'Event.threat_level_id' , 'Event.orgc_id' , 'Event.uuid' ))),
2020-01-31 11:45:23 +01:00
'enforceWarninglist' => $enforceWarninglist ,
'flatten' => 1
2018-07-19 11:48:22 +02:00
)
);
$orgs = $this -> Event -> Orgc -> find ( 'list' , array (
'fields' => array ( 'Orgc.id' , 'Orgc.name' )
));
2020-09-01 15:59:38 +02:00
return $export -> export ( $attributes , $orgs , $valueField , $allowedlist , $instanceString );
2018-07-19 11:48:22 +02:00
}
public function generateCorrelation ( $jobId = false , $startPercentage = 0 , $eventId = false , $attributeId = false )
{
$this -> purgeCorrelations ( $eventId );
2020-05-21 17:54:40 +02:00
$this -> FuzzyCorrelateSsdeep = ClassRegistry :: init ( 'FuzzyCorrelateSsdeep' );
$this -> FuzzyCorrelateSsdeep -> purge ( $eventId , $attributeId );
2018-07-19 11:48:22 +02:00
// get all attributes..
if ( ! $eventId ) {
2020-12-23 12:50:19 +01:00
$eventIds = $this -> Event -> find ( 'column' , [
2020-08-16 21:53:26 +02:00
'fields' => [ 'Event.id' ],
'conditions' => [ 'Event.disable_correlation' => 0 ],
]);
2020-07-17 09:19:58 +02:00
$full = true ;
2018-07-19 11:48:22 +02:00
} else {
$eventIds = array ( $eventId );
2020-07-17 09:19:58 +02:00
$full = false ;
2018-07-19 11:48:22 +02:00
}
$attributeCount = 0 ;
if ( Configure :: read ( 'MISP.background_jobs' ) && $jobId ) {
$this -> Job = ClassRegistry :: init ( 'Job' );
$eventCount = count ( $eventIds );
2020-08-16 21:53:26 +02:00
} else {
$jobId = false ;
2018-07-19 11:48:22 +02:00
}
2020-12-23 12:50:19 +01:00
foreach ( $eventIds as $j => $id ) {
2020-08-16 21:53:26 +02:00
if ( $jobId ) {
2018-07-19 11:48:22 +02:00
if ( $attributeId ) {
2020-08-16 21:53:26 +02:00
$message = 'Correlating Attribute ' . $attributeId ;
2018-07-19 11:48:22 +02:00
} else {
2020-08-16 21:53:26 +02:00
$message = 'Correlating Event ' . $id ;
2018-07-19 11:48:22 +02:00
}
2020-08-16 21:53:26 +02:00
$this -> Job -> saveProgress ( $jobId , $message , $startPercentage + ( $j / $eventCount * ( 100 - $startPercentage )));
2018-07-19 11:48:22 +02:00
}
$event = $this -> Event -> find ( 'first' , array (
2020-12-23 12:50:19 +01:00
'recursive' => - 1 ,
'fields' => array ( 'Event.distribution' , 'Event.id' , 'Event.info' , 'Event.org_id' , 'Event.date' , 'Event.sharing_group_id' , 'Event.disable_correlation' ),
'conditions' => array ( 'id' => $id ),
'order' => false ,
2018-07-19 11:48:22 +02:00
));
2020-05-17 13:40:01 +02:00
$attributeConditions = array (
'Attribute.event_id' => $id ,
'Attribute.deleted' => 0 ,
'Attribute.disable_correlation' => 0 ,
'NOT' => array (
2021-07-27 15:19:41 +02:00
'Attribute.type' => Attribute :: NON_CORRELATING_TYPES ,
2020-05-17 13:40:01 +02:00
),
);
2018-07-19 11:48:22 +02:00
if ( $attributeId ) {
$attributeConditions [ 'Attribute.id' ] = $attributeId ;
}
2020-08-16 21:53:26 +02:00
$attributes = $this -> find ( 'all' , [
'recursive' => - 1 ,
'conditions' => $attributeConditions ,
// fetch just necessary fields to save memory
'fields' => [
'Attribute.id' ,
'Attribute.event_id' ,
'Attribute.type' ,
'Attribute.value1' ,
'Attribute.value2' ,
'Attribute.distribution' ,
'Attribute.sharing_group_id' ,
'Attribute.disable_correlation' ,
],
'order' => [],
]);
foreach ( $attributes as $attribute ) {
2021-04-25 17:36:29 +02:00
$this -> Correlation -> afterSaveCorrelation ( $attribute [ 'Attribute' ], $full , $event );
2018-07-19 11:48:22 +02:00
$attributeCount ++ ;
}
}
2020-08-16 21:53:26 +02:00
if ( $jobId ) {
$this -> Job -> saveStatus ( $jobId , true );
2018-07-19 11:48:22 +02:00
}
return $attributeCount ;
}
2021-10-06 19:02:27 +02:00
public function purgeCorrelations ( $eventId = false )
2018-07-19 11:48:22 +02:00
{
if ( ! $eventId ) {
$this -> query ( 'TRUNCATE TABLE correlations;' );
} else {
2021-10-06 19:02:27 +02:00
$this -> Correlation -> deleteAll ([
'OR' => array (
'Correlation.1_event_id' => $eventId ,
'Correlation.event_id' => $eventId ,
)
], false );
2018-07-19 11:48:22 +02:00
}
}
public function reportValidationIssuesAttributes ( $eventId )
{
$conditions = array ();
if ( $eventId && is_numeric ( $eventId )) {
$conditions = array ( 'event_id' => $eventId );
}
// get all attributes..
$attributes = $this -> find ( 'all' , array ( 'recursive' => - 1 , 'fields' => array ( 'id' ), 'conditions' => $conditions ));
// for all attributes..
$result = array ();
$i = 0 ;
foreach ( $attributes as $a ) {
$attribute = $this -> find ( 'first' , array ( 'recursive' => - 1 , 'conditions' => array ( 'id' => $a [ 'Attribute' ][ 'id' ])));
$this -> set ( $attribute );
if ( ! $this -> validates ()) {
$errors = $this -> validationErrors ;
$result [ $i ][ 'id' ] = $attribute [ 'Attribute' ][ 'id' ];
$result [ $i ][ 'error' ] = array ();
foreach ( $errors as $field => $error ) {
$result [ $i ][ 'error' ][ $field ] = array ( 'value' => $attribute [ 'Attribute' ][ $field ], 'error' => $error [ 0 ]);
}
$result [ $i ][ 'details' ] = 'Event ID: [' . $attribute [ 'Attribute' ][ 'event_id' ] . " ] - Category: [ " . $attribute [ 'Attribute' ][ 'category' ] . " ] - Type: [ " . $attribute [ 'Attribute' ][ 'type' ] . " ] - Value: [ " . $attribute [ 'Attribute' ][ 'value' ] . ']' ;
$i ++ ;
}
}
return $result ;
}
// This method takes a string from an argument with several elements (separated by '&&' and negated by '!') and returns 2 arrays
// array 1 will have all of the non negated terms and array 2 all the negated terms
public function dissectArgs ( $args )
{
2018-11-23 14:11:33 +01:00
if ( empty ( $args )) {
2019-05-23 11:25:29 +02:00
return array ( 0 => array (), 1 => array (), 2 => array ());
2018-11-23 14:11:33 +01:00
}
2018-08-09 07:46:31 +02:00
if ( ! is_array ( $args )) {
$args = explode ( '&&' , $args );
2018-07-19 11:48:22 +02:00
}
2019-05-23 11:25:29 +02:00
$result = array ( 0 => array (), 1 => array (), 2 => array ());
2018-08-09 07:46:31 +02:00
if ( isset ( $args [ 'OR' ]) || isset ( $args [ 'NOT' ]) || isset ( $args [ 'AND' ])) {
if ( ! empty ( $args [ 'OR' ])) {
$result [ 0 ] = $args [ 'OR' ];
}
if ( ! empty ( $args [ 'NOT' ])) {
$result [ 1 ] = $args [ 'NOT' ];
}
2019-05-23 11:25:29 +02:00
if ( ! empty ( $args [ 'AND' ])) {
$result [ 2 ] = $args [ 'AND' ];
}
2018-07-19 11:48:22 +02:00
} else {
2018-08-09 07:46:31 +02:00
foreach ( $args as $arg ) {
if ( substr ( $arg , 0 , 1 ) == '!' ) {
$result [ 1 ][] = substr ( $arg , 1 );
} else {
$result [ 0 ][] = $arg ;
}
2018-07-19 11:48:22 +02:00
}
}
return $result ;
}
public function checkForValidationIssues ( $attribute )
{
$this -> set ( $attribute );
if ( $this -> validates ()) {
return false ;
} else {
return $this -> validationErrors ;
}
}
public function checkTemplateAttributes ( $template , $data , $event_id )
{
$result = array ();
$errors = array ();
$attributes = array ();
if ( isset ( $data [ 'Template' ][ 'fileArray' ])) {
$fileArray = json_decode ( $data [ 'Template' ][ 'fileArray' ], true );
}
foreach ( $template [ 'TemplateElement' ] as $element ) {
if ( $element [ 'element_definition' ] == 'attribute' ) {
$result = $this -> __resolveElementAttribute ( $element [ 'TemplateElementAttribute' ][ 0 ], $data [ 'Template' ][ 'value_' . $element [ 'id' ]]);
} elseif ( $element [ 'element_definition' ] == 'file' ) {
$temp = array ();
if ( isset ( $fileArray )) {
foreach ( $fileArray as $fileArrayElement ) {
if ( $fileArrayElement [ 'element_id' ] == $element [ 'id' ]) {
$temp [] = $fileArrayElement ;
}
}
}
$result = $this -> __resolveElementFile ( $element [ 'TemplateElementFile' ][ 0 ], $temp );
if ( $element [ 'TemplateElementFile' ][ 0 ][ 'mandatory' ] && empty ( $temp ) && empty ( $errors [ $element [ 'id' ]])) {
$errors [ $element [ 'id' ]] = 'This field is mandatory.' ;
}
}
if ( $element [ 'element_definition' ] == 'file' || $element [ 'element_definition' ] == 'attribute' ) {
if ( $result [ 'errors' ]) {
$errors [ $element [ 'id' ]] = $result [ 'errors' ];
} else {
foreach ( $result [ 'attributes' ] as & $a ) {
$a [ 'event_id' ] = $event_id ;
$a [ 'distribution' ] = 5 ;
$test = $this -> checkForValidationIssues ( array ( 'Attribute' => $a ));
if ( $test ) {
foreach ( $test [ 'value' ] as $e ) {
$errors [ $element [ 'id' ]] = $e ;
}
} else {
$attributes [] = $a ;
}
}
}
}
}
return array ( 'attributes' => $attributes , 'errors' => $errors );
}
private function __resolveElementAttribute ( $element , $value )
{
$attributes = array ();
$results = array ();
$errors = null ;
if ( ! empty ( $value )) {
if ( $element [ 'batch' ]) {
$values = explode ( " \n " , $value );
foreach ( $values as $v ) {
$v = trim ( $v );
$attributes [] = $this -> __createAttribute ( $element , $v );
}
} else {
$attributes [] = $this -> __createAttribute ( $element , trim ( $value ));
}
foreach ( $attributes as $att ) {
if ( isset ( $att [ 'multi' ])) {
foreach ( $att [ 'multi' ] as $a ) {
$results [] = $a ;
}
} else {
$results [] = $att ;
}
}
} else {
if ( $element [ 'mandatory' ]) {
$errors = __ ( 'This field is mandatory.' );
}
}
return array ( 'attributes' => $results , 'errors' => $errors );
}
private function __resolveElementFile ( $element , $files )
{
$attributes = array ();
$errors = null ;
$element [ 'complex' ] = 0 ;
if ( $element [ 'malware' ]) {
$element [ 'type' ] = 'malware-sample' ;
$element [ 'to_ids' ] = 1 ;
} else {
$element [ 'type' ] = 'attachment' ;
$element [ 'to_ids' ] = 0 ;
}
foreach ( $files as $file ) {
if ( ! $this -> checkFilename ( $file [ 'filename' ])) {
$errors = 'Filename not allowed.' ;
continue ;
}
if ( $element [ 'malware' ]) {
$malwareName = $file [ 'filename' ] . '|' . hash_file ( 'md5' , APP . 'tmp/files/' . $file [ 'tmp_name' ]);
$tmp_file = new File ( APP . 'tmp/files/' . $file [ 'tmp_name' ]);
if ( ! $tmp_file -> readable ()) {
$errors = 'File cannot be read.' ;
} else {
$element [ 'type' ] = 'malware-sample' ;
$attributes [] = $this -> __createAttribute ( $element , $malwareName );
$attributes [ count ( $attributes ) - 1 ][ 'data' ] = $file [ 'tmp_name' ];
$element [ 'type' ] = 'filename|sha256' ;
$sha256 = $file [ 'filename' ] . '|' . ( hash_file ( 'sha256' , APP . 'tmp/files/' . $file [ 'tmp_name' ]));
$attributes [] = $this -> __createAttribute ( $element , $sha256 );
$element [ 'type' ] = 'filename|sha1' ;
$sha1 = $file [ 'filename' ] . '|' . ( hash_file ( 'sha1' , APP . 'tmp/files/' . $file [ 'tmp_name' ]));
$attributes [] = $this -> __createAttribute ( $element , $sha1 );
}
} else {
$attributes [] = $this -> __createAttribute ( $element , $file [ 'filename' ]);
$tmp_file = new File ( APP . 'tmp/files/' . $file [ 'tmp_name' ]);
if ( ! $tmp_file -> readable ()) {
$errors = 'File cannot be read.' ;
} else {
$attributes [ count ( $attributes ) - 1 ][ 'data' ] = $file [ 'tmp_name' ];
}
}
}
return array ( 'attributes' => $attributes , 'errors' => $errors , 'files' => $files );
}
private function __createAttribute ( $element , $value )
{
$attribute = array (
'comment' => $element [ 'name' ],
'to_ids' => $element [ 'to_ids' ],
'category' => $element [ 'category' ],
'value' => $value ,
);
if ( $element [ 'complex' ]) {
$complexTypeTool = new ComplexTypeTool ();
$result = $complexTypeTool -> checkComplexRouter ( $value , ucfirst ( $element [ 'type' ]));
if ( isset ( $result [ 'multi' ])) {
$temp = $attribute ;
$attribute = array ();
foreach ( $result [ 'multi' ] as $k => $r ) {
$attribute [ 'multi' ][] = $temp ;
$attribute [ 'multi' ][ $k ][ 'type' ] = $r [ 'type' ];
$attribute [ 'multi' ][ $k ][ 'value' ] = $r [ 'value' ];
}
} elseif ( $result != false ) {
$attribute [ 'type' ] = $result [ 'type' ];
$attribute [ 'value' ] = $result [ 'value' ];
} else {
return false ;
}
} else {
$attribute [ 'type' ] = $element [ 'type' ];
}
return $attribute ;
}
public function buildConditions ( $user )
{
$conditions = array ();
if ( ! $user [ 'Role' ][ 'perm_site_admin' ]) {
2018-08-03 11:34:55 +02:00
$sgids = $this -> Event -> cacheSgids ( $user , true );
$eventConditions = $this -> Event -> createEventConditions ( $user );
2018-07-19 11:48:22 +02:00
$conditions = array (
'AND' => array (
2018-08-03 11:34:55 +02:00
$eventConditions [ 'AND' ],
2018-07-19 11:48:22 +02:00
array (
'OR' => array (
'Event.org_id' => $user [ 'org_id' ],
'Attribute.distribution' => array ( '1' , '2' , '3' , '5' ),
'AND ' => array (
'Attribute.distribution' => 4 ,
'Attribute.sharing_group_id' => $sgids ,
)
)
),
array (
'OR' => array (
'Attribute.object_id' => 0 ,
'Event.org_id' => $user [ 'org_id' ],
'Object.distribution' => array ( '1' , '2' , '3' , '5' ),
'AND' => array (
'Object.distribution' => 4 ,
'Object.sharing_group_id' => $sgids ,
)
)
)
)
);
}
return $conditions ;
}
/*
* Unlike the other fetchers , this one foregoes any ACL checks .
* the objective is simple : Fetch the given attribute with all related objects needed for the ZMQ output ,
* standardising on this function for fetching the attribute to be passed to Attribute -> save ()
*/
public function fetchAttribute ( $id )
{
$attribute = $this -> find ( 'first' , array (
'recursive' => - 1 ,
'conditions' => array ( 'Attribute.id' => $id ),
'contain' => array (
'Event' => array (
'Orgc' => array (
'fields' => array ( 'Orgc.id' , 'Orgc.uuid' , 'Orgc.name' )
),
'fields' => array ( 'Event.id' , 'Event.date' , 'Event.info' , 'Event.uuid' , 'Event.published' , 'Event.analysis' , 'Event.threat_level_id' , 'Event.org_id' , 'Event.orgc_id' , 'Event.distribution' , 'Event.sharing_group_id' )
),
'AttributeTag' => array (
'Tag' => array ( 'fields' => array ( 'Tag.id' , 'Tag.name' , 'Tag.colour' , 'Tag.exportable' ))
),
'Object'
)
));
if ( ! empty ( $attribute )) {
if ( ! empty ( $attribute [ 'AttributeTag' ])) {
foreach ( $attribute [ 'AttributeTag' ] as $at ) {
2021-02-08 16:59:57 +01:00
$attribute [ 'Attribute' ][ 'Tag' ][] = $at [ 'Tag' ];
2018-07-19 11:48:22 +02:00
}
}
unset ( $attribute [ 'AttributeTag' ]);
}
return $attribute ;
}
2021-10-06 18:26:59 +02:00
/**
* @ param array $user
* @ param array $options
* @ return array
*/
public function fetchAttributeSimple ( array $user , array $options = [])
{
$query = [
'recursive' => - 1 ,
'conditions' => $this -> buildConditions ( $user ),
'contain' => [ 'Event' , 'Object' ], // by default include Event and Object, because it is required for conditions
];
if ( isset ( $options [ 'conditions' ])) {
$query [ 'conditions' ][ 'AND' ][] = $options [ 'conditions' ];
}
if ( isset ( $options [ 'fields' ])) {
$query [ 'fields' ] = $options [ 'fields' ];
}
if ( isset ( $options [ 'contain' ])) {
$query [ 'contain' ] = $options [ 'contain' ];
}
return $this -> find ( 'first' , $query );
}
2020-11-02 17:42:48 +01:00
/**
* Fetches attributes that $user can see .
*
* @ param array $user
* @ param array $options
* @ return array
*/
public function fetchAttributesSimple ( array $user , array $options = array ())
2018-07-19 11:48:22 +02:00
{
$params = array (
'conditions' => $this -> buildConditions ( $user ),
'fields' => array (),
2019-07-17 16:13:31 +02:00
'recursive' => - 1 ,
2020-11-02 17:42:48 +01:00
'contain' => [ 'Event' , 'Object' ], // by default include Event and Object, because it is required for conditions
2018-07-19 11:48:22 +02:00
);
if ( isset ( $options [ 'conditions' ])) {
$params [ 'conditions' ][ 'AND' ][] = $options [ 'conditions' ];
}
if ( isset ( $options [ 'fields' ])) {
$params [ 'fields' ] = $options [ 'fields' ];
}
2019-07-17 16:13:31 +02:00
if ( isset ( $options [ 'contain' ])) {
$params [ 'contain' ] = $options [ 'contain' ];
}
2020-11-02 17:42:48 +01:00
return $this -> find ( 'all' , array (
2018-07-19 11:48:22 +02:00
'conditions' => $params [ 'conditions' ],
'recursive' => - 1 ,
'fields' => $params [ 'fields' ],
2019-07-17 16:13:31 +02:00
'contain' => $params [ 'contain' ],
2020-12-31 22:48:27 +01:00
'order' => false ,
2018-07-19 11:48:22 +02:00
));
}
// Method that fetches all attributes for the various exports
// very flexible, it's basically a replacement for find, with the addition that it restricts access based on user
// options:
// fields
// contain
// conditions
// order
// group
2018-08-05 19:10:52 +02:00
public function fetchAttributes ( $user , $options = array (), & $continue = true )
2018-07-19 11:48:22 +02:00
{
$params = array (
'conditions' => $this -> buildConditions ( $user ),
'recursive' => - 1 ,
'contain' => array (
'Event' => array (
'fields' => array ( 'id' , 'info' , 'org_id' , 'orgc_id' , 'uuid' ),
),
2021-01-02 15:23:59 +01:00
'AttributeTag' , // tags are fetched separately, @see Attribute::__attachTagsToAttributes
2018-07-19 11:48:22 +02:00
'Object' => array (
'fields' => array ( 'id' , 'distribution' , 'sharing_group_id' )
)
)
);
2019-01-25 08:35:50 +01:00
2018-11-26 16:07:39 +01:00
if ( ! empty ( $options [ 'includeProposals' ])) {
2018-11-23 14:11:33 +01:00
$this -> bindModel (
array ( 'hasMany' => array (
'ShadowAttribute' => array (
'className' => 'ShadowAttribute' ,
'foreignKey' => 'old_id' ,
'conditions' => array ( 'ShadowAttribute.deleted' => 0 )
)
)
)
);
$params [ 'contain' ][ 'ShadowAttribute' ] = array ( 'fields' => array (
" id " ,
" old_id " ,
" event_id " ,
" type " ,
" category " ,
" value1 " ,
" to_ids " ,
" uuid " ,
" value2 " ,
" org_id " ,
" event_org_id " ,
" comment " ,
" event_uuid " ,
" deleted " ,
" timestamp " ,
" proposal_to_delete " ,
" disable_correlation " ,
" value "
));
}
2019-07-30 14:47:02 +02:00
if ( ! empty ( $options [ 'includeContext' ])) {
2020-11-08 16:13:17 +01:00
// include just event id for conditions, rest event data will be fetched later
$params [ 'contain' ][ 'Event' ][ 'fields' ] = [ 'id' ];
2019-07-30 14:47:02 +02:00
}
2018-07-19 11:48:22 +02:00
if ( isset ( $options [ 'contain' ])) {
2019-08-26 17:59:12 +02:00
// We may use a string instead of an array to ask for everything
// instead of some specific attributes. If so, remove the array from
// params, as we will later add the string.
2021-09-08 10:41:27 +02:00
foreach ( $options [ 'contain' ] as $key => $contain ) {
if ( $contain === false ) {
unset ( $params [ 'contain' ][ $key ]);
unset ( $options [ 'contain' ][ $key ]);
if (( $key = array_search ( $key , $params [ 'contain' ])) !== false ) {
unset ( $params [ 'contain' ][ $key ]);
}
} else if ( is_string ( $contain )) {
2019-08-26 17:59:12 +02:00
unset ( $params [ 'contain' ][ $contain ]);
}
}
2018-07-19 11:48:22 +02:00
$params [ 'contain' ] = array_merge_recursive ( $params [ 'contain' ], $options [ 'contain' ]);
}
if ( isset ( $options [ 'page' ])) {
$params [ 'page' ] = $options [ 'page' ];
}
if ( isset ( $options [ 'limit' ])) {
$params [ 'limit' ] = $options [ 'limit' ];
}
2019-09-29 18:33:32 +02:00
if (
2020-09-02 16:49:45 +02:00
! empty ( $options [ 'allow_proposal_blocking' ]) &&
Configure :: read ( 'MISP.proposals_block_attributes' )
2019-09-29 18:33:32 +02:00
) {
2018-07-19 11:48:22 +02:00
$this -> bindModel ( array ( 'hasMany' => array ( 'ShadowAttribute' => array ( 'foreignKey' => 'old_id' ))));
$proposalRestriction = array (
'ShadowAttribute' => array (
'conditions' => array (
'AND' => array (
'ShadowAttribute.deleted' => 0 ,
'OR' => array (
'ShadowAttribute.proposal_to_delete' => 1 ,
'ShadowAttribute.to_ids' => 0
)
)
),
2019-03-19 14:16:35 +01:00
'fields' => array ( 'ShadowAttribute.id' , 'ShadowAttribute.value' , 'ShadowAttribute.type' , 'ShadowAttribute.category' , 'ShadowAttribute.to_ids' )
2018-07-19 11:48:22 +02:00
)
);
$params [ 'contain' ] = array_merge ( $params [ 'contain' ], $proposalRestriction );
}
if ( isset ( $options [ 'fields' ])) {
$params [ 'fields' ] = $options [ 'fields' ];
}
if ( isset ( $options [ 'conditions' ])) {
$params [ 'conditions' ][ 'AND' ][] = $options [ 'conditions' ];
}
if ( empty ( $options [ 'flatten' ])) {
2021-02-08 23:15:34 +01:00
$params [ 'conditions' ][ 'AND' ][] = array ( 'Attribute.object_id' => 0 );
2018-07-19 11:48:22 +02:00
}
if ( isset ( $options [ 'order' ])) {
$params [ 'order' ] = $options [ 'order' ];
}
if ( ! isset ( $options [ 'withAttachments' ])) {
$options [ 'withAttachments' ] = false ;
} else ( $params [ 'order' ] = array ());
if ( ! isset ( $options [ 'enforceWarninglist' ])) {
$options [ 'enforceWarninglist' ] = false ;
}
2019-05-09 17:25:39 +02:00
if ( ! isset ( $options [ 'includeWarninglistHits' ])) {
$options [ 'includeWarninglistHits' ] = false ;
}
2019-07-25 15:45:33 +02:00
if ( ! isset ( $options [ 'includeDecayScore' ])) {
$options [ 'includeDecayScore' ] = false ;
}
if ( ! isset ( $options [ 'decayingModel' ])) {
$options [ 'decayingModel' ] = false ;
}
2019-08-21 12:06:49 +02:00
if ( ! isset ( $options [ 'modelOverrides' ])) {
$options [ 'modelOverrides' ] = array ();
}
2019-09-06 14:55:12 +02:00
if ( isset ( $options [ 'score' ])) {
$options [ 'modelOverrides' ][ 'threshold' ] = $options [ 'score' ];
}
2019-07-25 16:21:12 +02:00
if ( ! isset ( $options [ 'excludeDecayed' ])) {
$options [ 'excludeDecayed' ] = 0 ;
2019-07-25 16:13:42 +02:00
} else {
$options [ 'includeDecayScore' ] = true ;
}
2021-03-30 23:22:46 +02:00
//Add EventTags to attributes to take them into account when calculating decay score
2020-04-27 10:45:19 +02:00
if ( $options [ 'includeDecayScore' ]) {
$options [ 'includeEventTags' ] = true ;
}
2018-07-19 11:48:22 +02:00
if ( ! $user [ 'Role' ][ 'perm_sync' ] || ! isset ( $options [ 'deleted' ]) || ! $options [ 'deleted' ]) {
2021-02-08 23:15:34 +01:00
$params [ 'conditions' ][ 'AND' ][ 'Attribute.deleted' ] = 0 ;
2019-05-22 16:08:27 +02:00
} else {
if ( $options [ 'deleted' ] === " only " ) {
$options [ 'deleted' ] = 1 ;
}
$params [ 'conditions' ][ 'AND' ][ '(Attribute.deleted + 0)' ] = $options [ 'deleted' ];
2018-07-19 11:48:22 +02:00
}
if ( isset ( $options [ 'group' ])) {
2020-11-17 12:08:46 +01:00
$params [ 'group' ] = ! empty ( $options [ 'group' ]) ? $options [ 'group' ] : false ;
2018-07-19 11:48:22 +02:00
}
2020-07-21 09:28:38 +02:00
// Site admin can access even unpublished event attributes if `unpublishedprivate` option is enabled
if ( ! $user [ 'Role' ][ 'perm_site_admin' ] && Configure :: read ( 'MISP.unpublishedprivate' )) {
2019-01-08 12:47:45 +01:00
$params [ 'conditions' ][ 'AND' ][] = array ( 'OR' => array ( 'Event.published' => 1 , 'Event.orgc_id' => $user [ 'org_id' ], 'Event.org_id' => $user [ 'org_id' ]));
2018-07-19 11:48:22 +02:00
}
if ( ! empty ( $options [ 'list' ])) {
if ( ! empty ( $options [ 'event_ids' ])) {
2020-12-23 12:50:19 +01:00
return $this -> find ( 'column' , [
'conditions' => $params [ 'conditions' ],
'contain' => array ( 'Event' , 'Object' ),
'fields' => [ 'Attribute.event_id' ],
'unique' => true ,
2020-12-31 22:48:27 +01:00
'order' => false ,
2020-12-23 12:50:19 +01:00
]);
2018-07-19 11:48:22 +02:00
} else {
2020-12-23 12:50:19 +01:00
return $this -> find ( 'list' , array (
'conditions' => $params [ 'conditions' ],
'recursive' => - 1 ,
'contain' => array ( 'Event' , 'Object' ),
'fields' => array ( 'Attribute.event_id' ),
2020-12-31 22:48:27 +01:00
'order' => false
2020-12-23 12:50:19 +01:00
));
2018-07-19 11:48:22 +02:00
}
}
2020-10-03 15:06:32 +02:00
if (( $options [ 'enforceWarninglist' ] || $options [ 'includeWarninglistHits' ]) && ! isset ( $this -> Warninglist )) {
2018-07-19 11:48:22 +02:00
$this -> Warninglist = ClassRegistry :: init ( 'Warninglist' );
}
if ( empty ( $params [ 'limit' ])) {
2018-08-06 20:53:30 +02:00
$loopLimit = 50000 ;
2018-07-19 11:48:22 +02:00
$loop = true ;
2018-08-02 15:41:51 +02:00
$params [ 'limit' ] = $loopLimit ;
$params [ 'page' ] = 0 ;
2018-07-19 11:48:22 +02:00
} else {
$loop = false ;
}
$attributes = array ();
2018-11-23 14:11:33 +01:00
if ( ! empty ( $options [ 'includeEventTags' ])) {
$eventTags = array ();
}
2018-08-02 15:41:51 +02:00
while ( $continue ) {
2018-07-19 11:48:22 +02:00
if ( $loop ) {
2018-08-02 15:41:51 +02:00
$params [ 'page' ] = $params [ 'page' ] + 1 ;
if ( isset ( $results ) && count ( $results ) < $loopLimit ) {
$continue = false ;
continue ;
}
2018-07-19 11:48:22 +02:00
}
$results = $this -> find ( 'all' , $params );
2020-11-08 16:13:17 +01:00
if ( ! empty ( $options [ 'includeContext' ]) && ! empty ( $results )) {
$eventIds = [];
foreach ( $results as $result ) {
$eventIds [ $result [ 'Attribute' ][ 'event_id' ]] = true ; // deduplicate
}
$eventsById = $this -> __fetchEventsForAttributeContext ( $user , array_keys ( $eventIds ), ! empty ( $options [ 'includeAllTags' ]));
foreach ( $results as & $result ) {
$result [ 'Event' ] = $eventsById [ $result [ 'Attribute' ][ 'event_id' ]];
}
unset ( $eventsById , $result ); // unset result is important, because it is reference
}
2021-01-02 15:23:59 +01:00
$this -> __attachTagsToAttributes ( $results , $options );
2018-11-23 14:11:33 +01:00
foreach ( $results as $k => $result ) {
2019-08-02 13:41:20 +02:00
if ( ! empty ( $options [ 'includeSightings' ])) {
$temp = $result [ 'Attribute' ];
$temp [ 'Event' ] = $result [ 'Event' ];
$results [ $k ][ 'Attribute' ][ 'Sighting' ] = $this -> Sighting -> attachToEvent ( $temp , $user , $temp [ 'id' ]);
}
if ( ! empty ( $options [ 'includeCorrelations' ])) {
$attributeFields = array ( 'id' , 'event_id' , 'object_id' , 'object_relation' , 'category' , 'type' , 'value' , 'uuid' , 'timestamp' , 'distribution' , 'sharing_group_id' , 'to_ids' , 'comment' );
2020-06-22 10:52:10 +02:00
$results [ $k ][ 'Attribute' ][ 'RelatedAttribute' ] = ( $this -> getRelatedAttributes ( $user , $results [ $k ][ 'Attribute' ], $attributeFields , true ));
2019-08-02 13:41:20 +02:00
}
2018-11-23 14:11:33 +01:00
}
2018-08-14 23:38:01 +02:00
if ( ! $loop ) {
2018-08-20 10:50:09 +02:00
if ( ! empty ( $params [ 'limit' ]) && count ( $results ) < $params [ 'limit' ]) {
$continue = false ;
}
$break = true ;
}
2018-07-19 11:48:22 +02:00
// return false if we're paginating
if ( isset ( $options [ 'limit' ]) && empty ( $results )) {
2018-08-05 19:10:52 +02:00
return array ();
2018-07-19 11:48:22 +02:00
}
$results = array_values ( $results );
$proposals_block_attributes = Configure :: read ( 'MISP.proposals_block_attributes' );
foreach ( $results as $key => $attribute ) {
2020-09-07 16:37:38 +02:00
if ( $options [ 'enforceWarninglist' ] && ! $this -> Warninglist -> filterWarninglistAttribute ( $attribute [ 'Attribute' ])) {
2020-10-03 15:06:32 +02:00
unset ( $results [ $key ]); // Remove attribute that match any enabled warninglists
2018-07-19 11:48:22 +02:00
continue ;
}
2018-11-23 14:11:33 +01:00
if ( ! empty ( $options [ 'includeEventTags' ])) {
$results = $this -> __attachEventTagsToAttributes ( $eventTags , $results , $key , $options );
}
2019-05-09 17:25:39 +02:00
if ( $options [ 'includeWarninglistHits' ]) {
2020-09-04 17:11:45 +02:00
$results [ $key ][ 'Attribute' ] = $this -> Warninglist -> checkForWarning ( $results [ $key ][ 'Attribute' ]);
2019-05-09 17:25:39 +02:00
}
2018-07-19 11:48:22 +02:00
if ( ! empty ( $options [ 'includeAttributeUuid' ]) || ! empty ( $options [ 'includeEventUuid' ])) {
$results [ $key ][ 'Attribute' ][ 'event_uuid' ] = $results [ $key ][ 'Event' ][ 'uuid' ];
}
if ( $proposals_block_attributes ) {
2019-03-19 14:16:35 +01:00
$this -> __blockAttributeViaProposal ( $results , $key );
2018-07-19 11:48:22 +02:00
}
if ( $options [ 'withAttachments' ]) {
if ( $this -> typeIsAttachment ( $attribute [ 'Attribute' ][ 'type' ])) {
$encodedFile = $this -> base64EncodeAttachment ( $attribute [ 'Attribute' ]);
$results [ $key ][ 'Attribute' ][ 'data' ] = $encodedFile ;
}
}
2019-07-25 15:45:33 +02:00
if ( $options [ 'includeDecayScore' ]) {
$this -> DecayingModel = ClassRegistry :: init ( 'DecayingModel' );
2019-09-06 11:32:54 +02:00
$include_full_model = isset ( $options [ 'includeFullModel' ]) && $options [ 'includeFullModel' ] ? 1 : 0 ;
2020-04-27 10:45:19 +02:00
if ( empty ( $results [ $key ][ 'Attribute' ][ 'AttributeTag' ])) {
2020-05-29 15:20:07 +02:00
$results [ $key ][ 'Attribute' ][ 'AttributeTag' ] = isset ( $results [ $key ][ 'AttributeTag' ]) ? $results [ $key ][ 'AttributeTag' ] : array ();
$results [ $key ][ 'Attribute' ][ 'EventTag' ] = isset ( $results [ $key ][ 'EventTag' ]) ? $results [ $key ][ 'EventTag' ] : array ();
2020-04-27 10:45:19 +02:00
}
2019-09-12 11:17:33 +02:00
$results [ $key ][ 'Attribute' ] = $this -> DecayingModel -> attachScoresToAttribute ( $user , $results [ $key ][ 'Attribute' ], $options [ 'decayingModel' ], $options [ 'modelOverrides' ], $include_full_model );
2020-04-27 10:45:19 +02:00
unset ( $results [ $key ][ 'Attribute' ][ 'AttributeTag' ]);
unset ( $results [ $key ][ 'Attribute' ][ 'EventTag' ]);
2020-02-21 14:47:52 +01:00
if ( $options [ 'excludeDecayed' ] && ! empty ( $results [ $key ][ 'Attribute' ][ 'decay_score' ])) { // filter out decayed attribute
2019-07-25 16:13:42 +02:00
$decayed_flag = true ;
2019-08-21 15:53:13 +02:00
foreach ( $results [ $key ][ 'Attribute' ][ 'decay_score' ] as $decayResult ) { // remove attribute if ALL score results in a decay
2019-07-25 16:13:42 +02:00
$decayed_flag = $decayed_flag && $decayResult [ 'decayed' ];
}
if ( $decayed_flag ) {
unset ( $results [ $key ]);
}
}
2019-07-25 15:45:33 +02:00
}
2019-03-19 14:16:35 +01:00
if ( ! empty ( $results [ $key ])) {
2019-05-10 14:25:38 +02:00
if ( ! empty ( $options [ 'includeGalaxy' ])) {
2020-03-12 10:26:09 +01:00
$massaged_attribute = $this -> Event -> massageTags ( $user , $results [ $key ], 'Attribute' );
$massaged_event = $this -> Event -> massageTags ( $user , $results [ $key ], 'Event' );
2019-06-11 14:13:17 +02:00
$massaged_attribute [ 'Galaxy' ] = array_merge_recursive ( $massaged_attribute [ 'Galaxy' ], $massaged_event [ 'Galaxy' ]);
$results [ $key ] = $massaged_attribute ;
2019-05-10 14:25:38 +02:00
}
2019-03-19 14:16:35 +01:00
$attributes [] = $results [ $key ];
}
2018-07-19 11:48:22 +02:00
}
2018-08-20 07:45:38 +02:00
if ( ! empty ( $break )) {
2018-08-14 23:38:01 +02:00
break ;
}
2018-07-19 11:48:22 +02:00
}
return $attributes ;
}
2020-11-08 16:13:17 +01:00
/**
* @ param array $user
* @ param array $eventIds
* @ param bool $includeAllTags
* @ return array
* @ throws Exception
*/
private function __fetchEventsForAttributeContext ( array $user , array $eventIds , $includeAllTags )
{
if ( empty ( $eventIds )) {
return [];
}
$events = $this -> Event -> fetchEvent ( $user , [
'eventid' => $eventIds ,
'metadata' => true ,
'sgReferenceOnly' => true ,
'includeEventCorrelations' => false ,
'includeAllTags' => $includeAllTags ,
]);
$eventFields = [ 'id' , 'orgc_id' , 'org_id' , 'date' , 'threat_level_id' , 'info' , 'published' , 'uuid' , 'analysis' , 'timestamp' , 'distribution' , 'publish_timestamp' , 'sharing_group_id' , 'extends_uuid' ];
$tagFields = [ 'id' , 'name' , 'colour' , 'numerical_value' ];
$eventsById = [];
// Reformat to required format
foreach ( $events as $event ) {
$newEvent = [];
foreach ( $eventFields as $eventField ) {
$newEvent [ $eventField ] = $event [ 'Event' ][ $eventField ];
}
$tags = [];
foreach ( $event [ 'EventTag' ] as $et ) {
$tag = [ 'local' => $et [ 'local' ]];
foreach ( $tagFields as $tagField ) {
$tag [ $tagField ] = $et [ 'Tag' ][ $tagField ];
}
$tags [] = $tag ;
}
$newEvent [ 'Tag' ] = $tags ;
$newEvent [ 'Orgc' ] = $event [ 'Orgc' ];
$eventsById [ $newEvent [ 'id' ]] = $newEvent ;
}
return $eventsById ;
}
2021-01-02 15:23:59 +01:00
private function __attachTagsToAttributes ( array & $attributes , array $options )
{
$tagIdsToFetch = [];
foreach ( $attributes as $attribute ) {
2021-09-08 10:41:27 +02:00
if ( ! empty ( $attribute [ 'AttributeTag' ])) {
foreach ( $attribute [ 'AttributeTag' ] as $at ) {
$tagIdsToFetch [ $at [ 'tag_id' ]] = true ;
}
2021-01-02 15:23:59 +01:00
}
}
if ( empty ( $tagIdsToFetch )) {
return ;
}
$conditions = [ 'Tag.id' => array_keys ( $tagIdsToFetch )];
unset ( $tagIdsToFetch );
if ( empty ( $options [ 'includeAllTags' ])) {
$conditions [ 'Tag.exportable' ] = 1 ;
}
$tagsToModify = $this -> AttributeTag -> Tag -> find ( 'all' , [
'conditions' => $conditions ,
'fields' => [ 'id' , 'name' , 'colour' , 'numerical_value' ],
'recursive' => - 1 ,
]);
$tags = [];
foreach ( $tagsToModify as $tag ) {
$tags [ $tag [ 'Tag' ][ 'id' ]] = $tag [ 'Tag' ];
}
foreach ( $attributes as $k => $attribute ) {
$tagCulled = false ;
foreach ( $attribute [ 'AttributeTag' ] as $k2 => $at ) {
if ( ! isset ( $tags [ $at [ 'tag_id' ]])) {
unset ( $attributes [ $k ][ 'AttributeTag' ][ $k2 ]);
$tagCulled = true ;
} else {
$tag = $tags [ $at [ 'tag_id' ]];
$tag [ 'local' ] = $at [ 'local' ];
$attributes [ $k ][ 'AttributeTag' ][ $k2 ][ 'Tag' ] = $tag ;
}
}
if ( $tagCulled ) {
$attributes [ $k ][ 'AttributeTag' ] = array_values ( $attributes [ $k ][ 'AttributeTag' ]);
}
}
}
2018-11-23 14:11:33 +01:00
private function __attachEventTagsToAttributes ( $eventTags , & $results , $key , $options )
{
if ( ! isset ( $eventTags [ $results [ $key ][ 'Event' ][ 'id' ]])) {
$tagConditions = array ( 'EventTag.event_id' => $results [ $key ][ 'Event' ][ 'id' ]);
if ( empty ( $options [ 'includeAllTags' ])) {
$tagConditions [ 'Tag.exportable' ] = 1 ;
}
$temp = $this -> Event -> EventTag -> find ( 'all' , array (
'recursive' => - 1 ,
'contain' => array ( 'Tag' ),
'conditions' => $tagConditions
));
foreach ( $temp as $tag ) {
$tag [ 'EventTag' ][ 'Tag' ] = $tag [ 'Tag' ];
unset ( $tag [ 'Tag' ]);
$eventTags [ $results [ $key ][ 'Event' ][ 'id' ]][] = $tag ;
}
}
2019-05-10 14:25:38 +02:00
if ( ! empty ( $eventTags )) {
foreach ( $eventTags [ $results [ $key ][ 'Event' ][ 'id' ]] as $eventTag ) {
$results [ $key ][ 'EventTag' ][] = $eventTag [ 'EventTag' ];
}
2018-11-23 14:11:33 +01:00
}
return $results ;
}
private function __blockAttributeViaProposal ( & $attributes , $k )
{
if ( ! empty ( $attributes [ $k ][ 'ShadowAttribute' ])) {
foreach ( $attributes [ $k ][ 'ShadowAttribute' ] as $sa ) {
if ( $sa [ 'value' ] === $attributes [ $k ][ 'Attribute' ][ 'value' ] &&
$sa [ 'type' ] === $attributes [ $k ][ 'Attribute' ][ 'type' ] &&
$sa [ 'category' ] === $attributes [ $k ][ 'Attribute' ][ 'category' ] &&
2019-03-19 14:16:35 +01:00
( $sa [ 'to_ids' ] == 0 || $sa [ 'to_ids' ] == '' ) &&
$attributes [ $k ][ 'Attribute' ][ 'to_ids' ] == 1
2018-11-23 14:11:33 +01:00
) {
unset ( $attributes [ $k ]);
}
}
} else {
unset ( $attributes [ $k ][ 'ShadowAttribute' ]);
}
}
2018-09-28 09:21:29 +02:00
2018-07-19 11:48:22 +02:00
// Method gets and converts the contents of a file passed along as a base64 encoded string with the original filename into a zip archive
// The zip archive is then passed back as a base64 encoded string along with the md5 hash and a flag whether the transaction was successful
// The archive is password protected using the "infected" password
// The contents of the archive will be the actual sample, named <md5> and the original filename in a text file named <md5>.filename.txt
public function handleMaliciousBase64 ( $event_id , $original_filename , $base64 , $hash_types , $proposal = false )
{
if ( ! is_numeric ( $event_id )) {
throw new Exception ( __ ( 'Something went wrong. Received a non-numeric event ID while trying to create a zip archive of an uploaded malware sample.' ));
}
2018-08-15 15:07:44 +02:00
2019-10-05 15:40:34 +02:00
$content = base64_decode ( $base64 );
2018-08-15 15:07:44 +02:00
2020-08-13 15:58:42 +02:00
$attachmentTool = $this -> loadAttachmentTool ();
2020-05-05 15:23:26 +02:00
$hashes = $attachmentTool -> computeHashes ( $content , $hash_types );
2019-10-05 15:40:34 +02:00
try {
2020-05-05 15:23:26 +02:00
$encrypted = $attachmentTool -> encrypt ( $original_filename , $content , $hashes [ 'md5' ]);
2019-10-05 15:40:34 +02:00
} catch ( Exception $e ) {
$this -> logException ( " Could not create encrypted malware sample. " , $e );
return array ( 'success' => false );
2018-07-19 11:48:22 +02:00
}
2019-10-05 15:40:34 +02:00
$result = array_merge ( array ( 'data' => base64_encode ( $encrypted ), 'success' => true ), $hashes );
2018-07-19 11:48:22 +02:00
return $result ;
}
2021-10-09 17:28:44 +02:00
/**
* @ param string $originalFilename
* @ param string $content
* @ param array $hashTypes
* @ return array
*/
private function handleMaliciousRaw ( $originalFilename , $content , array $hashTypes )
{
$attachmentTool = $this -> loadAttachmentTool ();
$hashes = $attachmentTool -> computeHashes ( $content , $hashTypes );
try {
$encrypted = $attachmentTool -> encrypt ( $originalFilename , $content , $hashes [ 'md5' ]);
} catch ( Exception $e ) {
$this -> logException ( " Could not create encrypted malware sample. " , $e );
return [ 'success' => false ];
}
$hashes [ 'success' ] = true ;
$hashes [ 'data_raw' ] = $encrypted ;
return $hashes ;
}
2019-10-06 13:05:29 +02:00
/**
* @ return bool Return true if at least one advanced extraction tool is available
*/
public function isAdvancedExtractionAvailable ()
2018-07-19 11:48:22 +02:00
{
2019-10-06 13:05:29 +02:00
try {
2020-08-13 15:58:42 +02:00
$types = $this -> loadAttachmentTool () -> checkAdvancedExtractionStatus ( $this -> getPythonVersion ());
2019-10-06 13:05:29 +02:00
} catch ( Exception $e ) {
2018-07-19 11:48:22 +02:00
return false ;
}
2019-10-06 13:05:29 +02:00
foreach ( $types as $type => $missing ) {
if ( $missing === false ) {
return true ;
}
2018-07-19 11:48:22 +02:00
}
2019-10-06 13:05:29 +02:00
2018-07-19 11:48:22 +02:00
return false ;
}
public function resolveHashType ( $hash )
{
$hashTypes = $this -> hashTypes ;
$validTypes = array ();
$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 ;
}
}
return $validTypes ;
}
2020-10-10 16:42:23 +02:00
/**
2020-10-18 12:53:41 +02:00
* @ param array $attribute
2020-10-10 16:42:23 +02:00
* @ param bool $context
* @ return array | true
*/
public function validateAttribute ( array $attribute , $context = true )
2018-07-19 11:48:22 +02:00
{
$this -> set ( $attribute );
if ( ! $context ) {
unset ( $this -> validate [ 'event_id' ]);
unset ( $this -> validate [ 'value' ][ 'uniqueValue' ]);
2020-10-18 12:53:41 +02:00
unset ( $this -> validate [ 'uuid' ][ 'unique' ]);
2018-07-19 11:48:22 +02:00
}
if ( $this -> validates ()) {
return true ;
} else {
return $this -> validationErrors ;
}
}
public function restore ( $id , $user )
{
$this -> id = $id ;
if ( ! $this -> exists ()) {
return 'Attribute doesn\'t exist, or you lack the permission to edit it.' ;
}
$attribute = $this -> find ( 'first' , array ( 'conditions' => array ( 'Attribute.id' => $id ), 'recursive' => - 1 , 'contain' => array ( 'Event' )));
if ( ! $user [ 'Role' ][ 'perm_site_admin' ]) {
if ( ! ( $attribute [ 'Event' ][ 'orgc_id' ] == $user [ 'org_id' ] && (( $user [ 'Role' ][ 'perm_modify' ] && $attribute [ 'Event' ][ 'user_id' ] != $user [ 'id' ]) || $user [ 'Role' ][ 'perm_modify_org' ]))) {
return 'Attribute doesn\'t exist, or you lack the permission to edit it.' ;
}
}
unset ( $attribute [ 'Attribute' ][ 'timestamp' ]);
$attribute [ 'Attribute' ][ 'deleted' ] = 0 ;
$date = new DateTime ();
$attribute [ 'Attribute' ][ 'timestamp' ] = $date -> getTimestamp ();
if ( $this -> save ( $attribute [ 'Attribute' ])) {
$attribute [ 'Event' ][ 'published' ] = 0 ;
$attribute [ 'Event' ][ 'timestamp' ] = $date -> getTimestamp ();
$this -> Event -> save ( $attribute [ 'Event' ]);
2021-04-06 14:33:43 +02:00
$this -> __alterAttributeCount ( $attribute [ 'Event' ][ 'id' ]);
2018-07-19 11:48:22 +02:00
return true ;
} else {
return 'Could not save changes.' ;
}
}
2020-07-24 14:30:41 +02:00
public function saveAttributes ( $attributes , $user )
2018-07-19 11:48:22 +02:00
{
2021-10-09 23:32:51 +02:00
$defaultDistribution = $this -> defaultDistribution ();
2019-10-02 14:30:34 +02:00
$saveResult = true ;
2021-10-09 18:45:51 +02:00
foreach ( $attributes as $attribute ) {
2018-07-19 11:48:22 +02:00
if ( ! empty ( $attribute [ 'encrypt' ]) && $attribute [ 'encrypt' ]) {
2019-03-24 22:30:41 +01:00
$attribute = $this -> onDemandEncrypt ( $attribute );
2018-07-19 11:48:22 +02:00
}
if ( ! isset ( $attribute [ 'distribution' ])) {
$attribute [ 'distribution' ] = $defaultDistribution ;
}
unset ( $attribute [ 'Attachment' ]);
$this -> create ();
2020-07-24 13:55:34 +02:00
$currentSave = $this -> save ( $attribute );
$saveResult = $saveResult && $currentSave ;
if ( $currentSave ) {
$attribute [ 'id' ] = $this -> id ;
2020-07-24 14:30:41 +02:00
$this -> AttributeTag -> handleAttributeTags ( $user , $attribute , $attribute [ 'event_id' ], $capture = true );
2020-07-24 13:55:34 +02:00
}
2018-07-19 11:48:22 +02:00
}
2019-10-02 14:30:34 +02:00
return $saveResult ;
2018-07-19 11:48:22 +02:00
}
2021-10-09 18:45:51 +02:00
/**
* @ param array $attribute
* @ return array
*/
public function onDemandEncrypt ( array $attribute )
2019-03-24 22:30:41 +01:00
{
if ( strpos ( $attribute [ 'value' ], '|' ) !== false ) {
$temp = explode ( '|' , $attribute [ 'value' ]);
$attribute [ 'value' ] = $temp [ 0 ];
}
2021-10-09 18:45:51 +02:00
$content = base64_decode ( $attribute [ 'data' ]);
if ( $content === false ) {
$this -> log ( " Invalid attachment data provided for attribute with ID { $attribute [ 'id' ] } . " );
return $attribute ;
}
$result = $this -> handleMaliciousRaw ( $attribute [ 'value' ], $content , array ( 'md5' ));
$attribute [ 'data_raw' ] = $result [ 'data_raw' ];
unset ( $attribute [ 'data' ]);
2019-03-24 22:30:41 +01:00
$attribute [ 'value' ] = $attribute [ 'value' ] . '|' . $result [ 'md5' ];
return $attribute ;
}
2018-07-19 11:48:22 +02:00
private function __createTagSubQuery ( $tag_id , $blocked = false , $scope = 'Event' , $limitAttributeHitsTo = 'event' )
{
$conditionKey = $blocked ? array ( 'NOT' => array ( 'EventTag.tag_id' => $tag_id )) : array ( 'EventTag.tag_id' => $tag_id );
$db = $this -> getDataSource ();
$subQuery = $db -> buildStatement (
array (
'fields' => array ( $scope . 'Tag.' . $limitAttributeHitsTo . '_id' ),
'table' => strtolower ( $scope ) . '_tags' ,
'alias' => $scope . 'Tag' ,
'limit' => null ,
'offset' => null ,
'joins' => array (),
'conditions' => array (
$scope . 'Tag.tag_id' => $tag_id
),
'group' => array ( $scope . 'Tag.' . $limitAttributeHitsTo . '_id' )
),
$this
);
$subQuery = ucfirst ( $limitAttributeHitsTo ) . '.id IN (' . $subQuery . ') ' ;
$conditions = array (
$db -> expression ( $subQuery ) -> value
);
return $conditions ;
}
public function setTagConditions ( $tags , $conditions , $limitAttributeHitsTo = 'event' )
{
$args = $this -> dissectArgs ( $tags );
$tagArray = $this -> AttributeTag -> Tag -> fetchTagIdsFromFilter ( $args [ 0 ], $args [ 1 ]);
$temp = array ();
if ( ! empty ( $tagArray [ 0 ])) {
$temp [ 'OR' ][] = $this -> __createTagSubQuery ( $tagArray [ 0 ]);
$temp [ 'OR' ][] = $this -> __createTagSubQuery ( $tagArray [ 0 ], false , 'Attribute' , $limitAttributeHitsTo );
}
if ( ! empty ( $tagArray [ 1 ])) {
$temp [ 'AND' ][ 'NOT' ] = $this -> __createTagSubQuery ( $tagArray [ 1 ], true );
if ( $limitAttributeHitsTo == 'attribute' ) {
$temp [ 'AND' ][ 'NOT' ] = $this -> __createTagSubQuery ( $tagArray [ 1 ], true , 'Attribute' , $limitAttributeHitsTo );
}
}
$conditions [ 'AND' ][] = $temp ;
return $conditions ;
}
2019-06-07 09:49:52 +02:00
public function setTimestampConditions ( $timestamp , $conditions , $scope = 'Event.timestamp' , $returnRaw = false )
2018-07-19 11:48:22 +02:00
{
if ( is_array ( $timestamp )) {
2019-01-17 10:12:47 +01:00
$timestamp [ 0 ] = intval ( $this -> Event -> resolveTimeDelta ( $timestamp [ 0 ]));
$timestamp [ 1 ] = intval ( $this -> Event -> resolveTimeDelta ( $timestamp [ 1 ]));
if ( $timestamp [ 0 ] > $timestamp [ 1 ]) {
$temp = $timestamp [ 0 ];
$timestamp [ 0 ] = $timestamp [ 1 ];
$timestamp [ 1 ] = $temp ;
}
$conditions [ 'AND' ][] = array ( $scope . ' >=' => $timestamp [ 0 ]);
$conditions [ 'AND' ][] = array ( $scope . ' <=' => $timestamp [ 1 ]);
2018-07-19 11:48:22 +02:00
} else {
2019-01-17 10:12:47 +01:00
$timestamp = intval ( $this -> Event -> resolveTimeDelta ( $timestamp ));
$conditions [ 'AND' ][] = array ( $scope . ' >=' => $timestamp );
2018-07-19 11:48:22 +02:00
}
2019-06-07 09:49:52 +02:00
if ( $returnRaw ) {
return $timestamp ;
}
2018-07-19 11:48:22 +02:00
return $conditions ;
}
2019-07-04 15:08:05 +02:00
public function setTimestampSeenConditions ( $timestamp , $conditions , $scope = 'Attribute.first_seen' , $returnRaw = false )
2019-07-04 13:51:36 +02:00
{
if ( is_array ( $timestamp )) {
$timestamp [ 0 ] = intval ( $this -> Event -> resolveTimeDelta ( $timestamp [ 0 ])) * 1000000 ; // seen in stored in micro-seconds in the DB
$timestamp [ 1 ] = intval ( $this -> Event -> resolveTimeDelta ( $timestamp [ 1 ])) * 1000000 ; // seen in stored in micro-seconds in the DB
if ( $timestamp [ 0 ] > $timestamp [ 1 ]) {
$temp = $timestamp [ 0 ];
$timestamp [ 0 ] = $timestamp [ 1 ];
$timestamp [ 1 ] = $temp ;
}
$conditions [ 'AND' ][] = array ( $scope . ' >=' => $timestamp [ 0 ]);
$conditions [ 'AND' ][] = array ( $scope . ' <=' => $timestamp [ 1 ]);
} else {
$timestamp = intval ( $this -> Event -> resolveTimeDelta ( $timestamp )) * 1000000 ; // seen in stored in micro-seconds in the DB
2019-07-04 15:08:05 +02:00
if ( $scope == 'Attribute.first_seen' ) {
$conditions [ 'AND' ][] = array ( $scope . ' >=' => $timestamp );
} else {
$conditions [ 'AND' ][] = array ( $scope . ' <=' => $timestamp );
}
2019-07-04 13:51:36 +02:00
}
if ( $returnRaw ) {
return $timestamp ;
}
return $conditions ;
}
2018-07-19 11:48:22 +02:00
public function setToIDSConditions ( $to_ids , $conditions )
{
if ( $to_ids === 'exclude' ) {
$conditions [ 'AND' ][] = array ( 'Attribute.to_ids' => 0 );
} else {
$conditions [ 'AND' ][] = array ( 'Attribute.to_ids' => 1 );
}
return $conditions ;
}
2021-09-02 21:12:31 +02:00
/**
* Get list of all CIDR for correlation .
* @ return array
*/
2018-07-19 11:48:22 +02:00
private function __getCIDRList ()
{
2020-12-22 23:43:17 +01:00
return $this -> find ( 'column' , array (
2018-07-19 11:48:22 +02:00
'conditions' => array (
'type' => array ( 'ip-src' , 'ip-dst' ),
2021-09-02 21:12:31 +02:00
'disable_correlation' => 0 ,
'deleted' => 0 ,
2018-07-19 11:48:22 +02:00
'value1 LIKE' => '%/%'
),
2020-12-22 23:43:17 +01:00
'fields' => array ( 'Attribute.value1' ),
'unique' => true ,
2018-07-19 11:48:22 +02:00
'order' => false
));
}
public function setCIDRList ()
{
$redis = $this -> setupRedis ();
$cidrList = array ();
if ( $redis ) {
$cidrList = $this -> __getCIDRList ();
2021-09-02 21:12:31 +02:00
$redis -> pipeline ();
$redis -> del ( 'misp:cidr_cache_list' );
2020-05-08 22:24:28 +02:00
if ( method_exists ( $redis , 'saddArray' )) {
$redis -> sAddArray ( 'misp:cidr_cache_list' , $cidrList );
} else {
foreach ( $cidrList as $cidr ) {
2021-09-02 21:12:31 +02:00
$redis -> sadd ( 'misp:cidr_cache_list' , $cidr );
2020-05-08 22:24:28 +02:00
}
2018-07-19 11:48:22 +02:00
}
2021-09-02 21:12:31 +02:00
$redis -> exec ();
2018-07-19 11:48:22 +02:00
}
return $cidrList ;
}
public function getSetCIDRList ()
{
$redis = $this -> setupRedis ();
if ( $redis ) {
2020-06-28 10:14:40 +02:00
if ( ! $redis -> exists ( 'misp:cidr_cache_list' )) {
2020-05-08 21:35:40 +02:00
$cidrList = $this -> setCIDRList ();
2018-07-19 11:48:22 +02:00
} else {
$cidrList = $redis -> smembers ( 'misp:cidr_cache_list' );
}
} else {
$cidrList = $this -> __getCIDRList ();
}
return $cidrList ;
}
public function fetchDistributionData ( $user )
{
2021-10-09 23:32:51 +02:00
$initialDistribution = $this -> defaultDistribution ();
2018-07-19 11:48:22 +02:00
$sgs = $this -> SharingGroup -> fetchAllAuthorised ( $user , 'name' , 1 );
$distributionLevels = $this -> distributionLevels ;
if ( empty ( $sgs )) {
unset ( $distributionLevels [ 4 ]);
}
return array ( 'sgs' => $sgs , 'levels' => $distributionLevels , 'initial' => $initialDistribution );
}
public function simpleAddMalwareSample ( $event_id , $attribute_settings , $filename , $tmpfile )
{
$attributes = array (
2021-10-09 17:28:44 +02:00
'malware-sample' => array ( 'type' => 'malware-sample' , 'category' => '' , 'to_ids' => 1 , 'disable_correlation' => 0 , 'object_relation' => 'malware-sample' ),
2018-07-19 11:48:22 +02:00
'filename' => array ( 'type' => 'filename' , 'category' => '' , 'to_ids' => 0 , 'disable_correlation' => 0 , 'object_relation' => 'filename' ),
'md5' => array ( 'type' => 'md5' , 'category' => '' , 'to_ids' => 1 , 'disable_correlation' => 0 , 'object_relation' => 'md5' ),
'sha1' => array ( 'type' => 'sha1' , 'category' => '' , 'to_ids' => 1 , 'disable_correlation' => 0 , 'object_relation' => 'sha1' ),
'sha256' => array ( 'type' => 'sha256' , 'category' => '' , 'to_ids' => 1 , 'disable_correlation' => 0 , 'object_relation' => 'sha256' ),
'size-in-bytes' => array ( 'type' => 'size-in-bytes' , 'category' => 'Other' , 'to_ids' => 0 , 'disable_correlation' => 1 , 'object_relation' => 'size-in-bytes' )
);
$hashes = array ( 'md5' , 'sha1' , 'sha256' );
2021-10-09 17:28:44 +02:00
$current = $this -> Object -> ObjectTemplate -> find ( 'first' , array (
2018-07-19 11:48:22 +02:00
'fields' => array ( 'MAX(version) AS version' , 'uuid' ),
'conditions' => array ( 'uuid' => '688c46fb-5edb-40a3-8273-1af7923e2215' ),
'recursive' => - 1 ,
'group' => array ( 'uuid' )
));
if ( ! empty ( $current )) {
2021-10-09 17:28:44 +02:00
$object_template = $this -> Object -> ObjectTemplate -> find ( 'first' , array (
2018-07-19 11:48:22 +02:00
'conditions' => array (
'ObjectTemplate.uuid' => '688c46fb-5edb-40a3-8273-1af7923e2215' ,
'ObjectTemplate.version' => $current [ 0 ][ 'version' ]
),
'recursive' => - 1
));
}
if ( empty ( $object_template )) {
$object_template = array (
'ObjectTemplate' => array (
'meta-category' => 'file' ,
'name' => 'file' ,
'uuid' => '688c46fb-5edb-40a3-8273-1af7923e2215' ,
'version' => 1 ,
'description' => 'File object describing a file with meta-information'
)
);
}
$object = array (
'distribution' => $attribute_settings [ 'distribution' ],
'sharing_group_id' => isset ( $attribute_settings [ 'sharing_group_id' ]) ? $attribute_settings [ 'sharing_group_id' ] : 0 ,
'meta-category' => $object_template [ 'ObjectTemplate' ][ 'meta-category' ],
'name' => $object_template [ 'ObjectTemplate' ][ 'name' ],
'template_version' => $object_template [ 'ObjectTemplate' ][ 'version' ],
'description' => $object_template [ 'ObjectTemplate' ][ 'description' ],
'template_uuid' => $object_template [ 'ObjectTemplate' ][ 'uuid' ],
'event_id' => $event_id ,
'comment' => ! empty ( $attribute_settings [ 'comment' ]) ? $attribute_settings [ 'comment' ] : ''
);
2021-10-09 17:28:44 +02:00
$result = $this -> handleMaliciousRaw ( $filename , $tmpfile -> read (), $hashes );
2018-07-19 11:48:22 +02:00
foreach ( $attributes as $k => $v ) {
$attribute = array (
'distribution' => 5 ,
'category' => empty ( $v [ 'category' ]) ? $attribute_settings [ 'category' ] : $v [ 'category' ],
'type' => $v [ 'type' ],
'to_ids' => $v [ 'to_ids' ],
'disable_correlation' => $v [ 'disable_correlation' ],
'object_id' => $this -> Object -> id ,
'event_id' => $event_id ,
'object_relation' => $v [ 'object_relation' ]
);
2021-10-09 17:07:29 +02:00
if ( $k === 'malware-sample' ) {
2018-07-19 11:48:22 +02:00
$attribute [ 'value' ] = $filename . '|' . $result [ 'md5' ];
2021-10-09 17:28:44 +02:00
$attribute [ 'data_raw' ] = $result [ 'data_raw' ];
2021-10-09 17:07:29 +02:00
} elseif ( $k === 'size-in-bytes' ) {
2018-07-19 11:48:22 +02:00
$attribute [ 'value' ] = $tmpfile -> size ();
2021-10-09 17:07:29 +02:00
} elseif ( $k === 'filename' ) {
2018-07-19 11:48:22 +02:00
$attribute [ 'value' ] = $filename ;
} else {
$attribute [ 'value' ] = $result [ $v [ 'type' ]];
}
$object [ 'Attribute' ][] = $attribute ;
}
return array ( 'Object' => array ( $object ));
}
public function advancedAddMalwareSample ( $event_id , $attribute_settings , $filename , $tmpfile )
{
2019-10-05 15:40:34 +02:00
try {
2020-08-13 15:58:42 +02:00
$result = $this -> loadAttachmentTool () -> advancedExtraction ( $this -> getPythonVersion (), $tmpfile -> path );
2019-10-05 15:40:34 +02:00
} catch ( Exception $e ) {
$this -> logException ( " Could not finish advanced extraction " , $e );
return $this -> simpleAddMalwareSample ( $event_id , $attribute_settings , $filename , $tmpfile );
}
if ( isset ( $result [ 'objects' ])) {
$result [ 'Object' ] = $result [ 'objects' ];
unset ( $result [ 'objects' ]);
}
if ( isset ( $result [ 'references' ])) {
$result [ 'ObjectReference' ] = $result [ 'references' ];
unset ( $result [ 'references' ]);
}
foreach ( $result [ 'Object' ] as $k => $object ) {
$result [ 'Object' ][ $k ][ 'distribution' ] = $attribute_settings [ 'distribution' ];
$result [ 'Object' ][ $k ][ 'sharing_group_id' ] = isset ( $attribute_settings [ 'distribution' ]) ? $attribute_settings [ 'distribution' ] : 0 ;
if ( ! empty ( $result [ 'Object' ][ $k ][ 'Attribute' ])) {
foreach ( $result [ 'Object' ][ $k ][ 'Attribute' ] as $k2 => $attribute ) {
if ( $attribute [ 'value' ] == $tmpfile -> name ) {
$result [ 'Object' ][ $k ][ 'Attribute' ][ $k2 ][ 'value' ] = $filename ;
2018-07-19 11:48:22 +02:00
}
}
}
}
2019-10-05 15:40:34 +02:00
2018-07-19 11:48:22 +02:00
return $result ;
}
// gets an attribute, saves it
// handles encryption, attaching to event/object, logging of issues, tag capturing
2019-08-06 15:55:16 +02:00
public function captureAttribute ( $attribute , $eventId , $user , $objectId = false , $log = false , $parentEvent = false , & $validationErrors = false , $params = array ())
2018-07-19 11:48:22 +02:00
{
$attribute [ 'event_id' ] = $eventId ;
2021-10-09 17:07:29 +02:00
$attribute [ 'object_id' ] = $objectId ? : 0 ;
2019-08-06 16:01:09 +02:00
if ( ! isset ( $attribute [ 'to_ids' ])) {
$attribute [ 'to_ids' ] = $this -> typeDefinitions [ $attribute [ 'type' ]][ 'to_ids' ];
}
2019-07-23 12:46:06 +02:00
$attribute [ 'to_ids' ] = $attribute [ 'to_ids' ] ? 1 : 0 ;
2019-08-06 16:01:09 +02:00
$attribute [ 'disable_correlation' ] = empty ( $attribute [ 'disable_correlation' ]) ? 0 : 1 ;
2018-07-19 11:48:22 +02:00
unset ( $attribute [ 'id' ]);
2019-08-06 15:55:16 +02:00
if ( isset ( $attribute [ 'base64' ])) {
$attribute [ 'data' ] = $attribute [ 'base64' ];
}
if ( ! empty ( $attribute [ 'enforceWarninglist' ]) || ! empty ( $params [ 'enforceWarninglist' ])) {
$this -> Warninglist = ClassRegistry :: init ( 'Warninglist' );
2020-09-07 16:37:38 +02:00
if ( ! $this -> Warninglist -> filterWarninglistAttribute ( $attribute )) {
2019-08-06 15:55:16 +02:00
$this -> validationErrors [ 'warninglist' ] = 'Attribute could not be saved as it trips over a warninglist and enforceWarninglist is enforced.' ;
$validationErrors = $this -> validationErrors [ 'warninglist' ];
2021-08-30 15:49:46 +02:00
$this -> loadLog () -> createLogEntry ( $user , 'add' , 'Attribute' , 0 ,
'Attribute dropped due to validation for Event ' . $eventId . ' failed' ,
'Validation errors: ' . json_encode ( $this -> validationErrors ) . ' Full Attribute: ' . json_encode ( $attribute )
);
2019-08-06 15:55:16 +02:00
return $attribute ;
}
}
2018-07-19 11:48:22 +02:00
if ( isset ( $attribute [ 'encrypt' ])) {
2021-10-09 18:45:51 +02:00
$attribute = $this -> onDemandEncrypt ( $attribute );
2018-07-19 11:48:22 +02:00
}
$this -> create ();
2018-07-20 16:32:30 +02:00
if ( ! isset ( $attribute [ 'distribution' ])) {
2021-10-09 23:32:51 +02:00
$attribute [ 'distribution' ] = $this -> defaultDistribution ();
2018-07-20 16:32:30 +02:00
}
2018-11-23 14:11:33 +01:00
$params = array (
2021-07-20 10:22:45 +02:00
'fieldList' => $this -> captureFields
2018-11-23 14:11:33 +01:00
);
if ( ! empty ( $parentEvent )) {
$params [ 'parentEvent' ] = $parentEvent ;
}
2021-06-07 14:44:36 +02:00
if ( ! empty ( $attribute [ 'SharingGroup' ])) {
$attribute [ 'sharing_group_id' ] = $this -> SharingGroup -> captureSG ( $attribute [ 'SharingGroup' ], $user );
} elseif ( ! empty ( $attribute [ 'sharing_group_id' ])) {
if ( ! $this -> SharingGroup -> checkIfAuthorised ( $user , $attribute [ 'sharing_group_id' ])) {
unset ( $attribute [ 'sharing_group_id' ]);
}
}
2018-09-28 09:21:29 +02:00
if ( ! $this -> save ( $attribute , $params )) {
2018-07-19 11:48:22 +02:00
$attribute_short = ( isset ( $attribute [ 'category' ]) ? $attribute [ 'category' ] : 'N/A' ) . '/' . ( isset ( $attribute [ 'type' ]) ? $attribute [ 'type' ] : 'N/A' ) . ' ' . ( isset ( $attribute [ 'value' ]) ? $attribute [ 'value' ] : 'N/A' );
2021-08-30 15:49:46 +02:00
$this -> loadLog () -> createLogEntry ( $user , 'add' , 'Attribute' , 0 ,
'Attribute dropped due to validation for Event ' . $eventId . ' failed: ' . $attribute_short ,
'Validation errors: ' . json_encode ( $this -> validationErrors ) . ' Full Attribute: ' . json_encode ( $attribute )
);
2018-07-19 11:48:22 +02:00
} else {
if ( isset ( $attribute [ 'AttributeTag' ])) {
foreach ( $attribute [ 'AttributeTag' ] as $at ) {
unset ( $at [ 'id' ]);
$this -> AttributeTag -> create ();
$at [ 'attribute_id' ] = $this -> id ;
$at [ 'event_id' ] = $eventId ;
$this -> AttributeTag -> save ( $at );
}
}
if ( isset ( $attribute [ 'Tag' ])) {
2019-08-06 16:33:16 +02:00
if ( ! empty ( $attribute [ 'Tag' ][ 'name' ])) {
$attribute [ 'Tag' ] = array ( $attribute [ 'Tag' ]);
}
2018-07-19 11:48:22 +02:00
foreach ( $attribute [ 'Tag' ] as $tag ) {
$tag_id = $this -> AttributeTag -> Tag -> captureTag ( $tag , $user );
if ( $tag_id ) {
$this -> AttributeTag -> create ();
2021-08-30 15:49:46 +02:00
$at = [
'attribute_id' => $this -> id ,
'event_id' => $eventId ,
'tag_id' => $tag_id ,
];
2018-07-19 11:48:22 +02:00
$this -> AttributeTag -> save ( $at );
}
}
}
if ( ! empty ( $attribute [ 'Sighting' ])) {
2021-02-02 23:11:27 +01:00
$this -> Sighting -> captureSightings ( $attribute [ 'Sighting' ], $this -> id , $eventId , $user );
2018-07-19 11:48:22 +02:00
}
}
2019-03-24 22:30:41 +01:00
if ( ! empty ( $this -> validationErrors )) {
$validationErrors = $this -> validationErrors ;
}
2018-07-19 11:48:22 +02:00
return $attribute ;
}
2021-09-07 10:24:08 +02:00
public function editAttribute ( $attribute , array $event , $user , $objectId , $log = false , $force = false , & $nothingToChange = false )
2018-07-19 11:48:22 +02:00
{
2021-09-07 10:24:08 +02:00
$eventId = $event [ 'Event' ][ 'id' ];
2018-07-19 11:48:22 +02:00
$attribute [ 'event_id' ] = $eventId ;
$attribute [ 'object_id' ] = $objectId ;
if ( isset ( $attribute [ 'encrypt' ])) {
2021-10-09 18:45:51 +02:00
$attribute = $this -> onDemandEncrypt ( $attribute );
2018-07-19 11:48:22 +02:00
}
unset ( $attribute [ 'id' ]);
if ( isset ( $attribute [ 'uuid' ])) {
$existingAttribute = $this -> find ( 'first' , array (
'conditions' => array ( 'Attribute.uuid' => $attribute [ 'uuid' ]),
2020-01-16 11:02:56 +01:00
'recursive' => - 1 ,
2018-07-19 11:48:22 +02:00
));
2021-09-03 22:13:35 +02:00
if ( ! empty ( $existingAttribute )) {
2019-06-06 15:11:34 +02:00
if ( $existingAttribute [ 'Attribute' ][ 'event_id' ] != $eventId || $existingAttribute [ 'Attribute' ][ 'object_id' ] != $objectId ) {
2021-09-03 22:13:35 +02:00
$change = 'An attribute was blocked from being saved due to a duplicate UUID. The uuid in question is: ' . $attribute [ 'uuid' ] . '. This can also be due to the same attribute (or an attribute with the same UUID) existing in a different event / object)' ;
$this -> loadLog () -> createLogEntry ( $user , 'edit' , 'Attribute' , 0 , 'Duplicate UUID found in attribute' , $change );
2018-07-19 11:48:22 +02:00
return true ;
}
// If a field is not set in the request, just reuse the old value
2019-06-13 09:16:34 +02:00
$recoverFields = array ( 'value' , 'to_ids' , 'distribution' , 'category' , 'type' , 'comment' , 'sharing_group_id' , 'object_id' , 'object_relation' , 'first_seen' , 'last_seen' );
2018-07-19 11:48:22 +02:00
foreach ( $recoverFields as $rF ) {
if ( ! isset ( $attribute [ $rF ])) {
$attribute [ $rF ] = $existingAttribute [ 'Attribute' ][ $rF ];
}
}
$attribute [ 'id' ] = $existingAttribute [ 'Attribute' ][ 'id' ];
// Check if the attribute's timestamp is bigger than the one that already exists.
// If yes, it means that it's newer, so insert it. If no, it means that it's the same attribute or older - don't insert it, insert the old attribute.
// Alternatively, we could unset this attribute from the request, but that could lead with issues if we decide that we want to start deleting attributes that don't exist in a pushed event.
if ( isset ( $attribute [ 'timestamp' ])) {
2020-05-14 09:34:49 +02:00
if ( ! $force && $attribute [ 'timestamp' ] <= $existingAttribute [ 'Attribute' ][ 'timestamp' ]) {
2020-07-22 13:10:52 +02:00
$nothingToChange = true ;
2018-07-19 11:48:22 +02:00
return true ;
}
} else {
2021-08-12 14:05:13 +02:00
$attribute [ 'timestamp' ] = time ();
2018-07-19 11:48:22 +02:00
}
} else {
$this -> create ();
}
} else {
$this -> create ();
}
$attribute [ 'event_id' ] = $eventId ;
if ( isset ( $attribute [ 'distribution' ]) && $attribute [ 'distribution' ] == 4 ) {
if ( ! empty ( $attribute [ 'SharingGroup' ])) {
$attribute [ 'sharing_group_id' ] = $this -> SharingGroup -> captureSG ( $attribute [ 'SharingGroup' ], $user );
} elseif ( ! empty ( $attribute [ 'sharing_group_id' ])) {
if ( ! $this -> SharingGroup -> checkIfAuthorised ( $user , $attribute [ 'sharing_group_id' ])) {
unset ( $attribute [ 'sharing_group_id' ]);
}
}
if ( empty ( $attribute [ 'sharing_group_id' ])) {
$attribute_short = ( isset ( $attribute [ 'category' ]) ? $attribute [ 'category' ] : 'N/A' ) . '/' . ( isset ( $attribute [ 'type' ]) ? $attribute [ 'type' ] : 'N/A' ) . ' ' . ( isset ( $attribute [ 'value' ]) ? $attribute [ 'value' ] : 'N/A' );
2021-09-03 22:13:35 +02:00
$this -> loadLog () -> createLogEntry ( $user , 'edit' , 'Attribute' , 0 ,
'Attribute dropped due to invalid sharing group for Event ' . $eventId . ' failed: ' . $attribute_short ,
'Validation errors: ' . json_encode ( $this -> validationErrors ) . ' Full Attribute: ' . json_encode ( $attribute )
);
2018-07-19 11:48:22 +02:00
return 'Invalid sharing group choice.' ;
}
2018-11-29 15:33:45 +01:00
} else if ( ! isset ( $attribute [ 'distribution' ])) {
2021-10-09 23:32:51 +02:00
$attribute [ 'distribution' ] = $this -> defaultDistribution ();
2019-02-10 13:08:12 +01:00
}
2021-07-27 18:58:04 +02:00
$fieldList = self :: EDITABLE_FIELDS ;
2020-07-24 10:59:42 +02:00
if ( empty ( $existingAttribute )) {
$addableFieldList = array ( 'event_id' , 'type' , 'uuid' );
$fieldList = array_merge ( $fieldList , $addableFieldList );
}
2018-07-19 11:48:22 +02:00
if ( $objectId ) {
$fieldList [] = 'object_id' ;
$fieldList [] = 'object_relation' ;
}
2021-09-07 10:24:08 +02:00
if ( ! $this -> save ( $attribute , [ 'fieldList' => $fieldList , 'parentEvent' => $event ])) {
2018-07-19 11:48:22 +02:00
$attribute_short = ( isset ( $attribute [ 'category' ]) ? $attribute [ 'category' ] : 'N/A' ) . '/' . ( isset ( $attribute [ 'type' ]) ? $attribute [ 'type' ] : 'N/A' ) . ' ' . ( isset ( $attribute [ 'value' ]) ? $attribute [ 'value' ] : 'N/A' );
2021-09-03 22:13:35 +02:00
$this -> loadLog () -> createLogEntry ( $user , 'edit' , 'Attribute' , 0 ,
'Attribute dropped due to validation for Event ' . $eventId . ' failed: ' . $attribute_short ,
'Validation errors: ' . json_encode ( $this -> validationErrors ) . ' Full Attribute: ' . json_encode ( $attribute )
);
2018-07-19 11:48:22 +02:00
return $this -> validationErrors ;
} else {
2021-03-15 17:53:01 +01:00
if ( isset ( $attribute [ 'Sighting' ]) && ! empty ( $attribute [ 'Sighting' ])) {
$this -> Sighting -> captureSightings ( $attribute [ 'Sighting' ], $this -> id , $eventId , $user );
}
2020-01-16 11:02:56 +01:00
if ( $user [ 'Role' ][ 'perm_tagger' ]) {
2020-01-23 09:45:27 +01:00
/*
2020-01-16 15:13:10 +01:00
We should uncomment the line below in the future once we have tag soft - delete
A solution to still keep the behavior for previous instance could be to not soft - delete the Tag if the remote instance
has a version below x
*/
// $this->AttributeTag->pruneOutdatedAttributeTagsFromSync(isset($attribute['Tag']) ? $attribute['Tag'] : array(), $existingAttribute['AttributeTag']);
2020-01-16 11:02:56 +01:00
if ( isset ( $attribute [ 'Tag' ])) {
foreach ( $attribute [ 'Tag' ] as $tag ) {
$tag_id = $this -> AttributeTag -> Tag -> captureTag ( $tag , $user );
if ( $tag_id ) {
2020-02-27 11:07:17 +01:00
$tag [ 'id' ] = $tag_id ;
2020-01-16 11:02:56 +01:00
// fix the IDs here
2020-02-27 11:07:17 +01:00
$this -> AttributeTag -> handleAttributeTag ( $this -> id , $attribute [ 'event_id' ], $tag );
2020-01-16 11:02:56 +01:00
} else {
// If we couldn't attach the tag it is most likely because we couldn't create it - which could have many reasons
// However, if a tag couldn't be added, it could also be that the user is a tagger but not a tag editor
// In which case if no matching tag is found, no tag ID is returned. Logging these is pointless as it is the correct behaviour.
if ( $user [ 'Role' ][ 'perm_tag_editor' ]) {
2021-09-03 22:13:35 +02:00
$this -> loadLog () -> createLogEntry ( $user , 'edit' , 'Attribute' , $this -> id , 'Failed create or attach Tag ' . $tag [ 'name' ] . ' to the attribute.' );
2020-01-16 11:02:56 +01:00
}
2018-07-19 11:48:22 +02:00
}
}
}
}
}
return true ;
}
2018-08-24 14:21:50 +02:00
2021-07-24 22:57:15 +02:00
public function deleteAttribute ( $id , array $user , $hard = false )
2019-06-06 15:11:34 +02:00
{
2019-08-20 16:46:32 +02:00
$result = $this -> fetchAttributes ( $user , array (
2019-06-06 15:11:34 +02:00
'conditions' => array ( 'Attribute.id' => $id ),
2019-08-20 16:46:32 +02:00
'flatten' => 1 ,
2019-12-04 16:11:38 +01:00
'deleted' => [ 0 , 1 ],
2019-06-12 12:04:49 +02:00
'recursive' => - 1 ,
'contain' => array ( 'Event' )
2019-06-06 15:11:34 +02:00
));
if ( empty ( $result )) {
2019-08-20 16:46:32 +02:00
throw new ForbiddenException ( __ ( 'Invalid attribute' ));
2019-06-06 15:11:34 +02:00
}
2019-08-20 17:00:21 +02:00
$result = $result [ 0 ];
2019-06-06 15:11:34 +02:00
// check for permissions
if ( ! $user [ 'Role' ][ 'perm_site_admin' ]) {
if ( $result [ 'Event' ][ 'locked' ]) {
if ( $user [ 'org_id' ] != $result [ 'Event' ][ 'org_id' ] || ! $user [ 'Role' ][ 'perm_sync' ]) {
2019-08-20 16:46:32 +02:00
throw new ForbiddenException ( __ ( 'You do not have permission to do that.' ));
2019-06-06 15:11:34 +02:00
}
} else {
if ( $user [ 'org_id' ] != $result [ 'Event' ][ 'orgc_id' ]) {
2019-08-20 16:46:32 +02:00
throw new ForbiddenException ( __ ( 'You do not have permission to do that.' ));
2019-06-06 15:11:34 +02:00
}
}
}
if ( $hard ) {
$save = $this -> delete ( $id );
} else {
if ( Configure :: read ( 'Security.sanitise_attribute_on_delete' )) {
$result [ 'Attribute' ][ 'category' ] = 'Other' ;
$result [ 'Attribute' ][ 'type' ] = 'comment' ;
$result [ 'Attribute' ][ 'value' ] = 'deleted' ;
$result [ 'Attribute' ][ 'comment' ] = '' ;
$result [ 'Attribute' ][ 'to_ids' ] = 0 ;
}
$result [ 'Attribute' ][ 'deleted' ] = 1 ;
2021-07-24 22:57:15 +02:00
$result [ 'Attribute' ][ 'timestamp' ] = time ();
2019-06-06 15:11:34 +02:00
$save = $this -> save ( $result );
$object_refs = $this -> Object -> ObjectReference -> find ( 'all' , array (
'conditions' => array (
'ObjectReference.referenced_type' => 0 ,
'ObjectReference.referenced_id' => $id ,
),
'recursive' => - 1
));
foreach ( $object_refs as $ref ) {
$ref [ 'ObjectReference' ][ 'deleted' ] = 1 ;
$this -> Object -> ObjectReference -> save ( $ref );
}
}
// attachment will be deleted with the beforeDelete() function in the Model
if ( $save ) {
// We have just deleted the attribute, let's also check if there are any shadow attributes that were attached to it and delete them
$this -> Event -> ShadowAttribute -> deleteAll ( array ( 'ShadowAttribute.old_id' => $id ), false );
// remove the published flag from the event
$this -> Event -> unpublishEvent ( $result [ 'Event' ][ 'id' ]);
return true ;
} else {
return false ;
}
}
2018-07-19 11:48:22 +02:00
public function attachValidationWarnings ( $adata )
{
if ( ! $this -> __fTool ) {
$this -> __fTool = new FinancialTool ();
}
if ( ! $this -> __fTool -> validateRouter ( $adata [ 'type' ], $adata [ 'value' ])) {
$adata [ 'validationIssue' ] = true ;
}
return $adata ;
}
2018-08-24 14:21:50 +02:00
2021-09-29 13:49:24 +02:00
public function buildFilterConditions ( array $user , array & $params )
2018-11-23 14:11:33 +01:00
{
$conditions = $this -> buildConditions ( $user );
if ( isset ( $params [ 'wildcard' ])) {
$temp = array ();
$options = array (
'filter' => 'wildcard' ,
'scope' => 'Attribute' ,
'pop' => false ,
'context' => 'Event'
);
$conditions [ 'AND' ][] = array ( 'OR' => $this -> Event -> set_filter_wildcard_attributes ( $params , $temp , $options ));
} else {
if ( isset ( $params [ 'ignore' ])) {
2021-09-29 13:49:24 +02:00
unset ( $params [ 'to_ids' ]);
unset ( $params [ 'published' ]);
2018-11-23 14:11:33 +01:00
}
$simple_params = array (
'Attribute' => array (
'value' => array ( 'function' => 'set_filter_value' ),
'category' => array ( 'function' => 'set_filter_simple_attribute' ),
2020-09-03 12:02:59 +02:00
'type' => array ( 'function' => 'set_filter_type' ),
2019-05-23 07:56:23 +02:00
'object_relation' => array ( 'function' => 'set_filter_simple_attribute' ),
2019-01-25 08:35:50 +01:00
'tags' => array ( 'function' => 'set_filter_tags' , 'pop' => true ),
2018-11-23 14:11:33 +01:00
'uuid' => array ( 'function' => 'set_filter_uuid' ),
'deleted' => array ( 'function' => 'set_filter_deleted' ),
2021-03-24 14:42:09 +01:00
'timestamp' => array ( 'function' => 'set_filter_timestamp' , 'pop' => true ),
2019-11-11 16:09:54 +01:00
'attribute_timestamp' => array ( 'function' => 'set_filter_timestamp' ),
2019-07-04 13:51:36 +02:00
'first_seen' => array ( 'function' => 'set_filter_seen' ),
'last_seen' => array ( 'function' => 'set_filter_seen' ),
2018-11-23 14:11:33 +01:00
'to_ids' => array ( 'function' => 'set_filter_to_ids' ),
'comment' => array ( 'function' => 'set_filter_comment' )
),
'Event' => array (
'eventid' => array ( 'function' => 'set_filter_eventid' ),
'eventinfo' => array ( 'function' => 'set_filter_eventinfo' ),
'ignore' => array ( 'function' => 'set_filter_ignore' ),
'from' => array ( 'function' => 'set_filter_timestamp' ),
'to' => array ( 'function' => 'set_filter_timestamp' ),
2019-06-07 09:49:52 +02:00
'date' => array ( 'function' => 'set_filter_date' ),
2019-01-25 08:35:50 +01:00
'tags' => array ( 'function' => 'set_filter_tags' ),
2018-11-23 14:11:33 +01:00
'last' => array ( 'function' => 'set_filter_timestamp' , 'pop' => true ),
'timestamp' => array ( 'function' => 'set_filter_timestamp' , 'pop' => true ),
'event_timestamp' => array ( 'function' => 'set_filter_timestamp' , 'pop' => true ),
'publish_timestamp' => array ( 'function' => 'set_filter_timestamp' ),
'org' => array ( 'function' => 'set_filter_org' ),
2020-05-05 18:07:51 +02:00
'published' => array ( 'function' => 'set_filter_published' ),
'threat_level_id' => array ( 'function' => 'set_filter_threat_level_id' )
2018-11-23 14:11:33 +01:00
),
'Object' => array (
'object_name' => array ( 'function' => 'set_filter_object_name' ),
'deleted' => array ( 'function' => 'set_filter_deleted' )
)
);
foreach ( $params as $param => $paramData ) {
foreach ( $simple_params as $scope => $simple_param_scoped ) {
2019-01-25 09:34:55 +01:00
if ( isset ( $simple_param_scoped [ $param ]) && isset ( $params [ $param ]) && $params [ $param ] !== false ) {
2018-11-23 14:11:33 +01:00
$options = array (
'filter' => $param ,
'scope' => $scope ,
'pop' => ! empty ( $simple_param_scoped [ $param ][ 'pop' ]),
'context' => 'Attribute'
);
$conditions = $this -> Event -> { $simple_param_scoped [ $param ][ 'function' ]}( $params , $conditions , $options );
}
}
}
}
return $conditions ;
}
2019-05-10 14:25:38 +02:00
public function restSearch ( $user , $returnFormat , $filters , $paramsOnly = false , $jobId = false , & $elementCounter = 0 , & $renderView = false )
2018-11-23 14:11:33 +01:00
{
if ( ! isset ( $this -> validFormats [ $returnFormat ][ 1 ])) {
throw new NotFoundException ( 'Invalid output format.' );
}
App :: uses ( $this -> validFormats [ $returnFormat ][ 1 ], 'Export' );
$exportTool = new $this -> validFormats [ $returnFormat ][ 1 ]();
2020-05-06 17:50:00 +02:00
if ( ! empty ( $exportTool -> use_default_filters )) {
$exportTool -> setDefaultFilters ( $filters );
}
2018-11-23 14:11:33 +01:00
if ( empty ( $exportTool -> non_restrictive_export )) {
if ( ! isset ( $filters [ 'to_ids' ])) {
$filters [ 'to_ids' ] = 1 ;
}
if ( ! isset ( $filters [ 'published' ])) {
$filters [ 'published' ] = 1 ;
}
2019-09-29 20:35:51 +02:00
$filters [ 'allow_proposal_blocking' ] = 1 ;
2018-11-23 14:11:33 +01:00
}
if ( ! empty ( $filters [ 'quickFilter' ])) {
$filters [ 'searchall' ] = $filters [ 'quickFilter' ];
if ( ! empty ( $filters [ 'value' ])) {
unset ( $filters [ 'value' ]);
}
}
2019-05-10 14:25:38 +02:00
if ( ! empty ( $exportTool -> renderView )) {
$renderView = $exportTool -> renderView ;
}
2018-11-23 14:11:33 +01:00
if ( isset ( $filters [ 'searchall' ])) {
if ( ! empty ( $filters [ 'value' ])) {
$filters [ 'wildcard' ] = $filters [ 'value' ];
unset ( $filters [ 'value' ]);
} else {
$filters [ 'wildcard' ] = $filters [ 'searchall' ];
}
}
2019-11-06 11:12:30 +01:00
$subqueryElements = $this -> Event -> harvestSubqueryElements ( $filters );
2020-03-12 10:26:09 +01:00
$filters = $this -> Event -> addFiltersFromSubqueryElements ( $filters , $subqueryElements , $user );
2020-05-29 15:23:27 +02:00
$filters = $this -> Event -> addFiltersFromUserSettings ( $user , $filters );
2018-11-23 14:11:33 +01:00
$conditions = $this -> buildFilterConditions ( $user , $filters );
$params = array (
'conditions' => $conditions ,
'fields' => array ( 'Attribute.*' , 'Event.org_id' , 'Event.distribution' ),
'withAttachments' => ! empty ( $filters [ 'withAttachments' ]) ? $filters [ 'withAttachments' ] : 0 ,
'enforceWarninglist' => ! empty ( $filters [ 'enforceWarninglist' ]) ? $filters [ 'enforceWarninglist' ] : 0 ,
'includeAllTags' => ! empty ( $filters [ 'includeAllTags' ]) ? $filters [ 'includeAllTags' ] : 0 ,
'flatten' => 1 ,
'includeEventUuid' => ! empty ( $filters [ 'includeEventUuid' ]) ? $filters [ 'includeEventUuid' ] : 0 ,
'includeEventTags' => ! empty ( $filters [ 'includeEventTags' ]) ? $filters [ 'includeEventTags' ] : 0 ,
2019-05-09 17:25:39 +02:00
'includeProposals' => ! empty ( $filters [ 'includeProposals' ]) ? $filters [ 'includeProposals' ] : 0 ,
2019-07-30 14:47:02 +02:00
'includeWarninglistHits' => ! empty ( $filters [ 'includeWarninglistHits' ]) ? $filters [ 'includeWarninglistHits' ] : 0 ,
'includeContext' => ! empty ( $filters [ 'includeContext' ]) ? $filters [ 'includeContext' ] : 0 ,
2019-08-02 13:41:20 +02:00
'includeSightings' => ! empty ( $filters [ 'includeSightings' ]) ? $filters [ 'includeSightings' ] : 0 ,
2019-11-06 21:20:04 +01:00
'includeSightingdb' => ! empty ( $filters [ 'includeSightingdb' ]) ? $filters [ 'includeSightingdb' ] : 0 ,
2019-08-13 16:32:58 +02:00
'includeCorrelations' => ! empty ( $filters [ 'includeCorrelations' ]) ? $filters [ 'includeCorrelations' ] : 0 ,
2019-09-06 11:32:54 +02:00
'includeDecayScore' => ! empty ( $filters [ 'includeDecayScore' ]) ? $filters [ 'includeDecayScore' ] : 0 ,
2019-09-29 20:35:51 +02:00
'includeFullModel' => ! empty ( $filters [ 'includeFullModel' ]) ? $filters [ 'includeFullModel' ] : 0 ,
'allow_proposal_blocking' => ! empty ( $filters [ 'allow_proposal_blocking' ]) ? $filters [ 'allow_proposal_blocking' ] : 0
2018-11-23 14:11:33 +01:00
);
2019-05-10 14:41:50 +02:00
if ( ! empty ( $filters [ 'attackGalaxy' ])) {
$params [ 'attackGalaxy' ] = $filters [ 'attackGalaxy' ];
}
2018-11-23 14:11:33 +01:00
if ( isset ( $filters [ 'include_event_uuid' ])) {
$params [ 'includeEventUuid' ] = $filters [ 'include_event_uuid' ];
}
if ( isset ( $filters [ 'limit' ])) {
$params [ 'limit' ] = $filters [ 'limit' ];
2019-10-08 08:27:21 +02:00
if ( ! isset ( $filters [ 'page' ])) {
$filters [ 'page' ] = 1 ;
}
2018-11-23 14:11:33 +01:00
}
if ( isset ( $filters [ 'page' ])) {
$params [ 'page' ] = $filters [ 'page' ];
}
2019-05-22 16:08:27 +02:00
if ( ! empty ( $filters [ 'deleted' ])) {
$params [ 'deleted' ] = $filters [ 'deleted' ];
2018-11-23 14:11:33 +01:00
}
2019-07-25 16:21:12 +02:00
if ( ! empty ( $filters [ 'excludeDecayed' ])) {
$params [ 'excludeDecayed' ] = $filters [ 'excludeDecayed' ];
2019-07-25 16:13:42 +02:00
$params [ 'includeDecayScore' ] = 1 ;
}
2019-07-25 15:45:33 +02:00
if ( ! empty ( $filters [ 'decayingModel' ])) {
$params [ 'decayingModel' ] = $filters [ 'decayingModel' ];
}
2019-08-14 10:48:13 +02:00
if ( ! empty ( $filters [ 'modelOverrides' ])) {
$params [ 'modelOverrides' ] = $filters [ 'modelOverrides' ];
}
2019-09-06 14:55:12 +02:00
if ( ! empty ( $filters [ 'score' ])) {
$params [ 'score' ] = $filters [ 'score' ];
}
2018-11-23 14:11:33 +01:00
if ( $paramsOnly ) {
return $params ;
}
if ( method_exists ( $exportTool , 'modify_params' )) {
$params = $exportTool -> modify_params ( $user , $params );
}
$exportToolParams = array (
'user' => $user ,
'params' => $params ,
'returnFormat' => $returnFormat ,
'scope' => 'Attribute' ,
'filters' => $filters
);
if ( ! empty ( $exportTool -> additional_params )) {
2019-09-25 20:17:25 +02:00
$params = array_merge_recursive (
$params ,
$exportTool -> additional_params
);
2018-11-23 14:11:33 +01:00
}
2020-06-19 16:21:53 +02:00
$tmpfile = new TmpFileTool ();
$tmpfile -> write ( $exportTool -> header ( $exportToolParams ));
2018-11-23 14:11:33 +01:00
$loop = false ;
if ( empty ( $params [ 'limit' ])) {
2020-11-08 16:13:17 +01:00
$memoryInMb = $this -> convert_to_memory_limit_to_mb ( ini_get ( 'memory_limit' ));
2020-01-29 22:33:27 +01:00
$default_attribute_memory_coefficient = Configure :: check ( 'MISP.default_attribute_memory_coefficient' ) ? Configure :: read ( 'MISP.default_attribute_memory_coefficient' ) : 80 ;
2020-11-08 16:13:17 +01:00
$memoryScalingFactor = isset ( $exportTool -> memory_scaling_factor ) ? $exportTool -> memory_scaling_factor : $default_attribute_memory_coefficient ;
$params [ 'limit' ] = $memoryInMb * $memoryScalingFactor ;
2018-11-23 14:11:33 +01:00
$loop = true ;
$params [ 'page' ] = 1 ;
}
2020-05-15 14:08:34 +02:00
if ( empty ( $exportTool -> mock_query_only )) {
2020-10-30 16:46:03 +01:00
$elementCounter = $this -> __iteratedFetch ( $user , $params , $loop , $tmpfile , $exportTool , $exportToolParams );
2020-05-11 12:54:11 +02:00
}
2020-06-19 16:21:53 +02:00
$tmpfile -> write ( $exportTool -> footer ( $exportToolParams ));
2020-10-25 09:44:01 +01:00
return $tmpfile ;
2018-11-23 14:11:33 +01:00
}
2018-11-20 19:53:02 +01:00
2020-10-30 16:46:03 +01:00
/**
* @ param array $user
* @ param array $params
* @ param bool $loop If true , data are fetched in loop to keep memory usage low
* @ param TmpFileTool $tmpfile
2020-11-08 16:13:17 +01:00
* @ param object $exportTool
2020-10-30 16:46:03 +01:00
* @ param array $exportToolParams
2020-11-08 16:13:17 +01:00
* @ return int Number of attributes
2020-10-30 16:46:03 +01:00
* @ throws Exception
*/
private function __iteratedFetch ( array $user , array $params , $loop , TmpFileTool $tmpfile , $exportTool , array $exportToolParams )
2018-11-23 14:11:33 +01:00
{
2020-09-01 15:59:38 +02:00
$this -> Allowedlist = ClassRegistry :: init ( 'Allowedlist' );
2020-10-30 16:46:03 +01:00
$separator = $exportTool -> separator ( $exportToolParams );
$elementCounter = 0 ;
2018-11-23 14:11:33 +01:00
$continue = true ;
2020-10-30 16:46:03 +01:00
do {
2018-11-23 14:11:33 +01:00
$results = $this -> fetchAttributes ( $user , $params , $continue );
2020-10-30 16:46:03 +01:00
if ( empty ( $results )) {
break ; // nothing found, skip rest
}
2019-11-06 21:20:04 +01:00
if ( $params [ 'includeSightingdb' ]) {
$this -> Sightingdb = ClassRegistry :: init ( 'Sightingdb' );
$results = $this -> Sightingdb -> attachToAttributes ( $results , $user );
}
2020-09-01 15:59:38 +02:00
$results = $this -> Allowedlist -> removeAllowedlistedFromArray ( $results , true );
2020-10-30 16:46:03 +01:00
$elementCounter += count ( $results );
2018-11-23 14:11:33 +01:00
foreach ( $results as $attribute ) {
2019-12-05 09:42:49 +01:00
$handlerResult = $exportTool -> handler ( $attribute , $exportToolParams );
if ( $handlerResult !== '' ) {
2020-10-30 16:46:03 +01:00
$tmpfile -> writeWithSeparator ( $handlerResult , $separator );
2018-11-23 14:11:33 +01:00
}
}
2020-10-30 16:46:03 +01:00
$params [ 'page' ] += 1 ;
} while ( $loop && $continue );
return $elementCounter ;
2018-11-23 14:11:33 +01:00
}
2020-04-02 07:02:24 +02:00
public function set_filter_uuid ( & $params , $conditions , $options )
{
if ( ! empty ( $params [ 'uuid' ])) {
$params [ 'uuid' ] = $this -> convert_filters ( $params [ 'uuid' ]);
if ( ! empty ( $params [ 'uuid' ][ 'OR' ])) {
$conditions [ 'AND' ][] = array (
'OR' => array (
'Event.uuid' => $params [ 'uuid' ][ 'OR' ],
'Attribute.uuid' => $params [ 'uuid' ][ 'OR' ]
)
);
}
if ( ! empty ( $params [ 'uuid' ][ 'NOT' ])) {
$conditions [ 'AND' ][] = array (
'NOT' => array (
'Event.uuid' => $params [ 'uuid' ][ 'NOT' ],
'Attribute.uuid' => $params [ 'uuid' ][ 'NOT' ]
)
);
}
}
return $conditions ;
}
2020-01-27 21:22:18 +01:00
/**
* @ param array $attribute
*/
public function removeGalaxyClusterTags ( array & $attribute )
{
$galaxyTagIds = array ();
foreach ( $attribute [ 'Galaxy' ] as $galaxy ) {
foreach ( $galaxy [ 'GalaxyCluster' ] as $galaxyCluster ) {
$galaxyTagIds [ $galaxyCluster [ 'tag_id' ]] = true ;
}
}
if ( empty ( $galaxyTagIds )) {
return ;
}
foreach ( $attribute [ 'AttributeTag' ] as $k => $attributeTag ) {
$tagId = $attributeTag [ 'Tag' ][ 'id' ];
if ( isset ( $galaxyTagIds [ $tagId ])) {
unset ( $attribute [ 'AttributeTag' ][ $k ]);
}
}
}
2020-07-06 20:15:56 +02:00
/**
* @ param string $value
* @ return bool
*/
private function isDomainValid ( $value )
{
return preg_match ( " #^[A-Z0-9. \ -_]+ \ .[A-Z0-9 \ -] { 2,} $ #i " , $value ) === 1 ;
}
/**
* @ param string $value
* @ return bool
*/
private function isPortValid ( $value )
{
2020-10-13 18:13:50 +02:00
return $this -> isPositiveInteger ( $value ) && $value >= 1 && $value <= 65535 ;
2020-10-13 09:23:44 +02:00
}
/**
* @ param string $type
* @ param string $value
* @ return bool
*/
private function isHashValid ( $type , $value )
{
2021-07-22 12:56:47 +02:00
if ( ! isset ( self :: HEX_HAS_LENGTHS [ $type ])) {
2020-10-13 09:23:44 +02:00
throw new InvalidArgumentException ( " Invalid hash type ' $type '. " );
}
2021-07-22 12:56:47 +02:00
return strlen ( $value ) === self :: HEX_HAS_LENGTHS [ $type ] && ctype_xdigit ( $value );
2020-07-06 20:15:56 +02:00
}
2020-10-13 18:13:50 +02:00
/**
* Returns true if input value is positive integer or zero .
* @ param int | string $value
* @ return bool
*/
private function isPositiveInteger ( $value )
{
return ( is_int ( $value ) && $value >= 0 ) || ctype_digit ( $value );
2020-07-06 20:15:56 +02:00
}
2020-10-15 11:56:21 +02:00
public function typeToCategoryMapping ()
{
$typeCategoryMapping = array ();
foreach ( $this -> categoryDefinitions as $k => $cat ) {
foreach ( $cat [ 'types' ] as $type ) {
$typeCategoryMapping [ $type ][ $k ] = $k ;
}
}
foreach ( $typeCategoryMapping as $k => $v ) {
$typeCategoryMapping [ $k ] = array_values ( $v );
}
return $typeCategoryMapping ;
2020-07-06 20:15:56 +02:00
}
2021-01-09 21:12:57 +01:00
2021-10-09 23:32:51 +02:00
/**
* Fetch default distribution from `MISP.default_attribute_distribution` setting . If this setting is not defined ,
* default distribution is `5` ( Inherit event )
* @ return int
*/
public function defaultDistribution ()
{
static $distribution ;
if ( $distribution === null ) {
$distribution = Configure :: read ( 'MISP.default_attribute_distribution' );
if ( $distribution === null || $distribution === 'event' ) {
$distribution = 5 ;
}
}
return $distribution ;
}
2021-01-09 21:12:57 +01:00
public function __isset ( $name )
{
if ( $name === 'typeDefinitions' || $name === 'categoryDefinitions' ) {
return true ;
}
return parent :: __isset ( $name );
}
public function __get ( $name )
{
if ( $name === 'typeDefinitions' ) {
$this -> typeDefinitions = $this -> generateTypeDefinitions ();
return $this -> typeDefinitions ;
} else if ( $name === 'categoryDefinitions' ) {
2021-08-20 08:38:17 +02:00
$this -> categoryDefinitions = $this -> generateCategoryDefinitions ();
2021-01-09 21:12:57 +01:00
return $this -> categoryDefinitions ;
}
return parent :: __get ( $name );
}
/**
* Generate just when really need
* NOTE WHEN MODIFYING : please ensure to run the script 'tools/gen_misp_types_categories.py' to update the new definitions everywhere . ( docu , website , RFC , ... )
* @ return array []
*/
2021-08-20 08:38:17 +02:00
private function generateCategoryDefinitions ()
2021-01-09 21:12:57 +01:00
{
return array (
'Internal reference' => array (
'desc' => __ ( 'Reference used by the publishing party (e.g. ticket number)' ),
'types' => array ( 'text' , 'link' , 'comment' , 'other' , 'hex' , 'anonymised' , 'git-commit-id' )
),
'Targeting data' => array (
'desc' => __ ( 'Internal Attack Targeting and Compromise Information' ),
'formdesc' => __ ( 'Targeting information to include recipient email, infected machines, department, and or locations.' ),
'types' => array ( 'target-user' , 'target-email' , 'target-machine' , 'target-org' , 'target-location' , 'target-external' , 'comment' , 'anonymised' )
),
'Antivirus detection' => array (
'desc' => __ ( 'All the info about how the malware is detected by the antivirus products' ),
'formdesc' => __ ( 'List of anti-virus vendors detecting the malware or information on detection performance (e.g. 13/43 or 67%). Attachment with list of detection or link to VirusTotal could be placed here as well.' ),
'types' => array ( 'link' , 'comment' , 'text' , 'hex' , 'attachment' , 'other' , 'anonymised' )
),
'Payload delivery' => array (
'desc' => __ ( 'Information about how the malware is delivered' ),
'formdesc' => __ ( 'Information about the way the malware payload is initially delivered, for example information about the email or web-site, vulnerability used, originating IP etc. Malware sample itself should be attached here.' ),
'types' => array ( 'md5' , 'sha1' , 'sha224' , 'sha256' , 'sha384' , 'sha512' , 'sha512/224' , 'sha512/256' , 'sha3-224' , 'sha3-256' , 'sha3-384' , 'sha3-512' , 'ssdeep' , 'imphash' , 'telfhash' , 'impfuzzy' , 'authentihash' , 'vhash' , 'pehash' , 'tlsh' , 'cdhash' , 'filename' , 'filename|md5' , 'filename|sha1' , 'filename|sha224' , 'filename|sha256' , 'filename|sha384' , 'filename|sha512' , 'filename|sha512/224' , 'filename|sha512/256' , 'filename|sha3-224' , 'filename|sha3-256' , 'filename|sha3-384' , 'filename|sha3-512' , 'filename|authentihash' , 'filename|vhash' , 'filename|ssdeep' , 'filename|tlsh' , 'filename|imphash' , 'filename|impfuzzy' , 'filename|pehash' , 'mac-address' , 'mac-eui-64' , 'ip-src' , 'ip-dst' , 'ip-dst|port' , 'ip-src|port' , 'hostname' , 'domain' , 'email' , 'email-src' , 'email-dst' , 'email-subject' , 'email-attachment' , 'email-body' , 'url' , 'user-agent' , 'AS' , 'pattern-in-file' , 'pattern-in-traffic' , 'filename-pattern' , 'stix2-pattern' , 'yara' , 'sigma' , 'mime-type' , 'attachment' , 'malware-sample' , 'link' , 'malware-type' , 'comment' , 'text' , 'hex' , 'vulnerability' , 'cpe' , 'weakness' , 'x509-fingerprint-sha1' , 'x509-fingerprint-md5' , 'x509-fingerprint-sha256' , 'ja3-fingerprint-md5' , 'jarm-fingerprint' , 'hassh-md5' , 'hasshserver-md5' , 'other' , 'hostname|port' , 'email-dst-display-name' , 'email-src-display-name' , 'email-header' , 'email-reply-to' , 'email-x-mailer' , 'email-mime-boundary' , 'email-thread-index' , 'email-message-id' , 'mobile-application-id' , 'chrome-extension-id' , 'whois-registrant-email' , 'anonymised' )
),
'Artifacts dropped' => array (
'desc' => __ ( 'Any artifact (files, registry keys etc.) dropped by the malware or other modifications to the system' ),
'types' => array ( 'md5' , 'sha1' , 'sha224' , 'sha256' , 'sha384' , 'sha512' , 'sha512/224' , 'sha512/256' , 'sha3-224' , 'sha3-256' , 'sha3-384' , 'sha3-512' , 'ssdeep' , 'imphash' , 'telfhash' , 'impfuzzy' , 'authentihash' , 'vhash' , 'cdhash' , 'filename' , 'filename|md5' , 'filename|sha1' , 'filename|sha224' , 'filename|sha256' , 'filename|sha384' , 'filename|sha512' , 'filename|sha512/224' , 'filename|sha512/256' , 'filename|sha3-224' , 'filename|sha3-256' , 'filename|sha3-384' , 'filename|sha3-512' , 'filename|authentihash' , 'filename|vhash' , 'filename|ssdeep' , 'filename|tlsh' , 'filename|imphash' , 'filename|impfuzzy' , 'filename|pehash' , 'regkey' , 'regkey|value' , 'pattern-in-file' , 'pattern-in-memory' , 'filename-pattern' , 'pdb' , 'stix2-pattern' , 'yara' , 'sigma' , 'attachment' , 'malware-sample' , 'named pipe' , 'mutex' , 'process-state' , 'windows-scheduled-task' , 'windows-service-name' , 'windows-service-displayname' , 'comment' , 'text' , 'hex' , 'x509-fingerprint-sha1' , 'x509-fingerprint-md5' , 'x509-fingerprint-sha256' , 'other' , 'cookie' , 'gene' , 'kusto-query' , 'mime-type' , 'anonymised' , 'pgp-public-key' , 'pgp-private-key' )
),
'Payload installation' => array (
'desc' => __ ( 'Info on where the malware gets installed in the system' ),
'formdesc' => __ ( 'Location where the payload was placed in the system and the way it was installed. For example, a filename|md5 type attribute can be added here like this: c:\\windows\\system32\\malicious.exe|41d8cd98f00b204e9800998ecf8427e.' ),
'types' => array ( 'md5' , 'sha1' , 'sha224' , 'sha256' , 'sha384' , 'sha512' , 'sha512/224' , 'sha512/256' , 'sha3-224' , 'sha3-256' , 'sha3-384' , 'sha3-512' , 'ssdeep' , 'imphash' , 'telfhash' , 'impfuzzy' , 'authentihash' , 'vhash' , 'pehash' , 'tlsh' , 'cdhash' , 'filename' , 'filename|md5' , 'filename|sha1' , 'filename|sha224' , 'filename|sha256' , 'filename|sha384' , 'filename|sha512' , 'filename|sha512/224' , 'filename|sha512/256' , 'filename|sha3-224' , 'filename|sha3-256' , 'filename|sha3-384' , 'filename|sha3-512' , 'filename|authentihash' , 'filename|vhash' , 'filename|ssdeep' , 'filename|tlsh' , 'filename|imphash' , 'filename|impfuzzy' , 'filename|pehash' , 'pattern-in-file' , 'pattern-in-traffic' , 'pattern-in-memory' , 'filename-pattern' , 'stix2-pattern' , 'yara' , 'sigma' , 'vulnerability' , 'cpe' , 'weakness' , 'attachment' , 'malware-sample' , 'malware-type' , 'comment' , 'text' , 'hex' , 'x509-fingerprint-sha1' , 'x509-fingerprint-md5' , 'x509-fingerprint-sha256' , 'mobile-application-id' , 'chrome-extension-id' , 'other' , 'mime-type' , 'anonymised' )
),
'Persistence mechanism' => array (
'desc' => __ ( 'Mechanisms used by the malware to start at boot' ),
'formdesc' => __ ( 'Mechanisms used by the malware to start at boot. This could be a registry key, legitimate driver modification, LNK file in startup' ),
'types' => array ( 'filename' , 'regkey' , 'regkey|value' , 'comment' , 'text' , 'other' , 'hex' , 'anonymised' )
),
'Network activity' => array (
'desc' => __ ( 'Information about network traffic generated by the malware' ),
2021-02-19 18:16:33 +01:00
'types' => array ( 'ip-src' , 'ip-dst' , 'ip-dst|port' , 'ip-src|port' , 'port' , 'hostname' , 'domain' , 'domain|ip' , 'mac-address' , 'mac-eui-64' , 'email' , 'email-dst' , 'email-src' , 'eppn' , 'url' , 'uri' , 'user-agent' , 'http-method' , 'AS' , 'snort' , 'pattern-in-file' , 'filename-pattern' , 'stix2-pattern' , 'pattern-in-traffic' , 'attachment' , 'comment' , 'text' , 'x509-fingerprint-md5' , 'x509-fingerprint-sha1' , 'x509-fingerprint-sha256' , 'ja3-fingerprint-md5' , 'jarm-fingerprint' , 'hassh-md5' , 'hasshserver-md5' , 'other' , 'hex' , 'cookie' , 'hostname|port' , 'bro' , 'zeek' , 'anonymised' , 'community-id' , 'email-subject' , 'favicon-mmh3' , 'dkim' , 'dkim-signature' )
2021-01-09 21:12:57 +01:00
),
'Payload type' => array (
'desc' => __ ( 'Information about the final payload(s)' ),
'formdesc' => __ ( 'Information about the final payload(s). Can contain a function of the payload, e.g. keylogger, RAT, or a name if identified, such as Poison Ivy.' ),
'types' => array ( 'comment' , 'text' , 'other' , 'anonymised' )
),
'Attribution' => array (
'desc' => __ ( 'Identification of the group, organisation, or country behind the attack' ),
'types' => array ( 'threat-actor' , 'campaign-name' , 'campaign-id' , 'whois-registrant-phone' , 'whois-registrant-email' , 'whois-registrant-name' , 'whois-registrant-org' , 'whois-registrar' , 'whois-creation-date' , 'comment' , 'text' , 'x509-fingerprint-sha1' , 'x509-fingerprint-md5' , 'x509-fingerprint-sha256' , 'other' , 'dns-soa-email' , 'anonymised' , 'email' )
),
'External analysis' => array (
'desc' => __ ( 'Any other result from additional analysis of the malware like tools output' ),
'formdesc' => __ ( 'Any other result from additional analysis of the malware like tools output Examples: pdf-parser output, automated sandbox analysis, reverse engineering report.' ),
'types' => array ( 'md5' , 'sha1' , 'sha256' , 'sha3-224' , 'sha3-256' , 'sha3-384' , 'sha3-512' , 'filename' , 'filename|md5' , 'filename|sha1' , 'filename|sha256' , 'filename|sha3-224' , 'filename|sha3-256' , 'filename|sha3-384' , 'filename|sha3-512' , 'ip-src' , 'ip-dst' , 'ip-dst|port' , 'ip-src|port' , 'mac-address' , 'mac-eui-64' , 'hostname' , 'domain' , 'domain|ip' , 'url' , 'user-agent' , 'regkey' , 'regkey|value' , 'AS' , 'snort' , 'bro' , 'zeek' , 'pattern-in-file' , 'pattern-in-traffic' , 'pattern-in-memory' , 'filename-pattern' , 'vulnerability' , 'cpe' , 'weakness' , 'attachment' , 'malware-sample' , 'link' , 'comment' , 'text' , 'x509-fingerprint-sha1' , 'x509-fingerprint-md5' , 'x509-fingerprint-sha256' , 'ja3-fingerprint-md5' , 'jarm-fingerprint' , 'hassh-md5' , 'hasshserver-md5' , 'github-repository' , 'other' , 'cortex' , 'anonymised' , 'community-id' )
),
'Financial fraud' => array (
'desc' => __ ( 'Financial Fraud indicators' ),
'formdesc' => __ ( 'Financial Fraud indicators, for example: IBAN Numbers, BIC codes, Credit card numbers, etc.' ),
'types' => array ( 'btc' , 'dash' , 'xmr' , 'iban' , 'bic' , 'bank-account-nr' , 'aba-rtn' , 'bin' , 'cc-number' , 'prtn' , 'phone-number' , 'comment' , 'text' , 'other' , 'hex' , 'anonymised' ),
),
'Support Tool' => array (
'desc' => __ ( 'Tools supporting analysis or detection of the event' ),
'types' => array ( 'link' , 'text' , 'attachment' , 'comment' , 'other' , 'hex' , 'anonymised' )
),
'Social network' => array (
'desc' => __ ( 'Social networks and platforms' ),
// email-src and email-dst or should we go with a new email type that is neither / both?
'types' => array ( 'github-username' , 'github-repository' , 'github-organisation' , 'jabber-id' , 'twitter-id' , 'email' , 'email-src' , 'email-dst' , 'eppn' , 'comment' , 'text' , 'other' , 'whois-registrant-email' , 'anonymised' , 'pgp-public-key' , 'pgp-private-key' )
),
'Person' => array (
'desc' => __ ( 'A human being - natural person' ),
2021-02-22 12:36:59 +01:00
'types' => array ( 'first-name' , 'middle-name' , 'last-name' , 'full-name' , 'date-of-birth' , 'place-of-birth' , 'gender' , 'passport-number' , 'passport-country' , 'passport-expiration' , 'redress-number' , 'nationality' , 'visa-number' , 'issue-date-of-the-visa' , 'primary-residence' , 'country-of-residence' , 'special-service-request' , 'frequent-flyer-number' , 'travel-details' , 'payment-details' , 'place-port-of-original-embarkation' , 'place-port-of-clearance' , 'place-port-of-onward-foreign-destination' , 'passenger-name-record-locator-number' , 'comment' , 'text' , 'other' , 'phone-number' , 'identity-card-number' , 'anonymised' , 'email' , 'pgp-public-key' , 'pgp-private-key' )
2021-01-09 21:12:57 +01:00
),
'Other' => array (
'desc' => __ ( 'Attributes that are not part of any other category or are meant to be used as a component in MISP objects in the future' ),
'types' => array ( 'comment' , 'text' , 'other' , 'size-in-bytes' , 'counter' , 'datetime' , 'cpe' , 'port' , 'float' , 'hex' , 'phone-number' , 'boolean' , 'anonymised' , 'pgp-public-key' , 'pgp-private-key' )
)
);
}
/**
* Generate just when really need
* NOTE WHEN MODIFYING : please ensure to run the script 'tools/gen_misp_types_categories.py' to update the new definitions everywhere . ( docu , website , RFC , ... )
* @ return array []
*/
private function generateTypeDefinitions ()
{
return array (
'md5' => array ( 'desc' => __ ( 'A checksum in md5 format' ), 'formdesc' => __ ( " You are encouraged to use filename|md5 instead. A checksum in md5 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha1' => array ( 'desc' => __ ( 'A checksum in sha1 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha1 instead. A checksum in sha1 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha256' => array ( 'desc' => __ ( 'A checksum in sha256 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha256 instead. A checksum in sha256 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename' => array ( 'desc' => __ ( 'Filename' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'pdb' => array ( 'desc' => __ ( 'Microsoft Program database (PDB) path information' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 0 ),
'filename|md5' => array ( 'desc' => __ ( 'A filename and an md5 hash separated by a |' ), 'formdesc' => __ ( " A filename and an md5 hash separated by a | (no spaces) " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha1' => array ( 'desc' => __ ( 'A filename and an sha1 hash separated by a |' ), 'formdesc' => __ ( " A filename and an sha1 hash separated by a | (no spaces) " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha256' => array ( 'desc' => __ ( 'A filename and an sha256 hash separated by a |' ), 'formdesc' => __ ( " A filename and an sha256 hash separated by a | (no spaces) " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'ip-src' => array ( 'desc' => __ ( " A source IP address of the attacker " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'ip-dst' => array ( 'desc' => __ ( 'A destination IP address of the attacker or C&C server' ), 'formdesc' => __ ( " A destination IP address of the attacker or C&C server. Also set the IDS flag on when this IP is hardcoded in malware " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'hostname' => array ( 'desc' => __ ( 'A full host/dnsname of an attacker' ), 'formdesc' => __ ( " A full host/dnsname of an attacker. Also set the IDS flag on when this hostname is hardcoded in malware " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'domain' => array ( 'desc' => __ ( 'A domain name used in the malware' ), 'formdesc' => __ ( " A domain name used in the malware. Use this instead of hostname when the upper domain is important or can be used to create links between events. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'domain|ip' => array ( 'desc' => __ ( 'A domain name and its IP address (as found in DNS lookup) separated by a |' ), 'formdesc' => __ ( " A domain name and its IP address (as found in DNS lookup) separated by a | (no spaces) " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'email' => array ( 'desc' => ( 'An e-mail address' ), 'default_category' => 'Social network' , 'to_ids' => 1 ),
'email-src' => array ( 'desc' => __ ( " The source email address. Used to describe the sender when describing an e-mail. " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'eppn' => array ( 'desc' => __ ( " eduPersonPrincipalName - eppn - the NetId of the person for the purposes of inter-institutional authentication. Should be stored in the form of user@univ.edu, where univ.edu is the name of the local security domain. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'email-dst' => array ( 'desc' => __ ( " The destination email address. Used to describe the recipient when describing an e-mail. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'email-subject' => array ( 'desc' => __ ( " The subject of the email " ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'email-attachment' => array ( 'desc' => __ ( " File name of the email attachment. " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'email-body' => array ( 'desc' => __ ( 'Email body' ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'float' => array ( 'desc' => __ ( " A floating point value. " ), 'default_category' => 'Other' , 'to_ids' => 0 ),
'git-commit-id' => array ( 'desc' => __ ( " A git commit ID. " ), 'default_category' => 'Internal reference' , 'to_ids' => 0 ),
'url' => array ( 'desc' => __ ( 'url' ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'http-method' => array ( 'desc' => __ ( " HTTP method used by the malware (e.g. POST, GET, ...). " ), 'default_category' => 'Network activity' , 'to_ids' => 0 ),
'user-agent' => array ( 'desc' => __ ( " The user-agent used by the malware in the HTTP request. " ), 'default_category' => 'Network activity' , 'to_ids' => 0 ),
'ja3-fingerprint-md5' => array ( 'desc' => __ ( " JA3 is a method for creating SSL/TLS client fingerprints that should be easy to produce on any platform and can be easily shared for threat intelligence. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'jarm-fingerprint' => array ( 'desc' => __ ( " JARM is a method for creating SSL/TLS server fingerprints. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'favicon-mmh3' => array ( 'desc' => __ ( " favicon-mmh3 is the murmur3 hash of a favicon as used in Shodan. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'hassh-md5' => array ( 'desc' => __ ( " hassh is a network fingerprinting standard which can be used to identify specific Client SSH implementations. The fingerprints can be easily stored, searched and shared in the form of an MD5 fingerprint. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'hasshserver-md5' => array ( 'desc' => __ ( " hasshServer is a network fingerprinting standard which can be used to identify specific Server SSH implementations. The fingerprints can be easily stored, searched and shared in the form of an MD5 fingerprint. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'regkey' => array ( 'desc' => __ ( " Registry key or value " ), 'default_category' => 'Persistence mechanism' , 'to_ids' => 1 ),
'regkey|value' => array ( 'desc' => __ ( " Registry value + data separated by | " ), 'default_category' => 'Persistence mechanism' , 'to_ids' => 1 ),
'AS' => array ( 'desc' => __ ( 'Autonomous system' ), 'default_category' => 'Network activity' , 'to_ids' => 0 ),
'snort' => array ( 'desc' => __ ( 'An IDS rule in Snort rule-format' ), 'formdesc' => __ ( " An IDS rule in Snort rule-format. This rule will be automatically rewritten in the NIDS exports. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'bro' => array ( 'desc' => __ ( 'An NIDS rule in the Bro rule-format' ), 'formdesc' => __ ( " An NIDS rule in the Bro rule-format. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'zeek' => array ( 'desc' => __ ( 'An NIDS rule in the Zeek rule-format' ), 'formdesc' => __ ( " An NIDS rule in the Zeek rule-format. " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'community-id' => array ( 'desc' => __ ( 'a community ID flow hashing algorithm to map multiple traffic monitors into common flow id' ), 'formdesc' => __ ( " a community ID flow hashing algorithm to map multiple traffic monitors into common flow id " ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'pattern-in-file' => array ( 'desc' => __ ( 'Pattern in file that identifies the malware' ), 'default_category' => 'Payload installation' , 'to_ids' => 1 ),
'pattern-in-traffic' => array ( 'desc' => __ ( 'Pattern in network traffic that identifies the malware' ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'pattern-in-memory' => array ( 'desc' => __ ( 'Pattern in memory dump that identifies the malware' ), 'default_category' => 'Payload installation' , 'to_ids' => 1 ),
2021-08-31 15:06:02 +02:00
'filename-pattern' => array ( 'desc' => __ ( 'A pattern in the name of a file' ), 'default_category' => 'Payload installation' , 'to_ids' => 1 ),
2021-01-09 21:12:57 +01:00
'pgp-public-key' => array ( 'desc' => __ ( 'A PGP public key' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'pgp-private-key' => array ( 'desc' => __ ( 'A PGP private key' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'yara' => array ( 'desc' => __ ( 'Yara signature' ), 'default_category' => 'Payload installation' , 'to_ids' => 1 ),
'stix2-pattern' => array ( 'desc' => __ ( 'STIX 2 pattern' ), 'default_category' => 'Payload installation' , 'to_ids' => 1 ),
'sigma' => array ( 'desc' => __ ( 'Sigma - Generic Signature Format for SIEM Systems' ), 'default_category' => 'Payload installation' , 'to_ids' => 1 ),
'gene' => array ( 'desc' => __ ( 'GENE - Go Evtx sigNature Engine' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 0 ),
'kusto-query' => array ( 'desc' => __ ( 'Kusto query - Kusto from Microsoft Azure is a service for storing and running interactive analytics over Big Data.' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 0 ),
'mime-type' => array ( 'desc' => __ ( 'A media type (also MIME type and content type) is a two-part identifier for file formats and format contents transmitted on the Internet' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 0 ),
'identity-card-number' => array ( 'desc' => __ ( 'Identity card number' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'cookie' => array ( 'desc' => __ ( 'HTTP cookie as often stored on the user web client. This can include authentication cookie or session cookie.' ), 'default_category' => 'Network activity' , 'to_ids' => 0 ),
'vulnerability' => array ( 'desc' => __ ( 'A reference to the vulnerability used in the exploit' ), 'default_category' => 'External analysis' , 'to_ids' => 0 ),
'cpe' => array ( 'desc' => __ ( 'Common Platform Enumeration - structured naming scheme for information technology systems, software, and packages.' ), 'default_category' => 'External analysis' , 'to_ids' => 0 ),
'weakness' => array ( 'desc' => __ ( 'A reference to the weakness used in the exploit' ), 'default_category' => 'External analysis' , 'to_ids' => 0 ),
'attachment' => array ( 'desc' => __ ( 'Attachment with external information' ), 'formdesc' => __ ( " Please upload files using the <em>Upload Attachment</em> button. " ), 'default_category' => 'External analysis' , 'to_ids' => 0 ),
'malware-sample' => array ( 'desc' => __ ( 'Attachment containing encrypted malware sample' ), 'formdesc' => __ ( " Please upload files using the <em>Upload Attachment</em> button. " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'link' => array ( 'desc' => __ ( 'Link to an external information' ), 'default_category' => 'External analysis' , 'to_ids' => 0 ),
'comment' => array ( 'desc' => __ ( 'Comment or description in a human language' ), 'formdesc' => __ ( 'Comment or description in a human language. This will not be correlated with other attributes' ), 'default_category' => 'Other' , 'to_ids' => 0 ),
'text' => array ( 'desc' => __ ( 'Name, ID or a reference' ), 'default_category' => 'Other' , 'to_ids' => 0 ),
'hex' => array ( 'desc' => __ ( 'A value in hexadecimal format' ), 'default_category' => 'Other' , 'to_ids' => 0 ),
'other' => array ( 'desc' => __ ( 'Other attribute' ), 'default_category' => 'Other' , 'to_ids' => 0 ),
'named pipe' => array ( 'desc' => __ ( 'Named pipe, use the format \\.\pipe\<PipeName>' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 0 ),
'mutex' => array ( 'desc' => __ ( 'Mutex, use the format \BaseNamedObjects\<Mutex>' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 1 ),
'process-state' => array ( 'desc' => __ ( 'State of a process' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 0 ),
'target-user' => array ( 'desc' => __ ( 'Attack Targets Username(s)' ), 'default_category' => 'Targeting data' , 'to_ids' => 0 ),
'target-email' => array ( 'desc' => __ ( 'Attack Targets Email(s)' ), 'default_category' => 'Targeting data' , 'to_ids' => 0 ),
'target-machine' => array ( 'desc' => __ ( 'Attack Targets Machine Name(s)' ), 'default_category' => 'Targeting data' , 'to_ids' => 0 ),
'target-org' => array ( 'desc' => __ ( 'Attack Targets Department or Organization(s)' ), 'default_category' => 'Targeting data' , 'to_ids' => 0 ),
'target-location' => array ( 'desc' => __ ( 'Attack Targets Physical Location(s)' ), 'default_category' => 'Targeting data' , 'to_ids' => 0 ),
'target-external' => array ( 'desc' => __ ( 'External Target Organizations Affected by this Attack' ), 'default_category' => 'Targeting data' , 'to_ids' => 0 ),
'btc' => array ( 'desc' => __ ( 'Bitcoin Address' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'dash' => array ( 'desc' => __ ( 'Dash Address' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'xmr' => array ( 'desc' => __ ( 'Monero Address' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'iban' => array ( 'desc' => __ ( 'International Bank Account Number' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'bic' => array ( 'desc' => __ ( 'Bank Identifier Code Number also known as SWIFT-BIC, SWIFT code or ISO 9362 code' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'bank-account-nr' => array ( 'desc' => __ ( 'Bank account number without any routing number' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'aba-rtn' => array ( 'desc' => __ ( 'ABA routing transit number' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'bin' => array ( 'desc' => __ ( 'Bank Identification Number' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'cc-number' => array ( 'desc' => __ ( 'Credit-Card Number' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'prtn' => array ( 'desc' => __ ( 'Premium-Rate Telephone Number' ), 'default_category' => 'Financial fraud' , 'to_ids' => 1 ),
'phone-number' => array ( 'desc' => __ ( 'Telephone Number' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'threat-actor' => array ( 'desc' => __ ( 'A string identifying the threat actor' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
'campaign-name' => array ( 'desc' => __ ( 'Associated campaign name' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
'campaign-id' => array ( 'desc' => __ ( 'Associated campaign ID' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
'malware-type' => array ( 'desc' => '' , 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'uri' => array ( 'desc' => __ ( 'Uniform Resource Identifier' ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'authentihash' => array ( 'desc' => __ ( 'Authenticode executable signature hash' ), 'formdesc' => __ ( " You are encouraged to use filename|authentihash instead. Authenticode executable signature hash, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'vhash' => array ( 'desc' => __ ( 'A VirusTotal checksum' ), 'formdesc' => __ ( " You are encouraged to use filename|vhash instead. A checksum from VirusTotal, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'ssdeep' => array ( 'desc' => __ ( 'A checksum in ssdeep format' ), 'formdesc' => __ ( " You are encouraged to use filename|ssdeep instead. A checksum in the SSDeep format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'imphash' => array ( 'desc' => __ ( 'Import hash - a hash created based on the imports in the sample.' ), 'formdesc' => __ ( " You are encouraged to use filename|imphash instead. A hash created based on the imports in the sample, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'telfhash' => array ( 'desc' => __ ( 'telfhash is symbol hash for ELF files, just like imphash is imports hash for PE files.' ), 'formdesc' => __ ( " You are encouraged to use a file object with telfash " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'pehash' => array ( 'desc' => __ ( 'PEhash - a hash calculated based of certain pieces of a PE executable file' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'impfuzzy' => array ( 'desc' => __ ( 'A fuzzy hash of import table of Portable Executable format' ), 'formdesc' => __ ( " You are encouraged to use filename|impfuzzy instead. A fuzzy hash created based on the imports in the sample, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha224' => array ( 'desc' => __ ( 'A checksum in sha-224 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha224 instead. A checksum in sha224 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha384' => array ( 'desc' => __ ( 'A checksum in sha-384 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha384 instead. A checksum in sha384 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha512' => array ( 'desc' => __ ( 'A checksum in sha-512 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha512 instead. A checksum in sha512 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha512/224' => array ( 'desc' => __ ( 'A checksum in the sha-512/224 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha512/224 instead. A checksum in sha512/224 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha512/256' => array ( 'desc' => __ ( 'A checksum in the sha-512/256 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha512/256 instead. A checksum in sha512/256 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha3-224' => array ( 'desc' => __ ( 'A checksum in sha3-224 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha3-224 instead. A checksum in sha3-224 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha3-256' => array ( 'desc' => __ ( 'A checksum in sha3-256 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha3-256 instead. A checksum in sha3-256 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha3-384' => array ( 'desc' => __ ( 'A checksum in sha3-384 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha3-384 instead. A checksum in sha3-384 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'sha3-512' => array ( 'desc' => __ ( 'A checksum in sha3-512 format' ), 'formdesc' => __ ( " You are encouraged to use filename|sha3-512 instead. A checksum in sha3-512 format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'tlsh' => array ( 'desc' => __ ( 'A checksum in the Trend Micro Locality Sensitive Hash format' ), 'formdesc' => __ ( " You are encouraged to use filename|tlsh instead. A checksum in the Trend Micro Locality Sensitive Hash format, only use this if you don't know the correct filename " ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'cdhash' => array ( 'desc' => __ ( 'An Apple Code Directory Hash, identifying a code-signed Mach-O executable file' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|authentihash' => array ( 'desc' => __ ( 'A checksum in md5 format' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|vhash' => array ( 'desc' => __ ( 'A filename and a VirusTotal hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|ssdeep' => array ( 'desc' => __ ( 'A checksum in ssdeep format' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|imphash' => array ( 'desc' => __ ( 'Import hash - a hash created based on the imports in the sample.' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|impfuzzy' => array ( 'desc' => __ ( 'Import fuzzy hash - a fuzzy hash created based on the imports in the sample.' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|pehash' => array ( 'desc' => __ ( 'A filename and a PEhash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha224' => array ( 'desc' => __ ( 'A filename and a sha-224 hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha384' => array ( 'desc' => __ ( 'A filename and a sha-384 hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha512' => array ( 'desc' => __ ( 'A filename and a sha-512 hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha512/224' => array ( 'desc' => __ ( 'A filename and a sha-512/224 hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha512/256' => array ( 'desc' => __ ( 'A filename and a sha-512/256 hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha3-224' => array ( 'desc' => __ ( 'A filename and an sha3-224 hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha3-256' => array ( 'desc' => __ ( 'A filename and an sha3-256 hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha3-384' => array ( 'desc' => __ ( 'A filename and an sha3-384 hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|sha3-512' => array ( 'desc' => __ ( 'A filename and an sha3-512 hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'filename|tlsh' => array ( 'desc' => __ ( 'A filename and a Trend Micro Locality Sensitive Hash separated by a |' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'windows-scheduled-task' => array ( 'desc' => __ ( 'A scheduled task in windows' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 0 ),
'windows-service-name' => array ( 'desc' => __ ( 'A windows service name. This is the name used internally by windows. Not to be confused with the windows-service-displayname.' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 0 ),
'windows-service-displayname' => array ( 'desc' => __ ( 'A windows service\'s displayname, not to be confused with the windows-service-name. This is the name that applications will generally display as the service\'s name in applications.' ), 'default_category' => 'Artifacts dropped' , 'to_ids' => 0 ),
'whois-registrant-email' => array ( 'desc' => __ ( 'The e-mail of a domain\'s registrant, obtained from the WHOIS information.' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
'whois-registrant-phone' => array ( 'desc' => __ ( 'The phone number of a domain\'s registrant, obtained from the WHOIS information.' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
'whois-registrant-name' => array ( 'desc' => __ ( 'The name of a domain\'s registrant, obtained from the WHOIS information.' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
'whois-registrant-org' => array ( 'desc' => __ ( 'The org of a domain\'s registrant, obtained from the WHOIS information.' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
'whois-registrar' => array ( 'desc' => __ ( 'The registrar of the domain, obtained from the WHOIS information.' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
'whois-creation-date' => array ( 'desc' => __ ( 'The date of domain\'s creation, obtained from the WHOIS information.' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
// 'targeted-threat-index' => array('desc' => ''), // currently not mapped!
// 'mailslot' => array('desc' => 'MailSlot interprocess communication'), // currently not mapped!
// 'pipe' => array('desc' => 'Pipeline (for named pipes use the attribute type "named pipe")'), // currently not mapped!
// 'ssl-cert-attributes' => array('desc' => 'SSL certificate attributes'), // currently not mapped!
'x509-fingerprint-sha1' => array ( 'desc' => __ ( 'X509 fingerprint in SHA-1 format' ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'x509-fingerprint-md5' => array ( 'desc' => __ ( 'X509 fingerprint in MD5 format' ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'x509-fingerprint-sha256' => array ( 'desc' => __ ( 'X509 fingerprint in SHA-256 format' ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'dns-soa-email' => array ( 'desc' => __ ( 'RFC1035 mandates that DNS zones should have a SOA (Statement Of Authority) record that contains an email address where a PoC for the domain could be contacted. This can sometimes be used for attribution/linkage between different domains even if protected by whois privacy' ), 'default_category' => 'Attribution' , 'to_ids' => 0 ),
'size-in-bytes' => array ( 'desc' => __ ( 'Size expressed in bytes' ), 'default_category' => 'Other' , 'to_ids' => 0 ),
'counter' => array ( 'desc' => __ ( 'An integer counter, generally to be used in objects' ), 'default_category' => 'Other' , 'to_ids' => 0 ),
'datetime' => array ( 'desc' => __ ( 'Datetime in the ISO 8601 format' ), 'default_category' => 'Other' , 'to_ids' => 0 ),
'port' => array ( 'desc' => __ ( 'Port number' ), 'default_category' => 'Network activity' , 'to_ids' => 0 ),
'ip-dst|port' => array ( 'desc' => __ ( 'IP destination and port number separated by a |' ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'ip-src|port' => array ( 'desc' => __ ( 'IP source and port number separated by a |' ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'hostname|port' => array ( 'desc' => __ ( 'Hostname and port number separated by a |' ), 'default_category' => 'Network activity' , 'to_ids' => 1 ),
'mac-address' => array ( 'desc' => __ ( 'Mac address' ), 'default_category' => 'Network activity' , 'to_ids' => 0 ),
'mac-eui-64' => array ( 'desc' => __ ( 'Mac EUI-64 address' ), 'default_category' => 'Network activity' , 'to_ids' => 0 ),
// verify IDS flag defaults for these
'email-dst-display-name' => array ( 'desc' => __ ( 'Email destination display name' ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'email-src-display-name' => array ( 'desc' => __ ( 'Email source display name' ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'email-header' => array ( 'desc' => __ ( 'Email header' ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'email-reply-to' => array ( 'desc' => __ ( 'Email reply to header' ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'email-x-mailer' => array ( 'desc' => __ ( 'Email x-mailer header' ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'email-mime-boundary' => array ( 'desc' => __ ( 'The email mime boundary separating parts in a multipart email' ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'email-thread-index' => array ( 'desc' => __ ( 'The email thread index header' ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'email-message-id' => array ( 'desc' => __ ( 'The email message ID' ), 'default_category' => 'Payload delivery' , 'to_ids' => 0 ),
'github-username' => array ( 'desc' => __ ( 'A github user name' ), 'default_category' => 'Social network' , 'to_ids' => 0 ),
'github-repository' => array ( 'desc' => __ ( 'A github repository' ), 'default_category' => 'Social network' , 'to_ids' => 0 ),
'github-organisation' => array ( 'desc' => __ ( 'A github organisation' ), 'default_category' => 'Social network' , 'to_ids' => 0 ),
'jabber-id' => array ( 'desc' => __ ( 'Jabber ID' ), 'default_category' => 'Social network' , 'to_ids' => 0 ),
'twitter-id' => array ( 'desc' => __ ( 'Twitter ID' ), 'default_category' => 'Social network' , 'to_ids' => 0 ),
2021-02-19 18:16:33 +01:00
'dkim' => array ( 'desc' => __ ( 'DKIM public key' ), 'default_category' => 'Network activity' , 'to_ids' => 0 ),
'dkim-signature' => array ( 'desc' => __ ( 'DKIM signature' ), 'default_category' => 'Network activity' , 'to_ids' => 0 ),
2021-01-09 21:12:57 +01:00
'first-name' => array ( 'desc' => __ ( 'First name of a natural person' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'middle-name' => array ( 'desc' => __ ( 'Middle name of a natural person' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'last-name' => array ( 'desc' => __ ( 'Last name of a natural person' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
2021-02-22 12:36:59 +01:00
'full-name' => array ( 'desc' => __ ( 'Full name of a natural person' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
2021-01-09 21:12:57 +01:00
'date-of-birth' => array ( 'desc' => __ ( 'Date of birth of a natural person (in YYYY-MM-DD format)' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'place-of-birth' => array ( 'desc' => __ ( 'Place of birth of a natural person' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'gender' => array ( 'desc' => __ ( 'The gender of a natural person (Male, Female, Other, Prefer not to say)' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'passport-number' => array ( 'desc' => __ ( 'The passport number of a natural person' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'passport-country' => array ( 'desc' => __ ( 'The country in which the passport was issued' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'passport-expiration' => array ( 'desc' => __ ( 'The expiration date of a passport' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'redress-number' => array ( 'desc' => __ ( 'The Redress Control Number is the record identifier for people who apply for redress through the DHS Travel Redress Inquiry Program (DHS TRIP). DHS TRIP is for travelers who have been repeatedly identified for additional screening and who want to file an inquiry to have erroneous information corrected in DHS systems' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'nationality' => array ( 'desc' => __ ( 'The nationality of a natural person' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'visa-number' => array ( 'desc' => __ ( 'Visa number' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'issue-date-of-the-visa' => array ( 'desc' => __ ( 'The date on which the visa was issued' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'primary-residence' => array ( 'desc' => __ ( 'The primary residence of a natural person' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'country-of-residence' => array ( 'desc' => __ ( 'The country of residence of a natural person' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'special-service-request' => array ( 'desc' => __ ( 'A Special Service Request is a function to an airline to provide a particular facility for A Passenger or passengers. ' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'frequent-flyer-number' => array ( 'desc' => __ ( 'The frequent flyer number of a passenger' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
// Do we really need remarks? Or just use comment/text for this?
//'remarks' => array('desc' => '', 'default_category' => 'Person', 'to_ids' => 0),
'travel-details' => array ( 'desc' => __ ( 'Travel details' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'payment-details' => array ( 'desc' => __ ( 'Payment details' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
2021-03-25 13:44:54 +01:00
'place-port-of-original-embarkation' => array ( 'desc' => __ ( 'The original port of embarkation' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
2021-01-09 21:12:57 +01:00
'place-port-of-clearance' => array ( 'desc' => __ ( 'The port of clearance' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'place-port-of-onward-foreign-destination' => array ( 'desc' => __ ( 'A Port where the passenger is transiting to' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'passenger-name-record-locator-number' => array ( 'desc' => __ ( 'The Passenger Name Record Locator is a key under which the reservation for a trip is stored in the system. The PNR contains, among other data, the name, flight segments and address of the passenger. It is defined by a combination of five or six letters and numbers.' ), 'default_category' => 'Person' , 'to_ids' => 0 ),
'mobile-application-id' => array ( 'desc' => __ ( 'The application id of a mobile application' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'chrome-extension-id' => array ( 'desc' => __ ( 'Chrome extension id' ), 'default_category' => 'Payload delivery' , 'to_ids' => 1 ),
'cortex' => array ( 'desc' => __ ( 'Cortex analysis result' ), 'default_category' => 'External analysis' , 'to_ids' => 0 ),
'boolean' => array ( 'desc' => __ ( 'Boolean value - to be used in objects' ), 'default_category' => 'Other' , 'to_ids' => 0 ),
'anonymised' => array ( 'desc' => __ ( 'Anonymised value - described with the anonymisation object via a relationship' ), 'formdesc' => __ ( 'Anonymised value - described with the anonymisation object via a relationship.' ), 'default_category' => 'Other' , 'to_ids' => 0 )
// Not convinced about this.
//'url-regex' => array('desc' => '', 'default_category' => 'Person', 'to_ids' => 0),
);
}
2012-03-15 15:06:45 +01:00
}