NSL‑KDD is a curated revision of the KDD’99 intrusion‑detection dataset created by the Canadian Institute for Cybersecurity (UNB). It removes duplicated records and balances example difficulty so that machine‑learning algorithms can be compared more fairly.
Why it matters: KDD’99’s heavy duplication and imbalance could inflate accuracy. NSL‑KDD addresses these issues, making it suitable for pedagogy and benchmarking.
Each row (connection record) includes a label column at the end.
If the label equals normal. the record is benign; any other string denotes
a specific attack (e.g., neptune., smurf., nmap., guess_passwd.).
| Label value (string) | Meaning |
|---|---|
normal. | Benign (non‑attack) connection |
neptune., smurf., teardrop., back., land. | DoS attacks |
portsweep., ipsweep., nmap., satan. | Probe attacks |
guess_passwd., ftp_write., imap., phf. | R2L attacks |
buffer_overflow., loadmodule., rootkit., perl. | U2R attacks |
You can convert the string label to a binary flag (0=normal, 1=attack) like this:
import pandas as pd
cols = [f"f{i}" for i in range(1, 42)] + ["label"] # 41 features + label
df = pd.read_csv("KDDTrain+.txt", header=None, names=cols)
df["attack_flag"] = (df["label"] != "normal.").astype(int)
print(df["label"].value_counts().head())
print(df["attack_flag"].value_counts())
| File | Description | Records |
|---|---|---|
KDDTrain+.txt | Full training set | 125,973 |
KDDTest+.txt | Full test set | 22,544 |
KDDTrain+_20Percent.txt | 20% train subset | 25,192 |
KDDTest-21.txt | Test subset (excludes easiest) | 11,850 |
.arff variants | WEKA-ready versions | — |
Grouped as:
Three features are categorical: protocol_type, service, flag.
Use either binary (attack vs normal) or multi‑class (specific attack) targets.
KDDTrain+.txt, test on KDDTest+.txt (or KDDTest‑21.txt).protocol_type, service, flag.| No. | Feature Name | Description | Type | Category |
|---|---|---|---|---|
| 1 | duration | Length of the connection in seconds. | Continuous | Basic |
| 2 | protocol_type | Type of the protocol (e.g., tcp, udp, icmp). | Categorical | Basic |
| 3 | service | Network service on the destination (e.g., http, smtp, ftp, private). | Categorical | Basic |
| 4 | flag | Status of the connection (e.g., SF, REJ, S0). | Categorical | Basic |
| 5 | src_bytes | Number of data bytes from source to destination. | Continuous | Basic |
| 6 | dst_bytes | Number of data bytes from destination to source. | Continuous | Basic |
| 7 | land | 1 if connection is from/to the same host/port; 0 otherwise. | Binary | Basic |
| 8 | wrong_fragment | Number of 'wrong' fragments. | Continuous | Basic |
| 9 | urgent | Number of urgent packets. | Continuous | Basic |
| 10 | hot | Number of 'hot' indicators (e.g., successful login attempts). | Continuous | Content |
| 11 | num_failed_logins | Number of failed login attempts. | Continuous | Content |
| 12 | logged_in | 1 if successfully logged in; 0 otherwise. | Binary | Content |
| 13 | num_compromised | Number of 'compromised' conditions. | Continuous | Content |
| 14 | root_shell | 1 if root shell is obtained; 0 otherwise. | Binary | Content |
| 15 | su_attempted | 1 if 'su root' command was attempted; 0 otherwise. | Binary | Content |
| 16 | num_root | Number of root accesses. | Continuous | Content |
| 17 | num_file_creations | Number of file creation operations. | Continuous | Content |
| 18 | num_shells | Number of shell prompts. | Continuous | Content |
| 19 | num_access_files | Number of operations on access control files. | Continuous | Content |
| 20 | num_outbound_cmds | Number of outbound commands in an ftp session. | Binary | Content |
| 21 | is_host_login | 1 if the connection is a host login; 0 otherwise. | Binary | Content |
| 22 | is_guest_login | 1 if the login is a guest login; 0 otherwise. | Binary | Content |
| 23 | count | Number of connections to the same destination host as the current connection in the last two seconds. | Continuous | Time-based |
| 24 | srv_count | Number of connections to the same service as the current connection in the last two seconds. | Continuous | Time-based |
| 25 | serror_rate | % of connections in count that had SYN errors. | Continuous | Time-based |
| 26 | srv_serror_rate | % of connections in srv_count that had SYN errors. | Continuous | Time-based |
| 27 | rerror_rate | % of connections in count that had REJ errors (connection rejected). | Continuous | Time-based |
| 28 | srv_rerror_rate | % of connections in srv_count that had REJ errors. | Continuous | Time-based |
| 29 | same_srv_rate | % of connections in count that were to the same service. | Continuous | Time-based |
| 30 | diff_srv_rate | % of connections in count that were to different services. | Continuous | Time-based |
| 31 | srv_diff_host_rate | % of connections in srv_count that were to different destination hosts. | Continuous | Time-based |
| 32 | dst_host_count | Number of connections with the same destination host (last 100 connections). | Continuous | Host-based |
| 33 | dst_host_srv_count | Number of connections with same service and same destination host. | Continuous | Host-based |
| 34 | dst_host_same_srv_rate | % of connections in dst_host_count that were to the same service. | Continuous | Host-based |
| 35 | dst_host_diff_srv_rate | % of connections in dst_host_count that were to different services. | Continuous | Host-based |
| 36 | dst_host_same_src_port_rate | % of connections in dst_host_count that were from the same source port. | Continuous | Host-based |
| 37 | dst_host_srv_diff_host_rate | % of connections in dst_host_srv_count that were to different destination hosts. | Continuous | Host-based |
| 38 | dst_host_serror_rate | % of connections in dst_host_count that had SYN errors. | Continuous | Host-based |
| 39 | dst_host_srv_serror_rate | % of connections in dst_host_srv_count that had SYN errors. | Continuous | Host-based |
| 40 | dst_host_rerror_rate | % of connections in dst_host_count that had REJ errors. | Continuous | Host-based |
| 41 | dst_host_srv_rerror_rate | % of connections in dst_host_srv_count that had REJ errors. | Continuous | Host-based |
| 42 | attack_type | The class label: normal or specific attack category/type (e.g., neptune, smurf). | Categorical | Target |
| 43 | difficulty_level | Difficulty score of the test record (1–21). | Discrete | Target |