-
Notifications
You must be signed in to change notification settings - Fork 109
/
extanalysis.py
192 lines (151 loc) · 6.45 KB
/
extanalysis.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ExtAnalysis - Browser Extension Analysis Framework
Copyright (C) 2019 - 2022 Tuhinshubhra
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import logging
import traceback
import argparse
import webbrowser
from flask import Flask, request, render_template, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
import core.core as core
import core.analyze as analysis
import core.helper as helper
import core.settings as settings
from flask_wtf.csrf import CSRFProtect
parser = argparse.ArgumentParser(prog='extanalysis.py', add_help=False)
parser.add_argument('-h', '--host', help='Host to run ExtAnalysis on. Default host is 127.0.0.1')
parser.add_argument('-p', '--port', help='Port to run ExtAnalysis on. Default port is 13337')
parser.add_argument('-v', '--version', action='store_true', help='Shows version and quits')
parser.add_argument('-u', '--update', action='store_true', help='Checks for update')
parser.add_argument('-q', '--quiet', action='store_true', help='Quiet mode shows only errors on cli!')
parser.add_argument('-n', '--nobrowser', action='store_true', help='Skips launching a web browser')
parser.add_argument('--help', action='store_true', help='Shows this help menu and exits')
args = parser.parse_args()
allowed_extension = set(['crx', 'zip', 'xpi', 'tar', 'gzip'])
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
# Set host and port
if args.host is not None:
host = args.host
else:
host = '127.0.0.1'
if args.port is not None:
port = int(args.port)
else:
port = 13337
# enable Quiet mode
if args.quiet:
core.quiet = True
# help
if args.help:
parser.print_help()
parser.exit()
# version
if args.version:
# core.print_logo()
print('ExtAnalysis Version: ' + core.version)
exit()
if args.update:
import core.updater as updater
updater.check()
# core.updatelog('Initiating settings...')
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in allowed_extension
csrf = CSRFProtect()
app = Flask('ExtAnalysis - Browser Extension Analysis Toolkit')
app.config['UPLOAD_FOLDER'] = core.lab_path
app.secret_key = str(os.urandom(24))
csrf.init_app(app)
@app.errorhandler(404)
def page_not_found(e):
error_txt = 'The page you are trying to browse does not exist... Please click on the logo to go back to homepage.'
return render_template('error.html', error_title="Error 404 - Page Not Found!",
error_head="The page you are looking for is kinda imaginary!", error_txt=error_txt), 404
@app.errorhandler(500)
def internal_error(e):
error_txt = 'Welp! There\'s no good way of telling this but something has gone terribly wrong with the program!'
return render_template('error.html', error_title="Error 500 - Internal Server Error!",
error_head="Something seriously went wrong... ", error_txt=error_txt), 500
@app.route("/")
def home():
core.updatelog('Accessed Main page')
lic = open(helper.fixpath(core.path + '/LICENSE'), 'r')
license_text = lic.read()
cred = open(helper.fixpath(core.path + '/CREDITS'), 'r')
credits_text = cred.read()
sett = open(core.settings_file, 'r')
settings_json = sett.read()
return render_template("index.html",
report_dir=core.reports_path,
lab_dir=core.lab_path,
license_text=license_text,
credits_text=credits_text,
virustotal_api=core.virustotal_api,
settings_json=settings_json
)
@app.route('/upload/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return ('error: No File uploaded')
file = request.files['file']
if file.filename == '':
return ('error: Empty File!')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
core.updatelog('File Uploaded.. Filename: ' + filename)
# saveas = filename.split('.')[0]
anls = analysis.analyze(filename)
return (anls)
else:
return (
'error: Invalid file format! only .crx files allowed. If you\'re trying to upload zip file rename it to crx instead')
@app.route("/api/", methods=["POST"])
def api():
if request.method == 'POST':
# query = request.args.get('query')
query = request.form['query']
import frontend.api as processapi
return (processapi.view(query, request.args))
@app.route("/log/")
def updatelogs():
return (core.log)
@app.route('/view-graph/<analysis_id>')
def large_graph(analysis_id):
import frontend.viewgraph as viewgraph
return (viewgraph.view(analysis_id))
@app.route('/view-source/<analysis_id>/<file_id>')
def view_source(analysis_id, file_id):
import frontend.viewfile as viewfile
return (viewfile.view(analysis_id, file_id))
@app.route('/source-code/<url>')
def source_code(url):
import frontend.viewsource as vs
return (vs.view(url))
@app.route('/analysis/<analysis_id>')
def show_analysis(analysis_id):
import frontend.viewresult as viewResult
return (viewResult.view(analysis_id))
if __name__ == "__main__":
core.print_logo()
settings.init_settings()
main_url = 'http://{0}:{1}'.format(host, port)
if args.nobrowser is not True:
webbrowser.open(main_url)
print('\n[~] Starting ExtAnalysis at: {0} \n\n'.format(main_url))
app.run(host=host, port=port, debug=False)