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

[Security Solution][Detections] Fixes Severity Override not matching for Elastic Endpoint Security rule #74317

Merged
merged 4 commits into from
Aug 5, 2020
Merged
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 @@ -109,6 +109,24 @@ export const sampleDocNoSortId = (
sort: [],
});

export const sampleDocSeverity = (
severity?: Array<string | number | null> | string | number | null
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 looks like you covered your bases of odd ball types that can come up

): SignalSourceHit => ({
_index: 'myFakeSignalIndex',
_type: 'doc',
_score: 100,
_version: 1,
_id: sampleIdGuid,
_source: {
someKey: 'someValue',
'@timestamp': '2020-04-20T21:27:45+0000',
Copy link
Contributor

Choose a reason for hiding this comment

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

That doesn't look like a Zulu? It's fine if you want to test other values, just mentioning

Copy link
Member Author

Choose a reason for hiding this comment

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

I had used a similar mock from above, that's all.

event: {
severity: severity ?? 100,
},
},
sort: [],
});

export const sampleEmptyDocSearchResults = (): SignalSearchResponse => ({
took: 10,
timed_out: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { sampleDocNoSortId } from '../__mocks__/es_results';
import { sampleDocNoSortId, sampleDocSeverity } from '../__mocks__/es_results';
import { buildSeverityFromMapping } from './build_severity_from_mapping';

describe('buildSeverityFromMapping', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('severity defaults to provided if mapping is incomplete', () => {
test('severity defaults to provided if mapping is undefined', () => {
const severity = buildSeverityFromMapping({
doc: sampleDocNoSortId(),
severity: 'low',
Expand All @@ -22,5 +22,45 @@ describe('buildSeverityFromMapping', () => {
expect(severity).toEqual({ severity: 'low', severityMeta: {} });
});

test('severity is overridden to highest matched mapping', () => {
const severity = buildSeverityFromMapping({
doc: sampleDocSeverity(23),
severity: 'low',
severityMapping: [
{ field: 'event.severity', operator: 'equals', value: '23', severity: 'critical' },
{ field: 'event.severity', operator: 'equals', value: '23', severity: 'low' },
{ field: 'event.severity', operator: 'equals', value: '11', severity: 'critical' },
{ field: 'event.severity', operator: 'equals', value: '23', severity: 'medium' },
],
});

expect(severity).toEqual({
severity: 'critical',
severityMeta: {
severityOverrideField: 'event.severity',
},
});
});

test('severity is overridden when field is event.severity and source value is number', () => {
const severity = buildSeverityFromMapping({
doc: sampleDocSeverity(23),
severity: 'low',
severityMapping: [
{ field: 'event.severity', operator: 'equals', value: '13', severity: 'low' },
{ field: 'event.severity', operator: 'equals', value: '23', severity: 'medium' },
{ field: 'event.severity', operator: 'equals', value: '33', severity: 'high' },
{ field: 'event.severity', operator: 'equals', value: '43', severity: 'critical' },
],
});

expect(severity).toEqual({
severity: 'medium',
severityMeta: {
severityOverrideField: 'event.severity',
},
});
});

// TODO: Enhance...
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,38 @@ interface BuildSeverityFromMappingReturn {
severityMeta: Meta; // TODO: Stricter types
}

const severitySortMapping = {
low: 0,
medium: 1,
high: 2,
critical: 3,
};

export const buildSeverityFromMapping = ({
doc,
severity,
severityMapping,
}: BuildSeverityFromMappingProps): BuildSeverityFromMappingReturn => {
if (severityMapping != null && severityMapping.length > 0) {
let severityMatch: SeverityMappingItem | undefined;
severityMapping.forEach((mapping) => {
// TODO: Expand by verifying fieldType from index via doc._index
const mappedValue = get(mapping.field, doc._source);
if (mapping.value === mappedValue) {

// Sort the SeverityMapping from low to high, so last match (highest severity) is used
const severityMappingSorted = severityMapping.sort(
(a, b) => severitySortMapping[a.severity] - severitySortMapping[b.severity]
);

severityMappingSorted.forEach((mapping) => {
const docValue = get(mapping.field, doc._source);
// TODO: Expand by verifying fieldType from index via doc._index
// Till then, explicit parsing of event.severity (long) to number. If not ECS, this could be
// another datatype, but until we can lookup datatype we must assume number for the Elastic
// Endpoint Security rule to function correctly
let parsedMappingValue: string | number = mapping.value;
if (mapping.field === 'event.severity') {
parsedMappingValue = Math.floor(Number(parsedMappingValue));
}

if (parsedMappingValue === docValue) {
severityMatch = { ...mapping };
}
});
Expand Down