Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend/frontend] Threat actor group should not be part-of threat actor individual (#9576) #9649

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import ListItem from '@mui/material/ListItem';
import ListItemIcon from '@mui/material/ListItemIcon';
import { BullseyeArrow, ArmFlexOutline, DramaMasks } from 'mdi-material-ui';
import ListItemText from '@mui/material/ListItemText';
import Tooltip from '@mui/material/Tooltip';
import ExpandableMarkdown from '../../../../components/ExpandableMarkdown';
import inject18n from '../../../../components/i18n';
import ItemOpenVocab from '../../../../components/ItemOpenVocab';
import FieldOrEmpty from '../../../../components/FieldOrEmpty';
import ImageCarousel from '../../../../components/ImageCarousel';
import ThreatActorGroupLocation from './ThreatActorGroupLocation';
import { truncate } from '../../../../utils/String';

const styles = (theme) => ({
paper: {
Expand Down Expand Up @@ -200,7 +202,11 @@ class ThreatActorGroupDetailsComponent extends Component {
</ListItemIcon>
<ListItemText
primary={
<pre className={classes.smallPre}>{goal}</pre>
<pre className={classes.smallPre}>
<Tooltip title={goal}>
{truncate(goal, 12)}
</Tooltip>
</pre>
}
/>
</ListItem>
Expand Down
26 changes: 0 additions & 26 deletions opencti-platform/opencti-graphql/src/database/stix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,21 +824,6 @@ export const stixCoreRelationshipsMapping: RelationshipMappings = {
[`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_MALWARE}`]: [
{ name: RELATION_USES, type: REL_BUILT_IN }
],
[`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_THREAT_ACTOR}`]: [
{ name: RELATION_PART_OF, type: REL_NEW },
{ name: RELATION_COOPERATES_WITH, type: REL_NEW },
{ name: RELATION_DERIVED_FROM, type: REL_BUILT_IN }
],
[`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_THREAT_ACTOR_GROUP}`]: [
{ name: RELATION_PART_OF, type: REL_NEW },
{ name: RELATION_COOPERATES_WITH, type: REL_NEW },
{ name: RELATION_DERIVED_FROM, type: REL_BUILT_IN }
],
[`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL}`]: [
{ name: RELATION_PART_OF, type: REL_NEW },
{ name: RELATION_COOPERATES_WITH, type: REL_NEW },
{ name: RELATION_DERIVED_FROM, type: REL_BUILT_IN }
labo-flg marked this conversation as resolved.
Show resolved Hide resolved
],
[`${ENTITY_TYPE_THREAT_ACTOR}_${ENTITY_TYPE_TOOL}`]: [
{ name: RELATION_USES, type: REL_BUILT_IN }
],
Expand Down Expand Up @@ -910,18 +895,7 @@ export const stixCoreRelationshipsMapping: RelationshipMappings = {
[`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_MALWARE}`]: [
{ name: RELATION_USES, type: REL_BUILT_IN }
],
[`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_THREAT_ACTOR}`]: [
{ name: RELATION_PART_OF, type: REL_NEW },
{ name: RELATION_COOPERATES_WITH, type: REL_NEW },
{ name: RELATION_DERIVED_FROM, type: REL_BUILT_IN }
],
[`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_THREAT_ACTOR_GROUP}`]: [
{ name: RELATION_PART_OF, type: REL_NEW },
{ name: RELATION_COOPERATES_WITH, type: REL_NEW },
{ name: RELATION_DERIVED_FROM, type: REL_BUILT_IN }
],
[`${ENTITY_TYPE_THREAT_ACTOR_GROUP}_${ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL}`]: [
{ name: RELATION_PART_OF, type: REL_NEW },
{ name: RELATION_COOPERATES_WITH, type: REL_NEW },
{ name: RELATION_DERIVED_FROM, type: REL_BUILT_IN }
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { logApp } from '../config/conf';
import { elReindexByQueryForMigration, elUpdateByQueryForMigration, elDeleteByQueryForMigration } from '../database/engine';
import { READ_INDEX_STIX_CORE_RELATIONSHIPS, READ_RELATIONSHIPS_INDICES } from '../database/utils';
import { RELATION_RELATED_TO } from '../schema/stixCoreRelationship';
import { executionContext } from '../utils/access';

export const up = async (next) => {
const context = executionContext('migration');
logApp.info('[MIGRATION] Transform invalid relationships to "related-to"');

// Définition des relations valides avec direction
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in english please :)

const validRelations = [
{ fromType: 'Threat-Actor-Individual', toType: 'Threat-Actor-Group', relationType: 'part-of' },
{ fromType: 'Threat-Actor-Individual', toType: 'Threat-Actor-Group', relationType: 'employed-by' },
{ fromType: 'Threat-Actor-Individual', toType: 'Threat-Actor-Group', relationType: 'cooperates-with' },
{ fromType: 'Threat-Actor-Individual', toType: 'Threat-Actor-Group', relationType: 'derived-from' },
{ fromType: 'Threat-Actor-Individual', toType: 'Threat-Actor-Group', relationType: 'related-to' },
{ fromType: 'Threat-Actor-Individual', toType: 'Threat-Actor-Individual', relationType: 'known-as' },
{ fromType: 'Threat-Actor-Individual', toType: 'Threat-Actor-Individual', relationType: 'reports-to' },
{ fromType: 'Threat-Actor-Individual', toType: 'Threat-Actor-Individual', relationType: 'supports' },
{ fromType: 'Threat-Actor-Individual', toType: 'Threat-Actor-Group', relationType: 'supports' },
{ fromType: 'Threat-Actor-Group', toType: 'Threat-Actor-Individual', relationType: 'cooperates-with' },
{ fromType: 'Threat-Actor-Group', toType: 'Threat-Actor-Individual', relationType: 'derived-from' },
{ fromType: 'Threat-Actor-Group', toType: 'Threat-Actor-Individual', relationType: 'related-to' },
];

// Étape 1 : Identifier les relations invalides
const invalidRelationshipsQuery = {
bool: {
must_not: validRelations.map((relation) => ({
bool: {
must: [
{ term: { 'fromType.keyword': relation.fromType } },
{ term: { 'toType.keyword': relation.toType } },
{ term: { 'relationship_type.keyword': relation.relationType } },
],
},
})),
},
};
Comment on lines +28 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand. This query will give you all relationships that are NOT one of the validRelations above, am I right ?

If so, you would get also valid relationships not listed in your array, like a campaign targeting a country.

Can't you instead search for invalid relationship directly ?


// Étape 2 : Reindexer les relations invalides
const reindexInvalidRelationshipsSource = `
ctx._source.relationship_type = params.newRelType;
ctx._source.entity_type = params.newRelType;
ctx._source.parent_types = params.parentTypes;
for (connection in ctx._source.connections) {
connection.role = connection.role.replace(connection.role, "related-to_" + connection.role.split("_")[1]);
}
`;
const reindexInvalidRelationshipsQuery = {
source: {
index: READ_INDEX_STIX_CORE_RELATIONSHIPS,
query: invalidRelationshipsQuery,
},
dest: {
index: READ_INDEX_STIX_CORE_RELATIONSHIPS,
},
script: {
source: reindexInvalidRelationshipsSource,
params: {
newRelType: RELATION_RELATED_TO,
parentTypes: ['basic-relationship', 'stix-relationship', 'stix-core-relationship'],
},
},
};

logApp.info('[MIGRATION] Reindexing invalid relationships to "related-to"');
await elReindexByQueryForMigration(
'[MIGRATION] Reindexing invalid relationships',
null,
reindexInvalidRelationshipsQuery
);

// Étape 3 : Mise à jour des connexions dans les entités
const updateInvalidConnectionsSource = `
if (!params.validRelations.some(
r => r.fromType == ctx._source.fromType && r.toType == ctx._source.toType && r.relationType == ctx._source.relationship_type)) {
ctx._source.relationship_type = params.newRelType;
ctx._source.entity_type = params.newRelType;
for (connection in ctx._source.connections) {
connection.role = connection.role.replace(connection.role, "related-to_" + connection.role.split("_")[1]);
}
}
`;
const updateInvalidConnectionsQuery = {
script: {
source: updateInvalidConnectionsSource,
params: { validRelations, newRelType: RELATION_RELATED_TO },
},
query: invalidRelationshipsQuery,
};

logApp.info('[MIGRATION] Updating invalid relationships in entities');
await elUpdateByQueryForMigration(
'[MIGRATION] Updating invalid relationships in entities',
[READ_RELATIONSHIPS_INDICES],
updateInvalidConnectionsQuery
);

// Étape 4 : Suppression des relations invalides d'origine
logApp.info('[MIGRATION] Deleting old invalid relationships');
await elDeleteByQueryForMigration(
'[MIGRATION] Deleting invalid relationships',
[READ_INDEX_STIX_CORE_RELATIONSHIPS],
{
query: invalidRelationshipsQuery,
}
);

logApp.info('[MIGRATION] Transform invalid relationships to "related-to" completed');
next();
};

export const down = async (next) => {
next();
};
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,12 @@ const THREAT_ACTOR_INDIVIDUAL_DEFINITION: ModuleDefinition<StoreEntityThreatActo
name: RELATION_PART_OF,
targets: [
{ name: ENTITY_TYPE_THREAT_ACTOR_GROUP, type: REL_NEW },
{ name: ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL, type: REL_NEW },
]
},
{
name: RELATION_COOPERATES_WITH,
targets: [
{ name: ENTITY_TYPE_THREAT_ACTOR_GROUP, type: REL_NEW },
{ name: ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL, type: REL_NEW },
]
},
{ name: RELATION_EMPLOYED_BY,
Expand Down Expand Up @@ -296,9 +294,7 @@ const THREAT_ACTOR_INDIVIDUAL_DEFINITION: ModuleDefinition<StoreEntityThreatActo
{
name: RELATION_DERIVED_FROM,
targets: [
{ name: ENTITY_TYPE_THREAT_ACTOR_INDIVIDUAL, type: REL_BUILT_IN },
{ name: ENTITY_TYPE_THREAT_ACTOR_GROUP, type: REL_BUILT_IN },
{ name: ENTITY_TYPE_THREAT_ACTOR, type: REL_BUILT_IN },
]
}
],
Expand Down
Loading