{"id":1410,"date":"2015-09-29T01:14:38","date_gmt":"2015-09-28T23:14:38","guid":{"rendered":"https:\/\/0x90r00t.com\/fr\/?p=1410"},"modified":"2015-09-29T01:35:06","modified_gmt":"2015-09-28T23:35:06","slug":"trend-micro-2015-programming-200-write-up","status":"publish","type":"post","link":"https:\/\/0x90r00t.com\/fr\/2015\/09\/29\/trend-micro-2015-programming-200-write-up\/","title":{"rendered":"[Trend Micro 2015] [Programming 200] Write-Up"},"content":{"rendered":"<h2>Description<\/h2>\n<blockquote><p>Calculate it.<br \/>\nnc ctfquest.trendmicro.co.jp 51740<\/p><\/blockquote>\n<p><!--more--><\/p>\n<h2>Resolution<\/h2>\n<p>La r\u00e8gle est simple, il faut calculer ce que le serveur nous donne comme calculs.<\/p>\n<p>Au d\u00e9but \u00e7a commence par de simples op\u00e9rations, puis avec des nombres n\u00e9gatifs, puis des virgules pour s\u00e9parer les centaines des milliers, puis des grands nombres, puis .. DES NOMBRES ROMAINS ?! puis &#8230; DES NOMBRES ECRITS DE TOUTES LETTRES EN ANGLAIS ?! puis &#8230; LE TOUT m\u00e9lang\u00e9 ?!!! C&rsquo;est presque du sadisme l\u00e0 \ud83d\ude09<\/p>\n<p>Alors on code :<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/\/ Pour convertir les nombres romains en INT\r\n\/\/ From http:\/\/stackoverflow.com\/questions\/1077600\/converting-words-to-numbers-in-php#answer-11219737\r\nfunction roman($roman) {\r\n$romans = array(\r\n    'M' =&gt; 1000,\r\n    'CM' =&gt; 900,\r\n    'D' =&gt; 500,\r\n    'CD' =&gt; 400,\r\n    'C' =&gt; 100,\r\n    'XC' =&gt; 90,\r\n    'L' =&gt; 50,\r\n    'XL' =&gt; 40,\r\n    'X' =&gt; 10,\r\n    'IX' =&gt; 9,\r\n    'V' =&gt; 5,\r\n    'IV' =&gt; 4,\r\n    'I' =&gt; 1,\r\n);\r\n\r\n$result = 0;\r\n\r\nforeach ($romans as $key =&gt; $value) {\r\n    while (strpos($roman, $key) === 0) {\r\n        $result += $value;\r\n        $roman = substr($roman, strlen($key));\r\n    }\r\n}\r\nreturn $result;\r\n}\r\n\r\n\/\/ Pour convertir les nombres anglais en INT\r\n\/\/ From http:\/\/stackoverflow.com\/questions\/6265596\/how-to-convert-a-roman-numeral-to-integer-in-php#answer-6266158\r\nfunction wordsToNumber($data) {\r\n    \/\/ Replace all number words with an equivalent numeric value\r\n    $data = strtr(\r\n        $data,\r\n        array(\r\n            'zero'      =&gt; '0',\r\n            'a'         =&gt; '1',\r\n            'one'       =&gt; '1',\r\n            'two'       =&gt; '2',\r\n            'three'     =&gt; '3',\r\n            'four'      =&gt; '4',\r\n            'five'      =&gt; '5',\r\n            'six'       =&gt; '6',\r\n            'seven'     =&gt; '7',\r\n            'eight'     =&gt; '8',\r\n            'nine'      =&gt; '9',\r\n            'ten'       =&gt; '10',\r\n            'eleven'    =&gt; '11',\r\n            'twelve'    =&gt; '12',\r\n            'thirteen'  =&gt; '13',\r\n            'fourteen'  =&gt; '14',\r\n            'fifteen'   =&gt; '15',\r\n            'sixteen'   =&gt; '16',\r\n            'seventeen' =&gt; '17',\r\n            'eighteen'  =&gt; '18',\r\n            'nineteen'  =&gt; '19',\r\n            'twenty'    =&gt; '20',\r\n            'thirty'    =&gt; '30',\r\n            'forty'     =&gt; '40',\r\n            'fourty'    =&gt; '40', \/\/ common misspelling\r\n            'fifty'     =&gt; '50',\r\n            'sixty'     =&gt; '60',\r\n            'seventy'   =&gt; '70',\r\n            'eighty'    =&gt; '80',\r\n            'ninety'    =&gt; '90',\r\n            'hundred'   =&gt; '100',\r\n            'thousand'  =&gt; '1000',\r\n            'million'   =&gt; '1000000',\r\n            'billion'   =&gt; '1000000000',\r\n            'and'       =&gt; '',\r\n        )\r\n    );\r\n\r\n    \/\/ Coerce all tokens to numbers\r\n    $parts = array_map(\r\n        function ($val) {\r\n            return floatval($val);\r\n        },\r\n        preg_split('\/&#x5B;\\s-]+\/', $data)\r\n    );\r\n\r\n    $stack = new SplStack; \/\/ Current work stack\r\n    $sum   = 0; \/\/ Running total\r\n    $last  = null;\r\n\r\n    foreach ($parts as $part) {\r\n        if (!$stack-&gt;isEmpty()) {\r\n            \/\/ We're part way through a phrase\r\n            if ($stack-&gt;top() &gt; $part) {\r\n                \/\/ Decreasing step, e.g. from hundreds to ones\r\n                if ($last &gt;= 1000) {\r\n                    \/\/ If we drop from more than 1000 then we've finished the phrase\r\n                    $sum += $stack-&gt;pop();\r\n                    \/\/ This is the first element of a new phrase\r\n                    $stack-&gt;push($part);\r\n                } else {\r\n                    \/\/ Drop down from less than 1000, just addition\r\n                    \/\/ e.g. &quot;seventy one&quot; -&gt; &quot;70 1&quot; -&gt; &quot;70 + 1&quot;\r\n                    $stack-&gt;push($stack-&gt;pop() + $part);\r\n                }\r\n            } else {\r\n                \/\/ Increasing step, e.g ones to hundreds\r\n                $stack-&gt;push($stack-&gt;pop() * $part);\r\n            }\r\n        } else {\r\n            \/\/ This is the first element of a new phrase\r\n            $stack-&gt;push($part);\r\n        }\r\n\r\n        \/\/ Store the last processed part\r\n        $last = $part;\r\n    }\r\n\r\n    return $sum + $stack-&gt;pop();\r\n}\r\n\r\n \/\/ Finalement notre code ;)\r\n $socket = fsockopen('ctfquest.trendmicro.co.jp', 51740);\r\n \r\n while (!feof($socket)) { \/\/ Tant qu'il y a des choses \u00e0 lire\r\n  $line = '';\r\n  while (($char = fgetc($socket)) != '=') { \/\/ On r\u00e9cup\u00e8re chaque caract\u00e8re tant qu'on n'arrive pas au signe \u00e9gal\r\n   $line .= $char;\r\n   echo $char;\r\n  }\r\n  fgetc($socket); \/\/ L'espace apr\u00e8s le \u00e9gal\r\n  \r\n  $calc = str_replace(',', '', rtrim($line, ' =')); \/\/ On vire la b\u00eatise de s\u00e9paration entre les centaines et les milliers\r\n  $calc = preg_replace_callback('#(&#x5B;A-Z]+)#', function($regs){ return roman($regs&#x5B;1]); }, $calc); \/\/ On remplace les nombres romains par leur valeur en int\r\n  $calc = preg_replace_callback('#(&#x5B;a-z]&#x5B;a-z ]+)#', function($regs){ return wordsToNumber(trim($regs&#x5B;1])).' '; }, $calc); \/\/ On remplace les nombre en toutes lettres par leur valeur en int\r\n  \r\n  $result = trim(shell_exec('echo '.escapeshellarg($calc).' | bc')); \/\/ On utilise bc pour calculer\r\n  \r\n  if ($result !== false) {\r\n   echo &quot;$line = '$result'\\n&quot;;\r\n  \r\n   fputs($socket, $result.&quot;\\r\\n&quot;);\r\n  }\r\n }\r\n<\/pre>\n<p>Le flag est : TMCTF{U D1D 17!}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description Calculate it. nc ctfquest.trendmicro.co.jp 51740<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,35,95],"tags":[99,96,100,54,55],"class_list":["post-1410","post","type-post","status-publish","format-standard","hentry","category-2015-fr","category-ctf-fr","category-trend-micro-2015-fr","tag-bc","tag-programmation","tag-romains","tag-php","tag-regexp"],"_links":{"self":[{"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/posts\/1410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/comments?post=1410"}],"version-history":[{"count":3,"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/posts\/1410\/revisions"}],"predecessor-version":[{"id":1415,"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/posts\/1410\/revisions\/1415"}],"wp:attachment":[{"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/media?parent=1410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/categories?post=1410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/0x90r00t.com\/fr\/wp-json\/wp\/v2\/tags?post=1410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}