-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
167 lines (141 loc) · 5.67 KB
/
app.py
File metadata and controls
167 lines (141 loc) · 5.67 KB
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
import subprocess
import streamlit as st
from ML_model import static
import sqlite3
import hashlib
import pefile
import os
import magic as magic_lib
import warnings
import yaml
import platform
warnings.filterwarnings('ignore')
def load_config(file_path):
with open(file_path, "r") as f:
return yaml.safe_load(f)
# Load configuration
config = load_config("E:/Malware Detection using ML/config.yaml")
webapp_config = config['WEBAPP']
sqlite_database = webapp_config['SQLITE_DATABASE']
exclusions = webapp_config['EXCLUSIONS']
# Set up database connection
connection = sqlite3.connect(sqlite_database)
# Set up magic for file type detection
if platform.system() == 'Windows':
magic = magic_lib.Magic(magic_file='E:/Malware Detection using ML/auxiliary/magic/magic.mgc')
else:
magic = magic_lib.Magic()
# Streamlit page configuration
st.set_page_config(layout="wide")
st.subheader("Malware Detection using Machine Learning")
# File uploader widget
file = st.file_uploader("Upload File")
def compute_sha256(filename, block_size=65536):
sha256 = hashlib.sha256()
with open(filename, 'rb') as f:
for block in iter(lambda: f.read(block_size), b''):
sha256.update(block)
return sha256.hexdigest()
def run_ssdeep_hash(file_path):
# Generate ssdeep hash using subprocess
result = subprocess.run([r"C:\msys64\usr\bin\ssdeep.exe", f'"{file_path}"'], capture_output=True, text=True, shell=True)
if result.returncode == 0:
return result.stdout.strip()
else:
st.error("Error running ssdeep.")
return None
def run_ssdeep_compare(hash1, hash2):
# Compare ssdeep hashes
result = subprocess.run([r"C:\msys64\usr\bin\ssdeep.exe", "-c", f'"{hash1}"', f'"{hash2}"'], capture_output=True, text=True, shell=True)
if result.returncode == 0:
try:
similarity = int(result.stdout.strip())
st.write(f"ssdeep comparison similarity: {similarity}%")
return similarity
except ValueError:
st.error("Error parsing ssdeep comparison output.")
return -1
else:
st.error("Error comparing ssdeep hashes.")
return -1
def main():
if file is not None:
# Display uploaded file details
file_details = {"filename": file.name, "filetype": file.type, "filesize": file.size}
st.write(file_details)
# Save uploaded file locally
file_path = os.path.join("temp", file.name)
os.makedirs("temp", exist_ok=True)
with open(file_path, 'wb') as outfile:
outfile.write(file.read())
# Determine file type with magic
magic_file = magic.from_file(file_path)
if magic_file.split()[0] in exclusions:
st.markdown(f"<b>{file.name}</b> is {magic_file}", unsafe_allow_html=True)
os.remove(file_path)
return
fileinfo = magic_file.split(',')[0]
cursor = connection.cursor()
sha256 = compute_sha256(file_path)
ssdeep_hash = run_ssdeep_hash(file_path)
message = f"Filename: <b>{file.name}</b><br>File info: <b>{fileinfo}</b><br>SHA256: <b>{sha256}</b><br>SSDEEP: <b>{ssdeep_hash}</b>"
# Attempt to load PE file and extract imphash
try:
pe = pefile.PE(file_path)
imphash = pe.get_imphash() or sha256 # Default to SHA256 if imphash is unavailable
message += f"<br>imphash: <b>{imphash}</b>"
st.write("Querying the database for existing signatures...")
cursor.execute(
f'SELECT ssdeep FROM signatures WHERE imphash = ? OR sha256 = ?',
(imphash, sha256)
)
db_entry = cursor.fetchone()
if db_entry:
ssdeep_hash_db = db_entry[0]
similarity = run_ssdeep_compare(ssdeep_hash, ssdeep_hash_db)
if similarity > 50:
st.write("Similar ssdeep hash found in database; retrieving classification.")
cursor.execute(
f'SELECT class, confidence FROM signatures WHERE imphash = ? OR sha256 = ?',
(imphash, sha256)
)
result = cursor.fetchone()
else:
result = None
else:
result = None
except Exception as e:
st.write(f"Error processing PE file: {e}")
result = None
# Display file details
with st.expander("File details"):
st.markdown(message, unsafe_allow_html=True)
# If no result, run static analysis model
if not result:
st.write("No matching entry in database; running ML model analysis.")
result = static.process(file)
if result:
status, confidence = result
st.write(f"Inserting new analysis result: {status}, {confidence}% confidence")
cursor.execute(
'INSERT INTO signatures (sha256, imphash, ssdeep, class, confidence) VALUES (?, ?, ?, ?, ?)',
(sha256, imphash, ssdeep_hash, status, confidence)
)
connection.commit()
# Display result
if result:
status, confidence = result
color = "green" if status == "Benign" else "red"
st.markdown(
f"Source: <b>Database</b><br>Status: <font color='{color}'>{status}</font><br>Confidence: <b>{confidence}%</b>",
unsafe_allow_html=True
)
else:
st.error("Unable to classify the file.")
# Clean up
cursor.close()
if pe:
pe.close()
os.remove(file_path)
if __name__ == '__main__':
main()