{"id":88,"date":"2019-02-15T13:24:20","date_gmt":"2019-02-15T13:24:20","guid":{"rendered":"http:\/\/robertmccallum.nl\/?p=88"},"modified":"2019-02-17T14:57:48","modified_gmt":"2019-02-17T14:57:48","slug":"juggling-to-win-a-lottery","status":"publish","type":"post","link":"http:\/\/robertmccallum.nl\/index.php\/2019\/02\/15\/juggling-to-win-a-lottery\/","title":{"rendered":"Juggling to win a Lottery!"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">SHU-CTF &#8211; lottery<\/h2>\n\n\n\n<p>Sorry about the Click-bait title.<br>This article is about how PHP type juggling and exploiting the Lottery challenge from <a href=\"http:\/\/120.79.191.75:8000\/challenges\">SHU-CTF<\/a>.<\/p>\n\n\n\n<p>This challenge is hosted at  <a href=\"http:\/\/120.79.191.75\/web-test\/lottery\/index.php\">http:\/\/120.79.191.75\/web-test\/lottery\/index.php<\/a><br>and at the time of writing still online.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Finding the vulnerability <\/h5>\n\n\n\n<p>They were nice enough to provide us with the source code of the challenge. <br>So lets start by checking how things work and see if we can find some bugs.<\/p>\n\n\n\n<p>in api.php there is a function that will compare the  winning lottery numbers against the user picked numbers. (api.php line 80)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function buy($req){\n\trequire_registered();\n\trequire_min_money(2);\n\n\t$money = $_SESSION['money'];\n\t$numbers = $req['numbers'];\n\t$win_numbers = random_win_nums();\n\t$same_count = 0;\n\tfor($i=0; $i&lt;7; $i++){\n\t\tif($numbers[$i] == $win_numbers[$i]){\n\t\t\t$same_count++;\n\t\t}\n\t}\n\tswitch ($same_count) {\n\t\tcase 2:\n\t\t\t$prize = 5;\n\t\t\tbreak;\n\t\tcase 3:\n\t\t\t$prize = 20;\n\t\t\tbreak;\n\t\tcase 4:\n\t\t\t$prize = 300;\n\t\t\tbreak;\n\t\tcase 5:\n\t\t\t$prize = 1800;\n\t\t\tbreak;\n\t\tcase 6:\n\t\t\t$prize = 200000;\n\t\t\tbreak;\n\t\tcase 7:\n\t\t\t$prize = 5000000;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\t$prize = 0;\n\t\t\tbreak;\n\t}\n\t$money += $prize - 2;\n\t$_SESSION['money'] = $money;\n\tresponse(['status'=&gt;'ok','numbers'=&gt;$numbers, 'win_numbers'=&gt;$win_numbers, 'money'=&gt;$money, 'prize'=&gt;$prize]);\n}<\/pre>\n\n\n\n<p>The vulnerability lays in the comparison of the following line.  A loose comparison == is used instead of a strict comparison ===<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if($numbers[$i] == $win_numbers[$i]){ $same_count++; }<br><\/pre>\n\n\n\n<figure class=\"wp-block-image is-resized\"><a href=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/phploose.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/phploose.png\" alt=\"\" class=\"wp-image-90\" width=\"691\" height=\"389\" srcset=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/phploose.png 921w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/phploose-300x169.png 300w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/phploose-768x433.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/figure>\n\n\n\n<p>This means that if I am able to send <em>true<\/em> instead a number it will match 1,2,3,4,5,6,7,8 and 9 the only number it won&#8217;t match is 0.<\/p>\n\n\n\n<p>The only restrictions on the input is done on the client side.  (buy.php line 7)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;input type=\"text\" name=\"numbers\" id=\"numbers\" minlength=\"7\" maxlength=\"7\" pattern=\"\\d{7}\" required placeholder=\"7 numbers\"&gt;<\/pre>\n\n\n\n<p>and the json is build up and ajax request send in buy.js (line 1)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function buy(){<br> $('#wait').show();<br> $('#result').hide();<br> var input = $('#numbers')[0];<br> if(input.validity.valid){<br> var numbers = input.value;<br> $.ajax({<br>   method: \"POST\",<br>   url: \"api.php\",<br>   dataType: \"json\",<br>   contentType: \"application\/json\", <br>   data: JSON.stringify({ action: \"buy\", numbers: numbers })<br> }).done(function(resp){<br> if(resp.status == 'ok'){<br> show_result(resp);<br> } else {<br> alert(resp.msg);<br> }<br> })<br> } else {<br> alert('invalid');<br> }<br> $('#wait').hide();<br> }<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">exploiting the vulnerability  <\/h5>\n\n\n\n<p>I could just boot up burp suit and intercept and modify the requests.<br>or craft some curl requests. But for the sake of simplicity I&#8217;ll use my browsers debugger.<\/p>\n\n\n\n<p>First get our self a session going by registering at <a href=\"http:\/\/120.79.191.75\/web-test\/lottery\/register.php\">http:\/\/120.79.191.75\/web-test\/lottery\/register.php<\/a> <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"155\" src=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/reg.png\" alt=\"\" class=\"wp-image-91\" srcset=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/reg.png 480w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/reg-300x97.png 300w\" sizes=\"auto, (max-width: 480px) 85vw, 480px\" \/><\/figure>\n\n\n\n<p>After registering it will redirect you to the buy.php page.<br>Hit &#8216;f12&#8217; to open up the debugging console in  your browser.<br><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><a href=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/debug.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/debug-1024x331.png\" alt=\"\" class=\"wp-image-92\" width=\"768\" height=\"248\" srcset=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/debug-1024x331.png 1024w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/debug-300x97.png 300w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/debug-768x248.png 768w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/debug-1200x388.png 1200w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/debug.png 1440w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/figure>\n\n\n\n<p>just enter any 7 numbers into the box next to the buy button on the page.<br>and go to the sources tab in the debugger and open <em>&#8216;js\/buy.js&#8217;<\/em><\/p>\n\n\n\n<p>now change line 12 from:<br><code>data: JSON.stringify({ action: \"buy\", numbers: numbers })<\/code><br>to:<br><code>data: JSON.stringify({ action: \"buy\", numbers: [true,true,true,true,true,true,true] })<\/code><br><\/p>\n\n\n\n<p>press CTRL+S to activate the modified script.<\/p>\n\n\n\n<p>And click the buy button until you have enough money to buy me a beer.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><a href=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/winner.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/winner-1024x330.png\" alt=\"\" class=\"wp-image-93\" width=\"768\" height=\"248\" srcset=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/winner-1024x330.png 1024w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/winner-300x97.png 300w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/winner-768x247.png 768w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/winner-1200x387.png 1200w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/winner.png 1434w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><figcaption>donate bitcoin : 14H2c7q53FhA7FFsahv9i3mknDyBqcdsSj<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>SHU-CTF &#8211; lottery Sorry about the Click-bait title.This article is about how PHP type juggling and exploiting the Lottery challenge from SHU-CTF. This challenge is hosted at http:\/\/120.79.191.75\/web-test\/lottery\/index.phpand at the time of writing still online. Finding the vulnerability They were nice enough to provide us with the source code of the challenge. So lets start &hellip; <a href=\"http:\/\/robertmccallum.nl\/index.php\/2019\/02\/15\/juggling-to-win-a-lottery\/\" class=\"more-link\">Lees <span class=\"screen-reader-text\">&#8220;Juggling to win a Lottery!&#8221;<\/span> verder<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,3],"tags":[23,21,22,8],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-shu-ctf","category-writeups","tag-burpsuit","tag-php","tag-typejuggling","tag-web"],"_links":{"self":[{"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":9,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":102,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions\/102"}],"wp:attachment":[{"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}