File size: 7,360 Bytes
a61db6a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import os
os.environ["CUDA_VISIBLE_DEVICES"]="3"
import torch
from datasets import Dataset
from unsloth import FastLanguageModel
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from transformers import TrainingArguments
from trl import SFTTrainer
import logging
from typing import List, Dict
import json
def setup_logging():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
return logging.getLogger(__name__)
def sample_balanced_dataset(df, max_samples_per_class=1000):
"""Sample a balanced subset of the data"""
sampled_dfs = []
# Handle 'benign' class separately
benign_df = df[df['Attack'].str.lower() == 'benign']
attack_df = df[df['Attack'].str.lower() != 'benign']
# Sample benign data
if len(benign_df) > max_samples_per_class:
benign_sampled = benign_df.sample(n=max_samples_per_class, random_state=42)
sampled_dfs.append(benign_sampled)
else:
sampled_dfs.append(benign_df)
# Sample each attack type
for attack_type in attack_df['Attack'].unique():
attack_type_df = attack_df[attack_df['Attack'] == attack_type]
if len(attack_type_df) > max_samples_per_class:
sampled = attack_type_df.sample(n=max_samples_per_class, random_state=42)
sampled_dfs.append(sampled)
else:
sampled_dfs.append(attack_type_df)
return pd.concat(sampled_dfs, ignore_index=True)
class NetworkFlowDataProcessor:
def __init__(self):
self.logger = setup_logging()
self.scaler = MinMaxScaler()
self.numerical_features = [
'L4_SRC_PORT', 'L4_DST_PORT', 'PROTOCOL', 'L7_PROTO',
'IN_BYTES', 'OUT_BYTES', 'IN_PKTS', 'OUT_PKTS',
'TCP_FLAGS', 'FLOW_DURATION_MILLISECONDS'
]
self.categorical_features = ['IPV4_SRC_ADDR', 'IPV4_DST_ADDR']
def process_ip_address(self, ip: str) -> str:
"""Convert IP address to a more descriptive format"""
parts = ip.split('.')
if parts[0] == '192' and parts[1] == '168':
return f"internal_network_{parts[2]}_{parts[3]}"
return f"external_network_{ip}"
def format_flow_data(self, row: pd.Series) -> str:
"""Format network flow data into a descriptive text"""
return f"""Network Flow Description:
Source: {self.process_ip_address(row['IPV4_SRC_ADDR'])} (Port: {row['L4_SRC_PORT']})
Destination: {self.process_ip_address(row['IPV4_DST_ADDR'])} (Port: {row['L4_DST_PORT']})
Protocol Information:
- Protocol ID: {row['PROTOCOL']}
- Layer 7 Protocol: {row['L7_PROTO']}
- TCP Flags: {row['TCP_FLAGS']}
Traffic Metrics:
- Bytes: {row['IN_BYTES']} inbound, {row['OUT_BYTES']} outbound
- Packets: {row['IN_PKTS']} inbound, {row['OUT_PKTS']} outbound
- Duration: {row['FLOW_DURATION_MILLISECONDS']} milliseconds"""
def get_attack_description(self, attack_type: str) -> str:
"""Get detailed description of attack type"""
descriptions = {
"benign": "This is normal network traffic with no malicious intent.",
"ddos": "A Distributed Denial of Service attack attempting to overwhelm network resources.",
"dos": "A Denial of Service attack targeting system availability.",
"injection": "An attack attempting to inject malicious code or commands.",
"scanning": "Network scanning activity to discover vulnerabilities.",
"backdoor": "Malicious activity indicating backdoor access attempts.",
"mitm": "Man-in-the-Middle attack intercepting network communications.",
"password": "Password-based attack attempting unauthorized access.",
"ransomware": "Ransomware-related network activity.",
"xss": "Cross-Site Scripting attack targeting web applications."
}
return descriptions.get(attack_type.lower(), "Unknown attack type")
def prepare_training_text(self, row: pd.Series) -> str:
"""Prepare single training example in LLaMA-3 chat format"""
flow_text = self.format_flow_data(row)
attack_type = row['Attack'].lower() if 'Attack' in row else 'benign'
attack_desc = self.get_attack_description(attack_type)
return f"""<|begin_of_text|><|start_header_id|>user<|end_header_id|>
Analyze this network flow for potential security threats:
{flow_text}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
This network flow is classified as {attack_type}. {attack_desc}
Key indicators from the flow data:
- Traffic volume: {row['IN_BYTES'] + row['OUT_BYTES']} total bytes
- Flow duration: {row['FLOW_DURATION_MILLISECONDS']} ms
- Protocol behavior: {row['TCP_FLAGS']} TCP flags<|eot_id|>"""
def load_and_process_data(train_path: str, processor: NetworkFlowDataProcessor, max_samples_per_class=50000):
"""Load and process the training data"""
logger = setup_logging()
logger.info(f"Loading data from {train_path}")
df = pd.read_csv(train_path)
df = sample_balanced_dataset(df, max_samples_per_class)
logger.info(f"Sampled dataset size: {len(df)}")
# Create dataset with text field
texts = [processor.prepare_training_text(row) for _, row in df.iterrows()]
dataset = Dataset.from_pandas(pd.DataFrame({'text': texts}))
return dataset
def main():
logger = setup_logging()
# Initialize data processor
processor = NetworkFlowDataProcessor()
# Load and process data
train_dataset = load_and_process_data("data/train.csv", processor, max_samples_per_class=50000)
# Model initialization
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="unsloth/llama-3-8b-Instruct-bnb-4bit",
max_seq_length=2048,
load_in_4bit=True,
)
# Configure tokenizer for LLaMA-3
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
# Add LoRA adapters
model = FastLanguageModel.get_peft_model(
model,
r=16,
target_modules=[
"q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj",
],
lora_alpha=16,
lora_dropout=0,
bias="none",
use_gradient_checkpointing="unsloth",
random_state=3407,
)
# Configure training arguments
training_args = TrainingArguments(
output_dir="cybersec_model_output",
num_train_epochs=3,
per_device_train_batch_size=64,
gradient_accumulation_steps=4,
learning_rate=2e-4,
bf16=True, # Use bfloat16 for A100
logging_steps=10,
save_strategy="epoch",
optim="adamw_8bit",
lr_scheduler_type="cosine",
)
# Initialize trainer
trainer = SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=train_dataset,
dataset_text_field="text", # Changed from conversations to text
max_seq_length=2048,
args=training_args,
)
# Train the model
logger.info("Starting training...")
trainer.train()
# Save the model
logger.info("Saving model...")
model.save_pretrained("cybersec_model")
tokenizer.save_pretrained("cybersec_model")
if __name__ == "__main__":
main() |