# Exercise 05¶

## Logistic regression exercise to detect network intrusions¶

Software to detect network intrusions protects a computer network from unauthorized users, including perhaps insiders. The intrusion detector learning task is to build a predictive model (i.e. a classifier) capable of distinguishing between bad connections, called intrusions or attacks, and good normal connections. The 1998 DARPA Intrusion Detection Evaluation Program was prepared and managed by MIT Lincoln Labs. The objective was to survey and evaluate research in intrusion detection. A standard set of data to be audited, which includes a wide variety of intrusions simulated in a military network environment, was provided. The 1999 KDD intrusion detection contest uses a version of this dataset.

Lincoln Labs set up an environment to acquire nine weeks of raw TCP dump data for a local-area network (LAN) simulating a typical U.S. Air Force LAN. They operated the LAN as if it were a true Air Force environment, but peppered it with multiple attacks.

The raw training data was about four gigabytes of compressed binary TCP dump data from seven weeks of network traffic. This was processed into about five million connection records. Similarly, the two weeks of test data yielded around two million connection records. description

A connection is a sequence of TCP packets starting and ending at some well defined times, between which data flows to and from a source IP address to a target IP address under some well defined protocol. Each connection is labeled as either normal, or as an attack, with exactly one specific attack type. Each connection record consists of about 100 bytes.

Attacks fall into four main categories:

• DOS: denial-of-service, e.g. syn flood;
• R2L: unauthorized access from a remote machine, e.g. guessing password;
• U2R: unauthorized access to local superuser (root) privileges, e.g., various buffer overflow attacks;
• probing: surveillance and other probing, e.g., port scanning. It is important to note that the test data is not from the same probability distribution as the training data, and it includes specific attack types not in the training data. This makes the task more realistic. Some intrusion experts believe that most novel attacks are variants of known attacks and the "signature" of known attacks can be sufficient to catch novel variants. The datasets contain a total of 24 training attack types, with an additional 14 types in the test data only.

In [4]:
import pandas as pd
pd.set_option('display.max_columns', 500)
import zipfile
with zipfile.ZipFile('../datasets/UNB_ISCX_NSL_KDD.csv.zip', 'r') as z:
f = z.open('UNB_ISCX_NSL_KDD.csv')

Out[4]:
duration protocol_type service flag src_bytes dst_bytes land wrong_fragment urgent hot num_failed_logins logged_in num_compromised root_shell su_attempted num_root num_file_creations num_shells num_access_files num_outbound_cmds is_host_login is_guest_login count srv_count serror_rate srv_serror_rate rerror_rate srv_rerror_rate same_srv_rate diff_srv_rate srv_diff_host_rate dst_host_count dst_host_srv_count dst_host_same_srv_rate dst_host_diff_srv_rate dst_host_same_src_port_rate dst_host_srv_diff_host_rate dst_host_serror_rate dst_host_srv_serror_rate dst_host_rerror_rate dst_host_srv_rerror_rate class
0 0 tcp ftp_data SF 491 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 0.0 0.0 0.0 0.0 1.00 0.00 0.00 150 25 0.17 0.03 0.17 0.00 0.00 0.00 0.05 0.00 normal
1 0 udp other SF 146 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 0.0 0.0 0.0 0.0 0.08 0.15 0.00 255 1 0.00 0.60 0.88 0.00 0.00 0.00 0.00 0.00 normal
2 0 tcp private S0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 123 6 1.0 1.0 0.0 0.0 0.05 0.07 0.00 255 26 0.10 0.05 0.00 0.00 1.00 1.00 0.00 0.00 anomaly
3 0 tcp http SF 232 8153 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 5 5 0.2 0.2 0.0 0.0 1.00 0.00 0.00 30 255 1.00 0.00 0.03 0.04 0.03 0.01 0.00 0.01 normal
4 0 tcp http SF 199 420 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 30 32 0.0 0.0 0.0 0.0 1.00 0.00 0.09 255 255 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 normal

Create X and y

Use only same_srv_rate and dst_host_srv_count

In [2]:
y = (data['class'] == 'anomaly').astype(int)

In [3]:
y.value_counts()

Out[3]:
0    77054
1    71463
Name: class, dtype: int64
In [13]:
X = data[['same_srv_rate','dst_host_srv_count']]


# Exercise 5.1¶

Split the data into training and testing sets

In [ ]:



# Exercise 5.2¶

Fit a logistic regression model and examine the coefficients

In [ ]:



# Exercise 5.3¶

Make predictions on the testing set and calculate the accuracy

In [ ]:



# Exercise 5.4¶

Confusion matrix of predictions

What is the percentage of detected anomalies

In [ ]:



# Exercise 5.5¶

Increase sensitivity by lowering the threshold for predicting anomaly connection

Create a new classifier by changing the probability threshold to 0.3

What is the new confusion matrix?

What is the new percentage of detected anomalies?

In [24]: