-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.py
More file actions
163 lines (139 loc) · 7.42 KB
/
clean.py
File metadata and controls
163 lines (139 loc) · 7.42 KB
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
def clean(a):
out = ""
for j in range(len(a) - 1):
i = a[j]
if i == "{":
out += "{{ '{' }}"
elif i == "}":
out += "{{ '}' }}"
else:
out += i
return out
a = """
<h3 id="question-1">Question 1</h3>
<blockquote>
<p>What is the value of Flag1?</p>
</blockquote>
<pre><code class="lang-php"><span class="hljs-keyword">include</span> <span class="hljs-string">"connection.php"</span>;
$query=<span class="hljs-string">"select * from users where id="</span>.intval($_GET[<span class="hljs-string">'id'</span>]);
$elves_rs=mysqli_query($db,$query);
<span class="hljs-keyword">if</span>(!$elves_rs)
{
<span class="hljs-keyword">echo</span> <span class="hljs-string">"<font color=red size=10>Error: Invalid SQL Query</font>"</span>;
<span class="hljs-keyword">die</span>($query);
}
<span class="hljs-comment">// Get the first result. There should be a single elf here.</span>
$elf=mysqli_fetch_assoc($elves_rs);
<span class="hljs-comment">//Now get the toys associated to this elf</span>
$query=<span class="hljs-string">"select * from toys where creator_id="</span>.intval($_GET[<span class="hljs-string">'id'</span>]);
$toys_rs=mysqli_query($db,$query);
<span class="hljs-keyword">if</span>(!$toys_rs)
{
<span class="hljs-keyword">echo</span> <span class="hljs-string">"<font color=red size=10>Error: Invalid SQL Query</font>"</span>;
<span class="hljs-keyword">die</span>($query);
}
</code></pre>
<p><img src="https://github.com/AtomicMaya/knowledge-base/blob/main/writeup_resources/aoc4/day16/1.png?raw=true" alt=""></p>
<p>Answer: <code>THM{McCode, Elf McCode}</code></p>
<h3 id="question-2">Question 2</h3>
<blockquote>
<p>What is the value of Flag2?</p>
</blockquote>
<pre><code class="lang-php">$query=<span class="hljs-string">"select * from toys where name like ? or description like ?"</span>;
$stmt = mysqli_prepare($db, $query);
$q = <span class="hljs-string">"%"</span>.$_GET[<span class="hljs-string">'q'</span>].<span class="hljs-string">"%"</span>;
mysqli_stmt_bind_param($stmt, <span class="hljs-string">'ss'</span>, $q, $q);
mysqli_stmt_execute($stmt);
$toys_rs=mysqli_stmt_get_result($stmt);
<span class="hljs-keyword">if</span>(!$toys_rs)
{
<span class="hljs-keyword">echo</span> <span class="hljs-string">"<font color=red size=10>Error: Invalid SQL Query</font>"</span>;
<span class="hljs-keyword">die</span>($query);
}
</code></pre>
<p><img src="https://github.com/AtomicMaya/knowledge-base/blob/main/writeup_resources/aoc4/day16/2.png?raw=true" alt=""></p>
<p>Answer: <code>THM{KodeNRoll}</code></p>
<h3 id="question-3">Question 3</h3>
<blockquote>
<p>What is the value of Flag3?</p>
</blockquote>
<pre><code class="lang-php"> <span class="hljs-keyword">include</span> <span class="hljs-string">"connection.php"</span>;
$query=<span class="hljs-string">"select * from toys where id="</span>.intval($_GET[<span class="hljs-string">'id'</span>]);
$toys_rs=mysqli_query($db,$query);
<span class="hljs-keyword">if</span>(!$toys_rs)
{
<span class="hljs-keyword">echo</span> <span class="hljs-string">"<font color=red size=10>Error: Invalid SQL Query</font>"</span>;
<span class="hljs-keyword">die</span>($query);
}
<span class="hljs-comment">// Get the first result. There should be a single elf here.</span>
$toy=mysqli_fetch_assoc($toys_rs);
<span class="hljs-comment">//query info on the creator elf</span>
$query=<span class="hljs-string">"select * from users where id="</span>.intval($toy[<span class="hljs-string">'creator_id'</span>]);
$elves_rs=mysqli_query($db,$query);
<span class="hljs-keyword">if</span>(!$elves_rs)
{
<span class="hljs-keyword">echo</span> <span class="hljs-string">"<font color=red size=10>Error: Invalid SQL Query</font>"</span>;
<span class="hljs-keyword">die</span>($query);
}
<span class="hljs-comment">// Get the first result. There should be a single elf here.</span>
$elf=mysqli_fetch_assoc($elves_rs);
<span class="hljs-comment">//query info on planned deliveries</span>
$query=<span class="hljs-string">"select * from kids where assigned_toy_id="</span>.intval($_GET[<span class="hljs-string">'id'</span>]);
$kids_rs=mysqli_query($db,$query);
<span class="hljs-keyword">if</span>(!$kids_rs)
{
<span class="hljs-keyword">echo</span> <span class="hljs-string">"<font color=red size=10>Error: Invalid SQL Query</font>"</span>;
<span class="hljs-keyword">die</span>($query);
}
</code></pre>
<p><img src="https://github.com/AtomicMaya/knowledge-base/blob/main/writeup_resources/aoc4/day16/3.png?raw=true" alt=""></p>
<p>Answer: <code>THM{Are we secure yet?}</code></p>
<h3 id="question-4">Question 4</h3>
<blockquote>
<p>What is the value of Flag4?</p>
</blockquote>
<pre><code class="lang-php"><span class="hljs-keyword">if</span>(<span class="hljs-keyword">isset</span>($_POST[<span class="hljs-string">'username'</span>]) && <span class="hljs-keyword">isset</span>($_POST[<span class="hljs-string">'password'</span>])){
$username=$_POST[<span class="hljs-string">'username'</span>];
$password=$_POST[<span class="hljs-string">'password'</span>];
$query=<span class="hljs-string">"select * from users where username=? and password=?"</span>;
$stmt = mysqli_prepare($db, $query);
mysqli_stmt_bind_param($stmt, <span class="hljs-string">'ss'</span>, $username, $password);
mysqli_stmt_execute($stmt);
$users_rs=mysqli_stmt_get_result($stmt);
<span class="hljs-keyword">if</span>(mysqli_num_rows($users_rs)><span class="hljs-number">0</span>)
{
$_SESSION[<span class="hljs-string">'username'</span>]=$username;
<span class="hljs-keyword">echo</span> <span class="hljs-string">"<script>window.location='admin.php';</script>"</span>;
}
<span class="hljs-keyword">else</span>
{
$message=<span class="hljs-string">"Incorrect username/password found!"</span>;
<span class="hljs-keyword">echo</span> <span class="hljs-string">"<script type='text/javascript'>alert('$message');</script>"</span>;
}
}
</code></pre>
<p><img src="https://github.com/AtomicMaya/knowledge-base/blob/main/writeup_resources/aoc4/day16/4.png?raw=true" alt=""></p>
<p>Answer: <code>THM{SQLi_who???}</code></p>
"""
day = 16
from bs4 import BeautifulSoup as bs
from bs4 import Tag
soup = bs(clean(a), features="html.parser", preserve_whitespace_tags=["p", "em", "span", "h3"])
for h3 in soup.find_all('h3'):
h3.attrs = {}
img = soup.find_all('img')
parags = list(filter(lambda p: len(p.find_all('img')) > 0, soup.find_all('p')))
v = 0
for p in parags:
img = p.find_all('img')
link = img[0].attrs['src']
link = link.replace(f'https://github.com/AtomicMaya/knowledge-base/blob/main/writeup_resources/aoc4/day{day}/', f'assets/images/walkthrough/thm/aoc-2022/day{day:02d}/').split('?')[0]
p.parent.insert(p.parent.index(p)+1, Tag(builder=soup.builder, name='div', attrs={'class': 'media-container'}))
div = soup.find_all('div', attrs={'class': 'media-container'})[v]
div.insert(0, Tag(builder=soup.builder, name='img', attrs={ 'alt': '', 'style': "max-height: 300px", 'src': link, 'loading':"lazy"}))
div.insert(1, Tag(builder=soup.builder, name='span'))
span = div.find_all('span')[0]
span.insert(0, Tag(builder=soup.builder, name='em'))
p.replaceWith('')
v += 1
print(soup.prettify())