AI network attack classifier

A multi-class model trained on 220,000 raw HTTP payloads to label brute force, SQL injection, XSS and command execution attacks without static signature rules, and an honest account of where it fails.

Overall accuracy
92.6%
Macro F1
0.72
Payloads
220,000
Command execution recall
0.02

The headline and the weakness, side by side: accuracy is flattered by brute force, which is 61.4% of the data.

Period
Nov to Dec 2024
Context
KISA AI Security Monitoring Specialist Training Program, 3rd batch
Stack
Python, scikit-learn, XGBoost, pandas, Jupyter Lab
Open
Command execution almost entirely missed; 5 improvements planned

Overview

Traditional network security systems rely on rule-based signature matching, which needs constant manual updates and misses novel attack variants. This project replaces that approach with a machine learning classifier trained on raw HTTP payloads from network security appliance logs.

The model classifies each payload into one of five categories: brute force, cross-site scripting, SQL injection, system command execution or normal. It was built during the KISA AI Security Monitoring Specialist Training Program in South Korea.

Dataset

LabelAttack typeCountShare
Brute_ForceCredential stuffing with automated tools (Hydra); 100% POST requests135,03761.4%
normalLegitimate traffic baseline44,00020.0%
Cross_Site_ScriptingXSS from scanner tools (Arachni, Nikto); 97% GET requests16,5427.5%
SQL_InjectionSQLmap-driven injection attempts; HTTP/1.1 only12,5255.7%
System_Cmd_ExecutionOS command injection, identified by the Connector user agent11,8965.4%
Total220,000100%

Feature engineering

Rather than vectorising raw text, which is computationally expensive at this scale, we ran a statistical frequency analysis across label and payload pairs to find keywords that appeared with high discriminative frequency for specific attack classes. Nine binary or categorical features were extracted from each payload string.

FeatureWhat it encodesSignal
methodHTTP method (GET = 1, POST = 0)Brute force is 100% POST
verHTTP version (1.0 = 1, 1.1 = 0)Brute force skews to HTTP/1.0
wordOS or tool keyword: hydra (1), linux (2), win (3), mac (4), nikto (5)hydra appears only in brute force
sql“sqlmap” in the payloadStrong indicator of SQL injection
ar“arachni” scanner presentXSS and command execution
uid“userid” parameter presentBrute force login targeting
zapOWASP ZAP scanner presentActive scanning, brute force
connector“connector” user agent presentCommand execution only
apple“applewebkit” user agent presentDistinguishes normal traffic

Preprocessing pipeline

# Feature extraction and encoding pipeline
def prepro(data):
    # Structural features from payload token positions
    data['method'] = data['payload'].str.split().str[0]  # GET / POST
    data['ver']    = data['payload'].str.split().str[1]  # HTTP version

    # Keyword-based binary feature extraction
    data['sql']       = data['payload'].apply(add_sql)    # sqlmap presence
    data['word']      = data['payload'].apply(add_word)   # OS/tool keyword
    data['ar']        = data['payload'].apply(add_ar)     # arachni scanner
    data['uid']       = data['payload'].apply(add_uid)    # userid param
    data['connector'] = data['payload'].apply(add_con)    # connector UA
    data['zap']       = data['payload'].apply(add_zap)    # ZAP scanner
    data['apple']     = data['payload'].apply(add_apple)  # AppleWebKit UA

    # Label encoding
    data['ver']    = data['ver'].map({'HTTP/1.1': 0, 'HTTP/1.0': 1})
    data['method'] = data['method'].map({'POST': 0, 'GET': 1})

    return data[['method', 'ver', 'word', 'uid', 'ar', 'sql', 'connector', 'zap', 'apple']], tg

Model training

Train and test split80% / 20%, stratified by label
Random seed42
Test set size44,000 samples
StratificationClass distribution preserved across splits
HardwareIntel i9-13900, 64 GB RAM, RTX 4070
EnvironmentPython 3.12.7, Anaconda, Jupyter Lab

Why Random Forest

Three classifiers were evaluated. Random Forest, a bagging ensemble, was chosen as the primary model: it matched XGBoost’s overall accuracy, beat Logistic Regression, and trained significantly faster. Randomly sampling subsets of the data and averaging across parallel trees proved more stable on this imbalanced dataset.

Results

Overall accuracy on the 44,000-sample test set
ModelAccuracy
Random Forest92.6%
XGBoost92.6%
Logistic Regression91.7%
Random Forest, per-class report
ClassPrecisionRecallF1Support
Brute_Force0.991.000.9927,008
normal0.930.990.968,800
SQL_Injection1.000.870.932,505
Cross_Site_Scripting0.550.870.673,308
System_Cmd_Execution1.000.020.042,379
Weighted average0.950.930.9144,000

Known limitations

Command execution is almost entirely missed

Recall for System_Cmd_Execution is 0.02 (F1 0.04). This comes from the keyword-based feature engineering: the connector feature, while unique to this class, was not enough as a sole discriminator. It is also the smallest class (5.4%), which amplifies the imbalance.

Class imbalance flatters the headline

Brute force makes up 61.4% of samples, pulling overall accuracy up. The weighted F1 of 0.91 leans on brute force’s 0.99; the macro F1 of 0.72 is the truer measure of performance across classes.

Future improvements

  1. Replace keyword features with TF-IDF or embedding vectorisation of the full payload, removing manual keyword curation and capturing richer signal.
  2. Apply SMOTE or class-weight balancing to counter brute-force dominance and lift command execution recall.
  3. Tune hyperparameters with GridSearchCV or Bayesian optimisation (Optuna) to improve XSS and command execution.
  4. Evaluate on external datasets (UNSW-NB15, KISTI Network Intrusion) to test generalisation beyond the training domain.
  5. Explore sequence-aware deep learning (LSTM or transformer-based) for payload classification.

Next case study

CyberShield