detector-custom-rules.asciidoc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. [role="xpack"]
  2. [[ml-configuring-detector-custom-rules]]
  3. === Customizing detectors with custom rules
  4. <<ml-rules,Custom rules>> enable you to change the behavior of anomaly
  5. detectors based on domain-specific knowledge.
  6. Custom rules describe _when_ a detector should take a certain _action_ instead
  7. of following its default behavior. To specify the _when_ a rule uses
  8. a `scope` and `conditions`. You can think of `scope` as the categorical
  9. specification of a rule, while `conditions` are the numerical part.
  10. A rule can have a scope, one or more conditions, or a combination of
  11. scope and conditions.
  12. Let us see how those can be configured by examples.
  13. ==== Specifying custom rule scope
  14. Let us assume we are configuring an {anomaly-job} in order to detect DNS data
  15. exfiltration. Our data contain fields "subdomain" and "highest_registered_domain".
  16. We can use a detector that looks like
  17. `high_info_content(subdomain) over highest_registered_domain`. If we run such a
  18. job, it is possible that we discover a lot of anomalies on frequently used
  19. domains that we have reasons to trust. As security analysts, we are not
  20. interested in such anomalies. Ideally, we could instruct the detector to skip
  21. results for domains that we consider safe. Using a rule with a scope allows us
  22. to achieve this.
  23. First, we need to create a list of our safe domains. Those lists are called
  24. _filters_ in {ml}. Filters can be shared across {anomaly-jobs}.
  25. We create our filter using the {ref}/ml-put-filter.html[put filter API]:
  26. [source,console]
  27. ----------------------------------
  28. PUT _ml/filters/safe_domains
  29. {
  30. "description": "Our list of safe domains",
  31. "items": ["safe.com", "trusted.com"]
  32. }
  33. ----------------------------------
  34. // TEST[skip:needs-licence]
  35. Now, we can create our {anomaly-job} specifying a scope that uses the
  36. `safe_domains` filter for the `highest_registered_domain` field:
  37. [source,console]
  38. ----------------------------------
  39. PUT _ml/anomaly_detectors/dns_exfiltration_with_rule
  40. {
  41. "analysis_config" : {
  42. "bucket_span":"5m",
  43. "detectors" :[{
  44. "function":"high_info_content",
  45. "field_name": "subdomain",
  46. "over_field_name": "highest_registered_domain",
  47. "custom_rules": [{
  48. "actions": ["skip_result"],
  49. "scope": {
  50. "highest_registered_domain": {
  51. "filter_id": "safe_domains",
  52. "filter_type": "include"
  53. }
  54. }
  55. }]
  56. }]
  57. },
  58. "data_description" : {
  59. "time_field":"timestamp"
  60. }
  61. }
  62. ----------------------------------
  63. // TEST[skip:needs-licence]
  64. As time advances and we see more data and more results, we might encounter new
  65. domains that we want to add in the filter. We can do that by using the
  66. {ref}/ml-update-filter.html[update filter API]:
  67. [source,console]
  68. ----------------------------------
  69. POST _ml/filters/safe_domains/_update
  70. {
  71. "add_items": ["another-safe.com"]
  72. }
  73. ----------------------------------
  74. // TEST[skip:setup:ml_filter_safe_domains]
  75. Note that we can use any of the `partition_field_name`, `over_field_name`, or
  76. `by_field_name` fields in the `scope`.
  77. In the following example we scope multiple fields:
  78. [source,console]
  79. ----------------------------------
  80. PUT _ml/anomaly_detectors/scoping_multiple_fields
  81. {
  82. "analysis_config" : {
  83. "bucket_span":"5m",
  84. "detectors" :[{
  85. "function":"count",
  86. "partition_field_name": "my_partition",
  87. "over_field_name": "my_over",
  88. "by_field_name": "my_by",
  89. "custom_rules": [{
  90. "actions": ["skip_result"],
  91. "scope": {
  92. "my_partition": {
  93. "filter_id": "filter_1"
  94. },
  95. "my_over": {
  96. "filter_id": "filter_2"
  97. },
  98. "my_by": {
  99. "filter_id": "filter_3"
  100. }
  101. }
  102. }]
  103. }]
  104. },
  105. "data_description" : {
  106. "time_field":"timestamp"
  107. }
  108. }
  109. ----------------------------------
  110. // TEST[skip:needs-licence]
  111. Such a detector will skip results when the values of all 3 scoped fields
  112. are included in the referenced filters.
  113. ==== Specifying custom rule conditions
  114. Imagine a detector that looks for anomalies in CPU utilization.
  115. Given a machine that is idle for long enough, small movement in CPU could
  116. result in anomalous results where the `actual` value is quite small, for
  117. example, 0.02. Given our knowledge about how CPU utilization behaves we might
  118. determine that anomalies with such small actual values are not interesting for
  119. investigation.
  120. Let us now configure an {anomaly-job} with a rule that will skip results where
  121. CPU utilization is less than 0.20.
  122. [source,console]
  123. ----------------------------------
  124. PUT _ml/anomaly_detectors/cpu_with_rule
  125. {
  126. "analysis_config" : {
  127. "bucket_span":"5m",
  128. "detectors" :[{
  129. "function":"high_mean",
  130. "field_name": "cpu_utilization",
  131. "custom_rules": [{
  132. "actions": ["skip_result"],
  133. "conditions": [
  134. {
  135. "applies_to": "actual",
  136. "operator": "lt",
  137. "value": 0.20
  138. }
  139. ]
  140. }]
  141. }]
  142. },
  143. "data_description" : {
  144. "time_field":"timestamp"
  145. }
  146. }
  147. ----------------------------------
  148. // TEST[skip:needs-licence]
  149. When there are multiple conditions they are combined with a logical `and`.
  150. This is useful when we want the rule to apply to a range. We simply create
  151. a rule with two conditions, one for each end of the desired range.
  152. Here is an example where a count detector will skip results when the count
  153. is greater than 30 and less than 50:
  154. [source,console]
  155. ----------------------------------
  156. PUT _ml/anomaly_detectors/rule_with_range
  157. {
  158. "analysis_config" : {
  159. "bucket_span":"5m",
  160. "detectors" :[{
  161. "function":"count",
  162. "custom_rules": [{
  163. "actions": ["skip_result"],
  164. "conditions": [
  165. {
  166. "applies_to": "actual",
  167. "operator": "gt",
  168. "value": 30
  169. },
  170. {
  171. "applies_to": "actual",
  172. "operator": "lt",
  173. "value": 50
  174. }
  175. ]
  176. }]
  177. }]
  178. },
  179. "data_description" : {
  180. "time_field":"timestamp"
  181. }
  182. }
  183. ----------------------------------
  184. // TEST[skip:needs-licence]
  185. ==== Custom rules in the life-cycle of a job
  186. Custom rules only affect results created after the rules were applied.
  187. Let us imagine that we have configured an {anomaly-job} and it has been running
  188. for some time. After observing its results we decide that we can employ
  189. rules in order to get rid of some uninteresting results. We can use
  190. the {ref}/ml-update-job.html[update {anomaly-job} API] to do so. However, the
  191. rule we added will only be in effect for any results created from the moment we
  192. added the rule onwards. Past results will remain unaffected.
  193. ==== Using custom rules vs. filtering data
  194. It might appear like using rules is just another way of filtering the data
  195. that feeds into an {anomaly-job}. For example, a rule that skips results when
  196. the partition field value is in a filter sounds equivalent to having a query
  197. that filters out such documents. But it is not. There is a fundamental
  198. difference. When the data is filtered before reaching a job it is as if they
  199. never existed for the job. With rules, the data still reaches the job and
  200. affects its behavior (depending on the rule actions).
  201. For example, a rule with the `skip_result` action means all data will still
  202. be modeled. On the other hand, a rule with the `skip_model_update` action means
  203. results will still be created even though the model will not be updated by
  204. data matched by a rule.