blob: fa5892ee2e3a742998dcc7729dde08f3c618cf64 [file] [log] [blame]
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.8 from src/site/markdown/metron-analytics/metron-maas-service/index.md at 2019-05-14
| Rendered using Apache Maven Fluido Skin 1.7
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="Date-Revision-yyyymmdd" content="20190514" />
<meta http-equiv="Content-Language" content="en" />
<title>Metron &#x2013; Model Management Infrastructure</title>
<link rel="stylesheet" href="../../css/apache-maven-fluido-1.7.min.css" />
<link rel="stylesheet" href="../../css/site.css" />
<link rel="stylesheet" href="../../css/print.css" media="print" />
<script type="text/javascript" src="../../js/apache-maven-fluido-1.7.min.js"></script>
<script type="text/javascript">
$( document ).ready( function() { $( '.carousel' ).carousel( { interval: 3500 } ) } );
</script>
</head>
<body class="topBarDisabled">
<div class="container-fluid">
<div id="banner">
<div class="pull-left"><a href="http://metron.apache.org/" id="bannerLeft"><img src="../../images/metron-logo.png" alt="Apache Metron" width="148px" height="48px"/></a></div>
<div class="pull-right"></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li class=""><a href="http://www.apache.org" class="externalLink" title="Apache">Apache</a><span class="divider">/</span></li>
<li class=""><a href="http://metron.apache.org/" class="externalLink" title="Metron">Metron</a><span class="divider">/</span></li>
<li class=""><a href="../../index.html" title="Documentation">Documentation</a><span class="divider">/</span></li>
<li class="active ">Model Management Infrastructure</li>
<li id="publishDate" class="pull-right"><span class="divider">|</span> Last Published: 2019-05-14</li>
<li id="projectVersion" class="pull-right">Version: 0.7.1</li>
</ul>
</div>
<div class="row-fluid">
<div id="leftColumn" class="span2">
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">User Documentation</li>
<li><a href="../../index.html" title="Metron"><span class="icon-chevron-down"></span>Metron</a>
<ul class="nav nav-list">
<li><a href="../../CONTRIBUTING.html" title="CONTRIBUTING"><span class="none"></span>CONTRIBUTING</a></li>
<li><a href="../../Upgrading.html" title="Upgrading"><span class="none"></span>Upgrading</a></li>
<li><a href="../../metron-analytics/index.html" title="Analytics"><span class="icon-chevron-down"></span>Analytics</a>
<ul class="nav nav-list">
<li class="active"><a href="#"><span class="none"></span>Maas-service</a></li>
<li><a href="../../metron-analytics/metron-profiler-client/index.html" title="Profiler-client"><span class="none"></span>Profiler-client</a></li>
<li><a href="../../metron-analytics/metron-profiler-common/index.html" title="Profiler-common"><span class="none"></span>Profiler-common</a></li>
<li><a href="../../metron-analytics/metron-profiler-repl/index.html" title="Profiler-repl"><span class="none"></span>Profiler-repl</a></li>
<li><a href="../../metron-analytics/metron-profiler-spark/index.html" title="Profiler-spark"><span class="none"></span>Profiler-spark</a></li>
<li><a href="../../metron-analytics/metron-profiler-storm/index.html" title="Profiler-storm"><span class="none"></span>Profiler-storm</a></li>
<li><a href="../../metron-analytics/metron-statistics/index.html" title="Statistics"><span class="icon-chevron-right"></span>Statistics</a></li>
</ul>
</li>
<li><a href="../../metron-contrib/metron-docker/index.html" title="Docker"><span class="none"></span>Docker</a></li>
<li><a href="../../metron-contrib/metron-performance/index.html" title="Performance"><span class="none"></span>Performance</a></li>
<li><a href="../../metron-deployment/index.html" title="Deployment"><span class="icon-chevron-right"></span>Deployment</a></li>
<li><a href="../../metron-interface/index.html" title="Interface"><span class="icon-chevron-right"></span>Interface</a></li>
<li><a href="../../metron-platform/index.html" title="Platform"><span class="icon-chevron-right"></span>Platform</a></li>
<li><a href="../../metron-sensors/index.html" title="Sensors"><span class="icon-chevron-right"></span>Sensors</a></li>
<li><a href="../../metron-stellar/stellar-3rd-party-example/index.html" title="Stellar-3rd-party-example"><span class="none"></span>Stellar-3rd-party-example</a></li>
<li><a href="../../metron-stellar/stellar-common/index.html" title="Stellar-common"><span class="icon-chevron-right"></span>Stellar-common</a></li>
<li><a href="../../metron-stellar/stellar-zeppelin/index.html" title="Stellar-zeppelin"><span class="none"></span>Stellar-zeppelin</a></li>
<li><a href="../../use-cases/index.html" title="Use-cases"><span class="icon-chevron-right"></span>Use-cases</a></li>
</ul>
</li>
</ul>
<hr />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy"><img class="builtBy" alt="Built by Maven" src="../../images/logos/maven-feather.png" /></a>
</div>
</div>
</div>
<div id="bodyColumn" class="span10" >
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<h1>Model Management Infrastructure</h1>
<p><a name="Model_Management_Infrastructure"></a></p>
<div class="section">
<h2><a name="Introduction"></a>Introduction</h2>
<p>One of the main features envisioned and requested is the ability to augment the threat intelligence and enrichment processes with insights derived from machine learning or statistical models. The challenges with this sort of infrastructure are</p>
<ul>
<li>Applying the model may be sufficiently computationally/resource intensive that we need to support scaling via load balancing, which will require service discovery and management.</li>
<li>Models require out of band and frequent training to react to growing threats and new patterns that emerge.</li>
<li>Models should be language/environment agnostic as much as possible. These should include small-data and big-data libraries and languages.</li>
</ul>
<p>To support a high throughput environment that is manageable, it is evident that</p>
<ul>
<li>Multiple versions of models will need to be exposed</li>
<li>Deployment should happen using Yarn to manage resources</li>
<li>Clients should have new model endpoints pushed to them</li>
</ul></div>
<div class="section">
<h2><a name="Architecture"></a>Architecture</h2>
<p><img src="../../images/maas_arch.png" alt="Architecture" /></p>
<p>To support these requirements, the following components have been created:</p>
<ul>
<li>A Yarn application which will listen for model deployment requests and upon execution, register their endpoints in zookeeper:
<ul>
<li>Operation type: ADD, REMOVE, LIST</li>
<li>Model Name</li>
<li>Model Version</li>
<li>Memory requirements (in megabytes)</li>
<li>Number of instances</li>
</ul>
</li>
<li>A command line deployment client which will localize the model payload onto HDFS and submit a model request</li>
<li>A Java client which will interact with zookeeper and receive updates about model state changes (new deployments, removals, etc.)</li>
<li>A series of Stellar functions for interacting with models deployed via the Model as a Service infrastructure.</li>
</ul></div>
<div class="section">
<h2><a name="maas_service.sh"></a><tt>maas_service.sh</tt></h2>
<p>The <tt>maas_service.sh</tt> script starts the Yarn application which will listen for requests. Right now the queue for the requests is a distributed queue stored in <a class="externalLink" href="http://curator.apache.org/curator-recipes/distributed-queue.html">zookeeper</a> for convenience.</p>
<div>
<div>
<pre class="source">./maas_service.sh
usage: MaaSClient
-c,--create Flag to indicate whether to create
the domain specified with -domain.
-d,--domain &lt;arg&gt; ID of the timeline domain where the
timeline entities will be put
-e,--shell_env &lt;arg&gt; Environment for shell script.
Specified as env_key=env_val pairs
-h,--help This screen
-j,--jar &lt;arg&gt; Jar file containing the application
master
-l,--log4j &lt;arg&gt; The log4j properties file to load
-ma,--modify_acls &lt;arg&gt; Users and groups that allowed to
modify the timeline entities in the
given domain
-mc,--master_vcores &lt;arg&gt; Amount of virtual cores to be
requested to run the application
master
-mm,--master_memory &lt;arg&gt; Amount of memory in MB to be
requested to run the application
master
-nle,--node_label_expression &lt;arg&gt; Node label expression to determine
the nodes where all the containers
of this application will be
allocated, &quot;&quot; means containers can
be allocated anywhere, if you don't
specify the option, default
node_label_expression of queue will
be used.
-q,--queue &lt;arg&gt; RM Queue in which this application
is to be submitted
-t,--timeout &lt;arg&gt; Application timeout in milliseconds
-va,--view_acls &lt;arg&gt; Users and groups that allowed to
view the timeline entities in the
given domain
-zq,--zk_quorum &lt;arg&gt; Zookeeper Quorum
-zr,--zk_root &lt;arg&gt; Zookeeper Root
</pre></div></div>
</div>
<div class="section">
<h2><a name="maas_deploy.sh"></a><tt>maas_deploy.sh</tt></h2>
<p>The <tt>maas_deploy.sh</tt> script allows users to deploy models and their collateral from their local disk to the cluster. It is assumed that the</p>
<ul>
<li>Collateral has exactly one <tt>.sh</tt> script capable of starting the endpoint</li>
<li>The model service executable will expose itself as a URL endpoint (e.g. as a REST interface, but not necessarily)</li>
<li>The model service executable will write out to local disk a JSON blob indicating the endpoint (see <a class="externalLink" href="https://gist.github.com/cestella/cba10aff0f970078a4c2c8cade3a4d1a#file-dga-py-L21">here</a> for an example mock service using Python and Flask).</li>
</ul>
<div>
<div>
<pre class="source">./maas_deploy.sh
usage: ModelSubmission
-h,--help This screen
-hmp,--hdfs_model_path &lt;arg&gt; Model Path (HDFS)
-lmp,--local_model_path &lt;arg&gt; Model Path (local)
-l,--log4j &lt;arg&gt; The log4j properties file to load
-m,--memory &lt;arg&gt; Memory for container
-mo,--mode &lt;arg&gt; ADD, LIST or REMOVE
-n,--name &lt;arg&gt; Model Name
-ni,--num_instances &lt;arg&gt; Number of model instances
-v,--version &lt;arg&gt; Model version
-zq,--zk_quorum &lt;arg&gt; Zookeeper Quorum
-zr,--zk_root &lt;arg&gt; Zookeeper Root
</pre></div></div>
</div>
<div class="section">
<h2><a name="Kerberos_Support"></a>Kerberos Support</h2>
<p>Model as a service will run on a kerberized cluster (see <a href="../../metron-deployment/vagrant/Kerberos-setup.html">here</a> for instructions for vagrant) with a caveat. The user who submits the service will be the user who executes the models on the cluster. That is to say that user impersonation of models deployed is not done at the moment.</p></div>
<div class="section">
<h2><a name="Stellar_Integration"></a>Stellar Integration</h2>
<p>Two Stellar functions have been added to provide the ability to call out to models deployed via Model as a Service. One aimed at recovering a load balanced endpoint of a deployed model given the name and, optionally, the version. The second is aimed at calling that endpoint assuming that it is exposed as a REST endpoint.</p>
<ul>
<li><tt>MAAS_MODEL_APPLY(endpoint, function?, model_args)</tt> : Returns the output of a model deployed via model which is deployed at endpoint. <tt>endpoint</tt> is a map containing <tt>name</tt>, <tt>version</tt>, <tt>url</tt> for the REST endpoint, <tt>function</tt> is the endpoint path and is optional, and <tt>model_args</tt> is a dictionary of arguments for the model (these become request params).</li>
<li><tt>MAAS_GET_ENDPOINT(model_name, model_version?)</tt> : Inspects zookeeper and returns a map containing the <tt>name</tt>, <tt>version</tt> and <tt>url</tt> for the model referred to by <tt>model_name</tt> and <tt>model_version</tt>. If <tt>model_version</tt> is not specified, the most current model associated with <tt>model_name</tt> is returned. In the instance where more than one model is deployed, a random one is selected with uniform probability.</li>
</ul>
<p><a name="Example"></a></p>
<h1>Example</h1>
<p>Let&#x2019;s augment the <tt>squid</tt> proxy sensor to use a model that will determine if the destination host is a domain generating algorithm. For the purposes of demonstration, this algorithm is super simple and is implemented using Python with a REST interface exposed via the Flask python library.</p></div>
<div class="section">
<h2><a name="Install_Prerequisites_and_Mock_DGA_Service"></a>Install Prerequisites and Mock DGA Service</h2>
<p>Now let&#x2019;s install some prerequisites:</p>
<ul>
<li>Flask via <tt>yum install python-flask</tt></li>
<li>Jinja2 via <tt>yum install python-jinja2</tt></li>
<li>Squid client via <tt>yum install squid</tt></li>
<li>ES Head plugin via <tt>/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head</tt></li>
</ul>
<p>Start Squid via <tt>service squid start</tt></p>
<p>Now that we have flask and jinja, we can create a mock DGA service to deploy with MaaS:</p>
<ul>
<li>Download the files in <a class="externalLink" href="https://gist.github.com/cestella/cba10aff0f970078a4c2c8cade3a4d1a">this</a> gist into the <tt>$HOME/mock_dga</tt> directory</li>
<li>Make <tt>rest.sh</tt> executable via <tt>chmod +x $HOME/mock_dga/rest.sh</tt></li>
</ul>
<p>This service will treat <tt>yahoo.com</tt> and <tt>amazon.com</tt> as legit and everything else as malicious. The contract is that the REST service exposes an endpoint <tt>/apply</tt> and returns back JSON maps with a single key <tt>is_malicious</tt> which can be <tt>malicious</tt> or <tt>legit</tt>.</p></div>
<div class="section">
<h2><a name="Deploy_Mock_DGA_Service_via_MaaS"></a>Deploy Mock DGA Service via MaaS</h2>
<p>The following presumes that you are a logged in as a user who has a home directory in HDFS under <tt>/user/$USER</tt>. If you do not, please create one and ensure the permissions are set appropriate:</p>
<div>
<div>
<pre class="source">su - hdfs -c &quot;hadoop fs -mkdir /user/$USER&quot;
su - hdfs -c &quot;hadoop fs -chown $USER:$USER /user/$USER&quot;
</pre></div></div>
<p>Or, in the common case for the <tt>metron</tt> user:</p>
<div>
<div>
<pre class="source">su - hdfs -c &quot;hadoop fs -mkdir /user/metron&quot;
su - hdfs -c &quot;hadoop fs -chown metron:metron /user/metron&quot;
</pre></div></div>
<p>Now let&#x2019;s start MaaS and deploy the Mock DGA Service:</p>
<ul>
<li>Start MaaS via <tt>$METRON_HOME/bin/maas_service.sh -zq node1:2181</tt></li>
<li>Start one instance of the mock DGA model with 512M of memory via <tt>$METRON_HOME/bin/maas_deploy.sh -zq node1:2181 -lmp $HOME/mock_dga -hmp /user/$USER/models -mo ADD -m 512 -n dga -v 1.0 -ni 1</tt></li>
<li>As a sanity check:
<ul>
<li>Ensure that the model is running via <tt>$METRON_HOME/bin/maas_deploy.sh -zq node1:2181 -mo LIST</tt>. You should see <tt>Model dga @ 1.0</tt> be displayed and under that a url such as (but not exactly) <tt>http://node1:36161</tt></li>
<li>Try to hit the model via curl: <tt>curl 'http://localhost:36161/apply?host=caseystella.com'</tt> and ensure that it returns a JSON map indicating the domain is malicious.</li>
</ul>
</li>
</ul></div>
<div class="section">
<h2><a name="Adjust_Configurations_for_Squid_to_Call_Model"></a>Adjust Configurations for Squid to Call Model</h2>
<p>Now that we have a deployed model, let&#x2019;s adjust the configurations for the Squid topology to annotate the messages with the output of the model.</p>
<ul>
<li>Edit the squid parser configuration at <tt>$METRON_HOME/config/zookeeper/parsers/squid.json</tt> in your favorite text editor and add a new FieldTransformation to indicate a threat alert based on the model (note the addition of <tt>is_malicious</tt> and <tt>is_alert</tt>):</li>
</ul>
<div>
<div>
<pre class="source">{
&quot;parserClassName&quot;: &quot;org.apache.metron.parsers.GrokParser&quot;,
&quot;sensorTopic&quot;: &quot;squid&quot;,
&quot;parserConfig&quot;: {
&quot;grokPath&quot;: &quot;/patterns/squid&quot;,
&quot;patternLabel&quot;: &quot;SQUID_DELIMITED&quot;,
&quot;timestampField&quot;: &quot;timestamp&quot;
},
&quot;fieldTransformations&quot; : [
{
&quot;transformation&quot; : &quot;STELLAR&quot;
,&quot;output&quot; : [ &quot;full_hostname&quot;, &quot;domain_without_subdomains&quot;, &quot;is_malicious&quot;, &quot;is_alert&quot; ]
,&quot;config&quot; : {
&quot;full_hostname&quot; : &quot;URL_TO_HOST(url)&quot;
,&quot;domain_without_subdomains&quot; : &quot;DOMAIN_REMOVE_SUBDOMAINS(full_hostname)&quot;
,&quot;is_malicious&quot; : &quot;MAP_GET('is_malicious', MAAS_MODEL_APPLY(MAAS_GET_ENDPOINT('dga'), {'host' : domain_without_subdomains}))&quot;
,&quot;is_alert&quot; : &quot;if is_malicious == 'malicious' then 'true' else null&quot;
}
}
]
}
</pre></div></div>
<ul>
<li>Edit the squid enrichment configuration at <tt>$METRON_HOME/config/zookeeper/enrichments/squid.json</tt> (this file will not exist, so create a new one) to make the threat triage adjust the level of risk based on the model output:</li>
</ul>
<div>
<div>
<pre class="source">{
&quot;enrichment&quot; : {
&quot;fieldMap&quot;: {}
},
&quot;threatIntel&quot; : {
&quot;fieldMap&quot;:{},
&quot;triageConfig&quot; : {
&quot;riskLevelRules&quot; : [
{
&quot;rule&quot; : &quot;is_malicious == 'malicious'&quot;,
&quot;score&quot; : 100
}
],
&quot;aggregator&quot; : &quot;MAX&quot;
}
}
}
</pre></div></div>
<ul>
<li>Upload new configs via <tt>$METRON_HOME/bin/zk_load_configs.sh --mode PUSH -i $METRON_HOME/config/zookeeper -z node1:2181</tt></li>
<li>Make the Squid topic in kafka via <tt>/usr/hdp/current/kafka-broker/bin/kafka-topics.sh --zookeeper node1:2181 --create --topic squid --partitions 1 --replication-factor 1</tt></li>
</ul></div>
<div class="section">
<h2><a name="Start_Topologies_and_Send_Data"></a>Start Topologies and Send Data</h2>
<p>Now we need to start the topologies and send some data:</p>
<ul>
<li>Start the squid topology via <tt>$METRON_HOME/bin/start_parser_topology.sh -k node1:6667 -z node1:2181 -s squid</tt></li>
<li>Generate some data via the squid client:
<ul>
<li>Generate a legit example: <tt>squidclient http://yahoo.com</tt></li>
<li>Generate a malicious example: <tt>squidclient http://cnn.com</tt></li>
</ul>
</li>
<li>Send the data to kafka via <tt>cat /var/log/squid/access.log | /usr/hdp/current/kafka-broker/bin/kafka-console-producer.sh --broker-list node1:6667 --topic squid</tt></li>
<li>Browse the data in elasticsearch via the ES Head plugin @ <a class="externalLink" href="http://node1:9200/_plugin/head/">http://node1:9200/_plugin/head/</a> and verify that in the squid index you have two documents
<ul>
<li>One from <tt>yahoo.com</tt> which does not have <tt>is_alert</tt> set and does have <tt>is_malicious</tt> set to <tt>legit</tt></li>
<li>One from <tt>cnn.com</tt> which does have <tt>is_alert</tt> set to <tt>true</tt>, <tt>is_malicious</tt> set to <tt>malicious</tt> and <tt>threat:triage:level</tt> set to 100</li>
</ul>
</li>
</ul></div>
</div>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
© 2015-2016 The Apache Software Foundation. Apache Metron, Metron, Apache, the Apache feather logo,
and the Apache Metron project logo are trademarks of The Apache Software Foundation.
</div>
</div>
</footer>
</body>
</html>