最新消息:

Machine Learning in Nmap

置顶文章 admin 212浏览 0评论

Yes, the most famous network scanner embeds Machine Learning. Really? A deep neural network with many scientific keywords ? Not at all, I’m more talking about grandpa machine learning. Let us discover it!


Notes I present some pieces of codes which are originally commented by their authors. I keep authors’ comments under the format /* comments */ while my own comments use the format // comments. I use the version 7.70SVN of nmap.


First discovery

After installing a basic Raspbian on my RPi, I also decided to add nmap to the system. Not so hard… but I was quite amazed.

$ sudo apt install nmap
...
The following additional packages will be installed:
  liblinear3 liblua5.3-0 ndiff python-bs4 python-html5lib python-lxml
  python-webencodings
...

The package manager demands other packages, especially lua and python stuff. Ok, why not, but why liblinear3 ?! Actually, liblinear is a library for linear classification. Hum… it looks weird, so I decided to go deeper!

Deep learning

Sorry for the pun, but there is not any neural network behind that. Looking at the code of liblinear, available on GitHub, we basically learn the following:

LIBLINEAR is a simple package for solving large-scale regularized linear
classification and regression. It currently supports
- L2-regularized logistic regression/L2-loss support vector classification/L1-loss support vector classification
- L1-regularized L2-loss support vector classification/L1-regularized logistic regression
- L2-regularized L2-loss support vector regression/L1-loss support vector regression.

In a word, it can do three things:

  • logistic regression
  • support vector classification
  • support vector regression

Even if the first technique aimed to fit data to a model, it is mainly used to perform binary classification. The code boils down to few files: linear.h tron.h


Note

The whole implementation is probably very efficient as liblinear won the ICML 2008 large-scale learning challenge and has been used to win the KDD Cup 2010 (see https://www.csie.ntu.edu.tw/~cjlin/liblinear/).


Coming back to Nmap

Now, the goal is to look for liblinear inside the nmap code (about 85Mb).

$ git clone https://github.com/nmap/nmap.git

The natural idea is to grep “linear.h” in the folder.

$ <span class="nb js-evernote-checked" data-evernote-id="312">cd</span> nmap
$ grep -r linear.h  
liblinear/Makefile:linear.o: linear.cpp linear.h
liblinear/liblinear.vcxproj:    &lt;ClInclude <span class="nv js-evernote-checked" data-evernote-id="313">Include</span><span class="o js-evernote-checked" data-evernote-id="314">=</span><span class="s2 js-evernote-checked" data-evernote-id="315">"linear.h"</span> /&gt;
liblinear/Makefile.win:linear.obj: linear.cpp linear.h
liblinear/Makefile.win:lib: linear.cpp linear.h linear.def tron.obj
liblinear/linear.cpp:#include <span class="s2 js-evernote-checked" data-evernote-id="316">"linear.h"</span>
liblinear/predict.c:#include <span class="s2 js-evernote-checked" data-evernote-id="317">"linear.h"</span>
liblinear/train.c:#include <span class="s2 js-evernote-checked" data-evernote-id="318">"linear.h"</span>
FPModel.cc:#include <span class="s2 js-evernote-checked" data-evernote-id="319">"linear.h"</span>
configure.ac:  AC_CHECK_HEADERS<span class="o js-evernote-checked" data-evernote-id="320">([</span>linear.h<span class="o js-evernote-checked" data-evernote-id="321">]</span>,
FPEngine.cc:#include <span class="s2 js-evernote-checked" data-evernote-id="322">"linear.h"</span>
configure:  <span class="k js-evernote-checked" data-evernote-id="323">for</span> ac_header in linear.h
configure:  ac_fn_c_check_header_mongrel <span class="s2 js-evernote-checked" data-evernote-id="324">"</span><span class="nv js-evernote-checked" data-evernote-id="325">$LINENO</span><span class="s2 js-evernote-checked" data-evernote-id="326">"</span> <span class="s2 js-evernote-checked" data-evernote-id="327">"linear.h"</span> <span class="s2 js-evernote-checked" data-evernote-id="328">"ac_cv_header_linear_h"</span> <span class="s2 js-evernote-checked" data-evernote-id="329">"</span><span class="nv js-evernote-checked" data-evernote-id="330">$ac_includes_default</span><span class="s2 js-evernote-checked" data-evernote-id="331">"</span>
configure:if <span class="nb js-evernote-checked" data-evernote-id="332">test</span> <span class="s2 js-evernote-checked" data-evernote-id="333">"x</span><span class="nv js-evernote-checked" data-evernote-id="334">$ac_cv_header_linear_h</span><span class="s2 js-evernote-checked" data-evernote-id="335">"</span> <span class="o js-evernote-checked" data-evernote-id="336">=</span> xyes<span class="p js-evernote-checked" data-evernote-id="337">;</span> <span class="k js-evernote-checked" data-evernote-id="338">then</span> :

The result shows a dozen of occurrences. The “configure” file is not interesting. The occurrences inside liblinear/ are not relevant as nmap stores the library code inside it. Finally we have two source code files FPModel.cc and FPEngine.cc which draw our attention (‘FP’ means ‘finger-printing’). In the folder we can also find two headers FPModel.h and FPEngine.h.

In FPModel.h, five objects are introduced.

<span class="k js-evernote-checked" data-evernote-id="343">extern</span> <span class="k js-evernote-checked" data-evernote-id="344">struct</span> <span class="n js-evernote-checked" data-evernote-id="345">model</span> <span class="n js-evernote-checked" data-evernote-id="346">FPModel</span><span class="p js-evernote-checked" data-evernote-id="347">;</span>
<span class="k js-evernote-checked" data-evernote-id="348">extern</span> <span class="kt js-evernote-checked" data-evernote-id="349">double</span> <span class="n js-evernote-checked" data-evernote-id="350">FPscale</span><span class="p js-evernote-checked" data-evernote-id="351">[][</span><span class="mi js-evernote-checked" data-evernote-id="352">2</span><span class="p js-evernote-checked" data-evernote-id="353">];</span>
<span class="k js-evernote-checked" data-evernote-id="354">extern</span> <span class="kt js-evernote-checked" data-evernote-id="355">double</span> <span class="n js-evernote-checked" data-evernote-id="356">FPmean</span><span class="p js-evernote-checked" data-evernote-id="357">[][</span><span class="mi js-evernote-checked" data-evernote-id="358">695</span><span class="p js-evernote-checked" data-evernote-id="359">];</span>
<span class="k js-evernote-checked" data-evernote-id="360">extern</span> <span class="kt js-evernote-checked" data-evernote-id="361">double</span> <span class="n js-evernote-checked" data-evernote-id="362">FPvariance</span><span class="p js-evernote-checked" data-evernote-id="363">[][</span><span class="mi js-evernote-checked" data-evernote-id="364">695</span><span class="p js-evernote-checked" data-evernote-id="365">];</span>
<span class="k js-evernote-checked" data-evernote-id="366">extern</span> <span class="n js-evernote-checked" data-evernote-id="367">FingerMatch</span> <span class="n js-evernote-checked" data-evernote-id="368">FPmatches</span><span class="p js-evernote-checked" data-evernote-id="369">[];</span>

The FPEngine.h is quite richer and notably defines an FPEngine object

<span class="cm js-evernote-checked" data-evernote-id="373">/* This class is the generic fingerprinting engine. */</span>
<span class="k js-evernote-checked" data-evernote-id="374">class</span> <span class="nc js-evernote-checked" data-evernote-id="375">FPEngine</span> <span class="p js-evernote-checked" data-evernote-id="376">{</span>

<span class="k js-evernote-checked" data-evernote-id="377">protected</span><span class="o js-evernote-checked" data-evernote-id="378">:</span>
  <span class="kt js-evernote-checked" data-evernote-id="379">size_t</span> <span class="n js-evernote-checked" data-evernote-id="380">osgroup_size</span><span class="p js-evernote-checked" data-evernote-id="381">;</span>

<span class="k js-evernote-checked" data-evernote-id="382">public</span><span class="o js-evernote-checked" data-evernote-id="383">:</span>
  <span class="n js-evernote-checked" data-evernote-id="384">FPEngine</span><span class="p js-evernote-checked" data-evernote-id="385">();</span>
  <span class="o js-evernote-checked" data-evernote-id="386">~</span><span class="n js-evernote-checked" data-evernote-id="387">FPEngine</span><span class="p js-evernote-checked" data-evernote-id="388">();</span>
  <span class="kt js-evernote-checked" data-evernote-id="389">void</span> <span class="nf js-evernote-checked" data-evernote-id="390">reset</span><span class="p js-evernote-checked" data-evernote-id="391">();</span>
  <span class="k js-evernote-checked" data-evernote-id="392">virtual</span> <span class="kt js-evernote-checked" data-evernote-id="393">int</span> <span class="nf js-evernote-checked" data-evernote-id="394">os_scan</span><span class="p js-evernote-checked" data-evernote-id="395">(</span><span class="n js-evernote-checked" data-evernote-id="396">std</span><span class="o js-evernote-checked" data-evernote-id="397">::</span><span class="n js-evernote-checked" data-evernote-id="398">vector</span><span class="o js-evernote-checked" data-evernote-id="399">&lt;</span><span class="n js-evernote-checked" data-evernote-id="400">Target</span> <span class="o js-evernote-checked" data-evernote-id="401">*&gt;</span> <span class="o js-evernote-checked" data-evernote-id="402">&amp;</span><span class="n js-evernote-checked" data-evernote-id="403">Targets</span><span class="p js-evernote-checked" data-evernote-id="404">)</span> <span class="o js-evernote-checked" data-evernote-id="405">=</span> <span class="mi js-evernote-checked" data-evernote-id="406">0</span><span class="p js-evernote-checked" data-evernote-id="407">;</span>
  <span class="k js-evernote-checked" data-evernote-id="408">const</span> <span class="kt js-evernote-checked" data-evernote-id="409">char</span> <span class="o js-evernote-checked" data-evernote-id="410">*</span><span class="nf js-evernote-checked" data-evernote-id="411">bpf_filter</span><span class="p js-evernote-checked" data-evernote-id="412">(</span><span class="n js-evernote-checked" data-evernote-id="413">std</span><span class="o js-evernote-checked" data-evernote-id="414">::</span><span class="n js-evernote-checked" data-evernote-id="415">vector</span><span class="o js-evernote-checked" data-evernote-id="416">&lt;</span><span class="n js-evernote-checked" data-evernote-id="417">Target</span> <span class="o js-evernote-checked" data-evernote-id="418">*&gt;</span> <span class="o js-evernote-checked" data-evernote-id="419">&amp;</span><span class="n js-evernote-checked" data-evernote-id="420">Targets</span><span class="p js-evernote-checked" data-evernote-id="421">);</span>

<span class="p js-evernote-checked" data-evernote-id="422">};</span>

Looking at the methods of FPEngine, we start to understand the purpose of liblinear: OS detection, which is a very nice and powerful feature in practice, isn’t it?

A fingerprint model

While the header FPModel.h is rather small, the objects it declares are really heavy! They are all implemented in FPModel.cc. In particular the FPModel structure is an instance of a model defined in liblinear.h:

<span class="k js-evernote-checked" data-evernote-id="427">struct</span> <span class="n js-evernote-checked" data-evernote-id="428">model</span>
<span class="p js-evernote-checked" data-evernote-id="429">{</span>
    <span class="k js-evernote-checked" data-evernote-id="430">struct</span> <span class="n js-evernote-checked" data-evernote-id="431">parameter</span> <span class="n js-evernote-checked" data-evernote-id="432">param</span><span class="p js-evernote-checked" data-evernote-id="433">;</span>
    <span class="kt js-evernote-checked" data-evernote-id="434">int</span> <span class="n js-evernote-checked" data-evernote-id="435">nr_class</span><span class="p js-evernote-checked" data-evernote-id="436">;</span>       <span class="cm js-evernote-checked" data-evernote-id="437">/* number of classes */</span>
    <span class="kt js-evernote-checked" data-evernote-id="438">int</span> <span class="n js-evernote-checked" data-evernote-id="439">nr_feature</span><span class="p js-evernote-checked" data-evernote-id="440">;</span>
    <span class="kt js-evernote-checked" data-evernote-id="441">double</span> <span class="o js-evernote-checked" data-evernote-id="442">*</span><span class="n js-evernote-checked" data-evernote-id="443">w</span><span class="p js-evernote-checked" data-evernote-id="444">;</span>
    <span class="kt js-evernote-checked" data-evernote-id="445">int</span> <span class="o js-evernote-checked" data-evernote-id="446">*</span><span class="n js-evernote-checked" data-evernote-id="447">label</span><span class="p js-evernote-checked" data-evernote-id="448">;</span>     <span class="cm js-evernote-checked" data-evernote-id="449">/* label of each class */</span>
    <span class="kt js-evernote-checked" data-evernote-id="450">double</span> <span class="n js-evernote-checked" data-evernote-id="451">bias</span><span class="p js-evernote-checked" data-evernote-id="452">;</span>
<span class="p js-evernote-checked" data-evernote-id="453">};</span>

The FPModel has the following attribute values:

<span class="k js-evernote-checked" data-evernote-id="457">struct</span> <span class="n js-evernote-checked" data-evernote-id="458">model</span> <span class="n js-evernote-checked" data-evernote-id="459">FPModel</span> <span class="o js-evernote-checked" data-evernote-id="460">=</span> <span class="p js-evernote-checked" data-evernote-id="461">{</span>
    <span class="p js-evernote-checked" data-evernote-id="462">{</span><span class="mi js-evernote-checked" data-evernote-id="463">0</span><span class="p js-evernote-checked" data-evernote-id="464">},</span>            <span class="c1 js-evernote-checked" data-evernote-id="465">// L2R_LR solver </span>
                    <span class="c1 js-evernote-checked" data-evernote-id="466">// (L2-regularized classifiers </span>
                    <span class="c1 js-evernote-checked" data-evernote-id="467">// and logistic regression)</span>
    <span class="mi js-evernote-checked" data-evernote-id="468">96</span><span class="p js-evernote-checked" data-evernote-id="469">,</span>             <span class="c1 js-evernote-checked" data-evernote-id="470">// number of classes</span>
    <span class="mi js-evernote-checked" data-evernote-id="471">695</span><span class="p js-evernote-checked" data-evernote-id="472">,</span>            <span class="c1 js-evernote-checked" data-evernote-id="473">// number of features</span>
    <span class="n js-evernote-checked" data-evernote-id="474">_w</span><span class="p js-evernote-checked" data-evernote-id="475">,</span>             <span class="c1 js-evernote-checked" data-evernote-id="476">// weights (695 x 96 values)</span>
    <span class="n js-evernote-checked" data-evernote-id="477">_labels</span><span class="p js-evernote-checked" data-evernote-id="478">,</span>        <span class="c1 js-evernote-checked" data-evernote-id="479">// index of the classes ([0, 1 ..., 95])</span>
    <span class="o js-evernote-checked" data-evernote-id="480">-</span><span class="mf js-evernote-checked" data-evernote-id="481">1.00000000</span>     <span class="c1 js-evernote-checked" data-evernote-id="482">// -1 means no bias</span>
<span class="p js-evernote-checked" data-evernote-id="483">};</span>

What do we learn? Nmap embeds a classification task. For every observation, i.e. a vector of size 695 (695 features), the goal is to find the class that best matches it among the 96 available.

What are the observations? Basically, they are the fingerprints. They are represented by 695 features.

What are the features? The 695 values are the results of network scanning.

What are the classes? The array FPmatches notably details the 96 classes: they are OS with specific version (for instance we have “Linux 2.6.11 – 2.6.15” and “Linux 2.6.32 – 2.6.39” but also “Netgear DGN3300v2 ADSL router”).

Currently, we don’t know exactly how the classification is performed, but it will basically use the local variable _w which is a big weights array. In a word, we can say that the classification model is stored in _w.

Three structures have not been unveiled: FPscale, FPmean and FPvariance. Let us have a look to the first:

<span class="cm js-evernote-checked" data-evernote-id="492">/* Scale parameters are pairs (a, b). A scaled value x' is calculated from</span>
<span class="cm js-evernote-checked" data-evernote-id="493">   a, b, and an observed x by x' = (x + a) * b. */</span>
<span class="kt js-evernote-checked" data-evernote-id="494">double</span> <span class="n js-evernote-checked" data-evernote-id="495">FPscale</span><span class="p js-evernote-checked" data-evernote-id="496">[][</span><span class="mi js-evernote-checked" data-evernote-id="497">2</span><span class="p js-evernote-checked" data-evernote-id="498">]</span> <span class="o js-evernote-checked" data-evernote-id="499">=</span> <span class="p js-evernote-checked" data-evernote-id="500">{</span>
    <span class="p js-evernote-checked" data-evernote-id="501">{</span>        <span class="o js-evernote-checked" data-evernote-id="502">-</span><span class="mi js-evernote-checked" data-evernote-id="503">20</span><span class="p js-evernote-checked" data-evernote-id="504">,</span>  <span class="mf js-evernote-checked" data-evernote-id="505">0.0416667</span> <span class="p js-evernote-checked" data-evernote-id="506">},</span> <span class="cm js-evernote-checked" data-evernote-id="507">/* S1.PLEN */</span>
    <span class="p js-evernote-checked" data-evernote-id="508">{</span>         <span class="o js-evernote-checked" data-evernote-id="509">-</span><span class="mi js-evernote-checked" data-evernote-id="510">0</span><span class="p js-evernote-checked" data-evernote-id="511">,</span> <span class="mf js-evernote-checked" data-evernote-id="512">0.00520833</span> <span class="p js-evernote-checked" data-evernote-id="513">},</span> <span class="cm js-evernote-checked" data-evernote-id="514">/* S1.TC */</span>
    <span class="p js-evernote-checked" data-evernote-id="515">{</span>        <span class="o js-evernote-checked" data-evernote-id="516">-</span><span class="mi js-evernote-checked" data-evernote-id="517">64</span><span class="p js-evernote-checked" data-evernote-id="518">,</span>  <span class="mf js-evernote-checked" data-evernote-id="519">0.0052356</span> <span class="p js-evernote-checked" data-evernote-id="520">},</span> <span class="cm js-evernote-checked" data-evernote-id="521">/* S1.HLIM */</span>
    <span class="p js-evernote-checked" data-evernote-id="522">{</span>        <span class="o js-evernote-checked" data-evernote-id="523">-</span><span class="mi js-evernote-checked" data-evernote-id="524">20</span><span class="p js-evernote-checked" data-evernote-id="525">,</span>  <span class="mf js-evernote-checked" data-evernote-id="526">0.0416667</span> <span class="p js-evernote-checked" data-evernote-id="527">},</span> <span class="cm js-evernote-checked" data-evernote-id="528">/* S2.PLEN */</span>
    <span class="p js-evernote-checked" data-evernote-id="529">{</span>         <span class="o js-evernote-checked" data-evernote-id="530">-</span><span class="mi js-evernote-checked" data-evernote-id="531">0</span><span class="p js-evernote-checked" data-evernote-id="532">,</span> <span class="mf js-evernote-checked" data-evernote-id="533">0.00520833</span> <span class="p js-evernote-checked" data-evernote-id="534">},</span> <span class="cm js-evernote-checked" data-evernote-id="535">/* S2.TC */</span>
        <span class="p js-evernote-checked" data-evernote-id="536">...</span>

The structure is a 695×2 matrix. At each line, a comment describes a quantitative feature used for OS detection. The values inside a row aim to “scale” the value of the feature.


Note

When we want to classify data according to some features, it is often relevant to balance their impact. In particular, if a feature lies between 1000 and 10000, it is likely to carry more weight during the classifier training than a feature stuck between 0 and 1. Therefore, features are “scaled” to make them lie in the same range.


Even if the use of these coefficients a and b are weird (I would rather use a mean and a standard deviation), they do the job (the author’s comment details how features are scaled).

FPscale normalizes the data, so… what is the purpose of FPmean and FPvariance ??! We will see that later.

Classifying the fingerprint

While FPModel.cc gathers data, detection procedures are implemented in FPEngine.cc. The function we are interested in is:

<span class="k js-evernote-checked" data-evernote-id="546">static</span> <span class="kt js-evernote-checked" data-evernote-id="547">void</span> <span class="n js-evernote-checked" data-evernote-id="548">classify</span><span class="p js-evernote-checked" data-evernote-id="549">(</span><span class="n js-evernote-checked" data-evernote-id="550">FingerPrintResultsIPv6</span> <span class="o js-evernote-checked" data-evernote-id="551">*</span><span class="n js-evernote-checked" data-evernote-id="552">FPR</span><span class="p js-evernote-checked" data-evernote-id="553">)</span>

Here we see a specificity: nmap uses this ML process only to classify IPv6 fingerprints. For IPv4 fingerprints, it actually tests them against fingerprint references (you can find at /usr/share/nmap/nmap-os-db). More details can be found at OS Matching Algorithm.

The classify function takes a fingerprint as input and stores the basic classification result inside the FPR->overall_results attribute (OSSCAN_NOMATCHES or OSSCAN_SUCCESS). More elements are stored in other attributes like FPR->matches or FPR->accuracy. Let us describe how this function treats a fingerprint:

First, it retrieves the features from the fingerprint itself:

<span class="n js-evernote-checked" data-evernote-id="559">features</span> <span class="o js-evernote-checked" data-evernote-id="560">=</span> <span class="n js-evernote-checked" data-evernote-id="561">vectorize</span><span class="p js-evernote-checked" data-evernote-id="562">(</span><span class="n js-evernote-checked" data-evernote-id="563">FPR</span><span class="p js-evernote-checked" data-evernote-id="564">);</span>

Then features are scaled:

<span class="n js-evernote-checked" data-evernote-id="568">apply_scale</span><span class="p js-evernote-checked" data-evernote-id="569">(</span><span class="n js-evernote-checked" data-evernote-id="570">features</span><span class="p js-evernote-checked" data-evernote-id="571">,</span> <span class="n js-evernote-checked" data-evernote-id="572">get_nr_feature</span><span class="p js-evernote-checked" data-evernote-id="573">(</span><span class="o js-evernote-checked" data-evernote-id="574">&amp;</span><span class="n js-evernote-checked" data-evernote-id="575">FPModel</span><span class="p js-evernote-checked" data-evernote-id="576">),</span> <span class="n js-evernote-checked" data-evernote-id="577">FPscale</span><span class="p js-evernote-checked" data-evernote-id="578">);</span>

Finally some scores are computed by the algorithms inside liblinear. Every score estimates the similarity between the current fingerprint (features) and a given OS. As these scores come from a logistic regression, they can be turned into probabilities.

<span class="n js-evernote-checked" data-evernote-id="582">predict_values</span><span class="p js-evernote-checked" data-evernote-id="583">(</span><span class="o js-evernote-checked" data-evernote-id="584">&amp;</span><span class="n js-evernote-checked" data-evernote-id="585">FPModel</span><span class="p js-evernote-checked" data-evernote-id="586">,</span> <span class="n js-evernote-checked" data-evernote-id="587">features</span><span class="p js-evernote-checked" data-evernote-id="588">,</span> <span class="n js-evernote-checked" data-evernote-id="589">values</span><span class="p js-evernote-checked" data-evernote-id="590">);</span>
  <span class="k js-evernote-checked" data-evernote-id="591">for</span> <span class="p js-evernote-checked" data-evernote-id="592">(</span><span class="n js-evernote-checked" data-evernote-id="593">i</span> <span class="o js-evernote-checked" data-evernote-id="594">=</span> <span class="mi js-evernote-checked" data-evernote-id="595">0</span><span class="p js-evernote-checked" data-evernote-id="596">;</span> <span class="n js-evernote-checked" data-evernote-id="597">i</span> <span class="o js-evernote-checked" data-evernote-id="598">&lt;</span> <span class="n js-evernote-checked" data-evernote-id="599">nr_class</span><span class="p js-evernote-checked" data-evernote-id="600">;</span> <span class="n js-evernote-checked" data-evernote-id="601">i</span><span class="o js-evernote-checked" data-evernote-id="602">++</span><span class="p js-evernote-checked" data-evernote-id="603">)</span> <span class="p js-evernote-checked" data-evernote-id="604">{</span>
    <span class="n js-evernote-checked" data-evernote-id="605">labels</span><span class="p js-evernote-checked" data-evernote-id="606">[</span><span class="n js-evernote-checked" data-evernote-id="607">i</span><span class="p js-evernote-checked" data-evernote-id="608">].</span><span class="n js-evernote-checked" data-evernote-id="609">label</span> <span class="o js-evernote-checked" data-evernote-id="610">=</span> <span class="n js-evernote-checked" data-evernote-id="611">i</span><span class="p js-evernote-checked" data-evernote-id="612">;</span>
    <span class="n js-evernote-checked" data-evernote-id="613">labels</span><span class="p js-evernote-checked" data-evernote-id="614">[</span><span class="n js-evernote-checked" data-evernote-id="615">i</span><span class="p js-evernote-checked" data-evernote-id="616">].</span><span class="n js-evernote-checked" data-evernote-id="617">prob</span> <span class="o js-evernote-checked" data-evernote-id="618">=</span> <span class="mf js-evernote-checked" data-evernote-id="619">1.0</span> <span class="o js-evernote-checked" data-evernote-id="620">/</span> <span class="p js-evernote-checked" data-evernote-id="621">(</span><span class="mf js-evernote-checked" data-evernote-id="622">1.0</span> <span class="o js-evernote-checked" data-evernote-id="623">+</span> <span class="n js-evernote-checked" data-evernote-id="624">exp</span><span class="p js-evernote-checked" data-evernote-id="625">(</span><span class="o js-evernote-checked" data-evernote-id="626">-</span><span class="n js-evernote-checked" data-evernote-id="627">values</span><span class="p js-evernote-checked" data-evernote-id="628">[</span><span class="n js-evernote-checked" data-evernote-id="629">i</span><span class="p js-evernote-checked" data-evernote-id="630">]));</span>
  <span class="p js-evernote-checked" data-evernote-id="631">}</span>

All these probabilities are then sorted in descending order. The OS giving the highest one is considered as a perfect match. Moreover, all the OS getting a probability higher that 90% of the highest probability are also considered as perfect matches.

<span class="k js-evernote-checked" data-evernote-id="636">if</span> <span class="p js-evernote-checked" data-evernote-id="637">(</span><span class="n js-evernote-checked" data-evernote-id="638">labels</span><span class="p js-evernote-checked" data-evernote-id="639">[</span><span class="n js-evernote-checked" data-evernote-id="640">i</span><span class="p js-evernote-checked" data-evernote-id="641">].</span><span class="n js-evernote-checked" data-evernote-id="642">prob</span> <span class="o js-evernote-checked" data-evernote-id="643">&gt;=</span> <span class="mf js-evernote-checked" data-evernote-id="644">0.90</span> <span class="o js-evernote-checked" data-evernote-id="645">*</span> <span class="n js-evernote-checked" data-evernote-id="646">labels</span><span class="p js-evernote-checked" data-evernote-id="647">[</span><span class="mi js-evernote-checked" data-evernote-id="648">0</span><span class="p js-evernote-checked" data-evernote-id="649">].</span><span class="n js-evernote-checked" data-evernote-id="650">prob</span><span class="p js-evernote-checked" data-evernote-id="651">)</span>
      <span class="n js-evernote-checked" data-evernote-id="652">FPR</span><span class="o js-evernote-checked" data-evernote-id="653">-&gt;</span><span class="n js-evernote-checked" data-evernote-id="654">num_perfect_matches</span> <span class="o js-evernote-checked" data-evernote-id="655">=</span> <span class="n js-evernote-checked" data-evernote-id="656">i</span> <span class="o js-evernote-checked" data-evernote-id="657">+</span> <span class="mi js-evernote-checked" data-evernote-id="658">1</span><span class="p js-evernote-checked" data-evernote-id="659">;</span>

After checking all the scores, the code looks at the number of perfect matches. If there is not exactly one perfect match, OSSCAN_NOMATCHES is set (meaning that OS has not been detected). It is rather strange because if we have two or more matches, we unfortunately get no result. However, this event could possibly not happen once classes are well separated.

What does liblinear do exactly?

As we said liblinear provides several solvers to perform linear regression/classification. nmap uses a regularized logistic regression to classify fingerprints. Logistic regression is basically used for a 2-classes classification task, but it can be extended to greater number of classes with a one-versus-all approach (this is what liblinear does).


Note

Let us consider a classification problem with k classes. The one-versus-all strategy consists in building k binary classifiers. The binary classifier i tries to separate the observations belonging to the class i from all the other observations. When a new observation has to be classified, each classifier can provide the probability p_i that it belongs to the class i. The class with maximum probability is naturally kept.


Best does not mean right

The function classify actually goes deeper as it performs another check on the best found OS. Precisely, it calls the function novelty_of and check its output:

<span class="n js-evernote-checked" data-evernote-id="669">novelty</span> <span class="o js-evernote-checked" data-evernote-id="670">=</span> <span class="n js-evernote-checked" data-evernote-id="671">novelty_of</span><span class="p js-evernote-checked" data-evernote-id="672">(</span><span class="n js-evernote-checked" data-evernote-id="673">features</span><span class="p js-evernote-checked" data-evernote-id="674">,</span> <span class="n js-evernote-checked" data-evernote-id="675">labels</span><span class="p js-evernote-checked" data-evernote-id="676">[</span><span class="mi js-evernote-checked" data-evernote-id="677">0</span><span class="p js-evernote-checked" data-evernote-id="678">].</span><span class="n js-evernote-checked" data-evernote-id="679">label</span><span class="p js-evernote-checked" data-evernote-id="680">);</span>
<span class="p js-evernote-checked" data-evernote-id="681">...</span>
<span class="k js-evernote-checked" data-evernote-id="682">if</span> <span class="p js-evernote-checked" data-evernote-id="683">(</span><span class="n js-evernote-checked" data-evernote-id="684">novelty</span> <span class="o js-evernote-checked" data-evernote-id="685">&lt;</span> <span class="n js-evernote-checked" data-evernote-id="686">FP_NOVELTY_THRESHOLD</span><span class="p js-evernote-checked" data-evernote-id="687">)</span> <span class="p js-evernote-checked" data-evernote-id="688">{</span>
      <span class="n js-evernote-checked" data-evernote-id="689">FPR</span><span class="o js-evernote-checked" data-evernote-id="690">-&gt;</span><span class="n js-evernote-checked" data-evernote-id="691">overall_results</span> <span class="o js-evernote-checked" data-evernote-id="692">=</span> <span class="n js-evernote-checked" data-evernote-id="693">OSSCAN_SUCCESS</span><span class="p js-evernote-checked" data-evernote-id="694">;</span>
    <span class="p js-evernote-checked" data-evernote-id="695">}</span> <span class="k js-evernote-checked" data-evernote-id="696">else</span> <span class="p js-evernote-checked" data-evernote-id="697">{</span>
      <span class="p js-evernote-checked" data-evernote-id="698">...</span>
      <span class="n js-evernote-checked" data-evernote-id="699">FPR</span><span class="o js-evernote-checked" data-evernote-id="700">-&gt;</span><span class="n js-evernote-checked" data-evernote-id="701">overall_results</span> <span class="o js-evernote-checked" data-evernote-id="702">=</span> <span class="n js-evernote-checked" data-evernote-id="703">OSSCAN_NOMATCHES</span><span class="p js-evernote-checked" data-evernote-id="704">;</span>
      <span class="p js-evernote-checked" data-evernote-id="705">...</span>
    <span class="p js-evernote-checked" data-evernote-id="706">}</span>

What does this function do? It actually computes a distance between our observation (features) and the group of all the observations belonging the predicted class (given through labels[0].label).

For that purpose, it uses… FPmean and FPVariance structures! They are merely 96×695 matrices where FPmean[i][j] represents the mean of the feature j for the OS i (FPvariance gives the corresponding variance).

Actually, the computed distance looks like the Mahalanobis distance except that it does not take into account the correlation between features. In a way, it assumes that there is no linear relation between the features (a stronger assumption would be that they are indepedent): for sure this is a wrong assumption but it does not prevent it from working in practice.


Note

The code is well documented and authors give many details about this function. In particular, they explain they use this approximation to “save space”.


As we see, it uses a harcoded threshold, defined in FPEngine.h:

<span class="cp js-evernote-checked" data-evernote-id="716">#define FP_NOVELTY_THRESHOLD 15.0</span>

Eventually, the whole approach is rather relevant: the fingerprint is compared to all the OS. The “closest” OS (the one with highest probability) is kept but we ensure that the fingerprint is not too far from other observations sharing this OS.

Conclusion

OS detection is obviously a powerful feature of nmap, that is also why we could be curious to know how it works. While IPv4 fingerprints are basically tested against some references, a ML-based approach is used to classify IPv6 ones.

Forget about fancy deep stuff algorithms! nmap uses a logistic regression to estimate the probability of a fingerprint to belong to a certain class (OS). Furthermore, it computes a novelty score which aims to avoid misclassification. The whole process is summed up below (for official details, you can look at IPv6 Matching )

Figure 1: IPv6 OS detection process in nmap

Despite its practical efficiency, we can do a couple of remarks:

  • The model (all the blue elements) is totally static, a bit crude (raw values) and hard to enrich by ourself.
  • Some aspects are more about hacking: uncommon pipeline, use of hardcoded constants and strange choices (about the number of matches especially).

I personnally think that we could improve the whole detection engine (using also an ML approach for IPv4). The key elements would be to use a more flexible design and to set up a more “modern” detection workflow (to try it at least). I develop some of these ideas in the last paragraph.

Ideas to improve NMAP

The built-in nmap model is very powerful as it takes into account almost hundred OS. However it is static and possibly updated only when a new version is available.

Imagine you have fingerprints of very uncommon systems like SCADA or seldom firmwares. You have to submit this fingerprint to update the whole model (updating the model locally is rather hard). You possibly don’t want to share your fingerprints or even you are very motivated, it will take time as we can see in that post of Daniel Miller:

“[…] integrating user-submitted fingerprints is a manual process that takes several weeks of dedicated time to accomplish each year. We (the Nmap developers) are always looking for ways to improve this process and make more frequent updates, but generally there are only 2 releases per year.”

So the idea would be to have a more dynamic model that every nmap user can enrich (interactively for instance) and possibly share. A solution would be to have a fingerprint database. Yes, it already exists: /usr/share/nmap/nmap-os-db, but this is for IPv4 fingerprints. Moreover fingerprints should be stored directly as a feature vector. Thus, the model could be easily rebuilt by the user.

Furthermore, the current process computes the novelty of a new fingerprint but this piece of information is not exploited either while it could improve our detection engine.

Can we use a even more powerful workflow? It is hard to talk about the choice of the algorithms. The number of features is quite high (695), but even if liblinear is made for large scale datasets, I would rather have a look to other classifiers like Random Forest which does not need scaled data, may select the most relevant features and fits all the classes at the same time (no one-versus-all approach). About novelty detection, the computed distance is a bit uncommon. From the random forest classifier, it is possible to set a kind of “certainty threshold” below which the fingerprint is not classified. Another way would be to compute the Local Outlier Factor (LOF) to check if the fingerprint looks like its neighbours (but it requires several labeled samples).

转自:https://blog.amossys.fr/nmap-ml.html

转载请注明:jinglingshu的博客 » Machine Learning in Nmap


Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in /usr/share/nginx/html/wp-content/themes/d8/comments.php on line 17
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址