|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import moment from 'moment'; |
| 9 | + |
| 10 | +import type { ToolingLog } from '@kbn/tooling-log'; |
| 11 | +import type { Client, estypes } from '@elastic/elasticsearch'; |
| 12 | +import { |
| 13 | + ActionType, |
| 14 | + Category, |
| 15 | + type SecurityWorkflowInsight, |
| 16 | + SourceType, |
| 17 | + TargetType, |
| 18 | +} from '../types/workflow_insights'; |
| 19 | + |
| 20 | +export interface IndexedWorkflowInsights { |
| 21 | + data: estypes.BulkResponse; |
| 22 | + cleanup: () => Promise<DeletedWorkflowInsights>; |
| 23 | +} |
| 24 | + |
| 25 | +export interface DeletedWorkflowInsights { |
| 26 | + data: estypes.BulkResponse; |
| 27 | +} |
| 28 | + |
| 29 | +export const indexWorkflowInsights = async ({ |
| 30 | + esClient, |
| 31 | + log, |
| 32 | + endpointId, |
| 33 | + os, |
| 34 | + count, |
| 35 | + antivirus, |
| 36 | + path, |
| 37 | +}: { |
| 38 | + esClient: Client; |
| 39 | + log: ToolingLog; |
| 40 | + endpointId: string; |
| 41 | + os: 'windows' | 'macos' | 'linux'; |
| 42 | + count: number; |
| 43 | + antivirus: string; |
| 44 | + path: string; |
| 45 | +}): Promise<IndexedWorkflowInsights> => { |
| 46 | + log.debug(`Indexing ${count} workflow insights`); |
| 47 | + |
| 48 | + const operations = Array.from({ length: count }).flatMap((_, i) => { |
| 49 | + return [ |
| 50 | + { |
| 51 | + index: { |
| 52 | + _index: '.edr-workflow-insights-default', |
| 53 | + op_type: 'create', |
| 54 | + }, |
| 55 | + }, |
| 56 | + generateWorkflowInsightsDoc({ endpointId, os, runNumber: i, antivirus, path }), |
| 57 | + ]; |
| 58 | + }); |
| 59 | + |
| 60 | + const response = await esClient.bulk({ |
| 61 | + refresh: 'wait_for', |
| 62 | + operations, |
| 63 | + }); |
| 64 | + |
| 65 | + if (response.errors) { |
| 66 | + log.error( |
| 67 | + `There was an error indexing workflow insights ${JSON.stringify(response.items, null, 2)}` |
| 68 | + ); |
| 69 | + } else { |
| 70 | + log.debug(`Indexed ${count} workflow insights successfully`); |
| 71 | + } |
| 72 | + |
| 73 | + return { |
| 74 | + data: response, |
| 75 | + cleanup: deleteIndexedWorkflowInsights.bind(null, esClient, response, log), |
| 76 | + }; |
| 77 | +}; |
| 78 | + |
| 79 | +const deleteIndexedWorkflowInsights = async ( |
| 80 | + esClient: Client, |
| 81 | + indexedWorkflowInsights: IndexedWorkflowInsights['data'], |
| 82 | + log: ToolingLog |
| 83 | +): Promise<DeletedWorkflowInsights> => { |
| 84 | + log.debug(`Deleting ${indexedWorkflowInsights.items.length} indexed workflow insights`); |
| 85 | + let response: estypes.BulkResponse = { |
| 86 | + took: 0, |
| 87 | + errors: false, |
| 88 | + items: [], |
| 89 | + }; |
| 90 | + |
| 91 | + if (indexedWorkflowInsights.items.length) { |
| 92 | + const idsToDelete = indexedWorkflowInsights.items |
| 93 | + .filter((item) => item.create) |
| 94 | + .map((item) => ({ |
| 95 | + delete: { |
| 96 | + _index: item.create?._index, |
| 97 | + _id: item.create?._id, |
| 98 | + }, |
| 99 | + })); |
| 100 | + |
| 101 | + if (idsToDelete.length) { |
| 102 | + response = await esClient.bulk({ |
| 103 | + operations: idsToDelete, |
| 104 | + }); |
| 105 | + log.debug('Indexed workflow insights deleted successfully'); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return { |
| 110 | + data: response, |
| 111 | + }; |
| 112 | +}; |
| 113 | + |
| 114 | +const generateWorkflowInsightsDoc = ({ |
| 115 | + endpointId, |
| 116 | + os, |
| 117 | + runNumber, |
| 118 | + antivirus, |
| 119 | + path, |
| 120 | +}: { |
| 121 | + endpointId: string; |
| 122 | + os: 'linux' | 'windows' | 'macos'; |
| 123 | + runNumber: number; |
| 124 | + antivirus: string; |
| 125 | + path: string; |
| 126 | +}): SecurityWorkflowInsight => { |
| 127 | + const currentTime = moment(); |
| 128 | + const signatureField = |
| 129 | + os === 'linux' |
| 130 | + ? undefined |
| 131 | + : os === 'windows' |
| 132 | + ? 'process.Ext.code_signature' |
| 133 | + : 'process.code_signature'; |
| 134 | + |
| 135 | + const signatureValue = os === 'linux' ? undefined : 'Elastic'; |
| 136 | + return { |
| 137 | + remediation: { |
| 138 | + exception_list_items: [ |
| 139 | + { |
| 140 | + entries: [ |
| 141 | + { |
| 142 | + field: 'process.executable.caseless', |
| 143 | + type: 'match', |
| 144 | + value: |
| 145 | + os !== 'windows' |
| 146 | + ? `/${runNumber}${path}` |
| 147 | + : (() => { |
| 148 | + const parts = path.split('\\'); // Split by Windows path separator |
| 149 | + const lastPart = parts.pop(); // Get the last part (executable) |
| 150 | + return `${parts.join('\\')}\\${runNumber}\\${lastPart}`; // Reconstruct the path |
| 151 | + })(), |
| 152 | + operator: 'included', |
| 153 | + }, |
| 154 | + ...(signatureField && signatureValue |
| 155 | + ? [ |
| 156 | + { |
| 157 | + field: signatureField, |
| 158 | + operator: 'included' as const, |
| 159 | + type: 'match' as const, |
| 160 | + value: signatureValue, |
| 161 | + }, |
| 162 | + ] |
| 163 | + : []), |
| 164 | + ], |
| 165 | + list_id: 'endpoint_trusted_apps', |
| 166 | + name: `${antivirus}`, |
| 167 | + os_types: [os], |
| 168 | + description: 'Suggested by Security Workflow Insights', |
| 169 | + tags: ['policy:all'], |
| 170 | + }, |
| 171 | + ], |
| 172 | + }, |
| 173 | + metadata: { |
| 174 | + notes: { |
| 175 | + llm_model: '', |
| 176 | + }, |
| 177 | + display_name: `${antivirus}`, |
| 178 | + }, |
| 179 | + '@timestamp': currentTime, |
| 180 | + action: { |
| 181 | + type: ActionType.Refreshed, |
| 182 | + timestamp: currentTime, |
| 183 | + }, |
| 184 | + source: { |
| 185 | + data_range_end: currentTime.clone().add(24, 'hours'), |
| 186 | + id: '7184ab52-c318-4c91-b765-805f889e34e2', |
| 187 | + type: SourceType.LlmConnector, |
| 188 | + data_range_start: currentTime, |
| 189 | + }, |
| 190 | + message: 'Incompatible antiviruses detected', |
| 191 | + category: Category.Endpoint, |
| 192 | + type: 'incompatible_antivirus', |
| 193 | + value: `${antivirus} ${path}${signatureValue ? ` ${signatureValue}` : ''}`, |
| 194 | + target: { |
| 195 | + ids: [endpointId], |
| 196 | + type: TargetType.Endpoint, |
| 197 | + }, |
| 198 | + }; |
| 199 | +}; |
0 commit comments