EZ LearnEZ Learn
Courses
EZ Board
Dashboard
All articles
ml-for-cyber-securitymlcsnetwork-trafficfeature-engineering

Engineering Features from Network Traffic

Raw packet captures are not features; they are bytes on a wire. A network flow has to be summarised into a fixed-length numeric vector before any model ever sees it.

Ashish Revar25 October 20265 min read

Raw packet captures are not features; they are bytes on a wire. A network flow, one conversation between two endpoints, is usually summarised into a fixed-length numeric vector before any model sees it.

FeatureWhat it captures
Flow durationVery short flows often indicate scanning; very long ones can indicate exfiltration or a maintained backdoor
Packets per secondA sudden spike relative to a host's baseline often indicates a flood or scan
Mean and variance of packet sizeAutomated tools tend to send suspiciously uniform packet sizes compared to human-driven traffic
Ratio of bytes sent to receivedA ratio far from what a given protocol normally produces can indicate exfiltration or beaconing
TCP flag counts (SYN, RST, FIN)An unusual ratio of SYNs to completed handshakes is a classic scan signature

These are the same style of feature used to build the NSL-KDD and CICIDS benchmark datasets this course cites in Unit 6.

flow_features.py

import numpy as np

def flow_features(packets: list[dict]) -> dict:
    """packets: list of {'ts': float, 'size': int, 'direction': 'out'|'in',
       'flags': set[str]} for one 5-tuple flow."""
    sizes = np.array([p["size"] for p in packets])
    timestamps = np.array([p["ts"] for p in packets])
    out_bytes = sum(p["size"] for p in packets if p["direction"] == "out")
    in_bytes = sum(p["size"] for p in packets if p["direction"] == "in")

    return {
        "duration": timestamps.max() - timestamps.min(),
        "packets_per_second": len(packets) / max(timestamps.max() - timestamps.min(), 1e-3),
        "mean_packet_size": sizes.mean(),
        "size_variance": sizes.var(),
        "bytes_out_in_ratio": out_bytes / max(in_bytes, 1),
        "syn_count": sum(1 for p in packets if "SYN" in p["flags"]),
        "rst_count": sum(1 for p in packets if "RST" in p["flags"]),
    }

Try it yourself: Spot the Scan

Two flows are summarised with flow_features. Flow A: duration 0.4 seconds, 38 SYN flags, 0 completed connections. Flow B: duration 340 seconds, mean packet size 1480 bytes with near-zero variance, bytes_out_in_ratio of 48. Which flow more likely represents a port scan, and which more likely represents automated data exfiltration? Name the specific field in each that gives it away.

Related to this topic

Continue learning

Reference material

eBook

ML for Cyber Security eBook

v1.0

Open resource

Cheatsheet

ML for Cyber Security Cheatsheet

v1.0

Open resource

MCQ Bank

ML for Cyber Security MCQ Bank

v1.0

Open resource

Question Bank

ML for Cyber Security Question Bank

v1.0

Open resource

Discussion

Loading comments…
More articlesTest your knowledge
EZ MentorAI · Free

Engineering Features from Network Traffic

Hi! I'm EZ Mentor

Your AI tutor for
Engineering Features from Network Traffic

Sign in to ask, it's free.

EZ Mentor is AI-powered · Answers may not be perfect · 10 free questions / day

EZ Learn

Structured learning across Reverse Engineering, Cloud Security and Research Methodology, plus games and practice tools. Free, and open to anyone.

Learn

  • Courses
  • EZ Articles
  • EZ Watch
  • EZ Bytes
  • EZ Talks
  • Community

Library & Exams

  • All Resources
  • eBooks & PDFs
  • Question Banks
  • Cheatsheets
  • All Exams
  • EZ Board

EZ Arena

  • EZ Games
  • Byte Rush
  • Firewall Defence
  • 3D Case Files
  • Live Quiz
  • ML Challenges

EZ Cyber Hygiene

  • Hygiene Journey
  • Cyber Safety Check
  • दिशा - Hygiene
  • सजग - Awareness
  • अभ्यास - Practice
  • Scammed? Call 1930

Platform

  • About
  • Contact
  • Events
  • Verify Certificate
  • Terms of Use
  • Disclaimer

© 2026 EZ Learn by EpochZero. Educational content for learning purposes.

Platform Lead: Ashish Revar