File size: 1,750 Bytes
fdad801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
import nltk
from nltk.corpus import stopwords
import re

# Download NLTK stopwords
nltk.download('stopwords')

# Load dataset
# Assuming you have a CSV file with 'url' and 'label' columns
data = pd.read_csv('malicious_phish.csv')

# Preprocess URLs
def preprocess_url(url):
    url = re.sub(r"http\S+", "", url)  # Remove http links
    url = re.sub(r"\d+", "", url)      # Remove digits
    url = re.sub(r"\W", " ", url)      # Remove non-word characters
    url = url.lower()                  # Convert to lowercase
    return url

data['url'] = data['url'].apply(preprocess_url)

# Split data into training and testing sets
X = data['url']
y = data['type']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Vectorize URLs using TF-IDF
vectorizer = TfidfVectorizer(stop_words=stopwords.words('english'))
X_train_tfidf = vectorizer.fit_transform(X_train)
X_test_tfidf = vectorizer.transform(X_test)

# Train a Naive Bayes classifier
model = MultinomialNB()
model.fit(X_train_tfidf, y_train)

# Predict and evaluate
y_pred = model.predict(X_test_tfidf)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")

# Function to predict if a URL is malicious
def predict_url(url):
    processed_url = preprocess_url(url)
    vectorized_url = vectorizer.transform([processed_url])
    prediction = model.predict(vectorized_url)
    return prediction[0]

# Example usage
print(predict_url("br-icloud.com.br"))