-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Defend Workflows] Defend Insights - Check existing Trusted Apps before creating an insight #207378
base: main
Are you sure you want to change the base?
Changes from all commits
f8fbe89
af73b0a
533ec60
1cd6ced
c0cf04e
7588ebe
489ea37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ export const useFetchInsights = ({ endpointId, onSuccess }: UseFetchInsightsConf | |
query: { | ||
actionTypes: JSON.stringify([ActionType.Refreshed]), | ||
targetIds: JSON.stringify([endpointId]), | ||
size: 100, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without it the es' default 10 is being used, we expect more results than that with certain AVs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why limit to 100? |
||
}, | ||
}); | ||
onSuccess(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ export async function buildIncompatibleAntivirusWorkflowInsights( | |
const codeSignaturesHits = ( | ||
await esClient.search<FileEventDoc>({ | ||
index: FILE_EVENTS_INDEX_PATTERN, | ||
size: eventIds.length, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was also defaulting to 10 causing instances of paths with no matched signers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are |
||
query: { | ||
bool: { | ||
must: [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ import type { ElasticsearchClient } from '@kbn/core/server'; | |
|
||
import { DataStreamSpacesAdapter } from '@kbn/data-stream-adapter'; | ||
|
||
import type { ExceptionListClient } from '@kbn/lists-plugin/server'; | ||
import { DefendInsightType } from '@kbn/elastic-assistant-common'; | ||
import type { | ||
SearchParams, | ||
SecurityWorkflowInsight, | ||
|
@@ -160,3 +162,58 @@ export function getUniqueInsights(insights: SecurityWorkflowInsight[]): Security | |
} | ||
return Object.values(uniqueInsights); | ||
} | ||
|
||
export const generateTrustedAppsFilter = (insight: SecurityWorkflowInsight): string | undefined => { | ||
return insight.remediation.exception_list_items | ||
?.flatMap((item) => | ||
item.entries.map((entry) => { | ||
if (!('value' in entry)) return ''; | ||
|
||
if (entry.field === 'process.executable.caseless') { | ||
return `exception-list-agnostic.attributes.entries.value:"${entry.value}"`; | ||
} | ||
|
||
if ( | ||
entry.field === 'process.code_signature' || | ||
(entry.field === 'process.Ext.code_signature' && typeof entry.value === 'string') | ||
) { | ||
const sanitizedValue = (entry.value as string) | ||
.replace(/[)(<>}{":\\]/gm, '\\$&') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section aligns with the approach already implemented for the search bar in Trusted Applications - https://github.com/szwarckonrad/kibana/blob/da25d13a2ac5be97216e202bf5d6bda6ff920b3b/x-pack/solutions/security/plugins/security_solution/public/management/common/utils.ts#L10-L26 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was hoping you could add a comment explaining the regex a bit? :) |
||
.replace(/\s/gm, '*'); | ||
return `exception-list-agnostic.attributes.entries.entries.value:(*${sanitizedValue}*)`; | ||
} | ||
|
||
return ''; | ||
}) | ||
) | ||
.filter(Boolean) | ||
.join(' AND '); | ||
}; | ||
|
||
export const checkIfRemediationExists = async ({ | ||
insight, | ||
exceptionListsClient, | ||
}: { | ||
insight: SecurityWorkflowInsight; | ||
exceptionListsClient: ExceptionListClient; | ||
}): Promise<boolean> => { | ||
if (insight.type !== DefendInsightType.Enum.incompatible_antivirus) { | ||
return false; | ||
} | ||
|
||
const filter = generateTrustedAppsFilter(insight); | ||
|
||
if (!filter) return false; | ||
|
||
const response = await exceptionListsClient.findExceptionListItem({ | ||
listId: 'endpoint_trusted_apps', | ||
page: 1, | ||
perPage: 1, | ||
namespaceType: 'agnostic', | ||
filter, | ||
sortField: 'created_at', | ||
sortOrder: 'desc', | ||
}); | ||
|
||
return !!response?.total && response.total > 0; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ import type { EndpointAppContextService } from '../../endpoint_app_context_servi | |
import { SecurityWorkflowInsightsFailedInitialized } from './errors'; | ||
import { | ||
buildEsQueryParams, | ||
checkIfRemediationExists, | ||
createDatastream, | ||
createPipeline, | ||
generateInsightId, | ||
|
@@ -127,7 +128,7 @@ class SecurityWorkflowInsightsService { | |
public async createFromDefendInsights( | ||
defendInsights: DefendInsight[], | ||
request: KibanaRequest<unknown, unknown, DefendInsightsPostRequestBody> | ||
): Promise<WriteResponseBase[]> { | ||
): Promise<Array<Awaited<WriteResponseBase | void>>> { | ||
await this.isInitialized; | ||
|
||
const workflowInsights = await buildWorkflowInsights({ | ||
|
@@ -137,14 +138,24 @@ class SecurityWorkflowInsightsService { | |
esClient: this.esClient, | ||
}); | ||
const uniqueInsights = getUniqueInsights(workflowInsights); | ||
|
||
return Promise.all(uniqueInsights.map((insight) => this.create(insight))); | ||
} | ||
|
||
public async create(insight: SecurityWorkflowInsight): Promise<WriteResponseBase> { | ||
public async create(insight: SecurityWorkflowInsight): Promise<WriteResponseBase | void> { | ||
await this.isInitialized; | ||
|
||
const id = generateInsightId(insight); | ||
|
||
const remediationExists = await checkIfRemediationExists({ | ||
insight, | ||
exceptionListsClient: this.endpointContext.getExceptionListsClient(), | ||
}); | ||
|
||
if (remediationExists) { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be useful to add a logger here or something? |
||
} | ||
|
||
// if insight already exists, update instead | ||
const existingInsights = await this.fetch({ ids: [id] }); | ||
if (existingInsights.length) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
longs paths were stretching out the container