-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
248 lines (200 loc) · 9.94 KB
/
app.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from flask import Flask, render_template, request, flash, redirect, url_for, jsonify, session
import os
from werkzeug.utils import secure_filename
import requests
import time
import json
import re
from datetime import datetime
import subprocess
from threading import Thread
project_root = os.path.dirname(os.path.abspath(__file__))
print(project_root)
METRICS_FOLDER = os.path.join(project_root, 'metrics')
CFM_FILE_FOLDER = os.path.join(project_root, 'static', 'cfm.png')
MODEL_FOLDER = os.path.join(project_root, 'utilities/model')
app = Flask(__name__)
app.config['UPLOAD_DATASET'] = os.path.join(project_root, 'dataset')
app.config['UPLOAD_APK'] = os.path.join(project_root, 'apk')
app.secret_key = 'supersecretkey'
# Jenkins configuration
JENKINS_URL = "http://localhost:8080"
JOB_NAME_1 = "Andmal-Detection-ML-Pipeline"
JOB_NAME_2 = "Andmal-APK-Pipeline"
JENKINS_TOKEN = "123456"
JENKINS_USER = "admin"
JENKINS_PASSWORD = "Automation@123"
ALLOWED_EXTENSIONS = {'csv', 'apk'}
last_build_number = None
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
import time
import requests
def trigger_jenkins_pipeline(job_name):
global last_build_number
# Step 1: Trigger Jenkins job and get the queue item URL
trigger_response = requests.get(
f"{JENKINS_URL}/job/{job_name}/build",
params={'token': JENKINS_TOKEN},
auth=(JENKINS_USER, JENKINS_PASSWORD)
)
if trigger_response.status_code != 201:
print(f"Failed to trigger Jenkins pipeline. Status Code: {trigger_response.status_code}")
return False
# Step 2: Extract queue item ID
queue_url = trigger_response.headers.get("Location") + "api/json"
queue_response = requests.get(queue_url, auth=(JENKINS_USER, JENKINS_PASSWORD))
if queue_response.status_code != 200:
print(f"Failed to retrieve queue info. Status Code: {queue_response.status_code}")
return False
queue_info = queue_response.json()
queue_id = queue_info.get("id")
# Step 3: Poll the queue to get the actual build number
build_number = None
for _ in range(10): # Adjust attempts and sleep duration as needed
time.sleep(5)
queue_item_response = requests.get(f"{JENKINS_URL}/queue/item/{queue_id}/api/json", auth=(JENKINS_USER, JENKINS_PASSWORD))
queue_item_info = queue_item_response.json()
if 'executable' in queue_item_info:
build_number = queue_item_info['executable']['number']
last_build_number = build_number
break
if not build_number:
print("Failed to retrieve the build number from the queue.")
return False
print(f"Build triggered with build number: {build_number}")
return True
@app.route("/", methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
dataset = request.files.get('dataset')
apk_file = request.files.get('apk_file')
if dataset and allowed_file(dataset.filename):
dataset_path = os.path.join(app.config['UPLOAD_DATASET'], secure_filename(dataset.filename))
dataset.save(dataset_path)
if trigger_jenkins_pipeline(JOB_NAME_1):
flash("Jenkins pipeline triggered successfully. Waiting for results...")
return render_template("result.html", report=None)
else:
flash("Failed to trigger Jenkins pipeline. Please check your configuration.")
return redirect(url_for('upload_file'))
elif apk_file and apk_file.filename.endswith('.apk'):
apk_path = os.path.join(app.config['UPLOAD_APK'], apk_file.filename)
apk_file.save(apk_path)
session['apk_filename'] = apk_file.filename
if trigger_jenkins_pipeline(JOB_NAME_2):
flash("Jenkins pipeline triggered successfully. Waiting for results...")
return render_template("apk_result.html", report=None)
else:
flash("Failed to trigger Jenkins pipeline. Please check your configuration.")
return redirect(url_for('upload_file'))
else:
flash("Please upload a valid .csv or .apk file.")
return render_template("upload.html")
@app.route('/check_apk_status', methods=['GET'])
def check_apk_status():
global last_build_number
if not last_build_number:
return jsonify({"status": "error", "message": "No build number found"})
# Poll Jenkins to check if the build is complete
build_info_url = f"{JENKINS_URL}/job/{JOB_NAME_2}/{last_build_number}/api/json"
build_info_response = requests.get(build_info_url, auth=(JENKINS_USER, JENKINS_PASSWORD))
if build_info_response.status_code != 200:
return jsonify({"status": "error", "message": "Failed to retrieve build info"})
build_info = build_info_response.json()
build_result = build_info.get("result")
print(build_result)
# Check if the build is complete
if build_result in ["SUCCESS", "FAILURE"]:
# Fetch console output for detailed results
console_output_url = f"{JENKINS_URL}/job/{JOB_NAME_2}/{last_build_number}/consoleText"
console_output_response = requests.get(console_output_url, auth=(JENKINS_USER, JENKINS_PASSWORD))
apk_filename = session.get('apk_filename')
if not apk_filename:
return jsonify({'status': 'error', 'message': 'No APK analysis in progress'})
feature_filename = apk_filename.split('.')[0]
analysis_dir = os.path.join(project_root, 'new_samples', feature_filename, 'Features_files')
analysis_output_path = os.path.join(analysis_dir, f"{feature_filename}-analysis.json")
result_path = os.path.join(project_root, 'new_samples', feature_filename, 'prediction_result.json')
print(analysis_output_path)
print(result_path)
# if os.path.exists(analysis_output_path) and os.path.exists(result_path):
with open(analysis_output_path, 'r') as f:
data = json.load(f)
with open(result_path, 'r') as f:
result = json.load(f)
# Extract relevant fields
pre_static_analysis = data.get('Pre_static_analysis', {})
static_analysis = data.get('Static_analysis', {})
prediction_result = result.get('malware')
if prediction_result == '0':
prediction_result = 'benign'
else:
prediction_result = 'malware'
# Return detailed JSON data for rendering
return jsonify({
'status': 'finished',
'result': prediction_result,
'Pre_static_analysis': {
'Filename': pre_static_analysis.get('Filename', 'N/A'),
'md5': pre_static_analysis.get('md5', 'N/A'),
'sha1': pre_static_analysis.get('sha1', 'N/A'),
'sha256': pre_static_analysis.get('sha256', 'N/A')
},
'Static_analysis': {
'Package name': static_analysis.get('Package name', 'N/A'),
'Permissions': static_analysis.get('Permissions', []),
'Opcodes': static_analysis.get('Opcodes', {}),
'Activities': static_analysis.get('Activities', {}),
'System commands': static_analysis.get('System commands', {}),
'FlowDroid': static_analysis.get('FlowDroid', {}),
'Intents': static_analysis.get('Intents', {})
}
})
return jsonify({'status': 'running'}) # Handle cases where build is neither SUCCESS nor FAILURE
@app.route("/check_status", methods=['GET'])
def check_status():
global last_build_number
if not last_build_number:
return jsonify({"status": "error", "message": "No build number found"})
# Poll Jenkins to check if the build is complete
build_info_url = f"{JENKINS_URL}/job/{JOB_NAME_1}/{last_build_number}/api/json"
build_info_response = requests.get(build_info_url, auth=(JENKINS_USER, JENKINS_PASSWORD))
if build_info_response.status_code != 200:
return jsonify({"status": "error", "message": "Failed to retrieve build info"})
build_info = build_info_response.json()
build_result = build_info.get("result")
# Check if the build is complete
if build_result in ["SUCCESS", "FAILURE"]:
# Fetch console output for detailed results
console_output_url = f"{JENKINS_URL}/job/{JOB_NAME_1}/{last_build_number}/consoleText"
console_output_response = requests.get(console_output_url, auth=(JENKINS_USER, JENKINS_PASSWORD))
# Load metrics from the JSON file if the build is finished
metrics_path = os.path.join(METRICS_FOLDER, "metrics.json")
metrics = {}
if os.path.exists(metrics_path):
with open(metrics_path, "r") as f:
metrics = json.load(f)
plots_path = os.path.join(METRICS_FOLDER)
confusion_matrix_image = os.path.join(METRICS_FOLDER)
# Return build and metrics information
return jsonify({
"status": "finished",
"result": build_result,
"output": console_output_response.text,
"metrics": metrics,
"visualization_image": url_for('static', filename='visualization.png') if os.path.exists(plots_path) else None,
"confusion_matrix_image": url_for('static', filename='cfm.png') if os.path.exists(confusion_matrix_image) else None,
"classification_image": url_for('static', filename='report.png') if os.path.exists(confusion_matrix_image) else None
})
else:
# Check if visualization exists
# plots_path = os.path.join(METRICS_FOLDER, "visualization.png")
plots_path = os.path.join(METRICS_FOLDER)
visualization_available = os.path.exists(plots_path)
return jsonify({
"status": "running",
"visualization_image": url_for('static', filename='visualization.png') if visualization_available else None
})
if __name__ == "__main__":
app.run(host='0.0.0.0',debug=True,use_reloader=False)