-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunhackable.py
266 lines (212 loc) · 5.74 KB
/
unhackable.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
# imports
from flask import Flask, render_template, session, request, redirect
import secrets
import sqlite3
import re
# initialize database
conn = sqlite3.connect('local.db')
print("Opened database successfully")
conn.execute('CREATE TABLE if not exists posts (postID TEXT, session TEXT, content TEXT, comments TEXT)')
print("Posts table created successfully")
conn.close()
# initialize flask
app = Flask(__name__)
app.secret_key = open("secret_key.txt", "r").read()
app.config["SESSION_COOKIE_HTTPONLY"] = False
# home page
@app.route('/', methods=['GET'])
def home():
# if you're a new tester, we'll make you a brand new id!
if 'id' not in session:
session['id'] = secrets.token_hex(32)
# get all posts
posts = []
con = sqlite3.connect("local.db")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(f"SELECT * FROM posts WHERE session = '{session['id']}'")
rows = cur.fetchall()
for row in rows:
posts.append({
'id': row['postID'],
'content': row['content'],
'comments': row['comments'].split("|")
})
# actual HTML for index.html
index = """
<!DOCTYPE html>
<head>
<title>Unhackable</title>
</head>
<body>
<div id="links">
<a href="/new">Write a new post</a> - <a href="#">Find friends</a>
</div>
<div id="title"><h1>Your posts</h1></div>"""
for post in posts:
index += """
<div class="post">"""+post["content"]+"""</div>
<div class="small">ID - <a href="/getpost?id="""+post["id"]+"""">"""+post["id"]+"""<a></div>
<div id="comments">"""
for comment in post["comments"]:
index += """<div class="comment">"""+comment+"""</div>"""
index += """
<div class="comment" id="addComment">
<form action="/addComment" method="post">
<h3>Comment:</h3>
<input name="comment" type="text" placeholder="Add a comment here...">
<input name="postID" type="hidden" value=\""""+post['id']+"""\">
<input type="submit">
</form>
</div>
</div>
<br><br>"""
index += """
</body>
<style>
#title, #links {
text-align:center;
width:100%;
}
.post {
width:700px;
border:1px solid black;
padding:10px;
border-radius:5px;
}
.small {
font-size:0.8em;
font-style:italic;
margin:10px 0;
}
#comments {
margin-left:40px;
}
.comment {
width:660px;
border:1px solid black;
padding:10px;
border-radius:5px;
}
h3 {
margin:0;
}
</style>"""
return index
# form to add a new post
@app.route('/new', methods=['GET'])
def new():
return render_template("new.html")
@app.route('/getpost', methods=['GET'])
def getpost():
# get variables
postid = request.args["id"]
# make sure it's hex
VALID_CHARS = "0123456789abcdef"
for letter in postid:
if letter not in VALID_CHARS:
return redirect('error')
# try to get post
con = sqlite3.connect("local.db")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(f"SELECT * FROM posts WHERE postID = '{postid}' LIMIT 1")
rows = cur.fetchall()
# make sure post exists
if len(rows) == 0:
return redirect('/error')
for row in rows:
content = row['content']
comments = row['comments'].split("|")
content = """
<!DOCTYPE html>
<head>
<title>Unhackable</title>
</head>
<body>
<div id="title"><h1>Your post</h1></div>
<div class="post">"""+content+"""</div>
<div class="small">ID - """+str(postid)+"""</div>
<div id="comments">"""
for comment in comments:
content += """<div class="comment">"""+comment+"""</div>"""
content += """
</div>
<br><br>
</body>
<style>
#title, #links {
text-align:center;
width:100%;
}
.post {
width:700px;
border:1px solid black;
padding:10px;
border-radius:5px;
}
.small {
font-size:0.8em;
font-style:italic;
margin:10px 0;
}
#comments {
margin-left:40px;
}
.comment {
width:660px;
border:1px solid black;
padding:10px;
border-radius:5px;
}
h3 {
margin:0;
}
</style>"""
return content
# actually adds a new post
@app.route('/add', methods=['POST'])
def add():
# get variables
content = request.form['content']
# no SQL injection today!!
if ("'" in content):
return redirect('error')
# add new post
with sqlite3.connect("local.db") as con:
cur = con.cursor()
id = secrets.token_hex(24)
cur.execute(f"INSERT INTO posts (postID, session, content, comments) VALUES ('{id}', '{session['id']}', '{content}', 'Test comment!')")
con.commit()
return redirect('/')
# adds a comment to a post
@app.route('/addComment', methods=['POST'])
def addComment():
# get variables
comment = request.form['comment']
id = request.form['postID']
# no SQL injection today!!
if ("'" in comment) or ('|' in comment):
return redirect('error')
# add comment
con = sqlite3.connect("local.db")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(f"SELECT comments FROM posts WHERE postID = '{id}'")
rows = cur.fetchall()
# make sure post exists!
if len(rows) == 0:
return redirect('/error')
finalComment = ""
for row in rows:
finalComment = row['comments']+"|"+comment
cur.execute(f"UPDATE posts SET comments = '{finalComment}' WHERE postID = '{id}'")
con.commit()
return redirect('/')
# generic error form
@app.route('/error', methods=['GET'])
def error():
return render_template("error.html")
# start application
if __name__ == "__main__":
app.run(host='0.0.0.0', port=40005, threaded=True)