Skip to content

Commit

Permalink
[SIEM][Detection Engine][Lists] Adds additional data types to value b…
Browse files Browse the repository at this point in the history
…ased lists

## Summary

Adds these data types to the value based lists end points from [Elasticsearch field data types](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html):

Single value based list types:
* binary
* boolean
* byte
* date
* date_nanos
* date_range
* double
* float
* integer
* ip
* half_float
* keyword
* text
* long
* short

Range value based list types:
* double_range
* float_range
* integer_range
* ip_range
* long_range


Geo value based list types: (caveat is that you cannot query them using other geometry just yet ... you can only these and export them)
* geo_point
* geo_shape
* shape

For importing and exporting different values such as ranges, geo, or single values, this introduces a serialize and deserialize option for the endpoints.

For example if you want to serialize in an ip_range such as 192.168.0.1,192.168.0.3 which has a comma between the two would use the following:

```ts
POST /api/lists
{
  "name": "List with an ip range",
  "serializer": "(?<gte>.+),(?<lte>.+)",
  "deserializer": "{{gte}},{{lte}}",
  "description": "This list has ip ranges",
  "type": "date_range"
}
``` 

If you want to serialize in keywords from a list that _only_ match a particular value you would use the following:

```ts
POST /api/lists
{
  "id": "keyword_custom_format_list",
  "name": "Simple list with a keyword using a custom format",
  "description": "This parses the first found ipv4 only",
  "serializer": "(?<value>((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))",
  "deserializer": "{{value}}",
  "type": "keyword"
}
```

The serializer is a [named capturing group](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) while the deserializer is using [MustacheJS](https://github.com/janl/mustache.js/). The range type, single value types, and geo types all have default captures for their serialize and default mustache templates if none are configured with an endpoint.

The default capture groups and mustache handles for each are:

* shape, geo_point, geo_shape:  `(?<lat>.+),(?<lon>.+)`
* date_range: `(?<gte>.+),(?<lte>.+)|(?<value>.+)`
* other ranges are: `(?<gte>.+)-(?<lte>.+)|(?<value>.+)`
* all single data types: `(?<value>.+)`

For ranges you can use both `gte, lte`, and `value` together. If `gte` _and_ `lte` matches it will use that for the greater than, less than elastic range and ignore `value`  even if `value` also matched. If _only_ `value` matches and `gte`, `lte` does not match then it will use `value` and put `value` as _both_ the `gte`, and `lte`.

For example, if you are serializing in a list of ip ranges as the list data type, `ip_range` and you have these 3 entries in the file:

```ts
127.0.0.1
127.0.0.2-5
```

The default `serializer` will use `(?<gte>.+)-(?<lte>.+)|(?<value>.+)` and you will get two elastic documents like so:

```ts
{
"_source" : {
  "ip_range" : {
    "gte" : "127.0.0.1",
    "lte" : "127.0.0.1"
  }
}

{
"_source" : {
  "ip_range" : {
    "gte" : "127.0.0.2",
    "lte" : "127.0.0.5"
  }
}
```

The default mustache handles for each are:

* shape, geo_point, geo_shape:  `{{{lat}}},{{{lon}}}`
* date_range: `{{{gte}}},{{{lte}}}`
* other ranges are: `{{{gte}}}-{{{lte}}}`
* all values are: `{{{value}}}`

I use three instead of two handle bars (`{{{` vs.` {{`) so that HTML is not escaped for the lists. You can override and change it if you need or want the escaping.

If during the deserializer phase it detects that a `gte` and `lte` are exactly the same it will still output them as a two items and use the mustache deserialize value. Using the ip-range example above that will be outputted like so since it detects that the lte-gte are exactly the same value:

```ts
127.0.0.1-127.0.0.1
127.0.0.2-127.0.0.5
```

---

Interesting queries to run from the lists scripts folder for testing:

Load some small test files from `./lists/files` for example:
```ts
./import_list_items_by_filename.sh ip_range ./lists/files/ip_range_cidr.txt
./import_list_items_by_filename.sh ip_range ./lists/files/ip_range.txt
./import_list_items_by_filename.sh date ./lists/files/date.txt
./import_list_items_by_filename.sh ip_range ./lists/files/ip_range_mixed.txt
... 
```

Export them
```ts
./export_list_items.sh ip_range_cidr.txt
./export_list_items.sh ip_range.txt
./export_list_items.sh date.txt
./export_list_items.sh ip_range_mixed.txt
...
```

Find on them
```ts
./find_list_items.sh ip_range_cidr.txt
./find_list_items.sh ip_range.txt
./find_list_items.sh date.txt
./find_list_items.sh ip_range_mixed.txt
...
```

Find specific values such as:

```ts
./get_list_item_by_value.sh ip_range_mixed.txt 192.168.0.1
./get_list_item_by_value.sh date.txt 2020-08-25T17:57:01.978Z
...
```

### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios
  • Loading branch information
FrankHassanabad authored Jul 8, 2020
1 parent 531cac0 commit 5f53597
Show file tree
Hide file tree
Showing 166 changed files with 3,300 additions and 420 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/lists/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ You should see the new list created like so:

```sh
{
"id": "list-ip",
"id": "list_ip",
"created_at": "2020-05-28T19:15:22.344Z",
"created_by": "yo",
"description": "This list describes bad internet ip",
Expand All @@ -96,7 +96,7 @@ You should see the new list item created and attached to the above list like so:
"value": "127.0.0.1",
"created_at": "2020-05-28T19:15:49.790Z",
"created_by": "yo",
"list_id": "list-ip",
"list_id": "list_ip",
"tie_breaker_id": "a881bf2e-1e17-4592-bba8-d567cb07d234",
"updated_at": "2020-05-28T19:15:49.790Z",
"updated_by": "yo"
Expand Down Expand Up @@ -195,7 +195,7 @@ You can then do find for each one like so:
"cursor": "WzIwLFsiYzU3ZWZiYzQtNDk3Ny00YTMyLTk5NWYtY2ZkMjk2YmVkNTIxIl1d",
"data": [
{
"id": "list-ip",
"id": "list_ip",
"created_at": "2020-05-28T19:15:22.344Z",
"created_by": "yo",
"description": "This list describes bad internet ip",
Expand Down
14 changes: 12 additions & 2 deletions x-pack/plugins/lists/common/get_call_cluster.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,15 @@ export const getEmptyCreateDocumentResponseMock = (): CreateDocumentResponse =>
});

export const getCallClusterMock = (
callCluster: unknown = getEmptyCreateDocumentResponseMock()
): LegacyAPICaller => jest.fn().mockResolvedValue(callCluster);
response: unknown = getEmptyCreateDocumentResponseMock()
): LegacyAPICaller => jest.fn().mockResolvedValue(response);

export const getCallClusterMockMultiTimes = (
responses: unknown[] = [getEmptyCreateDocumentResponseMock()]
): LegacyAPICaller => {
const returnJest = jest.fn();
responses.forEach((response) => {
returnJest.mockResolvedValueOnce(response);
});
return returnJest;
};
307 changes: 305 additions & 2 deletions x-pack/plugins/lists/common/schemas/common/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,27 @@
import { pipe } from 'fp-ts/lib/pipeable';
import { left } from 'fp-ts/lib/Either';

import { foldLeftRight, getPaths } from '../../siem_common_deps';
import { exactCheck, foldLeftRight, getPaths } from '../../siem_common_deps';

import { exceptionListType, operator, operator_type as operatorType } from './schemas';
import {
EsDataTypeGeoPoint,
EsDataTypeGeoPointRange,
EsDataTypeRange,
EsDataTypeRangeTerm,
EsDataTypeSingle,
EsDataTypeUnion,
Type,
esDataTypeGeoPoint,
esDataTypeGeoPointRange,
esDataTypeRange,
esDataTypeRangeTerm,
esDataTypeSingle,
esDataTypeUnion,
exceptionListType,
operator,
operator_type as operatorType,
type,
} from './schemas';

describe('Common schemas', () => {
describe('operatorType', () => {
Expand Down Expand Up @@ -122,4 +140,289 @@ describe('Common schemas', () => {
expect(keys.length).toEqual(2);
});
});

describe('type', () => {
test('it will work with a given expected type', () => {
const payload: Type = 'keyword';
const decoded = type.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given a type that does not exist', () => {
const payload: Type | 'madeup' = 'madeup';
const decoded = type.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([
'Invalid value "madeup" supplied to ""binary" | "boolean" | "byte" | "date" | "date_nanos" | "date_range" | "double" | "double_range" | "float" | "float_range" | "geo_point" | "geo_shape" | "half_float" | "integer" | "integer_range" | "ip" | "ip_range" | "keyword" | "long" | "long_range" | "shape" | "short" | "text""',
]);
expect(message.schema).toEqual({});
});
});

describe('esDataTypeRange', () => {
test('it will work with a given gte, lte range', () => {
const payload: EsDataTypeRange = { gte: '127.0.0.1', lte: '127.0.0.1' };
const decoded = esDataTypeRange.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given an extra madeup value', () => {
const payload: EsDataTypeRange & { madeupvalue: string } = {
gte: '127.0.0.1',
lte: '127.0.0.1',
madeupvalue: 'something',
};
const decoded = esDataTypeRange.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupvalue"']);
expect(message.schema).toEqual({});
});
});

describe('esDataTypeRangeTerm', () => {
test('it will work with a date_range', () => {
const payload: EsDataTypeRangeTerm = { date_range: { gte: '2015', lte: '2017' } };
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given an extra madeup value for date_range', () => {
const payload: EsDataTypeRangeTerm & { madeupvalue: string } = {
date_range: { gte: '2015', lte: '2017' },
madeupvalue: 'something',
};
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupvalue"']);
expect(message.schema).toEqual({});
});

test('it will work with a double_range', () => {
const payload: EsDataTypeRangeTerm = { double_range: { gte: '2015', lte: '2017' } };
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given an extra madeup value for double_range', () => {
const payload: EsDataTypeRangeTerm & { madeupvalue: string } = {
double_range: { gte: '2015', lte: '2017' },
madeupvalue: 'something',
};
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupvalue"']);
expect(message.schema).toEqual({});
});

test('it will work with a float_range', () => {
const payload: EsDataTypeRangeTerm = { float_range: { gte: '2015', lte: '2017' } };
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given an extra madeup value for float_range', () => {
const payload: EsDataTypeRangeTerm & { madeupvalue: string } = {
float_range: { gte: '2015', lte: '2017' },
madeupvalue: 'something',
};
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupvalue"']);
expect(message.schema).toEqual({});
});

test('it will work with a integer_range', () => {
const payload: EsDataTypeRangeTerm = { integer_range: { gte: '2015', lte: '2017' } };
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given an extra madeup value for integer_range', () => {
const payload: EsDataTypeRangeTerm & { madeupvalue: string } = {
integer_range: { gte: '2015', lte: '2017' },
madeupvalue: 'something',
};
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupvalue"']);
expect(message.schema).toEqual({});
});

test('it will work with a ip_range', () => {
const payload: EsDataTypeRangeTerm = { ip_range: { gte: '127.0.0.1', lte: '127.0.0.2' } };
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will work with a ip_range as a CIDR', () => {
const payload: EsDataTypeRangeTerm = { ip_range: '127.0.0.1/16' };
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given an extra madeup value for ip_range', () => {
const payload: EsDataTypeRangeTerm & { madeupvalue: string } = {
ip_range: { gte: '127.0.0.1', lte: '127.0.0.2' },
madeupvalue: 'something',
};
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupvalue"']);
expect(message.schema).toEqual({});
});

test('it will work with a long_range', () => {
const payload: EsDataTypeRangeTerm = { long_range: { gte: '2015', lte: '2017' } };
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given an extra madeup value for long_range', () => {
const payload: EsDataTypeRangeTerm & { madeupvalue: string } = {
long_range: { gte: '2015', lte: '2017' },
madeupvalue: 'something',
};
const decoded = esDataTypeRangeTerm.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupvalue"']);
expect(message.schema).toEqual({});
});
});

describe('esDataTypeGeoPointRange', () => {
test('it will work with a given lat, lon range', () => {
const payload: EsDataTypeGeoPointRange = { lat: '20', lon: '30' };
const decoded = esDataTypeGeoPointRange.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given an extra madeup value', () => {
const payload: EsDataTypeGeoPointRange & { madeupvalue: string } = {
lat: '20',
lon: '30',
madeupvalue: 'something',
};
const decoded = esDataTypeGeoPointRange.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupvalue"']);
expect(message.schema).toEqual({});
});
});

describe('esDataTypeGeoPoint', () => {
test('it will work with a given lat, lon range', () => {
const payload: EsDataTypeGeoPoint = { geo_point: { lat: '127.0.0.1', lon: '127.0.0.1' } };
const decoded = esDataTypeGeoPoint.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will work with a WKT (Well known text)', () => {
const payload: EsDataTypeGeoPoint = { geo_point: 'POINT (30 10)' };
const decoded = esDataTypeGeoPoint.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will give an error if given an extra madeup value', () => {
const payload: EsDataTypeGeoPoint & { madeupvalue: string } = {
geo_point: 'POINT (30 10)',
madeupvalue: 'something',
};
const decoded = esDataTypeGeoPoint.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupvalue"']);
expect(message.schema).toEqual({});
});
});

describe('esDataTypeSingle', () => {
test('it will work with single type', () => {
const payload: EsDataTypeSingle = { boolean: 'true' };
const decoded = esDataTypeSingle.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will not work with a madeup value', () => {
const payload: EsDataTypeSingle & { madeupValue: 'madeup' } = {
boolean: 'true',
madeupValue: 'madeup',
};
const decoded = esDataTypeSingle.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupValue"']);
expect(message.schema).toEqual({});
});
});

describe('esDataTypeUnion', () => {
test('it will work with a regular union', () => {
const payload: EsDataTypeUnion = { boolean: 'true' };
const decoded = esDataTypeUnion.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it will not work with a madeup value', () => {
const payload: EsDataTypeUnion & { madeupValue: 'madeupValue' } = {
boolean: 'true',
madeupValue: 'madeupValue',
};
const decoded = esDataTypeUnion.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "madeupValue"']);
expect(message.schema).toEqual({});
});
});
});
Loading

0 comments on commit 5f53597

Please sign in to comment.