Skip to content

Commit ae8222b

Browse files
authored
Add files via upload
1 parent d90e29d commit ae8222b

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

app.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from flask import Flask, request, render_template
2+
import joblib
3+
import os
4+
from feature_extraction import extract_features
5+
6+
app = Flask(__name__)
7+
8+
# Load the trained model
9+
model = joblib.load('ML_model/malwareclassifier-V2.pkl')
10+
11+
UPLOAD_FOLDER = 'uploads'
12+
if not os.path.exists(UPLOAD_FOLDER):
13+
os.makedirs(UPLOAD_FOLDER)
14+
15+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
16+
ALLOWED_EXTENSIONS = {'dll', 'exe'}
17+
18+
def allowed_file(filename):
19+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
20+
21+
@app.route('/')
22+
def index():
23+
return render_template('index.html')
24+
25+
@app.route('/analyze', methods=['POST'])
26+
def analyze():
27+
# Check if a file is uploaded
28+
if 'file' in request.files:
29+
file = request.files['file']
30+
31+
if file.filename == '' or not allowed_file(file.filename):
32+
return render_template('index.html', error="Unsupported file type.")
33+
34+
# Construct the full file path
35+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
36+
37+
# Save the file
38+
file.save(file_path)
39+
40+
# Use the model for prediction if the file is `.exe` or `.dll`
41+
if allowed_file(file.filename):
42+
features = extract_features(file_path) # Your feature extraction function
43+
prediction = model.predict(features) # Predict using your model
44+
result = {
45+
"type": "file",
46+
"prediction": "Malware" if prediction[0] == 1 else "Safe",
47+
"file_name": file.filename
48+
}
49+
50+
return render_template('result.html', result=result)
51+
52+
return render_template('index.html', error="No file uploaded.")
53+
54+
if __name__ == '__main__':
55+
app.run(port=5001, debug=True)
56+

feature_extraction.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pefile
2+
import pandas as pd
3+
import math
4+
5+
# Function to calculate entropy of a section
6+
def calculate_entropy(data):
7+
if not data:
8+
return 0
9+
entropy = 0
10+
for x in range(256):
11+
p_x = float(data.count(bytes([x]))) / len(data)
12+
if p_x > 0:
13+
entropy += - p_x * math.log(p_x, 2)
14+
return entropy
15+
16+
def extract_features(file_path):
17+
pe = pefile.PE(file_path)
18+
19+
# Extract the specified 23 features in the given order
20+
features = {
21+
'MajorLinkerVersion': pe.OPTIONAL_HEADER.MajorLinkerVersion,
22+
'MinorOperatingSystemVersion': pe.OPTIONAL_HEADER.MinorOperatingSystemVersion,
23+
'MajorSubsystemVersion': pe.OPTIONAL_HEADER.MajorSubsystemVersion,
24+
'SizeOfStackReserve': pe.OPTIONAL_HEADER.SizeOfStackReserve,
25+
'TimeDateStamp': pe.FILE_HEADER.TimeDateStamp,
26+
'MajorOperatingSystemVersion': pe.OPTIONAL_HEADER.MajorOperatingSystemVersion,
27+
'Characteristics': pe.FILE_HEADER.Characteristics,
28+
'ImageBase': pe.OPTIONAL_HEADER.ImageBase,
29+
'Subsystem': pe.OPTIONAL_HEADER.Subsystem,
30+
'MinorImageVersion': pe.OPTIONAL_HEADER.MinorImageVersion,
31+
'MinorSubsystemVersion': pe.OPTIONAL_HEADER.MinorSubsystemVersion,
32+
'SizeOfInitializedData': pe.OPTIONAL_HEADER.SizeOfInitializedData,
33+
'DllCharacteristics': pe.OPTIONAL_HEADER.DllCharacteristics,
34+
'DirectoryEntryExport': 1 if hasattr(pe, 'DIRECTORY_ENTRY_EXPORT') else 0,
35+
'ImageDirectoryEntryExport': pe.OPTIONAL_HEADER.DATA_DIRECTORY[0].Size if hasattr(pe, 'DIRECTORY_ENTRY_EXPORT') else 0,
36+
'CheckSum': pe.OPTIONAL_HEADER.CheckSum,
37+
'DirectoryEntryImportSize': pe.OPTIONAL_HEADER.DATA_DIRECTORY[1].Size if hasattr(pe, 'DIRECTORY_ENTRY_IMPORT') else 0,
38+
'SectionMaxChar': len(pe.sections), # Example calculation for demonstration
39+
'MajorImageVersion': pe.OPTIONAL_HEADER.MajorImageVersion,
40+
'AddressOfEntryPoint': pe.OPTIONAL_HEADER.AddressOfEntryPoint,
41+
'SectionMinEntropy': None, # Placeholder, will be calculated
42+
'SizeOfHeaders': pe.OPTIONAL_HEADER.SizeOfHeaders,
43+
'SectionMinVirtualsize': None # Placeholder, will be calculated
44+
}
45+
46+
# Calculate SectionMinEntropy
47+
entropies = []
48+
for section in pe.sections:
49+
entropy = calculate_entropy(section.get_data())
50+
entropies.append(entropy)
51+
52+
if entropies:
53+
features['SectionMinEntropy'] = min(entropies)
54+
55+
# Calculate SectionMinVirtualsize (example calculation)
56+
features['SectionMinVirtualsize'] = min(section.Misc_VirtualSize for section in pe.sections)
57+
58+
return pd.DataFrame([features])

0 commit comments

Comments
 (0)