-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
271 lines (194 loc) · 7.27 KB
/
main.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from ast import LShift
import shlex
import subprocess
from flask import make_response, render_template_string, Flask, url_for, request, render_template, redirect, session
from markupsafe import escape
import mysql.connector
import re
import requests
import secrets
import base64
import sys
from werkzeug.serving import run_simple
import jinja2, re, hashlib
from jinja2 import Template
import os
from flask import send_from_directory
from flask_mysqldb import MySQL
app = Flask(__name__)
app.secret_key = secrets.token_hex(32)
app.config['DEBUG'] = True
app.config['MYSQL_HOST'] = 'db'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '415263aA.!'
app.config['MYSQL_DB'] = 'blog'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor' # Kullanıcı dostu bir imleç
# Bağlantı havuzu oluştur
mysql = MySQL(app)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/', methods=['GET'])
def promotion():
return render_template('index.html')
@app.route('/home', methods=['GET'])
def home():
try:
# Bağlantıyı al
cursor = mysql.connection.cursor()
# SQL sorgusu
query = "SELECT * FROM posts"
cursor.execute(query)
# Sonuçları al
posts = cursor.fetchall()
# Bağlantıyı kapat
cursor.close()
if request.method == 'GET':
return render_template('anasayfa.html', posts=posts, title='Ana Sayfa')
except Exception as e:
return str(e)
@app.route('/<id>', methods=['GET'])
def posts(id):
blacklist = re.search('(sleep|and|or|=)', str(id), re.IGNORECASE)
if blacklist:
return render_template('blocklist.html', title='Error Page')
else:
try:
# Bağlantıyı al
cursor = mysql.connection.cursor()
# SQL sorgusu
query = "SELECT * FROM posts WHERE id REGEXP %s" % id
cursor.execute(query)
# Sonuçları al
posts = cursor.fetchall()
# Bağlantıyı kapat
cursor.close()
return render_template('anasayfa.html', posts=posts, title='Post')
except Exception as e:
return str(e)
@app.route('/about/1', methods=['GET', 'POST'])
def about():
try:
# Bağlantıyı al
cursor = mysql.connection.cursor()
# SQL sorgusu
query = "SELECT * FROM about WHERE id=1"
cursor.execute(query)
# Sonuçları al
abouts = cursor.fetchall()
# Bağlantıyı kapat
cursor.close()
header = request.headers.get('X-Forwarded-For', '')
if request.method == 'GET':
return render_template('hakkimda.html', abouts=abouts, title='Kimim ben?')
elif request.method == 'POST' and header:
return str(requests.get(header).text)
except Exception as e:
return str(e)
@app.route('/about/<int:id>', methods=['GET'])
def abouts(id):
blacklist = re.search('(union|and|or|sleep|=)', str(id), re.IGNORECASE)
if blacklist:
return render_template('blocklist.html', title='Hooop')
else:
try:
# Bağlantıyı havuzdan al
cursor = mysql.connection.cursor()
# SQL sorgusu
query = "SELECT * FROM about WHERE id = %s"
cursor.execute(query, (id,))
# Sonuçları al
abouts = cursor.fetchall()
# Bağlantıyı kapat
cursor.close()
return render_template('hakkimda.html', abouts=abouts, title='Hakkımda')
except Exception as e:
# Hata durumunda geri bildirim
return str(e)
@app.route('/admin', methods=['GET', 'POST'])
def admin():
if 'user_id' in session:
ping = request.args.get('echo')
if ping:
args = shlex.split(ping)
try:
result = subprocess.check_output(args, shell=False, text=True, stderr=subprocess.STDOUT)
return render_template('admin.html', title=result)
except subprocess.CalledProcessError as e:
return render_template('admin.html', title=f'Hata: {e.output}')
else:
return render_template('admin.html', title='Kabuk komutunun çıktısı:')
return render_template('anasayfa.html', title='Admin Sayfası')
@app.route('/login', methods=['GET', 'POST'])
def login():
url = request.args.get('next', '')
if 'mail' in session:
return redirect(url_for('home'))
if request.method == 'POST':
if request.form['mail'] and request.form['password']:
try:
# Bağlantıyı al
cursor = mysql.connection.cursor()
# SQL sorgusu
query = "SELECT * FROM users WHERE mail = %s AND password = %s"
cursor.execute(query, (request.form['mail'], request.form['password'],))
# Sonuçları al
user = cursor.fetchone()
# Bağlantıyı kapat
cursor.close()
if user:
session['user_id'] = user['mail']
session['kul_id'] = user['id']
return redirect(url_for('admin'))
except Exception as e:
return str(e)
if url:
return redirect(url)
return render_template('login.html', title='Login Sayfası')
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
search_query = request.form["search"]
blacklist = re.search('(script|SCRIPT)', str(search_query))
if blacklist:
return render_template('blocklist.html', title='Hooop')
else:
try:
# Bağlantıyı al
cursor = mysql.connection.cursor()
# SQL sorgusu
sql_query = "SELECT * FROM posts WHERE title LIKE %s"
cursor.execute(sql_query, ('%' + search_query + '%',))
# Sonuçları al
results = cursor.fetchall()
# Bağlantıyı kapat
cursor.close()
if results:
return render_template("search.html", posts=results)
else:
return render_template("search.html", search_query=search_query, template=render_template_string(search_query))
except Exception as e:
return str(e)
return render_template("search.html", posts=[])
@app.route('/logout')
def logout():
session.pop('user_id', None)
return redirect(url_for('home'))
tweets = []
@app.route('/xss', methods=['GET', 'POST'])
def xss():
global tweets
if request.method == 'POST':
tweet = request.form['tweet']
tweets.append(tweet)
elif request.method == 'GET':
tweets = []
return render_template('xss.html', title='XSS Sayfası', tweets=tweets)
@app.route('/robots.txt')
def robots():
return render_template('robots.txt')
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=6619,debug=True)