liveBook · Manning

Random forest

As explained in Cyber Threat Hunting

A random forest is an ensemble machine-learning algorithm that combines predictions from multiple decision trees trained with randomized sampling or feature selection.

A random forest is a supervised learning model that builds multiple decision trees and combines their predictions into a more robust final prediction. Each tree is trained on a random subset of the data, using features to predict an outcome. section 9.3

For DNS threat hunting, the model can classify requests as normal DNS traffic (class 0) or DNS-tunneling activity (classes 1–4). Its usefulness depends on preparation: collect relevant events, extract features, and supply good-quality labeled training data. section 9.3.3 section 9.7

A repeatable workflow is:

  • Train the classifier on PCA-generated training data and labels. section 9.3.1
  • Apply the training-fitted scaler and PCA transformation to test data rather than refitting them. section 9.3.2
  • Predict classes, then evaluate results with accuracy, F1 score, and a confusion matrix. section 9.3.2
  • Run the model on collected DNS requests and investigate events mapped to tunneling classes. section 9.3.3 section 9.3.6

Investigating suspicious DNS names

After classifying production DNS events, filter for classes other than 0 and inspect fully qualified domain names. Requests containing microsofts.info were predominantly mapped to a DNS-tunneling class, while 63 events were mapped to normal traffic and warranted review. section 9.3.6

Listing 9.20 Creating and training a Random Forest Classifier model on X_train_pca
from sklearn.ensemble import RandomForestClassifier                  #1
 
rf = RandomForestClassifier(n_estimators=100, \
    random_state=42)                                                 #2
rf.fit(X_train_pca, y_train)                                         #3

Creates a 100-tree scikit-learn classifier, fixes a random seed, and fits it to PCA-transformed training features and labels. section 9.3.1

How Cyber Threat Hunting covers it

How deep it goes
Explains ensemble decision trees and demonstrates training, testing, metrics, and production prediction; defers parameter tuning and deeper exploration.
Examples
Demonstrates DNS-tunneling classification and investigates microsofts.info requests through predicted class distributions.
What it assumes
Assumes labeled training and test data plus previously fitted scaling and PCA transformations.
What you can do after
Apply it via scikit-learn to train, evaluate, and use a DNS-request classifier for hunting clues.
How it connects
Connects supervised learning with decision trees, PCA, feature scaling, accuracy, F1 score, and confusion matrices.

Also explained in

Compare how 11 books explain random forest →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Cyber Threat Hunting. The section links point to the parts of the book it draws on.