-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
220 lines (178 loc) · 7.14 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
from flask import Flask, render_template, request, send_file, redirect, jsonify
from flask_cors import CORS, cross_origin
from forms import Input
from utils import Results, extract_url, getDomainInfo
from actions import submit
from openpyxl import Workbook
from urllib2 import urlopen
from flask_sqlalchemy import SQLAlchemy
import os
SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URI', None)
SECRET_KEY = os.environ.get('SECRET_KEY', None)
SQLALCHEMY_TRACK_MODIFICATIONS = os.environ.get('SQLALCHEMY_TRACK_MODIFICATIONS', None)
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
app.config['SECRET_KEY'] = SECRET_KEY
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = SQLALCHEMY_TRACK_MODIFICATIONS
port = int(os.environ.get("PORT", 5000))
db = SQLAlchemy(app)
class Store(db.Model):
id = db.Column(db.Integer, primary_key=True)
url = db.Column(db.String(100), unique=True)
verdict = db.Column(db.String(20))
def __init__(self, url, verdict):
self.url = url
self.verdict = verdict
@app.route('/test', methods=['POST'])
@cross_origin()
def test():
url = request.json['url']
base_url = extract_url(url)
domain_info = getDomainInfo(base_url)['WhoisRecord']
useful_domain_info = {url: url}
empty = False
try:
del domain_info['registrant']['rawText']
domain_info['registrant']['street'] = domain_info['registrant'].pop('street1')
useful_domain_info = {
'Registrar Name': domain_info['registrarName'],
'Registrant Details': domain_info['registrant'],
'Creation Date': domain_info['createdDate'],
'Updation Date': domain_info['updatedDate'],
'Expiration Date': domain_info['expiresDate'],
'Domain Name': base_url
}
except KeyError as e:
if e.message == 'registrant':
print 'REGISTRANT-------------------------------------------'
useful_domain_info = {}
empty = True
elif e.message == 'street1':
print 'STREET-------------------------------------------'
useful_domain_info = {
'Registrar Name': domain_info['registrarName'],
'Registrant Details': domain_info['registrant'],
'Creation Date': domain_info['createdDate'],
'Updation Date': domain_info['updatedDate'],
'Expiration Date': domain_info['expiresDate'],
'Domain Name': base_url,
}
empty = False
else:
print('---------------ELSE------')
print e.message
except Exception as e:
print('-----------EXCEPTION-------------')
print(e.message)
result = submit(url)
verdict = ''
if result == Results.SAFE:
verdict = 'SAFE'
elif result == Results.MALICIOUS:
verdict = 'MALICIOUS'
else:
verdict = 'MALWARE'
isSafe = False
if db.session.query(Store).filter(Store.url == base_url).count() == 0:
info = Store(base_url, verdict)
db.session.add(info)
db.session.commit()
if result == Results.SAFE:
isSafe = True
return jsonify({'domain': useful_domain_info,'isSafe': isSafe, 'url': url})
@app.route('/', methods=['GET', 'POST'])
@app.route('/home', methods=['GET', 'POST'])
def index():
form = Input(request.form)
if request.method == 'POST' and form.validate():
url = form.url.data;
base_url = extract_url(url)
domain_info = getDomainInfo(base_url)['WhoisRecord']
useful_domain_info = {}
empty = False
try:
del domain_info['registrant']['rawText']
domain_info['registrant']['street'] = domain_info['registrant'].pop('street1')
useful_domain_info = {
'Registrar Name': domain_info['registrarName'],
'Registrant Details': domain_info['registrant'],
'Creation Date': domain_info['createdDate'],
'Updation Date': domain_info['updatedDate'],
'Expiration Date': domain_info['expiresDate'],
'Domain Name': base_url
}
except KeyError as e:
if e.message == 'registrant':
print 'REGISTRANT-------------------------------------------'
useful_domain_info = {}
empty = True
elif e.message == 'street1':
print 'STREET-------------------------------------------'
useful_domain_info = {
'Registrar Name': domain_info['registrarName'],
'Registrant Details': domain_info['registrant'],
'Creation Date': domain_info['createdDate'],
'Updation Date': domain_info['updatedDate'],
'Expiration Date': domain_info['expiresDate'],
'Domain Name': base_url,
}
empty = False
else:
print('---------------ELSE------')
print e.message
except Exception as e:
print('-----------EXCEPTION-------------')
print(e.message)
result = submit(url)
verdict = ''
if result == Results.SAFE:
verdict = 'SAFE'
elif result == Results.MALICIOUS:
verdict = 'MALICIOUS'
else:
verdict = 'MALWARE'
if db.session.query(Store).filter(Store.url == base_url).count() == 0:
info = Store(base_url, verdict)
db.session.add(info)
db.session.commit()
if result == Results.SAFE:
print('----------- SAFE -----------')
return render_template('safe.html', url=url, base_url=extract_url(url), info=useful_domain_info, isempty=empty)
elif result == Results.MALICIOUS or result == Results.MALWARE:
print('----------- MALICIOUS -----------')
return render_template('malicious.html', url=url, base_url=extract_url(url), info = useful_domain_info, empty=empty)
return render_template('index.html',form=form)
@app.route('/about')
def about():
return render_template('about.html', title='About')
@app.route('/download', methods=['POST'])
@cross_origin()
def download():
wb = Workbook()
ws = wb.active
ws.title = 'Malicious links'
ws['A1'] = 'Url'
ws['A1'].style = 'Headline 1'
ws['B1'] = 'Verdict'
ws['B1'].style = 'Headline 1'
results = Store.query.all()
total_results = len(results) + 1
index = 0
for row in ws.iter_rows(min_row = 2, max_col=2, max_row=total_results):
link = results[index]
x = 0
for cell in row:
if x%2 == 0:
cell.value = str(link.url)
else:
cell.value = str(link.verdict)
x += 1
index += 1
wb.save('static/test.xlsx')
print('-------------DATA WRITTEN-----------')
return send_file('static/test.xlsx')
if __name__ == '__main__':
import main
app.run(host='0.0.0.0', port=port, debug=True)